1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2014 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
27 /* Forward declarations. */
28 static reloc_howto_type * m32c_reloc_type_lookup
29 (bfd *, bfd_reloc_code_real_type);
30 static void m32c_info_to_howto_rela
31 (bfd *, arelent *, Elf_Internal_Rela *);
32 static bfd_boolean m32c_elf_relocate_section
33 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
34 static bfd_boolean m32c_elf_check_relocs
35 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
36 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
38 char * m32c_get_reloc (long reloc);
39 void dump_symtab (bfd *, void *, void *);
41 static bfd_boolean m32c_elf_relax_section
42 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
45 static reloc_howto_type m32c_elf_howto_table [] =
47 /* This reloc does nothing. */
48 HOWTO (R_M32C_NONE, /* type */
50 0, /* size (0 = byte, 1 = short, 2 = long) */
52 FALSE, /* pc_relative */
54 complain_overflow_bitfield, /* complain_on_overflow */
55 bfd_elf_generic_reloc, /* special_function */
56 "R_M32C_NONE", /* name */
57 FALSE, /* partial_inplace */
60 FALSE), /* pcrel_offset */
62 /* GCC intentionally overflows these next two in order to work
63 around limitations in the addressing modes, so don't complain
65 HOWTO (R_M32C_16, /* type */
67 1, /* size (0 = byte, 1 = short, 2 = long) */
69 FALSE, /* pc_relative */
71 complain_overflow_dont, /* complain_on_overflow */
72 bfd_elf_generic_reloc, /* special_function */
73 "R_M32C_16", /* name */
74 FALSE, /* partial_inplace */
76 0xffff, /* dst_mask */
77 FALSE), /* pcrel_offset */
79 HOWTO (R_M32C_24, /* type */
81 2, /* size (0 = byte, 1 = short, 2 = long) */
83 FALSE, /* pc_relative */
85 complain_overflow_dont, /* complain_on_overflow */
86 bfd_elf_generic_reloc, /* special_function */
87 "R_M32C_24", /* name */
88 FALSE, /* partial_inplace */
90 0xffffff, /* dst_mask */
91 FALSE), /* pcrel_offset */
93 HOWTO (R_M32C_32, /* type */
95 2, /* size (0 = byte, 1 = short, 2 = long) */
97 FALSE, /* pc_relative */
99 complain_overflow_bitfield, /* complain_on_overflow */
100 bfd_elf_generic_reloc, /* special_function */
101 "R_M32C_32", /* name */
102 FALSE, /* partial_inplace */
104 0xffffffff, /* dst_mask */
105 FALSE), /* pcrel_offset */
107 HOWTO (R_M32C_8_PCREL, /* type */
109 0, /* size (0 = byte, 1 = short, 2 = long) */
111 TRUE, /* pc_relative */
113 complain_overflow_signed, /* complain_on_overflow */
114 bfd_elf_generic_reloc, /* special_function */
115 "R_M32C_8_PCREL", /* name */
116 FALSE, /* partial_inplace */
119 TRUE), /* pcrel_offset */
121 HOWTO (R_M32C_16_PCREL, /* type */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
125 TRUE, /* pc_relative */
127 complain_overflow_signed, /* complain_on_overflow */
128 bfd_elf_generic_reloc, /* special_function */
129 "R_M32C_16_PCREL", /* name */
130 FALSE, /* partial_inplace */
132 0xffff, /* dst_mask */
133 TRUE), /* pcrel_offset */
135 HOWTO (R_M32C_8, /* type */
137 0, /* size (0 = byte, 1 = short, 2 = long) */
139 FALSE, /* pc_relative */
141 complain_overflow_unsigned, /* complain_on_overflow */
142 bfd_elf_generic_reloc, /* special_function */
143 "R_M32C_8", /* name */
144 FALSE, /* partial_inplace */
147 FALSE), /* pcrel_offset */
149 HOWTO (R_M32C_LO16, /* type */
151 1, /* size (0 = byte, 1 = short, 2 = long) */
153 FALSE, /* pc_relative */
155 complain_overflow_dont, /* complain_on_overflow */
156 bfd_elf_generic_reloc, /* special_function */
157 "R_M32C_LO16", /* name */
158 FALSE, /* partial_inplace */
160 0xffff, /* dst_mask */
161 FALSE), /* pcrel_offset */
163 HOWTO (R_M32C_HI8, /* type */
165 0, /* size (0 = byte, 1 = short, 2 = long) */
167 FALSE, /* pc_relative */
169 complain_overflow_dont, /* complain_on_overflow */
170 bfd_elf_generic_reloc, /* special_function */
171 "R_M32C_HI8", /* name */
172 FALSE, /* partial_inplace */
175 FALSE), /* pcrel_offset */
177 HOWTO (R_M32C_HI16, /* type */
179 1, /* size (0 = byte, 1 = short, 2 = long) */
181 FALSE, /* pc_relative */
183 complain_overflow_dont, /* complain_on_overflow */
184 bfd_elf_generic_reloc, /* special_function */
185 "R_M32C_HI16", /* name */
186 FALSE, /* partial_inplace */
188 0xffff, /* dst_mask */
189 FALSE), /* pcrel_offset */
191 HOWTO (R_M32C_RL_JUMP, /* type */
193 0, /* size (0 = byte, 1 = short, 2 = long) */
195 FALSE, /* pc_relative */
197 complain_overflow_signed, /* complain_on_overflow */
198 bfd_elf_generic_reloc, /* special_function */
199 "R_M32C_RL_JUMP", /* name */
200 FALSE, /* partial_inplace */
203 FALSE), /* pcrel_offset */
205 HOWTO (R_M32C_RL_1ADDR, /* type */
207 0, /* size (0 = byte, 1 = short, 2 = long) */
209 FALSE, /* pc_relative */
211 complain_overflow_signed, /* complain_on_overflow */
212 bfd_elf_generic_reloc, /* special_function */
213 "R_M32C_RL_1ADDR", /* name */
214 FALSE, /* partial_inplace */
217 FALSE), /* pcrel_offset */
219 HOWTO (R_M32C_RL_2ADDR, /* type */
221 0, /* size (0 = byte, 1 = short, 2 = long) */
223 FALSE, /* pc_relative */
225 complain_overflow_signed, /* complain_on_overflow */
226 bfd_elf_generic_reloc, /* special_function */
227 "R_M32C_RL_2ADDR", /* name */
228 FALSE, /* partial_inplace */
231 FALSE), /* pcrel_offset */
235 /* Map BFD reloc types to M32C ELF reloc types. */
237 struct m32c_reloc_map
239 bfd_reloc_code_real_type bfd_reloc_val;
240 unsigned int m32c_reloc_val;
243 static const struct m32c_reloc_map m32c_reloc_map [] =
245 { BFD_RELOC_NONE, R_M32C_NONE },
246 { BFD_RELOC_16, R_M32C_16 },
247 { BFD_RELOC_24, R_M32C_24 },
248 { BFD_RELOC_32, R_M32C_32 },
249 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
250 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
251 { BFD_RELOC_8, R_M32C_8 },
252 { BFD_RELOC_LO16, R_M32C_LO16 },
253 { BFD_RELOC_HI16, R_M32C_HI16 },
254 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
255 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
256 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
257 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
260 static reloc_howto_type *
261 m32c_reloc_type_lookup
262 (bfd * abfd ATTRIBUTE_UNUSED,
263 bfd_reloc_code_real_type code)
267 for (i = ARRAY_SIZE (m32c_reloc_map); --i;)
268 if (m32c_reloc_map [i].bfd_reloc_val == code)
269 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
274 static reloc_howto_type *
275 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
280 i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
282 if (m32c_elf_howto_table[i].name != NULL
283 && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
284 return &m32c_elf_howto_table[i];
289 /* Set the howto pointer for an M32C ELF reloc. */
292 m32c_info_to_howto_rela
293 (bfd * abfd ATTRIBUTE_UNUSED,
295 Elf_Internal_Rela * dst)
299 r_type = ELF32_R_TYPE (dst->r_info);
300 BFD_ASSERT (r_type < (unsigned int) R_M32C_max);
301 cache_ptr->howto = & m32c_elf_howto_table [r_type];
306 /* Relocate an M32C ELF section.
307 There is some attempt to make this function usable for many architectures,
308 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
309 if only to serve as a learning tool.
311 The RELOCATE_SECTION function is called by the new ELF backend linker
312 to handle the relocations for a section.
314 The relocs are always passed as Rela structures; if the section
315 actually uses Rel structures, the r_addend field will always be
318 This function is responsible for adjusting the section contents as
319 necessary, and (if using Rela relocs and generating a relocatable
320 output file) adjusting the reloc addend as necessary.
322 This function does not have to worry about setting the reloc
323 address or the reloc symbol index.
325 LOCAL_SYMS is a pointer to the swapped in local symbols.
327 LOCAL_SECTIONS is an array giving the section in the input file
328 corresponding to the st_shndx field of each local symbol.
330 The global hash table entry for the global symbols can be found
331 via elf_sym_hashes (input_bfd).
333 When generating relocatable output, this function must handle
334 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
335 going to be the section symbol corresponding to the output
336 section, which means that the addend must be adjusted
340 m32c_elf_relocate_section
341 (bfd * output_bfd ATTRIBUTE_UNUSED,
342 struct bfd_link_info * info,
344 asection * input_section,
346 Elf_Internal_Rela * relocs,
347 Elf_Internal_Sym * local_syms,
348 asection ** local_sections)
350 Elf_Internal_Shdr * symtab_hdr;
351 struct elf_link_hash_entry ** sym_hashes;
352 Elf_Internal_Rela * rel;
353 Elf_Internal_Rela * relend;
357 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
358 sym_hashes = elf_sym_hashes (input_bfd);
359 relend = relocs + input_section->reloc_count;
361 dynobj = elf_hash_table (info)->dynobj;
364 splt = bfd_get_linker_section (dynobj, ".plt");
366 for (rel = relocs; rel < relend; rel ++)
368 reloc_howto_type * howto;
369 unsigned long r_symndx;
370 Elf_Internal_Sym * sym;
372 struct elf_link_hash_entry * h;
374 bfd_reloc_status_type r;
375 const char * name = NULL;
378 r_type = ELF32_R_TYPE (rel->r_info);
380 /* These are only used for relaxing; we don't actually relocate
381 anything with them, so skip them. */
382 if (r_type == R_M32C_RL_JUMP
383 || r_type == R_M32C_RL_1ADDR
384 || r_type == R_M32C_RL_2ADDR)
387 r_symndx = ELF32_R_SYM (rel->r_info);
389 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
395 if (r_symndx < symtab_hdr->sh_info)
397 sym = local_syms + r_symndx;
398 sec = local_sections [r_symndx];
399 relocation = (sec->output_section->vma
403 name = bfd_elf_string_from_elf_section
404 (input_bfd, symtab_hdr->sh_link, sym->st_name);
405 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
409 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
411 while (h->root.type == bfd_link_hash_indirect
412 || h->root.type == bfd_link_hash_warning)
413 h = (struct elf_link_hash_entry *) h->root.u.i.link;
415 name = h->root.root.string;
417 if (h->root.type == bfd_link_hash_defined
418 || h->root.type == bfd_link_hash_defweak)
420 sec = h->root.u.def.section;
421 relocation = (h->root.u.def.value
422 + sec->output_section->vma
423 + sec->output_offset);
425 else if (h->root.type == bfd_link_hash_undefweak)
427 else if (!info->relocatable)
429 if (! ((*info->callbacks->undefined_symbol)
430 (info, h->root.root.string, input_bfd,
431 input_section, rel->r_offset, TRUE)))
436 if (sec != NULL && discarded_section (sec))
437 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
438 rel, 1, relend, howto, 0, contents);
440 if (info->relocatable)
442 /* This is a relocatable link. We don't have to change
443 anything, unless the reloc is against a section symbol,
444 in which case we have to adjust according to where the
445 section symbol winds up in the output section. */
446 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
447 rel->r_addend += sec->output_offset;
451 switch (ELF32_R_TYPE (rel->r_info))
458 plt_offset = &h->plt.offset;
460 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
462 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
463 relocation, *plt_offset);*/
464 if (relocation <= 0xffff)
466 /* If the symbol is in range for a 16-bit address, we should
467 have deallocated the plt entry in relax_section. */
468 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
472 /* If the symbol is out of range for a 16-bit address,
473 we must have allocated a plt entry. */
474 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
476 /* If this is the first time we've processed this symbol,
477 fill in the plt entry with the correct symbol address. */
478 if ((*plt_offset & 1) == 0)
482 x = 0x000000fc; /* jmpf */
483 x |= (relocation << 8) & 0xffffff00;
484 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
488 relocation = (splt->output_section->vma
489 + splt->output_offset
490 + (*plt_offset & -2));
493 char *newname = bfd_malloc (strlen(name)+5);
494 strcpy (newname, name);
495 strcat(newname, ".plt");
496 _bfd_generic_link_add_one_symbol (info,
499 BSF_FUNCTION | BSF_WEAK,
518 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
519 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
520 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
521 relocation, rel->r_addend);
525 printf (" %02x", contents[rel->r_offset+i]);
529 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
530 contents, rel->r_offset, relocation,
533 if (r != bfd_reloc_ok)
535 const char * msg = (const char *) NULL;
539 case bfd_reloc_overflow:
540 r = info->callbacks->reloc_overflow
541 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
542 input_bfd, input_section, rel->r_offset);
545 case bfd_reloc_undefined:
546 r = info->callbacks->undefined_symbol
547 (info, name, input_bfd, input_section, rel->r_offset,
551 case bfd_reloc_outofrange:
552 msg = _("internal error: out of range error");
555 case bfd_reloc_notsupported:
556 msg = _("internal error: unsupported relocation error");
559 case bfd_reloc_dangerous:
560 msg = _("internal error: dangerous relocation");
564 msg = _("internal error: unknown error");
569 r = info->callbacks->warning
570 (info, msg, name, input_bfd, input_section, rel->r_offset);
580 /* We support 16-bit pointers to code above 64k by generating a thunk
581 below 64k containing a JMP instruction to the final address. */
584 m32c_elf_check_relocs
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;
594 bfd_vma *local_plt_offsets;
598 if (info->relocatable)
601 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
602 sym_hashes = elf_sym_hashes (abfd);
603 local_plt_offsets = elf_local_got_offsets (abfd);
605 dynobj = elf_hash_table(info)->dynobj;
607 rel_end = relocs + sec->reloc_count;
608 for (rel = relocs; rel < rel_end; rel++)
610 struct elf_link_hash_entry *h;
611 unsigned long r_symndx;
614 r_symndx = ELF32_R_SYM (rel->r_info);
615 if (r_symndx < symtab_hdr->sh_info)
619 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
620 while (h->root.type == bfd_link_hash_indirect
621 || h->root.type == bfd_link_hash_warning)
622 h = (struct elf_link_hash_entry *) h->root.u.i.link;
624 /* PR15323, ref flags aren't set for references in the same
626 h->root.non_ir_ref = 1;
629 switch (ELF32_R_TYPE (rel->r_info))
631 /* This relocation describes a 16-bit pointer to a function.
632 We may need to allocate a thunk in low memory; reserve memory
636 elf_hash_table (info)->dynobj = dynobj = abfd;
639 splt = bfd_get_linker_section (dynobj, ".plt");
642 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
643 | SEC_IN_MEMORY | SEC_LINKER_CREATED
644 | SEC_READONLY | SEC_CODE);
645 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
648 || ! bfd_set_section_alignment (dynobj, splt, 1))
654 offset = &h->plt.offset;
657 if (local_plt_offsets == NULL)
662 size = symtab_hdr->sh_info * sizeof (bfd_vma);
663 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
664 if (local_plt_offsets == NULL)
666 elf_local_got_offsets (abfd) = local_plt_offsets;
668 for (i = 0; i < symtab_hdr->sh_info; i++)
669 local_plt_offsets[i] = (bfd_vma) -1;
671 offset = &local_plt_offsets[r_symndx];
674 if (*offset == (bfd_vma) -1)
676 *offset = splt->size;
686 /* This must exist if dynobj is ever set. */
689 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
690 struct bfd_link_info *info)
695 /* As an extra sanity check, verify that all plt entries have
698 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
699 && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL)
701 bfd_byte *contents = splt->contents;
702 unsigned int i, size = splt->size;
703 for (i = 0; i < size; i += 4)
705 unsigned int x = bfd_get_32 (dynobj, contents + i);
714 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
715 struct bfd_link_info *info)
720 if (info->relocatable)
723 dynobj = elf_hash_table (info)->dynobj;
727 splt = bfd_get_linker_section (dynobj, ".plt");
728 BFD_ASSERT (splt != NULL);
730 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
731 if (splt->contents == NULL)
737 /* Function to set the ELF flag bits. */
740 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
742 elf_elfheader (abfd)->e_flags = flags;
743 elf_flags_init (abfd) = TRUE;
747 /* Merge backend specific data from an object file to the output
748 object file when linking. */
751 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
753 flagword old_flags, old_partial;
754 flagword new_flags, new_partial;
755 bfd_boolean error = FALSE;
759 new_opt[0] = old_opt[0] = '\0';
760 new_flags = elf_elfheader (ibfd)->e_flags;
761 old_flags = elf_elfheader (obfd)->e_flags;
764 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
765 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
766 bfd_get_filename (ibfd));
769 if (!elf_flags_init (obfd))
771 /* First call, no flags set. */
772 elf_flags_init (obfd) = TRUE;
773 elf_elfheader (obfd)->e_flags = new_flags;
776 else if (new_flags == old_flags)
777 /* Compatible flags are ok. */
780 else /* Possibly incompatible flags. */
782 /* Warn if different cpu is used (allow a specific cpu to override
784 new_partial = (new_flags & EF_M32C_CPU_MASK);
785 old_partial = (old_flags & EF_M32C_CPU_MASK);
786 if (new_partial == old_partial)
793 default: strcat (new_opt, " -m16c"); break;
794 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
795 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
800 default: strcat (old_opt, " -m16c"); break;
801 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
802 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
806 /* Print out any mismatches from above. */
810 (*_bfd_error_handler)
811 (_("%s: compiled with %s and linked with modules compiled with %s"),
812 bfd_get_filename (ibfd), new_opt, old_opt);
815 new_flags &= ~ EF_M32C_ALL_FLAGS;
816 old_flags &= ~ EF_M32C_ALL_FLAGS;
818 /* Warn about any other mismatches. */
819 if (new_flags != old_flags)
822 (*_bfd_error_handler)
823 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
824 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
829 bfd_set_error (bfd_error_bad_value);
836 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
838 FILE *file = (FILE *) ptr;
841 BFD_ASSERT (abfd != NULL && ptr != NULL);
843 /* Print normal ELF private data. */
844 _bfd_elf_print_private_bfd_data (abfd, ptr);
846 flags = elf_elfheader (abfd)->e_flags;
847 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
849 switch (flags & EF_M32C_CPU_MASK)
852 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
853 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
860 /* Return the MACH for an e_flags value. */
863 elf32_m32c_machine (bfd *abfd)
865 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
867 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
868 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
871 return bfd_mach_m16c;
875 m32c_elf_object_p (bfd *abfd)
877 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
878 elf32_m32c_machine (abfd));
885 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
888 Elf_Internal_Sym *isymbuf;
889 Elf_Internal_Sym *isymend;
890 Elf_Internal_Sym *isym;
891 Elf_Internal_Shdr *symtab_hdr;
892 bfd_boolean free_internal = 0, free_external = 0;
894 char * st_info_stb_str;
900 internal_syms = bfd_malloc (1000);
905 external_syms = bfd_malloc (1000);
909 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
910 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
912 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
913 symtab_hdr->sh_info, 0,
914 internal_syms, external_syms, NULL);
916 isymbuf = internal_syms;
917 isymend = isymbuf + locsymcount;
919 for (isym = isymbuf ; isym < isymend ; isym++)
921 switch (ELF_ST_TYPE (isym->st_info))
924 st_info_str = "STT_FUNC";
928 st_info_str = "STT_SECTION";
932 st_info_str = "STT_FILE";
936 st_info_str = "STT_OBJECT";
940 st_info_str = "STT_TLS";
947 switch (ELF_ST_BIND (isym->st_info))
950 st_info_stb_str = "STB_LOCAL";
954 st_info_stb_str = "STB_GLOBAL";
958 st_info_stb_str = "";
961 switch (ELF_ST_VISIBILITY (isym->st_other))
964 st_other_str = "STV_DEFAULT";
968 st_other_str = "STV_INTERNAL";
972 st_other_str = "STV_PROTECTED";
979 switch (isym->st_shndx)
982 st_shndx_str = "SHN_ABS";
986 st_shndx_str = "SHN_COMMON";
990 st_shndx_str = "SHN_UNDEF";
997 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
998 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1000 (unsigned long) isym->st_value,
1001 (unsigned long) isym->st_size,
1003 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1005 isym->st_info, st_info_str, st_info_stb_str,
1006 isym->st_other, st_other_str,
1007 isym->st_shndx, st_shndx_str);
1010 free (internal_syms);
1012 free (external_syms);
1016 m32c_get_reloc (long reloc)
1018 if (0 <= reloc && reloc < R_M32C_max)
1019 return m32c_elf_howto_table[reloc].name;
1025 /* Handle relaxing. */
1027 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1028 is within the low 64k, remove any entry for it in the plt. */
1030 struct relax_plt_data
1037 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1039 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1041 if (h->plt.offset != (bfd_vma) -1)
1045 if (h->root.type == bfd_link_hash_undefined
1046 || h->root.type == bfd_link_hash_undefweak)
1049 address = (h->root.u.def.section->output_section->vma
1050 + h->root.u.def.section->output_offset
1051 + h->root.u.def.value);
1053 if (address <= 0xffff)
1056 data->splt->size -= 4;
1057 *data->again = TRUE;
1064 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1065 previously had a plt entry, give it a new entry offset. */
1068 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1070 bfd_vma *entry = (bfd_vma *) xdata;
1072 if (h->plt.offset != (bfd_vma) -1)
1074 h->plt.offset = *entry;
1082 m32c_elf_relax_plt_section (asection *splt,
1083 struct bfd_link_info *info,
1086 struct relax_plt_data relax_plt_data;
1089 /* Assume nothing changes. */
1092 if (info->relocatable)
1095 /* Quick check for an empty plt. */
1096 if (splt->size == 0)
1099 /* Map across all global symbols; see which ones happen to
1100 fall in the low 64k. */
1101 relax_plt_data.splt = splt;
1102 relax_plt_data.again = again;
1103 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1106 /* Likewise for local symbols, though that's somewhat less convenient
1107 as we have to walk the list of input bfds and swap in symbol data. */
1108 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1110 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1111 Elf_Internal_Shdr *symtab_hdr;
1112 Elf_Internal_Sym *isymbuf = NULL;
1115 if (! local_plt_offsets)
1118 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1119 if (symtab_hdr->sh_info != 0)
1121 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1122 if (isymbuf == NULL)
1123 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1124 symtab_hdr->sh_info, 0,
1126 if (isymbuf == NULL)
1130 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1132 Elf_Internal_Sym *isym;
1136 if (local_plt_offsets[idx] == (bfd_vma) -1)
1139 isym = &isymbuf[idx];
1140 if (isym->st_shndx == SHN_UNDEF)
1142 else if (isym->st_shndx == SHN_ABS)
1143 tsec = bfd_abs_section_ptr;
1144 else if (isym->st_shndx == SHN_COMMON)
1145 tsec = bfd_com_section_ptr;
1147 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1149 address = (tsec->output_section->vma
1150 + tsec->output_offset
1152 if (address <= 0xffff)
1154 local_plt_offsets[idx] = -1;
1161 && symtab_hdr->contents != (unsigned char *) isymbuf)
1163 if (! info->keep_memory)
1167 /* Cache the symbols for elf_link_input_bfd. */
1168 symtab_hdr->contents = (unsigned char *) isymbuf;
1173 /* If we changed anything, walk the symbols again to reallocate
1174 .plt entry addresses. */
1175 if (*again && splt->size > 0)
1179 elf_link_hash_traverse (elf_hash_table (info),
1180 m32c_relax_plt_realloc, &entry);
1182 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next)
1184 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1185 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1188 if (! local_plt_offsets)
1191 for (idx = 0; idx < nlocals; ++idx)
1192 if (local_plt_offsets[idx] != (bfd_vma) -1)
1194 local_plt_offsets[idx] = entry;
1204 compare_reloc (const void *e1, const void *e2)
1206 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1207 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1209 if (i1->r_offset == i2->r_offset)
1212 return i1->r_offset < i2->r_offset ? -1 : 1;
1215 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1217 m32c_offset_for_reloc (bfd *abfd,
1218 Elf_Internal_Rela *rel,
1219 Elf_Internal_Shdr *symtab_hdr,
1220 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1221 Elf_Internal_Sym *intsyms)
1225 /* Get the value of the symbol referred to by the reloc. */
1226 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1228 /* A local symbol. */
1229 Elf_Internal_Sym *isym;
1232 isym = intsyms + ELF32_R_SYM (rel->r_info);
1233 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1234 symval = isym->st_value;
1236 symval += ssec->output_section->vma
1237 + ssec->output_offset;
1242 struct elf_link_hash_entry *h;
1244 /* An external symbol. */
1245 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1246 h = elf_sym_hashes (abfd)[indx];
1247 BFD_ASSERT (h != NULL);
1249 if (h->root.type != bfd_link_hash_defined
1250 && h->root.type != bfd_link_hash_defweak)
1251 /* This appears to be a reference to an undefined
1252 symbol. Just ignore it--it will be caught by the
1253 regular reloc processing. */
1256 symval = (h->root.u.def.value
1257 + h->root.u.def.section->output_section->vma
1258 + h->root.u.def.section->output_offset);
1263 static int bytes_saved = 0;
1265 static int bytes_to_reloc[] = {
1273 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1275 /* Mask for the number of relocs associated with this insn. */
1276 #define RLA_RELOCS 0x0000000f
1277 /* Number of bytes gas emitted (before gas's relaxing) */
1278 #define RLA_NBYTES 0x00000ff0
1280 /* If the displacement is within the given range and the new encoding
1281 differs from the old encoding (the index), then the insn can be
1282 relaxed to the new encoding. */
1285 unsigned int max_disp;
1286 unsigned char new_encoding;
1289 static EncodingTable m16c_addr_encodings[] = {
1290 { 0, 0, 0 }, /* R0 */
1291 { 0, 0, 1 }, /* R1 */
1292 { 0, 0, 2 }, /* R2 */
1293 { 0, 0, 3 }, /* R3 */
1294 { 0, 0, 4 }, /* A0 */
1295 { 0, 0, 5 }, /* A1 */
1296 { 0, 0, 6 }, /* [A0] */
1297 { 0, 0, 7 }, /* [A1] */
1298 { 1, 0, 6 }, /* udsp:8[A0] */
1299 { 1, 0, 7 }, /* udsp:8[A1] */
1300 { 1, 0, 10 }, /* udsp:8[SB] */
1301 { 1, 0, 11 }, /* sdsp:8[FB] */
1302 { 2, 255, 8 }, /* udsp:16[A0] */
1303 { 2, 255, 9 }, /* udsp:16[A1] */
1304 { 2, 255, 10 }, /* udsp:16[SB] */
1305 { 2, 0, 15 }, /* abs:16 */
1308 static EncodingTable m16c_jmpaddr_encodings[] = {
1309 { 0, 0, 0 }, /* R0 */
1310 { 0, 0, 1 }, /* R1 */
1311 { 0, 0, 2 }, /* R2 */
1312 { 0, 0, 3 }, /* R3 */
1313 { 0, 0, 4 }, /* A0 */
1314 { 0, 0, 5 }, /* A1 */
1315 { 0, 0, 6 }, /* [A0] */
1316 { 0, 0, 7 }, /* [A1] */
1317 { 1, 0, 6 }, /* udsp:8[A0] */
1318 { 1, 0, 7 }, /* udsp:8[A1] */
1319 { 1, 0, 10 }, /* udsp:8[SB] */
1320 { 1, 0, 11 }, /* sdsp:8[FB] */
1321 { 3, 255, 8 }, /* udsp:20[A0] */
1322 { 3, 255, 9 }, /* udsp:20[A1] */
1323 { 2, 255, 10 }, /* udsp:16[SB] */
1324 { 2, 0, 15 }, /* abs:16 */
1327 static EncodingTable m32c_addr_encodings[] = {
1328 { 0, 0, 0 }, /* [A0] */
1329 { 0, 0, 1 }, /* [A1] */
1330 { 0, 0, 2 }, /* A0 */
1331 { 0, 0, 3 }, /* A1 */
1332 { 1, 0, 0 }, /* udsp:8[A0] */
1333 { 1, 0, 1 }, /* udsp:8[A1] */
1334 { 1, 0, 6 }, /* udsp:8[SB] */
1335 { 1, 0, 7 }, /* sdsp:8[FB] */
1336 { 2, 255, 4 }, /* udsp:16[A0] */
1337 { 2, 255, 5 }, /* udsp:16[A1] */
1338 { 2, 255, 6 }, /* udsp:16[SB] */
1339 { 2, 127, 7 }, /* sdsp:16[FB] */
1340 { 3, 65535, 8 }, /* udsp:24[A0] */
1341 { 3, 65535, 9 }, /* udsp:24[A1] */
1342 { 3, 65535, 15 }, /* abs24 */
1343 { 2, 0, 15 }, /* abs16 */
1344 { 0, 0, 16 }, /* R2 */
1345 { 0, 0, 17 }, /* R3 */
1346 { 0, 0, 18 }, /* R0 */
1347 { 0, 0, 19 }, /* R1 */
1363 m32c_elf_relax_section
1366 struct bfd_link_info * link_info,
1367 bfd_boolean * again)
1369 Elf_Internal_Shdr *symtab_hdr;
1370 Elf_Internal_Shdr *shndx_hdr;
1371 Elf_Internal_Rela *internal_relocs;
1372 Elf_Internal_Rela *free_relocs = NULL;
1373 Elf_Internal_Rela *irel, *irelend, *srel;
1374 bfd_byte * contents = NULL;
1375 bfd_byte * free_contents = NULL;
1376 Elf_Internal_Sym *intsyms = NULL;
1377 Elf_Internal_Sym *free_intsyms = NULL;
1378 Elf_External_Sym_Shndx *shndx_buf = NULL;
1381 if (abfd == elf_hash_table (link_info)->dynobj
1382 && (sec->flags & SEC_LINKER_CREATED) != 0
1383 && strcmp (sec->name, ".plt") == 0)
1384 return m32c_elf_relax_plt_section (sec, link_info, again);
1386 /* Assume nothing changes. */
1389 machine = elf32_m32c_machine (abfd);
1391 /* We don't have to do anything for a relocatable link, if
1392 this section does not have relocs, or if this is not a
1394 if (link_info->relocatable
1395 || (sec->flags & SEC_RELOC) == 0
1396 || sec->reloc_count == 0
1397 || (sec->flags & SEC_CODE) == 0)
1400 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1401 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1403 /* Get the section contents. */
1404 if (elf_section_data (sec)->this_hdr.contents != NULL)
1405 contents = elf_section_data (sec)->this_hdr.contents;
1406 /* Go get them off disk. */
1407 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1410 /* Read this BFD's symbols. */
1411 /* Get cached copy if it exists. */
1412 if (symtab_hdr->contents != NULL)
1414 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1418 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1419 symtab_hdr->contents = (bfd_byte *) intsyms;
1422 if (shndx_hdr->sh_size != 0)
1426 amt = symtab_hdr->sh_info;
1427 amt *= sizeof (Elf_External_Sym_Shndx);
1428 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1429 if (shndx_buf == NULL)
1431 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1432 || bfd_bread (shndx_buf, amt, abfd) != amt)
1434 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1437 /* Get a copy of the native relocations. */
1438 internal_relocs = (_bfd_elf_link_read_relocs
1439 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1440 link_info->keep_memory));
1441 if (internal_relocs == NULL)
1443 if (! link_info->keep_memory)
1444 free_relocs = internal_relocs;
1446 /* The RL_ relocs must be just before the operand relocs they go
1447 with, so we must sort them to guarantee this. */
1448 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1451 /* Walk through them looking for relaxing opportunities. */
1452 irelend = internal_relocs + sec->reloc_count;
1454 for (irel = internal_relocs; irel < irelend; irel++)
1457 unsigned char *insn, *gap, *einsn;
1459 bfd_signed_vma pcrel;
1465 EncodingTable *enctbl;
1468 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1469 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1470 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1475 /* There will always be room for the relaxed insn, since it is smaller
1476 than the one it would replace. */
1477 BFD_ASSERT (irel->r_offset < sec->size);
1479 insn = contents + irel->r_offset;
1480 relax_relocs = irel->r_addend % 16;
1482 /* Ok, we only have three relocs we care about, and they're all
1483 fake. The lower four bits of the addend is always the number
1484 of following relocs (hence the qsort above) that are assigned
1485 to this opcode. The next 8 bits of the addend indicates the
1486 number of bytes in the insn. We use the rest of them
1487 ourselves as flags for the more expensive operations (defines
1488 above). The three relocs are:
1490 RL_JUMP: This marks all direct jump insns. We check the
1491 displacement and replace them with shorter jumps if
1492 they're in range. We also use this to find JMP.S
1493 insns and manually shorten them when we delete bytes.
1494 We have to decode these insns to figure out what to
1497 RL_1ADDR: This is a :G or :Q insn, which has a single
1498 "standard" operand. We have to extract the type
1499 field, see if it's a wide displacement, then figure
1500 out if we can replace it with a narrow displacement.
1501 We don't have to decode these insns.
1503 RL_2ADDR: Similarly, but two "standard" operands. Note that
1504 r_addend may still be 1, as standard operands don't
1505 always have displacements. Gas shouldn't give us one
1506 with zero operands, but since we don't know which one
1507 has the displacement, we check them both anyway.
1509 These all point to the beginning of the insn itself, not the
1512 Note that we only relax one step at a time, relying on the
1513 linker to call us repeatedly. Thus, there is no code for
1514 JMP.A->JMP.B although that will happen in two steps.
1515 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1518 /* Get the value of the symbol referred to by the reloc. Just
1519 in case this is the last reloc in the list, use the RL's
1520 addend to choose between this reloc (no addend) or the next
1521 (yes addend, which means at least one following reloc). */
1522 srel = irel + (relax_relocs ? 1 : 0);
1523 symval = OFFSET_FOR_RELOC (srel);
1525 /* Setting gap_size nonzero is the flag which means "something
1529 new_type = ELF32_R_TYPE(srel->r_info);
1531 pc = sec->output_section->vma + sec->output_offset
1533 pcrel = symval - pc + srel->r_addend;
1535 if (machine == bfd_mach_m16c)
1539 switch (ELF32_R_TYPE(irel->r_info))
1542 case R_M32C_RL_JUMP:
1545 case 0xfe: /* jmp.b */
1546 if (pcrel >= 2 && pcrel <= 9)
1548 /* Relax JMP.B -> JMP.S. We need to get rid of
1549 the following reloc though. */
1550 insn[0] = 0x60 | (pcrel - 2);
1551 new_type = R_M32C_NONE;
1552 irel->r_addend = 0x10;
1558 case 0xf4: /* jmp.w */
1559 /* 128 is allowed because it will be one byte closer
1560 after relaxing. Likewise for all other pc-rel
1562 if (pcrel <= 128 && pcrel >= -128)
1564 /* Relax JMP.W -> JMP.B */
1567 new_type = R_M32C_8_PCREL;
1573 case 0xfc: /* jmp.a */
1574 if (pcrel <= 32768 && pcrel >= -32768)
1576 /* Relax JMP.A -> JMP.W */
1580 new_type = R_M32C_16_PCREL;
1586 case 0xfd: /* jsr.a */
1587 if (pcrel <= 32768 && pcrel >= -32768)
1589 /* Relax JSR.A -> JSR.W */
1593 new_type = R_M32C_16_PCREL;
1601 case R_M32C_RL_2ADDR:
1602 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1604 enctbl = m16c_addr_encodings;
1606 enc = (insn[1] >> 4) & 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 << 4;
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];
1620 if (relax_relocs == 2)
1626 case R_M32C_RL_1ADDR:
1627 /* xxxx xxxx xxxx dest [disp] */
1629 enctbl = m16c_addr_encodings;
1632 /* Check the opcode for jumps. We know it's safe to
1633 do this because all 2ADDR insns are at least two
1635 enc = insn[0] * 256 + insn[1];
1642 enctbl = m16c_jmpaddr_encodings;
1646 /* srel, posn, and enc must be set here. */
1648 symval = OFFSET_FOR_RELOC (srel);
1649 enc = insn[1] & 0x0f;
1652 if (srel->r_offset == irel->r_offset + posn
1653 && e->new_encoding != enc
1654 && symval <= e->max_disp)
1657 insn[1] |= e->new_encoding;
1658 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1659 gap = insn + posn + enctbl[e->new_encoding].bytes;
1660 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1666 } /* Ends switch (reloc type) for m16c. */
1668 else /* machine == bfd_mach_m32c */
1672 switch (ELF32_R_TYPE(irel->r_info))
1675 case R_M32C_RL_JUMP:
1678 case 0xbb: /* jmp.b */
1679 if (pcrel >= 2 && pcrel <= 9)
1682 /* Relax JMP.B -> JMP.S. We need to get rid of
1683 the following reloc though. */
1684 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1685 new_type = R_M32C_NONE;
1686 irel->r_addend = 0x10;
1692 case 0xce: /* jmp.w */
1693 if (pcrel <= 128 && pcrel >= -128)
1695 /* Relax JMP.W -> JMP.B */
1698 new_type = R_M32C_8_PCREL;
1704 case 0xcc: /* jmp.a */
1705 if (pcrel <= 32768 && pcrel >= -32768)
1707 /* Relax JMP.A -> JMP.W */
1711 new_type = R_M32C_16_PCREL;
1717 case 0xcd: /* jsr.a */
1718 if (pcrel <= 32768 && pcrel >= -32768)
1720 /* Relax JSR.A -> JSR.W */
1724 new_type = R_M32C_16_PCREL;
1732 case R_M32C_RL_2ADDR:
1733 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1739 /* prefix; remove it as far as the RL reloc is concerned. */
1744 enctbl = m32c_addr_encodings;
1745 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1748 if (srel->r_offset == irel->r_offset + posn
1749 && e->new_encoding != enc
1750 && symval <= e->max_disp)
1753 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1755 einsn[1] |= (e->new_encoding & 0x03) << 4;
1756 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1757 gap = insn + posn + enctbl[e->new_encoding].bytes;
1758 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1761 if (relax_relocs == 2)
1767 case R_M32C_RL_1ADDR:
1768 /* xxxx DDDx DDxx xxxx [disp] */
1774 /* prefix; remove it as far as the RL reloc is concerned. */
1779 enctbl = m32c_addr_encodings;
1782 /* srel, posn, and enc must be set here. */
1784 symval = OFFSET_FOR_RELOC (srel);
1785 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1788 if (srel->r_offset == irel->r_offset + posn
1789 && e->new_encoding != enc
1790 && symval <= e->max_disp)
1793 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1795 einsn[1] |= (e->new_encoding & 0x03) << 6;
1796 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1797 gap = insn + posn + enctbl[e->new_encoding].bytes;
1798 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1804 } /* Ends switch (reloc type) for m32c. */
1812 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1814 /* Note that we've changed the relocs, section contents, etc. */
1815 elf_section_data (sec)->relocs = internal_relocs;
1818 elf_section_data (sec)->this_hdr.contents = contents;
1819 free_contents = NULL;
1821 symtab_hdr->contents = (bfd_byte *) intsyms;
1822 free_intsyms = NULL;
1824 bytes_saved += gap_size;
1826 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1829 } /* next relocation */
1831 if (free_relocs != NULL)
1837 if (free_contents != NULL)
1839 if (! link_info->keep_memory)
1840 free (free_contents);
1841 /* Cache the section contents for elf_link_input_bfd. */
1843 elf_section_data (sec)->this_hdr.contents = contents;
1845 free_contents = NULL;
1848 if (shndx_buf != NULL)
1850 shndx_hdr->contents = NULL;
1854 if (free_intsyms != NULL)
1856 if (! link_info->keep_memory)
1857 free (free_intsyms);
1858 /* Cache the symbols for elf_link_input_bfd. */
1861 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1864 free_intsyms = NULL;
1870 if (free_relocs != NULL)
1872 if (free_contents != NULL)
1873 free (free_contents);
1874 if (shndx_buf != NULL)
1876 shndx_hdr->contents = NULL;
1879 if (free_intsyms != NULL)
1880 free (free_intsyms);
1884 /* Delete some bytes from a section while relaxing. */
1887 m32c_elf_relax_delete_bytes
1893 Elf_Internal_Shdr *symtab_hdr;
1894 Elf_Internal_Shdr *shndx_hdr;
1897 Elf_Internal_Rela *irel;
1898 Elf_Internal_Rela *irelend;
1900 Elf_Internal_Sym *isym;
1901 Elf_Internal_Sym *isymend;
1902 Elf_Internal_Sym *intsyms;
1903 Elf_External_Sym_Shndx *shndx_buf;
1904 Elf_External_Sym_Shndx *shndx;
1905 struct elf_link_hash_entry ** sym_hashes;
1906 struct elf_link_hash_entry ** end_hashes;
1907 unsigned int symcount;
1909 contents = elf_section_data (sec)->this_hdr.contents;
1913 irel = elf_section_data (sec)->relocs;
1914 irelend = irel + sec->reloc_count;
1916 /* Actually delete the bytes. */
1917 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1920 /* Adjust all the relocs. */
1921 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1923 /* Get the new reloc address. */
1924 if (irel->r_offset > addr && irel->r_offset < toaddr)
1925 irel->r_offset -= count;
1927 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1928 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1929 && irel->r_offset + 1 < addr
1930 && irel->r_offset + 7 > addr)
1933 unsigned char *insn = &contents[irel->r_offset];
1935 /* This is a JMP.S, which we have to manually update. */
1936 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1938 if ((*insn & 0xf8) != 0x60)
1944 if ((*insn & 0xce) != 0x4a)
1946 disp = ((disp & 0x30) >> 3) | (disp & 1);
1948 if (irel->r_offset + disp + 2 >= addr+count)
1951 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1953 *insn = (*insn & 0xf8) | disp;
1957 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1963 /* Adjust the local symbols defined in this section. */
1964 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1965 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1967 isymend = isym + symtab_hdr->sh_info;
1969 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1970 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1971 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1974 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1976 /* If the symbol is in the range of memory we just moved, we
1977 have to adjust its value. */
1978 if ((int) isym->st_shndx == sec_shndx
1979 && isym->st_value > addr
1980 && isym->st_value < toaddr)
1982 isym->st_value -= count;
1984 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1985 *end* is in the moved bytes but it's *start* isn't), then we
1986 must adjust its size. */
1987 if ((int) isym->st_shndx == sec_shndx
1988 && isym->st_value < addr
1989 && isym->st_value + isym->st_size > addr
1990 && isym->st_value + isym->st_size < toaddr)
1992 isym->st_size -= count;
1996 /* Now adjust the global symbols defined in this section. */
1997 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1998 - symtab_hdr->sh_info);
1999 sym_hashes = elf_sym_hashes (abfd);
2000 // sym_hashes += symtab_hdr->sh_info;
2001 end_hashes = sym_hashes + symcount;
2003 for (; sym_hashes < end_hashes; sym_hashes ++)
2005 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2008 (sym_hash->root.type == bfd_link_hash_defined
2009 || sym_hash->root.type == bfd_link_hash_defweak)
2010 && sym_hash->root.u.def.section == sec)
2012 if (sym_hash->root.u.def.value > addr
2013 && sym_hash->root.u.def.value < toaddr)
2015 sym_hash->root.u.def.value -= count;
2017 if (sym_hash->root.u.def.value < addr
2018 && sym_hash->root.u.def.value + sym_hash->size > addr
2019 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2021 sym_hash->size -= count;
2029 /* This is for versions of gcc prior to 4.3. */
2031 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
2033 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2040 #define ELF_ARCH bfd_arch_m32c
2041 #define ELF_MACHINE_CODE EM_M32C
2042 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2043 #define ELF_MAXPAGESIZE 0x100
2046 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
2047 #define TARGET_BIG_NAME "elf32-m32c"
2049 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
2050 #define TARGET_LITTLE_NAME "elf32-m32c"
2053 #define elf_info_to_howto_rel NULL
2054 #define elf_info_to_howto m32c_info_to_howto_rela
2055 #define elf_backend_object_p m32c_elf_object_p
2056 #define elf_backend_relocate_section m32c_elf_relocate_section
2057 #define elf_backend_check_relocs m32c_elf_check_relocs
2058 #define elf_backend_object_p m32c_elf_object_p
2059 #define elf_symbol_leading_char ('_')
2060 #define elf_backend_always_size_sections \
2061 m32c_elf_always_size_sections
2062 #define elf_backend_finish_dynamic_sections \
2063 m32c_elf_finish_dynamic_sections
2065 #define elf_backend_can_gc_sections 1
2066 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2068 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2069 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2070 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2071 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2072 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2073 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2075 #include "elf32-target.h"