1 /* M16C/M32C specific support for 32-bit ELF.
2 Copyright (C) 2005, 2006
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 HOWTO (R_M32C_16
, /* type */
65 1, /* size (0 = byte, 1 = short, 2 = long) */
67 FALSE
, /* pc_relative */
69 complain_overflow_bitfield
, /* complain_on_overflow */
70 bfd_elf_generic_reloc
, /* special_function */
71 "R_M32C_16", /* name */
72 FALSE
, /* partial_inplace */
74 0xffff, /* dst_mask */
75 FALSE
), /* pcrel_offset */
77 HOWTO (R_M32C_24
, /* type */
79 2, /* size (0 = byte, 1 = short, 2 = long) */
81 FALSE
, /* pc_relative */
83 complain_overflow_bitfield
, /* complain_on_overflow */
84 bfd_elf_generic_reloc
, /* special_function */
85 "R_M32C_24", /* name */
86 FALSE
, /* partial_inplace */
88 0xffffff, /* dst_mask */
89 FALSE
), /* pcrel_offset */
91 HOWTO (R_M32C_32
, /* type */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
95 FALSE
, /* pc_relative */
97 complain_overflow_bitfield
, /* complain_on_overflow */
98 bfd_elf_generic_reloc
, /* special_function */
99 "R_M32C_32", /* name */
100 FALSE
, /* partial_inplace */
102 0xffffffff, /* dst_mask */
103 FALSE
), /* pcrel_offset */
105 HOWTO (R_M32C_8_PCREL
, /* type */
107 0, /* size (0 = byte, 1 = short, 2 = long) */
109 TRUE
, /* pc_relative */
111 complain_overflow_signed
, /* complain_on_overflow */
112 bfd_elf_generic_reloc
, /* special_function */
113 "R_M32C_8_PCREL", /* name */
114 FALSE
, /* partial_inplace */
117 TRUE
), /* pcrel_offset */
119 HOWTO (R_M32C_16_PCREL
, /* type */
121 1, /* size (0 = byte, 1 = short, 2 = long) */
123 TRUE
, /* pc_relative */
125 complain_overflow_signed
, /* complain_on_overflow */
126 bfd_elf_generic_reloc
, /* special_function */
127 "R_M32C_16_PCREL", /* name */
128 FALSE
, /* partial_inplace */
130 0xffff, /* dst_mask */
131 TRUE
), /* pcrel_offset */
133 HOWTO (R_M32C_8
, /* type */
135 0, /* size (0 = byte, 1 = short, 2 = long) */
137 FALSE
, /* pc_relative */
139 complain_overflow_unsigned
, /* complain_on_overflow */
140 bfd_elf_generic_reloc
, /* special_function */
141 "R_M32C_8", /* name */
142 FALSE
, /* partial_inplace */
145 FALSE
), /* pcrel_offset */
147 HOWTO (R_M32C_LO16
, /* type */
149 1, /* size (0 = byte, 1 = short, 2 = long) */
151 FALSE
, /* pc_relative */
153 complain_overflow_dont
, /* complain_on_overflow */
154 bfd_elf_generic_reloc
, /* special_function */
155 "R_M32C_LO16", /* name */
156 FALSE
, /* partial_inplace */
158 0xffff, /* dst_mask */
159 FALSE
), /* pcrel_offset */
161 HOWTO (R_M32C_HI8
, /* type */
163 0, /* size (0 = byte, 1 = short, 2 = long) */
165 FALSE
, /* pc_relative */
167 complain_overflow_dont
, /* complain_on_overflow */
168 bfd_elf_generic_reloc
, /* special_function */
169 "R_M32C_HI8", /* name */
170 FALSE
, /* partial_inplace */
173 FALSE
), /* pcrel_offset */
175 HOWTO (R_M32C_HI16
, /* type */
177 1, /* size (0 = byte, 1 = short, 2 = long) */
179 FALSE
, /* pc_relative */
181 complain_overflow_dont
, /* complain_on_overflow */
182 bfd_elf_generic_reloc
, /* special_function */
183 "R_M32C_HI16", /* name */
184 FALSE
, /* partial_inplace */
186 0xffff, /* dst_mask */
187 FALSE
), /* pcrel_offset */
189 HOWTO (R_M32C_RL_JUMP
, /* type */
191 0, /* size (0 = byte, 1 = short, 2 = long) */
193 FALSE
, /* pc_relative */
195 complain_overflow_signed
, /* complain_on_overflow */
196 bfd_elf_generic_reloc
, /* special_function */
197 "R_M32C_RL_JUMP", /* name */
198 FALSE
, /* partial_inplace */
201 FALSE
), /* pcrel_offset */
203 HOWTO (R_M32C_RL_1ADDR
, /* type */
205 0, /* size (0 = byte, 1 = short, 2 = long) */
207 FALSE
, /* pc_relative */
209 complain_overflow_signed
, /* complain_on_overflow */
210 bfd_elf_generic_reloc
, /* special_function */
211 "R_M32C_RL_1ADDR", /* name */
212 FALSE
, /* partial_inplace */
215 FALSE
), /* pcrel_offset */
217 HOWTO (R_M32C_RL_2ADDR
, /* type */
219 0, /* size (0 = byte, 1 = short, 2 = long) */
221 FALSE
, /* pc_relative */
223 complain_overflow_signed
, /* complain_on_overflow */
224 bfd_elf_generic_reloc
, /* special_function */
225 "R_M32C_RL_2ADDR", /* name */
226 FALSE
, /* partial_inplace */
229 FALSE
), /* pcrel_offset */
233 /* Map BFD reloc types to M32C ELF reloc types. */
235 struct m32c_reloc_map
237 bfd_reloc_code_real_type bfd_reloc_val
;
238 unsigned int m32c_reloc_val
;
241 static const struct m32c_reloc_map m32c_reloc_map
[] =
243 { BFD_RELOC_NONE
, R_M32C_NONE
},
244 { BFD_RELOC_16
, R_M32C_16
},
245 { BFD_RELOC_24
, R_M32C_24
},
246 { BFD_RELOC_32
, R_M32C_32
},
247 { BFD_RELOC_8_PCREL
, R_M32C_8_PCREL
},
248 { BFD_RELOC_16_PCREL
, R_M32C_16_PCREL
},
249 { BFD_RELOC_8
, R_M32C_8
},
250 { BFD_RELOC_LO16
, R_M32C_LO16
},
251 { BFD_RELOC_HI16
, R_M32C_HI16
},
252 { BFD_RELOC_M32C_HI8
, R_M32C_HI8
},
253 { BFD_RELOC_M32C_RL_JUMP
, R_M32C_RL_JUMP
},
254 { BFD_RELOC_M32C_RL_1ADDR
, R_M32C_RL_1ADDR
},
255 { BFD_RELOC_M32C_RL_2ADDR
, R_M32C_RL_2ADDR
}
258 static reloc_howto_type
*
259 m32c_reloc_type_lookup
260 (bfd
* abfd ATTRIBUTE_UNUSED
,
261 bfd_reloc_code_real_type code
)
265 for (i
= ARRAY_SIZE (m32c_reloc_map
); --i
;)
266 if (m32c_reloc_map
[i
].bfd_reloc_val
== code
)
267 return & m32c_elf_howto_table
[m32c_reloc_map
[i
].m32c_reloc_val
];
272 /* Set the howto pointer for an M32C ELF reloc. */
275 m32c_info_to_howto_rela
276 (bfd
* abfd ATTRIBUTE_UNUSED
,
278 Elf_Internal_Rela
* dst
)
282 r_type
= ELF32_R_TYPE (dst
->r_info
);
283 BFD_ASSERT (r_type
< (unsigned int) R_M32C_max
);
284 cache_ptr
->howto
= & m32c_elf_howto_table
[r_type
];
289 /* Relocate an M32C ELF section.
290 There is some attempt to make this function usable for many architectures,
291 both USE_REL and USE_RELA ['twould be nice if such a critter existed],
292 if only to serve as a learning tool.
294 The RELOCATE_SECTION function is called by the new ELF backend linker
295 to handle the relocations for a section.
297 The relocs are always passed as Rela structures; if the section
298 actually uses Rel structures, the r_addend field will always be
301 This function is responsible for adjusting the section contents as
302 necessary, and (if using Rela relocs and generating a relocatable
303 output file) adjusting the reloc addend as necessary.
305 This function does not have to worry about setting the reloc
306 address or the reloc symbol index.
308 LOCAL_SYMS is a pointer to the swapped in local symbols.
310 LOCAL_SECTIONS is an array giving the section in the input file
311 corresponding to the st_shndx field of each local symbol.
313 The global hash table entry for the global symbols can be found
314 via elf_sym_hashes (input_bfd).
316 When generating relocatable output, this function must handle
317 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
318 going to be the section symbol corresponding to the output
319 section, which means that the addend must be adjusted
323 m32c_elf_relocate_section
324 (bfd
* output_bfd ATTRIBUTE_UNUSED
,
325 struct bfd_link_info
* info
,
327 asection
* input_section
,
329 Elf_Internal_Rela
* relocs
,
330 Elf_Internal_Sym
* local_syms
,
331 asection
** local_sections
)
333 Elf_Internal_Shdr
* symtab_hdr
;
334 struct elf_link_hash_entry
** sym_hashes
;
335 Elf_Internal_Rela
* rel
;
336 Elf_Internal_Rela
* relend
;
340 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
341 sym_hashes
= elf_sym_hashes (input_bfd
);
342 relend
= relocs
+ input_section
->reloc_count
;
344 dynobj
= elf_hash_table (info
)->dynobj
;
347 splt
= bfd_get_section_by_name (dynobj
, ".plt");
349 for (rel
= relocs
; rel
< relend
; rel
++)
351 reloc_howto_type
* howto
;
352 unsigned long r_symndx
;
353 Elf_Internal_Sym
* sym
;
355 struct elf_link_hash_entry
* h
;
357 bfd_reloc_status_type r
;
358 const char * name
= NULL
;
361 r_type
= ELF32_R_TYPE (rel
->r_info
);
363 /* These are only used for relaxing; we don't actually relocate
364 anything with them, so skip them. */
365 if (r_type
== R_M32C_RL_JUMP
366 || r_type
== R_M32C_RL_1ADDR
367 || r_type
== R_M32C_RL_2ADDR
)
370 r_symndx
= ELF32_R_SYM (rel
->r_info
);
372 if (info
->relocatable
)
374 /* This is a relocatable link. We don't have to change
375 anything, unless the reloc is against a section symbol,
376 in which case we have to adjust according to where the
377 section symbol winds up in the output section. */
378 if (r_symndx
< symtab_hdr
->sh_info
)
380 sym
= local_syms
+ r_symndx
;
382 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
384 sec
= local_sections
[r_symndx
];
385 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
392 /* This is a final link. */
393 howto
= m32c_elf_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
398 if (r_symndx
< symtab_hdr
->sh_info
)
400 sym
= local_syms
+ r_symndx
;
401 sec
= local_sections
[r_symndx
];
402 relocation
= (sec
->output_section
->vma
406 name
= bfd_elf_string_from_elf_section
407 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
408 name
= (sym
->st_name
== 0) ? bfd_section_name (input_bfd
, sec
) : name
;
412 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
414 while (h
->root
.type
== bfd_link_hash_indirect
415 || h
->root
.type
== bfd_link_hash_warning
)
416 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
418 name
= h
->root
.root
.string
;
420 if (h
->root
.type
== bfd_link_hash_defined
421 || h
->root
.type
== bfd_link_hash_defweak
)
423 sec
= h
->root
.u
.def
.section
;
424 relocation
= (h
->root
.u
.def
.value
425 + sec
->output_section
->vma
426 + sec
->output_offset
);
428 else if (h
->root
.type
== bfd_link_hash_undefweak
)
434 if (! ((*info
->callbacks
->undefined_symbol
)
435 (info
, h
->root
.root
.string
, input_bfd
,
436 input_section
, rel
->r_offset
, TRUE
)))
442 switch (ELF32_R_TYPE (rel
->r_info
))
449 plt_offset
= &h
->plt
.offset
;
451 plt_offset
= elf_local_got_offsets (input_bfd
) + r_symndx
;
453 /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
454 relocation, *plt_offset);*/
455 if (relocation
<= 0xffff)
457 /* If the symbol is in range for a 16-bit address, we should
458 have deallocated the plt entry in relax_section. */
459 BFD_ASSERT (*plt_offset
== (bfd_vma
) -1);
463 /* If the symbol is out of range for a 16-bit address,
464 we must have allocated a plt entry. */
465 BFD_ASSERT (*plt_offset
!= (bfd_vma
) -1);
467 /* If this is the first time we've processed this symbol,
468 fill in the plt entry with the correct symbol address. */
469 if ((*plt_offset
& 1) == 0)
473 x
= 0x000000fc; /* jmpf */
474 x
|= (relocation
<< 8) & 0xffffff00;
475 bfd_put_32 (input_bfd
, x
, splt
->contents
+ *plt_offset
);
479 relocation
= (splt
->output_section
->vma
480 + splt
->output_offset
481 + (*plt_offset
& -2));
484 char *newname
= bfd_malloc (strlen(name
)+5);
485 strcpy (newname
, name
);
486 strcat(newname
, ".plt");
487 _bfd_generic_link_add_one_symbol (info
,
490 BSF_FUNCTION
| BSF_WEAK
,
509 printf ("relocate %s at %06lx relocation %06lx addend %ld ",
510 m32c_elf_howto_table
[ELF32_R_TYPE(rel
->r_info
)].name
,
511 rel
->r_offset
+ input_section
->output_section
->vma
+ input_section
->output_offset
,
512 relocation
, rel
->r_addend
);
516 printf (" %02x", contents
[rel
->r_offset
+i
]);
520 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
521 contents
, rel
->r_offset
, relocation
,
524 if (r
!= bfd_reloc_ok
)
526 const char * msg
= (const char *) NULL
;
530 case bfd_reloc_overflow
:
531 r
= info
->callbacks
->reloc_overflow
532 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
, (bfd_vma
) 0,
533 input_bfd
, input_section
, rel
->r_offset
);
536 case bfd_reloc_undefined
:
537 r
= info
->callbacks
->undefined_symbol
538 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
542 case bfd_reloc_outofrange
:
543 msg
= _("internal error: out of range error");
546 case bfd_reloc_notsupported
:
547 msg
= _("internal error: unsupported relocation error");
550 case bfd_reloc_dangerous
:
551 msg
= _("internal error: dangerous relocation");
555 msg
= _("internal error: unknown error");
560 r
= info
->callbacks
->warning
561 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
571 /* We support 16-bit pointers to code above 64k by generating a thunk
572 below 64k containing a JMP instruction to the final address. */
575 m32c_elf_check_relocs
577 struct bfd_link_info
* info
,
579 const Elf_Internal_Rela
* relocs
)
581 Elf_Internal_Shdr
* symtab_hdr
;
582 struct elf_link_hash_entry
** sym_hashes
;
583 struct elf_link_hash_entry
** sym_hashes_end
;
584 const Elf_Internal_Rela
* rel
;
585 const Elf_Internal_Rela
* rel_end
;
586 bfd_vma
*local_plt_offsets
;
590 if (info
->relocatable
)
593 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
594 sym_hashes
= elf_sym_hashes (abfd
);
595 local_plt_offsets
= elf_local_got_offsets (abfd
);
597 dynobj
= elf_hash_table(info
)->dynobj
;
599 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
600 if (!elf_bad_symtab (abfd
))
601 sym_hashes_end
-= symtab_hdr
->sh_info
;
603 rel_end
= relocs
+ sec
->reloc_count
;
604 for (rel
= relocs
; rel
< rel_end
; rel
++)
606 struct elf_link_hash_entry
*h
;
607 unsigned long r_symndx
;
610 r_symndx
= ELF32_R_SYM (rel
->r_info
);
611 if (r_symndx
< symtab_hdr
->sh_info
)
615 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
616 while (h
->root
.type
== bfd_link_hash_indirect
617 || h
->root
.type
== bfd_link_hash_warning
)
618 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
621 switch (ELF32_R_TYPE (rel
->r_info
))
623 /* This relocation describes a 16-bit pointer to a function.
624 We may need to allocate a thunk in low memory; reserve memory
628 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
631 splt
= bfd_get_section_by_name (dynobj
, ".plt");
634 flagword flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
635 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
636 | SEC_READONLY
| SEC_CODE
);
637 splt
= bfd_make_section_with_flags (dynobj
, ".plt", flags
);
639 || ! bfd_set_section_alignment (dynobj
, splt
, 1))
645 offset
= &h
->plt
.offset
;
648 if (local_plt_offsets
== NULL
)
653 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
654 local_plt_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
655 if (local_plt_offsets
== NULL
)
657 elf_local_got_offsets (abfd
) = local_plt_offsets
;
659 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
660 local_plt_offsets
[i
] = (bfd_vma
) -1;
662 offset
= &local_plt_offsets
[r_symndx
];
665 if (*offset
== (bfd_vma
) -1)
667 *offset
= splt
->size
;
677 /* This must exist if dynobj is ever set. */
680 m32c_elf_finish_dynamic_sections (bfd
*abfd ATTRIBUTE_UNUSED
,
681 struct bfd_link_info
*info
)
686 /* As an extra sanity check, verify that all plt entries have
689 if ((dynobj
= elf_hash_table (info
)->dynobj
) != NULL
690 && (splt
= bfd_get_section_by_name (dynobj
, ".plt")) != NULL
)
692 bfd_byte
*contents
= splt
->contents
;
693 unsigned int i
, size
= splt
->size
;
694 for (i
= 0; i
< size
; i
+= 4)
696 unsigned int x
= bfd_get_32 (dynobj
, contents
+ i
);
705 m32c_elf_always_size_sections (bfd
*output_bfd ATTRIBUTE_UNUSED
,
706 struct bfd_link_info
*info
)
711 if (info
->relocatable
)
714 dynobj
= elf_hash_table (info
)->dynobj
;
718 splt
= bfd_get_section_by_name (dynobj
, ".plt");
719 BFD_ASSERT (splt
!= NULL
);
721 splt
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, splt
->size
);
722 if (splt
->contents
== NULL
)
728 /* Function to set the ELF flag bits. */
731 m32c_elf_set_private_flags (bfd
*abfd
, flagword flags
)
733 elf_elfheader (abfd
)->e_flags
= flags
;
734 elf_flags_init (abfd
) = TRUE
;
738 /* Merge backend specific data from an object file to the output
739 object file when linking. */
742 m32c_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
744 flagword old_flags
, old_partial
;
745 flagword new_flags
, new_partial
;
746 bfd_boolean error
= FALSE
;
750 new_opt
[0] = old_opt
[0] = '\0';
751 new_flags
= elf_elfheader (ibfd
)->e_flags
;
752 old_flags
= elf_elfheader (obfd
)->e_flags
;
755 (*_bfd_error_handler
) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s",
756 old_flags
, new_flags
, elf_flags_init (obfd
) ? "yes" : "no",
757 bfd_get_filename (ibfd
));
760 if (!elf_flags_init (obfd
))
762 /* First call, no flags set. */
763 elf_flags_init (obfd
) = TRUE
;
764 elf_elfheader (obfd
)->e_flags
= new_flags
;
767 else if (new_flags
== old_flags
)
768 /* Compatible flags are ok. */
771 else /* Possibly incompatible flags. */
773 /* Warn if different cpu is used (allow a specific cpu to override
775 new_partial
= (new_flags
& EF_M32C_CPU_MASK
);
776 old_partial
= (old_flags
& EF_M32C_CPU_MASK
);
777 if (new_partial
== old_partial
)
784 default: strcat (new_opt
, " -m16c"); break;
785 case EF_M32C_CPU_M16C
: strcat (new_opt
, " -m16c"); break;
786 case EF_M32C_CPU_M32C
: strcat (new_opt
, " -m32c"); break;
791 default: strcat (old_opt
, " -m16c"); break;
792 case EF_M32C_CPU_M16C
: strcat (old_opt
, " -m16c"); break;
793 case EF_M32C_CPU_M32C
: strcat (old_opt
, " -m32c"); break;
797 /* Print out any mismatches from above. */
801 (*_bfd_error_handler
)
802 (_("%s: compiled with %s and linked with modules compiled with %s"),
803 bfd_get_filename (ibfd
), new_opt
, old_opt
);
806 new_flags
&= ~ EF_M32C_ALL_FLAGS
;
807 old_flags
&= ~ EF_M32C_ALL_FLAGS
;
809 /* Warn about any other mismatches. */
810 if (new_flags
!= old_flags
)
813 (*_bfd_error_handler
)
814 (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
815 bfd_get_filename (ibfd
), (long)new_flags
, (long)old_flags
);
820 bfd_set_error (bfd_error_bad_value
);
827 m32c_elf_print_private_bfd_data (bfd
*abfd
, PTR ptr
)
829 FILE *file
= (FILE *) ptr
;
832 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
834 /* Print normal ELF private data. */
835 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
837 flags
= elf_elfheader (abfd
)->e_flags
;
838 fprintf (file
, _("private flags = 0x%lx:"), (long)flags
);
840 switch (flags
& EF_M32C_CPU_MASK
)
843 case EF_M32C_CPU_M16C
: fprintf (file
, " -m16c"); break;
844 case EF_M32C_CPU_M32C
: fprintf (file
, " -m32c"); break;
851 /* Return the MACH for an e_flags value. */
854 elf32_m32c_machine (bfd
*abfd
)
856 switch (elf_elfheader (abfd
)->e_flags
& EF_M32C_CPU_MASK
)
858 case EF_M32C_CPU_M16C
: return bfd_mach_m16c
;
859 case EF_M32C_CPU_M32C
: return bfd_mach_m32c
;
862 return bfd_mach_m16c
;
866 m32c_elf_object_p (bfd
*abfd
)
868 bfd_default_set_arch_mach (abfd
, bfd_arch_m32c
,
869 elf32_m32c_machine (abfd
));
876 dump_symtab (bfd
* abfd
, void *internal_syms
, void *external_syms
)
879 Elf_Internal_Sym
*isymbuf
;
880 Elf_Internal_Sym
*isymend
;
881 Elf_Internal_Sym
*isym
;
882 Elf_Internal_Shdr
*symtab_hdr
;
883 bfd_boolean free_internal
= 0, free_external
= 0;
885 char * st_info_stb_str
;
891 internal_syms
= bfd_malloc (1000);
896 external_syms
= bfd_malloc (1000);
900 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
901 locsymcount
= symtab_hdr
->sh_size
/ get_elf_backend_data(abfd
)->s
->sizeof_sym
;
903 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
904 symtab_hdr
->sh_info
, 0,
905 internal_syms
, external_syms
, NULL
);
907 isymbuf
= internal_syms
;
908 isymend
= isymbuf
+ locsymcount
;
910 for (isym
= isymbuf
; isym
< isymend
; isym
++)
912 switch (ELF_ST_TYPE (isym
->st_info
))
914 case STT_FUNC
: st_info_str
= "STT_FUNC";
915 case STT_SECTION
: st_info_str
= "STT_SECTION";
916 case STT_FILE
: st_info_str
= "STT_FILE";
917 case STT_OBJECT
: st_info_str
= "STT_OBJECT";
918 case STT_TLS
: st_info_str
= "STT_TLS";
919 default: st_info_str
= "";
921 switch (ELF_ST_BIND (isym
->st_info
))
923 case STB_LOCAL
: st_info_stb_str
= "STB_LOCAL";
924 case STB_GLOBAL
: st_info_stb_str
= "STB_GLOBAL";
925 default: st_info_stb_str
= "";
927 switch (ELF_ST_VISIBILITY (isym
->st_other
))
929 case STV_DEFAULT
: st_other_str
= "STV_DEFAULT";
930 case STV_INTERNAL
: st_other_str
= "STV_INTERNAL";
931 case STV_PROTECTED
: st_other_str
= "STV_PROTECTED";
932 default: st_other_str
= "";
934 switch (isym
->st_shndx
)
936 case SHN_ABS
: st_shndx_str
= "SHN_ABS";
937 case SHN_COMMON
: st_shndx_str
= "SHN_COMMON";
938 case SHN_UNDEF
: st_shndx_str
= "SHN_UNDEF";
939 default: st_shndx_str
= "";
942 printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
943 "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
945 (unsigned long) isym
->st_value
,
946 (unsigned long) isym
->st_size
,
948 bfd_elf_string_from_elf_section (abfd
, symtab_hdr
->sh_link
,
950 isym
->st_info
, st_info_str
, st_info_stb_str
,
951 isym
->st_other
, st_other_str
,
952 isym
->st_shndx
, st_shndx_str
);
955 free (internal_syms
);
957 free (external_syms
);
961 m32c_get_reloc (long reloc
)
963 if (0 <= reloc
&& reloc
< R_M32C_max
)
964 return m32c_elf_howto_table
[reloc
].name
;
970 /* Handle relaxing. */
972 /* A subroutine of m32c_elf_relax_section. If the global symbol H
973 is within the low 64k, remove any entry for it in the plt. */
975 struct relax_plt_data
982 m32c_relax_plt_check (struct elf_link_hash_entry
*h
,
985 struct relax_plt_data
*data
= (struct relax_plt_data
*) xdata
;
987 if (h
->root
.type
== bfd_link_hash_warning
)
988 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
990 if (h
->plt
.offset
!= (bfd_vma
) -1)
994 if (h
->root
.type
== bfd_link_hash_undefined
995 || h
->root
.type
== bfd_link_hash_undefweak
)
998 address
= (h
->root
.u
.def
.section
->output_section
->vma
999 + h
->root
.u
.def
.section
->output_offset
1000 + h
->root
.u
.def
.value
);
1002 if (address
<= 0xffff)
1005 data
->splt
->size
-= 4;
1006 *data
->again
= TRUE
;
1013 /* A subroutine of m32c_elf_relax_section. If the global symbol H
1014 previously had a plt entry, give it a new entry offset. */
1017 m32c_relax_plt_realloc (struct elf_link_hash_entry
*h
,
1020 bfd_vma
*entry
= (bfd_vma
*) xdata
;
1022 if (h
->root
.type
== bfd_link_hash_warning
)
1023 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1025 if (h
->plt
.offset
!= (bfd_vma
) -1)
1027 h
->plt
.offset
= *entry
;
1035 m32c_elf_relax_plt_section (bfd
*dynobj
,
1037 struct bfd_link_info
*info
,
1040 struct relax_plt_data relax_plt_data
;
1043 /* Assume nothing changes. */
1046 if (info
->relocatable
)
1049 /* We only relax the .plt section at the moment. */
1050 if (dynobj
!= elf_hash_table (info
)->dynobj
1051 || strcmp (splt
->name
, ".plt") != 0)
1054 /* Quick check for an empty plt. */
1055 if (splt
->size
== 0)
1058 /* Map across all global symbols; see which ones happen to
1059 fall in the low 64k. */
1060 relax_plt_data
.splt
= splt
;
1061 relax_plt_data
.again
= again
;
1062 elf_link_hash_traverse (elf_hash_table (info
), m32c_relax_plt_check
,
1065 /* Likewise for local symbols, though that's somewhat less convenient
1066 as we have to walk the list of input bfds and swap in symbol data. */
1067 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1069 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1070 Elf_Internal_Shdr
*symtab_hdr
;
1071 Elf_Internal_Sym
*isymbuf
= NULL
;
1074 if (! local_plt_offsets
)
1077 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1078 if (symtab_hdr
->sh_info
!= 0)
1080 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1081 if (isymbuf
== NULL
)
1082 isymbuf
= bfd_elf_get_elf_syms (ibfd
, symtab_hdr
,
1083 symtab_hdr
->sh_info
, 0,
1085 if (isymbuf
== NULL
)
1089 for (idx
= 0; idx
< symtab_hdr
->sh_info
; ++idx
)
1091 Elf_Internal_Sym
*isym
;
1095 if (local_plt_offsets
[idx
] == (bfd_vma
) -1)
1098 isym
= &isymbuf
[idx
];
1099 if (isym
->st_shndx
== SHN_UNDEF
)
1101 else if (isym
->st_shndx
== SHN_ABS
)
1102 tsec
= bfd_abs_section_ptr
;
1103 else if (isym
->st_shndx
== SHN_COMMON
)
1104 tsec
= bfd_com_section_ptr
;
1106 tsec
= bfd_section_from_elf_index (ibfd
, isym
->st_shndx
);
1108 address
= (tsec
->output_section
->vma
1109 + tsec
->output_offset
1111 if (address
<= 0xffff)
1113 local_plt_offsets
[idx
] = -1;
1120 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1122 if (! info
->keep_memory
)
1126 /* Cache the symbols for elf_link_input_bfd. */
1127 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1132 /* If we changed anything, walk the symbols again to reallocate
1133 .plt entry addresses. */
1134 if (*again
&& splt
->size
> 0)
1138 elf_link_hash_traverse (elf_hash_table (info
),
1139 m32c_relax_plt_realloc
, &entry
);
1141 for (ibfd
= info
->input_bfds
; ibfd
; ibfd
= ibfd
->link_next
)
1143 bfd_vma
*local_plt_offsets
= elf_local_got_offsets (ibfd
);
1144 unsigned int nlocals
= elf_tdata (ibfd
)->symtab_hdr
.sh_info
;
1147 if (! local_plt_offsets
)
1150 for (idx
= 0; idx
< nlocals
; ++idx
)
1151 if (local_plt_offsets
[idx
] != (bfd_vma
) -1)
1153 local_plt_offsets
[idx
] = entry
;
1163 compare_reloc (const void *e1
, const void *e2
)
1165 const Elf_Internal_Rela
*i1
= (const Elf_Internal_Rela
*) e1
;
1166 const Elf_Internal_Rela
*i2
= (const Elf_Internal_Rela
*) e2
;
1168 if (i1
->r_offset
== i2
->r_offset
)
1171 return i1
->r_offset
< i2
->r_offset
? -1 : 1;
1174 #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1176 m32c_offset_for_reloc (bfd
*abfd
,
1177 Elf_Internal_Rela
*rel
,
1178 Elf_Internal_Shdr
*symtab_hdr
,
1179 Elf_External_Sym_Shndx
*shndx_buf
,
1180 Elf_Internal_Sym
*intsyms
)
1184 /* Get the value of the symbol referred to by the reloc. */
1185 if (ELF32_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
1187 /* A local symbol. */
1188 Elf_Internal_Sym
*isym
;
1189 Elf_External_Sym_Shndx
*shndx
;
1193 isym
= intsyms
+ ELF32_R_SYM (rel
->r_info
);
1194 ssec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1195 shndx
= shndx_buf
+ (shndx_buf
? ELF32_R_SYM (rel
->r_info
) : 0);
1197 symval
= isym
->st_value
;
1199 symval
+= ssec
->output_section
->vma
1200 + ssec
->output_offset
;
1205 struct elf_link_hash_entry
*h
;
1207 /* An external symbol. */
1208 indx
= ELF32_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
1209 h
= elf_sym_hashes (abfd
)[indx
];
1210 BFD_ASSERT (h
!= NULL
);
1212 if (h
->root
.type
!= bfd_link_hash_defined
1213 && h
->root
.type
!= bfd_link_hash_defweak
)
1214 /* This appears to be a reference to an undefined
1215 symbol. Just ignore it--it will be caught by the
1216 regular reloc processing. */
1219 symval
= (h
->root
.u
.def
.value
1220 + h
->root
.u
.def
.section
->output_section
->vma
1221 + h
->root
.u
.def
.section
->output_offset
);
1226 static int bytes_saved
= 0;
1228 static int bytes_to_reloc
[] = {
1236 /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */
1238 /* Mask for the number of relocs associated with this insn. */
1239 #define RLA_RELOCS 0x0000000f
1240 /* Number of bytes gas emitted (before gas's relaxing) */
1241 #define RLA_NBYTES 0x00000ff0
1243 /* If the displacement is within the given range and the new encoding
1244 differs from the old encoding (the index), then the insn can be
1245 relaxed to the new encoding. */
1248 unsigned int max_disp
;
1249 unsigned char new_encoding
;
1252 static EncodingTable m16c_addr_encodings
[] = {
1253 { 0, 0, 0 }, /* R0 */
1254 { 0, 0, 1 }, /* R1 */
1255 { 0, 0, 2 }, /* R2 */
1256 { 0, 0, 3 }, /* R3 */
1257 { 0, 0, 4 }, /* A0 */
1258 { 0, 0, 5 }, /* A1 */
1259 { 0, 0, 6 }, /* [A0] */
1260 { 0, 0, 7 }, /* [A1] */
1261 { 1, 0, 6 }, /* udsp:8[A0] */
1262 { 1, 0, 7 }, /* udsp:8[A1] */
1263 { 1, 0, 10 }, /* udsp:8[SB] */
1264 { 1, 0, 11 }, /* sdsp:8[FB] */
1265 { 2, 255, 8 }, /* udsp:16[A0] */
1266 { 2, 255, 9 }, /* udsp:16[A1] */
1267 { 2, 255, 10 }, /* udsp:16[SB] */
1268 { 2, 0, 15 }, /* abs:16 */
1271 static EncodingTable m16c_jmpaddr_encodings
[] = {
1272 { 0, 0, 0 }, /* R0 */
1273 { 0, 0, 1 }, /* R1 */
1274 { 0, 0, 2 }, /* R2 */
1275 { 0, 0, 3 }, /* R3 */
1276 { 0, 0, 4 }, /* A0 */
1277 { 0, 0, 5 }, /* A1 */
1278 { 0, 0, 6 }, /* [A0] */
1279 { 0, 0, 7 }, /* [A1] */
1280 { 1, 0, 6 }, /* udsp:8[A0] */
1281 { 1, 0, 7 }, /* udsp:8[A1] */
1282 { 1, 0, 10 }, /* udsp:8[SB] */
1283 { 1, 0, 11 }, /* sdsp:8[FB] */
1284 { 3, 255, 8 }, /* udsp:20[A0] */
1285 { 3, 255, 9 }, /* udsp:20[A1] */
1286 { 2, 255, 10 }, /* udsp:16[SB] */
1287 { 2, 0, 15 }, /* abs:16 */
1290 static EncodingTable m32c_addr_encodings
[] = {
1291 { 0, 0, 0 }, /* [A0] */
1292 { 0, 0, 1 }, /* [A1] */
1293 { 0, 0, 2 }, /* A0 */
1294 { 0, 0, 3 }, /* A1 */
1295 { 1, 0, 0 }, /* udsp:8[A0] */
1296 { 1, 0, 1 }, /* udsp:8[A1] */
1297 { 1, 0, 6 }, /* udsp:8[SB] */
1298 { 1, 0, 7 }, /* sdsp:8[FB] */
1299 { 2, 255, 4 }, /* udsp:16[A0] */
1300 { 2, 255, 5 }, /* udsp:16[A1] */
1301 { 2, 255, 6 }, /* udsp:16[SB] */
1302 { 2, 127, 7 }, /* sdsp:16[FB] */
1303 { 3, 65535, 8 }, /* udsp:24[A0] */
1304 { 3, 65535, 9 }, /* udsp:24[A1] */
1305 { 3, 65535, 15 }, /* abs24 */
1306 { 2, 0, 15 }, /* abs16 */
1307 { 0, 0, 16 }, /* R2 */
1308 { 0, 0, 17 }, /* R3 */
1309 { 0, 0, 18 }, /* R0 */
1310 { 0, 0, 19 }, /* R1 */
1326 m32c_elf_relax_section
1329 struct bfd_link_info
* link_info
,
1330 bfd_boolean
* again
)
1332 Elf_Internal_Shdr
*symtab_hdr
;
1333 Elf_Internal_Shdr
*shndx_hdr
;
1334 Elf_Internal_Rela
*internal_relocs
;
1335 Elf_Internal_Rela
*free_relocs
= NULL
;
1336 Elf_Internal_Rela
*irel
, *irelend
, *srel
;
1337 bfd_byte
* contents
= NULL
;
1338 bfd_byte
* free_contents
= NULL
;
1339 Elf_Internal_Sym
*intsyms
= NULL
;
1340 Elf_Internal_Sym
*free_intsyms
= NULL
;
1341 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1344 if (abfd
== elf_hash_table (link_info
)->dynobj
1345 && strcmp (sec
->name
, ".plt") == 0)
1346 return m32c_elf_relax_plt_section (abfd
, sec
, link_info
, again
);
1348 /* Assume nothing changes. */
1351 machine
= elf32_m32c_machine (abfd
);
1353 /* We don't have to do anything for a relocatable link, if
1354 this section does not have relocs, or if this is not a
1356 if (link_info
->relocatable
1357 || (sec
->flags
& SEC_RELOC
) == 0
1358 || sec
->reloc_count
== 0
1359 || (sec
->flags
& SEC_CODE
) == 0)
1362 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1363 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1365 /* Get the section contents. */
1366 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1367 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1368 /* Go get them off disk. */
1369 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1372 /* Read this BFD's symbols. */
1373 /* Get cached copy if it exists. */
1374 if (symtab_hdr
->contents
!= NULL
)
1376 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1380 intsyms
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
, symtab_hdr
->sh_info
, 0, NULL
, NULL
, NULL
);
1381 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1384 if (shndx_hdr
->sh_size
!= 0)
1388 amt
= symtab_hdr
->sh_info
;
1389 amt
*= sizeof (Elf_External_Sym_Shndx
);
1390 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1391 if (shndx_buf
== NULL
)
1393 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1394 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
1396 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
1399 /* Get a copy of the native relocations. */
1400 internal_relocs
= (_bfd_elf_link_read_relocs
1401 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1402 link_info
->keep_memory
));
1403 if (internal_relocs
== NULL
)
1405 if (! link_info
->keep_memory
)
1406 free_relocs
= internal_relocs
;
1408 /* The RL_ relocs must be just before the operand relocs they go
1409 with, so we must sort them to guarantee this. */
1410 qsort (internal_relocs
, sec
->reloc_count
, sizeof (Elf_Internal_Rela
),
1413 /* Walk through them looking for relaxing opportunities. */
1414 irelend
= internal_relocs
+ sec
->reloc_count
;
1416 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1419 unsigned char *insn
, *gap
, *einsn
;
1421 bfd_signed_vma pcrel
;
1427 EncodingTable
*enctbl
;
1430 if (ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_JUMP
1431 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_1ADDR
1432 && ELF32_R_TYPE(irel
->r_info
) != R_M32C_RL_2ADDR
)
1437 /* There will always be room for the relaxed insn, since it is smaller
1438 than the one it would replace. */
1439 BFD_ASSERT (irel
->r_offset
< sec
->size
);
1441 insn
= contents
+ irel
->r_offset
;
1442 relax_relocs
= irel
->r_addend
% 16;
1444 /* Ok, we only have three relocs we care about, and they're all
1445 fake. The lower four bits of the addend is always the number
1446 of following relocs (hence the qsort above) that are assigned
1447 to this opcode. The next 8 bits of the addend indicates the
1448 number of bytes in the insn. We use the rest of them
1449 ourselves as flags for the more expensive operations (defines
1450 above). The three relocs are:
1452 RL_JUMP: This marks all direct jump insns. We check the
1453 displacement and replace them with shorter jumps if
1454 they're in range. We also use this to find JMP.S
1455 insns and manually shorten them when we delete bytes.
1456 We have to decode these insns to figure out what to
1459 RL_1ADDR: This is a :G or :Q insn, which has a single
1460 "standard" operand. We have to extract the type
1461 field, see if it's a wide displacement, then figure
1462 out if we can replace it with a narrow displacement.
1463 We don't have to decode these insns.
1465 RL_2ADDR: Similarly, but two "standard" operands. Note that
1466 r_addend may still be 1, as standard operands don't
1467 always have displacements. Gas shouldn't give us one
1468 with zero operands, but since we don't know which one
1469 has the displacement, we check them both anyway.
1471 These all point to the beginning of the insn itself, not the
1474 Note that we only relax one step at a time, relying on the
1475 linker to call us repeatedly. Thus, there is no code for
1476 JMP.A->JMP.B although that will happen in two steps.
1477 Likewise, for 2ADDR relaxes, we do one operand per cycle.
1480 /* Get the value of the symbol referred to by the reloc. Just
1481 in case this is the last reloc in the list, use the RL's
1482 addend to choose between this reloc (no addend) or the next
1483 (yes addend, which means at least one following reloc). */
1484 srel
= irel
+ (relax_relocs
? 1 : 0);
1485 symval
= OFFSET_FOR_RELOC (srel
);
1487 /* Setting gap_size nonzero is the flag which means "something
1491 new_type
= ELF32_R_TYPE(srel
->r_info
);
1493 pc
= sec
->output_section
->vma
+ sec
->output_offset
1495 pcrel
= symval
- pc
+ srel
->r_addend
;
1497 if (machine
== bfd_mach_m16c
)
1501 switch (ELF32_R_TYPE(irel
->r_info
))
1504 case R_M32C_RL_JUMP
:
1507 case 0xfe: /* jmp.b */
1508 if (pcrel
>= 2 && pcrel
<= 9)
1510 /* Relax JMP.B -> JMP.S. We need to get rid of
1511 the following reloc though. */
1512 insn
[0] = 0x60 | (pcrel
- 2);
1513 new_type
= R_M32C_NONE
;
1514 irel
->r_addend
= 0x10;
1520 case 0xf4: /* jmp.w */
1521 /* 128 is allowed because it will be one byte closer
1522 after relaxing. Likewise for all other pc-rel
1524 if (pcrel
<= 128 && pcrel
>= -128)
1526 /* Relax JMP.W -> JMP.B */
1529 new_type
= R_M32C_8_PCREL
;
1535 case 0xfc: /* jmp.a */
1536 if (pcrel
<= 32768 && pcrel
>= -32768)
1538 /* Relax JMP.A -> JMP.W */
1542 new_type
= R_M32C_16_PCREL
;
1548 case 0xfd: /* jsr.a */
1549 if (pcrel
<= 32768 && pcrel
>= -32768)
1551 /* Relax JSR.A -> JSR.W */
1555 new_type
= R_M32C_16_PCREL
;
1563 case R_M32C_RL_2ADDR
:
1564 /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1566 enctbl
= m16c_addr_encodings
;
1568 enc
= (insn
[1] >> 4) & 0x0f;
1571 if (srel
->r_offset
== irel
->r_offset
+ posn
1572 && e
->new_encoding
!= enc
1573 && symval
<= e
->max_disp
)
1576 insn
[1] |= e
->new_encoding
<< 4;
1577 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1578 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1579 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1582 if (relax_relocs
== 2)
1588 case R_M32C_RL_1ADDR
:
1589 /* xxxx xxxx xxxx dest [disp] */
1591 enctbl
= m16c_addr_encodings
;
1594 /* Check the opcode for jumps. We know it's safe to
1595 do this because all 2ADDR insns are at least two
1597 enc
= insn
[0] * 256 + insn
[1];
1604 enctbl
= m16c_jmpaddr_encodings
;
1608 /* srel, posn, and enc must be set here. */
1610 symval
= OFFSET_FOR_RELOC (srel
);
1611 enc
= insn
[1] & 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
;
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
];
1628 } /* Ends switch (reloc type) for m16c. */
1630 else /* machine == bfd_mach_m32c */
1634 switch (ELF32_R_TYPE(irel
->r_info
))
1637 case R_M32C_RL_JUMP
:
1640 case 0xbb: /* jmp.b */
1641 if (pcrel
>= 2 && pcrel
<= 9)
1644 /* Relax JMP.B -> JMP.S. We need to get rid of
1645 the following reloc though. */
1646 insn
[0] = 0x4a | ((p
<< 3) & 0x30) | (p
& 1);
1647 new_type
= R_M32C_NONE
;
1648 irel
->r_addend
= 0x10;
1654 case 0xce: /* jmp.w */
1655 if (pcrel
<= 128 && pcrel
>= -128)
1657 /* Relax JMP.W -> JMP.B */
1660 new_type
= R_M32C_8_PCREL
;
1666 case 0xcc: /* jmp.a */
1667 if (pcrel
<= 32768 && pcrel
>= -32768)
1669 /* Relax JMP.A -> JMP.W */
1673 new_type
= R_M32C_16_PCREL
;
1679 case 0xcd: /* jsr.a */
1680 if (pcrel
<= 32768 && pcrel
>= -32768)
1682 /* Relax JSR.A -> JSR.W */
1686 new_type
= R_M32C_16_PCREL
;
1694 case R_M32C_RL_2ADDR
:
1695 /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1701 /* prefix; remove it as far as the RL reloc is concerned. */
1706 enctbl
= m32c_addr_encodings
;
1707 enc
= ((einsn
[0] & 0x70) >> 2) | ((einsn
[1] & 0x30) >> 4);
1710 if (srel
->r_offset
== irel
->r_offset
+ posn
1711 && e
->new_encoding
!= enc
1712 && symval
<= e
->max_disp
)
1715 einsn
[0] |= (e
->new_encoding
& 0x1c) << 2;
1717 einsn
[1] |= (e
->new_encoding
& 0x03) << 4;
1718 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1719 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1720 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1723 if (relax_relocs
== 2)
1729 case R_M32C_RL_1ADDR
:
1730 /* xxxx DDDx DDxx xxxx [disp] */
1736 /* prefix; remove it as far as the RL reloc is concerned. */
1741 enctbl
= m32c_addr_encodings
;
1744 /* srel, posn, and enc must be set here. */
1746 symval
= OFFSET_FOR_RELOC (srel
);
1747 enc
= ((einsn
[0] & 0x0e) << 1) | ((einsn
[1] & 0xc0) >> 6);
1750 if (srel
->r_offset
== irel
->r_offset
+ posn
1751 && e
->new_encoding
!= enc
1752 && symval
<= e
->max_disp
)
1755 einsn
[0] |= (e
->new_encoding
& 0x1c) >> 1;
1757 einsn
[1] |= (e
->new_encoding
& 0x03) << 6;
1758 gap_size
= e
->bytes
- enctbl
[e
->new_encoding
].bytes
;
1759 gap
= insn
+ posn
+ enctbl
[e
->new_encoding
].bytes
;
1760 new_type
= bytes_to_reloc
[enctbl
[e
->new_encoding
].bytes
];
1766 } /* Ends switch (reloc type) for m32c. */
1774 srel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (srel
->r_info
), new_type
);
1776 /* Note that we've changed the relocs, section contents, etc. */
1777 elf_section_data (sec
)->relocs
= internal_relocs
;
1780 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1781 free_contents
= NULL
;
1783 symtab_hdr
->contents
= (bfd_byte
*) intsyms
;
1784 free_intsyms
= NULL
;
1786 bytes_saved
+= gap_size
;
1788 if (! m32c_elf_relax_delete_bytes(abfd
, sec
, gap
- contents
, gap_size
))
1791 } /* next relocation */
1793 if (free_relocs
!= NULL
)
1799 if (free_contents
!= NULL
)
1801 if (! link_info
->keep_memory
)
1802 free (free_contents
);
1803 /* Cache the section contents for elf_link_input_bfd. */
1805 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1807 free_contents
= NULL
;
1810 if (shndx_buf
!= NULL
)
1812 shndx_hdr
->contents
= NULL
;
1816 if (free_intsyms
!= NULL
)
1818 if (! link_info
->keep_memory
)
1819 free (free_intsyms
);
1820 /* Cache the symbols for elf_link_input_bfd. */
1823 symtab_hdr
->contents
= NULL
/* (unsigned char *) intsyms*/;
1826 free_intsyms
= NULL
;
1832 if (free_relocs
!= NULL
)
1834 if (free_contents
!= NULL
)
1835 free (free_contents
);
1836 if (shndx_buf
!= NULL
)
1838 shndx_hdr
->contents
= NULL
;
1841 if (free_intsyms
!= NULL
)
1842 free (free_intsyms
);
1846 /* Delete some bytes from a section while relaxing. */
1849 m32c_elf_relax_delete_bytes
1855 Elf_Internal_Shdr
*symtab_hdr
;
1856 Elf_Internal_Shdr
*shndx_hdr
;
1859 Elf_Internal_Rela
*irel
;
1860 Elf_Internal_Rela
*irelend
;
1861 Elf_Internal_Rela
*irelalign
;
1863 Elf_Internal_Sym
*isym
;
1864 Elf_Internal_Sym
*isymend
;
1865 Elf_Internal_Sym
*intsyms
;
1866 Elf_External_Sym_Shndx
*shndx_buf
;
1867 Elf_External_Sym_Shndx
*shndx
;
1868 struct elf_link_hash_entry
** sym_hashes
;
1869 struct elf_link_hash_entry
** end_hashes
;
1870 unsigned int symcount
;
1872 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1874 /* The deletion must stop at the next ALIGN reloc for an aligment
1875 power larger than the number of bytes we are deleting. */
1879 irel
= elf_section_data (sec
)->relocs
;
1880 irelend
= irel
+ sec
->reloc_count
;
1882 /* Actually delete the bytes. */
1883 memmove (contents
+ addr
, contents
+ addr
+ count
, (size_t) (toaddr
- addr
- count
));
1886 /* Adjust all the relocs. */
1887 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1889 /* Get the new reloc address. */
1890 if (irel
->r_offset
> addr
&& irel
->r_offset
< toaddr
)
1891 irel
->r_offset
-= count
;
1893 if (ELF32_R_TYPE(irel
->r_info
) == R_M32C_RL_JUMP
1894 && irel
->r_addend
== 0x10 /* one byte insn, no relocs */
1895 && irel
->r_offset
+ 1 < addr
1896 && irel
->r_offset
+ 7 > addr
)
1899 unsigned char *insn
= &contents
[irel
->r_offset
];
1901 /* This is a JMP.S, which we have to manually update. */
1902 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1904 if ((*insn
& 0xf8) != 0x60)
1910 if ((*insn
& 0xce) != 0x4a)
1912 disp
= ((disp
& 0x30) >> 3) | (disp
& 1);
1914 if (irel
->r_offset
+ disp
+ 2 >= addr
+count
)
1917 if (elf32_m32c_machine (abfd
) == bfd_mach_m16c
)
1919 *insn
= (*insn
& 0xf8) | disp
;
1923 *insn
= (*insn
& 0xce) | ((disp
& 6) << 3) | (disp
& 1);
1929 /* Adjust the local symbols defined in this section. */
1930 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
1931 intsyms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1933 isymend
= isym
+ symtab_hdr
->sh_info
;
1935 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1936 shndx_hdr
= & elf_tdata (abfd
)->symtab_shndx_hdr
;
1937 shndx_buf
= (Elf_External_Sym_Shndx
*) shndx_hdr
->contents
;
1940 for (; isym
< isymend
; isym
++, shndx
= (shndx
? shndx
+ 1 : NULL
))
1943 if ((int) isym
->st_shndx
== sec_shndx
1944 && isym
->st_value
> addr
1945 && isym
->st_value
< toaddr
)
1947 isym
->st_value
-= count
;
1951 /* Now adjust the global symbols defined in this section. */
1952 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1953 - symtab_hdr
->sh_info
);
1954 sym_hashes
= elf_sym_hashes (abfd
);
1955 // sym_hashes += symtab_hdr->sh_info;
1956 end_hashes
= sym_hashes
+ symcount
;
1958 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1960 struct elf_link_hash_entry
* sym_hash
= * sym_hashes
;
1963 ( sym_hash
->root
.type
== bfd_link_hash_defined
1964 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1965 && sym_hash
->root
.u
.def
.section
== sec
1966 && sym_hash
->root
.u
.def
.value
> addr
1967 && sym_hash
->root
.u
.def
.value
< toaddr
)
1969 sym_hash
->root
.u
.def
.value
-= count
;
1977 #define ELF_ARCH bfd_arch_m32c
1978 #define ELF_MACHINE_CODE EM_M32C
1979 #define ELF_MAXPAGESIZE 0x1000
1982 #define TARGET_BIG_SYM bfd_elf32_m32c_vec
1983 #define TARGET_BIG_NAME "elf32-m32c"
1985 #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec
1986 #define TARGET_LITTLE_NAME "elf32-m32c"
1989 #define elf_info_to_howto_rel NULL
1990 #define elf_info_to_howto m32c_info_to_howto_rela
1991 #define elf_backend_object_p m32c_elf_object_p
1992 #define elf_backend_relocate_section m32c_elf_relocate_section
1993 #define elf_backend_check_relocs m32c_elf_check_relocs
1994 #define elf_backend_object_p m32c_elf_object_p
1995 #define elf_symbol_leading_char ('_')
1996 #define elf_backend_always_size_sections \
1997 m32c_elf_always_size_sections
1998 #define elf_backend_finish_dynamic_sections \
1999 m32c_elf_finish_dynamic_sections
2001 #define elf_backend_can_gc_sections 1
2003 #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup
2004 #define bfd_elf32_bfd_relax_section m32c_elf_relax_section
2005 #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags
2006 #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data
2007 #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data
2009 #include "elf32-target.h"