1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
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 bfd_boolean 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);
44 static bfd_reloc_status_type m32c_apply_reloc_24
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static reloc_howto_type m32c_elf_howto_table [] =
50 /* This reloc does nothing. */
51 HOWTO (R_M32C_NONE, /* type */
53 3, /* size (0 = byte, 1 = short, 2 = long) */
55 FALSE, /* pc_relative */
57 complain_overflow_dont, /* complain_on_overflow */
58 bfd_elf_generic_reloc, /* special_function */
59 "R_M32C_NONE", /* name */
60 FALSE, /* partial_inplace */
63 FALSE), /* pcrel_offset */
65 /* GCC intentionally overflows these next two in order to work
66 around limitations in the addressing modes, so don't complain
68 HOWTO (R_M32C_16, /* type */
70 1, /* size (0 = byte, 1 = short, 2 = long) */
72 FALSE, /* pc_relative */
74 complain_overflow_dont, /* complain_on_overflow */
75 bfd_elf_generic_reloc, /* special_function */
76 "R_M32C_16", /* name */
77 FALSE, /* partial_inplace */
79 0xffff, /* dst_mask */
80 FALSE), /* pcrel_offset */
82 HOWTO (R_M32C_24, /* type */
84 2, /* size (0 = byte, 1 = short, 2 = long) */
86 FALSE, /* pc_relative */
88 complain_overflow_dont, /* complain_on_overflow */
89 m32c_apply_reloc_24, /* special_function */
90 "R_M32C_24", /* name */
91 FALSE, /* partial_inplace */
93 0xffffff, /* dst_mask */
94 FALSE), /* pcrel_offset */
96 HOWTO (R_M32C_32, /* type */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
100 FALSE, /* pc_relative */
102 complain_overflow_bitfield, /* complain_on_overflow */
103 bfd_elf_generic_reloc, /* special_function */
104 "R_M32C_32", /* name */
105 FALSE, /* partial_inplace */
107 0xffffffff, /* dst_mask */
108 FALSE), /* pcrel_offset */
110 HOWTO (R_M32C_8_PCREL, /* type */
112 0, /* size (0 = byte, 1 = short, 2 = long) */
114 TRUE, /* pc_relative */
116 complain_overflow_signed, /* complain_on_overflow */
117 bfd_elf_generic_reloc, /* special_function */
118 "R_M32C_8_PCREL", /* name */
119 FALSE, /* partial_inplace */
122 TRUE), /* pcrel_offset */
124 HOWTO (R_M32C_16_PCREL, /* type */
126 1, /* size (0 = byte, 1 = short, 2 = long) */
128 TRUE, /* pc_relative */
130 complain_overflow_signed, /* complain_on_overflow */
131 bfd_elf_generic_reloc, /* special_function */
132 "R_M32C_16_PCREL", /* name */
133 FALSE, /* partial_inplace */
135 0xffff, /* dst_mask */
136 TRUE), /* pcrel_offset */
138 HOWTO (R_M32C_8, /* type */
140 0, /* size (0 = byte, 1 = short, 2 = long) */
142 FALSE, /* pc_relative */
144 complain_overflow_unsigned, /* complain_on_overflow */
145 bfd_elf_generic_reloc, /* special_function */
146 "R_M32C_8", /* name */
147 FALSE, /* partial_inplace */
150 FALSE), /* pcrel_offset */
152 HOWTO (R_M32C_LO16, /* type */
154 1, /* size (0 = byte, 1 = short, 2 = long) */
156 FALSE, /* pc_relative */
158 complain_overflow_dont, /* complain_on_overflow */
159 bfd_elf_generic_reloc, /* special_function */
160 "R_M32C_LO16", /* name */
161 FALSE, /* partial_inplace */
163 0xffff, /* dst_mask */
164 FALSE), /* pcrel_offset */
166 HOWTO (R_M32C_HI8, /* type */
168 0, /* size (0 = byte, 1 = short, 2 = long) */
170 FALSE, /* pc_relative */
172 complain_overflow_dont, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_M32C_HI8", /* name */
175 FALSE, /* partial_inplace */
178 FALSE), /* pcrel_offset */
180 HOWTO (R_M32C_HI16, /* type */
182 1, /* size (0 = byte, 1 = short, 2 = long) */
184 FALSE, /* pc_relative */
186 complain_overflow_dont, /* complain_on_overflow */
187 bfd_elf_generic_reloc, /* special_function */
188 "R_M32C_HI16", /* name */
189 FALSE, /* partial_inplace */
191 0xffff, /* dst_mask */
192 FALSE), /* pcrel_offset */
194 HOWTO (R_M32C_RL_JUMP, /* type */
196 0, /* size (0 = byte, 1 = short, 2 = long) */
198 FALSE, /* pc_relative */
200 complain_overflow_signed, /* complain_on_overflow */
201 bfd_elf_generic_reloc, /* special_function */
202 "R_M32C_RL_JUMP", /* name */
203 FALSE, /* partial_inplace */
206 FALSE), /* pcrel_offset */
208 HOWTO (R_M32C_RL_1ADDR, /* type */
210 0, /* size (0 = byte, 1 = short, 2 = long) */
212 FALSE, /* pc_relative */
214 complain_overflow_signed, /* complain_on_overflow */
215 bfd_elf_generic_reloc, /* special_function */
216 "R_M32C_RL_1ADDR", /* name */
217 FALSE, /* partial_inplace */
220 FALSE), /* pcrel_offset */
222 HOWTO (R_M32C_RL_2ADDR, /* type */
224 0, /* size (0 = byte, 1 = short, 2 = long) */
226 FALSE, /* pc_relative */
228 complain_overflow_signed, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_M32C_RL_2ADDR", /* name */
231 FALSE, /* partial_inplace */
234 FALSE), /* pcrel_offset */
238 /* Map BFD reloc types to M32C ELF reloc types. */
240 struct m32c_reloc_map
242 bfd_reloc_code_real_type bfd_reloc_val;
243 unsigned int m32c_reloc_val;
246 static const struct m32c_reloc_map m32c_reloc_map [] =
248 { BFD_RELOC_NONE, R_M32C_NONE },
249 { BFD_RELOC_16, R_M32C_16 },
250 { BFD_RELOC_24, R_M32C_24 },
251 { BFD_RELOC_32, R_M32C_32 },
252 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
253 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
254 { BFD_RELOC_8, R_M32C_8 },
255 { BFD_RELOC_LO16, R_M32C_LO16 },
256 { BFD_RELOC_HI16, R_M32C_HI16 },
257 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
258 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
259 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
260 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
263 static reloc_howto_type *
264 m32c_reloc_type_lookup
265 (bfd * abfd ATTRIBUTE_UNUSED,
266 bfd_reloc_code_real_type code)
270 for (i = ARRAY_SIZE (m32c_reloc_map); i--;)
271 if (m32c_reloc_map [i].bfd_reloc_val == code)
272 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
277 static reloc_howto_type *
278 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
283 i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
285 if (m32c_elf_howto_table[i].name != NULL
286 && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
287 return &m32c_elf_howto_table[i];
292 /* Set the howto pointer for an M32C ELF reloc. */
295 m32c_info_to_howto_rela (bfd * abfd,
297 Elf_Internal_Rela * dst)
301 r_type = ELF32_R_TYPE (dst->r_info);
302 if (r_type >= (unsigned int) R_M32C_max)
304 /* xgettext:c-format */
305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
307 bfd_set_error (bfd_error_bad_value);
310 cache_ptr->howto = & m32c_elf_howto_table [r_type];
316 /* Apply R_M32C_24 relocations. We have to do this because it's not a
317 power-of-two size, and the generic code may think it overruns the
318 section if it's right at the end.
320 Must return something other than bfd_reloc_continue to avoid the
321 above problem. Typical return values include bfd_reloc_ok or
325 static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
326 arelent *reloc_entry,
328 void *vdata_start ATTRIBUTE_UNUSED,
329 asection *input_section,
330 bfd *ibfd ATTRIBUTE_UNUSED,
331 char **error_msg ATTRIBUTE_UNUSED)
334 bfd_reloc_status_type s;
336 s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
338 input_section, ibfd, error_msg);
339 if (s != bfd_reloc_continue)
342 /* Get symbol value. (Common symbols are special.) */
343 if (bfd_is_com_section (symbol->section))
346 relocation = symbol->value;
348 relocation += symbol->section->output_offset;
350 /* Add in supplied addend. */
351 relocation += reloc_entry->addend;
353 reloc_entry->addend = relocation;
354 reloc_entry->address += input_section->output_offset;
358 /* Relocate an M32C ELF section.
359 There is some attempt to make this function usable for many architectures,
360 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
361 if only to serve as a learning tool.
363 The RELOCATE_SECTION function is called by the new ELF backend linker
364 to handle the relocations for a section.
366 The relocs are always passed as Rela structures; if the section
367 actually uses Rel structures, the r_addend field will always be
370 This function is responsible for adjusting the section contents as
371 necessary, and (if using Rela relocs and generating a relocatable
372 output file) adjusting the reloc addend as necessary.
374 This function does not have to worry about setting the reloc
375 address or the reloc symbol index.
377 LOCAL_SYMS is a pointer to the swapped in local symbols.
379 LOCAL_SECTIONS is an array giving the section in the input file
380 corresponding to the st_shndx field of each local symbol.
382 The global hash table entry for the global symbols can be found
383 via elf_sym_hashes (input_bfd).
385 When generating relocatable output, this function must handle
386 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
387 going to be the section symbol corresponding to the output
388 section, which means that the addend must be adjusted
392 m32c_elf_relocate_section
393 (bfd * output_bfd ATTRIBUTE_UNUSED,
394 struct bfd_link_info * info,
396 asection * input_section,
398 Elf_Internal_Rela * relocs,
399 Elf_Internal_Sym * local_syms,
400 asection ** local_sections)
402 Elf_Internal_Shdr * symtab_hdr;
403 struct elf_link_hash_entry ** sym_hashes;
404 Elf_Internal_Rela * rel;
405 Elf_Internal_Rela * relend;
408 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
409 sym_hashes = elf_sym_hashes (input_bfd);
410 relend = relocs + input_section->reloc_count;
412 splt = elf_hash_table (info)->splt;
414 for (rel = relocs; rel < relend; rel ++)
416 reloc_howto_type * howto;
417 unsigned long r_symndx;
418 Elf_Internal_Sym * sym;
420 struct elf_link_hash_entry * h;
422 bfd_reloc_status_type r;
423 const char * name = NULL;
426 r_type = ELF32_R_TYPE (rel->r_info);
428 /* These are only used for relaxing; we don't actually relocate
429 anything with them, so skip them. */
430 if (r_type == R_M32C_RL_JUMP
431 || r_type == R_M32C_RL_1ADDR
432 || r_type == R_M32C_RL_2ADDR)
435 r_symndx = ELF32_R_SYM (rel->r_info);
437 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
443 if (r_symndx < symtab_hdr->sh_info)
445 sym = local_syms + r_symndx;
446 sec = local_sections [r_symndx];
447 relocation = (sec->output_section->vma
451 name = bfd_elf_string_from_elf_section
452 (input_bfd, symtab_hdr->sh_link, sym->st_name);
453 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
457 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
459 if (info->wrap_hash != NULL
460 && (input_section->flags & SEC_DEBUGGING) != 0)
461 h = ((struct elf_link_hash_entry *)
462 unwrap_hash_lookup (info, input_bfd, &h->root));
464 while (h->root.type == bfd_link_hash_indirect
465 || h->root.type == bfd_link_hash_warning)
466 h = (struct elf_link_hash_entry *) h->root.u.i.link;
468 name = h->root.root.string;
470 if (h->root.type == bfd_link_hash_defined
471 || h->root.type == bfd_link_hash_defweak)
473 sec = h->root.u.def.section;
474 relocation = (h->root.u.def.value
475 + sec->output_section->vma
476 + sec->output_offset);
478 else if (h->root.type == bfd_link_hash_undefweak)
480 else if (!bfd_link_relocatable (info))
481 (*info->callbacks->undefined_symbol) (info, h->root.root.string,
482 input_bfd, input_section,
483 rel->r_offset, TRUE);
486 if (sec != NULL && discarded_section (sec))
487 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
488 rel, 1, relend, howto, 0, contents);
490 if (bfd_link_relocatable (info))
492 /* This is a relocatable link. We don't have to change
493 anything, unless the reloc is against a section symbol,
494 in which case we have to adjust according to where the
495 section symbol winds up in the output section. */
496 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
497 rel->r_addend += sec->output_offset;
501 switch (ELF32_R_TYPE (rel->r_info))
508 plt_offset = &h->plt.offset;
510 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
512 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
513 relocation, *plt_offset);*/
514 if (relocation <= 0xffff)
516 /* If the symbol is in range for a 16-bit address, we should
517 have deallocated the plt entry in relax_section. */
518 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
522 /* If the symbol is out of range for a 16-bit address,
523 we must have allocated a plt entry. */
524 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
526 /* If this is the first time we've processed this symbol,
527 fill in the plt entry with the correct symbol address. */
528 if ((*plt_offset & 1) == 0)
532 x = 0x000000fc; /* jmpf */
533 x |= (relocation << 8) & 0xffffff00;
534 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
538 relocation = (splt->output_section->vma
539 + splt->output_offset
540 + (*plt_offset & -2));
543 char *newname = bfd_malloc (strlen(name)+5);
544 strcpy (newname, name);
545 strcat(newname, ".plt");
546 _bfd_generic_link_add_one_symbol (info,
549 BSF_FUNCTION | BSF_WEAK,
568 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
569 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
570 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
571 relocation, rel->r_addend);
575 printf (" %02x", contents[rel->r_offset+i]);
579 switch (ELF32_R_TYPE(rel->r_info))
582 /* Like m32c_apply_reloc_24, we must handle this one separately. */
583 relocation += rel->r_addend;
585 /* Sanity check the address. */
586 if (rel->r_offset + 3
587 > bfd_get_section_limit_octets (input_bfd, input_section))
588 r = bfd_reloc_outofrange;
591 bfd_put_8 (input_bfd, relocation & 0xff, contents + rel->r_offset);
592 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff, contents + rel->r_offset + 1);
593 bfd_put_8 (input_bfd, (relocation >> 16) & 0xff, contents + rel->r_offset + 2);
600 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
601 contents, rel->r_offset, relocation,
606 if (r != bfd_reloc_ok)
608 const char * msg = (const char *) NULL;
612 case bfd_reloc_overflow:
613 (*info->callbacks->reloc_overflow)
614 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
615 input_bfd, input_section, rel->r_offset);
618 case bfd_reloc_undefined:
619 (*info->callbacks->undefined_symbol)
620 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
623 case bfd_reloc_outofrange:
624 msg = _("internal error: out of range error");
627 case bfd_reloc_notsupported:
628 msg = _("internal error: unsupported relocation error");
631 case bfd_reloc_dangerous:
632 msg = _("internal error: dangerous relocation");
636 msg = _("internal error: unknown error");
641 (*info->callbacks->warning) (info, msg, name, input_bfd,
642 input_section, rel->r_offset);
649 /* We support 16-bit pointers to code above 64k by generating a thunk
650 below 64k containing a JMP instruction to the final address. */
653 m32c_elf_check_relocs
655 struct bfd_link_info * info,
657 const Elf_Internal_Rela * relocs)
659 Elf_Internal_Shdr * symtab_hdr;
660 struct elf_link_hash_entry ** sym_hashes;
661 const Elf_Internal_Rela * rel;
662 const Elf_Internal_Rela * rel_end;
663 bfd_vma *local_plt_offsets;
667 if (bfd_link_relocatable (info))
670 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
671 sym_hashes = elf_sym_hashes (abfd);
672 local_plt_offsets = elf_local_got_offsets (abfd);
674 dynobj = elf_hash_table(info)->dynobj;
676 rel_end = relocs + sec->reloc_count;
677 for (rel = relocs; rel < rel_end; rel++)
679 struct elf_link_hash_entry *h;
680 unsigned long r_symndx;
683 r_symndx = ELF32_R_SYM (rel->r_info);
684 if (r_symndx < symtab_hdr->sh_info)
688 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
689 while (h->root.type == bfd_link_hash_indirect
690 || h->root.type == bfd_link_hash_warning)
691 h = (struct elf_link_hash_entry *) h->root.u.i.link;
694 switch (ELF32_R_TYPE (rel->r_info))
696 /* This relocation describes a 16-bit pointer to a function.
697 We may need to allocate a thunk in low memory; reserve memory
701 elf_hash_table (info)->dynobj = dynobj = abfd;
702 splt = elf_hash_table (info)->splt;
705 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
706 | SEC_IN_MEMORY | SEC_LINKER_CREATED
707 | SEC_READONLY | SEC_CODE);
708 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
710 elf_hash_table (info)->splt = splt;
712 || ! bfd_set_section_alignment (dynobj, splt, 1))
717 offset = &h->plt.offset;
720 if (local_plt_offsets == NULL)
725 size = symtab_hdr->sh_info * sizeof (bfd_vma);
726 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
727 if (local_plt_offsets == NULL)
729 elf_local_got_offsets (abfd) = local_plt_offsets;
731 for (i = 0; i < symtab_hdr->sh_info; i++)
732 local_plt_offsets[i] = (bfd_vma) -1;
734 offset = &local_plt_offsets[r_symndx];
737 if (*offset == (bfd_vma) -1)
739 *offset = splt->size;
749 /* This must exist if dynobj is ever set. */
752 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
753 struct bfd_link_info *info)
755 bfd *dynobj = elf_hash_table (info)->dynobj;
756 asection *splt = elf_hash_table (info)->splt;
758 /* As an extra sanity check, verify that all plt entries have
761 if (dynobj != NULL && splt != NULL)
763 bfd_byte *contents = splt->contents;
764 unsigned int i, size = splt->size;
765 for (i = 0; i < size; i += 4)
767 unsigned int x = bfd_get_32 (dynobj, contents + i);
776 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
777 struct bfd_link_info *info)
782 if (bfd_link_relocatable (info))
785 dynobj = elf_hash_table (info)->dynobj;
789 splt = elf_hash_table (info)->splt;
790 BFD_ASSERT (splt != NULL);
792 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
793 if (splt->contents == NULL)
799 /* Function to set the ELF flag bits. */
802 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
804 elf_elfheader (abfd)->e_flags = flags;
805 elf_flags_init (abfd) = TRUE;
809 /* Merge backend specific data from an object file to the output
810 object file when linking. */
813 m32c_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
815 bfd *obfd = info->output_bfd;
816 flagword old_flags, old_partial;
817 flagword new_flags, new_partial;
818 bfd_boolean error = FALSE;
822 new_opt[0] = old_opt[0] = '\0';
823 new_flags = elf_elfheader (ibfd)->e_flags;
824 old_flags = elf_elfheader (obfd)->e_flags;
828 ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
829 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
830 bfd_get_filename (ibfd));
833 if (!elf_flags_init (obfd))
835 /* First call, no flags set. */
836 elf_flags_init (obfd) = TRUE;
837 elf_elfheader (obfd)->e_flags = new_flags;
840 else if (new_flags == old_flags)
841 /* Compatible flags are ok. */
844 else /* Possibly incompatible flags. */
846 /* Warn if different cpu is used (allow a specific cpu to override
848 new_partial = (new_flags & EF_M32C_CPU_MASK);
849 old_partial = (old_flags & EF_M32C_CPU_MASK);
850 if (new_partial == old_partial)
857 default: strcat (new_opt, " -m16c"); break;
858 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
859 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
864 default: strcat (old_opt, " -m16c"); break;
865 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
866 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
870 /* Print out any mismatches from above. */
875 /* xgettext:c-format */
876 (_("%pB: compiled with %s and linked with modules compiled with %s"),
877 ibfd, new_opt, old_opt);
880 new_flags &= ~ EF_M32C_ALL_FLAGS;
881 old_flags &= ~ EF_M32C_ALL_FLAGS;
883 /* Warn about any other mismatches. */
884 if (new_flags != old_flags)
888 /* xgettext:c-format */
889 (_("%pB: uses different e_flags (%#x) fields"
890 " than previous modules (%#x)"),
891 ibfd, new_flags, old_flags);
896 bfd_set_error (bfd_error_bad_value);
903 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
905 FILE *file = (FILE *) ptr;
908 BFD_ASSERT (abfd != NULL && ptr != NULL);
910 /* Print normal ELF private data. */
911 _bfd_elf_print_private_bfd_data (abfd, ptr);
913 flags = elf_elfheader (abfd)->e_flags;
914 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
916 switch (flags & EF_M32C_CPU_MASK)
919 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
920 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
927 /* Return the MACH for an e_flags value. */
930 elf32_m32c_machine (bfd *abfd)
932 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
934 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
935 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
938 return bfd_mach_m16c;
942 m32c_elf_object_p (bfd *abfd)
944 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
945 elf32_m32c_machine (abfd));
952 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
955 Elf_Internal_Sym *isymbuf;
956 Elf_Internal_Sym *isymend;
957 Elf_Internal_Sym *isym;
958 Elf_Internal_Shdr *symtab_hdr;
959 bfd_boolean free_internal = 0, free_external = 0;
961 char * st_info_stb_str;
967 internal_syms = bfd_malloc (1000);
972 external_syms = bfd_malloc (1000);
976 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
977 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
979 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
980 symtab_hdr->sh_info, 0,
981 internal_syms, external_syms, NULL);
983 isymbuf = internal_syms;
984 isymend = isymbuf + locsymcount;
986 for (isym = isymbuf ; isym < isymend ; isym++)
988 switch (ELF_ST_TYPE (isym->st_info))
991 st_info_str = "STT_FUNC";
995 st_info_str = "STT_SECTION";
999 st_info_str = "STT_FILE";
1003 st_info_str = "STT_OBJECT";
1007 st_info_str = "STT_TLS";
1014 switch (ELF_ST_BIND (isym->st_info))
1017 st_info_stb_str = "STB_LOCAL";
1021 st_info_stb_str = "STB_GLOBAL";
1025 st_info_stb_str = "";
1028 switch (ELF_ST_VISIBILITY (isym->st_other))
1031 st_other_str = "STV_DEFAULT";
1035 st_other_str = "STV_INTERNAL";
1039 st_other_str = "STV_PROTECTED";
1046 switch (isym->st_shndx)
1049 st_shndx_str = "SHN_ABS";
1053 st_shndx_str = "SHN_COMMON";
1057 st_shndx_str = "SHN_UNDEF";
1064 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1065 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1067 (unsigned long) isym->st_value,
1068 (unsigned long) isym->st_size,
1070 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1072 isym->st_info, st_info_str, st_info_stb_str,
1073 isym->st_other, st_other_str,
1074 isym->st_shndx, st_shndx_str);
1077 free (internal_syms);
1079 free (external_syms);
1083 m32c_get_reloc (long reloc)
1085 if (0 <= reloc && reloc < R_M32C_max)
1086 return m32c_elf_howto_table[reloc].name;
1092 /* Handle relaxing. */
1094 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1095 is within the low 64k, remove any entry for it in the plt. */
1097 struct relax_plt_data
1104 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1106 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1108 if (h->plt.offset != (bfd_vma) -1)
1112 if (h->root.type == bfd_link_hash_undefined
1113 || h->root.type == bfd_link_hash_undefweak)
1116 address = (h->root.u.def.section->output_section->vma
1117 + h->root.u.def.section->output_offset
1118 + h->root.u.def.value);
1120 if (address <= 0xffff)
1123 data->splt->size -= 4;
1124 *data->again = TRUE;
1131 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1132 previously had a plt entry, give it a new entry offset. */
1135 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1137 bfd_vma *entry = (bfd_vma *) xdata;
1139 if (h->plt.offset != (bfd_vma) -1)
1141 h->plt.offset = *entry;
1149 m32c_elf_relax_plt_section (asection *splt,
1150 struct bfd_link_info *info,
1153 struct relax_plt_data relax_plt_data;
1156 /* Assume nothing changes. */
1159 if (bfd_link_relocatable (info))
1162 /* Quick check for an empty plt. */
1163 if (splt->size == 0)
1166 /* Map across all global symbols; see which ones happen to
1167 fall in the low 64k. */
1168 relax_plt_data.splt = splt;
1169 relax_plt_data.again = again;
1170 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1173 /* Likewise for local symbols, though that's somewhat less convenient
1174 as we have to walk the list of input bfds and swap in symbol data. */
1175 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1177 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1178 Elf_Internal_Shdr *symtab_hdr;
1179 Elf_Internal_Sym *isymbuf = NULL;
1182 if (! local_plt_offsets)
1185 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1186 if (symtab_hdr->sh_info != 0)
1188 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1189 if (isymbuf == NULL)
1190 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1191 symtab_hdr->sh_info, 0,
1193 if (isymbuf == NULL)
1197 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1199 Elf_Internal_Sym *isym;
1203 if (local_plt_offsets[idx] == (bfd_vma) -1)
1206 isym = &isymbuf[idx];
1207 if (isym->st_shndx == SHN_UNDEF)
1209 else if (isym->st_shndx == SHN_ABS)
1210 tsec = bfd_abs_section_ptr;
1211 else if (isym->st_shndx == SHN_COMMON)
1212 tsec = bfd_com_section_ptr;
1214 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1216 address = (tsec->output_section->vma
1217 + tsec->output_offset
1219 if (address <= 0xffff)
1221 local_plt_offsets[idx] = -1;
1228 && symtab_hdr->contents != (unsigned char *) isymbuf)
1230 if (! info->keep_memory)
1234 /* Cache the symbols for elf_link_input_bfd. */
1235 symtab_hdr->contents = (unsigned char *) isymbuf;
1240 /* If we changed anything, walk the symbols again to reallocate
1241 .plt entry addresses. */
1242 if (*again && splt->size > 0)
1246 elf_link_hash_traverse (elf_hash_table (info),
1247 m32c_relax_plt_realloc, &entry);
1249 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1251 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1252 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1255 if (! local_plt_offsets)
1258 for (idx = 0; idx < nlocals; ++idx)
1259 if (local_plt_offsets[idx] != (bfd_vma) -1)
1261 local_plt_offsets[idx] = entry;
1271 compare_reloc (const void *e1, const void *e2)
1273 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1274 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1276 if (i1->r_offset == i2->r_offset)
1279 return i1->r_offset < i2->r_offset ? -1 : 1;
1282 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1284 m32c_offset_for_reloc (bfd *abfd,
1285 Elf_Internal_Rela *rel,
1286 Elf_Internal_Shdr *symtab_hdr,
1287 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1288 Elf_Internal_Sym *intsyms)
1292 /* Get the value of the symbol referred to by the reloc. */
1293 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1295 /* A local symbol. */
1296 Elf_Internal_Sym *isym;
1299 isym = intsyms + ELF32_R_SYM (rel->r_info);
1300 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1301 symval = isym->st_value;
1303 symval += ssec->output_section->vma
1304 + ssec->output_offset;
1309 struct elf_link_hash_entry *h;
1311 /* An external symbol. */
1312 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1313 h = elf_sym_hashes (abfd)[indx];
1314 BFD_ASSERT (h != NULL);
1316 if (h->root.type != bfd_link_hash_defined
1317 && h->root.type != bfd_link_hash_defweak)
1318 /* This appears to be a reference to an undefined
1319 symbol. Just ignore it--it will be caught by the
1320 regular reloc processing. */
1323 symval = (h->root.u.def.value
1324 + h->root.u.def.section->output_section->vma
1325 + h->root.u.def.section->output_offset);
1330 static int bytes_saved = 0;
1332 static int bytes_to_reloc[] = {
1340 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1342 /* Mask for the number of relocs associated with this insn. */
1343 #define RLA_RELOCS 0x0000000f
1344 /* Number of bytes gas emitted (before gas's relaxing) */
1345 #define RLA_NBYTES 0x00000ff0
1347 /* If the displacement is within the given range and the new encoding
1348 differs from the old encoding (the index), then the insn can be
1349 relaxed to the new encoding. */
1352 unsigned int max_disp;
1353 unsigned char new_encoding;
1356 static EncodingTable m16c_addr_encodings[] = {
1357 { 0, 0, 0 }, /* R0 */
1358 { 0, 0, 1 }, /* R1 */
1359 { 0, 0, 2 }, /* R2 */
1360 { 0, 0, 3 }, /* R3 */
1361 { 0, 0, 4 }, /* A0 */
1362 { 0, 0, 5 }, /* A1 */
1363 { 0, 0, 6 }, /* [A0] */
1364 { 0, 0, 7 }, /* [A1] */
1365 { 1, 0, 6 }, /* udsp:8[A0] */
1366 { 1, 0, 7 }, /* udsp:8[A1] */
1367 { 1, 0, 10 }, /* udsp:8[SB] */
1368 { 1, 0, 11 }, /* sdsp:8[FB] */
1369 { 2, 255, 8 }, /* udsp:16[A0] */
1370 { 2, 255, 9 }, /* udsp:16[A1] */
1371 { 2, 255, 10 }, /* udsp:16[SB] */
1372 { 2, 0, 15 }, /* abs:16 */
1375 static EncodingTable m16c_jmpaddr_encodings[] = {
1376 { 0, 0, 0 }, /* R0 */
1377 { 0, 0, 1 }, /* R1 */
1378 { 0, 0, 2 }, /* R2 */
1379 { 0, 0, 3 }, /* R3 */
1380 { 0, 0, 4 }, /* A0 */
1381 { 0, 0, 5 }, /* A1 */
1382 { 0, 0, 6 }, /* [A0] */
1383 { 0, 0, 7 }, /* [A1] */
1384 { 1, 0, 6 }, /* udsp:8[A0] */
1385 { 1, 0, 7 }, /* udsp:8[A1] */
1386 { 1, 0, 10 }, /* udsp:8[SB] */
1387 { 1, 0, 11 }, /* sdsp:8[FB] */
1388 { 3, 255, 8 }, /* udsp:20[A0] */
1389 { 3, 255, 9 }, /* udsp:20[A1] */
1390 { 2, 255, 10 }, /* udsp:16[SB] */
1391 { 2, 0, 15 }, /* abs:16 */
1394 static EncodingTable m32c_addr_encodings[] = {
1395 { 0, 0, 0 }, /* [A0] */
1396 { 0, 0, 1 }, /* [A1] */
1397 { 0, 0, 2 }, /* A0 */
1398 { 0, 0, 3 }, /* A1 */
1399 { 1, 0, 0 }, /* udsp:8[A0] */
1400 { 1, 0, 1 }, /* udsp:8[A1] */
1401 { 1, 0, 6 }, /* udsp:8[SB] */
1402 { 1, 0, 7 }, /* sdsp:8[FB] */
1403 { 2, 255, 4 }, /* udsp:16[A0] */
1404 { 2, 255, 5 }, /* udsp:16[A1] */
1405 { 2, 255, 6 }, /* udsp:16[SB] */
1406 { 2, 127, 7 }, /* sdsp:16[FB] */
1407 { 3, 65535, 8 }, /* udsp:24[A0] */
1408 { 3, 65535, 9 }, /* udsp:24[A1] */
1409 { 3, 65535, 15 }, /* abs24 */
1410 { 2, 0, 15 }, /* abs16 */
1411 { 0, 0, 16 }, /* R2 */
1412 { 0, 0, 17 }, /* R3 */
1413 { 0, 0, 18 }, /* R0 */
1414 { 0, 0, 19 }, /* R1 */
1430 m32c_elf_relax_section
1433 struct bfd_link_info * link_info,
1434 bfd_boolean * again)
1436 Elf_Internal_Shdr *symtab_hdr;
1437 Elf_Internal_Shdr *shndx_hdr;
1438 Elf_Internal_Rela *internal_relocs;
1439 Elf_Internal_Rela *free_relocs = NULL;
1440 Elf_Internal_Rela *irel, *irelend, *srel;
1441 bfd_byte * contents = NULL;
1442 bfd_byte * free_contents = NULL;
1443 Elf_Internal_Sym *intsyms = NULL;
1444 Elf_Internal_Sym *free_intsyms = NULL;
1445 Elf_External_Sym_Shndx *shndx_buf = NULL;
1448 if (abfd == elf_hash_table (link_info)->dynobj
1449 && (sec->flags & SEC_LINKER_CREATED) != 0
1450 && strcmp (sec->name, ".plt") == 0)
1451 return m32c_elf_relax_plt_section (sec, link_info, again);
1453 /* Assume nothing changes. */
1456 machine = elf32_m32c_machine (abfd);
1458 /* We don't have to do anything for a relocatable link, if
1459 this section does not have relocs, or if this is not a
1461 if (bfd_link_relocatable (link_info)
1462 || (sec->flags & SEC_RELOC) == 0
1463 || sec->reloc_count == 0
1464 || (sec->flags & SEC_CODE) == 0)
1467 symtab_hdr = & elf_symtab_hdr (abfd);
1468 if (elf_symtab_shndx_list (abfd))
1469 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
1473 /* Get the section contents. */
1474 if (elf_section_data (sec)->this_hdr.contents != NULL)
1475 contents = elf_section_data (sec)->this_hdr.contents;
1476 /* Go get them off disk. */
1477 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1480 /* Read this BFD's symbols. */
1481 /* Get cached copy if it exists. */
1482 if (symtab_hdr->contents != NULL)
1484 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1488 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1489 symtab_hdr->contents = (bfd_byte *) intsyms;
1492 if (shndx_hdr && shndx_hdr->sh_size != 0)
1496 amt = symtab_hdr->sh_info;
1497 amt *= sizeof (Elf_External_Sym_Shndx);
1498 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1499 if (shndx_buf == NULL)
1501 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1502 || bfd_bread (shndx_buf, amt, abfd) != amt)
1504 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1507 /* Get a copy of the native relocations. */
1508 internal_relocs = (_bfd_elf_link_read_relocs
1509 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1510 link_info->keep_memory));
1511 if (internal_relocs == NULL)
1513 if (! link_info->keep_memory)
1514 free_relocs = internal_relocs;
1516 /* The RL_ relocs must be just before the operand relocs they go
1517 with, so we must sort them to guarantee this. */
1518 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1521 /* Walk through them looking for relaxing opportunities. */
1522 irelend = internal_relocs + sec->reloc_count;
1524 for (irel = internal_relocs; irel < irelend; irel++)
1527 unsigned char *insn, *gap, *einsn;
1529 bfd_signed_vma pcrel;
1535 EncodingTable *enctbl;
1538 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1539 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1540 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1545 /* There will always be room for the relaxed insn, since it is smaller
1546 than the one it would replace. */
1547 BFD_ASSERT (irel->r_offset < sec->size);
1549 insn = contents + irel->r_offset;
1550 relax_relocs = irel->r_addend % 16;
1552 /* Ok, we only have three relocs we care about, and they're all
1553 fake. The lower four bits of the addend is always the number
1554 of following relocs (hence the qsort above) that are assigned
1555 to this opcode. The next 8 bits of the addend indicates the
1556 number of bytes in the insn. We use the rest of them
1557 ourselves as flags for the more expensive operations (defines
1558 above). The three relocs are:
1560 RL_JUMP: This marks all direct jump insns. We check the
1561 displacement and replace them with shorter jumps if
1562 they're in range. We also use this to find JMP.S
1563 insns and manually shorten them when we delete bytes.
1564 We have to decode these insns to figure out what to
1567 RL_1ADDR: This is a :G or :Q insn, which has a single
1568 "standard" operand. We have to extract the type
1569 field, see if it's a wide displacement, then figure
1570 out if we can replace it with a narrow displacement.
1571 We don't have to decode these insns.
1573 RL_2ADDR: Similarly, but two "standard" operands. Note that
1574 r_addend may still be 1, as standard operands don't
1575 always have displacements. Gas shouldn't give us one
1576 with zero operands, but since we don't know which one
1577 has the displacement, we check them both anyway.
1579 These all point to the beginning of the insn itself, not the
1582 Note that we only relax one step at a time, relying on the
1583 linker to call us repeatedly. Thus, there is no code for
1584 JMP.A->JMP.B although that will happen in two steps.
1585 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1588 /* Get the value of the symbol referred to by the reloc. Just
1589 in case this is the last reloc in the list, use the RL's
1590 addend to choose between this reloc (no addend) or the next
1591 (yes addend, which means at least one following reloc). */
1592 srel = irel + (relax_relocs ? 1 : 0);
1593 symval = OFFSET_FOR_RELOC (srel);
1595 /* Setting gap_size nonzero is the flag which means "something
1599 new_type = ELF32_R_TYPE(srel->r_info);
1601 pc = sec->output_section->vma + sec->output_offset
1603 pcrel = symval - pc + srel->r_addend;
1605 if (machine == bfd_mach_m16c)
1609 switch (ELF32_R_TYPE(irel->r_info))
1612 case R_M32C_RL_JUMP:
1615 case 0xfe: /* jmp.b */
1616 if (pcrel >= 2 && pcrel <= 9)
1618 /* Relax JMP.B -> JMP.S. We need to get rid of
1619 the following reloc though. */
1620 insn[0] = 0x60 | (pcrel - 2);
1621 new_type = R_M32C_NONE;
1622 irel->r_addend = 0x10;
1628 case 0xf4: /* jmp.w */
1629 /* 128 is allowed because it will be one byte closer
1630 after relaxing. Likewise for all other pc-rel
1632 if (pcrel <= 128 && pcrel >= -128)
1634 /* Relax JMP.W -> JMP.B */
1637 new_type = R_M32C_8_PCREL;
1643 case 0xfc: /* jmp.a */
1644 if (pcrel <= 32768 && pcrel >= -32768)
1646 /* Relax JMP.A -> JMP.W */
1650 new_type = R_M32C_16_PCREL;
1656 case 0xfd: /* jsr.a */
1657 if (pcrel <= 32768 && pcrel >= -32768)
1659 /* Relax JSR.A -> JSR.W */
1663 new_type = R_M32C_16_PCREL;
1671 case R_M32C_RL_2ADDR:
1672 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1674 enctbl = m16c_addr_encodings;
1676 enc = (insn[1] >> 4) & 0x0f;
1679 if (srel->r_offset == irel->r_offset + posn
1680 && e->new_encoding != enc
1681 && symval <= e->max_disp)
1684 insn[1] |= e->new_encoding << 4;
1685 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1686 gap = insn + posn + enctbl[e->new_encoding].bytes;
1687 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1690 if (relax_relocs == 2)
1696 case R_M32C_RL_1ADDR:
1697 /* xxxx xxxx xxxx dest [disp] */
1699 enctbl = m16c_addr_encodings;
1702 /* Check the opcode for jumps. We know it's safe to
1703 do this because all 2ADDR insns are at least two
1705 enc = insn[0] * 256 + insn[1];
1712 enctbl = m16c_jmpaddr_encodings;
1716 /* srel, posn, and enc must be set here. */
1718 symval = OFFSET_FOR_RELOC (srel);
1719 enc = insn[1] & 0x0f;
1722 if (srel->r_offset == irel->r_offset + posn
1723 && e->new_encoding != enc
1724 && symval <= e->max_disp)
1727 insn[1] |= e->new_encoding;
1728 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1729 gap = insn + posn + enctbl[e->new_encoding].bytes;
1730 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1736 } /* Ends switch (reloc type) for m16c. */
1738 else /* machine == bfd_mach_m32c */
1742 switch (ELF32_R_TYPE(irel->r_info))
1745 case R_M32C_RL_JUMP:
1748 case 0xbb: /* jmp.b */
1749 if (pcrel >= 2 && pcrel <= 9)
1752 /* Relax JMP.B -> JMP.S. We need to get rid of
1753 the following reloc though. */
1754 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1755 new_type = R_M32C_NONE;
1756 irel->r_addend = 0x10;
1762 case 0xce: /* jmp.w */
1763 if (pcrel <= 128 && pcrel >= -128)
1765 /* Relax JMP.W -> JMP.B */
1768 new_type = R_M32C_8_PCREL;
1774 case 0xcc: /* jmp.a */
1775 if (pcrel <= 32768 && pcrel >= -32768)
1777 /* Relax JMP.A -> JMP.W */
1781 new_type = R_M32C_16_PCREL;
1787 case 0xcd: /* jsr.a */
1788 if (pcrel <= 32768 && pcrel >= -32768)
1790 /* Relax JSR.A -> JSR.W */
1794 new_type = R_M32C_16_PCREL;
1802 case R_M32C_RL_2ADDR:
1803 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1809 /* prefix; remove it as far as the RL reloc is concerned. */
1814 enctbl = m32c_addr_encodings;
1815 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1818 if (srel->r_offset == irel->r_offset + posn
1819 && e->new_encoding != enc
1820 && symval <= e->max_disp)
1823 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1825 einsn[1] |= (e->new_encoding & 0x03) << 4;
1826 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1827 gap = insn + posn + enctbl[e->new_encoding].bytes;
1828 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1831 if (relax_relocs == 2)
1837 case R_M32C_RL_1ADDR:
1838 /* xxxx DDDx DDxx xxxx [disp] */
1844 /* prefix; remove it as far as the RL reloc is concerned. */
1849 enctbl = m32c_addr_encodings;
1852 /* srel, posn, and enc must be set here. */
1854 symval = OFFSET_FOR_RELOC (srel);
1855 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1858 if (srel->r_offset == irel->r_offset + posn
1859 && e->new_encoding != enc
1860 && symval <= e->max_disp)
1863 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1865 einsn[1] |= (e->new_encoding & 0x03) << 6;
1866 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1867 gap = insn + posn + enctbl[e->new_encoding].bytes;
1868 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1874 } /* Ends switch (reloc type) for m32c. */
1882 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1884 /* Note that we've changed the relocs, section contents, etc. */
1885 elf_section_data (sec)->relocs = internal_relocs;
1888 elf_section_data (sec)->this_hdr.contents = contents;
1889 free_contents = NULL;
1891 symtab_hdr->contents = (bfd_byte *) intsyms;
1892 free_intsyms = NULL;
1894 bytes_saved += gap_size;
1896 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1899 } /* next relocation */
1901 if (free_relocs != NULL)
1907 if (free_contents != NULL)
1909 if (! link_info->keep_memory)
1910 free (free_contents);
1911 /* Cache the section contents for elf_link_input_bfd. */
1913 elf_section_data (sec)->this_hdr.contents = contents;
1915 free_contents = NULL;
1918 if (shndx_buf != NULL)
1920 shndx_hdr->contents = NULL;
1924 if (free_intsyms != NULL)
1926 if (! link_info->keep_memory)
1927 free (free_intsyms);
1928 /* Cache the symbols for elf_link_input_bfd. */
1931 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1934 free_intsyms = NULL;
1940 if (free_relocs != NULL)
1942 if (free_contents != NULL)
1943 free (free_contents);
1944 if (shndx_buf != NULL)
1946 shndx_hdr->contents = NULL;
1949 if (free_intsyms != NULL)
1950 free (free_intsyms);
1954 /* Delete some bytes from a section while relaxing. */
1957 m32c_elf_relax_delete_bytes
1963 Elf_Internal_Shdr *symtab_hdr;
1964 Elf_Internal_Shdr *shndx_hdr;
1967 Elf_Internal_Rela *irel;
1968 Elf_Internal_Rela *irelend;
1970 Elf_Internal_Sym *isym;
1971 Elf_Internal_Sym *isymend;
1972 Elf_Internal_Sym *intsyms;
1973 Elf_External_Sym_Shndx *shndx_buf;
1974 Elf_External_Sym_Shndx *shndx;
1975 struct elf_link_hash_entry ** sym_hashes;
1976 struct elf_link_hash_entry ** end_hashes;
1977 unsigned int symcount;
1979 contents = elf_section_data (sec)->this_hdr.contents;
1983 irel = elf_section_data (sec)->relocs;
1984 irelend = irel + sec->reloc_count;
1986 /* Actually delete the bytes. */
1987 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1990 /* Adjust all the relocs. */
1991 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1993 /* Get the new reloc address. */
1994 if (irel->r_offset > addr && irel->r_offset < toaddr)
1995 irel->r_offset -= count;
1997 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1998 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1999 && irel->r_offset + 1 < addr
2000 && irel->r_offset + 7 > addr)
2003 unsigned char *insn = &contents[irel->r_offset];
2005 /* This is a JMP.S, which we have to manually update. */
2006 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2008 if ((*insn & 0xf8) != 0x60)
2014 if ((*insn & 0xce) != 0x4a)
2016 disp = ((disp & 0x30) >> 3) | (disp & 1);
2018 if (irel->r_offset + disp + 2 >= addr+count)
2021 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2023 *insn = (*insn & 0xf8) | disp;
2027 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
2033 /* Adjust the local symbols defined in this section. */
2034 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2035 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2037 isymend = isym + symtab_hdr->sh_info;
2039 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2040 if (elf_symtab_shndx_list (abfd))
2042 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2043 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2052 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
2054 /* If the symbol is in the range of memory we just moved, we
2055 have to adjust its value. */
2056 if ((int) isym->st_shndx == sec_shndx
2057 && isym->st_value > addr
2058 && isym->st_value < toaddr)
2060 isym->st_value -= count;
2062 /* If the symbol *spans* the bytes we just deleted (i.e. it's
2063 *end* is in the moved bytes but it's *start* isn't), then we
2064 must adjust its size. */
2065 if ((int) isym->st_shndx == sec_shndx
2066 && isym->st_value < addr
2067 && isym->st_value + isym->st_size > addr
2068 && isym->st_value + isym->st_size < toaddr)
2070 isym->st_size -= count;
2074 /* Now adjust the global symbols defined in this section. */
2075 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2076 - symtab_hdr->sh_info);
2077 sym_hashes = elf_sym_hashes (abfd);
2078 // sym_hashes += symtab_hdr->sh_info;
2079 end_hashes = sym_hashes + symcount;
2081 for (; sym_hashes < end_hashes; sym_hashes ++)
2083 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2086 (sym_hash->root.type == bfd_link_hash_defined
2087 || sym_hash->root.type == bfd_link_hash_defweak)
2088 && sym_hash->root.u.def.section == sec)
2090 if (sym_hash->root.u.def.value > addr
2091 && sym_hash->root.u.def.value < toaddr)
2093 sym_hash->root.u.def.value -= count;
2095 if (sym_hash->root.u.def.value < addr
2096 && sym_hash->root.u.def.value + sym_hash->size > addr
2097 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2099 sym_hash->size -= count;
2107 /* This is for versions of gcc prior to 4.3. */
2109 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd,
2110 const asection *sec ATTRIBUTE_UNUSED)
2112 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2119 #define ELF_ARCH bfd_arch_m32c
2120 #define ELF_MACHINE_CODE EM_M32C
2121 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2122 #define ELF_MAXPAGESIZE 0x100
2125 #define TARGET_BIG_SYM m32c_elf32_vec
2126 #define TARGET_BIG_NAME "elf32-m32c"
2128 #define TARGET_LITTLE_SYM m32c_elf32_vec
2129 #define TARGET_LITTLE_NAME "elf32-m32c"
2132 #define elf_info_to_howto_rel NULL
2133 #define elf_info_to_howto m32c_info_to_howto_rela
2134 #define elf_backend_object_p m32c_elf_object_p
2135 #define elf_backend_relocate_section m32c_elf_relocate_section
2136 #define elf_backend_check_relocs m32c_elf_check_relocs
2137 #define elf_backend_object_p m32c_elf_object_p
2138 #define elf_symbol_leading_char ('_')
2139 #define elf_backend_always_size_sections \
2140 m32c_elf_always_size_sections
2141 #define elf_backend_finish_dynamic_sections \
2142 m32c_elf_finish_dynamic_sections
2144 #define elf_backend_can_gc_sections 1
2145 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2147 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2148 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2149 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2150 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2151 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2152 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2154 #include "elf32-target.h"