1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 mn10200_elf_relax_delete_bytes (bfd
*, asection
*, bfd_vma
, int);
29 mn10200_elf_symbol_address_p (bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
);
44 static reloc_howto_type elf_mn10200_howto_table
[] =
46 /* Dummy relocation. Does nothing. */
47 HOWTO (R_MN10200_NONE
,
53 complain_overflow_dont
,
54 bfd_elf_generic_reloc
,
60 /* Standard 32 bit reloc. */
67 complain_overflow_bitfield
,
68 bfd_elf_generic_reloc
,
74 /* Standard 16 bit reloc. */
81 complain_overflow_bitfield
,
82 bfd_elf_generic_reloc
,
88 /* Standard 8 bit reloc. */
95 complain_overflow_bitfield
,
96 bfd_elf_generic_reloc
,
102 /* Standard 24 bit reloc. */
109 complain_overflow_bitfield
,
110 bfd_elf_generic_reloc
,
116 /* Simple 8 pc-relative reloc. */
117 HOWTO (R_MN10200_PCREL8
,
123 complain_overflow_bitfield
,
124 bfd_elf_generic_reloc
,
130 /* Simple 16 pc-relative reloc. */
131 HOWTO (R_MN10200_PCREL16
,
137 complain_overflow_bitfield
,
138 bfd_elf_generic_reloc
,
144 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
145 to get the pc-relative offset correct. */
146 HOWTO (R_MN10200_PCREL24
,
152 complain_overflow_bitfield
,
153 bfd_elf_generic_reloc
,
161 struct mn10200_reloc_map
163 bfd_reloc_code_real_type bfd_reloc_val
;
164 unsigned char elf_reloc_val
;
167 static const struct mn10200_reloc_map mn10200_reloc_map
[] =
169 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
170 { BFD_RELOC_32
, R_MN10200_32
, },
171 { BFD_RELOC_16
, R_MN10200_16
, },
172 { BFD_RELOC_8
, R_MN10200_8
, },
173 { BFD_RELOC_24
, R_MN10200_24
, },
174 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
175 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
176 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
179 static reloc_howto_type
*
180 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
181 bfd_reloc_code_real_type code
)
186 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
189 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
190 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
196 static reloc_howto_type
*
197 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
203 i
< (sizeof (elf_mn10200_howto_table
)
204 / sizeof (elf_mn10200_howto_table
[0]));
206 if (elf_mn10200_howto_table
[i
].name
!= NULL
207 && strcasecmp (elf_mn10200_howto_table
[i
].name
, r_name
) == 0)
208 return &elf_mn10200_howto_table
[i
];
213 /* Set the howto pointer for an MN10200 ELF reloc. */
216 mn10200_info_to_howto (bfd
*abfd
,
218 Elf_Internal_Rela
*dst
)
222 r_type
= ELF32_R_TYPE (dst
->r_info
);
223 if (r_type
>= (unsigned int) R_MN10200_MAX
)
225 /* xgettext:c-format */
226 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
228 bfd_set_error (bfd_error_bad_value
);
232 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
233 return cache_ptr
->howto
!= NULL
;
236 /* Perform a relocation as part of a final link. */
238 static bfd_reloc_status_type
239 mn10200_elf_final_link_relocate (reloc_howto_type
*howto
,
241 bfd
*output_bfd ATTRIBUTE_UNUSED
,
242 asection
*input_section
,
247 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
248 asection
*sym_sec ATTRIBUTE_UNUSED
,
249 int is_local ATTRIBUTE_UNUSED
)
251 unsigned long r_type
= howto
->type
;
252 bfd_byte
*hit_data
= contents
+ offset
;
262 bfd_put_32 (input_bfd
, value
, hit_data
);
268 if ((long) value
> 0x7fff || (long) value
< -0x8000)
269 return bfd_reloc_overflow
;
271 bfd_put_16 (input_bfd
, value
, hit_data
);
277 if ((long) value
> 0x7f || (long) value
< -0x80)
278 return bfd_reloc_overflow
;
280 bfd_put_8 (input_bfd
, value
, hit_data
);
286 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
287 return bfd_reloc_overflow
;
290 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
291 bfd_put_32 (input_bfd
, value
, hit_data
);
294 case R_MN10200_PCREL8
:
295 value
-= (input_section
->output_section
->vma
296 + input_section
->output_offset
);
297 value
-= (offset
+ 1);
300 if ((long) value
> 0xff || (long) value
< -0x100)
301 return bfd_reloc_overflow
;
303 bfd_put_8 (input_bfd
, value
, hit_data
);
306 case R_MN10200_PCREL16
:
307 value
-= (input_section
->output_section
->vma
308 + input_section
->output_offset
);
309 value
-= (offset
+ 2);
312 if ((long) value
> 0xffff || (long) value
< -0x10000)
313 return bfd_reloc_overflow
;
315 bfd_put_16 (input_bfd
, value
, hit_data
);
318 case R_MN10200_PCREL24
:
319 value
-= (input_section
->output_section
->vma
320 + input_section
->output_offset
);
321 value
-= (offset
+ 3);
324 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
325 return bfd_reloc_overflow
;
328 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
329 bfd_put_32 (input_bfd
, value
, hit_data
);
333 return bfd_reloc_notsupported
;
337 /* Relocate an MN10200 ELF section. */
339 mn10200_elf_relocate_section (bfd
*output_bfd
,
340 struct bfd_link_info
*info
,
342 asection
*input_section
,
344 Elf_Internal_Rela
*relocs
,
345 Elf_Internal_Sym
*local_syms
,
346 asection
**local_sections
)
348 Elf_Internal_Shdr
*symtab_hdr
;
349 struct elf_link_hash_entry
**sym_hashes
;
350 Elf_Internal_Rela
*rel
, *relend
;
352 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
353 sym_hashes
= elf_sym_hashes (input_bfd
);
356 relend
= relocs
+ input_section
->reloc_count
;
357 for (; rel
< relend
; rel
++)
360 reloc_howto_type
*howto
;
361 unsigned long r_symndx
;
362 Elf_Internal_Sym
*sym
;
364 struct elf_link_hash_entry
*h
;
366 bfd_reloc_status_type r
;
368 r_symndx
= ELF32_R_SYM (rel
->r_info
);
369 r_type
= ELF32_R_TYPE (rel
->r_info
);
370 howto
= elf_mn10200_howto_table
+ r_type
;
375 if (r_symndx
< symtab_hdr
->sh_info
)
377 sym
= local_syms
+ r_symndx
;
378 sec
= local_sections
[r_symndx
];
379 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
383 bool unresolved_reloc
, warned
, ignored
;
385 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
386 r_symndx
, symtab_hdr
, sym_hashes
,
388 unresolved_reloc
, warned
, ignored
);
391 if (sec
!= NULL
&& discarded_section (sec
))
392 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
393 rel
, 1, relend
, howto
, 0, contents
);
395 if (bfd_link_relocatable (info
))
398 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
400 contents
, rel
->r_offset
,
401 relocation
, rel
->r_addend
,
402 info
, sec
, h
== NULL
);
404 if (r
!= bfd_reloc_ok
)
407 const char *msg
= (const char *) 0;
410 name
= h
->root
.root
.string
;
413 name
= (bfd_elf_string_from_elf_section
414 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
415 if (name
== NULL
|| *name
== '\0')
416 name
= bfd_section_name (sec
);
421 case bfd_reloc_overflow
:
422 (*info
->callbacks
->reloc_overflow
)
423 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
424 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
427 case bfd_reloc_undefined
:
428 (*info
->callbacks
->undefined_symbol
) (info
, name
, input_bfd
,
430 rel
->r_offset
, true);
433 case bfd_reloc_outofrange
:
434 msg
= _("internal error: out of range error");
437 case bfd_reloc_notsupported
:
438 msg
= _("internal error: unsupported relocation error");
441 case bfd_reloc_dangerous
:
442 msg
= _("internal error: dangerous error");
446 msg
= _("internal error: unknown error");
450 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
451 input_section
, rel
->r_offset
);
460 /* Delete some bytes from a section while relaxing. */
463 mn10200_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
,
464 bfd_vma addr
, int count
)
466 Elf_Internal_Shdr
*symtab_hdr
;
467 unsigned int sec_shndx
;
469 Elf_Internal_Rela
*irel
, *irelend
;
471 Elf_Internal_Sym
*isym
;
472 Elf_Internal_Sym
*isymend
;
473 struct elf_link_hash_entry
**sym_hashes
;
474 struct elf_link_hash_entry
**end_hashes
;
475 unsigned int symcount
;
477 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
479 contents
= elf_section_data (sec
)->this_hdr
.contents
;
483 irel
= elf_section_data (sec
)->relocs
;
484 irelend
= irel
+ sec
->reloc_count
;
486 /* Actually delete the bytes. */
487 memmove (contents
+ addr
, contents
+ addr
+ count
,
488 (size_t) (toaddr
- addr
- count
));
491 /* Adjust all the relocs. */
492 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
494 /* Get the new reloc address. */
495 if ((irel
->r_offset
> addr
496 && irel
->r_offset
< toaddr
))
497 irel
->r_offset
-= count
;
500 /* Adjust the local symbols defined in this section. */
501 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
502 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
503 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
505 if (isym
->st_shndx
== sec_shndx
506 && isym
->st_value
> addr
507 && isym
->st_value
< toaddr
)
508 isym
->st_value
-= count
;
511 /* Now adjust the global symbols defined in this section. */
512 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
513 - symtab_hdr
->sh_info
);
514 sym_hashes
= elf_sym_hashes (abfd
);
515 end_hashes
= sym_hashes
+ symcount
;
516 for (; sym_hashes
< end_hashes
; sym_hashes
++)
518 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
519 if ((sym_hash
->root
.type
== bfd_link_hash_defined
520 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
521 && sym_hash
->root
.u
.def
.section
== sec
522 && sym_hash
->root
.u
.def
.value
> addr
523 && sym_hash
->root
.u
.def
.value
< toaddr
)
525 sym_hash
->root
.u
.def
.value
-= count
;
532 /* This function handles relaxing for the mn10200.
534 There are quite a few relaxing opportunities available on the mn10200:
536 * jsr:24 -> jsr:16 2 bytes
538 * jmp:24 -> jmp:16 2 bytes
539 * jmp:16 -> bra:8 1 byte
541 * If the previous instruction is a conditional branch
542 around the jump/bra, we may be able to reverse its condition
543 and change its target to the jump's target. The jump/bra
544 can then be deleted. 2 bytes
546 * mov abs24 -> mov abs16 2 byte savings
548 * Most instructions which accept imm24 can relax to imm16 2 bytes
549 - Most instructions which accept imm16 can relax to imm8 1 byte
551 * Most instructions which accept d24 can relax to d16 2 bytes
552 - Most instructions which accept d16 can relax to d8 1 byte
554 abs24, imm24, d24 all look the same at the reloc level. It
555 might make the code simpler if we had different relocs for
556 the various relaxable operand types.
558 We don't handle imm16->imm8 or d16->d8 as they're very rare
559 and somewhat more difficult to support. */
562 mn10200_elf_relax_section (bfd
*abfd
,
564 struct bfd_link_info
*link_info
,
567 Elf_Internal_Shdr
*symtab_hdr
;
568 Elf_Internal_Rela
*internal_relocs
;
569 Elf_Internal_Rela
*irel
, *irelend
;
570 bfd_byte
*contents
= NULL
;
571 Elf_Internal_Sym
*isymbuf
= NULL
;
573 /* Assume nothing changes. */
576 /* We don't have to do anything for a relocatable link, if
577 this section does not have relocs, or if this is not a
579 if (bfd_link_relocatable (link_info
)
580 || sec
->reloc_count
== 0
581 || (sec
->flags
& SEC_RELOC
) == 0
582 || (sec
->flags
& SEC_HAS_CONTENTS
) == 0
583 || (sec
->flags
& SEC_CODE
) == 0)
586 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
588 /* Get a copy of the native relocations. */
589 internal_relocs
= (_bfd_elf_link_read_relocs
590 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
591 link_info
->keep_memory
));
592 if (internal_relocs
== NULL
)
595 /* Walk through them looking for relaxing opportunities. */
596 irelend
= internal_relocs
+ sec
->reloc_count
;
597 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
601 /* If this isn't something that can be relaxed, then ignore
603 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
604 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
605 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
608 /* Get the section contents if we haven't done so already. */
609 if (contents
== NULL
)
611 /* Get cached copy if it exists. */
612 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
613 contents
= elf_section_data (sec
)->this_hdr
.contents
;
616 /* Go get them off disk. */
617 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
622 /* Read this BFD's local symbols if we haven't done so already. */
623 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
625 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
627 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
628 symtab_hdr
->sh_info
, 0,
634 /* Get the value of the symbol referred to by the reloc. */
635 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
637 /* A local symbol. */
638 Elf_Internal_Sym
*isym
;
641 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
642 if (isym
->st_shndx
== SHN_UNDEF
)
643 sym_sec
= bfd_und_section_ptr
;
644 else if (isym
->st_shndx
== SHN_ABS
)
645 sym_sec
= bfd_abs_section_ptr
;
646 else if (isym
->st_shndx
== SHN_COMMON
)
647 sym_sec
= bfd_com_section_ptr
;
649 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
650 symval
= (isym
->st_value
651 + sym_sec
->output_section
->vma
652 + sym_sec
->output_offset
);
657 struct elf_link_hash_entry
*h
;
659 /* An external symbol. */
660 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
661 h
= elf_sym_hashes (abfd
)[indx
];
662 BFD_ASSERT (h
!= NULL
);
663 if (h
->root
.type
!= bfd_link_hash_defined
664 && h
->root
.type
!= bfd_link_hash_defweak
)
666 /* This appears to be a reference to an undefined
667 symbol. Just ignore it--it will be caught by the
668 regular reloc processing. */
672 symval
= (h
->root
.u
.def
.value
673 + h
->root
.u
.def
.section
->output_section
->vma
674 + h
->root
.u
.def
.section
->output_offset
);
677 /* For simplicity of coding, we are going to modify the section
678 contents, the section relocs, and the BFD symbol table. We
679 must tell the rest of the code not to free up this
680 information. It would be possible to instead create a table
681 of changes which have to be made, as is done in coff-mips.c;
682 that would be more work, but would require less memory when
683 the linker is run. */
685 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
687 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
689 bfd_vma value
= symval
;
691 /* Deal with pc-relative gunk. */
692 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
693 value
-= (irel
->r_offset
+ 3);
694 value
+= irel
->r_addend
;
696 /* See if the value will fit in 16 bits, note the high value is
697 0x7fff + 2 as the target will be two bytes closer if we are
699 if ((long) value
< 0x8001 && (long) value
> -0x8000)
703 /* Get the opcode. */
704 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
706 if (code
!= 0xe0 && code
!= 0xe1)
709 /* Note that we've changed the relocs, section contents, etc. */
710 elf_section_data (sec
)->relocs
= internal_relocs
;
711 elf_section_data (sec
)->this_hdr
.contents
= contents
;
712 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
714 /* Fix the opcode. */
716 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
717 else if (code
== 0xe1)
718 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
720 /* Fix the relocation's type. */
721 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
724 /* The opcode got shorter too, so we have to fix the offset. */
727 /* Delete two bytes of data. */
728 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
729 irel
->r_offset
+ 1, 2))
732 /* That will change things, so, we should relax again.
733 Note that this is not required, and it may be slow. */
738 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
740 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
742 bfd_vma value
= symval
;
744 /* Deal with pc-relative gunk. */
745 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
746 value
-= (irel
->r_offset
+ 2);
747 value
+= irel
->r_addend
;
749 /* See if the value will fit in 8 bits, note the high value is
750 0x7f + 1 as the target will be one bytes closer if we are
752 if ((long) value
< 0x80 && (long) value
> -0x80)
756 /* Get the opcode. */
757 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
762 /* Note that we've changed the relocs, section contents, etc. */
763 elf_section_data (sec
)->relocs
= internal_relocs
;
764 elf_section_data (sec
)->this_hdr
.contents
= contents
;
765 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
767 /* Fix the opcode. */
768 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
770 /* Fix the relocation's type. */
771 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
774 /* Delete one byte of data. */
775 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
776 irel
->r_offset
+ 1, 1))
779 /* That will change things, so, we should relax again.
780 Note that this is not required, and it may be slow. */
785 /* Try to eliminate an unconditional 8 bit pc-relative branch
786 which immediately follows a conditional 8 bit pc-relative
787 branch around the unconditional branch.
794 This happens when the bCC can't reach lab2 at assembly time,
795 but due to other relaxations it can reach at link time. */
796 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
798 Elf_Internal_Rela
*nrel
;
801 /* Do nothing if this reloc is the last byte in the section. */
802 if (irel
->r_offset
== sec
->size
)
805 /* See if the next instruction is an unconditional pc-relative
806 branch, more often than not this test will fail, so we
807 test it first to speed things up. */
808 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
812 /* Also make sure the next relocation applies to the next
813 instruction and that it's a pc-relative 8 bit branch. */
816 || irel
->r_offset
+ 2 != nrel
->r_offset
817 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
820 /* Make sure our destination immediately follows the
821 unconditional branch. */
822 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
823 + irel
->r_offset
+ 3))
826 /* Now make sure we are a conditional branch. This may not
827 be necessary, but why take the chance.
829 Note these checks assume that R_MN10200_PCREL8 relocs
830 only occur on bCC and bCCx insns. If they occured
831 elsewhere, we'd need to know the start of this insn
832 for this check to be accurate. */
833 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
834 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
835 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
836 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
837 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
838 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
839 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
842 /* We also have to be sure there is no symbol/label
843 at the unconditional branch. */
844 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
848 /* Note that we've changed the relocs, section contents, etc. */
849 elf_section_data (sec
)->relocs
= internal_relocs
;
850 elf_section_data (sec
)->this_hdr
.contents
= contents
;
851 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
853 /* Reverse the condition of the first branch. */
911 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
913 /* Set the reloc type and symbol for the first branch
914 from the second branch. */
915 irel
->r_info
= nrel
->r_info
;
917 /* Make the reloc for the second branch a null reloc. */
918 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
921 /* Delete two bytes of data. */
922 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
923 irel
->r_offset
+ 1, 2))
926 /* That will change things, so, we should relax again.
927 Note that this is not required, and it may be slow. */
931 /* Try to turn a 24bit immediate, displacement or absolute address
932 into a 16bit immediate, displacement or absolute address. */
933 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
935 bfd_vma value
= symval
;
937 /* See if the value will fit in 16 bits.
938 We allow any 16bit match here. We prune those we can't
940 if ((long) value
< 0x7fff && (long) value
> -0x8000)
944 /* All insns which have 24bit operands are 5 bytes long,
945 the first byte will always be 0xf4, but we double check
948 /* Get the first opcode. */
949 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
954 /* Get the second opcode. */
955 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
959 /* mov imm24,dn -> mov imm16,dn */
961 /* Not safe if the high bit is on as relaxing may
962 move the value out of high mem and thus not fit
963 in a signed 16bit value. */
967 /* Note that we've changed the relocation contents, etc. */
968 elf_section_data (sec
)->relocs
= internal_relocs
;
969 elf_section_data (sec
)->this_hdr
.contents
= contents
;
970 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
972 /* Fix the opcode. */
973 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
974 contents
+ irel
->r_offset
- 2);
976 /* Fix the relocation's type. */
977 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
980 /* The opcode got shorter too, so we have to fix the
984 /* Delete two bytes of data. */
985 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
986 irel
->r_offset
+ 1, 2))
989 /* That will change things, so, we should relax again.
990 Note that this is not required, and it may be slow. */
994 /* mov imm24,an -> mov imm16,an
995 cmp imm24,an -> cmp imm16,an
996 mov (abs24),dn -> mov (abs16),dn
997 mov dn,(abs24) -> mov dn,(abs16)
998 movb dn,(abs24) -> movb dn,(abs16)
999 movbu (abs24),dn -> movbu (abs16),dn */
1006 /* Note that we've changed the relocation contents, etc. */
1007 elf_section_data (sec
)->relocs
= internal_relocs
;
1008 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1009 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1011 if ((code
& 0xfc) == 0x74)
1012 code
= 0xdc + (code
& 0x03);
1013 else if ((code
& 0xfc) == 0x7c)
1014 code
= 0xec + (code
& 0x03);
1015 else if ((code
& 0xfc) == 0xc0)
1016 code
= 0xc8 + (code
& 0x03);
1017 else if ((code
& 0xfc) == 0x40)
1018 code
= 0xc0 + (code
& 0x03);
1019 else if ((code
& 0xfc) == 0x44)
1020 code
= 0xc4 + (code
& 0x03);
1021 else if ((code
& 0xfc) == 0xc8)
1022 code
= 0xcc + (code
& 0x03);
1024 /* Fix the opcode. */
1025 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1027 /* Fix the relocation's type. */
1028 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1031 /* The opcode got shorter too, so we have to fix the
1033 irel
->r_offset
-= 1;
1035 /* Delete two bytes of data. */
1036 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1037 irel
->r_offset
+ 1, 2))
1040 /* That will change things, so, we should relax again.
1041 Note that this is not required, and it may be slow. */
1045 /* cmp imm24,dn -> cmp imm16,dn
1046 mov (abs24),an -> mov (abs16),an
1047 mov an,(abs24) -> mov an,(abs16)
1048 add imm24,dn -> add imm16,dn
1049 add imm24,an -> add imm16,an
1050 sub imm24,dn -> sub imm16,dn
1051 sub imm24,an -> sub imm16,an
1052 And all d24->d16 in memory ops. */
1069 /* Not safe if the high bit is on as relaxing may
1070 move the value out of high mem and thus not fit
1071 in a signed 16bit value. */
1072 if (((code
& 0xfc) == 0x78
1073 || (code
& 0xfc) == 0x60
1074 || (code
& 0xfc) == 0x64
1075 || (code
& 0xfc) == 0x68
1076 || (code
& 0xfc) == 0x6c
1077 || (code
& 0xfc) == 0x80
1078 || (code
& 0xfc) == 0xf0
1079 || (code
& 0xfc) == 0x00
1080 || (code
& 0xfc) == 0x10
1081 || (code
& 0xfc) == 0xb0
1082 || (code
& 0xfc) == 0x30
1083 || (code
& 0xfc) == 0xa0
1084 || (code
& 0xfc) == 0x20
1085 || (code
& 0xfc) == 0x90)
1086 && (value
& 0x8000) != 0)
1089 /* Note that we've changed the relocation contents, etc. */
1090 elf_section_data (sec
)->relocs
= internal_relocs
;
1091 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1092 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1094 /* Fix the opcode. */
1095 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1097 if ((code
& 0xfc) == 0x78)
1098 code
= 0x48 + (code
& 0x03);
1099 else if ((code
& 0xfc) == 0xd0)
1100 code
= 0x30 + (code
& 0x03);
1101 else if ((code
& 0xfc) == 0x50)
1102 code
= 0x20 + (code
& 0x03);
1103 else if ((code
& 0xfc) == 0x60)
1104 code
= 0x18 + (code
& 0x03);
1105 else if ((code
& 0xfc) == 0x64)
1106 code
= 0x08 + (code
& 0x03);
1107 else if ((code
& 0xfc) == 0x68)
1108 code
= 0x1c + (code
& 0x03);
1109 else if ((code
& 0xfc) == 0x6c)
1110 code
= 0x0c + (code
& 0x03);
1111 else if ((code
& 0xfc) == 0x80)
1112 code
= 0xc0 + (code
& 0x07);
1113 else if ((code
& 0xfc) == 0xf0)
1114 code
= 0xb0 + (code
& 0x07);
1115 else if ((code
& 0xfc) == 0x00)
1116 code
= 0x80 + (code
& 0x07);
1117 else if ((code
& 0xfc) == 0x10)
1118 code
= 0xa0 + (code
& 0x07);
1119 else if ((code
& 0xfc) == 0xb0)
1120 code
= 0x70 + (code
& 0x07);
1121 else if ((code
& 0xfc) == 0x30)
1122 code
= 0x60 + (code
& 0x07);
1123 else if ((code
& 0xfc) == 0xa0)
1124 code
= 0xd0 + (code
& 0x07);
1125 else if ((code
& 0xfc) == 0x20)
1126 code
= 0x90 + (code
& 0x07);
1127 else if ((code
& 0xfc) == 0x90)
1128 code
= 0x50 + (code
& 0x07);
1130 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1132 /* Fix the relocation's type. */
1133 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1136 /* Delete one bytes of data. */
1137 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1138 irel
->r_offset
+ 2, 1))
1141 /* That will change things, so, we should relax again.
1142 Note that this is not required, and it may be slow. */
1146 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1148 /* Note that we've changed the reldection contents, etc. */
1149 elf_section_data (sec
)->relocs
= internal_relocs
;
1150 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1151 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1153 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1154 contents
+ irel
->r_offset
- 2);
1156 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1157 contents
+ irel
->r_offset
- 1);
1159 /* Fix the relocation's type. */
1160 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1163 /* The reloc will be applied one byte in front of its
1164 current location. */
1165 irel
->r_offset
-= 1;
1167 /* Delete one bytes of data. */
1168 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1169 irel
->r_offset
+ 2, 1))
1172 /* That will change things, so, we should relax again.
1173 Note that this is not required, and it may be slow. */
1182 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1184 if (! link_info
->keep_memory
)
1188 /* Cache the symbols for elf_link_input_bfd. */
1189 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1193 if (contents
!= NULL
1194 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1196 if (! link_info
->keep_memory
)
1200 /* Cache the section contents for elf_link_input_bfd. */
1201 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1205 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
1206 free (internal_relocs
);
1211 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1213 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1215 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
1216 free (internal_relocs
);
1221 /* Return TRUE if a symbol exists at the given address, else return
1224 mn10200_elf_symbol_address_p (bfd
*abfd
,
1226 Elf_Internal_Sym
*isym
,
1229 Elf_Internal_Shdr
*symtab_hdr
;
1230 unsigned int sec_shndx
;
1231 Elf_Internal_Sym
*isymend
;
1232 struct elf_link_hash_entry
**sym_hashes
;
1233 struct elf_link_hash_entry
**end_hashes
;
1234 unsigned int symcount
;
1236 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1238 /* Examine all the local symbols. */
1239 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1240 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1242 if (isym
->st_shndx
== sec_shndx
1243 && isym
->st_value
== addr
)
1247 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1248 - symtab_hdr
->sh_info
);
1249 sym_hashes
= elf_sym_hashes (abfd
);
1250 end_hashes
= sym_hashes
+ symcount
;
1251 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1253 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1254 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1255 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1256 && sym_hash
->root
.u
.def
.section
== sec
1257 && sym_hash
->root
.u
.def
.value
== addr
)
1264 /* This is a version of bfd_generic_get_relocated_section_contents
1265 which uses mn10200_elf_relocate_section. */
1268 mn10200_elf_get_relocated_section_contents (bfd
*output_bfd
,
1269 struct bfd_link_info
*link_info
,
1270 struct bfd_link_order
*link_order
,
1275 Elf_Internal_Shdr
*symtab_hdr
;
1276 asection
*input_section
= link_order
->u
.indirect
.section
;
1277 bfd
*input_bfd
= input_section
->owner
;
1278 asection
**sections
= NULL
;
1279 Elf_Internal_Rela
*internal_relocs
= NULL
;
1280 Elf_Internal_Sym
*isymbuf
= NULL
;
1282 /* We only need to handle the case of relaxing, or of having a
1283 particular set of section contents, specially. */
1285 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1286 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1291 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1293 bfd_byte
*orig_data
= data
;
1296 data
= bfd_malloc (input_section
->size
);
1300 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1301 (size_t) input_section
->size
);
1303 if ((input_section
->flags
& SEC_RELOC
) != 0
1304 && input_section
->reloc_count
> 0)
1306 Elf_Internal_Sym
*isym
;
1307 Elf_Internal_Sym
*isymend
;
1311 internal_relocs
= (_bfd_elf_link_read_relocs
1312 (input_bfd
, input_section
, NULL
,
1313 (Elf_Internal_Rela
*) NULL
, false));
1314 if (internal_relocs
== NULL
)
1317 if (symtab_hdr
->sh_info
!= 0)
1319 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1320 if (isymbuf
== NULL
)
1321 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1322 symtab_hdr
->sh_info
, 0,
1324 if (isymbuf
== NULL
)
1328 amt
= symtab_hdr
->sh_info
;
1329 amt
*= sizeof (asection
*);
1330 sections
= (asection
**) bfd_malloc (amt
);
1331 if (sections
== NULL
&& amt
!= 0)
1334 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1335 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1339 if (isym
->st_shndx
== SHN_UNDEF
)
1340 isec
= bfd_und_section_ptr
;
1341 else if (isym
->st_shndx
== SHN_ABS
)
1342 isec
= bfd_abs_section_ptr
;
1343 else if (isym
->st_shndx
== SHN_COMMON
)
1344 isec
= bfd_com_section_ptr
;
1346 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1351 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1352 input_section
, data
, internal_relocs
,
1357 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1359 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1360 free (internal_relocs
);
1367 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1369 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1370 free (internal_relocs
);
1371 if (orig_data
== NULL
)
1376 #define TARGET_LITTLE_SYM mn10200_elf32_vec
1377 #define TARGET_LITTLE_NAME "elf32-mn10200"
1378 #define ELF_ARCH bfd_arch_mn10200
1379 #define ELF_MACHINE_CODE EM_MN10200
1380 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1381 #define ELF_MAXPAGESIZE 0x1000
1383 #define elf_backend_rela_normal 1
1384 #define elf_info_to_howto mn10200_info_to_howto
1385 #define elf_info_to_howto_rel NULL
1386 #define elf_backend_relocate_section mn10200_elf_relocate_section
1387 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1388 #define bfd_elf32_bfd_get_relocated_section_contents \
1389 mn10200_elf_get_relocated_section_contents
1391 #define elf_symbol_leading_char '_'
1393 #include "elf32-target.h"