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. */
28 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
29 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
30 static void mn10200_info_to_howto
31 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
32 static bfd_boolean mn10200_elf_relax_delete_bytes
33 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
34 static bfd_boolean mn10200_elf_symbol_address_p
35 PARAMS ((bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
));
36 static bfd_reloc_status_type mn10200_elf_final_link_relocate
37 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*,
38 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
39 struct bfd_link_info
*, asection
*, int));
40 static bfd_boolean mn10200_elf_relocate_section
41 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
42 bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*,
44 static bfd_boolean mn10200_elf_relax_section
45 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
46 static bfd_byte
* mn10200_elf_get_relocated_section_contents
47 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
48 bfd_byte
*, bfd_boolean
, asymbol
**));
62 static reloc_howto_type elf_mn10200_howto_table
[] = {
63 /* Dummy relocation. Does nothing. */
64 HOWTO (R_MN10200_NONE
,
70 complain_overflow_bitfield
,
71 bfd_elf_generic_reloc
,
77 /* Standard 32 bit reloc. */
84 complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
,
91 /* Standard 16 bit reloc. */
98 complain_overflow_bitfield
,
99 bfd_elf_generic_reloc
,
105 /* Standard 8 bit reloc. */
112 complain_overflow_bitfield
,
113 bfd_elf_generic_reloc
,
119 /* Standard 24 bit reloc. */
126 complain_overflow_bitfield
,
127 bfd_elf_generic_reloc
,
133 /* Simple 8 pc-relative reloc. */
134 HOWTO (R_MN10200_PCREL8
,
140 complain_overflow_bitfield
,
141 bfd_elf_generic_reloc
,
147 /* Simple 16 pc-relative reloc. */
148 HOWTO (R_MN10200_PCREL16
,
154 complain_overflow_bitfield
,
155 bfd_elf_generic_reloc
,
161 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
162 to get the pc-relative offset correct. */
163 HOWTO (R_MN10200_PCREL24
,
169 complain_overflow_bitfield
,
170 bfd_elf_generic_reloc
,
178 struct mn10200_reloc_map
{
179 bfd_reloc_code_real_type bfd_reloc_val
;
180 unsigned char elf_reloc_val
;
183 static const struct mn10200_reloc_map mn10200_reloc_map
[] = {
184 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
185 { BFD_RELOC_32
, R_MN10200_32
, },
186 { BFD_RELOC_16
, R_MN10200_16
, },
187 { BFD_RELOC_8
, R_MN10200_8
, },
188 { BFD_RELOC_24
, R_MN10200_24
, },
189 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
190 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
191 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
194 static reloc_howto_type
*
195 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
196 bfd
*abfd ATTRIBUTE_UNUSED
;
197 bfd_reloc_code_real_type code
;
202 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
205 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
206 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
212 static reloc_howto_type
*
213 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
219 i
< (sizeof (elf_mn10200_howto_table
)
220 / sizeof (elf_mn10200_howto_table
[0]));
222 if (elf_mn10200_howto_table
[i
].name
!= NULL
223 && strcasecmp (elf_mn10200_howto_table
[i
].name
, r_name
) == 0)
224 return &elf_mn10200_howto_table
[i
];
229 /* Set the howto pointer for an MN10200 ELF reloc. */
232 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
233 bfd
*abfd ATTRIBUTE_UNUSED
;
235 Elf_Internal_Rela
*dst
;
239 r_type
= ELF32_R_TYPE (dst
->r_info
);
240 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
241 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
244 /* Perform a relocation as part of a final link. */
246 static bfd_reloc_status_type
247 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
248 input_section
, contents
, offset
, value
,
249 addend
, info
, sym_sec
, is_local
)
250 reloc_howto_type
*howto
;
252 bfd
*output_bfd ATTRIBUTE_UNUSED
;
253 asection
*input_section
;
258 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
259 asection
*sym_sec ATTRIBUTE_UNUSED
;
260 int is_local ATTRIBUTE_UNUSED
;
262 unsigned long r_type
= howto
->type
;
263 bfd_byte
*hit_data
= contents
+ offset
;
273 bfd_put_32 (input_bfd
, value
, hit_data
);
279 if ((long) value
> 0x7fff || (long) value
< -0x8000)
280 return bfd_reloc_overflow
;
282 bfd_put_16 (input_bfd
, value
, hit_data
);
288 if ((long) value
> 0x7f || (long) value
< -0x80)
289 return bfd_reloc_overflow
;
291 bfd_put_8 (input_bfd
, value
, hit_data
);
297 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
298 return bfd_reloc_overflow
;
301 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
302 bfd_put_32 (input_bfd
, value
, hit_data
);
305 case R_MN10200_PCREL8
:
306 value
-= (input_section
->output_section
->vma
307 + input_section
->output_offset
);
308 value
-= (offset
+ 1);
311 if ((long) value
> 0xff || (long) value
< -0x100)
312 return bfd_reloc_overflow
;
314 bfd_put_8 (input_bfd
, value
, hit_data
);
317 case R_MN10200_PCREL16
:
318 value
-= (input_section
->output_section
->vma
319 + input_section
->output_offset
);
320 value
-= (offset
+ 2);
323 if ((long) value
> 0xffff || (long) value
< -0x10000)
324 return bfd_reloc_overflow
;
326 bfd_put_16 (input_bfd
, value
, hit_data
);
329 case R_MN10200_PCREL24
:
330 value
-= (input_section
->output_section
->vma
331 + input_section
->output_offset
);
332 value
-= (offset
+ 3);
335 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
336 return bfd_reloc_overflow
;
339 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
340 bfd_put_32 (input_bfd
, value
, hit_data
);
344 return bfd_reloc_notsupported
;
348 /* Relocate an MN10200 ELF section. */
350 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
351 contents
, relocs
, local_syms
, local_sections
)
353 struct bfd_link_info
*info
;
355 asection
*input_section
;
357 Elf_Internal_Rela
*relocs
;
358 Elf_Internal_Sym
*local_syms
;
359 asection
**local_sections
;
361 Elf_Internal_Shdr
*symtab_hdr
;
362 struct elf_link_hash_entry
**sym_hashes
;
363 Elf_Internal_Rela
*rel
, *relend
;
365 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
366 sym_hashes
= elf_sym_hashes (input_bfd
);
369 relend
= relocs
+ input_section
->reloc_count
;
370 for (; rel
< relend
; rel
++)
373 reloc_howto_type
*howto
;
374 unsigned long r_symndx
;
375 Elf_Internal_Sym
*sym
;
377 struct elf_link_hash_entry
*h
;
379 bfd_reloc_status_type r
;
381 r_symndx
= ELF32_R_SYM (rel
->r_info
);
382 r_type
= ELF32_R_TYPE (rel
->r_info
);
383 howto
= elf_mn10200_howto_table
+ r_type
;
388 if (r_symndx
< symtab_hdr
->sh_info
)
390 sym
= local_syms
+ r_symndx
;
391 sec
= local_sections
[r_symndx
];
392 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
396 bfd_boolean unresolved_reloc
, warned
;
398 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
399 r_symndx
, symtab_hdr
, sym_hashes
,
401 unresolved_reloc
, warned
);
404 if (sec
!= NULL
&& elf_discarded_section (sec
))
406 /* For relocs against symbols from removed linkonce sections,
407 or sections discarded by a linker script, we just want the
408 section contents zeroed. Avoid any special processing. */
409 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
415 if (info
->relocatable
)
418 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
420 contents
, rel
->r_offset
,
421 relocation
, rel
->r_addend
,
422 info
, sec
, h
== NULL
);
424 if (r
!= bfd_reloc_ok
)
427 const char *msg
= (const char *) 0;
430 name
= h
->root
.root
.string
;
433 name
= (bfd_elf_string_from_elf_section
434 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
435 if (name
== NULL
|| *name
== '\0')
436 name
= bfd_section_name (input_bfd
, sec
);
441 case bfd_reloc_overflow
:
442 if (! ((*info
->callbacks
->reloc_overflow
)
443 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
444 (bfd_vma
) 0, input_bfd
, input_section
,
449 case bfd_reloc_undefined
:
450 if (! ((*info
->callbacks
->undefined_symbol
)
451 (info
, name
, input_bfd
, input_section
,
452 rel
->r_offset
, TRUE
)))
456 case bfd_reloc_outofrange
:
457 msg
= _("internal error: out of range error");
460 case bfd_reloc_notsupported
:
461 msg
= _("internal error: unsupported relocation error");
464 case bfd_reloc_dangerous
:
465 msg
= _("internal error: dangerous error");
469 msg
= _("internal error: unknown error");
473 if (!((*info
->callbacks
->warning
)
474 (info
, msg
, name
, input_bfd
, input_section
,
485 /* This function handles relaxing for the mn10200.
487 There are quite a few relaxing opportunities available on the mn10200:
489 * jsr:24 -> jsr:16 2 bytes
491 * jmp:24 -> jmp:16 2 bytes
492 * jmp:16 -> bra:8 1 byte
494 * If the previous instruction is a conditional branch
495 around the jump/bra, we may be able to reverse its condition
496 and change its target to the jump's target. The jump/bra
497 can then be deleted. 2 bytes
499 * mov abs24 -> mov abs16 2 byte savings
501 * Most instructions which accept imm24 can relax to imm16 2 bytes
502 - Most instructions which accept imm16 can relax to imm8 1 byte
504 * Most instructions which accept d24 can relax to d16 2 bytes
505 - Most instructions which accept d16 can relax to d8 1 byte
507 abs24, imm24, d24 all look the same at the reloc level. It
508 might make the code simpler if we had different relocs for
509 the various relaxable operand types.
511 We don't handle imm16->imm8 or d16->d8 as they're very rare
512 and somewhat more difficult to support. */
515 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
518 struct bfd_link_info
*link_info
;
521 Elf_Internal_Shdr
*symtab_hdr
;
522 Elf_Internal_Rela
*internal_relocs
;
523 Elf_Internal_Rela
*irel
, *irelend
;
524 bfd_byte
*contents
= NULL
;
525 Elf_Internal_Sym
*isymbuf
= NULL
;
527 /* Assume nothing changes. */
530 /* We don't have to do anything for a relocatable link, if
531 this section does not have relocs, or if this is not a
533 if (link_info
->relocatable
534 || (sec
->flags
& SEC_RELOC
) == 0
535 || sec
->reloc_count
== 0
536 || (sec
->flags
& SEC_CODE
) == 0)
539 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
541 /* Get a copy of the native relocations. */
542 internal_relocs
= (_bfd_elf_link_read_relocs
543 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
544 link_info
->keep_memory
));
545 if (internal_relocs
== NULL
)
548 /* Walk through them looking for relaxing opportunities. */
549 irelend
= internal_relocs
+ sec
->reloc_count
;
550 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
554 /* If this isn't something that can be relaxed, then ignore
556 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
557 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
558 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
561 /* Get the section contents if we haven't done so already. */
562 if (contents
== NULL
)
564 /* Get cached copy if it exists. */
565 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
566 contents
= elf_section_data (sec
)->this_hdr
.contents
;
569 /* Go get them off disk. */
570 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
575 /* Read this BFD's local symbols if we haven't done so already. */
576 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
578 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
580 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
581 symtab_hdr
->sh_info
, 0,
587 /* Get the value of the symbol referred to by the reloc. */
588 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
590 /* A local symbol. */
591 Elf_Internal_Sym
*isym
;
594 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
595 if (isym
->st_shndx
== SHN_UNDEF
)
596 sym_sec
= bfd_und_section_ptr
;
597 else if (isym
->st_shndx
== SHN_ABS
)
598 sym_sec
= bfd_abs_section_ptr
;
599 else if (isym
->st_shndx
== SHN_COMMON
)
600 sym_sec
= bfd_com_section_ptr
;
602 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
603 symval
= (isym
->st_value
604 + sym_sec
->output_section
->vma
605 + sym_sec
->output_offset
);
610 struct elf_link_hash_entry
*h
;
612 /* An external symbol. */
613 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
614 h
= elf_sym_hashes (abfd
)[indx
];
615 BFD_ASSERT (h
!= NULL
);
616 if (h
->root
.type
!= bfd_link_hash_defined
617 && h
->root
.type
!= bfd_link_hash_defweak
)
619 /* This appears to be a reference to an undefined
620 symbol. Just ignore it--it will be caught by the
621 regular reloc processing. */
625 symval
= (h
->root
.u
.def
.value
626 + h
->root
.u
.def
.section
->output_section
->vma
627 + h
->root
.u
.def
.section
->output_offset
);
630 /* For simplicity of coding, we are going to modify the section
631 contents, the section relocs, and the BFD symbol table. We
632 must tell the rest of the code not to free up this
633 information. It would be possible to instead create a table
634 of changes which have to be made, as is done in coff-mips.c;
635 that would be more work, but would require less memory when
636 the linker is run. */
638 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
640 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
642 bfd_vma value
= symval
;
644 /* Deal with pc-relative gunk. */
645 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
646 value
-= (irel
->r_offset
+ 3);
647 value
+= irel
->r_addend
;
649 /* See if the value will fit in 16 bits, note the high value is
650 0x7fff + 2 as the target will be two bytes closer if we are
652 if ((long) value
< 0x8001 && (long) value
> -0x8000)
656 /* Get the opcode. */
657 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
659 if (code
!= 0xe0 && code
!= 0xe1)
662 /* Note that we've changed the relocs, section contents, etc. */
663 elf_section_data (sec
)->relocs
= internal_relocs
;
664 elf_section_data (sec
)->this_hdr
.contents
= contents
;
665 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
667 /* Fix the opcode. */
669 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
670 else if (code
== 0xe1)
671 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
673 /* Fix the relocation's type. */
674 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
677 /* The opcode got shorter too, so we have to fix the offset. */
680 /* Delete two bytes of data. */
681 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
682 irel
->r_offset
+ 1, 2))
685 /* That will change things, so, we should relax again.
686 Note that this is not required, and it may be slow. */
691 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
693 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
695 bfd_vma value
= symval
;
697 /* Deal with pc-relative gunk. */
698 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
699 value
-= (irel
->r_offset
+ 2);
700 value
+= irel
->r_addend
;
702 /* See if the value will fit in 8 bits, note the high value is
703 0x7f + 1 as the target will be one bytes closer if we are
705 if ((long) value
< 0x80 && (long) value
> -0x80)
709 /* Get the opcode. */
710 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
715 /* Note that we've changed the relocs, section contents, etc. */
716 elf_section_data (sec
)->relocs
= internal_relocs
;
717 elf_section_data (sec
)->this_hdr
.contents
= contents
;
718 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
720 /* Fix the opcode. */
721 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
723 /* Fix the relocation's type. */
724 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
727 /* Delete one byte of data. */
728 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
729 irel
->r_offset
+ 1, 1))
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 eliminate an unconditional 8 bit pc-relative branch
739 which immediately follows a conditional 8 bit pc-relative
740 branch around the unconditional branch.
747 This happens when the bCC can't reach lab2 at assembly time,
748 but due to other relaxations it can reach at link time. */
749 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
751 Elf_Internal_Rela
*nrel
;
752 bfd_vma value
= symval
;
755 /* Deal with pc-relative gunk. */
756 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
757 value
-= (irel
->r_offset
+ 1);
758 value
+= irel
->r_addend
;
760 /* Do nothing if this reloc is the last byte in the section. */
761 if (irel
->r_offset
== sec
->size
)
764 /* See if the next instruction is an unconditional pc-relative
765 branch, more often than not this test will fail, so we
766 test it first to speed things up. */
767 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
771 /* Also make sure the next relocation applies to the next
772 instruction and that it's a pc-relative 8 bit branch. */
775 || irel
->r_offset
+ 2 != nrel
->r_offset
776 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
779 /* Make sure our destination immediately follows the
780 unconditional branch. */
781 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
782 + irel
->r_offset
+ 3))
785 /* Now make sure we are a conditional branch. This may not
786 be necessary, but why take the chance.
788 Note these checks assume that R_MN10200_PCREL8 relocs
789 only occur on bCC and bCCx insns. If they occured
790 elsewhere, we'd need to know the start of this insn
791 for this check to be accurate. */
792 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
793 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
794 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
795 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
796 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
797 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
798 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
801 /* We also have to be sure there is no symbol/label
802 at the unconditional branch. */
803 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
807 /* Note that we've changed the relocs, section contents, etc. */
808 elf_section_data (sec
)->relocs
= internal_relocs
;
809 elf_section_data (sec
)->this_hdr
.contents
= contents
;
810 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
812 /* Reverse the condition of the first branch. */
870 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
872 /* Set the reloc type and symbol for the first branch
873 from the second branch. */
874 irel
->r_info
= nrel
->r_info
;
876 /* Make the reloc for the second branch a null reloc. */
877 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
880 /* Delete two bytes of data. */
881 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
882 irel
->r_offset
+ 1, 2))
885 /* That will change things, so, we should relax again.
886 Note that this is not required, and it may be slow. */
890 /* Try to turn a 24bit immediate, displacement or absolute address
891 into a 16bit immediate, displacement or absolute address. */
892 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
894 bfd_vma value
= symval
;
896 /* See if the value will fit in 16 bits.
897 We allow any 16bit match here. We prune those we can't
899 if ((long) value
< 0x7fff && (long) value
> -0x8000)
903 /* All insns which have 24bit operands are 5 bytes long,
904 the first byte will always be 0xf4, but we double check
907 /* Get the first opcode. */
908 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
913 /* Get the second opcode. */
914 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
918 /* mov imm24,dn -> mov imm16,dn */
920 /* Not safe if the high bit is on as relaxing may
921 move the value out of high mem and thus not fit
922 in a signed 16bit value. */
926 /* Note that we've changed the relocation contents, etc. */
927 elf_section_data (sec
)->relocs
= internal_relocs
;
928 elf_section_data (sec
)->this_hdr
.contents
= contents
;
929 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
931 /* Fix the opcode. */
932 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
933 contents
+ irel
->r_offset
- 2);
935 /* Fix the relocation's type. */
936 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
939 /* The opcode got shorter too, so we have to fix the
943 /* Delete two bytes of data. */
944 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
945 irel
->r_offset
+ 1, 2))
948 /* That will change things, so, we should relax again.
949 Note that this is not required, and it may be slow. */
953 /* mov imm24,an -> mov imm16,an
954 cmp imm24,an -> cmp imm16,an
955 mov (abs24),dn -> mov (abs16),dn
956 mov dn,(abs24) -> mov dn,(abs16)
957 movb dn,(abs24) -> movb dn,(abs16)
958 movbu (abs24),dn -> movbu (abs16),dn */
965 /* Note that we've changed the relocation contents, etc. */
966 elf_section_data (sec
)->relocs
= internal_relocs
;
967 elf_section_data (sec
)->this_hdr
.contents
= contents
;
968 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
970 if ((code
& 0xfc) == 0x74)
971 code
= 0xdc + (code
& 0x03);
972 else if ((code
& 0xfc) == 0x7c)
973 code
= 0xec + (code
& 0x03);
974 else if ((code
& 0xfc) == 0xc0)
975 code
= 0xc8 + (code
& 0x03);
976 else if ((code
& 0xfc) == 0x40)
977 code
= 0xc0 + (code
& 0x03);
978 else if ((code
& 0xfc) == 0x44)
979 code
= 0xc4 + (code
& 0x03);
980 else if ((code
& 0xfc) == 0xc8)
981 code
= 0xcc + (code
& 0x03);
983 /* Fix the opcode. */
984 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
986 /* Fix the relocation's type. */
987 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
990 /* The opcode got shorter too, so we have to fix the
994 /* Delete two bytes of data. */
995 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
996 irel
->r_offset
+ 1, 2))
999 /* That will change things, so, we should relax again.
1000 Note that this is not required, and it may be slow. */
1004 /* cmp imm24,dn -> cmp imm16,dn
1005 mov (abs24),an -> mov (abs16),an
1006 mov an,(abs24) -> mov an,(abs16)
1007 add imm24,dn -> add imm16,dn
1008 add imm24,an -> add imm16,an
1009 sub imm24,dn -> sub imm16,dn
1010 sub imm24,an -> sub imm16,an
1011 And all d24->d16 in memory ops. */
1028 /* Not safe if the high bit is on as relaxing may
1029 move the value out of high mem and thus not fit
1030 in a signed 16bit value. */
1031 if (((code
& 0xfc) == 0x78
1032 || (code
& 0xfc) == 0x60
1033 || (code
& 0xfc) == 0x64
1034 || (code
& 0xfc) == 0x68
1035 || (code
& 0xfc) == 0x6c
1036 || (code
& 0xfc) == 0x80
1037 || (code
& 0xfc) == 0xf0
1038 || (code
& 0xfc) == 0x00
1039 || (code
& 0xfc) == 0x10
1040 || (code
& 0xfc) == 0xb0
1041 || (code
& 0xfc) == 0x30
1042 || (code
& 0xfc) == 0xa0
1043 || (code
& 0xfc) == 0x20
1044 || (code
& 0xfc) == 0x90)
1045 && (value
& 0x8000) != 0)
1048 /* Note that we've changed the relocation contents, etc. */
1049 elf_section_data (sec
)->relocs
= internal_relocs
;
1050 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1051 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1053 /* Fix the opcode. */
1054 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1056 if ((code
& 0xfc) == 0x78)
1057 code
= 0x48 + (code
& 0x03);
1058 else if ((code
& 0xfc) == 0xd0)
1059 code
= 0x30 + (code
& 0x03);
1060 else if ((code
& 0xfc) == 0x50)
1061 code
= 0x20 + (code
& 0x03);
1062 else if ((code
& 0xfc) == 0x60)
1063 code
= 0x18 + (code
& 0x03);
1064 else if ((code
& 0xfc) == 0x64)
1065 code
= 0x08 + (code
& 0x03);
1066 else if ((code
& 0xfc) == 0x68)
1067 code
= 0x1c + (code
& 0x03);
1068 else if ((code
& 0xfc) == 0x6c)
1069 code
= 0x0c + (code
& 0x03);
1070 else if ((code
& 0xfc) == 0x80)
1071 code
= 0xc0 + (code
& 0x07);
1072 else if ((code
& 0xfc) == 0xf0)
1073 code
= 0xb0 + (code
& 0x07);
1074 else if ((code
& 0xfc) == 0x00)
1075 code
= 0x80 + (code
& 0x07);
1076 else if ((code
& 0xfc) == 0x10)
1077 code
= 0xa0 + (code
& 0x07);
1078 else if ((code
& 0xfc) == 0xb0)
1079 code
= 0x70 + (code
& 0x07);
1080 else if ((code
& 0xfc) == 0x30)
1081 code
= 0x60 + (code
& 0x07);
1082 else if ((code
& 0xfc) == 0xa0)
1083 code
= 0xd0 + (code
& 0x07);
1084 else if ((code
& 0xfc) == 0x20)
1085 code
= 0x90 + (code
& 0x07);
1086 else if ((code
& 0xfc) == 0x90)
1087 code
= 0x50 + (code
& 0x07);
1089 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1091 /* Fix the relocation's type. */
1092 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1095 /* Delete one bytes of data. */
1096 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1097 irel
->r_offset
+ 2, 1))
1100 /* That will change things, so, we should relax again.
1101 Note that this is not required, and it may be slow. */
1105 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1107 /* Note that we've changed the reldection contents, etc. */
1108 elf_section_data (sec
)->relocs
= internal_relocs
;
1109 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1110 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1112 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1113 contents
+ irel
->r_offset
- 2);
1115 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1116 contents
+ irel
->r_offset
- 1);
1118 /* Fix the relocation's type. */
1119 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1122 /* The reloc will be applied one byte in front of its
1123 current location. */
1124 irel
->r_offset
-= 1;
1126 /* Delete one bytes of data. */
1127 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1128 irel
->r_offset
+ 2, 1))
1131 /* That will change things, so, we should relax again.
1132 Note that this is not required, and it may be slow. */
1141 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1143 if (! link_info
->keep_memory
)
1147 /* Cache the symbols for elf_link_input_bfd. */
1148 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1152 if (contents
!= NULL
1153 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1155 if (! link_info
->keep_memory
)
1159 /* Cache the section contents for elf_link_input_bfd. */
1160 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1164 if (internal_relocs
!= NULL
1165 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1166 free (internal_relocs
);
1172 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1174 if (contents
!= NULL
1175 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1177 if (internal_relocs
!= NULL
1178 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1179 free (internal_relocs
);
1184 /* Delete some bytes from a section while relaxing. */
1187 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1193 Elf_Internal_Shdr
*symtab_hdr
;
1194 unsigned int sec_shndx
;
1196 Elf_Internal_Rela
*irel
, *irelend
;
1198 Elf_Internal_Sym
*isym
;
1199 Elf_Internal_Sym
*isymend
;
1200 struct elf_link_hash_entry
**sym_hashes
;
1201 struct elf_link_hash_entry
**end_hashes
;
1202 unsigned int symcount
;
1204 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1206 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1210 irel
= elf_section_data (sec
)->relocs
;
1211 irelend
= irel
+ sec
->reloc_count
;
1213 /* Actually delete the bytes. */
1214 memmove (contents
+ addr
, contents
+ addr
+ count
,
1215 (size_t) (toaddr
- addr
- count
));
1218 /* Adjust all the relocs. */
1219 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1221 /* Get the new reloc address. */
1222 if ((irel
->r_offset
> addr
1223 && irel
->r_offset
< toaddr
))
1224 irel
->r_offset
-= count
;
1227 /* Adjust the local symbols defined in this section. */
1228 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1229 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1230 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1232 if (isym
->st_shndx
== sec_shndx
1233 && isym
->st_value
> addr
1234 && isym
->st_value
< toaddr
)
1235 isym
->st_value
-= count
;
1238 /* Now adjust the global symbols defined in this section. */
1239 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1240 - symtab_hdr
->sh_info
);
1241 sym_hashes
= elf_sym_hashes (abfd
);
1242 end_hashes
= sym_hashes
+ symcount
;
1243 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1245 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1246 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1247 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1248 && sym_hash
->root
.u
.def
.section
== sec
1249 && sym_hash
->root
.u
.def
.value
> addr
1250 && sym_hash
->root
.u
.def
.value
< toaddr
)
1252 sym_hash
->root
.u
.def
.value
-= count
;
1259 /* Return TRUE if a symbol exists at the given address, else return
1262 mn10200_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
1265 Elf_Internal_Sym
*isym
;
1268 Elf_Internal_Shdr
*symtab_hdr
;
1269 unsigned int sec_shndx
;
1270 Elf_Internal_Sym
*isymend
;
1271 struct elf_link_hash_entry
**sym_hashes
;
1272 struct elf_link_hash_entry
**end_hashes
;
1273 unsigned int symcount
;
1275 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1277 /* Examine all the local symbols. */
1278 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1279 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1281 if (isym
->st_shndx
== sec_shndx
1282 && isym
->st_value
== addr
)
1286 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1287 - symtab_hdr
->sh_info
);
1288 sym_hashes
= elf_sym_hashes (abfd
);
1289 end_hashes
= sym_hashes
+ symcount
;
1290 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1292 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1293 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1294 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1295 && sym_hash
->root
.u
.def
.section
== sec
1296 && sym_hash
->root
.u
.def
.value
== addr
)
1303 /* This is a version of bfd_generic_get_relocated_section_contents
1304 which uses mn10200_elf_relocate_section. */
1307 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1308 data
, relocatable
, symbols
)
1310 struct bfd_link_info
*link_info
;
1311 struct bfd_link_order
*link_order
;
1313 bfd_boolean relocatable
;
1316 Elf_Internal_Shdr
*symtab_hdr
;
1317 asection
*input_section
= link_order
->u
.indirect
.section
;
1318 bfd
*input_bfd
= input_section
->owner
;
1319 asection
**sections
= NULL
;
1320 Elf_Internal_Rela
*internal_relocs
= NULL
;
1321 Elf_Internal_Sym
*isymbuf
= NULL
;
1323 /* We only need to handle the case of relaxing, or of having a
1324 particular set of section contents, specially. */
1326 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1327 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1332 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1334 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1335 (size_t) input_section
->size
);
1337 if ((input_section
->flags
& SEC_RELOC
) != 0
1338 && input_section
->reloc_count
> 0)
1340 Elf_Internal_Sym
*isym
;
1341 Elf_Internal_Sym
*isymend
;
1345 internal_relocs
= (_bfd_elf_link_read_relocs
1346 (input_bfd
, input_section
, (PTR
) NULL
,
1347 (Elf_Internal_Rela
*) NULL
, FALSE
));
1348 if (internal_relocs
== NULL
)
1351 if (symtab_hdr
->sh_info
!= 0)
1353 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1354 if (isymbuf
== NULL
)
1355 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1356 symtab_hdr
->sh_info
, 0,
1358 if (isymbuf
== NULL
)
1362 amt
= symtab_hdr
->sh_info
;
1363 amt
*= sizeof (asection
*);
1364 sections
= (asection
**) bfd_malloc (amt
);
1365 if (sections
== NULL
&& amt
!= 0)
1368 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1369 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1373 if (isym
->st_shndx
== SHN_UNDEF
)
1374 isec
= bfd_und_section_ptr
;
1375 else if (isym
->st_shndx
== SHN_ABS
)
1376 isec
= bfd_abs_section_ptr
;
1377 else if (isym
->st_shndx
== SHN_COMMON
)
1378 isec
= bfd_com_section_ptr
;
1380 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1385 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1386 input_section
, data
, internal_relocs
,
1390 if (sections
!= NULL
)
1393 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1395 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1396 free (internal_relocs
);
1402 if (sections
!= NULL
)
1405 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1407 if (internal_relocs
!= NULL
1408 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1409 free (internal_relocs
);
1413 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1414 #define TARGET_LITTLE_NAME "elf32-mn10200"
1415 #define ELF_ARCH bfd_arch_mn10200
1416 #define ELF_MACHINE_CODE EM_MN10200
1417 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1418 #define ELF_MAXPAGESIZE 0x1000
1420 #define elf_backend_rela_normal 1
1421 #define elf_info_to_howto mn10200_info_to_howto
1422 #define elf_info_to_howto_rel 0
1423 #define elf_backend_relocate_section mn10200_elf_relocate_section
1424 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1425 #define bfd_elf32_bfd_get_relocated_section_contents \
1426 mn10200_elf_get_relocated_section_contents
1428 #define elf_symbol_leading_char '_'
1430 #include "elf32-target.h"