1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2016 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
27 /* Forward declarations. */
28 static reloc_howto_type * m32c_reloc_type_lookup
29 (bfd *, bfd_reloc_code_real_type);
30 static void m32c_info_to_howto_rela
31 (bfd *, arelent *, Elf_Internal_Rela *);
32 static bfd_boolean m32c_elf_relocate_section
33 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
34 static bfd_boolean m32c_elf_check_relocs
35 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
36 static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
38 char * m32c_get_reloc (long reloc);
39 void dump_symtab (bfd *, void *, void *);
41 static bfd_boolean m32c_elf_relax_section
42 (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again);
43 static bfd_reloc_status_type m32c_apply_reloc_24
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static reloc_howto_type m32c_elf_howto_table [] =
49 /* This reloc does nothing. */
50 HOWTO (R_M32C_NONE, /* type */
52 3, /* size (0 = byte, 1 = short, 2 = long) */
54 FALSE, /* pc_relative */
56 complain_overflow_dont, /* complain_on_overflow */
57 bfd_elf_generic_reloc, /* special_function */
58 "R_M32C_NONE", /* name */
59 FALSE, /* partial_inplace */
62 FALSE), /* pcrel_offset */
64 /* GCC intentionally overflows these next two in order to work
65 around limitations in the addressing modes, so don't complain
67 HOWTO (R_M32C_16, /* type */
69 1, /* size (0 = byte, 1 = short, 2 = long) */
71 FALSE, /* pc_relative */
73 complain_overflow_dont, /* complain_on_overflow */
74 bfd_elf_generic_reloc, /* special_function */
75 "R_M32C_16", /* name */
76 FALSE, /* partial_inplace */
78 0xffff, /* dst_mask */
79 FALSE), /* pcrel_offset */
81 HOWTO (R_M32C_24, /* type */
83 2, /* size (0 = byte, 1 = short, 2 = long) */
85 FALSE, /* pc_relative */
87 complain_overflow_dont, /* complain_on_overflow */
88 m32c_apply_reloc_24, /* special_function */
89 "R_M32C_24", /* name */
90 FALSE, /* partial_inplace */
92 0xffffff, /* dst_mask */
93 FALSE), /* pcrel_offset */
95 HOWTO (R_M32C_32, /* type */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
99 FALSE, /* pc_relative */
101 complain_overflow_bitfield, /* complain_on_overflow */
102 bfd_elf_generic_reloc, /* special_function */
103 "R_M32C_32", /* name */
104 FALSE, /* partial_inplace */
106 0xffffffff, /* dst_mask */
107 FALSE), /* pcrel_offset */
109 HOWTO (R_M32C_8_PCREL, /* type */
111 0, /* size (0 = byte, 1 = short, 2 = long) */
113 TRUE, /* pc_relative */
115 complain_overflow_signed, /* complain_on_overflow */
116 bfd_elf_generic_reloc, /* special_function */
117 "R_M32C_8_PCREL", /* name */
118 FALSE, /* partial_inplace */
121 TRUE), /* pcrel_offset */
123 HOWTO (R_M32C_16_PCREL, /* type */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
127 TRUE, /* pc_relative */
129 complain_overflow_signed, /* complain_on_overflow */
130 bfd_elf_generic_reloc, /* special_function */
131 "R_M32C_16_PCREL", /* name */
132 FALSE, /* partial_inplace */
134 0xffff, /* dst_mask */
135 TRUE), /* pcrel_offset */
137 HOWTO (R_M32C_8, /* type */
139 0, /* size (0 = byte, 1 = short, 2 = long) */
141 FALSE, /* pc_relative */
143 complain_overflow_unsigned, /* complain_on_overflow */
144 bfd_elf_generic_reloc, /* special_function */
145 "R_M32C_8", /* name */
146 FALSE, /* partial_inplace */
149 FALSE), /* pcrel_offset */
151 HOWTO (R_M32C_LO16, /* type */
153 1, /* size (0 = byte, 1 = short, 2 = long) */
155 FALSE, /* pc_relative */
157 complain_overflow_dont, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_M32C_LO16", /* name */
160 FALSE, /* partial_inplace */
162 0xffff, /* dst_mask */
163 FALSE), /* pcrel_offset */
165 HOWTO (R_M32C_HI8, /* type */
167 0, /* size (0 = byte, 1 = short, 2 = long) */
169 FALSE, /* pc_relative */
171 complain_overflow_dont, /* complain_on_overflow */
172 bfd_elf_generic_reloc, /* special_function */
173 "R_M32C_HI8", /* name */
174 FALSE, /* partial_inplace */
177 FALSE), /* pcrel_offset */
179 HOWTO (R_M32C_HI16, /* type */
181 1, /* size (0 = byte, 1 = short, 2 = long) */
183 FALSE, /* pc_relative */
185 complain_overflow_dont, /* complain_on_overflow */
186 bfd_elf_generic_reloc, /* special_function */
187 "R_M32C_HI16", /* name */
188 FALSE, /* partial_inplace */
190 0xffff, /* dst_mask */
191 FALSE), /* pcrel_offset */
193 HOWTO (R_M32C_RL_JUMP, /* type */
195 0, /* size (0 = byte, 1 = short, 2 = long) */
197 FALSE, /* pc_relative */
199 complain_overflow_signed, /* complain_on_overflow */
200 bfd_elf_generic_reloc, /* special_function */
201 "R_M32C_RL_JUMP", /* name */
202 FALSE, /* partial_inplace */
205 FALSE), /* pcrel_offset */
207 HOWTO (R_M32C_RL_1ADDR, /* type */
209 0, /* size (0 = byte, 1 = short, 2 = long) */
211 FALSE, /* pc_relative */
213 complain_overflow_signed, /* complain_on_overflow */
214 bfd_elf_generic_reloc, /* special_function */
215 "R_M32C_RL_1ADDR", /* name */
216 FALSE, /* partial_inplace */
219 FALSE), /* pcrel_offset */
221 HOWTO (R_M32C_RL_2ADDR, /* type */
223 0, /* size (0 = byte, 1 = short, 2 = long) */
225 FALSE, /* pc_relative */
227 complain_overflow_signed, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_M32C_RL_2ADDR", /* name */
230 FALSE, /* partial_inplace */
233 FALSE), /* pcrel_offset */
237 /* Map BFD reloc types to M32C ELF reloc types. */
239 struct m32c_reloc_map
241 bfd_reloc_code_real_type bfd_reloc_val;
242 unsigned int m32c_reloc_val;
245 static const struct m32c_reloc_map m32c_reloc_map [] =
247 { BFD_RELOC_NONE, R_M32C_NONE },
248 { BFD_RELOC_16, R_M32C_16 },
249 { BFD_RELOC_24, R_M32C_24 },
250 { BFD_RELOC_32, R_M32C_32 },
251 { BFD_RELOC_8_PCREL, R_M32C_8_PCREL },
252 { BFD_RELOC_16_PCREL, R_M32C_16_PCREL },
253 { BFD_RELOC_8, R_M32C_8 },
254 { BFD_RELOC_LO16, R_M32C_LO16 },
255 { BFD_RELOC_HI16, R_M32C_HI16 },
256 { BFD_RELOC_M32C_HI8, R_M32C_HI8 },
257 { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
258 { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR },
259 { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR }
262 static reloc_howto_type *
263 m32c_reloc_type_lookup
264 (bfd * abfd ATTRIBUTE_UNUSED,
265 bfd_reloc_code_real_type code)
269 for (i = ARRAY_SIZE (m32c_reloc_map); i--;)
270 if (m32c_reloc_map [i].bfd_reloc_val == code)
271 return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
276 static reloc_howto_type *
277 m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
282 i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
284 if (m32c_elf_howto_table[i].name != NULL
285 && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
286 return &m32c_elf_howto_table[i];
291 /* Set the howto pointer for an M32C ELF reloc. */
294 m32c_info_to_howto_rela
295 (bfd * abfd ATTRIBUTE_UNUSED,
297 Elf_Internal_Rela * dst)
301 r_type = ELF32_R_TYPE (dst->r_info);
302 if (r_type >= (unsigned int) R_M32C_max)
304 _bfd_error_handler (_("%B: invalid M32C reloc number: %d"), abfd, r_type);
307 cache_ptr->howto = & m32c_elf_howto_table [r_type];
312 /* Apply R_M32C_24 relocations. We have to do this because it's not a
313 power-of-two size, and the generic code may think it overruns the
314 section if it's right at the end.
316 Must return something other than bfd_reloc_continue to avoid the
317 above problem. Typical return values include bfd_reloc_ok or
321 static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
322 arelent *reloc_entry,
324 void *vdata_start ATTRIBUTE_UNUSED,
325 asection *input_section,
326 bfd *ibfd ATTRIBUTE_UNUSED,
327 char **error_msg ATTRIBUTE_UNUSED)
330 bfd_reloc_status_type s;
332 s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
334 input_section, ibfd, error_msg);
335 if (s != bfd_reloc_continue)
338 /* Get symbol value. (Common symbols are special.) */
339 if (bfd_is_com_section (symbol->section))
342 relocation = symbol->value;
344 relocation += symbol->section->output_offset;
346 /* Add in supplied addend. */
347 relocation += reloc_entry->addend;
349 reloc_entry->addend = relocation;
350 reloc_entry->address += input_section->output_offset;
354 /* Relocate an M32C ELF section.
355 There is some attempt to make this function usable for many architectures,
356 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
357 if only to serve as a learning tool.
359 The RELOCATE_SECTION function is called by the new ELF backend linker
360 to handle the relocations for a section.
362 The relocs are always passed as Rela structures; if the section
363 actually uses Rel structures, the r_addend field will always be
366 This function is responsible for adjusting the section contents as
367 necessary, and (if using Rela relocs and generating a relocatable
368 output file) adjusting the reloc addend as necessary.
370 This function does not have to worry about setting the reloc
371 address or the reloc symbol index.
373 LOCAL_SYMS is a pointer to the swapped in local symbols.
375 LOCAL_SECTIONS is an array giving the section in the input file
376 corresponding to the st_shndx field of each local symbol.
378 The global hash table entry for the global symbols can be found
379 via elf_sym_hashes (input_bfd).
381 When generating relocatable output, this function must handle
382 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
383 going to be the section symbol corresponding to the output
384 section, which means that the addend must be adjusted
388 m32c_elf_relocate_section
389 (bfd * output_bfd ATTRIBUTE_UNUSED,
390 struct bfd_link_info * info,
392 asection * input_section,
394 Elf_Internal_Rela * relocs,
395 Elf_Internal_Sym * local_syms,
396 asection ** local_sections)
398 Elf_Internal_Shdr * symtab_hdr;
399 struct elf_link_hash_entry ** sym_hashes;
400 Elf_Internal_Rela * rel;
401 Elf_Internal_Rela * relend;
405 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
406 sym_hashes = elf_sym_hashes (input_bfd);
407 relend = relocs + input_section->reloc_count;
409 dynobj = elf_hash_table (info)->dynobj;
412 splt = bfd_get_linker_section (dynobj, ".plt");
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;
693 /* PR15323, ref flags aren't set for references in the same
695 h->root.non_ir_ref = 1;
698 switch (ELF32_R_TYPE (rel->r_info))
700 /* This relocation describes a 16-bit pointer to a function.
701 We may need to allocate a thunk in low memory; reserve memory
705 elf_hash_table (info)->dynobj = dynobj = abfd;
708 splt = bfd_get_linker_section (dynobj, ".plt");
711 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
712 | SEC_IN_MEMORY | SEC_LINKER_CREATED
713 | SEC_READONLY | SEC_CODE);
714 splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
717 || ! bfd_set_section_alignment (dynobj, splt, 1))
723 offset = &h->plt.offset;
726 if (local_plt_offsets == NULL)
731 size = symtab_hdr->sh_info * sizeof (bfd_vma);
732 local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
733 if (local_plt_offsets == NULL)
735 elf_local_got_offsets (abfd) = local_plt_offsets;
737 for (i = 0; i < symtab_hdr->sh_info; i++)
738 local_plt_offsets[i] = (bfd_vma) -1;
740 offset = &local_plt_offsets[r_symndx];
743 if (*offset == (bfd_vma) -1)
745 *offset = splt->size;
755 /* This must exist if dynobj is ever set. */
758 m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
759 struct bfd_link_info *info)
764 /* As an extra sanity check, verify that all plt entries have
767 if ((dynobj = elf_hash_table (info)->dynobj) != NULL
768 && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL)
770 bfd_byte *contents = splt->contents;
771 unsigned int i, size = splt->size;
772 for (i = 0; i < size; i += 4)
774 unsigned int x = bfd_get_32 (dynobj, contents + i);
783 m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
784 struct bfd_link_info *info)
789 if (bfd_link_relocatable (info))
792 dynobj = elf_hash_table (info)->dynobj;
796 splt = bfd_get_linker_section (dynobj, ".plt");
797 BFD_ASSERT (splt != NULL);
799 splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
800 if (splt->contents == NULL)
806 /* Function to set the ELF flag bits. */
809 m32c_elf_set_private_flags (bfd *abfd, flagword flags)
811 elf_elfheader (abfd)->e_flags = flags;
812 elf_flags_init (abfd) = TRUE;
816 /* Merge backend specific data from an object file to the output
817 object file when linking. */
820 m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
822 flagword old_flags, old_partial;
823 flagword new_flags, new_partial;
824 bfd_boolean error = FALSE;
828 new_opt[0] = old_opt[0] = '\0';
829 new_flags = elf_elfheader (ibfd)->e_flags;
830 old_flags = elf_elfheader (obfd)->e_flags;
834 ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
835 old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
836 bfd_get_filename (ibfd));
839 if (!elf_flags_init (obfd))
841 /* First call, no flags set. */
842 elf_flags_init (obfd) = TRUE;
843 elf_elfheader (obfd)->e_flags = new_flags;
846 else if (new_flags == old_flags)
847 /* Compatible flags are ok. */
850 else /* Possibly incompatible flags. */
852 /* Warn if different cpu is used (allow a specific cpu to override
854 new_partial = (new_flags & EF_M32C_CPU_MASK);
855 old_partial = (old_flags & EF_M32C_CPU_MASK);
856 if (new_partial == old_partial)
863 default: strcat (new_opt, " -m16c"); break;
864 case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break;
865 case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break;
870 default: strcat (old_opt, " -m16c"); break;
871 case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break;
872 case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break;
876 /* Print out any mismatches from above. */
881 (_("%s: compiled with %s and linked with modules compiled with %s"),
882 bfd_get_filename (ibfd), new_opt, old_opt);
885 new_flags &= ~ EF_M32C_ALL_FLAGS;
886 old_flags &= ~ EF_M32C_ALL_FLAGS;
888 /* Warn about any other mismatches. */
889 if (new_flags != old_flags)
893 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
894 bfd_get_filename (ibfd), (long)new_flags, (long)old_flags);
899 bfd_set_error (bfd_error_bad_value);
906 m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
908 FILE *file = (FILE *) ptr;
911 BFD_ASSERT (abfd != NULL && ptr != NULL);
913 /* Print normal ELF private data. */
914 _bfd_elf_print_private_bfd_data (abfd, ptr);
916 flags = elf_elfheader (abfd)->e_flags;
917 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
919 switch (flags & EF_M32C_CPU_MASK)
922 case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
923 case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break;
930 /* Return the MACH for an e_flags value. */
933 elf32_m32c_machine (bfd *abfd)
935 switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
937 case EF_M32C_CPU_M16C: return bfd_mach_m16c;
938 case EF_M32C_CPU_M32C: return bfd_mach_m32c;
941 return bfd_mach_m16c;
945 m32c_elf_object_p (bfd *abfd)
947 bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
948 elf32_m32c_machine (abfd));
955 dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
958 Elf_Internal_Sym *isymbuf;
959 Elf_Internal_Sym *isymend;
960 Elf_Internal_Sym *isym;
961 Elf_Internal_Shdr *symtab_hdr;
962 bfd_boolean free_internal = 0, free_external = 0;
964 char * st_info_stb_str;
970 internal_syms = bfd_malloc (1000);
975 external_syms = bfd_malloc (1000);
979 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
980 locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
982 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
983 symtab_hdr->sh_info, 0,
984 internal_syms, external_syms, NULL);
986 isymbuf = internal_syms;
987 isymend = isymbuf + locsymcount;
989 for (isym = isymbuf ; isym < isymend ; isym++)
991 switch (ELF_ST_TYPE (isym->st_info))
994 st_info_str = "STT_FUNC";
998 st_info_str = "STT_SECTION";
1002 st_info_str = "STT_FILE";
1006 st_info_str = "STT_OBJECT";
1010 st_info_str = "STT_TLS";
1017 switch (ELF_ST_BIND (isym->st_info))
1020 st_info_stb_str = "STB_LOCAL";
1024 st_info_stb_str = "STB_GLOBAL";
1028 st_info_stb_str = "";
1031 switch (ELF_ST_VISIBILITY (isym->st_other))
1034 st_other_str = "STV_DEFAULT";
1038 st_other_str = "STV_INTERNAL";
1042 st_other_str = "STV_PROTECTED";
1049 switch (isym->st_shndx)
1052 st_shndx_str = "SHN_ABS";
1056 st_shndx_str = "SHN_COMMON";
1060 st_shndx_str = "SHN_UNDEF";
1067 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1068 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1070 (unsigned long) isym->st_value,
1071 (unsigned long) isym->st_size,
1073 bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1075 isym->st_info, st_info_str, st_info_stb_str,
1076 isym->st_other, st_other_str,
1077 isym->st_shndx, st_shndx_str);
1080 free (internal_syms);
1082 free (external_syms);
1086 m32c_get_reloc (long reloc)
1088 if (0 <= reloc && reloc < R_M32C_max)
1089 return m32c_elf_howto_table[reloc].name;
1095 /* Handle relaxing. */
1097 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1098 is within the low 64k, remove any entry for it in the plt. */
1100 struct relax_plt_data
1107 m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1109 struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1111 if (h->plt.offset != (bfd_vma) -1)
1115 if (h->root.type == bfd_link_hash_undefined
1116 || h->root.type == bfd_link_hash_undefweak)
1119 address = (h->root.u.def.section->output_section->vma
1120 + h->root.u.def.section->output_offset
1121 + h->root.u.def.value);
1123 if (address <= 0xffff)
1126 data->splt->size -= 4;
1127 *data->again = TRUE;
1134 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1135 previously had a plt entry, give it a new entry offset. */
1138 m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1140 bfd_vma *entry = (bfd_vma *) xdata;
1142 if (h->plt.offset != (bfd_vma) -1)
1144 h->plt.offset = *entry;
1152 m32c_elf_relax_plt_section (asection *splt,
1153 struct bfd_link_info *info,
1156 struct relax_plt_data relax_plt_data;
1159 /* Assume nothing changes. */
1162 if (bfd_link_relocatable (info))
1165 /* Quick check for an empty plt. */
1166 if (splt->size == 0)
1169 /* Map across all global symbols; see which ones happen to
1170 fall in the low 64k. */
1171 relax_plt_data.splt = splt;
1172 relax_plt_data.again = again;
1173 elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1176 /* Likewise for local symbols, though that's somewhat less convenient
1177 as we have to walk the list of input bfds and swap in symbol data. */
1178 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1180 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1181 Elf_Internal_Shdr *symtab_hdr;
1182 Elf_Internal_Sym *isymbuf = NULL;
1185 if (! local_plt_offsets)
1188 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1189 if (symtab_hdr->sh_info != 0)
1191 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1192 if (isymbuf == NULL)
1193 isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1194 symtab_hdr->sh_info, 0,
1196 if (isymbuf == NULL)
1200 for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1202 Elf_Internal_Sym *isym;
1206 if (local_plt_offsets[idx] == (bfd_vma) -1)
1209 isym = &isymbuf[idx];
1210 if (isym->st_shndx == SHN_UNDEF)
1212 else if (isym->st_shndx == SHN_ABS)
1213 tsec = bfd_abs_section_ptr;
1214 else if (isym->st_shndx == SHN_COMMON)
1215 tsec = bfd_com_section_ptr;
1217 tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1219 address = (tsec->output_section->vma
1220 + tsec->output_offset
1222 if (address <= 0xffff)
1224 local_plt_offsets[idx] = -1;
1231 && symtab_hdr->contents != (unsigned char *) isymbuf)
1233 if (! info->keep_memory)
1237 /* Cache the symbols for elf_link_input_bfd. */
1238 symtab_hdr->contents = (unsigned char *) isymbuf;
1243 /* If we changed anything, walk the symbols again to reallocate
1244 .plt entry addresses. */
1245 if (*again && splt->size > 0)
1249 elf_link_hash_traverse (elf_hash_table (info),
1250 m32c_relax_plt_realloc, &entry);
1252 for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1254 bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1255 unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
1258 if (! local_plt_offsets)
1261 for (idx = 0; idx < nlocals; ++idx)
1262 if (local_plt_offsets[idx] != (bfd_vma) -1)
1264 local_plt_offsets[idx] = entry;
1274 compare_reloc (const void *e1, const void *e2)
1276 const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1277 const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1279 if (i1->r_offset == i2->r_offset)
1282 return i1->r_offset < i2->r_offset ? -1 : 1;
1285 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1287 m32c_offset_for_reloc (bfd *abfd,
1288 Elf_Internal_Rela *rel,
1289 Elf_Internal_Shdr *symtab_hdr,
1290 Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED,
1291 Elf_Internal_Sym *intsyms)
1295 /* Get the value of the symbol referred to by the reloc. */
1296 if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1298 /* A local symbol. */
1299 Elf_Internal_Sym *isym;
1302 isym = intsyms + ELF32_R_SYM (rel->r_info);
1303 ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1304 symval = isym->st_value;
1306 symval += ssec->output_section->vma
1307 + ssec->output_offset;
1312 struct elf_link_hash_entry *h;
1314 /* An external symbol. */
1315 indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1316 h = elf_sym_hashes (abfd)[indx];
1317 BFD_ASSERT (h != NULL);
1319 if (h->root.type != bfd_link_hash_defined
1320 && h->root.type != bfd_link_hash_defweak)
1321 /* This appears to be a reference to an undefined
1322 symbol. Just ignore it--it will be caught by the
1323 regular reloc processing. */
1326 symval = (h->root.u.def.value
1327 + h->root.u.def.section->output_section->vma
1328 + h->root.u.def.section->output_offset);
1333 static int bytes_saved = 0;
1335 static int bytes_to_reloc[] = {
1343 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1345 /* Mask for the number of relocs associated with this insn. */
1346 #define RLA_RELOCS 0x0000000f
1347 /* Number of bytes gas emitted (before gas's relaxing) */
1348 #define RLA_NBYTES 0x00000ff0
1350 /* If the displacement is within the given range and the new encoding
1351 differs from the old encoding (the index), then the insn can be
1352 relaxed to the new encoding. */
1355 unsigned int max_disp;
1356 unsigned char new_encoding;
1359 static EncodingTable m16c_addr_encodings[] = {
1360 { 0, 0, 0 }, /* R0 */
1361 { 0, 0, 1 }, /* R1 */
1362 { 0, 0, 2 }, /* R2 */
1363 { 0, 0, 3 }, /* R3 */
1364 { 0, 0, 4 }, /* A0 */
1365 { 0, 0, 5 }, /* A1 */
1366 { 0, 0, 6 }, /* [A0] */
1367 { 0, 0, 7 }, /* [A1] */
1368 { 1, 0, 6 }, /* udsp:8[A0] */
1369 { 1, 0, 7 }, /* udsp:8[A1] */
1370 { 1, 0, 10 }, /* udsp:8[SB] */
1371 { 1, 0, 11 }, /* sdsp:8[FB] */
1372 { 2, 255, 8 }, /* udsp:16[A0] */
1373 { 2, 255, 9 }, /* udsp:16[A1] */
1374 { 2, 255, 10 }, /* udsp:16[SB] */
1375 { 2, 0, 15 }, /* abs:16 */
1378 static EncodingTable m16c_jmpaddr_encodings[] = {
1379 { 0, 0, 0 }, /* R0 */
1380 { 0, 0, 1 }, /* R1 */
1381 { 0, 0, 2 }, /* R2 */
1382 { 0, 0, 3 }, /* R3 */
1383 { 0, 0, 4 }, /* A0 */
1384 { 0, 0, 5 }, /* A1 */
1385 { 0, 0, 6 }, /* [A0] */
1386 { 0, 0, 7 }, /* [A1] */
1387 { 1, 0, 6 }, /* udsp:8[A0] */
1388 { 1, 0, 7 }, /* udsp:8[A1] */
1389 { 1, 0, 10 }, /* udsp:8[SB] */
1390 { 1, 0, 11 }, /* sdsp:8[FB] */
1391 { 3, 255, 8 }, /* udsp:20[A0] */
1392 { 3, 255, 9 }, /* udsp:20[A1] */
1393 { 2, 255, 10 }, /* udsp:16[SB] */
1394 { 2, 0, 15 }, /* abs:16 */
1397 static EncodingTable m32c_addr_encodings[] = {
1398 { 0, 0, 0 }, /* [A0] */
1399 { 0, 0, 1 }, /* [A1] */
1400 { 0, 0, 2 }, /* A0 */
1401 { 0, 0, 3 }, /* A1 */
1402 { 1, 0, 0 }, /* udsp:8[A0] */
1403 { 1, 0, 1 }, /* udsp:8[A1] */
1404 { 1, 0, 6 }, /* udsp:8[SB] */
1405 { 1, 0, 7 }, /* sdsp:8[FB] */
1406 { 2, 255, 4 }, /* udsp:16[A0] */
1407 { 2, 255, 5 }, /* udsp:16[A1] */
1408 { 2, 255, 6 }, /* udsp:16[SB] */
1409 { 2, 127, 7 }, /* sdsp:16[FB] */
1410 { 3, 65535, 8 }, /* udsp:24[A0] */
1411 { 3, 65535, 9 }, /* udsp:24[A1] */
1412 { 3, 65535, 15 }, /* abs24 */
1413 { 2, 0, 15 }, /* abs16 */
1414 { 0, 0, 16 }, /* R2 */
1415 { 0, 0, 17 }, /* R3 */
1416 { 0, 0, 18 }, /* R0 */
1417 { 0, 0, 19 }, /* R1 */
1433 m32c_elf_relax_section
1436 struct bfd_link_info * link_info,
1437 bfd_boolean * again)
1439 Elf_Internal_Shdr *symtab_hdr;
1440 Elf_Internal_Shdr *shndx_hdr;
1441 Elf_Internal_Rela *internal_relocs;
1442 Elf_Internal_Rela *free_relocs = NULL;
1443 Elf_Internal_Rela *irel, *irelend, *srel;
1444 bfd_byte * contents = NULL;
1445 bfd_byte * free_contents = NULL;
1446 Elf_Internal_Sym *intsyms = NULL;
1447 Elf_Internal_Sym *free_intsyms = NULL;
1448 Elf_External_Sym_Shndx *shndx_buf = NULL;
1451 if (abfd == elf_hash_table (link_info)->dynobj
1452 && (sec->flags & SEC_LINKER_CREATED) != 0
1453 && strcmp (sec->name, ".plt") == 0)
1454 return m32c_elf_relax_plt_section (sec, link_info, again);
1456 /* Assume nothing changes. */
1459 machine = elf32_m32c_machine (abfd);
1461 /* We don't have to do anything for a relocatable link, if
1462 this section does not have relocs, or if this is not a
1464 if (bfd_link_relocatable (link_info)
1465 || (sec->flags & SEC_RELOC) == 0
1466 || sec->reloc_count == 0
1467 || (sec->flags & SEC_CODE) == 0)
1470 symtab_hdr = & elf_symtab_hdr (abfd);
1471 if (elf_symtab_shndx_list (abfd))
1472 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
1476 /* Get the section contents. */
1477 if (elf_section_data (sec)->this_hdr.contents != NULL)
1478 contents = elf_section_data (sec)->this_hdr.contents;
1479 /* Go get them off disk. */
1480 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1483 /* Read this BFD's symbols. */
1484 /* Get cached copy if it exists. */
1485 if (symtab_hdr->contents != NULL)
1487 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1491 intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1492 symtab_hdr->contents = (bfd_byte *) intsyms;
1495 if (shndx_hdr && shndx_hdr->sh_size != 0)
1499 amt = symtab_hdr->sh_info;
1500 amt *= sizeof (Elf_External_Sym_Shndx);
1501 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1502 if (shndx_buf == NULL)
1504 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1505 || bfd_bread (shndx_buf, amt, abfd) != amt)
1507 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1510 /* Get a copy of the native relocations. */
1511 internal_relocs = (_bfd_elf_link_read_relocs
1512 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1513 link_info->keep_memory));
1514 if (internal_relocs == NULL)
1516 if (! link_info->keep_memory)
1517 free_relocs = internal_relocs;
1519 /* The RL_ relocs must be just before the operand relocs they go
1520 with, so we must sort them to guarantee this. */
1521 qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1524 /* Walk through them looking for relaxing opportunities. */
1525 irelend = internal_relocs + sec->reloc_count;
1527 for (irel = internal_relocs; irel < irelend; irel++)
1530 unsigned char *insn, *gap, *einsn;
1532 bfd_signed_vma pcrel;
1538 EncodingTable *enctbl;
1541 if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1542 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1543 && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1548 /* There will always be room for the relaxed insn, since it is smaller
1549 than the one it would replace. */
1550 BFD_ASSERT (irel->r_offset < sec->size);
1552 insn = contents + irel->r_offset;
1553 relax_relocs = irel->r_addend % 16;
1555 /* Ok, we only have three relocs we care about, and they're all
1556 fake. The lower four bits of the addend is always the number
1557 of following relocs (hence the qsort above) that are assigned
1558 to this opcode. The next 8 bits of the addend indicates the
1559 number of bytes in the insn. We use the rest of them
1560 ourselves as flags for the more expensive operations (defines
1561 above). The three relocs are:
1563 RL_JUMP: This marks all direct jump insns. We check the
1564 displacement and replace them with shorter jumps if
1565 they're in range. We also use this to find JMP.S
1566 insns and manually shorten them when we delete bytes.
1567 We have to decode these insns to figure out what to
1570 RL_1ADDR: This is a :G or :Q insn, which has a single
1571 "standard" operand. We have to extract the type
1572 field, see if it's a wide displacement, then figure
1573 out if we can replace it with a narrow displacement.
1574 We don't have to decode these insns.
1576 RL_2ADDR: Similarly, but two "standard" operands. Note that
1577 r_addend may still be 1, as standard operands don't
1578 always have displacements. Gas shouldn't give us one
1579 with zero operands, but since we don't know which one
1580 has the displacement, we check them both anyway.
1582 These all point to the beginning of the insn itself, not the
1585 Note that we only relax one step at a time, relying on the
1586 linker to call us repeatedly. Thus, there is no code for
1587 JMP.A->JMP.B although that will happen in two steps.
1588 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1591 /* Get the value of the symbol referred to by the reloc. Just
1592 in case this is the last reloc in the list, use the RL's
1593 addend to choose between this reloc (no addend) or the next
1594 (yes addend, which means at least one following reloc). */
1595 srel = irel + (relax_relocs ? 1 : 0);
1596 symval = OFFSET_FOR_RELOC (srel);
1598 /* Setting gap_size nonzero is the flag which means "something
1602 new_type = ELF32_R_TYPE(srel->r_info);
1604 pc = sec->output_section->vma + sec->output_offset
1606 pcrel = symval - pc + srel->r_addend;
1608 if (machine == bfd_mach_m16c)
1612 switch (ELF32_R_TYPE(irel->r_info))
1615 case R_M32C_RL_JUMP:
1618 case 0xfe: /* jmp.b */
1619 if (pcrel >= 2 && pcrel <= 9)
1621 /* Relax JMP.B -> JMP.S. We need to get rid of
1622 the following reloc though. */
1623 insn[0] = 0x60 | (pcrel - 2);
1624 new_type = R_M32C_NONE;
1625 irel->r_addend = 0x10;
1631 case 0xf4: /* jmp.w */
1632 /* 128 is allowed because it will be one byte closer
1633 after relaxing. Likewise for all other pc-rel
1635 if (pcrel <= 128 && pcrel >= -128)
1637 /* Relax JMP.W -> JMP.B */
1640 new_type = R_M32C_8_PCREL;
1646 case 0xfc: /* jmp.a */
1647 if (pcrel <= 32768 && pcrel >= -32768)
1649 /* Relax JMP.A -> JMP.W */
1653 new_type = R_M32C_16_PCREL;
1659 case 0xfd: /* jsr.a */
1660 if (pcrel <= 32768 && pcrel >= -32768)
1662 /* Relax JSR.A -> JSR.W */
1666 new_type = R_M32C_16_PCREL;
1674 case R_M32C_RL_2ADDR:
1675 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1677 enctbl = m16c_addr_encodings;
1679 enc = (insn[1] >> 4) & 0x0f;
1682 if (srel->r_offset == irel->r_offset + posn
1683 && e->new_encoding != enc
1684 && symval <= e->max_disp)
1687 insn[1] |= e->new_encoding << 4;
1688 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1689 gap = insn + posn + enctbl[e->new_encoding].bytes;
1690 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1693 if (relax_relocs == 2)
1699 case R_M32C_RL_1ADDR:
1700 /* xxxx xxxx xxxx dest [disp] */
1702 enctbl = m16c_addr_encodings;
1705 /* Check the opcode for jumps. We know it's safe to
1706 do this because all 2ADDR insns are at least two
1708 enc = insn[0] * 256 + insn[1];
1715 enctbl = m16c_jmpaddr_encodings;
1719 /* srel, posn, and enc must be set here. */
1721 symval = OFFSET_FOR_RELOC (srel);
1722 enc = insn[1] & 0x0f;
1725 if (srel->r_offset == irel->r_offset + posn
1726 && e->new_encoding != enc
1727 && symval <= e->max_disp)
1730 insn[1] |= e->new_encoding;
1731 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1732 gap = insn + posn + enctbl[e->new_encoding].bytes;
1733 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1739 } /* Ends switch (reloc type) for m16c. */
1741 else /* machine == bfd_mach_m32c */
1745 switch (ELF32_R_TYPE(irel->r_info))
1748 case R_M32C_RL_JUMP:
1751 case 0xbb: /* jmp.b */
1752 if (pcrel >= 2 && pcrel <= 9)
1755 /* Relax JMP.B -> JMP.S. We need to get rid of
1756 the following reloc though. */
1757 insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1758 new_type = R_M32C_NONE;
1759 irel->r_addend = 0x10;
1765 case 0xce: /* jmp.w */
1766 if (pcrel <= 128 && pcrel >= -128)
1768 /* Relax JMP.W -> JMP.B */
1771 new_type = R_M32C_8_PCREL;
1777 case 0xcc: /* jmp.a */
1778 if (pcrel <= 32768 && pcrel >= -32768)
1780 /* Relax JMP.A -> JMP.W */
1784 new_type = R_M32C_16_PCREL;
1790 case 0xcd: /* jsr.a */
1791 if (pcrel <= 32768 && pcrel >= -32768)
1793 /* Relax JSR.A -> JSR.W */
1797 new_type = R_M32C_16_PCREL;
1805 case R_M32C_RL_2ADDR:
1806 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1812 /* prefix; remove it as far as the RL reloc is concerned. */
1817 enctbl = m32c_addr_encodings;
1818 enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1821 if (srel->r_offset == irel->r_offset + posn
1822 && e->new_encoding != enc
1823 && symval <= e->max_disp)
1826 einsn[0] |= (e->new_encoding & 0x1c) << 2;
1828 einsn[1] |= (e->new_encoding & 0x03) << 4;
1829 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1830 gap = insn + posn + enctbl[e->new_encoding].bytes;
1831 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1834 if (relax_relocs == 2)
1840 case R_M32C_RL_1ADDR:
1841 /* xxxx DDDx DDxx xxxx [disp] */
1847 /* prefix; remove it as far as the RL reloc is concerned. */
1852 enctbl = m32c_addr_encodings;
1855 /* srel, posn, and enc must be set here. */
1857 symval = OFFSET_FOR_RELOC (srel);
1858 enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6);
1861 if (srel->r_offset == irel->r_offset + posn
1862 && e->new_encoding != enc
1863 && symval <= e->max_disp)
1866 einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1868 einsn[1] |= (e->new_encoding & 0x03) << 6;
1869 gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1870 gap = insn + posn + enctbl[e->new_encoding].bytes;
1871 new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1877 } /* Ends switch (reloc type) for m32c. */
1885 srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1887 /* Note that we've changed the relocs, section contents, etc. */
1888 elf_section_data (sec)->relocs = internal_relocs;
1891 elf_section_data (sec)->this_hdr.contents = contents;
1892 free_contents = NULL;
1894 symtab_hdr->contents = (bfd_byte *) intsyms;
1895 free_intsyms = NULL;
1897 bytes_saved += gap_size;
1899 if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1902 } /* next relocation */
1904 if (free_relocs != NULL)
1910 if (free_contents != NULL)
1912 if (! link_info->keep_memory)
1913 free (free_contents);
1914 /* Cache the section contents for elf_link_input_bfd. */
1916 elf_section_data (sec)->this_hdr.contents = contents;
1918 free_contents = NULL;
1921 if (shndx_buf != NULL)
1923 shndx_hdr->contents = NULL;
1927 if (free_intsyms != NULL)
1929 if (! link_info->keep_memory)
1930 free (free_intsyms);
1931 /* Cache the symbols for elf_link_input_bfd. */
1934 symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1937 free_intsyms = NULL;
1943 if (free_relocs != NULL)
1945 if (free_contents != NULL)
1946 free (free_contents);
1947 if (shndx_buf != NULL)
1949 shndx_hdr->contents = NULL;
1952 if (free_intsyms != NULL)
1953 free (free_intsyms);
1957 /* Delete some bytes from a section while relaxing. */
1960 m32c_elf_relax_delete_bytes
1966 Elf_Internal_Shdr *symtab_hdr;
1967 Elf_Internal_Shdr *shndx_hdr;
1970 Elf_Internal_Rela *irel;
1971 Elf_Internal_Rela *irelend;
1973 Elf_Internal_Sym *isym;
1974 Elf_Internal_Sym *isymend;
1975 Elf_Internal_Sym *intsyms;
1976 Elf_External_Sym_Shndx *shndx_buf;
1977 Elf_External_Sym_Shndx *shndx;
1978 struct elf_link_hash_entry ** sym_hashes;
1979 struct elf_link_hash_entry ** end_hashes;
1980 unsigned int symcount;
1982 contents = elf_section_data (sec)->this_hdr.contents;
1986 irel = elf_section_data (sec)->relocs;
1987 irelend = irel + sec->reloc_count;
1989 /* Actually delete the bytes. */
1990 memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1993 /* Adjust all the relocs. */
1994 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1996 /* Get the new reloc address. */
1997 if (irel->r_offset > addr && irel->r_offset < toaddr)
1998 irel->r_offset -= count;
2000 if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
2001 && irel->r_addend == 0x10 /* one byte insn, no relocs */
2002 && irel->r_offset + 1 < addr
2003 && irel->r_offset + 7 > addr)
2006 unsigned char *insn = &contents[irel->r_offset];
2008 /* This is a JMP.S, which we have to manually update. */
2009 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2011 if ((*insn & 0xf8) != 0x60)
2017 if ((*insn & 0xce) != 0x4a)
2019 disp = ((disp & 0x30) >> 3) | (disp & 1);
2021 if (irel->r_offset + disp + 2 >= addr+count)
2024 if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2026 *insn = (*insn & 0xf8) | disp;
2030 *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
2036 /* Adjust the local symbols defined in this section. */
2037 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2038 intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2040 isymend = isym + symtab_hdr->sh_info;
2042 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2043 if (elf_symtab_shndx_list (abfd))
2045 shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2046 shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2055 for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
2057 /* If the symbol is in the range of memory we just moved, we
2058 have to adjust its value. */
2059 if ((int) isym->st_shndx == sec_shndx
2060 && isym->st_value > addr
2061 && isym->st_value < toaddr)
2063 isym->st_value -= count;
2065 /* If the symbol *spans* the bytes we just deleted (i.e. it's
2066 *end* is in the moved bytes but it's *start* isn't), then we
2067 must adjust its size. */
2068 if ((int) isym->st_shndx == sec_shndx
2069 && isym->st_value < addr
2070 && isym->st_value + isym->st_size > addr
2071 && isym->st_value + isym->st_size < toaddr)
2073 isym->st_size -= count;
2077 /* Now adjust the global symbols defined in this section. */
2078 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2079 - symtab_hdr->sh_info);
2080 sym_hashes = elf_sym_hashes (abfd);
2081 // sym_hashes += symtab_hdr->sh_info;
2082 end_hashes = sym_hashes + symcount;
2084 for (; sym_hashes < end_hashes; sym_hashes ++)
2086 struct elf_link_hash_entry * sym_hash = * sym_hashes;
2089 (sym_hash->root.type == bfd_link_hash_defined
2090 || sym_hash->root.type == bfd_link_hash_defweak)
2091 && sym_hash->root.u.def.section == sec)
2093 if (sym_hash->root.u.def.value > addr
2094 && sym_hash->root.u.def.value < toaddr)
2096 sym_hash->root.u.def.value -= count;
2098 if (sym_hash->root.u.def.value < addr
2099 && sym_hash->root.u.def.value + sym_hash->size > addr
2100 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2102 sym_hash->size -= count;
2110 /* This is for versions of gcc prior to 4.3. */
2112 _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED)
2114 if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2121 #define ELF_ARCH bfd_arch_m32c
2122 #define ELF_MACHINE_CODE EM_M32C
2123 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2124 #define ELF_MAXPAGESIZE 0x100
2127 #define TARGET_BIG_SYM m32c_elf32_vec
2128 #define TARGET_BIG_NAME "elf32-m32c"
2130 #define TARGET_LITTLE_SYM m32c_elf32_vec
2131 #define TARGET_LITTLE_NAME "elf32-m32c"
2134 #define elf_info_to_howto_rel NULL
2135 #define elf_info_to_howto m32c_info_to_howto_rela
2136 #define elf_backend_object_p m32c_elf_object_p
2137 #define elf_backend_relocate_section m32c_elf_relocate_section
2138 #define elf_backend_check_relocs m32c_elf_check_relocs
2139 #define elf_backend_object_p m32c_elf_object_p
2140 #define elf_symbol_leading_char ('_')
2141 #define elf_backend_always_size_sections \
2142 m32c_elf_always_size_sections
2143 #define elf_backend_finish_dynamic_sections \
2144 m32c_elf_finish_dynamic_sections
2146 #define elf_backend_can_gc_sections 1
2147 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2149 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2150 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2151 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2152 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2153 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2154 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2156 #include "elf32-target.h"