1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2017 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 void m32c_info_to_howto_rela
32 (bfd *, arelent *, Elf_Internal_Rela *);
33 static bfd_boolean m32c_elf_relocate_section
34 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35 static bfd_boolean m32c_elf_check_relocs
36 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
39 char * m32c_get_reloc (long reloc);
40 void dump_symtab (bfd *, void *, void *);
42 static bfd_boolean m32c_elf_relax_section
43 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
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
296 (bfd * abfd ATTRIBUTE_UNUSED,
298 Elf_Internal_Rela * dst)
302 r_type = ELF32_R_TYPE (dst->r_info);
303 if (r_type >= (unsigned int) R_M32C_max)
305 /* xgettext:c-format */
306 _bfd_error_handler (_("%B: invalid M32C reloc number: %d"), abfd, r_type);
309 cache_ptr->howto = & m32c_elf_howto_table [r_type];
314 /* Apply R_M32C_24 relocations. We have to do this because it's not a
315 power-of-two size, and the generic code may think it overruns the
316 section if it's right at the end.
318 Must return something other than bfd_reloc_continue to avoid the
319 above problem. Typical return values include bfd_reloc_ok or
323 static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
324 arelent *reloc_entry,
326 void *vdata_start ATTRIBUTE_UNUSED,
327 asection *input_section,
328 bfd *ibfd ATTRIBUTE_UNUSED,
329 char **error_msg ATTRIBUTE_UNUSED)
332 bfd_reloc_status_type s;
334 s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
336 input_section, ibfd, error_msg);
337 if (s != bfd_reloc_continue)
340 /* Get symbol value. (Common symbols are special.) */
341 if (bfd_is_com_section (symbol->section))
344 relocation = symbol->value;
346 relocation += symbol->section->output_offset;
348 /* Add in supplied addend. */
349 relocation += reloc_entry->addend;
351 reloc_entry->addend = relocation;
352 reloc_entry->address += input_section->output_offset;
356 /* Relocate an M32C ELF section.
357 There is some attempt to make this function usable for many architectures,
358 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
359 if only to serve as a learning tool.
361 The RELOCATE_SECTION function is called by the new ELF backend linker
362 to handle the relocations for a section.
364 The relocs are always passed as Rela structures; if the section
365 actually uses Rel structures, the r_addend field will always be
368 This function is responsible for adjusting the section contents as
369 necessary, and (if using Rela relocs and generating a relocatable
370 output file) adjusting the reloc addend as necessary.
372 This function does not have to worry about setting the reloc
373 address or the reloc symbol index.
375 LOCAL_SYMS is a pointer to the swapped in local symbols.
377 LOCAL_SECTIONS is an array giving the section in the input file
378 corresponding to the st_shndx field of each local symbol.
380 The global hash table entry for the global symbols can be found
381 via elf_sym_hashes (input_bfd).
383 When generating relocatable output, this function must handle
384 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
385 going to be the section symbol corresponding to the output
386 section, which means that the addend must be adjusted
390 m32c_elf_relocate_section
391 (bfd * output_bfd ATTRIBUTE_UNUSED,
392 struct bfd_link_info * info,
394 asection * input_section,
396 Elf_Internal_Rela * relocs,
397 Elf_Internal_Sym * local_syms,
398 asection ** local_sections)
400 Elf_Internal_Shdr * symtab_hdr;
401 struct elf_link_hash_entry ** sym_hashes;
402 Elf_Internal_Rela * rel;
403 Elf_Internal_Rela * relend;
406 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
407 sym_hashes = elf_sym_hashes (input_bfd);
408 relend = relocs + input_section->reloc_count;
410 splt = elf_hash_table (info)->splt;
412 for (rel = relocs; rel < relend; rel ++)
414 reloc_howto_type * howto;
415 unsigned long r_symndx;
416 Elf_Internal_Sym * sym;
418 struct elf_link_hash_entry * h;
420 bfd_reloc_status_type r;
421 const char * name = NULL;
424 r_type = ELF32_R_TYPE (rel->r_info);
426 /* These are only used for relaxing; we don't actually relocate
427 anything with them, so skip them. */
428 if (r_type == R_M32C_RL_JUMP
429 || r_type == R_M32C_RL_1ADDR
430 || r_type == R_M32C_RL_2ADDR)
433 r_symndx = ELF32_R_SYM (rel->r_info);
435 howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
441 if (r_symndx < symtab_hdr->sh_info)
443 sym = local_syms + r_symndx;
444 sec = local_sections [r_symndx];
445 relocation = (sec->output_section->vma
449 name = bfd_elf_string_from_elf_section
450 (input_bfd, symtab_hdr->sh_link, sym->st_name);
451 name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name;
455 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
457 if (info->wrap_hash != NULL
458 && (input_section->flags & SEC_DEBUGGING) != 0)
459 h = ((struct elf_link_hash_entry *)
460 unwrap_hash_lookup (info, input_bfd, &h->root));
462 while (h->root.type == bfd_link_hash_indirect
463 || h->root.type == bfd_link_hash_warning)
464 h = (struct elf_link_hash_entry *) h->root.u.i.link;
466 name = h->root.root.string;
468 if (h->root.type == bfd_link_hash_defined
469 || h->root.type == bfd_link_hash_defweak)
471 sec = h->root.u.def.section;
472 relocation = (h->root.u.def.value
473 + sec->output_section->vma
474 + sec->output_offset);
476 else if (h->root.type == bfd_link_hash_undefweak)
478 else if (!bfd_link_relocatable (info))
479 (*info->callbacks->undefined_symbol) (info, h->root.root.string,
480 input_bfd, input_section,
481 rel->r_offset, TRUE);
484 if (sec != NULL && discarded_section (sec))
485 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
486 rel, 1, relend, howto, 0, contents);
488 if (bfd_link_relocatable (info))
490 /* This is a relocatable link. We don't have to change
491 anything, unless the reloc is against a section symbol,
492 in which case we have to adjust according to where the
493 section symbol winds up in the output section. */
494 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
495 rel->r_addend += sec->output_offset;
499 switch (ELF32_R_TYPE (rel->r_info))
506 plt_offset = &h->plt.offset;
508 plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
510 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
511 relocation, *plt_offset);*/
512 if (relocation <= 0xffff)
514 /* If the symbol is in range for a 16-bit address, we should
515 have deallocated the plt entry in relax_section. */
516 BFD_ASSERT (*plt_offset == (bfd_vma) -1);
520 /* If the symbol is out of range for a 16-bit address,
521 we must have allocated a plt entry. */
522 BFD_ASSERT (*plt_offset != (bfd_vma) -1);
524 /* If this is the first time we've processed this symbol,
525 fill in the plt entry with the correct symbol address. */
526 if ((*plt_offset & 1) == 0)
530 x = 0x000000fc; /* jmpf */
531 x |= (relocation << 8) & 0xffffff00;
532 bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
536 relocation = (splt->output_section->vma
537 + splt->output_offset
538 + (*plt_offset & -2));
541 char *newname = bfd_malloc (strlen(name)+5);
542 strcpy (newname, name);
543 strcat(newname, ".plt");
544 _bfd_generic_link_add_one_symbol (info,
547 BSF_FUNCTION | BSF_WEAK,
566 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
567 m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
568 rel->r_offset + input_section->output_section->vma + input_section->output_offset,
569 relocation, rel->r_addend);
573 printf (" %02x", contents[rel->r_offset+i]);
577 switch (ELF32_R_TYPE(rel->r_info))
580 /* Like m32c_apply_reloc_24, we must handle this one separately. */
581 relocation += rel->r_addend;
583 /* Sanity check the address. */
584 if (rel->r_offset + 3
585 > bfd_get_section_limit_octets (input_bfd, input_section))
586 r = bfd_reloc_outofrange;
589 bfd_put_8 (input_bfd, relocation & 0xff, contents + rel->r_offset);
590 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff, contents + rel->r_offset + 1);
591 bfd_put_8 (input_bfd, (relocation >> 16) & 0xff, contents + rel->r_offset + 2);
598 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
599 contents, rel->r_offset, relocation,
604 if (r != bfd_reloc_ok)
606 const char * msg = (const char *) NULL;
610 case bfd_reloc_overflow:
611 (*info->callbacks->reloc_overflow)
612 (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
613 input_bfd, input_section, rel->r_offset);
616 case bfd_reloc_undefined:
617 (*info->callbacks->undefined_symbol)
618 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
621 case bfd_reloc_outofrange:
622 msg = _("internal error: out of range error");
625 case bfd_reloc_notsupported:
626 msg = _("internal error: unsupported relocation error");
629 case bfd_reloc_dangerous:
630 msg = _("internal error: dangerous relocation");
634 msg = _("internal error: unknown error");
639 (*info->callbacks->warning) (info, msg, name, input_bfd,
640 input_section, rel->r_offset);
647 /* We support 16-bit pointers to code above 64k by generating a thunk
648 below 64k containing a JMP instruction to the final address. */
651 m32c_elf_check_relocs
653 struct bfd_link_info * info,
655 const Elf_Internal_Rela * relocs)
657 Elf_Internal_Shdr * symtab_hdr;
658 struct elf_link_hash_entry ** sym_hashes;
659 const Elf_Internal_Rela * rel;
660 const Elf_Internal_Rela * rel_end;
661 bfd_vma *local_plt_offsets;
665 if (bfd_link_relocatable (info))
668 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
669 sym_hashes = elf_sym_hashes (abfd);
670 local_plt_offsets = elf_local_got_offsets (abfd);
672 dynobj = elf_hash_table(info)->dynobj;
674 rel_end = relocs + sec->reloc_count;
675 for (rel = relocs; rel < rel_end; rel++)
677 struct elf_link_hash_entry *h;
678 unsigned long r_symndx;
681 r_symndx = ELF32_R_SYM (rel->r_info);
682 if (r_symndx < symtab_hdr->sh_info)
686 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
687 while (h->root.type == bfd_link_hash_indirect
688 || h->root.type == bfd_link_hash_warning)
689 h = (struct elf_link_hash_entry *) h->root.u.i.link;
692 switch (ELF32_R_TYPE (rel->r_info))
694 /* This relocation describes a 16-bit pointer to a function.
695 We may need to allocate a thunk in low memory; reserve memory
699 elf_hash_table (info)->dynobj = dynobj = abfd;
700 splt = elf_hash_table (info)->splt;
703 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
704 | SEC_IN_MEMORY | SEC_LINKER_CREATED
705 | SEC_READONLY | SEC_CODE);
706 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
708 elf_hash_table (info)->splt = splt;
710 || ! bfd_set_section_alignment (dynobj, splt, 1))
715 offset = &h->plt.offset;
718 if (local_plt_offsets == NULL)
723 size = symtab_hdr->sh_info * sizeof (bfd_vma);
724 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
725 if (local_plt_offsets == NULL)
727 elf_local_got_offsets (abfd) = local_plt_offsets;
729 for (i = 0; i < symtab_hdr->sh_info; i++)
730 local_plt_offsets[i] = (bfd_vma) -1;
732 offset = &local_plt_offsets[r_symndx];
735 if (*offset == (bfd_vma) -1)
737 *offset = splt->size;
747 /* This must exist if dynobj is ever set. */
750 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
751 struct bfd_link_info *info)
753 bfd *dynobj = elf_hash_table (info)->dynobj;
754 asection *splt = elf_hash_table (info)->splt;
756 /* As an extra sanity check, verify that all plt entries have
759 if (dynobj != NULL && splt != NULL)
761 bfd_byte *contents = splt->contents;
762 unsigned int i, size = splt->size;
763 for (i = 0; i < size; i += 4)
765 unsigned int x = bfd_get_32 (dynobj, contents + i);
774 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
775 struct bfd_link_info *info)
780 if (bfd_link_relocatable (info))
783 dynobj = elf_hash_table (info)->dynobj;
787 splt = elf_hash_table (info)->splt;
788 BFD_ASSERT (splt != NULL);
790 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
791 if (splt->contents == NULL)
797 /* Function to set the ELF flag bits. */
800 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
802 elf_elfheader (abfd)->e_flags = flags;
803 elf_flags_init (abfd) = TRUE;
807 /* Merge backend specific data from an object file to the output
808 object file when linking. */
811 m32c_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
813 bfd *obfd = info->output_bfd;
814 flagword old_flags, old_partial;
815 flagword new_flags, new_partial;
816 bfd_boolean error = FALSE;
820 new_opt[0] = old_opt[0] = '\0';
821 new_flags = elf_elfheader (ibfd)->e_flags;
822 old_flags = elf_elfheader (obfd)->e_flags;
826 ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
827 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
828 bfd_get_filename (ibfd));
831 if (!elf_flags_init (obfd))
833 /* First call, no flags set. */
834 elf_flags_init (obfd) = TRUE;
835 elf_elfheader (obfd)->e_flags = new_flags;
838 else if (new_flags == old_flags)
839 /* Compatible flags are ok. */
842 else /* Possibly incompatible flags. */
844 /* Warn if different cpu is used (allow a specific cpu to override
846 new_partial = (new_flags & EF_M32C_CPU_MASK);
847 old_partial = (old_flags & EF_M32C_CPU_MASK);
848 if (new_partial == old_partial)
855 default: strcat (new_opt, " -m16c"); break;
856 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
857 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
862 default: strcat (old_opt, " -m16c"); break;
863 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
864 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
868 /* Print out any mismatches from above. */
873 /* xgettext:c-format */
874 (_("%B: compiled with %s and linked with modules compiled with %s"),
875 ibfd, new_opt, old_opt);
878 new_flags &= ~ EF_M32C_ALL_FLAGS;
879 old_flags &= ~ EF_M32C_ALL_FLAGS;
881 /* Warn about any other mismatches. */
882 if (new_flags != old_flags)
886 /* xgettext:c-format */
887 (_("%B: uses different e_flags (%#x) fields"
888 " than previous modules (%#x)"),
889 ibfd, new_flags, old_flags);
894 bfd_set_error (bfd_error_bad_value);
901 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
903 FILE *file = (FILE *) ptr;
906 BFD_ASSERT (abfd != NULL && ptr != NULL);
908 /* Print normal ELF private data. */
909 _bfd_elf_print_private_bfd_data (abfd, ptr);
911 flags = elf_elfheader (abfd)->e_flags;
912 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
914 switch (flags & EF_M32C_CPU_MASK)
917 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
918 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
925 /* Return the MACH for an e_flags value. */
928 elf32_m32c_machine (bfd *abfd)
930 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
932 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
933 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
936 return bfd_mach_m16c;
940 m32c_elf_object_p (bfd *abfd)
942 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
943 elf32_m32c_machine (abfd));
950 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
953 Elf_Internal_Sym *isymbuf;
954 Elf_Internal_Sym *isymend;
955 Elf_Internal_Sym *isym;
956 Elf_Internal_Shdr *symtab_hdr;
957 bfd_boolean free_internal = 0, free_external = 0;
959 char * st_info_stb_str;
965 internal_syms = bfd_malloc (1000);
970 external_syms = bfd_malloc (1000);
974 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
975 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
977 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
978 symtab_hdr->sh_info, 0,
979 internal_syms, external_syms, NULL);
981 isymbuf = internal_syms;
982 isymend = isymbuf + locsymcount;
984 for (isym = isymbuf ; isym < isymend ; isym++)
986 switch (ELF_ST_TYPE (isym->st_info))
989 st_info_str = "STT_FUNC";
993 st_info_str = "STT_SECTION";
997 st_info_str = "STT_FILE";
1001 st_info_str = "STT_OBJECT";
1005 st_info_str = "STT_TLS";
1012 switch (ELF_ST_BIND (isym->st_info))
1015 st_info_stb_str = "STB_LOCAL";
1019 st_info_stb_str = "STB_GLOBAL";
1023 st_info_stb_str = "";
1026 switch (ELF_ST_VISIBILITY (isym->st_other))
1029 st_other_str = "STV_DEFAULT";
1033 st_other_str = "STV_INTERNAL";
1037 st_other_str = "STV_PROTECTED";
1044 switch (isym->st_shndx)
1047 st_shndx_str = "SHN_ABS";
1051 st_shndx_str = "SHN_COMMON";
1055 st_shndx_str = "SHN_UNDEF";
1062 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1063 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1065 (unsigned long) isym->st_value,
1066 (unsigned long) isym->st_size,
1068 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1070 isym->st_info, st_info_str, st_info_stb_str,
1071 isym->st_other, st_other_str,
1072 isym->st_shndx, st_shndx_str);
1075 free (internal_syms);
1077 free (external_syms);
1081 m32c_get_reloc (long reloc)
1083 if (0 <= reloc && reloc < R_M32C_max)
1084 return m32c_elf_howto_table[reloc].name;
1090 /* Handle relaxing. */
1092 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1093 is within the low 64k, remove any entry for it in the plt. */
1095 struct relax_plt_data
1102 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1104 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1106 if (h->plt.offset != (bfd_vma) -1)
1110 if (h->root.type == bfd_link_hash_undefined
1111 || h->root.type == bfd_link_hash_undefweak)
1114 address = (h->root.u.def.section->output_section->vma
1115 + h->root.u.def.section->output_offset
1116 + h->root.u.def.value);
1118 if (address <= 0xffff)
1121 data->splt->size -= 4;
1122 *data->again = TRUE;
1129 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1130 previously had a plt entry, give it a new entry offset. */
1133 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1135 bfd_vma *entry = (bfd_vma *) xdata;
1137 if (h->plt.offset != (bfd_vma) -1)
1139 h->plt.offset = *entry;
1147 m32c_elf_relax_plt_section (asection *splt,
1148 struct bfd_link_info *info,
1151 struct relax_plt_data relax_plt_data;
1154 /* Assume nothing changes. */
1157 if (bfd_link_relocatable (info))
1160 /* Quick check for an empty plt. */
1161 if (splt->size == 0)
1164 /* Map across all global symbols; see which ones happen to
1165 fall in the low 64k. */
1166 relax_plt_data.splt = splt;
1167 relax_plt_data.again = again;
1168 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1171 /* Likewise for local symbols, though that's somewhat less convenient
1172 as we have to walk the list of input bfds and swap in symbol data. */
1173 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1175 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1176 Elf_Internal_Shdr *symtab_hdr;
1177 Elf_Internal_Sym *isymbuf = NULL;
1180 if (! local_plt_offsets)
1183 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1184 if (symtab_hdr->sh_info != 0)
1186 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1187 if (isymbuf == NULL)
1188 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1189 symtab_hdr->sh_info, 0,
1191 if (isymbuf == NULL)
1195 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1197 Elf_Internal_Sym *isym;
1201 if (local_plt_offsets[idx] == (bfd_vma) -1)
1204 isym = &isymbuf[idx];
1205 if (isym->st_shndx == SHN_UNDEF)
1207 else if (isym->st_shndx == SHN_ABS)
1208 tsec = bfd_abs_section_ptr;
1209 else if (isym->st_shndx == SHN_COMMON)
1210 tsec = bfd_com_section_ptr;
1212 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1214 address = (tsec->output_section->vma
1215 + tsec->output_offset
1217 if (address <= 0xffff)
1219 local_plt_offsets[idx] = -1;
1226 && symtab_hdr->contents != (unsigned char *) isymbuf)
1228 if (! info->keep_memory)
1232 /* Cache the symbols for elf_link_input_bfd. */
1233 symtab_hdr->contents = (unsigned char *) isymbuf;
1238 /* If we changed anything, walk the symbols again to reallocate
1239 .plt entry addresses. */
1240 if (*again && splt->size > 0)
1244 elf_link_hash_traverse (elf_hash_table (info),
1245 m32c_relax_plt_realloc, &entry);
1247 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1249 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1250 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1253 if (! local_plt_offsets)
1256 for (idx = 0; idx < nlocals; ++idx)
1257 if (local_plt_offsets[idx] != (bfd_vma) -1)
1259 local_plt_offsets[idx] = entry;
1269 compare_reloc (const void *e1, const void *e2)
1271 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1272 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1274 if (i1->r_offset == i2->r_offset)
1277 return i1->r_offset < i2->r_offset ? -1 : 1;
1280 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1282 m32c_offset_for_reloc (bfd *abfd,
1283 Elf_Internal_Rela *rel,
1284 Elf_Internal_Shdr *symtab_hdr,
1285 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1286 Elf_Internal_Sym *intsyms)
1290 /* Get the value of the symbol referred to by the reloc. */
1291 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1293 /* A local symbol. */
1294 Elf_Internal_Sym *isym;
1297 isym = intsyms + ELF32_R_SYM (rel->r_info);
1298 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1299 symval = isym->st_value;
1301 symval += ssec->output_section->vma
1302 + ssec->output_offset;
1307 struct elf_link_hash_entry *h;
1309 /* An external symbol. */
1310 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1311 h = elf_sym_hashes (abfd)[indx];
1312 BFD_ASSERT (h != NULL);
1314 if (h->root.type != bfd_link_hash_defined
1315 && h->root.type != bfd_link_hash_defweak)
1316 /* This appears to be a reference to an undefined
1317 symbol. Just ignore it--it will be caught by the
1318 regular reloc processing. */
1321 symval = (h->root.u.def.value
1322 + h->root.u.def.section->output_section->vma
1323 + h->root.u.def.section->output_offset);
1328 static int bytes_saved = 0;
1330 static int bytes_to_reloc[] = {
1338 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1340 /* Mask for the number of relocs associated with this insn. */
1341 #define RLA_RELOCS 0x0000000f
1342 /* Number of bytes gas emitted (before gas's relaxing) */
1343 #define RLA_NBYTES 0x00000ff0
1345 /* If the displacement is within the given range and the new encoding
1346 differs from the old encoding (the index), then the insn can be
1347 relaxed to the new encoding. */
1350 unsigned int max_disp;
1351 unsigned char new_encoding;
1354 static EncodingTable m16c_addr_encodings[] = {
1355 { 0, 0, 0 }, /* R0 */
1356 { 0, 0, 1 }, /* R1 */
1357 { 0, 0, 2 }, /* R2 */
1358 { 0, 0, 3 }, /* R3 */
1359 { 0, 0, 4 }, /* A0 */
1360 { 0, 0, 5 }, /* A1 */
1361 { 0, 0, 6 }, /* [A0] */
1362 { 0, 0, 7 }, /* [A1] */
1363 { 1, 0, 6 }, /* udsp:8[A0] */
1364 { 1, 0, 7 }, /* udsp:8[A1] */
1365 { 1, 0, 10 }, /* udsp:8[SB] */
1366 { 1, 0, 11 }, /* sdsp:8[FB] */
1367 { 2, 255, 8 }, /* udsp:16[A0] */
1368 { 2, 255, 9 }, /* udsp:16[A1] */
1369 { 2, 255, 10 }, /* udsp:16[SB] */
1370 { 2, 0, 15 }, /* abs:16 */
1373 static EncodingTable m16c_jmpaddr_encodings[] = {
1374 { 0, 0, 0 }, /* R0 */
1375 { 0, 0, 1 }, /* R1 */
1376 { 0, 0, 2 }, /* R2 */
1377 { 0, 0, 3 }, /* R3 */
1378 { 0, 0, 4 }, /* A0 */
1379 { 0, 0, 5 }, /* A1 */
1380 { 0, 0, 6 }, /* [A0] */
1381 { 0, 0, 7 }, /* [A1] */
1382 { 1, 0, 6 }, /* udsp:8[A0] */
1383 { 1, 0, 7 }, /* udsp:8[A1] */
1384 { 1, 0, 10 }, /* udsp:8[SB] */
1385 { 1, 0, 11 }, /* sdsp:8[FB] */
1386 { 3, 255, 8 }, /* udsp:20[A0] */
1387 { 3, 255, 9 }, /* udsp:20[A1] */
1388 { 2, 255, 10 }, /* udsp:16[SB] */
1389 { 2, 0, 15 }, /* abs:16 */
1392 static EncodingTable m32c_addr_encodings[] = {
1393 { 0, 0, 0 }, /* [A0] */
1394 { 0, 0, 1 }, /* [A1] */
1395 { 0, 0, 2 }, /* A0 */
1396 { 0, 0, 3 }, /* A1 */
1397 { 1, 0, 0 }, /* udsp:8[A0] */
1398 { 1, 0, 1 }, /* udsp:8[A1] */
1399 { 1, 0, 6 }, /* udsp:8[SB] */
1400 { 1, 0, 7 }, /* sdsp:8[FB] */
1401 { 2, 255, 4 }, /* udsp:16[A0] */
1402 { 2, 255, 5 }, /* udsp:16[A1] */
1403 { 2, 255, 6 }, /* udsp:16[SB] */
1404 { 2, 127, 7 }, /* sdsp:16[FB] */
1405 { 3, 65535, 8 }, /* udsp:24[A0] */
1406 { 3, 65535, 9 }, /* udsp:24[A1] */
1407 { 3, 65535, 15 }, /* abs24 */
1408 { 2, 0, 15 }, /* abs16 */
1409 { 0, 0, 16 }, /* R2 */
1410 { 0, 0, 17 }, /* R3 */
1411 { 0, 0, 18 }, /* R0 */
1412 { 0, 0, 19 }, /* R1 */
1428 m32c_elf_relax_section
1431 struct bfd_link_info * link_info,
1432 bfd_boolean * again)
1434 Elf_Internal_Shdr *symtab_hdr;
1435 Elf_Internal_Shdr *shndx_hdr;
1436 Elf_Internal_Rela *internal_relocs;
1437 Elf_Internal_Rela *free_relocs = NULL;
1438 Elf_Internal_Rela *irel, *irelend, *srel;
1439 bfd_byte * contents = NULL;
1440 bfd_byte * free_contents = NULL;
1441 Elf_Internal_Sym *intsyms = NULL;
1442 Elf_Internal_Sym *free_intsyms = NULL;
1443 Elf_External_Sym_Shndx *shndx_buf = NULL;
1446 if (abfd == elf_hash_table (link_info)->dynobj
1447 && (sec->flags & SEC_LINKER_CREATED) != 0
1448 && strcmp (sec->name, ".plt") == 0)
1449 return m32c_elf_relax_plt_section (sec, link_info, again);
1451 /* Assume nothing changes. */
1454 machine = elf32_m32c_machine (abfd);
1456 /* We don't have to do anything for a relocatable link, if
1457 this section does not have relocs, or if this is not a
1459 if (bfd_link_relocatable (link_info)
1460 || (sec->flags & SEC_RELOC) == 0
1461 || sec->reloc_count == 0
1462 || (sec->flags & SEC_CODE) == 0)
1465 symtab_hdr = & elf_symtab_hdr (abfd);
1466 if (elf_symtab_shndx_list (abfd))
1467 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
1471 /* Get the section contents. */
1472 if (elf_section_data (sec)->this_hdr.contents != NULL)
1473 contents = elf_section_data (sec)->this_hdr.contents;
1474 /* Go get them off disk. */
1475 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1478 /* Read this BFD's symbols. */
1479 /* Get cached copy if it exists. */
1480 if (symtab_hdr->contents != NULL)
1482 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1486 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1487 symtab_hdr->contents = (bfd_byte *) intsyms;
1490 if (shndx_hdr && shndx_hdr->sh_size != 0)
1494 amt = symtab_hdr->sh_info;
1495 amt *= sizeof (Elf_External_Sym_Shndx);
1496 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1497 if (shndx_buf == NULL)
1499 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1500 || bfd_bread (shndx_buf, amt, abfd) != amt)
1502 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1505 /* Get a copy of the native relocations. */
1506 internal_relocs = (_bfd_elf_link_read_relocs
1507 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1508 link_info->keep_memory));
1509 if (internal_relocs == NULL)
1511 if (! link_info->keep_memory)
1512 free_relocs = internal_relocs;
1514 /* The RL_ relocs must be just before the operand relocs they go
1515 with, so we must sort them to guarantee this. */
1516 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1519 /* Walk through them looking for relaxing opportunities. */
1520 irelend = internal_relocs + sec->reloc_count;
1522 for (irel = internal_relocs; irel < irelend; irel++)
1525 unsigned char *insn, *gap, *einsn;
1527 bfd_signed_vma pcrel;
1533 EncodingTable *enctbl;
1536 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1537 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1538 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1543 /* There will always be room for the relaxed insn, since it is smaller
1544 than the one it would replace. */
1545 BFD_ASSERT (irel->r_offset < sec->size);
1547 insn = contents + irel->r_offset;
1548 relax_relocs = irel->r_addend % 16;
1550 /* Ok, we only have three relocs we care about, and they're all
1551 fake. The lower four bits of the addend is always the number
1552 of following relocs (hence the qsort above) that are assigned
1553 to this opcode. The next 8 bits of the addend indicates the
1554 number of bytes in the insn. We use the rest of them
1555 ourselves as flags for the more expensive operations (defines
1556 above). The three relocs are:
1558 RL_JUMP: This marks all direct jump insns. We check the
1559 displacement and replace them with shorter jumps if
1560 they're in range. We also use this to find JMP.S
1561 insns and manually shorten them when we delete bytes.
1562 We have to decode these insns to figure out what to
1565 RL_1ADDR: This is a :G or :Q insn, which has a single
1566 "standard" operand. We have to extract the type
1567 field, see if it's a wide displacement, then figure
1568 out if we can replace it with a narrow displacement.
1569 We don't have to decode these insns.
1571 RL_2ADDR: Similarly, but two "standard" operands. Note that
1572 r_addend may still be 1, as standard operands don't
1573 always have displacements. Gas shouldn't give us one
1574 with zero operands, but since we don't know which one
1575 has the displacement, we check them both anyway.
1577 These all point to the beginning of the insn itself, not the
1580 Note that we only relax one step at a time, relying on the
1581 linker to call us repeatedly. Thus, there is no code for
1582 JMP.A->JMP.B although that will happen in two steps.
1583 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1586 /* Get the value of the symbol referred to by the reloc. Just
1587 in case this is the last reloc in the list, use the RL's
1588 addend to choose between this reloc (no addend) or the next
1589 (yes addend, which means at least one following reloc). */
1590 srel = irel + (relax_relocs ? 1 : 0);
1591 symval = OFFSET_FOR_RELOC (srel);
1593 /* Setting gap_size nonzero is the flag which means "something
1597 new_type = ELF32_R_TYPE(srel->r_info);
1599 pc = sec->output_section->vma + sec->output_offset
1601 pcrel = symval - pc + srel->r_addend;
1603 if (machine == bfd_mach_m16c)
1607 switch (ELF32_R_TYPE(irel->r_info))
1610 case R_M32C_RL_JUMP:
1613 case 0xfe: /* jmp.b */
1614 if (pcrel >= 2 && pcrel <= 9)
1616 /* Relax JMP.B -> JMP.S. We need to get rid of
1617 the following reloc though. */
1618 insn[0] = 0x60 | (pcrel - 2);
1619 new_type = R_M32C_NONE;
1620 irel->r_addend = 0x10;
1626 case 0xf4: /* jmp.w */
1627 /* 128 is allowed because it will be one byte closer
1628 after relaxing. Likewise for all other pc-rel
1630 if (pcrel <= 128 && pcrel >= -128)
1632 /* Relax JMP.W -> JMP.B */
1635 new_type = R_M32C_8_PCREL;
1641 case 0xfc: /* jmp.a */
1642 if (pcrel <= 32768 && pcrel >= -32768)
1644 /* Relax JMP.A -> JMP.W */
1648 new_type = R_M32C_16_PCREL;
1654 case 0xfd: /* jsr.a */
1655 if (pcrel <= 32768 && pcrel >= -32768)
1657 /* Relax JSR.A -> JSR.W */
1661 new_type = R_M32C_16_PCREL;
1669 case R_M32C_RL_2ADDR:
1670 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1672 enctbl = m16c_addr_encodings;
1674 enc = (insn[1] >> 4) & 0x0f;
1677 if (srel->r_offset == irel->r_offset + posn
1678 && e->new_encoding != enc
1679 && symval <= e->max_disp)
1682 insn[1] |= e->new_encoding << 4;
1683 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1684 gap = insn + posn + enctbl[e->new_encoding].bytes;
1685 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1688 if (relax_relocs == 2)
1694 case R_M32C_RL_1ADDR:
1695 /* xxxx xxxx xxxx dest [disp] */
1697 enctbl = m16c_addr_encodings;
1700 /* Check the opcode for jumps. We know it's safe to
1701 do this because all 2ADDR insns are at least two
1703 enc = insn[0] * 256 + insn[1];
1710 enctbl = m16c_jmpaddr_encodings;
1714 /* srel, posn, and enc must be set here. */
1716 symval = OFFSET_FOR_RELOC (srel);
1717 enc = insn[1] & 0x0f;
1720 if (srel->r_offset == irel->r_offset + posn
1721 && e->new_encoding != enc
1722 && symval <= e->max_disp)
1725 insn[1] |= e->new_encoding;
1726 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1727 gap = insn + posn + enctbl[e->new_encoding].bytes;
1728 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1734 } /* Ends switch (reloc type) for m16c. */
1736 else /* machine == bfd_mach_m32c */
1740 switch (ELF32_R_TYPE(irel->r_info))
1743 case R_M32C_RL_JUMP:
1746 case 0xbb: /* jmp.b */
1747 if (pcrel >= 2 && pcrel <= 9)
1750 /* Relax JMP.B -> JMP.S. We need to get rid of
1751 the following reloc though. */
1752 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1753 new_type = R_M32C_NONE;
1754 irel->r_addend = 0x10;
1760 case 0xce: /* jmp.w */
1761 if (pcrel <= 128 && pcrel >= -128)
1763 /* Relax JMP.W -> JMP.B */
1766 new_type = R_M32C_8_PCREL;
1772 case 0xcc: /* jmp.a */
1773 if (pcrel <= 32768 && pcrel >= -32768)
1775 /* Relax JMP.A -> JMP.W */
1779 new_type = R_M32C_16_PCREL;
1785 case 0xcd: /* jsr.a */
1786 if (pcrel <= 32768 && pcrel >= -32768)
1788 /* Relax JSR.A -> JSR.W */
1792 new_type = R_M32C_16_PCREL;
1800 case R_M32C_RL_2ADDR:
1801 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1807 /* prefix; remove it as far as the RL reloc is concerned. */
1812 enctbl = m32c_addr_encodings;
1813 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1816 if (srel->r_offset == irel->r_offset + posn
1817 && e->new_encoding != enc
1818 && symval <= e->max_disp)
1821 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1823 einsn[1] |= (e->new_encoding & 0x03) << 4;
1824 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1825 gap = insn + posn + enctbl[e->new_encoding].bytes;
1826 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1829 if (relax_relocs == 2)
1835 case R_M32C_RL_1ADDR:
1836 /* xxxx DDDx DDxx xxxx [disp] */
1842 /* prefix; remove it as far as the RL reloc is concerned. */
1847 enctbl = m32c_addr_encodings;
1850 /* srel, posn, and enc must be set here. */
1852 symval = OFFSET_FOR_RELOC (srel);
1853 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1856 if (srel->r_offset == irel->r_offset + posn
1857 && e->new_encoding != enc
1858 && symval <= e->max_disp)
1861 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1863 einsn[1] |= (e->new_encoding & 0x03) << 6;
1864 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1865 gap = insn + posn + enctbl[e->new_encoding].bytes;
1866 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1872 } /* Ends switch (reloc type) for m32c. */
1880 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1882 /* Note that we've changed the relocs, section contents, etc. */
1883 elf_section_data (sec)->relocs = internal_relocs;
1886 elf_section_data (sec)->this_hdr.contents = contents;
1887 free_contents = NULL;
1889 symtab_hdr->contents = (bfd_byte *) intsyms;
1890 free_intsyms = NULL;
1892 bytes_saved += gap_size;
1894 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1897 } /* next relocation */
1899 if (free_relocs != NULL)
1905 if (free_contents != NULL)
1907 if (! link_info->keep_memory)
1908 free (free_contents);
1909 /* Cache the section contents for elf_link_input_bfd. */
1911 elf_section_data (sec)->this_hdr.contents = contents;
1913 free_contents = NULL;
1916 if (shndx_buf != NULL)
1918 shndx_hdr->contents = NULL;
1922 if (free_intsyms != NULL)
1924 if (! link_info->keep_memory)
1925 free (free_intsyms);
1926 /* Cache the symbols for elf_link_input_bfd. */
1929 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1932 free_intsyms = NULL;
1938 if (free_relocs != NULL)
1940 if (free_contents != NULL)
1941 free (free_contents);
1942 if (shndx_buf != NULL)
1944 shndx_hdr->contents = NULL;
1947 if (free_intsyms != NULL)
1948 free (free_intsyms);
1952 /* Delete some bytes from a section while relaxing. */
1955 m32c_elf_relax_delete_bytes
1961 Elf_Internal_Shdr *symtab_hdr;
1962 Elf_Internal_Shdr *shndx_hdr;
1965 Elf_Internal_Rela *irel;
1966 Elf_Internal_Rela *irelend;
1968 Elf_Internal_Sym *isym;
1969 Elf_Internal_Sym *isymend;
1970 Elf_Internal_Sym *intsyms;
1971 Elf_External_Sym_Shndx *shndx_buf;
1972 Elf_External_Sym_Shndx *shndx;
1973 struct elf_link_hash_entry ** sym_hashes;
1974 struct elf_link_hash_entry ** end_hashes;
1975 unsigned int symcount;
1977 contents = elf_section_data (sec)->this_hdr.contents;
1981 irel = elf_section_data (sec)->relocs;
1982 irelend = irel + sec->reloc_count;
1984 /* Actually delete the bytes. */
1985 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1988 /* Adjust all the relocs. */
1989 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1991 /* Get the new reloc address. */
1992 if (irel->r_offset > addr && irel->r_offset < toaddr)
1993 irel->r_offset -= count;
1995 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1996 && irel->r_addend == 0x10 /* one byte insn, no relocs */
1997 && irel->r_offset + 1 < addr
1998 && irel->r_offset + 7 > addr)
2001 unsigned char *insn = &contents[irel->r_offset];
2003 /* This is a JMP.S, which we have to manually update. */
2004 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2006 if ((*insn & 0xf8) != 0x60)
2012 if ((*insn & 0xce) != 0x4a)
2014 disp = ((disp & 0x30) >> 3) | (disp & 1);
2016 if (irel->r_offset + disp + 2 >= addr+count)
2019 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2021 *insn = (*insn & 0xf8) | disp;
2025 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
2031 /* Adjust the local symbols defined in this section. */
2032 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2033 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2035 isymend = isym + symtab_hdr->sh_info;
2037 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2038 if (elf_symtab_shndx_list (abfd))
2040 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2041 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2050 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
2052 /* If the symbol is in the range of memory we just moved, we
2053 have to adjust its value. */
2054 if ((int) isym->st_shndx == sec_shndx
2055 && isym->st_value > addr
2056 && isym->st_value < toaddr)
2058 isym->st_value -= count;
2060 /* If the symbol *spans* the bytes we just deleted (i.e. it's
2061 *end* is in the moved bytes but it's *start* isn't), then we
2062 must adjust its size. */
2063 if ((int) isym->st_shndx == sec_shndx
2064 && isym->st_value < addr
2065 && isym->st_value + isym->st_size > addr
2066 && isym->st_value + isym->st_size < toaddr)
2068 isym->st_size -= count;
2072 /* Now adjust the global symbols defined in this section. */
2073 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2074 - symtab_hdr->sh_info);
2075 sym_hashes = elf_sym_hashes (abfd);
2076 // sym_hashes += symtab_hdr->sh_info;
2077 end_hashes = sym_hashes + symcount;
2079 for (; sym_hashes < end_hashes; sym_hashes ++)
2081 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2084 (sym_hash->root.type == bfd_link_hash_defined
2085 || sym_hash->root.type == bfd_link_hash_defweak)
2086 && sym_hash->root.u.def.section == sec)
2088 if (sym_hash->root.u.def.value > addr
2089 && sym_hash->root.u.def.value < toaddr)
2091 sym_hash->root.u.def.value -= count;
2093 if (sym_hash->root.u.def.value < addr
2094 && sym_hash->root.u.def.value + sym_hash->size > addr
2095 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2097 sym_hash->size -= count;
2105 /* This is for versions of gcc prior to 4.3. */
2107 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd,
2108 const asection *sec ATTRIBUTE_UNUSED)
2110 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2117 #define ELF_ARCH bfd_arch_m32c
2118 #define ELF_MACHINE_CODE EM_M32C
2119 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2120 #define ELF_MAXPAGESIZE 0x100
2123 #define TARGET_BIG_SYM m32c_elf32_vec
2124 #define TARGET_BIG_NAME "elf32-m32c"
2126 #define TARGET_LITTLE_SYM m32c_elf32_vec
2127 #define TARGET_LITTLE_NAME "elf32-m32c"
2130 #define elf_info_to_howto_rel NULL
2131 #define elf_info_to_howto m32c_info_to_howto_rela
2132 #define elf_backend_object_p m32c_elf_object_p
2133 #define elf_backend_relocate_section m32c_elf_relocate_section
2134 #define elf_backend_check_relocs m32c_elf_check_relocs
2135 #define elf_backend_object_p m32c_elf_object_p
2136 #define elf_symbol_leading_char ('_')
2137 #define elf_backend_always_size_sections \
2138 m32c_elf_always_size_sections
2139 #define elf_backend_finish_dynamic_sections \
2140 m32c_elf_finish_dynamic_sections
2142 #define elf_backend_can_gc_sections 1
2143 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2145 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2146 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2147 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2148 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2149 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2150 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2152 #include "elf32-target.h"