1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "libiberty.h"
28 /* Forward declarations. */
29 static reloc_howto_type
* m32c_reloc_type_lookup
30 (bfd
*, bfd_reloc_code_real_type
);
31 static void m32c_info_to_howto_rela
32 (bfd
*, arelent
*, Elf_Internal_Rela
*);
33 static bfd_boolean m32c_elf_relocate_section
34 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
35 static bfd_boolean m32c_elf_check_relocs
36 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
37 static bfd_boolean
m32c_elf_relax_delete_bytes (bfd
*, asection
*, bfd_vma
, int);
39 char * m32c_get_reloc (long reloc
);
40 void dump_symtab (bfd
*, void *, void *);
42 static bfd_boolean m32c_elf_relax_section
43 (bfd
*abfd
, asection
*sec
, struct bfd_link_info
*link_info
, bfd_boolean
*again
);
46 static reloc_howto_type m32c_elf_howto_table
[] =
48 /* This reloc does nothing. */
49 HOWTO (R_M32C_NONE
, /* type */
51 0, /* size (0 = byte, 1 = short, 2 = long) */
53 FALSE
, /* pc_relative */
55 complain_overflow_bitfield
, /* complain_on_overflow */
56 bfd_elf_generic_reloc
, /* special_function */
57 "R_M32C_NONE", /* name */
58 FALSE
, /* partial_inplace */
61 FALSE
), /* pcrel_offset */
63 /* GCC intentionally overflows these next two in order to work
64 around limitations in the addressing modes, so don't complain
66 HOWTO (R_M32C_16
, /* type */
68 1, /* size (0 = byte, 1 = short, 2 = long) */
70 FALSE
, /* pc_relative */
72 complain_overflow_dont
, /* complain_on_overflow */
73 bfd_elf_generic_reloc
, /* special_function */
74 "R_M32C_16", /* name */
75 FALSE
, /* partial_inplace */
77 0xffff, /* dst_mask */
78 FALSE
), /* pcrel_offset */
80 HOWTO (R_M32C_24
, /* type */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
84 FALSE
, /* pc_relative */
86 complain_overflow_dont
, /* complain_on_overflow */
87 bfd_elf_generic_reloc
, /* special_function */
88 "R_M32C_24", /* name */
89 FALSE
, /* partial_inplace */
91 0xffffff, /* dst_mask */
92 FALSE
), /* pcrel_offset */
94 HOWTO (R_M32C_32
, /* type */
96 2, /* size (0 = byte, 1 = short, 2 = long) */
98 FALSE
, /* pc_relative */
100 complain_overflow_bitfield
, /* complain_on_overflow */
101 bfd_elf_generic_reloc
, /* special_function */
102 "R_M32C_32", /* name */
103 FALSE
, /* partial_inplace */
105 0xffffffff, /* dst_mask */
106 FALSE
), /* pcrel_offset */
108 HOWTO (R_M32C_8_PCREL
, /* type */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
112 TRUE
, /* pc_relative */
114 complain_overflow_signed
, /* complain_on_overflow */
115 bfd_elf_generic_reloc
, /* special_function */
116 "R_M32C_8_PCREL", /* name */
117 FALSE
, /* partial_inplace */
120 TRUE
), /* pcrel_offset */
122 HOWTO (R_M32C_16_PCREL
, /* type */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
126 TRUE
, /* pc_relative */
128 complain_overflow_signed
, /* complain_on_overflow */
129 bfd_elf_generic_reloc
, /* special_function */
130 "R_M32C_16_PCREL", /* name */
131 FALSE
, /* partial_inplace */
133 0xffff, /* dst_mask */
134 TRUE
), /* pcrel_offset */
136 HOWTO (R_M32C_8
, /* type */
138 0, /* size (0 = byte, 1 = short, 2 = long) */
140 FALSE
, /* pc_relative */
142 complain_overflow_unsigned
, /* complain_on_overflow */
143 bfd_elf_generic_reloc
, /* special_function */
144 "R_M32C_8", /* name */
145 FALSE
, /* partial_inplace */
148 FALSE
), /* pcrel_offset */
150 HOWTO (R_M32C_LO16
, /* type */
152 1, /* size (0 = byte, 1 = short, 2 = long) */
154 FALSE
, /* pc_relative */
156 complain_overflow_dont
, /* complain_on_overflow */
157 bfd_elf_generic_reloc
, /* special_function */
158 "R_M32C_LO16", /* name */
159 FALSE
, /* partial_inplace */
161 0xffff, /* dst_mask */
162 FALSE
), /* pcrel_offset */
164 HOWTO (R_M32C_HI8
, /* type */
166 0, /* size (0 = byte, 1 = short, 2 = long) */
168 FALSE
, /* pc_relative */
170 complain_overflow_dont
, /* complain_on_overflow */
171 bfd_elf_generic_reloc
, /* special_function */
172 "R_M32C_HI8", /* name */
173 FALSE
, /* partial_inplace */
176 FALSE
), /* pcrel_offset */
178 HOWTO (R_M32C_HI16
, /* type */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
182 FALSE
, /* pc_relative */
184 complain_overflow_dont
, /* complain_on_overflow */
185 bfd_elf_generic_reloc
, /* special_function */
186 "R_M32C_HI16", /* name */
187 FALSE
, /* partial_inplace */
189 0xffff, /* dst_mask */
190 FALSE
), /* pcrel_offset */
192 HOWTO (R_M32C_RL_JUMP
, /* type */
194 0, /* size (0 = byte, 1 = short, 2 = long) */
196 FALSE
, /* pc_relative */
198 complain_overflow_signed
, /* complain_on_overflow */
199 bfd_elf_generic_reloc
, /* special_function */
200 "R_M32C_RL_JUMP", /* name */
201 FALSE
, /* partial_inplace */
204 FALSE
), /* pcrel_offset */
206 HOWTO (R_M32C_RL_1ADDR
, /* type */
208 0, /* size (0 = byte, 1 = short, 2 = long) */
210 FALSE
, /* pc_relative */
212 complain_overflow_signed
, /* complain_on_overflow */
213 bfd_elf_generic_reloc
, /* special_function */
214 "R_M32C_RL_1ADDR", /* name */
215 FALSE
, /* partial_inplace */
218 FALSE
), /* pcrel_offset */
220 HOWTO (R_M32C_RL_2ADDR
, /* type */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
224 FALSE
, /* pc_relative */
226 complain_overflow_signed
, /* complain_on_overflow */
227 bfd_elf_generic_reloc
, /* special_function */
228 "R_M32C_RL_2ADDR", /* name */
229 FALSE
, /* partial_inplace */
232 FALSE
), /* pcrel_offset */
236 /* Map BFD reloc types to M32C ELF reloc types. */
238 struct m32c_reloc_map
240 bfd_reloc_code_real_type bfd_reloc_val
;
241 unsigned int m32c_reloc_val
;
244 static const struct m32c_reloc_map m32c_reloc_map
[] =
246 { BFD_RELOC_NONE
, R_M32C_NONE
},
247 { BFD_RELOC_16
, R_M32C_16
},
248 { BFD_RELOC_24
, R_M32C_24
},
249 { BFD_RELOC_32
, R_M32C_32
},
250 { BFD_RELOC_8_PCREL
, R_M32C_8_PCREL
},
251 { BFD_RELOC_16_PCREL
, R_M32C_16_PCREL
},
252 { BFD_RELOC_8
, R_M32C_8
},
253 { BFD_RELOC_LO16
, R_M32C_LO16
},
254 { BFD_RELOC_HI16
, R_M32C_HI16
},
255 { BFD_RELOC_M32C_HI8
, R_M32C_HI8
},
256 { BFD_RELOC_M32C_RL_JUMP
, R_M32C_RL_JUMP
},
257 { BFD_RELOC_M32C_RL_1ADDR
, R_M32C_RL_1ADDR
},
258 { BFD_RELOC_M32C_RL_2ADDR
, R_M32C_RL_2ADDR
}
261 static reloc_howto_type
*
262 m32c_reloc_type_lookup
263 (bfd
* abfd ATTRIBUTE_UNUSED
,
264 bfd_reloc_code_real_type code
)
268 for (i
= ARRAY_SIZE (m32c_reloc_map
); --i
;)
269 if (m32c_reloc_map
[i
].bfd_reloc_val
== code
)
270 return & m32c_elf_howto_table
[m32c_reloc_map
[i
].m32c_reloc_val
];
275 /* Set the howto pointer for an M32C ELF reloc. */
278 m32c_info_to_howto_rela
279 (bfd
* abfd ATTRIBUTE_UNUSED
,
281 Elf_Internal_Rela
* dst
)
285 r_type
= ELF32_R_TYPE (dst
->r_info
);
286 BFD_ASSERT (r_type
< (unsigned int) R_M32C_max
);
287 cache_ptr
->howto
= & m32c_elf_howto_table
[r_type
];
292 /* Relocate an M32C ELF section.
293 There is some attempt to make this function usable for many architectures,
294 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
295 if only to serve as a learning tool.
297 The RELOCATE_SECTION function is called by the new ELF backend linker
298 to handle the relocations for a section.
300 The relocs are always passed as Rela structures; if the section
301 actually uses Rel structures, the r_addend field will always be
304 This function is responsible for adjusting the section contents as
305 necessary, and (if using Rela relocs and generating a relocatable
306 output file) adjusting the reloc addend as necessary.
308 This function does not have to worry about setting the reloc
309 address or the reloc symbol index.
311 LOCAL_SYMS is a pointer to the swapped in local symbols.
313 LOCAL_SECTIONS is an array giving the section in the input file
314 corresponding to the st_shndx field of each local symbol.
316 The global hash table entry for the global symbols can be found
317 via elf_sym_hashes (input_bfd).
319 When generating relocatable output, this function must handle
320 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
321 going to be the section symbol corresponding to the output
322 section, which means that the addend must be adjusted
326 m32c_elf_relocate_section
327 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
328 struct bfd_link_info
* info
,
330 asection
* input_section
,
332 Elf_Internal_Rela
* relocs
,
333 Elf_Internal_Sym
* local_syms
,
334 asection
** local_sections
)
336 Elf_Internal_Shdr
* symtab_hdr
;
337 struct elf_link_hash_entry
** sym_hashes
;
338 Elf_Internal_Rela
* rel
;
339 Elf_Internal_Rela
* relend
;
343 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
344 sym_hashes
= elf_sym_hashes (input_bfd
);
345 relend
= relocs
+ input_section
->reloc_count
;
347 dynobj
= elf_hash_table (info
)->dynobj
;
350 splt
= bfd_get_section_by_name (dynobj
, ".plt");
352 for (rel
= relocs
; rel
< relend
; rel
++)
354 reloc_howto_type
* howto
;
355 unsigned long r_symndx
;
356 Elf_Internal_Sym
* sym
;
358 struct elf_link_hash_entry
* h
;
360 bfd_reloc_status_type r
;
361 const char * name
= NULL
;
364 r_type
= ELF32_R_TYPE (rel
->r_info
);
366 /* These are only used for relaxing; we don't actually relocate
367 anything with them, so skip them. */
368 if (r_type
== R_M32C_RL_JUMP
369 || r_type
== R_M32C_RL_1ADDR
370 || r_type
== R_M32C_RL_2ADDR
)
373 r_symndx
= ELF32_R_SYM (rel
->r_info
);
375 howto
= m32c_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
381 if (r_symndx
< symtab_hdr
->sh_info
)
383 sym
= local_syms
+ r_symndx
;
384 sec
= local_sections
[r_symndx
];
385 relocation
= (sec
->output_section
->vma
389 name
= bfd_elf_string_from_elf_section
390 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
391 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
395 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
397 while (h
->root
.type
== bfd_link_hash_indirect
398 || h
->root
.type
== bfd_link_hash_warning
)
399 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
401 name
= h
->root
.root
.string
;
403 if (h
->root
.type
== bfd_link_hash_defined
404 || h
->root
.type
== bfd_link_hash_defweak
)
406 sec
= h
->root
.u
.def
.section
;
407 relocation
= (h
->root
.u
.def
.value
408 + sec
->output_section
->vma
409 + sec
->output_offset
);
411 else if (h
->root
.type
== bfd_link_hash_undefweak
)
413 else if (!info
->relocatable
)
415 if (! ((*info
->callbacks
->undefined_symbol
)
416 (info
, h
->root
.root
.string
, input_bfd
,
417 input_section
, rel
->r_offset
, TRUE
)))
422 if (sec
!= NULL
&& elf_discarded_section (sec
))
424 /* For relocs against symbols from removed linkonce sections,
425 or sections discarded by a linker script, we just want the
426 section contents zeroed. Avoid any special processing. */
427 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
433 if (info
->relocatable
)
435 /* This is a relocatable link. We don't have to change
436 anything, unless the reloc is against a section symbol,
437 in which case we have to adjust according to where the
438 section symbol winds up in the output section. */
439 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
440 rel
->r_addend
+= sec
->output_offset
;
444 switch (ELF32_R_TYPE (rel
->r_info
))
451 plt_offset
= &h
->plt
.offset
;
453 plt_offset
= elf_local_got_offsets (input_bfd
) + r_symndx
;
455 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
456 relocation, *plt_offset);*/
457 if (relocation
<= 0xffff)
459 /* If the symbol is in range for a 16-bit address, we should
460 have deallocated the plt entry in relax_section. */
461 BFD_ASSERT (*plt_offset
== (bfd_vma
) -1);
465 /* If the symbol is out of range for a 16-bit address,
466 we must have allocated a plt entry. */
467 BFD_ASSERT (*plt_offset
!= (bfd_vma
) -1);
469 /* If this is the first time we've processed this symbol,
470 fill in the plt entry with the correct symbol address. */
471 if ((*plt_offset
& 1) == 0)
475 x
= 0x000000fc; /* jmpf */
476 x
|= (relocation
<< 8) & 0xffffff00;
477 bfd_put_32 (input_bfd
, x
, splt
->contents
+ *plt_offset
);
481 relocation
= (splt
->output_section
->vma
482 + splt
->output_offset
483 + (*plt_offset
& -2));
486 char *newname
= bfd_malloc (strlen(name
)+5);
487 strcpy (newname
, name
);
488 strcat(newname
, ".plt");
489 _bfd_generic_link_add_one_symbol (info
,
492 BSF_FUNCTION
| BSF_WEAK
,
511 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
512 m32c_elf_howto_table
[ELF32_R_TYPE(rel
->r_info
)].name
,
513 rel
->r_offset
+ input_section
->output_section
->vma
+ input_section
->output_offset
,
514 relocation
, rel
->r_addend
);
518 printf (" %02x", contents
[rel
->r_offset
+i
]);
522 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
523 contents
, rel
->r_offset
, relocation
,
526 if (r
!= bfd_reloc_ok
)
528 const char * msg
= (const char *) NULL
;
532 case bfd_reloc_overflow
:
533 r
= info
->callbacks
->reloc_overflow
534 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
535 input_bfd
, input_section
, rel
->r_offset
);
538 case bfd_reloc_undefined
:
539 r
= info
->callbacks
->undefined_symbol
540 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
544 case bfd_reloc_outofrange
:
545 msg
= _("internal error: out of range error");
548 case bfd_reloc_notsupported
:
549 msg
= _("internal error: unsupported relocation error");
552 case bfd_reloc_dangerous
:
553 msg
= _("internal error: dangerous relocation");
557 msg
= _("internal error: unknown error");
562 r
= info
->callbacks
->warning
563 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
573 /* We support 16-bit pointers to code above 64k by generating a thunk
574 below 64k containing a JMP instruction to the final address. */
577 m32c_elf_check_relocs
579 struct bfd_link_info
* info
,
581 const Elf_Internal_Rela
* relocs
)
583 Elf_Internal_Shdr
* symtab_hdr
;
584 struct elf_link_hash_entry
** sym_hashes
;
585 struct elf_link_hash_entry
** sym_hashes_end
;
586 const Elf_Internal_Rela
* rel
;
587 const Elf_Internal_Rela
* rel_end
;
588 bfd_vma
*local_plt_offsets
;
592 if (info
->relocatable
)
595 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
596 sym_hashes
= elf_sym_hashes (abfd
);
597 local_plt_offsets
= elf_local_got_offsets (abfd
);
599 dynobj
= elf_hash_table(info
)->dynobj
;
601 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
602 if (!elf_bad_symtab (abfd
))
603 sym_hashes_end
-= symtab_hdr
->sh_info
;
605 rel_end
= relocs
+ sec
->reloc_count
;
606 for (rel
= relocs
; rel
< rel_end
; rel
++)
608 struct elf_link_hash_entry
*h
;
609 unsigned long r_symndx
;
612 r_symndx
= ELF32_R_SYM (rel
->r_info
);
613 if (r_symndx
< symtab_hdr
->sh_info
)
617 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
618 while (h
->root
.type
== bfd_link_hash_indirect
619 || h
->root
.type
== bfd_link_hash_warning
)
620 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
623 switch (ELF32_R_TYPE (rel
->r_info
))
625 /* This relocation describes a 16-bit pointer to a function.
626 We may need to allocate a thunk in low memory; reserve memory
630 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
633 splt
= bfd_get_section_by_name (dynobj
, ".plt");
636 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
637 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
638 | SEC_READONLY
| SEC_CODE
);
639 splt
= bfd_make_section_with_flags (dynobj
, ".plt", flags
);
641 || ! bfd_set_section_alignment (dynobj
, splt
, 1))
647 offset
= &h
->plt
.offset
;
650 if (local_plt_offsets
== NULL
)
655 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
656 local_plt_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
657 if (local_plt_offsets
== NULL
)
659 elf_local_got_offsets (abfd
) = local_plt_offsets
;
661 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
662 local_plt_offsets
[i
] = (bfd_vma
) -1;
664 offset
= &local_plt_offsets
[r_symndx
];
667 if (*offset
== (bfd_vma
) -1)
669 *offset
= splt
->size
;
679 /* This must exist if dynobj is ever set. */
682 m32c_elf_finish_dynamic_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
683 struct bfd_link_info
*info
)
688 /* As an extra sanity check, verify that all plt entries have
691 if ((dynobj
= elf_hash_table (info
)->dynobj
) != NULL
692 && (splt
= bfd_get_section_by_name (dynobj
, ".plt")) != NULL
)
694 bfd_byte
*contents
= splt
->contents
;
695 unsigned int i
, size
= splt
->size
;
696 for (i
= 0; i
< size
; i
+= 4)
698 unsigned int x
= bfd_get_32 (dynobj
, contents
+ i
);
707 m32c_elf_always_size_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
708 struct bfd_link_info
*info
)
713 if (info
->relocatable
)
716 dynobj
= elf_hash_table (info
)->dynobj
;
720 splt
= bfd_get_section_by_name (dynobj
, ".plt");
721 BFD_ASSERT (splt
!= NULL
);
723 splt
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, splt
->size
);
724 if (splt
->contents
== NULL
)
730 /* Function to set the ELF flag bits. */
733 m32c_elf_set_private_flags (bfd
*abfd
, flagword flags
)
735 elf_elfheader (abfd
)->e_flags
= flags
;
736 elf_flags_init (abfd
) = TRUE
;
740 /* Merge backend specific data from an object file to the output
741 object file when linking. */
744 m32c_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
746 flagword old_flags
, old_partial
;
747 flagword new_flags
, new_partial
;
748 bfd_boolean error
= FALSE
;
752 new_opt
[0] = old_opt
[0] = '\0';
753 new_flags
= elf_elfheader (ibfd
)->e_flags
;
754 old_flags
= elf_elfheader (obfd
)->e_flags
;
757 (*_bfd_error_handler
) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
758 old_flags
, new_flags
, elf_flags_init (obfd
) ? "yes" : "no",
759 bfd_get_filename (ibfd
));
762 if (!elf_flags_init (obfd
))
764 /* First call, no flags set. */
765 elf_flags_init (obfd
) = TRUE
;
766 elf_elfheader (obfd
)->e_flags
= new_flags
;
769 else if (new_flags
== old_flags
)
770 /* Compatible flags are ok. */
773 else /* Possibly incompatible flags. */
775 /* Warn if different cpu is used (allow a specific cpu to override
777 new_partial
= (new_flags
& EF_M32C_CPU_MASK
);
778 old_partial
= (old_flags
& EF_M32C_CPU_MASK
);
779 if (new_partial
== old_partial
)
786 default: strcat (new_opt
, " -m16c"); break;
787 case EF_M32C_CPU_M16C
: strcat (new_opt
, " -m16c"); break;
788 case EF_M32C_CPU_M32C
: strcat (new_opt
, " -m32c"); break;
793 default: strcat (old_opt
, " -m16c"); break;
794 case EF_M32C_CPU_M16C
: strcat (old_opt
, " -m16c"); break;
795 case EF_M32C_CPU_M32C
: strcat (old_opt
, " -m32c"); break;
799 /* Print out any mismatches from above. */
803 (*_bfd_error_handler
)
804 (_("%s: compiled with %s and linked with modules compiled with %s"),
805 bfd_get_filename (ibfd
), new_opt
, old_opt
);
808 new_flags
&= ~ EF_M32C_ALL_FLAGS
;
809 old_flags
&= ~ EF_M32C_ALL_FLAGS
;
811 /* Warn about any other mismatches. */
812 if (new_flags
!= old_flags
)
815 (*_bfd_error_handler
)
816 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
817 bfd_get_filename (ibfd
), (long)new_flags
, (long)old_flags
);
822 bfd_set_error (bfd_error_bad_value
);
829 m32c_elf_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
831 FILE *file
= (FILE *) ptr
;
834 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
836 /* Print normal ELF private data. */
837 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
839 flags
= elf_elfheader (abfd
)->e_flags
;
840 fprintf (file
, _("private flags = 0x%lx:"), (long)flags
);
842 switch (flags
& EF_M32C_CPU_MASK
)
845 case EF_M32C_CPU_M16C
: fprintf (file
, " -m16c"); break;
846 case EF_M32C_CPU_M32C
: fprintf (file
, " -m32c"); break;
853 /* Return the MACH for an e_flags value. */
856 elf32_m32c_machine (bfd
*abfd
)
858 switch (elf_elfheader (abfd
)->e_flags
& EF_M32C_CPU_MASK
)
860 case EF_M32C_CPU_M16C
: return bfd_mach_m16c
;
861 case EF_M32C_CPU_M32C
: return bfd_mach_m32c
;
864 return bfd_mach_m16c
;
868 m32c_elf_object_p (bfd
*abfd
)
870 bfd_default_set_arch_mach (abfd
, bfd_arch_m32c
,
871 elf32_m32c_machine (abfd
));
878 dump_symtab (bfd
* abfd
, void *internal_syms
, void *external_syms
)
881 Elf_Internal_Sym
*isymbuf
;
882 Elf_Internal_Sym
*isymend
;
883 Elf_Internal_Sym
*isym
;
884 Elf_Internal_Shdr
*symtab_hdr
;
885 bfd_boolean free_internal
= 0, free_external
= 0;
887 char * st_info_stb_str
;
893 internal_syms
= bfd_malloc (1000);
898 external_syms
= bfd_malloc (1000);
902 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
903 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data(abfd
)->s
->sizeof_sym
;
905 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
906 symtab_hdr
->sh_info
, 0,
907 internal_syms
, external_syms
, NULL
);
909 isymbuf
= internal_syms
;
910 isymend
= isymbuf
+ locsymcount
;
912 for (isym
= isymbuf
; isym
< isymend
; isym
++)
914 switch (ELF_ST_TYPE (isym
->st_info
))
916 case STT_FUNC
: st_info_str
= "STT_FUNC";
917 case STT_SECTION
: st_info_str
= "STT_SECTION";
918 case STT_FILE
: st_info_str
= "STT_FILE";
919 case STT_OBJECT
: st_info_str
= "STT_OBJECT";
920 case STT_TLS
: st_info_str
= "STT_TLS";
921 default: st_info_str
= "";
923 switch (ELF_ST_BIND (isym
->st_info
))
925 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL";
926 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL";
927 default: st_info_stb_str
= "";
929 switch (ELF_ST_VISIBILITY (isym
->st_other
))
931 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT";
932 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL";
933 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED";
934 default: st_other_str
= "";
936 switch (isym
->st_shndx
)
938 case SHN_ABS
: st_shndx_str
= "SHN_ABS";
939 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON";
940 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF";
941 default: st_shndx_str
= "";
944 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
945 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
947 (unsigned long) isym
->st_value
,
948 (unsigned long) isym
->st_size
,
950 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
952 isym
->st_info
, st_info_str
, st_info_stb_str
,
953 isym
->st_other
, st_other_str
,
954 isym
->st_shndx
, st_shndx_str
);
957 free (internal_syms
);
959 free (external_syms
);
963 m32c_get_reloc (long reloc
)
965 if (0 <= reloc
&& reloc
< R_M32C_max
)
966 return m32c_elf_howto_table
[reloc
].name
;
972 /* Handle relaxing. */
974 /* A subroutine of m32c_elf_relax_section. If the global symbol H
975 is within the low 64k, remove any entry for it in the plt. */
977 struct relax_plt_data
984 m32c_relax_plt_check (struct elf_link_hash_entry
*h
,
987 struct relax_plt_data
*data
= (struct relax_plt_data
*) xdata
;
989 if (h
->root
.type
== bfd_link_hash_warning
)
990 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
992 if (h
->plt
.offset
!= (bfd_vma
) -1)
996 if (h
->root
.type
== bfd_link_hash_undefined
997 || h
->root
.type
== bfd_link_hash_undefweak
)
1000 address
= (h
->root
.u
.def
.section
->output_section
->vma
1001 + h
->root
.u
.def
.section
->output_offset
1002 + h
->root
.u
.def
.value
);
1004 if (address
<= 0xffff)
1007 data
->splt
->size
-= 4;
1008 *data
->again
= TRUE
;
1015 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1016 previously had a plt entry, give it a new entry offset. */
1019 m32c_relax_plt_realloc (struct elf_link_hash_entry
*h
,
1022 bfd_vma
*entry
= (bfd_vma
*) xdata
;
1024 if (h
->root
.type
== bfd_link_hash_warning
)
1025 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1027 if (h
->plt
.offset
!= (bfd_vma
) -1)
1029 h
->plt
.offset
= *entry
;
1037 m32c_elf_relax_plt_section (bfd
*dynobj
,
1039 struct bfd_link_info
*info
,
1042 struct relax_plt_data relax_plt_data
;
1045 /* Assume nothing changes. */
1048 if (info
->relocatable
)
1051 /* We only relax the .plt section at the moment. */
1052 if (dynobj
!= elf_hash_table (info
)->dynobj
1053 || strcmp (splt
->name
, ".plt") != 0)
1056 /* Quick check for an empty plt. */
1057 if (splt
->size
== 0)
1060 /* Map across all global symbols; see which ones happen to
1061 fall in the low 64k. */
1062 relax_plt_data
.splt
= splt
;
1063 relax_plt_data
.again
= again
;
1064 elf_link_hash_traverse (elf_hash_table (info
), m32c_relax_plt_check
,
1067 /* Likewise for local symbols, though that's somewhat less convenient
1068 as we have to walk the list of input bfds and swap in symbol data. */
1069 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1071 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1072 Elf_Internal_Shdr
*symtab_hdr
;
1073 Elf_Internal_Sym
*isymbuf
= NULL
;
1076 if (! local_plt_offsets
)
1079 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1080 if (symtab_hdr
->sh_info
!= 0)
1082 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1083 if (isymbuf
== NULL
)
1084 isymbuf
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
,
1085 symtab_hdr
->sh_info
, 0,
1087 if (isymbuf
== NULL
)
1091 for (idx
= 0; idx
< symtab_hdr
->sh_info
; ++idx
)
1093 Elf_Internal_Sym
*isym
;
1097 if (local_plt_offsets
[idx
] == (bfd_vma
) -1)
1100 isym
= &isymbuf
[idx
];
1101 if (isym
->st_shndx
== SHN_UNDEF
)
1103 else if (isym
->st_shndx
== SHN_ABS
)
1104 tsec
= bfd_abs_section_ptr
;
1105 else if (isym
->st_shndx
== SHN_COMMON
)
1106 tsec
= bfd_com_section_ptr
;
1108 tsec
= bfd_section_from_elf_index (ibfd
, isym
->st_shndx
);
1110 address
= (tsec
->output_section
->vma
1111 + tsec
->output_offset
1113 if (address
<= 0xffff)
1115 local_plt_offsets
[idx
] = -1;
1122 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1124 if (! info
->keep_memory
)
1128 /* Cache the symbols for elf_link_input_bfd. */
1129 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1134 /* If we changed anything, walk the symbols again to reallocate
1135 .plt entry addresses. */
1136 if (*again
&& splt
->size
> 0)
1140 elf_link_hash_traverse (elf_hash_table (info
),
1141 m32c_relax_plt_realloc
, &entry
);
1143 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1145 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1146 unsigned int nlocals
= elf_tdata (ibfd
)->symtab_hdr
.sh_info
;
1149 if (! local_plt_offsets
)
1152 for (idx
= 0; idx
< nlocals
; ++idx
)
1153 if (local_plt_offsets
[idx
] != (bfd_vma
) -1)
1155 local_plt_offsets
[idx
] = entry
;
1165 compare_reloc (const void *e1
, const void *e2
)
1167 const Elf_Internal_Rela
*i1
= (const Elf_Internal_Rela
*) e1
;
1168 const Elf_Internal_Rela
*i2
= (const Elf_Internal_Rela
*) e2
;
1170 if (i1
->r_offset
== i2
->r_offset
)
1173 return i1
->r_offset
< i2
->r_offset
? -1 : 1;
1176 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1178 m32c_offset_for_reloc (bfd
*abfd
,
1179 Elf_Internal_Rela
*rel
,
1180 Elf_Internal_Shdr
*symtab_hdr
,
1181 Elf_External_Sym_Shndx
*shndx_buf
,
1182 Elf_Internal_Sym
*intsyms
)
1186 /* Get the value of the symbol referred to by the reloc. */
1187 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1189 /* A local symbol. */
1190 Elf_Internal_Sym
*isym
;
1191 Elf_External_Sym_Shndx
*shndx
;
1195 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1196 ssec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1197 shndx
= shndx_buf
+ (shndx_buf
? ELF32_R_SYM (rel
->r_info
) : 0);
1199 symval
= isym
->st_value
;
1201 symval
+= ssec
->output_section
->vma
1202 + ssec
->output_offset
;
1207 struct elf_link_hash_entry
*h
;
1209 /* An external symbol. */
1210 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1211 h
= elf_sym_hashes (abfd
)[indx
];
1212 BFD_ASSERT (h
!= NULL
);
1214 if (h
->root
.type
!= bfd_link_hash_defined
1215 && h
->root
.type
!= bfd_link_hash_defweak
)
1216 /* This appears to be a reference to an undefined
1217 symbol. Just ignore it--it will be caught by the
1218 regular reloc processing. */
1221 symval
= (h
->root
.u
.def
.value
1222 + h
->root
.u
.def
.section
->output_section
->vma
1223 + h
->root
.u
.def
.section
->output_offset
);
1228 static int bytes_saved
= 0;
1230 static int bytes_to_reloc
[] = {
1238 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1240 /* Mask for the number of relocs associated with this insn. */
1241 #define RLA_RELOCS 0x0000000f
1242 /* Number of bytes gas emitted (before gas's relaxing) */
1243 #define RLA_NBYTES 0x00000ff0
1245 /* If the displacement is within the given range and the new encoding
1246 differs from the old encoding (the index), then the insn can be
1247 relaxed to the new encoding. */
1250 unsigned int max_disp
;
1251 unsigned char new_encoding
;
1254 static EncodingTable m16c_addr_encodings
[] = {
1255 { 0, 0, 0 }, /* R0 */
1256 { 0, 0, 1 }, /* R1 */
1257 { 0, 0, 2 }, /* R2 */
1258 { 0, 0, 3 }, /* R3 */
1259 { 0, 0, 4 }, /* A0 */
1260 { 0, 0, 5 }, /* A1 */
1261 { 0, 0, 6 }, /* [A0] */
1262 { 0, 0, 7 }, /* [A1] */
1263 { 1, 0, 6 }, /* udsp:8[A0] */
1264 { 1, 0, 7 }, /* udsp:8[A1] */
1265 { 1, 0, 10 }, /* udsp:8[SB] */
1266 { 1, 0, 11 }, /* sdsp:8[FB] */
1267 { 2, 255, 8 }, /* udsp:16[A0] */
1268 { 2, 255, 9 }, /* udsp:16[A1] */
1269 { 2, 255, 10 }, /* udsp:16[SB] */
1270 { 2, 0, 15 }, /* abs:16 */
1273 static EncodingTable m16c_jmpaddr_encodings
[] = {
1274 { 0, 0, 0 }, /* R0 */
1275 { 0, 0, 1 }, /* R1 */
1276 { 0, 0, 2 }, /* R2 */
1277 { 0, 0, 3 }, /* R3 */
1278 { 0, 0, 4 }, /* A0 */
1279 { 0, 0, 5 }, /* A1 */
1280 { 0, 0, 6 }, /* [A0] */
1281 { 0, 0, 7 }, /* [A1] */
1282 { 1, 0, 6 }, /* udsp:8[A0] */
1283 { 1, 0, 7 }, /* udsp:8[A1] */
1284 { 1, 0, 10 }, /* udsp:8[SB] */
1285 { 1, 0, 11 }, /* sdsp:8[FB] */
1286 { 3, 255, 8 }, /* udsp:20[A0] */
1287 { 3, 255, 9 }, /* udsp:20[A1] */
1288 { 2, 255, 10 }, /* udsp:16[SB] */
1289 { 2, 0, 15 }, /* abs:16 */
1292 static EncodingTable m32c_addr_encodings
[] = {
1293 { 0, 0, 0 }, /* [A0] */
1294 { 0, 0, 1 }, /* [A1] */
1295 { 0, 0, 2 }, /* A0 */
1296 { 0, 0, 3 }, /* A1 */
1297 { 1, 0, 0 }, /* udsp:8[A0] */
1298 { 1, 0, 1 }, /* udsp:8[A1] */
1299 { 1, 0, 6 }, /* udsp:8[SB] */
1300 { 1, 0, 7 }, /* sdsp:8[FB] */
1301 { 2, 255, 4 }, /* udsp:16[A0] */
1302 { 2, 255, 5 }, /* udsp:16[A1] */
1303 { 2, 255, 6 }, /* udsp:16[SB] */
1304 { 2, 127, 7 }, /* sdsp:16[FB] */
1305 { 3, 65535, 8 }, /* udsp:24[A0] */
1306 { 3, 65535, 9 }, /* udsp:24[A1] */
1307 { 3, 65535, 15 }, /* abs24 */
1308 { 2, 0, 15 }, /* abs16 */
1309 { 0, 0, 16 }, /* R2 */
1310 { 0, 0, 17 }, /* R3 */
1311 { 0, 0, 18 }, /* R0 */
1312 { 0, 0, 19 }, /* R1 */
1328 m32c_elf_relax_section
1331 struct bfd_link_info
* link_info
,
1332 bfd_boolean
* again
)
1334 Elf_Internal_Shdr
*symtab_hdr
;
1335 Elf_Internal_Shdr
*shndx_hdr
;
1336 Elf_Internal_Rela
*internal_relocs
;
1337 Elf_Internal_Rela
*free_relocs
= NULL
;
1338 Elf_Internal_Rela
*irel
, *irelend
, *srel
;
1339 bfd_byte
* contents
= NULL
;
1340 bfd_byte
* free_contents
= NULL
;
1341 Elf_Internal_Sym
*intsyms
= NULL
;
1342 Elf_Internal_Sym
*free_intsyms
= NULL
;
1343 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1346 if (abfd
== elf_hash_table (link_info
)->dynobj
1347 && strcmp (sec
->name
, ".plt") == 0)
1348 return m32c_elf_relax_plt_section (abfd
, sec
, link_info
, again
);
1350 /* Assume nothing changes. */
1353 machine
= elf32_m32c_machine (abfd
);
1355 /* We don't have to do anything for a relocatable link, if
1356 this section does not have relocs, or if this is not a
1358 if (link_info
->relocatable
1359 || (sec
->flags
& SEC_RELOC
) == 0
1360 || sec
->reloc_count
== 0
1361 || (sec
->flags
& SEC_CODE
) == 0)
1364 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1365 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1367 /* Get the section contents. */
1368 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1369 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1370 /* Go get them off disk. */
1371 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1374 /* Read this BFD's symbols. */
1375 /* Get cached copy if it exists. */
1376 if (symtab_hdr
->contents
!= NULL
)
1378 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1382 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
1383 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1386 if (shndx_hdr
->sh_size
!= 0)
1390 amt
= symtab_hdr
->sh_info
;
1391 amt
*= sizeof (Elf_External_Sym_Shndx
);
1392 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1393 if (shndx_buf
== NULL
)
1395 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1396 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1398 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
1401 /* Get a copy of the native relocations. */
1402 internal_relocs
= (_bfd_elf_link_read_relocs
1403 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1404 link_info
->keep_memory
));
1405 if (internal_relocs
== NULL
)
1407 if (! link_info
->keep_memory
)
1408 free_relocs
= internal_relocs
;
1410 /* The RL_ relocs must be just before the operand relocs they go
1411 with, so we must sort them to guarantee this. */
1412 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
1415 /* Walk through them looking for relaxing opportunities. */
1416 irelend
= internal_relocs
+ sec
->reloc_count
;
1418 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1421 unsigned char *insn
, *gap
, *einsn
;
1423 bfd_signed_vma pcrel
;
1429 EncodingTable
*enctbl
;
1432 if (ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_JUMP
1433 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_1ADDR
1434 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_2ADDR
)
1439 /* There will always be room for the relaxed insn, since it is smaller
1440 than the one it would replace. */
1441 BFD_ASSERT (irel
->r_offset
< sec
->size
);
1443 insn
= contents
+ irel
->r_offset
;
1444 relax_relocs
= irel
->r_addend
% 16;
1446 /* Ok, we only have three relocs we care about, and they're all
1447 fake. The lower four bits of the addend is always the number
1448 of following relocs (hence the qsort above) that are assigned
1449 to this opcode. The next 8 bits of the addend indicates the
1450 number of bytes in the insn. We use the rest of them
1451 ourselves as flags for the more expensive operations (defines
1452 above). The three relocs are:
1454 RL_JUMP: This marks all direct jump insns. We check the
1455 displacement and replace them with shorter jumps if
1456 they're in range. We also use this to find JMP.S
1457 insns and manually shorten them when we delete bytes.
1458 We have to decode these insns to figure out what to
1461 RL_1ADDR: This is a :G or :Q insn, which has a single
1462 "standard" operand. We have to extract the type
1463 field, see if it's a wide displacement, then figure
1464 out if we can replace it with a narrow displacement.
1465 We don't have to decode these insns.
1467 RL_2ADDR: Similarly, but two "standard" operands. Note that
1468 r_addend may still be 1, as standard operands don't
1469 always have displacements. Gas shouldn't give us one
1470 with zero operands, but since we don't know which one
1471 has the displacement, we check them both anyway.
1473 These all point to the beginning of the insn itself, not the
1476 Note that we only relax one step at a time, relying on the
1477 linker to call us repeatedly. Thus, there is no code for
1478 JMP.A->JMP.B although that will happen in two steps.
1479 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1482 /* Get the value of the symbol referred to by the reloc. Just
1483 in case this is the last reloc in the list, use the RL's
1484 addend to choose between this reloc (no addend) or the next
1485 (yes addend, which means at least one following reloc). */
1486 srel
= irel
+ (relax_relocs
? 1 : 0);
1487 symval
= OFFSET_FOR_RELOC (srel
);
1489 /* Setting gap_size nonzero is the flag which means "something
1493 new_type
= ELF32_R_TYPE(srel
->r_info
);
1495 pc
= sec
->output_section
->vma
+ sec
->output_offset
1497 pcrel
= symval
- pc
+ srel
->r_addend
;
1499 if (machine
== bfd_mach_m16c
)
1503 switch (ELF32_R_TYPE(irel
->r_info
))
1506 case R_M32C_RL_JUMP
:
1509 case 0xfe: /* jmp.b */
1510 if (pcrel
>= 2 && pcrel
<= 9)
1512 /* Relax JMP.B -> JMP.S. We need to get rid of
1513 the following reloc though. */
1514 insn
[0] = 0x60 | (pcrel
- 2);
1515 new_type
= R_M32C_NONE
;
1516 irel
->r_addend
= 0x10;
1522 case 0xf4: /* jmp.w */
1523 /* 128 is allowed because it will be one byte closer
1524 after relaxing. Likewise for all other pc-rel
1526 if (pcrel
<= 128 && pcrel
>= -128)
1528 /* Relax JMP.W -> JMP.B */
1531 new_type
= R_M32C_8_PCREL
;
1537 case 0xfc: /* jmp.a */
1538 if (pcrel
<= 32768 && pcrel
>= -32768)
1540 /* Relax JMP.A -> JMP.W */
1544 new_type
= R_M32C_16_PCREL
;
1550 case 0xfd: /* jsr.a */
1551 if (pcrel
<= 32768 && pcrel
>= -32768)
1553 /* Relax JSR.A -> JSR.W */
1557 new_type
= R_M32C_16_PCREL
;
1565 case R_M32C_RL_2ADDR
:
1566 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1568 enctbl
= m16c_addr_encodings
;
1570 enc
= (insn
[1] >> 4) & 0x0f;
1573 if (srel
->r_offset
== irel
->r_offset
+ posn
1574 && e
->new_encoding
!= enc
1575 && symval
<= e
->max_disp
)
1578 insn
[1] |= e
->new_encoding
<< 4;
1579 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1580 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1581 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1584 if (relax_relocs
== 2)
1590 case R_M32C_RL_1ADDR
:
1591 /* xxxx xxxx xxxx dest [disp] */
1593 enctbl
= m16c_addr_encodings
;
1596 /* Check the opcode for jumps. We know it's safe to
1597 do this because all 2ADDR insns are at least two
1599 enc
= insn
[0] * 256 + insn
[1];
1606 enctbl
= m16c_jmpaddr_encodings
;
1610 /* srel, posn, and enc must be set here. */
1612 symval
= OFFSET_FOR_RELOC (srel
);
1613 enc
= insn
[1] & 0x0f;
1616 if (srel
->r_offset
== irel
->r_offset
+ posn
1617 && e
->new_encoding
!= enc
1618 && symval
<= e
->max_disp
)
1621 insn
[1] |= e
->new_encoding
;
1622 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1623 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1624 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1630 } /* Ends switch (reloc type) for m16c. */
1632 else /* machine == bfd_mach_m32c */
1636 switch (ELF32_R_TYPE(irel
->r_info
))
1639 case R_M32C_RL_JUMP
:
1642 case 0xbb: /* jmp.b */
1643 if (pcrel
>= 2 && pcrel
<= 9)
1646 /* Relax JMP.B -> JMP.S. We need to get rid of
1647 the following reloc though. */
1648 insn
[0] = 0x4a | ((p
<< 3) & 0x30) | (p
& 1);
1649 new_type
= R_M32C_NONE
;
1650 irel
->r_addend
= 0x10;
1656 case 0xce: /* jmp.w */
1657 if (pcrel
<= 128 && pcrel
>= -128)
1659 /* Relax JMP.W -> JMP.B */
1662 new_type
= R_M32C_8_PCREL
;
1668 case 0xcc: /* jmp.a */
1669 if (pcrel
<= 32768 && pcrel
>= -32768)
1671 /* Relax JMP.A -> JMP.W */
1675 new_type
= R_M32C_16_PCREL
;
1681 case 0xcd: /* jsr.a */
1682 if (pcrel
<= 32768 && pcrel
>= -32768)
1684 /* Relax JSR.A -> JSR.W */
1688 new_type
= R_M32C_16_PCREL
;
1696 case R_M32C_RL_2ADDR
:
1697 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1703 /* prefix; remove it as far as the RL reloc is concerned. */
1708 enctbl
= m32c_addr_encodings
;
1709 enc
= ((einsn
[0] & 0x70) >> 2) | ((einsn
[1] & 0x30) >> 4);
1712 if (srel
->r_offset
== irel
->r_offset
+ posn
1713 && e
->new_encoding
!= enc
1714 && symval
<= e
->max_disp
)
1717 einsn
[0] |= (e
->new_encoding
& 0x1c) << 2;
1719 einsn
[1] |= (e
->new_encoding
& 0x03) << 4;
1720 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1721 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1722 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1725 if (relax_relocs
== 2)
1731 case R_M32C_RL_1ADDR
:
1732 /* xxxx DDDx DDxx xxxx [disp] */
1738 /* prefix; remove it as far as the RL reloc is concerned. */
1743 enctbl
= m32c_addr_encodings
;
1746 /* srel, posn, and enc must be set here. */
1748 symval
= OFFSET_FOR_RELOC (srel
);
1749 enc
= ((einsn
[0] & 0x0e) << 1) | ((einsn
[1] & 0xc0) >> 6);
1752 if (srel
->r_offset
== irel
->r_offset
+ posn
1753 && e
->new_encoding
!= enc
1754 && symval
<= e
->max_disp
)
1757 einsn
[0] |= (e
->new_encoding
& 0x1c) >> 1;
1759 einsn
[1] |= (e
->new_encoding
& 0x03) << 6;
1760 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1761 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1762 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1768 } /* Ends switch (reloc type) for m32c. */
1776 srel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (srel
->r_info
), new_type
);
1778 /* Note that we've changed the relocs, section contents, etc. */
1779 elf_section_data (sec
)->relocs
= internal_relocs
;
1782 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1783 free_contents
= NULL
;
1785 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1786 free_intsyms
= NULL
;
1788 bytes_saved
+= gap_size
;
1790 if (! m32c_elf_relax_delete_bytes(abfd
, sec
, gap
- contents
, gap_size
))
1793 } /* next relocation */
1795 if (free_relocs
!= NULL
)
1801 if (free_contents
!= NULL
)
1803 if (! link_info
->keep_memory
)
1804 free (free_contents
);
1805 /* Cache the section contents for elf_link_input_bfd. */
1807 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1809 free_contents
= NULL
;
1812 if (shndx_buf
!= NULL
)
1814 shndx_hdr
->contents
= NULL
;
1818 if (free_intsyms
!= NULL
)
1820 if (! link_info
->keep_memory
)
1821 free (free_intsyms
);
1822 /* Cache the symbols for elf_link_input_bfd. */
1825 symtab_hdr
->contents
= NULL
/* (unsigned char *) intsyms*/;
1828 free_intsyms
= NULL
;
1834 if (free_relocs
!= NULL
)
1836 if (free_contents
!= NULL
)
1837 free (free_contents
);
1838 if (shndx_buf
!= NULL
)
1840 shndx_hdr
->contents
= NULL
;
1843 if (free_intsyms
!= NULL
)
1844 free (free_intsyms
);
1848 /* Delete some bytes from a section while relaxing. */
1851 m32c_elf_relax_delete_bytes
1857 Elf_Internal_Shdr
*symtab_hdr
;
1858 Elf_Internal_Shdr
*shndx_hdr
;
1861 Elf_Internal_Rela
*irel
;
1862 Elf_Internal_Rela
*irelend
;
1863 Elf_Internal_Rela
*irelalign
;
1865 Elf_Internal_Sym
*isym
;
1866 Elf_Internal_Sym
*isymend
;
1867 Elf_Internal_Sym
*intsyms
;
1868 Elf_External_Sym_Shndx
*shndx_buf
;
1869 Elf_External_Sym_Shndx
*shndx
;
1870 struct elf_link_hash_entry
** sym_hashes
;
1871 struct elf_link_hash_entry
** end_hashes
;
1872 unsigned int symcount
;
1874 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1876 /* The deletion must stop at the next ALIGN reloc for an aligment
1877 power larger than the number of bytes we are deleting. */
1881 irel
= elf_section_data (sec
)->relocs
;
1882 irelend
= irel
+ sec
->reloc_count
;
1884 /* Actually delete the bytes. */
1885 memmove (contents
+ addr
, contents
+ addr
+ count
, (size_t) (toaddr
- addr
- count
));
1888 /* Adjust all the relocs. */
1889 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1891 /* Get the new reloc address. */
1892 if (irel
->r_offset
> addr
&& irel
->r_offset
< toaddr
)
1893 irel
->r_offset
-= count
;
1895 if (ELF32_R_TYPE(irel
->r_info
) == R_M32C_RL_JUMP
1896 && irel
->r_addend
== 0x10 /* one byte insn, no relocs */
1897 && irel
->r_offset
+ 1 < addr
1898 && irel
->r_offset
+ 7 > addr
)
1901 unsigned char *insn
= &contents
[irel
->r_offset
];
1903 /* This is a JMP.S, which we have to manually update. */
1904 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1906 if ((*insn
& 0xf8) != 0x60)
1912 if ((*insn
& 0xce) != 0x4a)
1914 disp
= ((disp
& 0x30) >> 3) | (disp
& 1);
1916 if (irel
->r_offset
+ disp
+ 2 >= addr
+count
)
1919 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1921 *insn
= (*insn
& 0xf8) | disp
;
1925 *insn
= (*insn
& 0xce) | ((disp
& 6) << 3) | (disp
& 1);
1931 /* Adjust the local symbols defined in this section. */
1932 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
1933 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1935 isymend
= isym
+ symtab_hdr
->sh_info
;
1937 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1938 shndx_hdr
= & elf_tdata (abfd
)->symtab_shndx_hdr
;
1939 shndx_buf
= (Elf_External_Sym_Shndx
*) shndx_hdr
->contents
;
1942 for (; isym
< isymend
; isym
++, shndx
= (shndx
? shndx
+ 1 : NULL
))
1945 if ((int) isym
->st_shndx
== sec_shndx
1946 && isym
->st_value
> addr
1947 && isym
->st_value
< toaddr
)
1949 isym
->st_value
-= count
;
1953 /* Now adjust the global symbols defined in this section. */
1954 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1955 - symtab_hdr
->sh_info
);
1956 sym_hashes
= elf_sym_hashes (abfd
);
1957 // sym_hashes += symtab_hdr->sh_info;
1958 end_hashes
= sym_hashes
+ symcount
;
1960 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1962 struct elf_link_hash_entry
* sym_hash
= * sym_hashes
;
1965 ( sym_hash
->root
.type
== bfd_link_hash_defined
1966 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1967 && sym_hash
->root
.u
.def
.section
== sec
1968 && sym_hash
->root
.u
.def
.value
> addr
1969 && sym_hash
->root
.u
.def
.value
< toaddr
)
1971 sym_hash
->root
.u
.def
.value
-= count
;
1979 #define ELF_ARCH bfd_arch_m32c
1980 #define ELF_MACHINE_CODE EM_M32C
1981 #define ELF_MAXPAGESIZE 0x1000
1984 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
1985 #define TARGET_BIG_NAME "elf32-m32c"
1987 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
1988 #define TARGET_LITTLE_NAME "elf32-m32c"
1991 #define elf_info_to_howto_rel NULL
1992 #define elf_info_to_howto m32c_info_to_howto_rela
1993 #define elf_backend_object_p m32c_elf_object_p
1994 #define elf_backend_relocate_section m32c_elf_relocate_section
1995 #define elf_backend_check_relocs m32c_elf_check_relocs
1996 #define elf_backend_object_p m32c_elf_object_p
1997 #define elf_symbol_leading_char ('_')
1998 #define elf_backend_always_size_sections \
1999 m32c_elf_always_size_sections
2000 #define elf_backend_finish_dynamic_sections \
2001 m32c_elf_finish_dynamic_sections
2003 #define elf_backend_can_gc_sections 1
2005 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2006 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2007 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2008 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2009 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2011 #include "elf32-target.h"