1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
26 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
28 static void mn10200_info_to_howto
29 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
30 static bfd_boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
32 static bfd_boolean mn10200_elf_symbol_address_p
33 PARAMS ((bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
));
34 static bfd_reloc_status_type mn10200_elf_final_link_relocate
35 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*,
36 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
37 struct bfd_link_info
*, asection
*, int));
38 static bfd_boolean mn10200_elf_relocate_section
39 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
40 bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*,
42 static bfd_boolean mn10200_elf_relax_section
43 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
44 static bfd_byte
* mn10200_elf_get_relocated_section_contents
45 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
46 bfd_byte
*, bfd_boolean
, asymbol
**));
60 static reloc_howto_type elf_mn10200_howto_table
[] = {
61 /* Dummy relocation. Does nothing. */
62 HOWTO (R_MN10200_NONE
,
68 complain_overflow_bitfield
,
69 bfd_elf_generic_reloc
,
75 /* Standard 32 bit reloc. */
82 complain_overflow_bitfield
,
83 bfd_elf_generic_reloc
,
89 /* Standard 16 bit reloc. */
96 complain_overflow_bitfield
,
97 bfd_elf_generic_reloc
,
103 /* Standard 8 bit reloc. */
110 complain_overflow_bitfield
,
111 bfd_elf_generic_reloc
,
117 /* Standard 24 bit reloc. */
124 complain_overflow_bitfield
,
125 bfd_elf_generic_reloc
,
131 /* Simple 8 pc-relative reloc. */
132 HOWTO (R_MN10200_PCREL8
,
138 complain_overflow_bitfield
,
139 bfd_elf_generic_reloc
,
145 /* Simple 16 pc-relative reloc. */
146 HOWTO (R_MN10200_PCREL16
,
152 complain_overflow_bitfield
,
153 bfd_elf_generic_reloc
,
159 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
160 to get the pc-relative offset correct. */
161 HOWTO (R_MN10200_PCREL24
,
167 complain_overflow_bitfield
,
168 bfd_elf_generic_reloc
,
176 struct mn10200_reloc_map
{
177 bfd_reloc_code_real_type bfd_reloc_val
;
178 unsigned char elf_reloc_val
;
181 static const struct mn10200_reloc_map mn10200_reloc_map
[] = {
182 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
183 { BFD_RELOC_32
, R_MN10200_32
, },
184 { BFD_RELOC_16
, R_MN10200_16
, },
185 { BFD_RELOC_8
, R_MN10200_8
, },
186 { BFD_RELOC_24
, R_MN10200_24
, },
187 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
188 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
189 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
192 static reloc_howto_type
*
193 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
194 bfd
*abfd ATTRIBUTE_UNUSED
;
195 bfd_reloc_code_real_type code
;
200 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
203 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
204 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
210 static reloc_howto_type
*
211 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
217 i
< (sizeof (elf_mn10200_howto_table
)
218 / sizeof (elf_mn10200_howto_table
[0]));
220 if (elf_mn10200_howto_table
[i
].name
!= NULL
221 && strcasecmp (elf_mn10200_howto_table
[i
].name
, r_name
) == 0)
222 return &elf_mn10200_howto_table
[i
];
227 /* Set the howto pointer for an MN10200 ELF reloc. */
230 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
231 bfd
*abfd ATTRIBUTE_UNUSED
;
233 Elf_Internal_Rela
*dst
;
237 r_type
= ELF32_R_TYPE (dst
->r_info
);
238 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
239 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
242 /* Perform a relocation as part of a final link. */
244 static bfd_reloc_status_type
245 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
246 input_section
, contents
, offset
, value
,
247 addend
, info
, sym_sec
, is_local
)
248 reloc_howto_type
*howto
;
250 bfd
*output_bfd ATTRIBUTE_UNUSED
;
251 asection
*input_section
;
256 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
257 asection
*sym_sec ATTRIBUTE_UNUSED
;
258 int is_local ATTRIBUTE_UNUSED
;
260 unsigned long r_type
= howto
->type
;
261 bfd_byte
*hit_data
= contents
+ offset
;
271 bfd_put_32 (input_bfd
, value
, hit_data
);
277 if ((long) value
> 0x7fff || (long) value
< -0x8000)
278 return bfd_reloc_overflow
;
280 bfd_put_16 (input_bfd
, value
, hit_data
);
286 if ((long) value
> 0x7f || (long) value
< -0x80)
287 return bfd_reloc_overflow
;
289 bfd_put_8 (input_bfd
, value
, hit_data
);
295 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
296 return bfd_reloc_overflow
;
299 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
300 bfd_put_32 (input_bfd
, value
, hit_data
);
303 case R_MN10200_PCREL8
:
304 value
-= (input_section
->output_section
->vma
305 + input_section
->output_offset
);
306 value
-= (offset
+ 1);
309 if ((long) value
> 0xff || (long) value
< -0x100)
310 return bfd_reloc_overflow
;
312 bfd_put_8 (input_bfd
, value
, hit_data
);
315 case R_MN10200_PCREL16
:
316 value
-= (input_section
->output_section
->vma
317 + input_section
->output_offset
);
318 value
-= (offset
+ 2);
321 if ((long) value
> 0xffff || (long) value
< -0x10000)
322 return bfd_reloc_overflow
;
324 bfd_put_16 (input_bfd
, value
, hit_data
);
327 case R_MN10200_PCREL24
:
328 value
-= (input_section
->output_section
->vma
329 + input_section
->output_offset
);
330 value
-= (offset
+ 3);
333 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
334 return bfd_reloc_overflow
;
337 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
338 bfd_put_32 (input_bfd
, value
, hit_data
);
342 return bfd_reloc_notsupported
;
346 /* Relocate an MN10200 ELF section. */
348 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
349 contents
, relocs
, local_syms
, local_sections
)
351 struct bfd_link_info
*info
;
353 asection
*input_section
;
355 Elf_Internal_Rela
*relocs
;
356 Elf_Internal_Sym
*local_syms
;
357 asection
**local_sections
;
359 Elf_Internal_Shdr
*symtab_hdr
;
360 struct elf_link_hash_entry
**sym_hashes
;
361 Elf_Internal_Rela
*rel
, *relend
;
363 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
364 sym_hashes
= elf_sym_hashes (input_bfd
);
367 relend
= relocs
+ input_section
->reloc_count
;
368 for (; rel
< relend
; rel
++)
371 reloc_howto_type
*howto
;
372 unsigned long r_symndx
;
373 Elf_Internal_Sym
*sym
;
375 struct elf_link_hash_entry
*h
;
377 bfd_reloc_status_type r
;
379 r_symndx
= ELF32_R_SYM (rel
->r_info
);
380 r_type
= ELF32_R_TYPE (rel
->r_info
);
381 howto
= elf_mn10200_howto_table
+ r_type
;
386 if (r_symndx
< symtab_hdr
->sh_info
)
388 sym
= local_syms
+ r_symndx
;
389 sec
= local_sections
[r_symndx
];
390 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
394 bfd_boolean unresolved_reloc
, warned
;
396 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
397 r_symndx
, symtab_hdr
, sym_hashes
,
399 unresolved_reloc
, warned
);
402 if (sec
!= NULL
&& elf_discarded_section (sec
))
404 /* For relocs against symbols from removed linkonce sections,
405 or sections discarded by a linker script, we just want the
406 section contents zeroed. Avoid any special processing. */
407 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
413 if (info
->relocatable
)
416 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
418 contents
, rel
->r_offset
,
419 relocation
, rel
->r_addend
,
420 info
, sec
, h
== NULL
);
422 if (r
!= bfd_reloc_ok
)
425 const char *msg
= (const char *) 0;
428 name
= h
->root
.root
.string
;
431 name
= (bfd_elf_string_from_elf_section
432 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
433 if (name
== NULL
|| *name
== '\0')
434 name
= bfd_section_name (input_bfd
, sec
);
439 case bfd_reloc_overflow
:
440 if (! ((*info
->callbacks
->reloc_overflow
)
441 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
442 (bfd_vma
) 0, input_bfd
, input_section
,
447 case bfd_reloc_undefined
:
448 if (! ((*info
->callbacks
->undefined_symbol
)
449 (info
, name
, input_bfd
, input_section
,
450 rel
->r_offset
, TRUE
)))
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 are quite a few relaxing opportunities 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
*irel
, *irelend
;
522 bfd_byte
*contents
= NULL
;
523 Elf_Internal_Sym
*isymbuf
= NULL
;
525 /* Assume nothing changes. */
528 /* We don't have to do anything for a relocatable link, if
529 this section does not have relocs, or if this is not a
531 if (link_info
->relocatable
532 || (sec
->flags
& SEC_RELOC
) == 0
533 || sec
->reloc_count
== 0
534 || (sec
->flags
& SEC_CODE
) == 0)
537 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
539 /* Get a copy of the native relocations. */
540 internal_relocs
= (_bfd_elf_link_read_relocs
541 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
542 link_info
->keep_memory
));
543 if (internal_relocs
== NULL
)
546 /* Walk through them looking for relaxing opportunities. */
547 irelend
= internal_relocs
+ sec
->reloc_count
;
548 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
552 /* If this isn't something that can be relaxed, then ignore
554 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
555 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
556 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
559 /* Get the section contents if we haven't done so already. */
560 if (contents
== NULL
)
562 /* Get cached copy if it exists. */
563 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
564 contents
= elf_section_data (sec
)->this_hdr
.contents
;
567 /* Go get them off disk. */
568 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
573 /* Read this BFD's local symbols if we haven't done so already. */
574 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
576 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
578 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
579 symtab_hdr
->sh_info
, 0,
585 /* Get the value of the symbol referred to by the reloc. */
586 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
588 /* A local symbol. */
589 Elf_Internal_Sym
*isym
;
592 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
593 if (isym
->st_shndx
== SHN_UNDEF
)
594 sym_sec
= bfd_und_section_ptr
;
595 else if (isym
->st_shndx
== SHN_ABS
)
596 sym_sec
= bfd_abs_section_ptr
;
597 else if (isym
->st_shndx
== SHN_COMMON
)
598 sym_sec
= bfd_com_section_ptr
;
600 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
601 symval
= (isym
->st_value
602 + sym_sec
->output_section
->vma
603 + sym_sec
->output_offset
);
608 struct elf_link_hash_entry
*h
;
610 /* An external symbol. */
611 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
612 h
= elf_sym_hashes (abfd
)[indx
];
613 BFD_ASSERT (h
!= NULL
);
614 if (h
->root
.type
!= bfd_link_hash_defined
615 && h
->root
.type
!= bfd_link_hash_defweak
)
617 /* This appears to be a reference to an undefined
618 symbol. Just ignore it--it will be caught by the
619 regular reloc processing. */
623 symval
= (h
->root
.u
.def
.value
624 + h
->root
.u
.def
.section
->output_section
->vma
625 + h
->root
.u
.def
.section
->output_offset
);
628 /* For simplicity of coding, we are going to modify the section
629 contents, the section relocs, and the BFD symbol table. We
630 must tell the rest of the code not to free up this
631 information. It would be possible to instead create a table
632 of changes which have to be made, as is done in coff-mips.c;
633 that would be more work, but would require less memory when
634 the linker is run. */
636 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
638 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
640 bfd_vma value
= symval
;
642 /* Deal with pc-relative gunk. */
643 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
644 value
-= (irel
->r_offset
+ 3);
645 value
+= irel
->r_addend
;
647 /* See if the value will fit in 16 bits, note the high value is
648 0x7fff + 2 as the target will be two bytes closer if we are
650 if ((long) value
< 0x8001 && (long) value
> -0x8000)
654 /* Get the opcode. */
655 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
657 if (code
!= 0xe0 && code
!= 0xe1)
660 /* Note that we've changed the relocs, section contents, etc. */
661 elf_section_data (sec
)->relocs
= internal_relocs
;
662 elf_section_data (sec
)->this_hdr
.contents
= contents
;
663 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
665 /* Fix the opcode. */
667 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
668 else if (code
== 0xe1)
669 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
671 /* Fix the relocation's type. */
672 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
675 /* The opcode got shorter too, so we have to fix the offset. */
678 /* Delete two bytes of data. */
679 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
680 irel
->r_offset
+ 1, 2))
683 /* That will change things, so, we should relax again.
684 Note that this is not required, and it may be slow. */
689 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
691 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
693 bfd_vma value
= symval
;
695 /* Deal with pc-relative gunk. */
696 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
697 value
-= (irel
->r_offset
+ 2);
698 value
+= irel
->r_addend
;
700 /* See if the value will fit in 8 bits, note the high value is
701 0x7f + 1 as the target will be one bytes closer if we are
703 if ((long) value
< 0x80 && (long) value
> -0x80)
707 /* Get the opcode. */
708 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
713 /* Note that we've changed the relocs, section contents, etc. */
714 elf_section_data (sec
)->relocs
= internal_relocs
;
715 elf_section_data (sec
)->this_hdr
.contents
= contents
;
716 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
718 /* Fix the opcode. */
719 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
721 /* Fix the relocation's type. */
722 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
725 /* Delete one byte of data. */
726 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
727 irel
->r_offset
+ 1, 1))
730 /* That will change things, so, we should relax again.
731 Note that this is not required, and it may be slow. */
736 /* Try to eliminate an unconditional 8 bit pc-relative branch
737 which immediately follows a conditional 8 bit pc-relative
738 branch around the unconditional branch.
745 This happens when the bCC can't reach lab2 at assembly time,
746 but due to other relaxations it can reach at link time. */
747 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
749 Elf_Internal_Rela
*nrel
;
750 bfd_vma value
= symval
;
753 /* Deal with pc-relative gunk. */
754 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
755 value
-= (irel
->r_offset
+ 1);
756 value
+= irel
->r_addend
;
758 /* Do nothing if this reloc is the last byte in the section. */
759 if (irel
->r_offset
== sec
->size
)
762 /* See if the next instruction is an unconditional pc-relative
763 branch, more often than not this test will fail, so we
764 test it first to speed things up. */
765 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
769 /* Also make sure the next relocation applies to the next
770 instruction and that it's a pc-relative 8 bit branch. */
773 || irel
->r_offset
+ 2 != nrel
->r_offset
774 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
777 /* Make sure our destination immediately follows the
778 unconditional branch. */
779 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
780 + irel
->r_offset
+ 3))
783 /* Now make sure we are a conditional branch. This may not
784 be necessary, but why take the chance.
786 Note these checks assume that R_MN10200_PCREL8 relocs
787 only occur on bCC and bCCx insns. If they occured
788 elsewhere, we'd need to know the start of this insn
789 for this check to be accurate. */
790 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
791 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
792 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
793 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
794 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
795 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
796 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
799 /* We also have to be sure there is no symbol/label
800 at the unconditional branch. */
801 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
805 /* Note that we've changed the relocs, section contents, etc. */
806 elf_section_data (sec
)->relocs
= internal_relocs
;
807 elf_section_data (sec
)->this_hdr
.contents
= contents
;
808 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
810 /* Reverse the condition of the first branch. */
868 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
870 /* Set the reloc type and symbol for the first branch
871 from the second branch. */
872 irel
->r_info
= nrel
->r_info
;
874 /* Make the reloc for the second branch a null reloc. */
875 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
878 /* Delete two bytes of data. */
879 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
880 irel
->r_offset
+ 1, 2))
883 /* That will change things, so, we should relax again.
884 Note that this is not required, and it may be slow. */
888 /* Try to turn a 24bit immediate, displacement or absolute address
889 into a 16bit immediate, displacement or absolute address. */
890 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
892 bfd_vma value
= symval
;
894 /* See if the value will fit in 16 bits.
895 We allow any 16bit match here. We prune those we can't
897 if ((long) value
< 0x7fff && (long) value
> -0x8000)
901 /* All insns which have 24bit operands are 5 bytes long,
902 the first byte will always be 0xf4, but we double check
905 /* Get the first opcode. */
906 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
911 /* Get the second opcode. */
912 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
916 /* mov imm24,dn -> mov imm16,dn */
918 /* Not safe if the high bit is on as relaxing may
919 move the value out of high mem and thus not fit
920 in a signed 16bit value. */
924 /* Note that we've changed the relocation contents, etc. */
925 elf_section_data (sec
)->relocs
= internal_relocs
;
926 elf_section_data (sec
)->this_hdr
.contents
= contents
;
927 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
929 /* Fix the opcode. */
930 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
931 contents
+ irel
->r_offset
- 2);
933 /* Fix the relocation's type. */
934 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
937 /* The opcode got shorter too, so we have to fix the
941 /* Delete two bytes of data. */
942 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
943 irel
->r_offset
+ 1, 2))
946 /* That will change things, so, we should relax again.
947 Note that this is not required, and it may be slow. */
951 /* mov imm24,an -> mov imm16,an
952 cmp imm24,an -> cmp imm16,an
953 mov (abs24),dn -> mov (abs16),dn
954 mov dn,(abs24) -> mov dn,(abs16)
955 movb dn,(abs24) -> movb dn,(abs16)
956 movbu (abs24),dn -> movbu (abs16),dn */
963 /* Note that we've changed the relocation contents, etc. */
964 elf_section_data (sec
)->relocs
= internal_relocs
;
965 elf_section_data (sec
)->this_hdr
.contents
= contents
;
966 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
968 if ((code
& 0xfc) == 0x74)
969 code
= 0xdc + (code
& 0x03);
970 else if ((code
& 0xfc) == 0x7c)
971 code
= 0xec + (code
& 0x03);
972 else if ((code
& 0xfc) == 0xc0)
973 code
= 0xc8 + (code
& 0x03);
974 else if ((code
& 0xfc) == 0x40)
975 code
= 0xc0 + (code
& 0x03);
976 else if ((code
& 0xfc) == 0x44)
977 code
= 0xc4 + (code
& 0x03);
978 else if ((code
& 0xfc) == 0xc8)
979 code
= 0xcc + (code
& 0x03);
981 /* Fix the opcode. */
982 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
984 /* Fix the relocation's type. */
985 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
988 /* The opcode got shorter too, so we have to fix the
992 /* Delete two bytes of data. */
993 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
994 irel
->r_offset
+ 1, 2))
997 /* That will change things, so, we should relax again.
998 Note that this is not required, and it may be slow. */
1002 /* cmp imm24,dn -> cmp imm16,dn
1003 mov (abs24),an -> mov (abs16),an
1004 mov an,(abs24) -> mov an,(abs16)
1005 add imm24,dn -> add imm16,dn
1006 add imm24,an -> add imm16,an
1007 sub imm24,dn -> sub imm16,dn
1008 sub imm24,an -> sub imm16,an
1009 And all d24->d16 in memory ops. */
1026 /* Not safe if the high bit is on as relaxing may
1027 move the value out of high mem and thus not fit
1028 in a signed 16bit value. */
1029 if (((code
& 0xfc) == 0x78
1030 || (code
& 0xfc) == 0x60
1031 || (code
& 0xfc) == 0x64
1032 || (code
& 0xfc) == 0x68
1033 || (code
& 0xfc) == 0x6c
1034 || (code
& 0xfc) == 0x80
1035 || (code
& 0xfc) == 0xf0
1036 || (code
& 0xfc) == 0x00
1037 || (code
& 0xfc) == 0x10
1038 || (code
& 0xfc) == 0xb0
1039 || (code
& 0xfc) == 0x30
1040 || (code
& 0xfc) == 0xa0
1041 || (code
& 0xfc) == 0x20
1042 || (code
& 0xfc) == 0x90)
1043 && (value
& 0x8000) != 0)
1046 /* Note that we've changed the relocation contents, etc. */
1047 elf_section_data (sec
)->relocs
= internal_relocs
;
1048 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1049 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1051 /* Fix the opcode. */
1052 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1054 if ((code
& 0xfc) == 0x78)
1055 code
= 0x48 + (code
& 0x03);
1056 else if ((code
& 0xfc) == 0xd0)
1057 code
= 0x30 + (code
& 0x03);
1058 else if ((code
& 0xfc) == 0x50)
1059 code
= 0x20 + (code
& 0x03);
1060 else if ((code
& 0xfc) == 0x60)
1061 code
= 0x18 + (code
& 0x03);
1062 else if ((code
& 0xfc) == 0x64)
1063 code
= 0x08 + (code
& 0x03);
1064 else if ((code
& 0xfc) == 0x68)
1065 code
= 0x1c + (code
& 0x03);
1066 else if ((code
& 0xfc) == 0x6c)
1067 code
= 0x0c + (code
& 0x03);
1068 else if ((code
& 0xfc) == 0x80)
1069 code
= 0xc0 + (code
& 0x07);
1070 else if ((code
& 0xfc) == 0xf0)
1071 code
= 0xb0 + (code
& 0x07);
1072 else if ((code
& 0xfc) == 0x00)
1073 code
= 0x80 + (code
& 0x07);
1074 else if ((code
& 0xfc) == 0x10)
1075 code
= 0xa0 + (code
& 0x07);
1076 else if ((code
& 0xfc) == 0xb0)
1077 code
= 0x70 + (code
& 0x07);
1078 else if ((code
& 0xfc) == 0x30)
1079 code
= 0x60 + (code
& 0x07);
1080 else if ((code
& 0xfc) == 0xa0)
1081 code
= 0xd0 + (code
& 0x07);
1082 else if ((code
& 0xfc) == 0x20)
1083 code
= 0x90 + (code
& 0x07);
1084 else if ((code
& 0xfc) == 0x90)
1085 code
= 0x50 + (code
& 0x07);
1087 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1089 /* Fix the relocation's type. */
1090 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1093 /* Delete one bytes of data. */
1094 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1095 irel
->r_offset
+ 2, 1))
1098 /* That will change things, so, we should relax again.
1099 Note that this is not required, and it may be slow. */
1103 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1105 /* Note that we've changed the reldection contents, etc. */
1106 elf_section_data (sec
)->relocs
= internal_relocs
;
1107 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1108 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1110 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1111 contents
+ irel
->r_offset
- 2);
1113 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1114 contents
+ irel
->r_offset
- 1);
1116 /* Fix the relocation's type. */
1117 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1120 /* The reloc will be applied one byte in front of its
1121 current location. */
1122 irel
->r_offset
-= 1;
1124 /* Delete one bytes of data. */
1125 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1126 irel
->r_offset
+ 2, 1))
1129 /* That will change things, so, we should relax again.
1130 Note that this is not required, and it may be slow. */
1139 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1141 if (! link_info
->keep_memory
)
1145 /* Cache the symbols for elf_link_input_bfd. */
1146 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1150 if (contents
!= NULL
1151 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1153 if (! link_info
->keep_memory
)
1157 /* Cache the section contents for elf_link_input_bfd. */
1158 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1162 if (internal_relocs
!= NULL
1163 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1164 free (internal_relocs
);
1170 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1172 if (contents
!= NULL
1173 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1175 if (internal_relocs
!= NULL
1176 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1177 free (internal_relocs
);
1182 /* Delete some bytes from a section while relaxing. */
1185 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1191 Elf_Internal_Shdr
*symtab_hdr
;
1192 unsigned int sec_shndx
;
1194 Elf_Internal_Rela
*irel
, *irelend
;
1195 Elf_Internal_Rela
*irelalign
;
1197 Elf_Internal_Sym
*isym
;
1198 Elf_Internal_Sym
*isymend
;
1199 struct elf_link_hash_entry
**sym_hashes
;
1200 struct elf_link_hash_entry
**end_hashes
;
1201 unsigned int symcount
;
1203 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1205 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1207 /* The deletion must stop at the next ALIGN reloc for an aligment
1208 power larger than the number of bytes we are deleting. */
1213 irel
= elf_section_data (sec
)->relocs
;
1214 irelend
= irel
+ sec
->reloc_count
;
1216 /* Actually delete the bytes. */
1217 memmove (contents
+ addr
, contents
+ addr
+ count
,
1218 (size_t) (toaddr
- addr
- count
));
1221 /* Adjust all the relocs. */
1222 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1224 /* Get the new reloc address. */
1225 if ((irel
->r_offset
> addr
1226 && irel
->r_offset
< toaddr
))
1227 irel
->r_offset
-= count
;
1230 /* Adjust the local symbols defined in this section. */
1231 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1232 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1233 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1235 if (isym
->st_shndx
== sec_shndx
1236 && isym
->st_value
> addr
1237 && isym
->st_value
< toaddr
)
1238 isym
->st_value
-= count
;
1241 /* Now adjust the global symbols defined in this section. */
1242 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1243 - symtab_hdr
->sh_info
);
1244 sym_hashes
= elf_sym_hashes (abfd
);
1245 end_hashes
= sym_hashes
+ symcount
;
1246 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1248 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1249 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1250 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1251 && sym_hash
->root
.u
.def
.section
== sec
1252 && sym_hash
->root
.u
.def
.value
> addr
1253 && sym_hash
->root
.u
.def
.value
< toaddr
)
1255 sym_hash
->root
.u
.def
.value
-= count
;
1262 /* Return TRUE if a symbol exists at the given address, else return
1265 mn10200_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
1268 Elf_Internal_Sym
*isym
;
1271 Elf_Internal_Shdr
*symtab_hdr
;
1272 unsigned int sec_shndx
;
1273 Elf_Internal_Sym
*isymend
;
1274 struct elf_link_hash_entry
**sym_hashes
;
1275 struct elf_link_hash_entry
**end_hashes
;
1276 unsigned int symcount
;
1278 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1280 /* Examine all the local symbols. */
1281 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1282 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1284 if (isym
->st_shndx
== sec_shndx
1285 && isym
->st_value
== addr
)
1289 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1290 - symtab_hdr
->sh_info
);
1291 sym_hashes
= elf_sym_hashes (abfd
);
1292 end_hashes
= sym_hashes
+ symcount
;
1293 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1295 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1296 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1297 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1298 && sym_hash
->root
.u
.def
.section
== sec
1299 && sym_hash
->root
.u
.def
.value
== addr
)
1306 /* This is a version of bfd_generic_get_relocated_section_contents
1307 which uses mn10200_elf_relocate_section. */
1310 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1311 data
, relocatable
, symbols
)
1313 struct bfd_link_info
*link_info
;
1314 struct bfd_link_order
*link_order
;
1316 bfd_boolean relocatable
;
1319 Elf_Internal_Shdr
*symtab_hdr
;
1320 asection
*input_section
= link_order
->u
.indirect
.section
;
1321 bfd
*input_bfd
= input_section
->owner
;
1322 asection
**sections
= NULL
;
1323 Elf_Internal_Rela
*internal_relocs
= NULL
;
1324 Elf_Internal_Sym
*isymbuf
= NULL
;
1326 /* We only need to handle the case of relaxing, or of having a
1327 particular set of section contents, specially. */
1329 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1330 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1335 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1337 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1338 (size_t) input_section
->size
);
1340 if ((input_section
->flags
& SEC_RELOC
) != 0
1341 && input_section
->reloc_count
> 0)
1343 Elf_Internal_Sym
*isym
;
1344 Elf_Internal_Sym
*isymend
;
1348 internal_relocs
= (_bfd_elf_link_read_relocs
1349 (input_bfd
, input_section
, (PTR
) NULL
,
1350 (Elf_Internal_Rela
*) NULL
, FALSE
));
1351 if (internal_relocs
== NULL
)
1354 if (symtab_hdr
->sh_info
!= 0)
1356 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1357 if (isymbuf
== NULL
)
1358 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1359 symtab_hdr
->sh_info
, 0,
1361 if (isymbuf
== NULL
)
1365 amt
= symtab_hdr
->sh_info
;
1366 amt
*= sizeof (asection
*);
1367 sections
= (asection
**) bfd_malloc (amt
);
1368 if (sections
== NULL
&& amt
!= 0)
1371 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1372 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1376 if (isym
->st_shndx
== SHN_UNDEF
)
1377 isec
= bfd_und_section_ptr
;
1378 else if (isym
->st_shndx
== SHN_ABS
)
1379 isec
= bfd_abs_section_ptr
;
1380 else if (isym
->st_shndx
== SHN_COMMON
)
1381 isec
= bfd_com_section_ptr
;
1383 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1388 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1389 input_section
, data
, internal_relocs
,
1393 if (sections
!= NULL
)
1396 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1398 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1399 free (internal_relocs
);
1405 if (sections
!= NULL
)
1408 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1410 if (internal_relocs
!= NULL
1411 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1412 free (internal_relocs
);
1416 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1417 #define TARGET_LITTLE_NAME "elf32-mn10200"
1418 #define ELF_ARCH bfd_arch_mn10200
1419 #define ELF_MACHINE_CODE EM_MN10200
1420 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1421 #define ELF_MAXPAGESIZE 0x1000
1423 #define elf_backend_rela_normal 1
1424 #define elf_info_to_howto mn10200_info_to_howto
1425 #define elf_info_to_howto_rel 0
1426 #define elf_backend_relocate_section mn10200_elf_relocate_section
1427 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1428 #define bfd_elf32_bfd_get_relocated_section_contents \
1429 mn10200_elf_get_relocated_section_contents
1431 #define elf_symbol_leading_char '_'
1433 #include "elf32-target.h"