1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 mn10200_elf_relax_delete_bytes (bfd
*, asection
*, bfd_vma
, int);
31 mn10200_elf_symbol_address_p (bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
);
46 static reloc_howto_type elf_mn10200_howto_table
[] =
48 /* Dummy relocation. Does nothing. */
49 HOWTO (R_MN10200_NONE
,
55 complain_overflow_bitfield
,
56 bfd_elf_generic_reloc
,
62 /* Standard 32 bit reloc. */
69 complain_overflow_bitfield
,
70 bfd_elf_generic_reloc
,
76 /* Standard 16 bit reloc. */
83 complain_overflow_bitfield
,
84 bfd_elf_generic_reloc
,
90 /* Standard 8 bit reloc. */
97 complain_overflow_bitfield
,
98 bfd_elf_generic_reloc
,
104 /* Standard 24 bit reloc. */
111 complain_overflow_bitfield
,
112 bfd_elf_generic_reloc
,
118 /* Simple 8 pc-relative reloc. */
119 HOWTO (R_MN10200_PCREL8
,
125 complain_overflow_bitfield
,
126 bfd_elf_generic_reloc
,
132 /* Simple 16 pc-relative reloc. */
133 HOWTO (R_MN10200_PCREL16
,
139 complain_overflow_bitfield
,
140 bfd_elf_generic_reloc
,
146 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
147 to get the pc-relative offset correct. */
148 HOWTO (R_MN10200_PCREL24
,
154 complain_overflow_bitfield
,
155 bfd_elf_generic_reloc
,
163 struct mn10200_reloc_map
165 bfd_reloc_code_real_type bfd_reloc_val
;
166 unsigned char elf_reloc_val
;
169 static const struct mn10200_reloc_map mn10200_reloc_map
[] =
171 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
172 { BFD_RELOC_32
, R_MN10200_32
, },
173 { BFD_RELOC_16
, R_MN10200_16
, },
174 { BFD_RELOC_8
, R_MN10200_8
, },
175 { BFD_RELOC_24
, R_MN10200_24
, },
176 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
177 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
178 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
181 static reloc_howto_type
*
182 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
183 bfd_reloc_code_real_type code
)
188 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
191 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
192 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
198 static reloc_howto_type
*
199 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
205 i
< (sizeof (elf_mn10200_howto_table
)
206 / sizeof (elf_mn10200_howto_table
[0]));
208 if (elf_mn10200_howto_table
[i
].name
!= NULL
209 && strcasecmp (elf_mn10200_howto_table
[i
].name
, r_name
) == 0)
210 return &elf_mn10200_howto_table
[i
];
215 /* Set the howto pointer for an MN10200 ELF reloc. */
218 mn10200_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
,
220 Elf_Internal_Rela
*dst
)
224 r_type
= ELF32_R_TYPE (dst
->r_info
);
225 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
226 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
229 /* Perform a relocation as part of a final link. */
231 static bfd_reloc_status_type
232 mn10200_elf_final_link_relocate (reloc_howto_type
*howto
,
234 bfd
*output_bfd ATTRIBUTE_UNUSED
,
235 asection
*input_section
,
240 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
241 asection
*sym_sec ATTRIBUTE_UNUSED
,
242 int is_local ATTRIBUTE_UNUSED
)
244 unsigned long r_type
= howto
->type
;
245 bfd_byte
*hit_data
= contents
+ offset
;
255 bfd_put_32 (input_bfd
, value
, hit_data
);
261 if ((long) value
> 0x7fff || (long) value
< -0x8000)
262 return bfd_reloc_overflow
;
264 bfd_put_16 (input_bfd
, value
, hit_data
);
270 if ((long) value
> 0x7f || (long) value
< -0x80)
271 return bfd_reloc_overflow
;
273 bfd_put_8 (input_bfd
, value
, hit_data
);
279 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
280 return bfd_reloc_overflow
;
283 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
284 bfd_put_32 (input_bfd
, value
, hit_data
);
287 case R_MN10200_PCREL8
:
288 value
-= (input_section
->output_section
->vma
289 + input_section
->output_offset
);
290 value
-= (offset
+ 1);
293 if ((long) value
> 0xff || (long) value
< -0x100)
294 return bfd_reloc_overflow
;
296 bfd_put_8 (input_bfd
, value
, hit_data
);
299 case R_MN10200_PCREL16
:
300 value
-= (input_section
->output_section
->vma
301 + input_section
->output_offset
);
302 value
-= (offset
+ 2);
305 if ((long) value
> 0xffff || (long) value
< -0x10000)
306 return bfd_reloc_overflow
;
308 bfd_put_16 (input_bfd
, value
, hit_data
);
311 case R_MN10200_PCREL24
:
312 value
-= (input_section
->output_section
->vma
313 + input_section
->output_offset
);
314 value
-= (offset
+ 3);
317 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
318 return bfd_reloc_overflow
;
321 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
322 bfd_put_32 (input_bfd
, value
, hit_data
);
326 return bfd_reloc_notsupported
;
330 /* Relocate an MN10200 ELF section. */
332 mn10200_elf_relocate_section (bfd
*output_bfd
,
333 struct bfd_link_info
*info
,
335 asection
*input_section
,
337 Elf_Internal_Rela
*relocs
,
338 Elf_Internal_Sym
*local_syms
,
339 asection
**local_sections
)
341 Elf_Internal_Shdr
*symtab_hdr
;
342 struct elf_link_hash_entry
**sym_hashes
;
343 Elf_Internal_Rela
*rel
, *relend
;
345 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
346 sym_hashes
= elf_sym_hashes (input_bfd
);
349 relend
= relocs
+ input_section
->reloc_count
;
350 for (; rel
< relend
; rel
++)
353 reloc_howto_type
*howto
;
354 unsigned long r_symndx
;
355 Elf_Internal_Sym
*sym
;
357 struct elf_link_hash_entry
*h
;
359 bfd_reloc_status_type r
;
361 r_symndx
= ELF32_R_SYM (rel
->r_info
);
362 r_type
= ELF32_R_TYPE (rel
->r_info
);
363 howto
= elf_mn10200_howto_table
+ r_type
;
368 if (r_symndx
< symtab_hdr
->sh_info
)
370 sym
= local_syms
+ r_symndx
;
371 sec
= local_sections
[r_symndx
];
372 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
376 bfd_boolean unresolved_reloc
, warned
;
378 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
379 r_symndx
, symtab_hdr
, sym_hashes
,
381 unresolved_reloc
, warned
);
384 if (sec
!= NULL
&& discarded_section (sec
))
385 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
386 rel
, 1, relend
, howto
, 0, contents
);
388 if (info
->relocatable
)
391 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
393 contents
, rel
->r_offset
,
394 relocation
, rel
->r_addend
,
395 info
, sec
, h
== NULL
);
397 if (r
!= bfd_reloc_ok
)
400 const char *msg
= (const char *) 0;
403 name
= h
->root
.root
.string
;
406 name
= (bfd_elf_string_from_elf_section
407 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
408 if (name
== NULL
|| *name
== '\0')
409 name
= bfd_section_name (input_bfd
, sec
);
414 case bfd_reloc_overflow
:
415 if (! ((*info
->callbacks
->reloc_overflow
)
416 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
417 (bfd_vma
) 0, input_bfd
, input_section
,
422 case bfd_reloc_undefined
:
423 if (! ((*info
->callbacks
->undefined_symbol
)
424 (info
, name
, input_bfd
, input_section
,
425 rel
->r_offset
, TRUE
)))
429 case bfd_reloc_outofrange
:
430 msg
= _("internal error: out of range error");
433 case bfd_reloc_notsupported
:
434 msg
= _("internal error: unsupported relocation error");
437 case bfd_reloc_dangerous
:
438 msg
= _("internal error: dangerous error");
442 msg
= _("internal error: unknown error");
446 if (!((*info
->callbacks
->warning
)
447 (info
, msg
, name
, input_bfd
, input_section
,
458 /* Delete some bytes from a section while relaxing. */
461 mn10200_elf_relax_delete_bytes (bfd
*abfd
, asection
*sec
,
462 bfd_vma addr
, int count
)
464 Elf_Internal_Shdr
*symtab_hdr
;
465 unsigned int sec_shndx
;
467 Elf_Internal_Rela
*irel
, *irelend
;
469 Elf_Internal_Sym
*isym
;
470 Elf_Internal_Sym
*isymend
;
471 struct elf_link_hash_entry
**sym_hashes
;
472 struct elf_link_hash_entry
**end_hashes
;
473 unsigned int symcount
;
475 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
477 contents
= elf_section_data (sec
)->this_hdr
.contents
;
481 irel
= elf_section_data (sec
)->relocs
;
482 irelend
= irel
+ sec
->reloc_count
;
484 /* Actually delete the bytes. */
485 memmove (contents
+ addr
, contents
+ addr
+ count
,
486 (size_t) (toaddr
- addr
- count
));
489 /* Adjust all the relocs. */
490 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
492 /* Get the new reloc address. */
493 if ((irel
->r_offset
> addr
494 && irel
->r_offset
< toaddr
))
495 irel
->r_offset
-= count
;
498 /* Adjust the local symbols defined in this section. */
499 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
500 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
501 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
503 if (isym
->st_shndx
== sec_shndx
504 && isym
->st_value
> addr
505 && isym
->st_value
< toaddr
)
506 isym
->st_value
-= count
;
509 /* Now adjust the global symbols defined in this section. */
510 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
511 - symtab_hdr
->sh_info
);
512 sym_hashes
= elf_sym_hashes (abfd
);
513 end_hashes
= sym_hashes
+ symcount
;
514 for (; sym_hashes
< end_hashes
; sym_hashes
++)
516 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
517 if ((sym_hash
->root
.type
== bfd_link_hash_defined
518 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
519 && sym_hash
->root
.u
.def
.section
== sec
520 && sym_hash
->root
.u
.def
.value
> addr
521 && sym_hash
->root
.u
.def
.value
< toaddr
)
523 sym_hash
->root
.u
.def
.value
-= count
;
530 /* This function handles relaxing for the mn10200.
532 There are quite a few relaxing opportunities available on the mn10200:
534 * jsr:24 -> jsr:16 2 bytes
536 * jmp:24 -> jmp:16 2 bytes
537 * jmp:16 -> bra:8 1 byte
539 * If the previous instruction is a conditional branch
540 around the jump/bra, we may be able to reverse its condition
541 and change its target to the jump's target. The jump/bra
542 can then be deleted. 2 bytes
544 * mov abs24 -> mov abs16 2 byte savings
546 * Most instructions which accept imm24 can relax to imm16 2 bytes
547 - Most instructions which accept imm16 can relax to imm8 1 byte
549 * Most instructions which accept d24 can relax to d16 2 bytes
550 - Most instructions which accept d16 can relax to d8 1 byte
552 abs24, imm24, d24 all look the same at the reloc level. It
553 might make the code simpler if we had different relocs for
554 the various relaxable operand types.
556 We don't handle imm16->imm8 or d16->d8 as they're very rare
557 and somewhat more difficult to support. */
560 mn10200_elf_relax_section (bfd
*abfd
,
562 struct bfd_link_info
*link_info
,
565 Elf_Internal_Shdr
*symtab_hdr
;
566 Elf_Internal_Rela
*internal_relocs
;
567 Elf_Internal_Rela
*irel
, *irelend
;
568 bfd_byte
*contents
= NULL
;
569 Elf_Internal_Sym
*isymbuf
= NULL
;
571 /* Assume nothing changes. */
574 /* We don't have to do anything for a relocatable link, if
575 this section does not have relocs, or if this is not a
577 if (link_info
->relocatable
578 || (sec
->flags
& SEC_RELOC
) == 0
579 || sec
->reloc_count
== 0
580 || (sec
->flags
& SEC_CODE
) == 0)
583 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
585 /* Get a copy of the native relocations. */
586 internal_relocs
= (_bfd_elf_link_read_relocs
587 (abfd
, sec
, NULL
, (Elf_Internal_Rela
*) NULL
,
588 link_info
->keep_memory
));
589 if (internal_relocs
== NULL
)
592 /* Walk through them looking for relaxing opportunities. */
593 irelend
= internal_relocs
+ sec
->reloc_count
;
594 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
598 /* If this isn't something that can be relaxed, then ignore
600 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
601 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
602 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
605 /* Get the section contents if we haven't done so already. */
606 if (contents
== NULL
)
608 /* Get cached copy if it exists. */
609 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
610 contents
= elf_section_data (sec
)->this_hdr
.contents
;
613 /* Go get them off disk. */
614 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
619 /* Read this BFD's local symbols if we haven't done so already. */
620 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
622 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
624 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
625 symtab_hdr
->sh_info
, 0,
631 /* Get the value of the symbol referred to by the reloc. */
632 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
634 /* A local symbol. */
635 Elf_Internal_Sym
*isym
;
638 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
639 if (isym
->st_shndx
== SHN_UNDEF
)
640 sym_sec
= bfd_und_section_ptr
;
641 else if (isym
->st_shndx
== SHN_ABS
)
642 sym_sec
= bfd_abs_section_ptr
;
643 else if (isym
->st_shndx
== SHN_COMMON
)
644 sym_sec
= bfd_com_section_ptr
;
646 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
647 symval
= (isym
->st_value
648 + sym_sec
->output_section
->vma
649 + sym_sec
->output_offset
);
654 struct elf_link_hash_entry
*h
;
656 /* An external symbol. */
657 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
658 h
= elf_sym_hashes (abfd
)[indx
];
659 BFD_ASSERT (h
!= NULL
);
660 if (h
->root
.type
!= bfd_link_hash_defined
661 && h
->root
.type
!= bfd_link_hash_defweak
)
663 /* This appears to be a reference to an undefined
664 symbol. Just ignore it--it will be caught by the
665 regular reloc processing. */
669 symval
= (h
->root
.u
.def
.value
670 + h
->root
.u
.def
.section
->output_section
->vma
671 + h
->root
.u
.def
.section
->output_offset
);
674 /* For simplicity of coding, we are going to modify the section
675 contents, the section relocs, and the BFD symbol table. We
676 must tell the rest of the code not to free up this
677 information. It would be possible to instead create a table
678 of changes which have to be made, as is done in coff-mips.c;
679 that would be more work, but would require less memory when
680 the linker is run. */
682 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
684 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
686 bfd_vma value
= symval
;
688 /* Deal with pc-relative gunk. */
689 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
690 value
-= (irel
->r_offset
+ 3);
691 value
+= irel
->r_addend
;
693 /* See if the value will fit in 16 bits, note the high value is
694 0x7fff + 2 as the target will be two bytes closer if we are
696 if ((long) value
< 0x8001 && (long) value
> -0x8000)
700 /* Get the opcode. */
701 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
703 if (code
!= 0xe0 && code
!= 0xe1)
706 /* Note that we've changed the relocs, section contents, etc. */
707 elf_section_data (sec
)->relocs
= internal_relocs
;
708 elf_section_data (sec
)->this_hdr
.contents
= contents
;
709 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
711 /* Fix the opcode. */
713 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
714 else if (code
== 0xe1)
715 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
717 /* Fix the relocation's type. */
718 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
721 /* The opcode got shorter too, so we have to fix the offset. */
724 /* Delete two bytes of data. */
725 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
726 irel
->r_offset
+ 1, 2))
729 /* That will change things, so, we should relax again.
730 Note that this is not required, and it may be slow. */
735 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
737 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
739 bfd_vma value
= symval
;
741 /* Deal with pc-relative gunk. */
742 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
743 value
-= (irel
->r_offset
+ 2);
744 value
+= irel
->r_addend
;
746 /* See if the value will fit in 8 bits, note the high value is
747 0x7f + 1 as the target will be one bytes closer if we are
749 if ((long) value
< 0x80 && (long) value
> -0x80)
753 /* Get the opcode. */
754 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
759 /* Note that we've changed the relocs, section contents, etc. */
760 elf_section_data (sec
)->relocs
= internal_relocs
;
761 elf_section_data (sec
)->this_hdr
.contents
= contents
;
762 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
764 /* Fix the opcode. */
765 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
767 /* Fix the relocation's type. */
768 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
771 /* Delete one byte of data. */
772 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
773 irel
->r_offset
+ 1, 1))
776 /* That will change things, so, we should relax again.
777 Note that this is not required, and it may be slow. */
782 /* Try to eliminate an unconditional 8 bit pc-relative branch
783 which immediately follows a conditional 8 bit pc-relative
784 branch around the unconditional branch.
791 This happens when the bCC can't reach lab2 at assembly time,
792 but due to other relaxations it can reach at link time. */
793 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
795 Elf_Internal_Rela
*nrel
;
796 bfd_vma value
= symval
;
799 /* Deal with pc-relative gunk. */
800 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
801 value
-= (irel
->r_offset
+ 1);
802 value
+= irel
->r_addend
;
804 /* Do nothing if this reloc is the last byte in the section. */
805 if (irel
->r_offset
== sec
->size
)
808 /* See if the next instruction is an unconditional pc-relative
809 branch, more often than not this test will fail, so we
810 test it first to speed things up. */
811 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
815 /* Also make sure the next relocation applies to the next
816 instruction and that it's a pc-relative 8 bit branch. */
819 || irel
->r_offset
+ 2 != nrel
->r_offset
820 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
823 /* Make sure our destination immediately follows the
824 unconditional branch. */
825 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
826 + irel
->r_offset
+ 3))
829 /* Now make sure we are a conditional branch. This may not
830 be necessary, but why take the chance.
832 Note these checks assume that R_MN10200_PCREL8 relocs
833 only occur on bCC and bCCx insns. If they occured
834 elsewhere, we'd need to know the start of this insn
835 for this check to be accurate. */
836 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
837 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
838 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
839 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
840 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
841 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
842 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
845 /* We also have to be sure there is no symbol/label
846 at the unconditional branch. */
847 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
851 /* Note that we've changed the relocs, section contents, etc. */
852 elf_section_data (sec
)->relocs
= internal_relocs
;
853 elf_section_data (sec
)->this_hdr
.contents
= contents
;
854 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
856 /* Reverse the condition of the first branch. */
914 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
916 /* Set the reloc type and symbol for the first branch
917 from the second branch. */
918 irel
->r_info
= nrel
->r_info
;
920 /* Make the reloc for the second branch a null reloc. */
921 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
924 /* Delete two bytes of data. */
925 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
926 irel
->r_offset
+ 1, 2))
929 /* That will change things, so, we should relax again.
930 Note that this is not required, and it may be slow. */
934 /* Try to turn a 24bit immediate, displacement or absolute address
935 into a 16bit immediate, displacement or absolute address. */
936 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
938 bfd_vma value
= symval
;
940 /* See if the value will fit in 16 bits.
941 We allow any 16bit match here. We prune those we can't
943 if ((long) value
< 0x7fff && (long) value
> -0x8000)
947 /* All insns which have 24bit operands are 5 bytes long,
948 the first byte will always be 0xf4, but we double check
951 /* Get the first opcode. */
952 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
957 /* Get the second opcode. */
958 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
962 /* mov imm24,dn -> mov imm16,dn */
964 /* Not safe if the high bit is on as relaxing may
965 move the value out of high mem and thus not fit
966 in a signed 16bit value. */
970 /* Note that we've changed the relocation contents, etc. */
971 elf_section_data (sec
)->relocs
= internal_relocs
;
972 elf_section_data (sec
)->this_hdr
.contents
= contents
;
973 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
975 /* Fix the opcode. */
976 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
977 contents
+ irel
->r_offset
- 2);
979 /* Fix the relocation's type. */
980 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
983 /* The opcode got shorter too, so we have to fix the
987 /* Delete two bytes of data. */
988 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
989 irel
->r_offset
+ 1, 2))
992 /* That will change things, so, we should relax again.
993 Note that this is not required, and it may be slow. */
997 /* mov imm24,an -> mov imm16,an
998 cmp imm24,an -> cmp imm16,an
999 mov (abs24),dn -> mov (abs16),dn
1000 mov dn,(abs24) -> mov dn,(abs16)
1001 movb dn,(abs24) -> movb dn,(abs16)
1002 movbu (abs24),dn -> movbu (abs16),dn */
1009 /* Note that we've changed the relocation contents, etc. */
1010 elf_section_data (sec
)->relocs
= internal_relocs
;
1011 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1012 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1014 if ((code
& 0xfc) == 0x74)
1015 code
= 0xdc + (code
& 0x03);
1016 else if ((code
& 0xfc) == 0x7c)
1017 code
= 0xec + (code
& 0x03);
1018 else if ((code
& 0xfc) == 0xc0)
1019 code
= 0xc8 + (code
& 0x03);
1020 else if ((code
& 0xfc) == 0x40)
1021 code
= 0xc0 + (code
& 0x03);
1022 else if ((code
& 0xfc) == 0x44)
1023 code
= 0xc4 + (code
& 0x03);
1024 else if ((code
& 0xfc) == 0xc8)
1025 code
= 0xcc + (code
& 0x03);
1027 /* Fix the opcode. */
1028 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1030 /* Fix the relocation's type. */
1031 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1034 /* The opcode got shorter too, so we have to fix the
1036 irel
->r_offset
-= 1;
1038 /* Delete two bytes of data. */
1039 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1040 irel
->r_offset
+ 1, 2))
1043 /* That will change things, so, we should relax again.
1044 Note that this is not required, and it may be slow. */
1048 /* cmp imm24,dn -> cmp imm16,dn
1049 mov (abs24),an -> mov (abs16),an
1050 mov an,(abs24) -> mov an,(abs16)
1051 add imm24,dn -> add imm16,dn
1052 add imm24,an -> add imm16,an
1053 sub imm24,dn -> sub imm16,dn
1054 sub imm24,an -> sub imm16,an
1055 And all d24->d16 in memory ops. */
1072 /* Not safe if the high bit is on as relaxing may
1073 move the value out of high mem and thus not fit
1074 in a signed 16bit value. */
1075 if (((code
& 0xfc) == 0x78
1076 || (code
& 0xfc) == 0x60
1077 || (code
& 0xfc) == 0x64
1078 || (code
& 0xfc) == 0x68
1079 || (code
& 0xfc) == 0x6c
1080 || (code
& 0xfc) == 0x80
1081 || (code
& 0xfc) == 0xf0
1082 || (code
& 0xfc) == 0x00
1083 || (code
& 0xfc) == 0x10
1084 || (code
& 0xfc) == 0xb0
1085 || (code
& 0xfc) == 0x30
1086 || (code
& 0xfc) == 0xa0
1087 || (code
& 0xfc) == 0x20
1088 || (code
& 0xfc) == 0x90)
1089 && (value
& 0x8000) != 0)
1092 /* Note that we've changed the relocation contents, etc. */
1093 elf_section_data (sec
)->relocs
= internal_relocs
;
1094 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1095 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1097 /* Fix the opcode. */
1098 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1100 if ((code
& 0xfc) == 0x78)
1101 code
= 0x48 + (code
& 0x03);
1102 else if ((code
& 0xfc) == 0xd0)
1103 code
= 0x30 + (code
& 0x03);
1104 else if ((code
& 0xfc) == 0x50)
1105 code
= 0x20 + (code
& 0x03);
1106 else if ((code
& 0xfc) == 0x60)
1107 code
= 0x18 + (code
& 0x03);
1108 else if ((code
& 0xfc) == 0x64)
1109 code
= 0x08 + (code
& 0x03);
1110 else if ((code
& 0xfc) == 0x68)
1111 code
= 0x1c + (code
& 0x03);
1112 else if ((code
& 0xfc) == 0x6c)
1113 code
= 0x0c + (code
& 0x03);
1114 else if ((code
& 0xfc) == 0x80)
1115 code
= 0xc0 + (code
& 0x07);
1116 else if ((code
& 0xfc) == 0xf0)
1117 code
= 0xb0 + (code
& 0x07);
1118 else if ((code
& 0xfc) == 0x00)
1119 code
= 0x80 + (code
& 0x07);
1120 else if ((code
& 0xfc) == 0x10)
1121 code
= 0xa0 + (code
& 0x07);
1122 else if ((code
& 0xfc) == 0xb0)
1123 code
= 0x70 + (code
& 0x07);
1124 else if ((code
& 0xfc) == 0x30)
1125 code
= 0x60 + (code
& 0x07);
1126 else if ((code
& 0xfc) == 0xa0)
1127 code
= 0xd0 + (code
& 0x07);
1128 else if ((code
& 0xfc) == 0x20)
1129 code
= 0x90 + (code
& 0x07);
1130 else if ((code
& 0xfc) == 0x90)
1131 code
= 0x50 + (code
& 0x07);
1133 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1135 /* Fix the relocation's type. */
1136 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1139 /* Delete one bytes of data. */
1140 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1141 irel
->r_offset
+ 2, 1))
1144 /* That will change things, so, we should relax again.
1145 Note that this is not required, and it may be slow. */
1149 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1151 /* Note that we've changed the reldection contents, etc. */
1152 elf_section_data (sec
)->relocs
= internal_relocs
;
1153 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1154 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1156 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1157 contents
+ irel
->r_offset
- 2);
1159 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1160 contents
+ irel
->r_offset
- 1);
1162 /* Fix the relocation's type. */
1163 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1166 /* The reloc will be applied one byte in front of its
1167 current location. */
1168 irel
->r_offset
-= 1;
1170 /* Delete one bytes of data. */
1171 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1172 irel
->r_offset
+ 2, 1))
1175 /* That will change things, so, we should relax again.
1176 Note that this is not required, and it may be slow. */
1185 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1187 if (! link_info
->keep_memory
)
1191 /* Cache the symbols for elf_link_input_bfd. */
1192 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1196 if (contents
!= NULL
1197 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1199 if (! link_info
->keep_memory
)
1203 /* Cache the section contents for elf_link_input_bfd. */
1204 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1208 if (internal_relocs
!= NULL
1209 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1210 free (internal_relocs
);
1216 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1218 if (contents
!= NULL
1219 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1221 if (internal_relocs
!= NULL
1222 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1223 free (internal_relocs
);
1228 /* Return TRUE if a symbol exists at the given address, else return
1231 mn10200_elf_symbol_address_p (bfd
*abfd
,
1233 Elf_Internal_Sym
*isym
,
1236 Elf_Internal_Shdr
*symtab_hdr
;
1237 unsigned int sec_shndx
;
1238 Elf_Internal_Sym
*isymend
;
1239 struct elf_link_hash_entry
**sym_hashes
;
1240 struct elf_link_hash_entry
**end_hashes
;
1241 unsigned int symcount
;
1243 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1245 /* Examine all the local symbols. */
1246 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1247 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1249 if (isym
->st_shndx
== sec_shndx
1250 && isym
->st_value
== addr
)
1254 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1255 - symtab_hdr
->sh_info
);
1256 sym_hashes
= elf_sym_hashes (abfd
);
1257 end_hashes
= sym_hashes
+ symcount
;
1258 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1260 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1261 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1262 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1263 && sym_hash
->root
.u
.def
.section
== sec
1264 && sym_hash
->root
.u
.def
.value
== addr
)
1271 /* This is a version of bfd_generic_get_relocated_section_contents
1272 which uses mn10200_elf_relocate_section. */
1275 mn10200_elf_get_relocated_section_contents (bfd
*output_bfd
,
1276 struct bfd_link_info
*link_info
,
1277 struct bfd_link_order
*link_order
,
1279 bfd_boolean relocatable
,
1282 Elf_Internal_Shdr
*symtab_hdr
;
1283 asection
*input_section
= link_order
->u
.indirect
.section
;
1284 bfd
*input_bfd
= input_section
->owner
;
1285 asection
**sections
= NULL
;
1286 Elf_Internal_Rela
*internal_relocs
= NULL
;
1287 Elf_Internal_Sym
*isymbuf
= NULL
;
1289 /* We only need to handle the case of relaxing, or of having a
1290 particular set of section contents, specially. */
1292 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1293 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1298 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
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
,
1356 if (sections
!= NULL
)
1359 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1361 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1362 free (internal_relocs
);
1368 if (sections
!= NULL
)
1371 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1373 if (internal_relocs
!= NULL
1374 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1375 free (internal_relocs
);
1379 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1380 #define TARGET_LITTLE_NAME "elf32-mn10200"
1381 #define ELF_ARCH bfd_arch_mn10200
1382 #define ELF_MACHINE_CODE EM_MN10200
1383 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1384 #define ELF_MAXPAGESIZE 0x1000
1386 #define elf_backend_rela_normal 1
1387 #define elf_info_to_howto mn10200_info_to_howto
1388 #define elf_info_to_howto_rel 0
1389 #define elf_backend_relocate_section mn10200_elf_relocate_section
1390 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1391 #define bfd_elf32_bfd_get_relocated_section_contents \
1392 mn10200_elf_get_relocated_section_contents
1394 #define elf_symbol_leading_char '_'
1396 #include "elf32-target.h"