1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2015 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 if (r_type >= (unsigned int) R_M32C_max)
302 _bfd_error_handler (_("%A: invalid M32C reloc number: %d"), abfd, r_type);
305 cache_ptr->howto = & m32c_elf_howto_table [r_type];
310 /* Relocate an M32C ELF section.
311 There is some attempt to make this function usable for many architectures,
312 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
313 if only to serve as a learning tool.
315 The RELOCATE_SECTION function is called by the new ELF backend linker
316 to handle the relocations for a section.
318 The relocs are always passed as Rela structures; if the section
319 actually uses Rel structures, the r_addend field will always be
322 This function is responsible for adjusting the section contents as
323 necessary, and (if using Rela relocs and generating a relocatable
324 output file) adjusting the reloc addend as necessary.
326 This function does not have to worry about setting the reloc
327 address or the reloc symbol index.
329 LOCAL_SYMS is a pointer to the swapped in local symbols.
331 LOCAL_SECTIONS is an array giving the section in the input file
332 corresponding to the st_shndx field of each local symbol.
334 The global hash table entry for the global symbols can be found
335 via elf_sym_hashes (input_bfd).
337 When generating relocatable output, this function must handle
338 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
339 going to be the section symbol corresponding to the output
340 section, which means that the addend must be adjusted
344 m32c_elf_relocate_section
345 (bfd * output_bfd ATTRIBUTE_UNUSED,
346 struct bfd_link_info * info,
348 asection * input_section,
350 Elf_Internal_Rela * relocs,
351 Elf_Internal_Sym * local_syms,
352 asection ** local_sections)
354 Elf_Internal_Shdr * symtab_hdr;
355 struct elf_link_hash_entry ** sym_hashes;
356 Elf_Internal_Rela * rel;
357 Elf_Internal_Rela * relend;
361 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
362 sym_hashes = elf_sym_hashes (input_bfd);
363 relend = relocs + input_section->reloc_count;
365 dynobj = elf_hash_table (info)->dynobj;
368 splt = bfd_get_linker_section (dynobj, ".plt");
370 for (rel = relocs; rel < relend; rel ++)
372 reloc_howto_type * howto;
373 unsigned long r_symndx;
374 Elf_Internal_Sym * sym;
376 struct elf_link_hash_entry * h;
378 bfd_reloc_status_type r;
379 const char * name = NULL;
382 r_type = ELF32_R_TYPE (rel->r_info);
384 /* These are only used for relaxing; we don't actually relocate
385 anything with them, so skip them. */
386 if (r_type == R_M32C_RL_JUMP
387 || r_type == R_M32C_RL_1ADDR
388 || r_type == R_M32C_RL_2ADDR)
391 r_symndx = ELF32_R_SYM (rel->r_info);
393 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
399 if (r_symndx < symtab_hdr->sh_info)
401 sym = local_syms + r_symndx;
402 sec = local_sections [r_symndx];
403 relocation = (sec->output_section->vma
407 name = bfd_elf_string_from_elf_section
408 (input_bfd, symtab_hdr->sh_link, sym->st_name);
409 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
413 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
415 if (info->wrap_hash != NULL
416 && (input_section->flags & SEC_DEBUGGING) != 0)
417 h = ((struct elf_link_hash_entry *)
418 unwrap_hash_lookup (info, input_bfd, &h->root));
420 while (h->root.type == bfd_link_hash_indirect
421 || h->root.type == bfd_link_hash_warning)
422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
424 name = h->root.root.string;
426 if (h->root.type == bfd_link_hash_defined
427 || h->root.type == bfd_link_hash_defweak)
429 sec = h->root.u.def.section;
430 relocation = (h->root.u.def.value
431 + sec->output_section->vma
432 + sec->output_offset);
434 else if (h->root.type == bfd_link_hash_undefweak)
436 else if (!info->relocatable)
438 if (! ((*info->callbacks->undefined_symbol)
439 (info, h->root.root.string, input_bfd,
440 input_section, rel->r_offset, TRUE)))
445 if (sec != NULL && discarded_section (sec))
446 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
447 rel, 1, relend, howto, 0, contents);
449 if (info->relocatable)
451 /* This is a relocatable link. We don't have to change
452 anything, unless the reloc is against a section symbol,
453 in which case we have to adjust according to where the
454 section symbol winds up in the output section. */
455 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
456 rel->r_addend += sec->output_offset;
460 switch (ELF32_R_TYPE (rel->r_info))
467 plt_offset = &h->plt.offset;
469 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
471 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
472 relocation, *plt_offset);*/
473 if (relocation <= 0xffff)
475 /* If the symbol is in range for a 16-bit address, we should
476 have deallocated the plt entry in relax_section. */
477 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
481 /* If the symbol is out of range for a 16-bit address,
482 we must have allocated a plt entry. */
483 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
485 /* If this is the first time we've processed this symbol,
486 fill in the plt entry with the correct symbol address. */
487 if ((*plt_offset & 1) == 0)
491 x = 0x000000fc; /* jmpf */
492 x |= (relocation << 8) & 0xffffff00;
493 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
497 relocation = (splt->output_section->vma
498 + splt->output_offset
499 + (*plt_offset & -2));
502 char *newname = bfd_malloc (strlen(name)+5);
503 strcpy (newname, name);
504 strcat(newname, ".plt");
505 _bfd_generic_link_add_one_symbol (info,
508 BSF_FUNCTION | BSF_WEAK,
527 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
528 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
529 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
530 relocation, rel->r_addend);
534 printf (" %02x", contents[rel->r_offset+i]);
538 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
539 contents, rel->r_offset, relocation,
542 if (r != bfd_reloc_ok)
544 const char * msg = (const char *) NULL;
548 case bfd_reloc_overflow:
549 r = info->callbacks->reloc_overflow
550 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
551 input_bfd, input_section, rel->r_offset);
554 case bfd_reloc_undefined:
555 r = info->callbacks->undefined_symbol
556 (info, name, input_bfd, input_section, rel->r_offset,
560 case bfd_reloc_outofrange:
561 msg = _("internal error: out of range error");
564 case bfd_reloc_notsupported:
565 msg = _("internal error: unsupported relocation error");
568 case bfd_reloc_dangerous:
569 msg = _("internal error: dangerous relocation");
573 msg = _("internal error: unknown error");
578 r = info->callbacks->warning
579 (info, msg, name, input_bfd, input_section, rel->r_offset);
589 /* We support 16-bit pointers to code above 64k by generating a thunk
590 below 64k containing a JMP instruction to the final address. */
593 m32c_elf_check_relocs
595 struct bfd_link_info * info,
597 const Elf_Internal_Rela * relocs)
599 Elf_Internal_Shdr * symtab_hdr;
600 struct elf_link_hash_entry ** sym_hashes;
601 const Elf_Internal_Rela * rel;
602 const Elf_Internal_Rela * rel_end;
603 bfd_vma *local_plt_offsets;
607 if (info->relocatable)
610 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
611 sym_hashes = elf_sym_hashes (abfd);
612 local_plt_offsets = elf_local_got_offsets (abfd);
614 dynobj = elf_hash_table(info)->dynobj;
616 rel_end = relocs + sec->reloc_count;
617 for (rel = relocs; rel < rel_end; rel++)
619 struct elf_link_hash_entry *h;
620 unsigned long r_symndx;
623 r_symndx = ELF32_R_SYM (rel->r_info);
624 if (r_symndx < symtab_hdr->sh_info)
628 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
629 while (h->root.type == bfd_link_hash_indirect
630 || h->root.type == bfd_link_hash_warning)
631 h = (struct elf_link_hash_entry *) h->root.u.i.link;
633 /* PR15323, ref flags aren't set for references in the same
635 h->root.non_ir_ref = 1;
638 switch (ELF32_R_TYPE (rel->r_info))
640 /* This relocation describes a 16-bit pointer to a function.
641 We may need to allocate a thunk in low memory; reserve memory
645 elf_hash_table (info)->dynobj = dynobj = abfd;
648 splt = bfd_get_linker_section (dynobj, ".plt");
651 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
652 | SEC_IN_MEMORY | SEC_LINKER_CREATED
653 | SEC_READONLY | SEC_CODE);
654 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
657 || ! bfd_set_section_alignment (dynobj, splt, 1))
663 offset = &h->plt.offset;
666 if (local_plt_offsets == NULL)
671 size = symtab_hdr->sh_info * sizeof (bfd_vma);
672 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
673 if (local_plt_offsets == NULL)
675 elf_local_got_offsets (abfd) = local_plt_offsets;
677 for (i = 0; i < symtab_hdr->sh_info; i++)
678 local_plt_offsets[i] = (bfd_vma) -1;
680 offset = &local_plt_offsets[r_symndx];
683 if (*offset == (bfd_vma) -1)
685 *offset = splt->size;
695 /* This must exist if dynobj is ever set. */
698 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
699 struct bfd_link_info *info)
704 /* As an extra sanity check, verify that all plt entries have
707 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
708 && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL)
710 bfd_byte *contents = splt->contents;
711 unsigned int i, size = splt->size;
712 for (i = 0; i < size; i += 4)
714 unsigned int x = bfd_get_32 (dynobj, contents + i);
723 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
724 struct bfd_link_info *info)
729 if (info->relocatable)
732 dynobj = elf_hash_table (info)->dynobj;
736 splt = bfd_get_linker_section (dynobj, ".plt");
737 BFD_ASSERT (splt != NULL);
739 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
740 if (splt->contents == NULL)
746 /* Function to set the ELF flag bits. */
749 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
751 elf_elfheader (abfd)->e_flags = flags;
752 elf_flags_init (abfd) = TRUE;
756 /* Merge backend specific data from an object file to the output
757 object file when linking. */
760 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
762 flagword old_flags, old_partial;
763 flagword new_flags, new_partial;
764 bfd_boolean error = FALSE;
768 new_opt[0] = old_opt[0] = '\0';
769 new_flags = elf_elfheader (ibfd)->e_flags;
770 old_flags = elf_elfheader (obfd)->e_flags;
773 (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
774 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
775 bfd_get_filename (ibfd));
778 if (!elf_flags_init (obfd))
780 /* First call, no flags set. */
781 elf_flags_init (obfd) = TRUE;
782 elf_elfheader (obfd)->e_flags = new_flags;
785 else if (new_flags == old_flags)
786 /* Compatible flags are ok. */
789 else /* Possibly incompatible flags. */
791 /* Warn if different cpu is used (allow a specific cpu to override
793 new_partial = (new_flags & EF_M32C_CPU_MASK);
794 old_partial = (old_flags & EF_M32C_CPU_MASK);
795 if (new_partial == old_partial)
802 default: strcat (new_opt, " -m16c"); break;
803 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
804 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
809 default: strcat (old_opt, " -m16c"); break;
810 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
811 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
815 /* Print out any mismatches from above. */
819 (*_bfd_error_handler)
820 (_("%s: compiled with %s and linked with modules compiled with %s"),
821 bfd_get_filename (ibfd), new_opt, old_opt);
824 new_flags &= ~ EF_M32C_ALL_FLAGS;
825 old_flags &= ~ EF_M32C_ALL_FLAGS;
827 /* Warn about any other mismatches. */
828 if (new_flags != old_flags)
831 (*_bfd_error_handler)
832 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
833 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
838 bfd_set_error (bfd_error_bad_value);
845 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
847 FILE *file = (FILE *) ptr;
850 BFD_ASSERT (abfd != NULL && ptr != NULL);
852 /* Print normal ELF private data. */
853 _bfd_elf_print_private_bfd_data (abfd, ptr);
855 flags = elf_elfheader (abfd)->e_flags;
856 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
858 switch (flags & EF_M32C_CPU_MASK)
861 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
862 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
869 /* Return the MACH for an e_flags value. */
872 elf32_m32c_machine (bfd *abfd)
874 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
876 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
877 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
880 return bfd_mach_m16c;
884 m32c_elf_object_p (bfd *abfd)
886 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
887 elf32_m32c_machine (abfd));
894 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
897 Elf_Internal_Sym *isymbuf;
898 Elf_Internal_Sym *isymend;
899 Elf_Internal_Sym *isym;
900 Elf_Internal_Shdr *symtab_hdr;
901 bfd_boolean free_internal = 0, free_external = 0;
903 char * st_info_stb_str;
909 internal_syms = bfd_malloc (1000);
914 external_syms = bfd_malloc (1000);
918 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
919 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
921 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
922 symtab_hdr->sh_info, 0,
923 internal_syms, external_syms, NULL);
925 isymbuf = internal_syms;
926 isymend = isymbuf + locsymcount;
928 for (isym = isymbuf ; isym < isymend ; isym++)
930 switch (ELF_ST_TYPE (isym->st_info))
933 st_info_str = "STT_FUNC";
937 st_info_str = "STT_SECTION";
941 st_info_str = "STT_FILE";
945 st_info_str = "STT_OBJECT";
949 st_info_str = "STT_TLS";
956 switch (ELF_ST_BIND (isym->st_info))
959 st_info_stb_str = "STB_LOCAL";
963 st_info_stb_str = "STB_GLOBAL";
967 st_info_stb_str = "";
970 switch (ELF_ST_VISIBILITY (isym->st_other))
973 st_other_str = "STV_DEFAULT";
977 st_other_str = "STV_INTERNAL";
981 st_other_str = "STV_PROTECTED";
988 switch (isym->st_shndx)
991 st_shndx_str = "SHN_ABS";
995 st_shndx_str = "SHN_COMMON";
999 st_shndx_str = "SHN_UNDEF";
1006 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1007 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1009 (unsigned long) isym->st_value,
1010 (unsigned long) isym->st_size,
1012 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1014 isym->st_info, st_info_str, st_info_stb_str,
1015 isym->st_other, st_other_str,
1016 isym->st_shndx, st_shndx_str);
1019 free (internal_syms);
1021 free (external_syms);
1025 m32c_get_reloc (long reloc)
1027 if (0 <= reloc && reloc < R_M32C_max)
1028 return m32c_elf_howto_table[reloc].name;
1034 /* Handle relaxing. */
1036 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1037 is within the low 64k, remove any entry for it in the plt. */
1039 struct relax_plt_data
1046 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1048 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1050 if (h->plt.offset != (bfd_vma) -1)
1054 if (h->root.type == bfd_link_hash_undefined
1055 || h->root.type == bfd_link_hash_undefweak)
1058 address = (h->root.u.def.section->output_section->vma
1059 + h->root.u.def.section->output_offset
1060 + h->root.u.def.value);
1062 if (address <= 0xffff)
1065 data->splt->size -= 4;
1066 *data->again = TRUE;
1073 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1074 previously had a plt entry, give it a new entry offset. */
1077 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1079 bfd_vma *entry = (bfd_vma *) xdata;
1081 if (h->plt.offset != (bfd_vma) -1)
1083 h->plt.offset = *entry;
1091 m32c_elf_relax_plt_section (asection *splt,
1092 struct bfd_link_info *info,
1095 struct relax_plt_data relax_plt_data;
1098 /* Assume nothing changes. */
1101 if (info->relocatable)
1104 /* Quick check for an empty plt. */
1105 if (splt->size == 0)
1108 /* Map across all global symbols; see which ones happen to
1109 fall in the low 64k. */
1110 relax_plt_data.splt = splt;
1111 relax_plt_data.again = again;
1112 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1115 /* Likewise for local symbols, though that's somewhat less convenient
1116 as we have to walk the list of input bfds and swap in symbol data. */
1117 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1119 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1120 Elf_Internal_Shdr *symtab_hdr;
1121 Elf_Internal_Sym *isymbuf = NULL;
1124 if (! local_plt_offsets)
1127 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1128 if (symtab_hdr->sh_info != 0)
1130 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1131 if (isymbuf == NULL)
1132 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1133 symtab_hdr->sh_info, 0,
1135 if (isymbuf == NULL)
1139 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1141 Elf_Internal_Sym *isym;
1145 if (local_plt_offsets[idx] == (bfd_vma) -1)
1148 isym = &isymbuf[idx];
1149 if (isym->st_shndx == SHN_UNDEF)
1151 else if (isym->st_shndx == SHN_ABS)
1152 tsec = bfd_abs_section_ptr;
1153 else if (isym->st_shndx == SHN_COMMON)
1154 tsec = bfd_com_section_ptr;
1156 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1158 address = (tsec->output_section->vma
1159 + tsec->output_offset
1161 if (address <= 0xffff)
1163 local_plt_offsets[idx] = -1;
1170 && symtab_hdr->contents != (unsigned char *) isymbuf)
1172 if (! info->keep_memory)
1176 /* Cache the symbols for elf_link_input_bfd. */
1177 symtab_hdr->contents = (unsigned char *) isymbuf;
1182 /* If we changed anything, walk the symbols again to reallocate
1183 .plt entry addresses. */
1184 if (*again && splt->size > 0)
1188 elf_link_hash_traverse (elf_hash_table (info),
1189 m32c_relax_plt_realloc, &entry);
1191 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1193 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1194 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1197 if (! local_plt_offsets)
1200 for (idx = 0; idx < nlocals; ++idx)
1201 if (local_plt_offsets[idx] != (bfd_vma) -1)
1203 local_plt_offsets[idx] = entry;
1213 compare_reloc (const void *e1, const void *e2)
1215 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1216 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1218 if (i1->r_offset == i2->r_offset)
1221 return i1->r_offset < i2->r_offset ? -1 : 1;
1224 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1226 m32c_offset_for_reloc (bfd *abfd,
1227 Elf_Internal_Rela *rel,
1228 Elf_Internal_Shdr *symtab_hdr,
1229 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1230 Elf_Internal_Sym *intsyms)
1234 /* Get the value of the symbol referred to by the reloc. */
1235 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1237 /* A local symbol. */
1238 Elf_Internal_Sym *isym;
1241 isym = intsyms + ELF32_R_SYM (rel->r_info);
1242 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1243 symval = isym->st_value;
1245 symval += ssec->output_section->vma
1246 + ssec->output_offset;
1251 struct elf_link_hash_entry *h;
1253 /* An external symbol. */
1254 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1255 h = elf_sym_hashes (abfd)[indx];
1256 BFD_ASSERT (h != NULL);
1258 if (h->root.type != bfd_link_hash_defined
1259 && h->root.type != bfd_link_hash_defweak)
1260 /* This appears to be a reference to an undefined
1261 symbol. Just ignore it--it will be caught by the
1262 regular reloc processing. */
1265 symval = (h->root.u.def.value
1266 + h->root.u.def.section->output_section->vma
1267 + h->root.u.def.section->output_offset);
1272 static int bytes_saved = 0;
1274 static int bytes_to_reloc[] = {
1282 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1284 /* Mask for the number of relocs associated with this insn. */
1285 #define RLA_RELOCS 0x0000000f
1286 /* Number of bytes gas emitted (before gas's relaxing) */
1287 #define RLA_NBYTES 0x00000ff0
1289 /* If the displacement is within the given range and the new encoding
1290 differs from the old encoding (the index), then the insn can be
1291 relaxed to the new encoding. */
1294 unsigned int max_disp;
1295 unsigned char new_encoding;
1298 static EncodingTable m16c_addr_encodings[] = {
1299 { 0, 0, 0 }, /* R0 */
1300 { 0, 0, 1 }, /* R1 */
1301 { 0, 0, 2 }, /* R2 */
1302 { 0, 0, 3 }, /* R3 */
1303 { 0, 0, 4 }, /* A0 */
1304 { 0, 0, 5 }, /* A1 */
1305 { 0, 0, 6 }, /* [A0] */
1306 { 0, 0, 7 }, /* [A1] */
1307 { 1, 0, 6 }, /* udsp:8[A0] */
1308 { 1, 0, 7 }, /* udsp:8[A1] */
1309 { 1, 0, 10 }, /* udsp:8[SB] */
1310 { 1, 0, 11 }, /* sdsp:8[FB] */
1311 { 2, 255, 8 }, /* udsp:16[A0] */
1312 { 2, 255, 9 }, /* udsp:16[A1] */
1313 { 2, 255, 10 }, /* udsp:16[SB] */
1314 { 2, 0, 15 }, /* abs:16 */
1317 static EncodingTable m16c_jmpaddr_encodings[] = {
1318 { 0, 0, 0 }, /* R0 */
1319 { 0, 0, 1 }, /* R1 */
1320 { 0, 0, 2 }, /* R2 */
1321 { 0, 0, 3 }, /* R3 */
1322 { 0, 0, 4 }, /* A0 */
1323 { 0, 0, 5 }, /* A1 */
1324 { 0, 0, 6 }, /* [A0] */
1325 { 0, 0, 7 }, /* [A1] */
1326 { 1, 0, 6 }, /* udsp:8[A0] */
1327 { 1, 0, 7 }, /* udsp:8[A1] */
1328 { 1, 0, 10 }, /* udsp:8[SB] */
1329 { 1, 0, 11 }, /* sdsp:8[FB] */
1330 { 3, 255, 8 }, /* udsp:20[A0] */
1331 { 3, 255, 9 }, /* udsp:20[A1] */
1332 { 2, 255, 10 }, /* udsp:16[SB] */
1333 { 2, 0, 15 }, /* abs:16 */
1336 static EncodingTable m32c_addr_encodings[] = {
1337 { 0, 0, 0 }, /* [A0] */
1338 { 0, 0, 1 }, /* [A1] */
1339 { 0, 0, 2 }, /* A0 */
1340 { 0, 0, 3 }, /* A1 */
1341 { 1, 0, 0 }, /* udsp:8[A0] */
1342 { 1, 0, 1 }, /* udsp:8[A1] */
1343 { 1, 0, 6 }, /* udsp:8[SB] */
1344 { 1, 0, 7 }, /* sdsp:8[FB] */
1345 { 2, 255, 4 }, /* udsp:16[A0] */
1346 { 2, 255, 5 }, /* udsp:16[A1] */
1347 { 2, 255, 6 }, /* udsp:16[SB] */
1348 { 2, 127, 7 }, /* sdsp:16[FB] */
1349 { 3, 65535, 8 }, /* udsp:24[A0] */
1350 { 3, 65535, 9 }, /* udsp:24[A1] */
1351 { 3, 65535, 15 }, /* abs24 */
1352 { 2, 0, 15 }, /* abs16 */
1353 { 0, 0, 16 }, /* R2 */
1354 { 0, 0, 17 }, /* R3 */
1355 { 0, 0, 18 }, /* R0 */
1356 { 0, 0, 19 }, /* R1 */
1372 m32c_elf_relax_section
1375 struct bfd_link_info * link_info,
1376 bfd_boolean * again)
1378 Elf_Internal_Shdr *symtab_hdr;
1379 Elf_Internal_Shdr *shndx_hdr;
1380 Elf_Internal_Rela *internal_relocs;
1381 Elf_Internal_Rela *free_relocs = NULL;
1382 Elf_Internal_Rela *irel, *irelend, *srel;
1383 bfd_byte * contents = NULL;
1384 bfd_byte * free_contents = NULL;
1385 Elf_Internal_Sym *intsyms = NULL;
1386 Elf_Internal_Sym *free_intsyms = NULL;
1387 Elf_External_Sym_Shndx *shndx_buf = NULL;
1390 if (abfd == elf_hash_table (link_info)->dynobj
1391 && (sec->flags & SEC_LINKER_CREATED) != 0
1392 && strcmp (sec->name, ".plt") == 0)
1393 return m32c_elf_relax_plt_section (sec, link_info, again);
1395 /* Assume nothing changes. */
1398 machine = elf32_m32c_machine (abfd);
1400 /* We don't have to do anything for a relocatable link, if
1401 this section does not have relocs, or if this is not a
1403 if (link_info->relocatable
1404 || (sec->flags & SEC_RELOC) == 0
1405 || sec->reloc_count == 0
1406 || (sec->flags & SEC_CODE) == 0)
1409 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1410 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1412 /* Get the section contents. */
1413 if (elf_section_data (sec)->this_hdr.contents != NULL)
1414 contents = elf_section_data (sec)->this_hdr.contents;
1415 /* Go get them off disk. */
1416 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1419 /* Read this BFD's symbols. */
1420 /* Get cached copy if it exists. */
1421 if (symtab_hdr->contents != NULL)
1423 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1427 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1428 symtab_hdr->contents = (bfd_byte *) intsyms;
1431 if (shndx_hdr->sh_size != 0)
1435 amt = symtab_hdr->sh_info;
1436 amt *= sizeof (Elf_External_Sym_Shndx);
1437 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1438 if (shndx_buf == NULL)
1440 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1441 || bfd_bread (shndx_buf, amt, abfd) != amt)
1443 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1446 /* Get a copy of the native relocations. */
1447 internal_relocs = (_bfd_elf_link_read_relocs
1448 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1449 link_info->keep_memory));
1450 if (internal_relocs == NULL)
1452 if (! link_info->keep_memory)
1453 free_relocs = internal_relocs;
1455 /* The RL_ relocs must be just before the operand relocs they go
1456 with, so we must sort them to guarantee this. */
1457 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1460 /* Walk through them looking for relaxing opportunities. */
1461 irelend = internal_relocs + sec->reloc_count;
1463 for (irel = internal_relocs; irel < irelend; irel++)
1466 unsigned char *insn, *gap, *einsn;
1468 bfd_signed_vma pcrel;
1474 EncodingTable *enctbl;
1477 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1478 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1479 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1484 /* There will always be room for the relaxed insn, since it is smaller
1485 than the one it would replace. */
1486 BFD_ASSERT (irel->r_offset < sec->size);
1488 insn = contents + irel->r_offset;
1489 relax_relocs = irel->r_addend % 16;
1491 /* Ok, we only have three relocs we care about, and they're all
1492 fake. The lower four bits of the addend is always the number
1493 of following relocs (hence the qsort above) that are assigned
1494 to this opcode. The next 8 bits of the addend indicates the
1495 number of bytes in the insn. We use the rest of them
1496 ourselves as flags for the more expensive operations (defines
1497 above). The three relocs are:
1499 RL_JUMP: This marks all direct jump insns. We check the
1500 displacement and replace them with shorter jumps if
1501 they're in range. We also use this to find JMP.S
1502 insns and manually shorten them when we delete bytes.
1503 We have to decode these insns to figure out what to
1506 RL_1ADDR: This is a :G or :Q insn, which has a single
1507 "standard" operand. We have to extract the type
1508 field, see if it's a wide displacement, then figure
1509 out if we can replace it with a narrow displacement.
1510 We don't have to decode these insns.
1512 RL_2ADDR: Similarly, but two "standard" operands. Note that
1513 r_addend may still be 1, as standard operands don't
1514 always have displacements. Gas shouldn't give us one
1515 with zero operands, but since we don't know which one
1516 has the displacement, we check them both anyway.
1518 These all point to the beginning of the insn itself, not the
1521 Note that we only relax one step at a time, relying on the
1522 linker to call us repeatedly. Thus, there is no code for
1523 JMP.A->JMP.B although that will happen in two steps.
1524 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1527 /* Get the value of the symbol referred to by the reloc. Just
1528 in case this is the last reloc in the list, use the RL's
1529 addend to choose between this reloc (no addend) or the next
1530 (yes addend, which means at least one following reloc). */
1531 srel = irel + (relax_relocs ? 1 : 0);
1532 symval = OFFSET_FOR_RELOC (srel);
1534 /* Setting gap_size nonzero is the flag which means "something
1538 new_type = ELF32_R_TYPE(srel->r_info);
1540 pc = sec->output_section->vma + sec->output_offset
1542 pcrel = symval - pc + srel->r_addend;
1544 if (machine == bfd_mach_m16c)
1548 switch (ELF32_R_TYPE(irel->r_info))
1551 case R_M32C_RL_JUMP:
1554 case 0xfe: /* jmp.b */
1555 if (pcrel >= 2 && pcrel <= 9)
1557 /* Relax JMP.B -> JMP.S. We need to get rid of
1558 the following reloc though. */
1559 insn[0] = 0x60 | (pcrel - 2);
1560 new_type = R_M32C_NONE;
1561 irel->r_addend = 0x10;
1567 case 0xf4: /* jmp.w */
1568 /* 128 is allowed because it will be one byte closer
1569 after relaxing. Likewise for all other pc-rel
1571 if (pcrel <= 128 && pcrel >= -128)
1573 /* Relax JMP.W -> JMP.B */
1576 new_type = R_M32C_8_PCREL;
1582 case 0xfc: /* jmp.a */
1583 if (pcrel <= 32768 && pcrel >= -32768)
1585 /* Relax JMP.A -> JMP.W */
1589 new_type = R_M32C_16_PCREL;
1595 case 0xfd: /* jsr.a */
1596 if (pcrel <= 32768 && pcrel >= -32768)
1598 /* Relax JSR.A -> JSR.W */
1602 new_type = R_M32C_16_PCREL;
1610 case R_M32C_RL_2ADDR:
1611 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1613 enctbl = m16c_addr_encodings;
1615 enc = (insn[1] >> 4) & 0x0f;
1618 if (srel->r_offset == irel->r_offset + posn
1619 && e->new_encoding != enc
1620 && symval <= e->max_disp)
1623 insn[1] |= e->new_encoding << 4;
1624 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1625 gap = insn + posn + enctbl[e->new_encoding].bytes;
1626 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1629 if (relax_relocs == 2)
1635 case R_M32C_RL_1ADDR:
1636 /* xxxx xxxx xxxx dest [disp] */
1638 enctbl = m16c_addr_encodings;
1641 /* Check the opcode for jumps. We know it's safe to
1642 do this because all 2ADDR insns are at least two
1644 enc = insn[0] * 256 + insn[1];
1651 enctbl = m16c_jmpaddr_encodings;
1655 /* srel, posn, and enc must be set here. */
1657 symval = OFFSET_FOR_RELOC (srel);
1658 enc = insn[1] & 0x0f;
1661 if (srel->r_offset == irel->r_offset + posn
1662 && e->new_encoding != enc
1663 && symval <= e->max_disp)
1666 insn[1] |= e->new_encoding;
1667 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1668 gap = insn + posn + enctbl[e->new_encoding].bytes;
1669 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1675 } /* Ends switch (reloc type) for m16c. */
1677 else /* machine == bfd_mach_m32c */
1681 switch (ELF32_R_TYPE(irel->r_info))
1684 case R_M32C_RL_JUMP:
1687 case 0xbb: /* jmp.b */
1688 if (pcrel >= 2 && pcrel <= 9)
1691 /* Relax JMP.B -> JMP.S. We need to get rid of
1692 the following reloc though. */
1693 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1694 new_type = R_M32C_NONE;
1695 irel->r_addend = 0x10;
1701 case 0xce: /* jmp.w */
1702 if (pcrel <= 128 && pcrel >= -128)
1704 /* Relax JMP.W -> JMP.B */
1707 new_type = R_M32C_8_PCREL;
1713 case 0xcc: /* jmp.a */
1714 if (pcrel <= 32768 && pcrel >= -32768)
1716 /* Relax JMP.A -> JMP.W */
1720 new_type = R_M32C_16_PCREL;
1726 case 0xcd: /* jsr.a */
1727 if (pcrel <= 32768 && pcrel >= -32768)
1729 /* Relax JSR.A -> JSR.W */
1733 new_type = R_M32C_16_PCREL;
1741 case R_M32C_RL_2ADDR:
1742 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1748 /* prefix; remove it as far as the RL reloc is concerned. */
1753 enctbl = m32c_addr_encodings;
1754 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1757 if (srel->r_offset == irel->r_offset + posn
1758 && e->new_encoding != enc
1759 && symval <= e->max_disp)
1762 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1764 einsn[1] |= (e->new_encoding & 0x03) << 4;
1765 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1766 gap = insn + posn + enctbl[e->new_encoding].bytes;
1767 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1770 if (relax_relocs == 2)
1776 case R_M32C_RL_1ADDR:
1777 /* xxxx DDDx DDxx xxxx [disp] */
1783 /* prefix; remove it as far as the RL reloc is concerned. */
1788 enctbl = m32c_addr_encodings;
1791 /* srel, posn, and enc must be set here. */
1793 symval = OFFSET_FOR_RELOC (srel);
1794 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1797 if (srel->r_offset == irel->r_offset + posn
1798 && e->new_encoding != enc
1799 && symval <= e->max_disp)
1802 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1804 einsn[1] |= (e->new_encoding & 0x03) << 6;
1805 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1806 gap = insn + posn + enctbl[e->new_encoding].bytes;
1807 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1813 } /* Ends switch (reloc type) for m32c. */
1821 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1823 /* Note that we've changed the relocs, section contents, etc. */
1824 elf_section_data (sec)->relocs = internal_relocs;
1827 elf_section_data (sec)->this_hdr.contents = contents;
1828 free_contents = NULL;
1830 symtab_hdr->contents = (bfd_byte *) intsyms;
1831 free_intsyms = NULL;
1833 bytes_saved += gap_size;
1835 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1838 } /* next relocation */
1840 if (free_relocs != NULL)
1846 if (free_contents != NULL)
1848 if (! link_info->keep_memory)
1849 free (free_contents);
1850 /* Cache the section contents for elf_link_input_bfd. */
1852 elf_section_data (sec)->this_hdr.contents = contents;
1854 free_contents = NULL;
1857 if (shndx_buf != NULL)
1859 shndx_hdr->contents = NULL;
1863 if (free_intsyms != NULL)
1865 if (! link_info->keep_memory)
1866 free (free_intsyms);
1867 /* Cache the symbols for elf_link_input_bfd. */
1870 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1873 free_intsyms = NULL;
1879 if (free_relocs != NULL)
1881 if (free_contents != NULL)
1882 free (free_contents);
1883 if (shndx_buf != NULL)
1885 shndx_hdr->contents = NULL;
1888 if (free_intsyms != NULL)
1889 free (free_intsyms);
1893 /* Delete some bytes from a section while relaxing. */
1896 m32c_elf_relax_delete_bytes
1902 Elf_Internal_Shdr *symtab_hdr;
1903 Elf_Internal_Shdr *shndx_hdr;
1906 Elf_Internal_Rela *irel;
1907 Elf_Internal_Rela *irelend;
1909 Elf_Internal_Sym *isym;
1910 Elf_Internal_Sym *isymend;
1911 Elf_Internal_Sym *intsyms;
1912 Elf_External_Sym_Shndx *shndx_buf;
1913 Elf_External_Sym_Shndx *shndx;
1914 struct elf_link_hash_entry ** sym_hashes;
1915 struct elf_link_hash_entry ** end_hashes;
1916 unsigned int symcount;
1918 contents = elf_section_data (sec)->this_hdr.contents;
1922 irel = elf_section_data (sec)->relocs;
1923 irelend = irel + sec->reloc_count;
1925 /* Actually delete the bytes. */
1926 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1929 /* Adjust all the relocs. */
1930 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1932 /* Get the new reloc address. */
1933 if (irel->r_offset > addr && irel->r_offset < toaddr)
1934 irel->r_offset -= count;
1936 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1937 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1938 && irel->r_offset + 1 < addr
1939 && irel->r_offset + 7 > addr)
1942 unsigned char *insn = &contents[irel->r_offset];
1944 /* This is a JMP.S, which we have to manually update. */
1945 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1947 if ((*insn & 0xf8) != 0x60)
1953 if ((*insn & 0xce) != 0x4a)
1955 disp = ((disp & 0x30) >> 3) | (disp & 1);
1957 if (irel->r_offset + disp + 2 >= addr+count)
1960 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
1962 *insn = (*insn & 0xf8) | disp;
1966 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
1972 /* Adjust the local symbols defined in this section. */
1973 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1974 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1976 isymend = isym + symtab_hdr->sh_info;
1978 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1979 shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr;
1980 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1983 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
1985 /* If the symbol is in the range of memory we just moved, we
1986 have to adjust its value. */
1987 if ((int) isym->st_shndx == sec_shndx
1988 && isym->st_value > addr
1989 && isym->st_value < toaddr)
1991 isym->st_value -= count;
1993 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1994 *end* is in the moved bytes but it's *start* isn't), then we
1995 must adjust its size. */
1996 if ((int) isym->st_shndx == sec_shndx
1997 && isym->st_value < addr
1998 && isym->st_value + isym->st_size > addr
1999 && isym->st_value + isym->st_size < toaddr)
2001 isym->st_size -= count;
2005 /* Now adjust the global symbols defined in this section. */
2006 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2007 - symtab_hdr->sh_info);
2008 sym_hashes = elf_sym_hashes (abfd);
2009 // sym_hashes += symtab_hdr->sh_info;
2010 end_hashes = sym_hashes + symcount;
2012 for (; sym_hashes < end_hashes; sym_hashes ++)
2014 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2017 (sym_hash->root.type == bfd_link_hash_defined
2018 || sym_hash->root.type == bfd_link_hash_defweak)
2019 && sym_hash->root.u.def.section == sec)
2021 if (sym_hash->root.u.def.value > addr
2022 && sym_hash->root.u.def.value < toaddr)
2024 sym_hash->root.u.def.value -= count;
2026 if (sym_hash->root.u.def.value < addr
2027 && sym_hash->root.u.def.value + sym_hash->size > addr
2028 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2030 sym_hash->size -= count;
2038 /* This is for versions of gcc prior to 4.3. */
2040 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
2042 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2049 #define ELF_ARCH bfd_arch_m32c
2050 #define ELF_MACHINE_CODE EM_M32C
2051 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2052 #define ELF_MAXPAGESIZE 0x100
2055 #define TARGET_BIG_SYM m32c_elf32_vec
2056 #define TARGET_BIG_NAME "elf32-m32c"
2058 #define TARGET_LITTLE_SYM m32c_elf32_vec
2059 #define TARGET_LITTLE_NAME "elf32-m32c"
2062 #define elf_info_to_howto_rel NULL
2063 #define elf_info_to_howto m32c_info_to_howto_rela
2064 #define elf_backend_object_p m32c_elf_object_p
2065 #define elf_backend_relocate_section m32c_elf_relocate_section
2066 #define elf_backend_check_relocs m32c_elf_check_relocs
2067 #define elf_backend_object_p m32c_elf_object_p
2068 #define elf_symbol_leading_char ('_')
2069 #define elf_backend_always_size_sections \
2070 m32c_elf_always_size_sections
2071 #define elf_backend_finish_dynamic_sections \
2072 m32c_elf_finish_dynamic_sections
2074 #define elf_backend_can_gc_sections 1
2075 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2077 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2078 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2079 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2080 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2081 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2082 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2084 #include "elf32-target.h"