1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005-2014 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "libiberty.h"
27 /* Forward declarations. */
28 static reloc_howto_type
* m32c_reloc_type_lookup
29 (bfd
*, bfd_reloc_code_real_type
);
30 static void m32c_info_to_howto_rela
31 (bfd
*, arelent
*, Elf_Internal_Rela
*);
32 static bfd_boolean m32c_elf_relocate_section
33 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
34 static bfd_boolean m32c_elf_check_relocs
35 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
36 static bfd_boolean
m32c_elf_relax_delete_bytes (bfd
*, asection
*, bfd_vma
, int);
38 char * m32c_get_reloc (long reloc
);
39 void dump_symtab (bfd
*, void *, void *);
41 static bfd_boolean m32c_elf_relax_section
42 (bfd
*abfd
, asection
*sec
, struct bfd_link_info
*link_info
, bfd_boolean
*again
);
45 static reloc_howto_type m32c_elf_howto_table
[] =
47 /* This reloc does nothing. */
48 HOWTO (R_M32C_NONE
, /* type */
50 0, /* size (0 = byte, 1 = short, 2 = long) */
52 FALSE
, /* pc_relative */
54 complain_overflow_bitfield
, /* complain_on_overflow */
55 bfd_elf_generic_reloc
, /* special_function */
56 "R_M32C_NONE", /* name */
57 FALSE
, /* partial_inplace */
60 FALSE
), /* pcrel_offset */
62 /* GCC intentionally overflows these next two in order to work
63 around limitations in the addressing modes, so don't complain
65 HOWTO (R_M32C_16
, /* type */
67 1, /* size (0 = byte, 1 = short, 2 = long) */
69 FALSE
, /* pc_relative */
71 complain_overflow_dont
, /* complain_on_overflow */
72 bfd_elf_generic_reloc
, /* special_function */
73 "R_M32C_16", /* name */
74 FALSE
, /* partial_inplace */
76 0xffff, /* dst_mask */
77 FALSE
), /* pcrel_offset */
79 HOWTO (R_M32C_24
, /* type */
81 2, /* size (0 = byte, 1 = short, 2 = long) */
83 FALSE
, /* pc_relative */
85 complain_overflow_dont
, /* complain_on_overflow */
86 bfd_elf_generic_reloc
, /* special_function */
87 "R_M32C_24", /* name */
88 FALSE
, /* partial_inplace */
90 0xffffff, /* dst_mask */
91 FALSE
), /* pcrel_offset */
93 HOWTO (R_M32C_32
, /* type */
95 2, /* size (0 = byte, 1 = short, 2 = long) */
97 FALSE
, /* pc_relative */
99 complain_overflow_bitfield
, /* complain_on_overflow */
100 bfd_elf_generic_reloc
, /* special_function */
101 "R_M32C_32", /* name */
102 FALSE
, /* partial_inplace */
104 0xffffffff, /* dst_mask */
105 FALSE
), /* pcrel_offset */
107 HOWTO (R_M32C_8_PCREL
, /* type */
109 0, /* size (0 = byte, 1 = short, 2 = long) */
111 TRUE
, /* pc_relative */
113 complain_overflow_signed
, /* complain_on_overflow */
114 bfd_elf_generic_reloc
, /* special_function */
115 "R_M32C_8_PCREL", /* name */
116 FALSE
, /* partial_inplace */
119 TRUE
), /* pcrel_offset */
121 HOWTO (R_M32C_16_PCREL
, /* type */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
125 TRUE
, /* pc_relative */
127 complain_overflow_signed
, /* complain_on_overflow */
128 bfd_elf_generic_reloc
, /* special_function */
129 "R_M32C_16_PCREL", /* name */
130 FALSE
, /* partial_inplace */
132 0xffff, /* dst_mask */
133 TRUE
), /* pcrel_offset */
135 HOWTO (R_M32C_8
, /* type */
137 0, /* size (0 = byte, 1 = short, 2 = long) */
139 FALSE
, /* pc_relative */
141 complain_overflow_unsigned
, /* complain_on_overflow */
142 bfd_elf_generic_reloc
, /* special_function */
143 "R_M32C_8", /* name */
144 FALSE
, /* partial_inplace */
147 FALSE
), /* pcrel_offset */
149 HOWTO (R_M32C_LO16
, /* type */
151 1, /* size (0 = byte, 1 = short, 2 = long) */
153 FALSE
, /* pc_relative */
155 complain_overflow_dont
, /* complain_on_overflow */
156 bfd_elf_generic_reloc
, /* special_function */
157 "R_M32C_LO16", /* name */
158 FALSE
, /* partial_inplace */
160 0xffff, /* dst_mask */
161 FALSE
), /* pcrel_offset */
163 HOWTO (R_M32C_HI8
, /* type */
165 0, /* size (0 = byte, 1 = short, 2 = long) */
167 FALSE
, /* pc_relative */
169 complain_overflow_dont
, /* complain_on_overflow */
170 bfd_elf_generic_reloc
, /* special_function */
171 "R_M32C_HI8", /* name */
172 FALSE
, /* partial_inplace */
175 FALSE
), /* pcrel_offset */
177 HOWTO (R_M32C_HI16
, /* type */
179 1, /* size (0 = byte, 1 = short, 2 = long) */
181 FALSE
, /* pc_relative */
183 complain_overflow_dont
, /* complain_on_overflow */
184 bfd_elf_generic_reloc
, /* special_function */
185 "R_M32C_HI16", /* name */
186 FALSE
, /* partial_inplace */
188 0xffff, /* dst_mask */
189 FALSE
), /* pcrel_offset */
191 HOWTO (R_M32C_RL_JUMP
, /* type */
193 0, /* size (0 = byte, 1 = short, 2 = long) */
195 FALSE
, /* pc_relative */
197 complain_overflow_signed
, /* complain_on_overflow */
198 bfd_elf_generic_reloc
, /* special_function */
199 "R_M32C_RL_JUMP", /* name */
200 FALSE
, /* partial_inplace */
203 FALSE
), /* pcrel_offset */
205 HOWTO (R_M32C_RL_1ADDR
, /* type */
207 0, /* size (0 = byte, 1 = short, 2 = long) */
209 FALSE
, /* pc_relative */
211 complain_overflow_signed
, /* complain_on_overflow */
212 bfd_elf_generic_reloc
, /* special_function */
213 "R_M32C_RL_1ADDR", /* name */
214 FALSE
, /* partial_inplace */
217 FALSE
), /* pcrel_offset */
219 HOWTO (R_M32C_RL_2ADDR
, /* type */
221 0, /* size (0 = byte, 1 = short, 2 = long) */
223 FALSE
, /* pc_relative */
225 complain_overflow_signed
, /* complain_on_overflow */
226 bfd_elf_generic_reloc
, /* special_function */
227 "R_M32C_RL_2ADDR", /* name */
228 FALSE
, /* partial_inplace */
231 FALSE
), /* pcrel_offset */
235 /* Map BFD reloc types to M32C ELF reloc types. */
237 struct m32c_reloc_map
239 bfd_reloc_code_real_type bfd_reloc_val
;
240 unsigned int m32c_reloc_val
;
243 static const struct m32c_reloc_map m32c_reloc_map
[] =
245 { BFD_RELOC_NONE
, R_M32C_NONE
},
246 { BFD_RELOC_16
, R_M32C_16
},
247 { BFD_RELOC_24
, R_M32C_24
},
248 { BFD_RELOC_32
, R_M32C_32
},
249 { BFD_RELOC_8_PCREL
, R_M32C_8_PCREL
},
250 { BFD_RELOC_16_PCREL
, R_M32C_16_PCREL
},
251 { BFD_RELOC_8
, R_M32C_8
},
252 { BFD_RELOC_LO16
, R_M32C_LO16
},
253 { BFD_RELOC_HI16
, R_M32C_HI16
},
254 { BFD_RELOC_M32C_HI8
, R_M32C_HI8
},
255 { BFD_RELOC_M32C_RL_JUMP
, R_M32C_RL_JUMP
},
256 { BFD_RELOC_M32C_RL_1ADDR
, R_M32C_RL_1ADDR
},
257 { BFD_RELOC_M32C_RL_2ADDR
, R_M32C_RL_2ADDR
}
260 static reloc_howto_type
*
261 m32c_reloc_type_lookup
262 (bfd
* abfd ATTRIBUTE_UNUSED
,
263 bfd_reloc_code_real_type code
)
267 for (i
= ARRAY_SIZE (m32c_reloc_map
); --i
;)
268 if (m32c_reloc_map
[i
].bfd_reloc_val
== code
)
269 return & m32c_elf_howto_table
[m32c_reloc_map
[i
].m32c_reloc_val
];
274 static reloc_howto_type
*
275 m32c_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
, const char *r_name
)
280 i
< sizeof (m32c_elf_howto_table
) / sizeof (m32c_elf_howto_table
[0]);
282 if (m32c_elf_howto_table
[i
].name
!= NULL
283 && strcasecmp (m32c_elf_howto_table
[i
].name
, r_name
) == 0)
284 return &m32c_elf_howto_table
[i
];
289 /* Set the howto pointer for an M32C ELF reloc. */
292 m32c_info_to_howto_rela
293 (bfd
* abfd ATTRIBUTE_UNUSED
,
295 Elf_Internal_Rela
* dst
)
299 r_type
= ELF32_R_TYPE (dst
->r_info
);
300 BFD_ASSERT (r_type
< (unsigned int) R_M32C_max
);
301 cache_ptr
->howto
= & m32c_elf_howto_table
[r_type
];
306 /* Relocate an M32C ELF section.
307 There is some attempt to make this function usable for many architectures,
308 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
309 if only to serve as a learning tool.
311 The RELOCATE_SECTION function is called by the new ELF backend linker
312 to handle the relocations for a section.
314 The relocs are always passed as Rela structures; if the section
315 actually uses Rel structures, the r_addend field will always be
318 This function is responsible for adjusting the section contents as
319 necessary, and (if using Rela relocs and generating a relocatable
320 output file) adjusting the reloc addend as necessary.
322 This function does not have to worry about setting the reloc
323 address or the reloc symbol index.
325 LOCAL_SYMS is a pointer to the swapped in local symbols.
327 LOCAL_SECTIONS is an array giving the section in the input file
328 corresponding to the st_shndx field of each local symbol.
330 The global hash table entry for the global symbols can be found
331 via elf_sym_hashes (input_bfd).
333 When generating relocatable output, this function must handle
334 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
335 going to be the section symbol corresponding to the output
336 section, which means that the addend must be adjusted
340 m32c_elf_relocate_section
341 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
342 struct bfd_link_info
* info
,
344 asection
* input_section
,
346 Elf_Internal_Rela
* relocs
,
347 Elf_Internal_Sym
* local_syms
,
348 asection
** local_sections
)
350 Elf_Internal_Shdr
* symtab_hdr
;
351 struct elf_link_hash_entry
** sym_hashes
;
352 Elf_Internal_Rela
* rel
;
353 Elf_Internal_Rela
* relend
;
357 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
358 sym_hashes
= elf_sym_hashes (input_bfd
);
359 relend
= relocs
+ input_section
->reloc_count
;
361 dynobj
= elf_hash_table (info
)->dynobj
;
364 splt
= bfd_get_linker_section (dynobj
, ".plt");
366 for (rel
= relocs
; rel
< relend
; rel
++)
368 reloc_howto_type
* howto
;
369 unsigned long r_symndx
;
370 Elf_Internal_Sym
* sym
;
372 struct elf_link_hash_entry
* h
;
374 bfd_reloc_status_type r
;
375 const char * name
= NULL
;
378 r_type
= ELF32_R_TYPE (rel
->r_info
);
380 /* These are only used for relaxing; we don't actually relocate
381 anything with them, so skip them. */
382 if (r_type
== R_M32C_RL_JUMP
383 || r_type
== R_M32C_RL_1ADDR
384 || r_type
== R_M32C_RL_2ADDR
)
387 r_symndx
= ELF32_R_SYM (rel
->r_info
);
389 howto
= m32c_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
395 if (r_symndx
< symtab_hdr
->sh_info
)
397 sym
= local_syms
+ r_symndx
;
398 sec
= local_sections
[r_symndx
];
399 relocation
= (sec
->output_section
->vma
403 name
= bfd_elf_string_from_elf_section
404 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
405 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
409 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
411 if (info
->wrap_hash
!= NULL
412 && (input_section
->flags
& SEC_DEBUGGING
) != 0)
413 h
= ((struct elf_link_hash_entry
*)
414 unwrap_hash_lookup (info
, input_bfd
, &h
->root
));
416 while (h
->root
.type
== bfd_link_hash_indirect
417 || h
->root
.type
== bfd_link_hash_warning
)
418 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
420 name
= h
->root
.root
.string
;
422 if (h
->root
.type
== bfd_link_hash_defined
423 || h
->root
.type
== bfd_link_hash_defweak
)
425 sec
= h
->root
.u
.def
.section
;
426 relocation
= (h
->root
.u
.def
.value
427 + sec
->output_section
->vma
428 + sec
->output_offset
);
430 else if (h
->root
.type
== bfd_link_hash_undefweak
)
432 else if (!info
->relocatable
)
434 if (! ((*info
->callbacks
->undefined_symbol
)
435 (info
, h
->root
.root
.string
, input_bfd
,
436 input_section
, rel
->r_offset
, TRUE
)))
441 if (sec
!= NULL
&& discarded_section (sec
))
442 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
443 rel
, 1, relend
, howto
, 0, contents
);
445 if (info
->relocatable
)
447 /* This is a relocatable link. We don't have to change
448 anything, unless the reloc is against a section symbol,
449 in which case we have to adjust according to where the
450 section symbol winds up in the output section. */
451 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
452 rel
->r_addend
+= sec
->output_offset
;
456 switch (ELF32_R_TYPE (rel
->r_info
))
463 plt_offset
= &h
->plt
.offset
;
465 plt_offset
= elf_local_got_offsets (input_bfd
) + r_symndx
;
467 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
468 relocation, *plt_offset);*/
469 if (relocation
<= 0xffff)
471 /* If the symbol is in range for a 16-bit address, we should
472 have deallocated the plt entry in relax_section. */
473 BFD_ASSERT (*plt_offset
== (bfd_vma
) -1);
477 /* If the symbol is out of range for a 16-bit address,
478 we must have allocated a plt entry. */
479 BFD_ASSERT (*plt_offset
!= (bfd_vma
) -1);
481 /* If this is the first time we've processed this symbol,
482 fill in the plt entry with the correct symbol address. */
483 if ((*plt_offset
& 1) == 0)
487 x
= 0x000000fc; /* jmpf */
488 x
|= (relocation
<< 8) & 0xffffff00;
489 bfd_put_32 (input_bfd
, x
, splt
->contents
+ *plt_offset
);
493 relocation
= (splt
->output_section
->vma
494 + splt
->output_offset
495 + (*plt_offset
& -2));
498 char *newname
= bfd_malloc (strlen(name
)+5);
499 strcpy (newname
, name
);
500 strcat(newname
, ".plt");
501 _bfd_generic_link_add_one_symbol (info
,
504 BSF_FUNCTION
| BSF_WEAK
,
523 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
524 m32c_elf_howto_table
[ELF32_R_TYPE(rel
->r_info
)].name
,
525 rel
->r_offset
+ input_section
->output_section
->vma
+ input_section
->output_offset
,
526 relocation
, rel
->r_addend
);
530 printf (" %02x", contents
[rel
->r_offset
+i
]);
534 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
535 contents
, rel
->r_offset
, relocation
,
538 if (r
!= bfd_reloc_ok
)
540 const char * msg
= (const char *) NULL
;
544 case bfd_reloc_overflow
:
545 r
= info
->callbacks
->reloc_overflow
546 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
547 input_bfd
, input_section
, rel
->r_offset
);
550 case bfd_reloc_undefined
:
551 r
= info
->callbacks
->undefined_symbol
552 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
556 case bfd_reloc_outofrange
:
557 msg
= _("internal error: out of range error");
560 case bfd_reloc_notsupported
:
561 msg
= _("internal error: unsupported relocation error");
564 case bfd_reloc_dangerous
:
565 msg
= _("internal error: dangerous relocation");
569 msg
= _("internal error: unknown error");
574 r
= info
->callbacks
->warning
575 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
585 /* We support 16-bit pointers to code above 64k by generating a thunk
586 below 64k containing a JMP instruction to the final address. */
589 m32c_elf_check_relocs
591 struct bfd_link_info
* info
,
593 const Elf_Internal_Rela
* relocs
)
595 Elf_Internal_Shdr
* symtab_hdr
;
596 struct elf_link_hash_entry
** sym_hashes
;
597 const Elf_Internal_Rela
* rel
;
598 const Elf_Internal_Rela
* rel_end
;
599 bfd_vma
*local_plt_offsets
;
603 if (info
->relocatable
)
606 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
607 sym_hashes
= elf_sym_hashes (abfd
);
608 local_plt_offsets
= elf_local_got_offsets (abfd
);
610 dynobj
= elf_hash_table(info
)->dynobj
;
612 rel_end
= relocs
+ sec
->reloc_count
;
613 for (rel
= relocs
; rel
< rel_end
; rel
++)
615 struct elf_link_hash_entry
*h
;
616 unsigned long r_symndx
;
619 r_symndx
= ELF32_R_SYM (rel
->r_info
);
620 if (r_symndx
< symtab_hdr
->sh_info
)
624 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
625 while (h
->root
.type
== bfd_link_hash_indirect
626 || h
->root
.type
== bfd_link_hash_warning
)
627 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
629 /* PR15323, ref flags aren't set for references in the same
631 h
->root
.non_ir_ref
= 1;
634 switch (ELF32_R_TYPE (rel
->r_info
))
636 /* This relocation describes a 16-bit pointer to a function.
637 We may need to allocate a thunk in low memory; reserve memory
641 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
644 splt
= bfd_get_linker_section (dynobj
, ".plt");
647 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
648 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
649 | SEC_READONLY
| SEC_CODE
);
650 splt
= bfd_make_section_anyway_with_flags (dynobj
, ".plt",
653 || ! bfd_set_section_alignment (dynobj
, splt
, 1))
659 offset
= &h
->plt
.offset
;
662 if (local_plt_offsets
== NULL
)
667 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
668 local_plt_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
669 if (local_plt_offsets
== NULL
)
671 elf_local_got_offsets (abfd
) = local_plt_offsets
;
673 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
674 local_plt_offsets
[i
] = (bfd_vma
) -1;
676 offset
= &local_plt_offsets
[r_symndx
];
679 if (*offset
== (bfd_vma
) -1)
681 *offset
= splt
->size
;
691 /* This must exist if dynobj is ever set. */
694 m32c_elf_finish_dynamic_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
695 struct bfd_link_info
*info
)
700 /* As an extra sanity check, verify that all plt entries have
703 if ((dynobj
= elf_hash_table (info
)->dynobj
) != NULL
704 && (splt
= bfd_get_linker_section (dynobj
, ".plt")) != NULL
)
706 bfd_byte
*contents
= splt
->contents
;
707 unsigned int i
, size
= splt
->size
;
708 for (i
= 0; i
< size
; i
+= 4)
710 unsigned int x
= bfd_get_32 (dynobj
, contents
+ i
);
719 m32c_elf_always_size_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
720 struct bfd_link_info
*info
)
725 if (info
->relocatable
)
728 dynobj
= elf_hash_table (info
)->dynobj
;
732 splt
= bfd_get_linker_section (dynobj
, ".plt");
733 BFD_ASSERT (splt
!= NULL
);
735 splt
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, splt
->size
);
736 if (splt
->contents
== NULL
)
742 /* Function to set the ELF flag bits. */
745 m32c_elf_set_private_flags (bfd
*abfd
, flagword flags
)
747 elf_elfheader (abfd
)->e_flags
= flags
;
748 elf_flags_init (abfd
) = TRUE
;
752 /* Merge backend specific data from an object file to the output
753 object file when linking. */
756 m32c_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
758 flagword old_flags
, old_partial
;
759 flagword new_flags
, new_partial
;
760 bfd_boolean error
= FALSE
;
764 new_opt
[0] = old_opt
[0] = '\0';
765 new_flags
= elf_elfheader (ibfd
)->e_flags
;
766 old_flags
= elf_elfheader (obfd
)->e_flags
;
769 (*_bfd_error_handler
) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
770 old_flags
, new_flags
, elf_flags_init (obfd
) ? "yes" : "no",
771 bfd_get_filename (ibfd
));
774 if (!elf_flags_init (obfd
))
776 /* First call, no flags set. */
777 elf_flags_init (obfd
) = TRUE
;
778 elf_elfheader (obfd
)->e_flags
= new_flags
;
781 else if (new_flags
== old_flags
)
782 /* Compatible flags are ok. */
785 else /* Possibly incompatible flags. */
787 /* Warn if different cpu is used (allow a specific cpu to override
789 new_partial
= (new_flags
& EF_M32C_CPU_MASK
);
790 old_partial
= (old_flags
& EF_M32C_CPU_MASK
);
791 if (new_partial
== old_partial
)
798 default: strcat (new_opt
, " -m16c"); break;
799 case EF_M32C_CPU_M16C
: strcat (new_opt
, " -m16c"); break;
800 case EF_M32C_CPU_M32C
: strcat (new_opt
, " -m32c"); break;
805 default: strcat (old_opt
, " -m16c"); break;
806 case EF_M32C_CPU_M16C
: strcat (old_opt
, " -m16c"); break;
807 case EF_M32C_CPU_M32C
: strcat (old_opt
, " -m32c"); break;
811 /* Print out any mismatches from above. */
815 (*_bfd_error_handler
)
816 (_("%s: compiled with %s and linked with modules compiled with %s"),
817 bfd_get_filename (ibfd
), new_opt
, old_opt
);
820 new_flags
&= ~ EF_M32C_ALL_FLAGS
;
821 old_flags
&= ~ EF_M32C_ALL_FLAGS
;
823 /* Warn about any other mismatches. */
824 if (new_flags
!= old_flags
)
827 (*_bfd_error_handler
)
828 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
829 bfd_get_filename (ibfd
), (long)new_flags
, (long)old_flags
);
834 bfd_set_error (bfd_error_bad_value
);
841 m32c_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
843 FILE *file
= (FILE *) ptr
;
846 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
848 /* Print normal ELF private data. */
849 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
851 flags
= elf_elfheader (abfd
)->e_flags
;
852 fprintf (file
, _("private flags = 0x%lx:"), (unsigned long) flags
);
854 switch (flags
& EF_M32C_CPU_MASK
)
857 case EF_M32C_CPU_M16C
: fprintf (file
, " -m16c"); break;
858 case EF_M32C_CPU_M32C
: fprintf (file
, " -m32c"); break;
865 /* Return the MACH for an e_flags value. */
868 elf32_m32c_machine (bfd
*abfd
)
870 switch (elf_elfheader (abfd
)->e_flags
& EF_M32C_CPU_MASK
)
872 case EF_M32C_CPU_M16C
: return bfd_mach_m16c
;
873 case EF_M32C_CPU_M32C
: return bfd_mach_m32c
;
876 return bfd_mach_m16c
;
880 m32c_elf_object_p (bfd
*abfd
)
882 bfd_default_set_arch_mach (abfd
, bfd_arch_m32c
,
883 elf32_m32c_machine (abfd
));
890 dump_symtab (bfd
* abfd
, void *internal_syms
, void *external_syms
)
893 Elf_Internal_Sym
*isymbuf
;
894 Elf_Internal_Sym
*isymend
;
895 Elf_Internal_Sym
*isym
;
896 Elf_Internal_Shdr
*symtab_hdr
;
897 bfd_boolean free_internal
= 0, free_external
= 0;
899 char * st_info_stb_str
;
905 internal_syms
= bfd_malloc (1000);
910 external_syms
= bfd_malloc (1000);
914 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
915 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data(abfd
)->s
->sizeof_sym
;
917 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
918 symtab_hdr
->sh_info
, 0,
919 internal_syms
, external_syms
, NULL
);
921 isymbuf
= internal_syms
;
922 isymend
= isymbuf
+ locsymcount
;
924 for (isym
= isymbuf
; isym
< isymend
; isym
++)
926 switch (ELF_ST_TYPE (isym
->st_info
))
929 st_info_str
= "STT_FUNC";
933 st_info_str
= "STT_SECTION";
937 st_info_str
= "STT_FILE";
941 st_info_str
= "STT_OBJECT";
945 st_info_str
= "STT_TLS";
952 switch (ELF_ST_BIND (isym
->st_info
))
955 st_info_stb_str
= "STB_LOCAL";
959 st_info_stb_str
= "STB_GLOBAL";
963 st_info_stb_str
= "";
966 switch (ELF_ST_VISIBILITY (isym
->st_other
))
969 st_other_str
= "STV_DEFAULT";
973 st_other_str
= "STV_INTERNAL";
977 st_other_str
= "STV_PROTECTED";
984 switch (isym
->st_shndx
)
987 st_shndx_str
= "SHN_ABS";
991 st_shndx_str
= "SHN_COMMON";
995 st_shndx_str
= "SHN_UNDEF";
1002 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1003 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1005 (unsigned long) isym
->st_value
,
1006 (unsigned long) isym
->st_size
,
1008 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
1010 isym
->st_info
, st_info_str
, st_info_stb_str
,
1011 isym
->st_other
, st_other_str
,
1012 isym
->st_shndx
, st_shndx_str
);
1015 free (internal_syms
);
1017 free (external_syms
);
1021 m32c_get_reloc (long reloc
)
1023 if (0 <= reloc
&& reloc
< R_M32C_max
)
1024 return m32c_elf_howto_table
[reloc
].name
;
1030 /* Handle relaxing. */
1032 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1033 is within the low 64k, remove any entry for it in the plt. */
1035 struct relax_plt_data
1042 m32c_relax_plt_check (struct elf_link_hash_entry
*h
, void * xdata
)
1044 struct relax_plt_data
*data
= (struct relax_plt_data
*) xdata
;
1046 if (h
->plt
.offset
!= (bfd_vma
) -1)
1050 if (h
->root
.type
== bfd_link_hash_undefined
1051 || h
->root
.type
== bfd_link_hash_undefweak
)
1054 address
= (h
->root
.u
.def
.section
->output_section
->vma
1055 + h
->root
.u
.def
.section
->output_offset
1056 + h
->root
.u
.def
.value
);
1058 if (address
<= 0xffff)
1061 data
->splt
->size
-= 4;
1062 *data
->again
= TRUE
;
1069 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1070 previously had a plt entry, give it a new entry offset. */
1073 m32c_relax_plt_realloc (struct elf_link_hash_entry
*h
, void * xdata
)
1075 bfd_vma
*entry
= (bfd_vma
*) xdata
;
1077 if (h
->plt
.offset
!= (bfd_vma
) -1)
1079 h
->plt
.offset
= *entry
;
1087 m32c_elf_relax_plt_section (asection
*splt
,
1088 struct bfd_link_info
*info
,
1091 struct relax_plt_data relax_plt_data
;
1094 /* Assume nothing changes. */
1097 if (info
->relocatable
)
1100 /* Quick check for an empty plt. */
1101 if (splt
->size
== 0)
1104 /* Map across all global symbols; see which ones happen to
1105 fall in the low 64k. */
1106 relax_plt_data
.splt
= splt
;
1107 relax_plt_data
.again
= again
;
1108 elf_link_hash_traverse (elf_hash_table (info
), m32c_relax_plt_check
,
1111 /* Likewise for local symbols, though that's somewhat less convenient
1112 as we have to walk the list of input bfds and swap in symbol data. */
1113 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
1115 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1116 Elf_Internal_Shdr
*symtab_hdr
;
1117 Elf_Internal_Sym
*isymbuf
= NULL
;
1120 if (! local_plt_offsets
)
1123 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1124 if (symtab_hdr
->sh_info
!= 0)
1126 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1127 if (isymbuf
== NULL
)
1128 isymbuf
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
,
1129 symtab_hdr
->sh_info
, 0,
1131 if (isymbuf
== NULL
)
1135 for (idx
= 0; idx
< symtab_hdr
->sh_info
; ++idx
)
1137 Elf_Internal_Sym
*isym
;
1141 if (local_plt_offsets
[idx
] == (bfd_vma
) -1)
1144 isym
= &isymbuf
[idx
];
1145 if (isym
->st_shndx
== SHN_UNDEF
)
1147 else if (isym
->st_shndx
== SHN_ABS
)
1148 tsec
= bfd_abs_section_ptr
;
1149 else if (isym
->st_shndx
== SHN_COMMON
)
1150 tsec
= bfd_com_section_ptr
;
1152 tsec
= bfd_section_from_elf_index (ibfd
, isym
->st_shndx
);
1154 address
= (tsec
->output_section
->vma
1155 + tsec
->output_offset
1157 if (address
<= 0xffff)
1159 local_plt_offsets
[idx
] = -1;
1166 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1168 if (! info
->keep_memory
)
1172 /* Cache the symbols for elf_link_input_bfd. */
1173 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1178 /* If we changed anything, walk the symbols again to reallocate
1179 .plt entry addresses. */
1180 if (*again
&& splt
->size
> 0)
1184 elf_link_hash_traverse (elf_hash_table (info
),
1185 m32c_relax_plt_realloc
, &entry
);
1187 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link
.next
)
1189 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1190 unsigned int nlocals
= elf_tdata (ibfd
)->symtab_hdr
.sh_info
;
1193 if (! local_plt_offsets
)
1196 for (idx
= 0; idx
< nlocals
; ++idx
)
1197 if (local_plt_offsets
[idx
] != (bfd_vma
) -1)
1199 local_plt_offsets
[idx
] = entry
;
1209 compare_reloc (const void *e1
, const void *e2
)
1211 const Elf_Internal_Rela
*i1
= (const Elf_Internal_Rela
*) e1
;
1212 const Elf_Internal_Rela
*i2
= (const Elf_Internal_Rela
*) e2
;
1214 if (i1
->r_offset
== i2
->r_offset
)
1217 return i1
->r_offset
< i2
->r_offset
? -1 : 1;
1220 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1222 m32c_offset_for_reloc (bfd
*abfd
,
1223 Elf_Internal_Rela
*rel
,
1224 Elf_Internal_Shdr
*symtab_hdr
,
1225 Elf_External_Sym_Shndx
*shndx_buf ATTRIBUTE_UNUSED
,
1226 Elf_Internal_Sym
*intsyms
)
1230 /* Get the value of the symbol referred to by the reloc. */
1231 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1233 /* A local symbol. */
1234 Elf_Internal_Sym
*isym
;
1237 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1238 ssec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1239 symval
= isym
->st_value
;
1241 symval
+= ssec
->output_section
->vma
1242 + ssec
->output_offset
;
1247 struct elf_link_hash_entry
*h
;
1249 /* An external symbol. */
1250 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1251 h
= elf_sym_hashes (abfd
)[indx
];
1252 BFD_ASSERT (h
!= NULL
);
1254 if (h
->root
.type
!= bfd_link_hash_defined
1255 && h
->root
.type
!= bfd_link_hash_defweak
)
1256 /* This appears to be a reference to an undefined
1257 symbol. Just ignore it--it will be caught by the
1258 regular reloc processing. */
1261 symval
= (h
->root
.u
.def
.value
1262 + h
->root
.u
.def
.section
->output_section
->vma
1263 + h
->root
.u
.def
.section
->output_offset
);
1268 static int bytes_saved
= 0;
1270 static int bytes_to_reloc
[] = {
1278 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1280 /* Mask for the number of relocs associated with this insn. */
1281 #define RLA_RELOCS 0x0000000f
1282 /* Number of bytes gas emitted (before gas's relaxing) */
1283 #define RLA_NBYTES 0x00000ff0
1285 /* If the displacement is within the given range and the new encoding
1286 differs from the old encoding (the index), then the insn can be
1287 relaxed to the new encoding. */
1290 unsigned int max_disp
;
1291 unsigned char new_encoding
;
1294 static EncodingTable m16c_addr_encodings
[] = {
1295 { 0, 0, 0 }, /* R0 */
1296 { 0, 0, 1 }, /* R1 */
1297 { 0, 0, 2 }, /* R2 */
1298 { 0, 0, 3 }, /* R3 */
1299 { 0, 0, 4 }, /* A0 */
1300 { 0, 0, 5 }, /* A1 */
1301 { 0, 0, 6 }, /* [A0] */
1302 { 0, 0, 7 }, /* [A1] */
1303 { 1, 0, 6 }, /* udsp:8[A0] */
1304 { 1, 0, 7 }, /* udsp:8[A1] */
1305 { 1, 0, 10 }, /* udsp:8[SB] */
1306 { 1, 0, 11 }, /* sdsp:8[FB] */
1307 { 2, 255, 8 }, /* udsp:16[A0] */
1308 { 2, 255, 9 }, /* udsp:16[A1] */
1309 { 2, 255, 10 }, /* udsp:16[SB] */
1310 { 2, 0, 15 }, /* abs:16 */
1313 static EncodingTable m16c_jmpaddr_encodings
[] = {
1314 { 0, 0, 0 }, /* R0 */
1315 { 0, 0, 1 }, /* R1 */
1316 { 0, 0, 2 }, /* R2 */
1317 { 0, 0, 3 }, /* R3 */
1318 { 0, 0, 4 }, /* A0 */
1319 { 0, 0, 5 }, /* A1 */
1320 { 0, 0, 6 }, /* [A0] */
1321 { 0, 0, 7 }, /* [A1] */
1322 { 1, 0, 6 }, /* udsp:8[A0] */
1323 { 1, 0, 7 }, /* udsp:8[A1] */
1324 { 1, 0, 10 }, /* udsp:8[SB] */
1325 { 1, 0, 11 }, /* sdsp:8[FB] */
1326 { 3, 255, 8 }, /* udsp:20[A0] */
1327 { 3, 255, 9 }, /* udsp:20[A1] */
1328 { 2, 255, 10 }, /* udsp:16[SB] */
1329 { 2, 0, 15 }, /* abs:16 */
1332 static EncodingTable m32c_addr_encodings
[] = {
1333 { 0, 0, 0 }, /* [A0] */
1334 { 0, 0, 1 }, /* [A1] */
1335 { 0, 0, 2 }, /* A0 */
1336 { 0, 0, 3 }, /* A1 */
1337 { 1, 0, 0 }, /* udsp:8[A0] */
1338 { 1, 0, 1 }, /* udsp:8[A1] */
1339 { 1, 0, 6 }, /* udsp:8[SB] */
1340 { 1, 0, 7 }, /* sdsp:8[FB] */
1341 { 2, 255, 4 }, /* udsp:16[A0] */
1342 { 2, 255, 5 }, /* udsp:16[A1] */
1343 { 2, 255, 6 }, /* udsp:16[SB] */
1344 { 2, 127, 7 }, /* sdsp:16[FB] */
1345 { 3, 65535, 8 }, /* udsp:24[A0] */
1346 { 3, 65535, 9 }, /* udsp:24[A1] */
1347 { 3, 65535, 15 }, /* abs24 */
1348 { 2, 0, 15 }, /* abs16 */
1349 { 0, 0, 16 }, /* R2 */
1350 { 0, 0, 17 }, /* R3 */
1351 { 0, 0, 18 }, /* R0 */
1352 { 0, 0, 19 }, /* R1 */
1368 m32c_elf_relax_section
1371 struct bfd_link_info
* link_info
,
1372 bfd_boolean
* again
)
1374 Elf_Internal_Shdr
*symtab_hdr
;
1375 Elf_Internal_Shdr
*shndx_hdr
;
1376 Elf_Internal_Rela
*internal_relocs
;
1377 Elf_Internal_Rela
*free_relocs
= NULL
;
1378 Elf_Internal_Rela
*irel
, *irelend
, *srel
;
1379 bfd_byte
* contents
= NULL
;
1380 bfd_byte
* free_contents
= NULL
;
1381 Elf_Internal_Sym
*intsyms
= NULL
;
1382 Elf_Internal_Sym
*free_intsyms
= NULL
;
1383 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1386 if (abfd
== elf_hash_table (link_info
)->dynobj
1387 && (sec
->flags
& SEC_LINKER_CREATED
) != 0
1388 && strcmp (sec
->name
, ".plt") == 0)
1389 return m32c_elf_relax_plt_section (sec
, link_info
, again
);
1391 /* Assume nothing changes. */
1394 machine
= elf32_m32c_machine (abfd
);
1396 /* We don't have to do anything for a relocatable link, if
1397 this section does not have relocs, or if this is not a
1399 if (link_info
->relocatable
1400 || (sec
->flags
& SEC_RELOC
) == 0
1401 || sec
->reloc_count
== 0
1402 || (sec
->flags
& SEC_CODE
) == 0)
1405 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1406 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1408 /* Get the section contents. */
1409 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1410 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1411 /* Go get them off disk. */
1412 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1415 /* Read this BFD's symbols. */
1416 /* Get cached copy if it exists. */
1417 if (symtab_hdr
->contents
!= NULL
)
1419 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1423 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
1424 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1427 if (shndx_hdr
->sh_size
!= 0)
1431 amt
= symtab_hdr
->sh_info
;
1432 amt
*= sizeof (Elf_External_Sym_Shndx
);
1433 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1434 if (shndx_buf
== NULL
)
1436 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1437 || bfd_bread (shndx_buf
, amt
, abfd
) != amt
)
1439 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
1442 /* Get a copy of the native relocations. */
1443 internal_relocs
= (_bfd_elf_link_read_relocs
1444 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
1445 link_info
->keep_memory
));
1446 if (internal_relocs
== NULL
)
1448 if (! link_info
->keep_memory
)
1449 free_relocs
= internal_relocs
;
1451 /* The RL_ relocs must be just before the operand relocs they go
1452 with, so we must sort them to guarantee this. */
1453 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
1456 /* Walk through them looking for relaxing opportunities. */
1457 irelend
= internal_relocs
+ sec
->reloc_count
;
1459 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1462 unsigned char *insn
, *gap
, *einsn
;
1464 bfd_signed_vma pcrel
;
1470 EncodingTable
*enctbl
;
1473 if (ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_JUMP
1474 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_1ADDR
1475 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_2ADDR
)
1480 /* There will always be room for the relaxed insn, since it is smaller
1481 than the one it would replace. */
1482 BFD_ASSERT (irel
->r_offset
< sec
->size
);
1484 insn
= contents
+ irel
->r_offset
;
1485 relax_relocs
= irel
->r_addend
% 16;
1487 /* Ok, we only have three relocs we care about, and they're all
1488 fake. The lower four bits of the addend is always the number
1489 of following relocs (hence the qsort above) that are assigned
1490 to this opcode. The next 8 bits of the addend indicates the
1491 number of bytes in the insn. We use the rest of them
1492 ourselves as flags for the more expensive operations (defines
1493 above). The three relocs are:
1495 RL_JUMP: This marks all direct jump insns. We check the
1496 displacement and replace them with shorter jumps if
1497 they're in range. We also use this to find JMP.S
1498 insns and manually shorten them when we delete bytes.
1499 We have to decode these insns to figure out what to
1502 RL_1ADDR: This is a :G or :Q insn, which has a single
1503 "standard" operand. We have to extract the type
1504 field, see if it's a wide displacement, then figure
1505 out if we can replace it with a narrow displacement.
1506 We don't have to decode these insns.
1508 RL_2ADDR: Similarly, but two "standard" operands. Note that
1509 r_addend may still be 1, as standard operands don't
1510 always have displacements. Gas shouldn't give us one
1511 with zero operands, but since we don't know which one
1512 has the displacement, we check them both anyway.
1514 These all point to the beginning of the insn itself, not the
1517 Note that we only relax one step at a time, relying on the
1518 linker to call us repeatedly. Thus, there is no code for
1519 JMP.A->JMP.B although that will happen in two steps.
1520 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1523 /* Get the value of the symbol referred to by the reloc. Just
1524 in case this is the last reloc in the list, use the RL's
1525 addend to choose between this reloc (no addend) or the next
1526 (yes addend, which means at least one following reloc). */
1527 srel
= irel
+ (relax_relocs
? 1 : 0);
1528 symval
= OFFSET_FOR_RELOC (srel
);
1530 /* Setting gap_size nonzero is the flag which means "something
1534 new_type
= ELF32_R_TYPE(srel
->r_info
);
1536 pc
= sec
->output_section
->vma
+ sec
->output_offset
1538 pcrel
= symval
- pc
+ srel
->r_addend
;
1540 if (machine
== bfd_mach_m16c
)
1544 switch (ELF32_R_TYPE(irel
->r_info
))
1547 case R_M32C_RL_JUMP
:
1550 case 0xfe: /* jmp.b */
1551 if (pcrel
>= 2 && pcrel
<= 9)
1553 /* Relax JMP.B -> JMP.S. We need to get rid of
1554 the following reloc though. */
1555 insn
[0] = 0x60 | (pcrel
- 2);
1556 new_type
= R_M32C_NONE
;
1557 irel
->r_addend
= 0x10;
1563 case 0xf4: /* jmp.w */
1564 /* 128 is allowed because it will be one byte closer
1565 after relaxing. Likewise for all other pc-rel
1567 if (pcrel
<= 128 && pcrel
>= -128)
1569 /* Relax JMP.W -> JMP.B */
1572 new_type
= R_M32C_8_PCREL
;
1578 case 0xfc: /* jmp.a */
1579 if (pcrel
<= 32768 && pcrel
>= -32768)
1581 /* Relax JMP.A -> JMP.W */
1585 new_type
= R_M32C_16_PCREL
;
1591 case 0xfd: /* jsr.a */
1592 if (pcrel
<= 32768 && pcrel
>= -32768)
1594 /* Relax JSR.A -> JSR.W */
1598 new_type
= R_M32C_16_PCREL
;
1606 case R_M32C_RL_2ADDR
:
1607 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1609 enctbl
= m16c_addr_encodings
;
1611 enc
= (insn
[1] >> 4) & 0x0f;
1614 if (srel
->r_offset
== irel
->r_offset
+ posn
1615 && e
->new_encoding
!= enc
1616 && symval
<= e
->max_disp
)
1619 insn
[1] |= e
->new_encoding
<< 4;
1620 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1621 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1622 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1625 if (relax_relocs
== 2)
1631 case R_M32C_RL_1ADDR
:
1632 /* xxxx xxxx xxxx dest [disp] */
1634 enctbl
= m16c_addr_encodings
;
1637 /* Check the opcode for jumps. We know it's safe to
1638 do this because all 2ADDR insns are at least two
1640 enc
= insn
[0] * 256 + insn
[1];
1647 enctbl
= m16c_jmpaddr_encodings
;
1651 /* srel, posn, and enc must be set here. */
1653 symval
= OFFSET_FOR_RELOC (srel
);
1654 enc
= insn
[1] & 0x0f;
1657 if (srel
->r_offset
== irel
->r_offset
+ posn
1658 && e
->new_encoding
!= enc
1659 && symval
<= e
->max_disp
)
1662 insn
[1] |= e
->new_encoding
;
1663 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1664 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1665 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1671 } /* Ends switch (reloc type) for m16c. */
1673 else /* machine == bfd_mach_m32c */
1677 switch (ELF32_R_TYPE(irel
->r_info
))
1680 case R_M32C_RL_JUMP
:
1683 case 0xbb: /* jmp.b */
1684 if (pcrel
>= 2 && pcrel
<= 9)
1687 /* Relax JMP.B -> JMP.S. We need to get rid of
1688 the following reloc though. */
1689 insn
[0] = 0x4a | ((p
<< 3) & 0x30) | (p
& 1);
1690 new_type
= R_M32C_NONE
;
1691 irel
->r_addend
= 0x10;
1697 case 0xce: /* jmp.w */
1698 if (pcrel
<= 128 && pcrel
>= -128)
1700 /* Relax JMP.W -> JMP.B */
1703 new_type
= R_M32C_8_PCREL
;
1709 case 0xcc: /* jmp.a */
1710 if (pcrel
<= 32768 && pcrel
>= -32768)
1712 /* Relax JMP.A -> JMP.W */
1716 new_type
= R_M32C_16_PCREL
;
1722 case 0xcd: /* jsr.a */
1723 if (pcrel
<= 32768 && pcrel
>= -32768)
1725 /* Relax JSR.A -> JSR.W */
1729 new_type
= R_M32C_16_PCREL
;
1737 case R_M32C_RL_2ADDR
:
1738 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1744 /* prefix; remove it as far as the RL reloc is concerned. */
1749 enctbl
= m32c_addr_encodings
;
1750 enc
= ((einsn
[0] & 0x70) >> 2) | ((einsn
[1] & 0x30) >> 4);
1753 if (srel
->r_offset
== irel
->r_offset
+ posn
1754 && e
->new_encoding
!= enc
1755 && symval
<= e
->max_disp
)
1758 einsn
[0] |= (e
->new_encoding
& 0x1c) << 2;
1760 einsn
[1] |= (e
->new_encoding
& 0x03) << 4;
1761 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1762 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1763 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1766 if (relax_relocs
== 2)
1772 case R_M32C_RL_1ADDR
:
1773 /* xxxx DDDx DDxx xxxx [disp] */
1779 /* prefix; remove it as far as the RL reloc is concerned. */
1784 enctbl
= m32c_addr_encodings
;
1787 /* srel, posn, and enc must be set here. */
1789 symval
= OFFSET_FOR_RELOC (srel
);
1790 enc
= ((einsn
[0] & 0x0e) << 1) | ((einsn
[1] & 0xc0) >> 6);
1793 if (srel
->r_offset
== irel
->r_offset
+ posn
1794 && e
->new_encoding
!= enc
1795 && symval
<= e
->max_disp
)
1798 einsn
[0] |= (e
->new_encoding
& 0x1c) >> 1;
1800 einsn
[1] |= (e
->new_encoding
& 0x03) << 6;
1801 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1802 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1803 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1809 } /* Ends switch (reloc type) for m32c. */
1817 srel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (srel
->r_info
), new_type
);
1819 /* Note that we've changed the relocs, section contents, etc. */
1820 elf_section_data (sec
)->relocs
= internal_relocs
;
1823 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1824 free_contents
= NULL
;
1826 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1827 free_intsyms
= NULL
;
1829 bytes_saved
+= gap_size
;
1831 if (! m32c_elf_relax_delete_bytes(abfd
, sec
, gap
- contents
, gap_size
))
1834 } /* next relocation */
1836 if (free_relocs
!= NULL
)
1842 if (free_contents
!= NULL
)
1844 if (! link_info
->keep_memory
)
1845 free (free_contents
);
1846 /* Cache the section contents for elf_link_input_bfd. */
1848 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1850 free_contents
= NULL
;
1853 if (shndx_buf
!= NULL
)
1855 shndx_hdr
->contents
= NULL
;
1859 if (free_intsyms
!= NULL
)
1861 if (! link_info
->keep_memory
)
1862 free (free_intsyms
);
1863 /* Cache the symbols for elf_link_input_bfd. */
1866 symtab_hdr
->contents
= NULL
/* (unsigned char *) intsyms*/;
1869 free_intsyms
= NULL
;
1875 if (free_relocs
!= NULL
)
1877 if (free_contents
!= NULL
)
1878 free (free_contents
);
1879 if (shndx_buf
!= NULL
)
1881 shndx_hdr
->contents
= NULL
;
1884 if (free_intsyms
!= NULL
)
1885 free (free_intsyms
);
1889 /* Delete some bytes from a section while relaxing. */
1892 m32c_elf_relax_delete_bytes
1898 Elf_Internal_Shdr
*symtab_hdr
;
1899 Elf_Internal_Shdr
*shndx_hdr
;
1902 Elf_Internal_Rela
*irel
;
1903 Elf_Internal_Rela
*irelend
;
1905 Elf_Internal_Sym
*isym
;
1906 Elf_Internal_Sym
*isymend
;
1907 Elf_Internal_Sym
*intsyms
;
1908 Elf_External_Sym_Shndx
*shndx_buf
;
1909 Elf_External_Sym_Shndx
*shndx
;
1910 struct elf_link_hash_entry
** sym_hashes
;
1911 struct elf_link_hash_entry
** end_hashes
;
1912 unsigned int symcount
;
1914 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1918 irel
= elf_section_data (sec
)->relocs
;
1919 irelend
= irel
+ sec
->reloc_count
;
1921 /* Actually delete the bytes. */
1922 memmove (contents
+ addr
, contents
+ addr
+ count
, (size_t) (toaddr
- addr
- count
));
1925 /* Adjust all the relocs. */
1926 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1928 /* Get the new reloc address. */
1929 if (irel
->r_offset
> addr
&& irel
->r_offset
< toaddr
)
1930 irel
->r_offset
-= count
;
1932 if (ELF32_R_TYPE(irel
->r_info
) == R_M32C_RL_JUMP
1933 && irel
->r_addend
== 0x10 /* one byte insn, no relocs */
1934 && irel
->r_offset
+ 1 < addr
1935 && irel
->r_offset
+ 7 > addr
)
1938 unsigned char *insn
= &contents
[irel
->r_offset
];
1940 /* This is a JMP.S, which we have to manually update. */
1941 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1943 if ((*insn
& 0xf8) != 0x60)
1949 if ((*insn
& 0xce) != 0x4a)
1951 disp
= ((disp
& 0x30) >> 3) | (disp
& 1);
1953 if (irel
->r_offset
+ disp
+ 2 >= addr
+count
)
1956 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1958 *insn
= (*insn
& 0xf8) | disp
;
1962 *insn
= (*insn
& 0xce) | ((disp
& 6) << 3) | (disp
& 1);
1968 /* Adjust the local symbols defined in this section. */
1969 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
1970 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1972 isymend
= isym
+ symtab_hdr
->sh_info
;
1974 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1975 shndx_hdr
= & elf_tdata (abfd
)->symtab_shndx_hdr
;
1976 shndx_buf
= (Elf_External_Sym_Shndx
*) shndx_hdr
->contents
;
1979 for (; isym
< isymend
; isym
++, shndx
= (shndx
? shndx
+ 1 : NULL
))
1981 /* If the symbol is in the range of memory we just moved, we
1982 have to adjust its value. */
1983 if ((int) isym
->st_shndx
== sec_shndx
1984 && isym
->st_value
> addr
1985 && isym
->st_value
< toaddr
)
1987 isym
->st_value
-= count
;
1989 /* If the symbol *spans* the bytes we just deleted (i.e. it's
1990 *end* is in the moved bytes but it's *start* isn't), then we
1991 must adjust its size. */
1992 if ((int) isym
->st_shndx
== sec_shndx
1993 && isym
->st_value
< addr
1994 && isym
->st_value
+ isym
->st_size
> addr
1995 && isym
->st_value
+ isym
->st_size
< toaddr
)
1997 isym
->st_size
-= count
;
2001 /* Now adjust the global symbols defined in this section. */
2002 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2003 - symtab_hdr
->sh_info
);
2004 sym_hashes
= elf_sym_hashes (abfd
);
2005 // sym_hashes += symtab_hdr->sh_info;
2006 end_hashes
= sym_hashes
+ symcount
;
2008 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2010 struct elf_link_hash_entry
* sym_hash
= * sym_hashes
;
2013 (sym_hash
->root
.type
== bfd_link_hash_defined
2014 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2015 && sym_hash
->root
.u
.def
.section
== sec
)
2017 if (sym_hash
->root
.u
.def
.value
> addr
2018 && sym_hash
->root
.u
.def
.value
< toaddr
)
2020 sym_hash
->root
.u
.def
.value
-= count
;
2022 if (sym_hash
->root
.u
.def
.value
< addr
2023 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
2024 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
< toaddr
)
2026 sym_hash
->size
-= count
;
2034 /* This is for versions of gcc prior to 4.3. */
2036 _bfd_m32c_elf_eh_frame_address_size (bfd
*abfd
, asection
*sec ATTRIBUTE_UNUSED
)
2038 if ((elf_elfheader (abfd
)->e_flags
& EF_M32C_CPU_MASK
) == EF_M32C_CPU_M16C
)
2045 #define ELF_ARCH bfd_arch_m32c
2046 #define ELF_MACHINE_CODE EM_M32C
2047 #define ELF_MACHINE_ALT1 EM_M32C_OLD
2048 #define ELF_MAXPAGESIZE 0x100
2051 #define TARGET_BIG_SYM m32c_elf32_vec
2052 #define TARGET_BIG_NAME "elf32-m32c"
2054 #define TARGET_LITTLE_SYM m32c_elf32_vec
2055 #define TARGET_LITTLE_NAME "elf32-m32c"
2058 #define elf_info_to_howto_rel NULL
2059 #define elf_info_to_howto m32c_info_to_howto_rela
2060 #define elf_backend_object_p m32c_elf_object_p
2061 #define elf_backend_relocate_section m32c_elf_relocate_section
2062 #define elf_backend_check_relocs m32c_elf_check_relocs
2063 #define elf_backend_object_p m32c_elf_object_p
2064 #define elf_symbol_leading_char ('_')
2065 #define elf_backend_always_size_sections \
2066 m32c_elf_always_size_sections
2067 #define elf_backend_finish_dynamic_sections \
2068 m32c_elf_finish_dynamic_sections
2070 #define elf_backend_can_gc_sections 1
2071 #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2073 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2074 #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2075 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2076 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2077 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2078 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2080 #include "elf32-target.h"