1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998, 1999 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 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
26 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
27 static void mn10200_info_to_howto
28 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
29 static boolean mn10200_elf_relax_delete_bytes
30 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
31 static boolean mn10200_elf_symbol_address_p
32 PARAMS ((bfd
*, asection
*, Elf32_External_Sym
*, bfd_vma
));
34 /* We have to use RELA instructions since md_apply_fix3 in the assembler
35 does absolutely nothing. */
51 static reloc_howto_type elf_mn10200_howto_table
[] =
53 /* Dummy relocation. Does nothing. */
54 HOWTO (R_MN10200_NONE
,
60 complain_overflow_bitfield
,
61 bfd_elf_generic_reloc
,
67 /* Standard 32 bit reloc. */
74 complain_overflow_bitfield
,
75 bfd_elf_generic_reloc
,
81 /* Standard 16 bit reloc. */
88 complain_overflow_bitfield
,
89 bfd_elf_generic_reloc
,
95 /* Standard 8 bit reloc. */
102 complain_overflow_bitfield
,
103 bfd_elf_generic_reloc
,
109 /* Standard 24 bit reloc. */
116 complain_overflow_bitfield
,
117 bfd_elf_generic_reloc
,
123 /* Simple 8 pc-relative reloc. */
124 HOWTO (R_MN10200_PCREL8
,
130 complain_overflow_bitfield
,
131 bfd_elf_generic_reloc
,
137 /* Simple 16 pc-relative reloc. */
138 HOWTO (R_MN10200_PCREL16
,
144 complain_overflow_bitfield
,
145 bfd_elf_generic_reloc
,
151 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
152 to get the pc-relative offset correct. */
153 HOWTO (R_MN10200_PCREL24
,
159 complain_overflow_bitfield
,
160 bfd_elf_generic_reloc
,
168 struct mn10200_reloc_map
170 bfd_reloc_code_real_type bfd_reloc_val
;
171 unsigned char elf_reloc_val
;
174 static const struct mn10200_reloc_map mn10200_reloc_map
[] =
176 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
177 { BFD_RELOC_32
, R_MN10200_32
, },
178 { BFD_RELOC_16
, R_MN10200_16
, },
179 { BFD_RELOC_8
, R_MN10200_8
, },
180 { BFD_RELOC_24
, R_MN10200_24
, },
181 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
182 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
183 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
186 static reloc_howto_type
*
187 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
188 bfd
*abfd ATTRIBUTE_UNUSED
;
189 bfd_reloc_code_real_type code
;
194 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
197 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
198 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
204 /* Set the howto pointer for an MN10200 ELF reloc. */
207 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
208 bfd
*abfd ATTRIBUTE_UNUSED
;
210 Elf32_Internal_Rela
*dst
;
214 r_type
= ELF32_R_TYPE (dst
->r_info
);
215 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
216 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
219 /* Perform a relocation as part of a final link. */
220 static bfd_reloc_status_type
221 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
222 input_section
, contents
, offset
, value
,
223 addend
, info
, sym_sec
, is_local
)
224 reloc_howto_type
*howto
;
226 bfd
*output_bfd ATTRIBUTE_UNUSED
;
227 asection
*input_section
;
232 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
233 asection
*sym_sec ATTRIBUTE_UNUSED
;
234 int is_local ATTRIBUTE_UNUSED
;
236 unsigned long r_type
= howto
->type
;
237 bfd_byte
*hit_data
= contents
+ offset
;
247 bfd_put_32 (input_bfd
, value
, hit_data
);
253 if ((long)value
> 0x7fff || (long)value
< -0x8000)
254 return bfd_reloc_overflow
;
256 bfd_put_16 (input_bfd
, value
, hit_data
);
262 if ((long)value
> 0x7f || (long)value
< -0x80)
263 return bfd_reloc_overflow
;
265 bfd_put_8 (input_bfd
, value
, hit_data
);
271 if ((long)value
> 0x7fffff || (long)value
< -0x800000)
272 return bfd_reloc_overflow
;
275 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
276 bfd_put_32 (input_bfd
, value
, hit_data
);
279 case R_MN10200_PCREL8
:
280 value
-= (input_section
->output_section
->vma
281 + input_section
->output_offset
);
282 value
-= (offset
+ 1);
285 if ((long)value
> 0xff || (long)value
< -0x100)
286 return bfd_reloc_overflow
;
288 bfd_put_8 (input_bfd
, value
, hit_data
);
291 case R_MN10200_PCREL16
:
292 value
-= (input_section
->output_section
->vma
293 + input_section
->output_offset
);
294 value
-= (offset
+ 2);
297 if ((long)value
> 0xffff || (long)value
< -0x10000)
298 return bfd_reloc_overflow
;
300 bfd_put_16 (input_bfd
, value
, hit_data
);
303 case R_MN10200_PCREL24
:
304 value
-= (input_section
->output_section
->vma
305 + input_section
->output_offset
);
306 value
-= (offset
+ 3);
309 if ((long)value
> 0xffffff || (long)value
< -0x1000000)
310 return bfd_reloc_overflow
;
313 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
314 bfd_put_32 (input_bfd
, value
, hit_data
);
318 return bfd_reloc_notsupported
;
323 /* Relocate an MN10200 ELF section. */
325 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
326 contents
, relocs
, local_syms
, local_sections
)
328 struct bfd_link_info
*info
;
330 asection
*input_section
;
332 Elf_Internal_Rela
*relocs
;
333 Elf_Internal_Sym
*local_syms
;
334 asection
**local_sections
;
336 Elf_Internal_Shdr
*symtab_hdr
;
337 struct elf_link_hash_entry
**sym_hashes
;
338 Elf_Internal_Rela
*rel
, *relend
;
340 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
341 sym_hashes
= elf_sym_hashes (input_bfd
);
344 relend
= relocs
+ input_section
->reloc_count
;
345 for (; rel
< relend
; rel
++)
348 reloc_howto_type
*howto
;
349 unsigned long r_symndx
;
350 Elf_Internal_Sym
*sym
;
352 struct elf_link_hash_entry
*h
;
354 bfd_reloc_status_type r
;
356 r_symndx
= ELF32_R_SYM (rel
->r_info
);
357 r_type
= ELF32_R_TYPE (rel
->r_info
);
358 howto
= elf_mn10200_howto_table
+ r_type
;
360 if (info
->relocateable
)
362 /* This is a relocateable link. We don't have to change
363 anything, unless the reloc is against a section symbol,
364 in which case we have to adjust according to where the
365 section symbol winds up in the output section. */
366 if (r_symndx
< symtab_hdr
->sh_info
)
368 sym
= local_syms
+ r_symndx
;
369 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
371 sec
= local_sections
[r_symndx
];
372 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
379 /* This is a final link. */
383 if (r_symndx
< symtab_hdr
->sh_info
)
385 sym
= local_syms
+ r_symndx
;
386 sec
= local_sections
[r_symndx
];
387 relocation
= (sec
->output_section
->vma
393 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
394 while (h
->root
.type
== bfd_link_hash_indirect
395 || h
->root
.type
== bfd_link_hash_warning
)
396 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
397 if (h
->root
.type
== bfd_link_hash_defined
398 || h
->root
.type
== bfd_link_hash_defweak
)
400 sec
= h
->root
.u
.def
.section
;
401 relocation
= (h
->root
.u
.def
.value
402 + sec
->output_section
->vma
403 + sec
->output_offset
);
405 else if (h
->root
.type
== bfd_link_hash_undefweak
)
409 if (! ((*info
->callbacks
->undefined_symbol
)
410 (info
, h
->root
.root
.string
, input_bfd
,
411 input_section
, rel
->r_offset
)))
417 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
419 contents
, rel
->r_offset
,
420 relocation
, rel
->r_addend
,
421 info
, sec
, h
== NULL
);
423 if (r
!= bfd_reloc_ok
)
426 const char *msg
= (const char *)0;
429 name
= h
->root
.root
.string
;
432 name
= (bfd_elf_string_from_elf_section
433 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
434 if (name
== NULL
|| *name
== '\0')
435 name
= bfd_section_name (input_bfd
, sec
);
440 case bfd_reloc_overflow
:
441 if (! ((*info
->callbacks
->reloc_overflow
)
442 (info
, name
, howto
->name
, (bfd_vma
) 0,
443 input_bfd
, input_section
, rel
->r_offset
)))
447 case bfd_reloc_undefined
:
448 if (! ((*info
->callbacks
->undefined_symbol
)
449 (info
, name
, input_bfd
, input_section
,
454 case bfd_reloc_outofrange
:
455 msg
= _("internal error: out of range error");
458 case bfd_reloc_notsupported
:
459 msg
= _("internal error: unsupported relocation error");
462 case bfd_reloc_dangerous
:
463 msg
= _("internal error: dangerous error");
467 msg
= _("internal error: unknown error");
471 if (!((*info
->callbacks
->warning
)
472 (info
, msg
, name
, input_bfd
, input_section
,
483 /* This function handles relaxing for the mn10200.
485 There's quite a few relaxing opportunites available on the mn10200:
487 * jsr:24 -> jsr:16 2 bytes
489 * jmp:24 -> jmp:16 2 bytes
490 * jmp:16 -> bra:8 1 byte
492 * If the previous instruction is a conditional branch
493 around the jump/bra, we may be able to reverse its condition
494 and change its target to the jump's target. The jump/bra
495 can then be deleted. 2 bytes
497 * mov abs24 -> mov abs16 2 byte savings
499 * Most instructions which accept imm24 can relax to imm16 2 bytes
500 - Most instructions which accept imm16 can relax to imm8 1 byte
502 * Most instructions which accept d24 can relax to d16 2 bytes
503 - Most instructions which accept d16 can relax to d8 1 byte
505 abs24, imm24, d24 all look the same at the reloc level. It
506 might make the code simpler if we had different relocs for
507 the various relaxable operand types.
509 We don't handle imm16->imm8 or d16->d8 as they're very rare
510 and somewhat more difficult to support. */
513 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
516 struct bfd_link_info
*link_info
;
519 Elf_Internal_Shdr
*symtab_hdr
;
520 Elf_Internal_Rela
*internal_relocs
;
521 Elf_Internal_Rela
*free_relocs
= NULL
;
522 Elf_Internal_Rela
*irel
, *irelend
;
523 bfd_byte
*contents
= NULL
;
524 bfd_byte
*free_contents
= NULL
;
525 Elf32_External_Sym
*extsyms
= NULL
;
526 Elf32_External_Sym
*free_extsyms
= NULL
;
528 /* Assume nothing changes. */
531 /* We don't have to do anything for a relocateable link, if
532 this section does not have relocs, or if this is not a
534 if (link_info
->relocateable
535 || (sec
->flags
& SEC_RELOC
) == 0
536 || sec
->reloc_count
== 0
537 || (sec
->flags
& SEC_CODE
) == 0)
540 /* If this is the first time we have been called for this section,
541 initialize the cooked size. */
542 if (sec
->_cooked_size
== 0)
543 sec
->_cooked_size
= sec
->_raw_size
;
545 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
547 /* Get a copy of the native relocations. */
548 internal_relocs
= (_bfd_elf32_link_read_relocs
549 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
550 link_info
->keep_memory
));
551 if (internal_relocs
== NULL
)
553 if (! link_info
->keep_memory
)
554 free_relocs
= internal_relocs
;
556 /* Walk through them looking for relaxing opportunities. */
557 irelend
= internal_relocs
+ sec
->reloc_count
;
558 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
562 /* If this isn't something that can be relaxed, then ignore
564 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
565 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
566 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
569 /* Get the section contents if we haven't done so already. */
570 if (contents
== NULL
)
572 /* Get cached copy if it exists. */
573 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
574 contents
= elf_section_data (sec
)->this_hdr
.contents
;
577 /* Go get them off disk. */
578 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
579 if (contents
== NULL
)
581 free_contents
= contents
;
583 if (! bfd_get_section_contents (abfd
, sec
, contents
,
584 (file_ptr
) 0, sec
->_raw_size
))
589 /* Read this BFD's symbols if we haven't done so already. */
592 /* Get cached copy if it exists. */
593 if (symtab_hdr
->contents
!= NULL
)
594 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
597 /* Go get them off disk. */
598 extsyms
= ((Elf32_External_Sym
*)
599 bfd_malloc (symtab_hdr
->sh_size
));
602 free_extsyms
= extsyms
;
603 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
604 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
605 != symtab_hdr
->sh_size
))
610 /* Get the value of the symbol referred to by the reloc. */
611 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
613 Elf_Internal_Sym isym
;
616 /* A local symbol. */
617 bfd_elf32_swap_symbol_in (abfd
,
618 extsyms
+ ELF32_R_SYM (irel
->r_info
),
621 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
622 symval
= (isym
.st_value
623 + sym_sec
->output_section
->vma
624 + sym_sec
->output_offset
);
629 struct elf_link_hash_entry
*h
;
631 /* An external symbol. */
632 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
633 h
= elf_sym_hashes (abfd
)[indx
];
634 BFD_ASSERT (h
!= NULL
);
635 if (h
->root
.type
!= bfd_link_hash_defined
636 && h
->root
.type
!= bfd_link_hash_defweak
)
638 /* This appears to be a reference to an undefined
639 symbol. Just ignore it--it will be caught by the
640 regular reloc processing. */
644 symval
= (h
->root
.u
.def
.value
645 + h
->root
.u
.def
.section
->output_section
->vma
646 + h
->root
.u
.def
.section
->output_offset
);
649 /* For simplicity of coding, we are going to modify the section
650 contents, the section relocs, and the BFD symbol table. We
651 must tell the rest of the code not to free up this
652 information. It would be possible to instead create a table
653 of changes which have to be made, as is done in coff-mips.c;
654 that would be more work, but would require less memory when
655 the linker is run. */
658 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
660 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
662 bfd_vma value
= symval
;
664 /* Deal with pc-relative gunk. */
665 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
666 value
-= (irel
->r_offset
+ 3);
667 value
+= irel
->r_addend
;
669 /* See if the value will fit in 16 bits, note the high value is
670 0x7fff + 2 as the target will be two bytes closer if we are
672 if ((long)value
< 0x8001 && (long)value
> -0x8000)
676 /* Get the opcode. */
677 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
679 if (code
!= 0xe0 && code
!= 0xe1)
682 /* Note that we've changed the relocs, section contents, etc. */
683 elf_section_data (sec
)->relocs
= internal_relocs
;
686 elf_section_data (sec
)->this_hdr
.contents
= contents
;
687 free_contents
= NULL
;
689 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
692 /* Fix the opcode. */
694 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
695 else if (code
== 0xe1)
696 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
698 /* Fix the relocation's type. */
699 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
702 /* The opcode got shorter too, so we have to fix the offset. */
705 /* Delete two bytes of data. */
706 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
707 irel
->r_offset
+ 1, 2))
710 /* That will change things, so, we should relax again.
711 Note that this is not required, and it may be slow. */
716 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
718 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
720 bfd_vma value
= symval
;
722 /* Deal with pc-relative gunk. */
723 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
724 value
-= (irel
->r_offset
+ 2);
725 value
+= irel
->r_addend
;
727 /* See if the value will fit in 8 bits, note the high value is
728 0x7f + 1 as the target will be one bytes closer if we are
730 if ((long)value
< 0x80 && (long)value
> -0x80)
734 /* Get the opcode. */
735 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
740 /* Note that we've changed the relocs, section contents, etc. */
741 elf_section_data (sec
)->relocs
= internal_relocs
;
744 elf_section_data (sec
)->this_hdr
.contents
= contents
;
745 free_contents
= NULL
;
747 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
750 /* Fix the opcode. */
751 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
753 /* Fix the relocation's type. */
754 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
757 /* Delete one byte of data. */
758 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
759 irel
->r_offset
+ 1, 1))
762 /* That will change things, so, we should relax again.
763 Note that this is not required, and it may be slow. */
768 /* Try to eliminate an unconditional 8 bit pc-relative branch
769 which immediately follows a conditional 8 bit pc-relative
770 branch around the unconditional branch.
778 This happens when the bCC can't reach lab2 at assembly time,
779 but due to other relaxations it can reach at link time. */
780 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
782 Elf_Internal_Rela
*nrel
;
783 bfd_vma value
= symval
;
786 /* Deal with pc-relative gunk. */
787 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
788 value
-= (irel
->r_offset
+ 1);
789 value
+= irel
->r_addend
;
791 /* Do nothing if this reloc is the last byte in the section. */
792 if (irel
->r_offset
== sec
->_cooked_size
)
795 /* See if the next instruction is an unconditional pc-relative
796 branch, more often than not this test will fail, so we
797 test it first to speed things up. */
798 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
802 /* Also make sure the next relocation applies to the next
803 instruction and that it's a pc-relative 8 bit branch. */
806 || irel
->r_offset
+ 2 != nrel
->r_offset
807 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
810 /* Make sure our destination immediately follows the
811 unconditional branch. */
812 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
813 + irel
->r_offset
+ 3))
816 /* Now make sure we are a conditional branch. This may not
817 be necessary, but why take the chance.
819 Note these checks assume that R_MN10200_PCREL8 relocs
820 only occur on bCC and bCCx insns. If they occured
821 elsewhere, we'd need to know the start of this insn
822 for this check to be accurate. */
823 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
824 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
825 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
826 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
827 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
828 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
829 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
832 /* We also have to be sure there is no symbol/label
833 at the unconditional branch. */
834 if (mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
,
838 /* Note that we've changed the relocs, section contents, etc. */
839 elf_section_data (sec
)->relocs
= internal_relocs
;
842 elf_section_data (sec
)->this_hdr
.contents
= contents
;
843 free_contents
= NULL
;
845 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
848 /* Reverse the condition of the first branch. */
906 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
908 /* Set the reloc type and symbol for the first branch
909 from the second branch. */
910 irel
->r_info
= nrel
->r_info
;
912 /* Make the reloc for the second branch a null reloc. */
913 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
916 /* Delete two bytes of data. */
917 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
918 irel
->r_offset
+ 1, 2))
921 /* That will change things, so, we should relax again.
922 Note that this is not required, and it may be slow. */
926 /* Try to turn a 24bit immediate, displacement or absolute address
927 into a 16bit immediate, displacement or absolute address. */
928 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
930 bfd_vma value
= symval
;
932 /* See if the value will fit in 16 bits.
933 We allow any 16bit match here. We prune those we can't
935 if ((long)value
< 0x7fff && (long)value
> -0x8000)
939 /* All insns which have 24bit operands are 5 bytes long,
940 the first byte will always be 0xf4, but we double check
943 /* Get the first opcode. */
944 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
949 /* Get the second opcode. */
950 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
954 /* mov imm24,dn -> mov imm16,dn */
956 /* Not safe if the high bit is on as relaxing may
957 move the value out of high mem and thus not fit
958 in a signed 16bit value. */
962 /* Note that we've changed the reldection contents, etc. */
963 elf_section_data (sec
)->relocs
= internal_relocs
;
966 elf_section_data (sec
)->this_hdr
.contents
= contents
;
967 free_contents
= NULL
;
969 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
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 reldection contents, etc. */
1007 elf_section_data (sec
)->relocs
= internal_relocs
;
1010 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1011 free_contents
= NULL
;
1013 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1014 free_extsyms
= NULL
;
1016 if ((code
& 0xfc) == 0x74)
1017 code
= 0xdc + (code
& 0x03);
1018 else if ((code
& 0xfc) == 0x7c)
1019 code
= 0xec + (code
& 0x03);
1020 else if ((code
& 0xfc) == 0xc0)
1021 code
= 0xc8 + (code
& 0x03);
1022 else if ((code
& 0xfc) == 0x40)
1023 code
= 0xc0 + (code
& 0x03);
1024 else if ((code
& 0xfc) == 0x44)
1025 code
= 0xc4 + (code
& 0x03);
1026 else if ((code
& 0xfc) == 0xc8)
1027 code
= 0xcc + (code
& 0x03);
1029 /* Fix the opcode. */
1030 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1032 /* Fix the relocation's type. */
1033 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1036 /* The opcode got shorter too, so we have to fix the
1038 irel
->r_offset
-= 1;
1040 /* Delete two bytes of data. */
1041 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1042 irel
->r_offset
+ 1, 2))
1045 /* That will change things, so, we should relax again.
1046 Note that this is not required, and it may be slow. */
1050 /* cmp imm24,dn -> cmp imm16,dn
1051 mov (abs24),an -> mov (abs16),an
1052 mov an,(abs24) -> mov an,(abs16)
1053 add imm24,dn -> add imm16,dn
1054 add imm24,an -> add imm16,an
1055 sub imm24,dn -> sub imm16,dn
1056 sub imm24,an -> sub imm16,an
1057 And all d24->d16 in memory ops. */
1074 /* Not safe if the high bit is on as relaxing may
1075 move the value out of high mem and thus not fit
1076 in a signed 16bit value. */
1077 if (((code
& 0xfc) == 0x78
1078 || (code
& 0xfc) == 0x60
1079 || (code
& 0xfc) == 0x64
1080 || (code
& 0xfc) == 0x68
1081 || (code
& 0xfc) == 0x6c
1082 || (code
& 0xfc) == 0x80
1083 || (code
& 0xfc) == 0xf0
1084 || (code
& 0xfc) == 0x00
1085 || (code
& 0xfc) == 0x10
1086 || (code
& 0xfc) == 0xb0
1087 || (code
& 0xfc) == 0x30
1088 || (code
& 0xfc) == 0xa0
1089 || (code
& 0xfc) == 0x20
1090 || (code
& 0xfc) == 0x90)
1091 && (value
& 0x8000) != 0)
1094 /* Note that we've changed the reldection contents, etc. */
1095 elf_section_data (sec
)->relocs
= internal_relocs
;
1098 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1099 free_contents
= NULL
;
1101 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1102 free_extsyms
= NULL
;
1104 /* Fix the opcode. */
1105 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1107 if ((code
& 0xfc) == 0x78)
1108 code
= 0x48 + (code
& 0x03);
1109 else if ((code
& 0xfc) == 0xd0)
1110 code
= 0x30 + (code
& 0x03);
1111 else if ((code
& 0xfc) == 0x50)
1112 code
= 0x20 + (code
& 0x03);
1113 else if ((code
& 0xfc) == 0x60)
1114 code
= 0x18 + (code
& 0x03);
1115 else if ((code
& 0xfc) == 0x64)
1116 code
= 0x08 + (code
& 0x03);
1117 else if ((code
& 0xfc) == 0x68)
1118 code
= 0x1c + (code
& 0x03);
1119 else if ((code
& 0xfc) == 0x6c)
1120 code
= 0x0c + (code
& 0x03);
1121 else if ((code
& 0xfc) == 0x80)
1122 code
= 0xc0 + (code
& 0x07);
1123 else if ((code
& 0xfc) == 0xf0)
1124 code
= 0xb0 + (code
& 0x07);
1125 else if ((code
& 0xfc) == 0x00)
1126 code
= 0x80 + (code
& 0x07);
1127 else if ((code
& 0xfc) == 0x10)
1128 code
= 0xa0 + (code
& 0x07);
1129 else if ((code
& 0xfc) == 0xb0)
1130 code
= 0x70 + (code
& 0x07);
1131 else if ((code
& 0xfc) == 0x30)
1132 code
= 0x60 + (code
& 0x07);
1133 else if ((code
& 0xfc) == 0xa0)
1134 code
= 0xd0 + (code
& 0x07);
1135 else if ((code
& 0xfc) == 0x20)
1136 code
= 0x90 + (code
& 0x07);
1137 else if ((code
& 0xfc) == 0x90)
1138 code
= 0x50 + (code
& 0x07);
1140 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1142 /* Fix the relocation's type. */
1143 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1146 /* Delete one bytes of data. */
1147 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1148 irel
->r_offset
+ 2, 1))
1151 /* That will change things, so, we should relax again.
1152 Note that this is not required, and it may be slow. */
1156 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1158 /* Note that we've changed the reldection contents, etc. */
1159 elf_section_data (sec
)->relocs
= internal_relocs
;
1162 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1163 free_contents
= NULL
;
1165 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1166 free_extsyms
= NULL
;
1168 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1169 contents
+ irel
->r_offset
- 2);
1171 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1172 contents
+ irel
->r_offset
- 1);
1174 /* Fix the relocation's type. */
1175 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1178 /* The reloc will be applied one byte in front of its
1179 current location. */
1180 irel
->r_offset
-= 1;
1182 /* Delete one bytes of data. */
1183 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1184 irel
->r_offset
+ 2, 1))
1187 /* That will change things, so, we should relax again.
1188 Note that this is not required, and it may be slow. */
1196 if (free_relocs
!= NULL
)
1202 if (free_contents
!= NULL
)
1204 if (! link_info
->keep_memory
)
1205 free (free_contents
);
1208 /* Cache the section contents for elf_link_input_bfd. */
1209 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1211 free_contents
= NULL
;
1214 if (free_extsyms
!= NULL
)
1216 if (! link_info
->keep_memory
)
1217 free (free_extsyms
);
1220 /* Cache the symbols for elf_link_input_bfd. */
1221 symtab_hdr
->contents
= extsyms
;
1223 free_extsyms
= NULL
;
1229 if (free_relocs
!= NULL
)
1231 if (free_contents
!= NULL
)
1232 free (free_contents
);
1233 if (free_extsyms
!= NULL
)
1234 free (free_extsyms
);
1238 /* Delete some bytes from a section while relaxing. */
1241 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1247 Elf_Internal_Shdr
*symtab_hdr
;
1248 Elf32_External_Sym
*extsyms
;
1251 Elf_Internal_Rela
*irel
, *irelend
;
1252 Elf_Internal_Rela
*irelalign
;
1254 Elf32_External_Sym
*esym
, *esymend
;
1255 struct elf_link_hash_entry
*sym_hash
;
1257 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1258 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1260 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1262 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1264 /* The deletion must stop at the next ALIGN reloc for an aligment
1265 power larger than the number of bytes we are deleting. */
1268 toaddr
= sec
->_cooked_size
;
1270 irel
= elf_section_data (sec
)->relocs
;
1271 irelend
= irel
+ sec
->reloc_count
;
1273 /* Actually delete the bytes. */
1274 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1275 sec
->_cooked_size
-= count
;
1277 /* Adjust all the relocs. */
1278 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1280 /* Get the new reloc address. */
1281 if ((irel
->r_offset
> addr
1282 && irel
->r_offset
< toaddr
))
1283 irel
->r_offset
-= count
;
1286 /* Adjust the local symbols defined in this section. */
1288 esymend
= esym
+ symtab_hdr
->sh_info
;
1289 for (; esym
< esymend
; esym
++)
1291 Elf_Internal_Sym isym
;
1293 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1295 if (isym
.st_shndx
== shndx
1296 && isym
.st_value
> addr
1297 && isym
.st_value
< toaddr
)
1299 isym
.st_value
-= count
;
1300 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1304 /* Now adjust the global symbols defined in this section. */
1305 esym
= extsyms
+ symtab_hdr
->sh_info
;
1306 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1307 for (index
= 0; esym
< esymend
; esym
++, index
++)
1309 Elf_Internal_Sym isym
;
1311 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1312 sym_hash
= elf_sym_hashes (abfd
)[index
];
1313 if (isym
.st_shndx
== shndx
1314 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1315 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1316 && (sym_hash
)->root
.u
.def
.section
== sec
1317 && (sym_hash
)->root
.u
.def
.value
> addr
1318 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1320 (sym_hash
)->root
.u
.def
.value
-= count
;
1327 /* Return true if a symbol exists at the given address, else return
1330 mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
1333 Elf32_External_Sym
*extsyms
;
1336 Elf_Internal_Shdr
*symtab_hdr
;
1338 Elf32_External_Sym
*esym
, *esymend
;
1339 struct elf_link_hash_entry
**sym_hash
, **sym_hash_end
;
1341 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1342 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1344 /* Examine all the symbols. */
1346 esymend
= esym
+ symtab_hdr
->sh_info
;
1347 for (; esym
< esymend
; esym
++)
1349 Elf_Internal_Sym isym
;
1351 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1353 if (isym
.st_shndx
== shndx
1354 && isym
.st_value
== addr
)
1358 sym_hash
= elf_sym_hashes (abfd
);
1359 sym_hash_end
= (sym_hash
1360 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1361 - symtab_hdr
->sh_info
));
1362 for (; sym_hash
< sym_hash_end
; sym_hash
++)
1364 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1365 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1366 && (*sym_hash
)->root
.u
.def
.section
== sec
1367 && (*sym_hash
)->root
.u
.def
.value
== addr
)
1373 /* This is a version of bfd_generic_get_relocated_section_contents
1374 which uses mn10200_elf_relocate_section. */
1377 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1378 data
, relocateable
, symbols
)
1380 struct bfd_link_info
*link_info
;
1381 struct bfd_link_order
*link_order
;
1383 boolean relocateable
;
1386 Elf_Internal_Shdr
*symtab_hdr
;
1387 asection
*input_section
= link_order
->u
.indirect
.section
;
1388 bfd
*input_bfd
= input_section
->owner
;
1389 asection
**sections
= NULL
;
1390 Elf_Internal_Rela
*internal_relocs
= NULL
;
1391 Elf32_External_Sym
*external_syms
= NULL
;
1392 Elf_Internal_Sym
*internal_syms
= NULL
;
1394 /* We only need to handle the case of relaxing, or of having a
1395 particular set of section contents, specially. */
1397 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1398 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1403 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1405 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1406 input_section
->_raw_size
);
1408 if ((input_section
->flags
& SEC_RELOC
) != 0
1409 && input_section
->reloc_count
> 0)
1411 Elf_Internal_Sym
*isymp
;
1413 Elf32_External_Sym
*esym
, *esymend
;
1415 if (symtab_hdr
->contents
!= NULL
)
1416 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1419 external_syms
= ((Elf32_External_Sym
*)
1420 bfd_malloc (symtab_hdr
->sh_info
1421 * sizeof (Elf32_External_Sym
)));
1422 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1424 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1425 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
1426 symtab_hdr
->sh_info
, input_bfd
)
1427 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
1431 internal_relocs
= (_bfd_elf32_link_read_relocs
1432 (input_bfd
, input_section
, (PTR
) NULL
,
1433 (Elf_Internal_Rela
*) NULL
, false));
1434 if (internal_relocs
== NULL
)
1437 internal_syms
= ((Elf_Internal_Sym
*)
1438 bfd_malloc (symtab_hdr
->sh_info
1439 * sizeof (Elf_Internal_Sym
)));
1440 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1443 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
1444 * sizeof (asection
*));
1445 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
1448 isymp
= internal_syms
;
1450 esym
= external_syms
;
1451 esymend
= esym
+ symtab_hdr
->sh_info
;
1452 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
1456 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
1458 if (isymp
->st_shndx
== SHN_UNDEF
)
1459 isec
= bfd_und_section_ptr
;
1460 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
1461 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1462 else if (isymp
->st_shndx
== SHN_ABS
)
1463 isec
= bfd_abs_section_ptr
;
1464 else if (isymp
->st_shndx
== SHN_COMMON
)
1465 isec
= bfd_com_section_ptr
;
1475 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1476 input_section
, data
, internal_relocs
,
1477 internal_syms
, sections
))
1480 if (sections
!= NULL
)
1483 if (internal_syms
!= NULL
)
1484 free (internal_syms
);
1485 internal_syms
= NULL
;
1486 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1487 free (external_syms
);
1488 external_syms
= NULL
;
1489 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1490 free (internal_relocs
);
1491 internal_relocs
= NULL
;
1497 if (internal_relocs
!= NULL
1498 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1499 free (internal_relocs
);
1500 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1501 free (external_syms
);
1502 if (internal_syms
!= NULL
)
1503 free (internal_syms
);
1504 if (sections
!= NULL
)
1510 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1511 #define TARGET_LITTLE_NAME "elf32-mn10200"
1512 #define ELF_ARCH bfd_arch_mn10200
1513 #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
1514 #define ELF_MAXPAGESIZE 0x1000
1516 #define elf_info_to_howto mn10200_info_to_howto
1517 #define elf_info_to_howto_rel 0
1518 #define elf_backend_relocate_section mn10200_elf_relocate_section
1519 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1520 #define bfd_elf32_bfd_get_relocated_section_contents \
1521 mn10200_elf_get_relocated_section_contents
1523 #define elf_symbol_leading_char '_'
1525 #include "elf32-target.h"