1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
*, Elf32_Internal_Rela
*));
30 static boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
32 static 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 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 boolean mn10200_elf_relax_section
43 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, 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
*, boolean
, asymbol
**));
48 /* We have to use RELA instructions since md_apply_fix3 in the assembler
49 does absolutely nothing. */
64 static reloc_howto_type elf_mn10200_howto_table
[] = {
65 /* Dummy relocation. Does nothing. */
66 HOWTO (R_MN10200_NONE
,
72 complain_overflow_bitfield
,
73 bfd_elf_generic_reloc
,
79 /* Standard 32 bit reloc. */
86 complain_overflow_bitfield
,
87 bfd_elf_generic_reloc
,
93 /* Standard 16 bit reloc. */
100 complain_overflow_bitfield
,
101 bfd_elf_generic_reloc
,
107 /* Standard 8 bit reloc. */
114 complain_overflow_bitfield
,
115 bfd_elf_generic_reloc
,
121 /* Standard 24 bit reloc. */
128 complain_overflow_bitfield
,
129 bfd_elf_generic_reloc
,
135 /* Simple 8 pc-relative reloc. */
136 HOWTO (R_MN10200_PCREL8
,
142 complain_overflow_bitfield
,
143 bfd_elf_generic_reloc
,
149 /* Simple 16 pc-relative reloc. */
150 HOWTO (R_MN10200_PCREL16
,
156 complain_overflow_bitfield
,
157 bfd_elf_generic_reloc
,
163 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
164 to get the pc-relative offset correct. */
165 HOWTO (R_MN10200_PCREL24
,
171 complain_overflow_bitfield
,
172 bfd_elf_generic_reloc
,
180 struct mn10200_reloc_map
{
181 bfd_reloc_code_real_type bfd_reloc_val
;
182 unsigned char elf_reloc_val
;
185 static const struct mn10200_reloc_map mn10200_reloc_map
[] = {
186 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
187 { BFD_RELOC_32
, R_MN10200_32
, },
188 { BFD_RELOC_16
, R_MN10200_16
, },
189 { BFD_RELOC_8
, R_MN10200_8
, },
190 { BFD_RELOC_24
, R_MN10200_24
, },
191 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
192 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
193 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
196 static reloc_howto_type
*
197 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
198 bfd
*abfd ATTRIBUTE_UNUSED
;
199 bfd_reloc_code_real_type code
;
204 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
207 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
208 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
214 /* Set the howto pointer for an MN10200 ELF reloc. */
217 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
218 bfd
*abfd ATTRIBUTE_UNUSED
;
220 Elf32_Internal_Rela
*dst
;
224 r_type
= ELF32_R_TYPE (dst
->r_info
);
225 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
226 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
229 /* Perform a relocation as part of a final link. */
231 static bfd_reloc_status_type
232 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
233 input_section
, contents
, offset
, value
,
234 addend
, info
, sym_sec
, is_local
)
235 reloc_howto_type
*howto
;
237 bfd
*output_bfd ATTRIBUTE_UNUSED
;
238 asection
*input_section
;
243 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
244 asection
*sym_sec ATTRIBUTE_UNUSED
;
245 int is_local ATTRIBUTE_UNUSED
;
247 unsigned long r_type
= howto
->type
;
248 bfd_byte
*hit_data
= contents
+ offset
;
258 bfd_put_32 (input_bfd
, value
, hit_data
);
264 if ((long) value
> 0x7fff || (long) value
< -0x8000)
265 return bfd_reloc_overflow
;
267 bfd_put_16 (input_bfd
, value
, hit_data
);
273 if ((long) value
> 0x7f || (long) value
< -0x80)
274 return bfd_reloc_overflow
;
276 bfd_put_8 (input_bfd
, value
, hit_data
);
282 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
283 return bfd_reloc_overflow
;
286 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
287 bfd_put_32 (input_bfd
, value
, hit_data
);
290 case R_MN10200_PCREL8
:
291 value
-= (input_section
->output_section
->vma
292 + input_section
->output_offset
);
293 value
-= (offset
+ 1);
296 if ((long) value
> 0xff || (long) value
< -0x100)
297 return bfd_reloc_overflow
;
299 bfd_put_8 (input_bfd
, value
, hit_data
);
302 case R_MN10200_PCREL16
:
303 value
-= (input_section
->output_section
->vma
304 + input_section
->output_offset
);
305 value
-= (offset
+ 2);
308 if ((long) value
> 0xffff || (long) value
< -0x10000)
309 return bfd_reloc_overflow
;
311 bfd_put_16 (input_bfd
, value
, hit_data
);
314 case R_MN10200_PCREL24
:
315 value
-= (input_section
->output_section
->vma
316 + input_section
->output_offset
);
317 value
-= (offset
+ 3);
320 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
321 return bfd_reloc_overflow
;
324 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
325 bfd_put_32 (input_bfd
, value
, hit_data
);
329 return bfd_reloc_notsupported
;
333 /* Relocate an MN10200 ELF section. */
335 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
336 contents
, relocs
, local_syms
, local_sections
)
338 struct bfd_link_info
*info
;
340 asection
*input_section
;
342 Elf_Internal_Rela
*relocs
;
343 Elf_Internal_Sym
*local_syms
;
344 asection
**local_sections
;
346 Elf_Internal_Shdr
*symtab_hdr
;
347 struct elf_link_hash_entry
**sym_hashes
;
348 Elf_Internal_Rela
*rel
, *relend
;
350 if (info
->relocateable
)
353 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
354 sym_hashes
= elf_sym_hashes (input_bfd
);
357 relend
= relocs
+ input_section
->reloc_count
;
358 for (; rel
< relend
; rel
++)
361 reloc_howto_type
*howto
;
362 unsigned long r_symndx
;
363 Elf_Internal_Sym
*sym
;
365 struct elf_link_hash_entry
*h
;
367 bfd_reloc_status_type r
;
369 r_symndx
= ELF32_R_SYM (rel
->r_info
);
370 r_type
= ELF32_R_TYPE (rel
->r_info
);
371 howto
= elf_mn10200_howto_table
+ r_type
;
376 if (r_symndx
< symtab_hdr
->sh_info
)
378 sym
= local_syms
+ r_symndx
;
379 sec
= local_sections
[r_symndx
];
380 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
384 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
385 while (h
->root
.type
== bfd_link_hash_indirect
386 || h
->root
.type
== bfd_link_hash_warning
)
387 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
388 if (h
->root
.type
== bfd_link_hash_defined
389 || h
->root
.type
== bfd_link_hash_defweak
)
391 sec
= h
->root
.u
.def
.section
;
392 relocation
= (h
->root
.u
.def
.value
393 + sec
->output_section
->vma
394 + sec
->output_offset
);
396 else if (h
->root
.type
== bfd_link_hash_undefweak
)
400 if (! ((*info
->callbacks
->undefined_symbol
)
401 (info
, h
->root
.root
.string
, input_bfd
,
402 input_section
, rel
->r_offset
, true)))
408 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
410 contents
, rel
->r_offset
,
411 relocation
, rel
->r_addend
,
412 info
, sec
, h
== NULL
);
414 if (r
!= bfd_reloc_ok
)
417 const char *msg
= (const char *) 0;
420 name
= h
->root
.root
.string
;
423 name
= (bfd_elf_string_from_elf_section
424 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
425 if (name
== NULL
|| *name
== '\0')
426 name
= bfd_section_name (input_bfd
, sec
);
431 case bfd_reloc_overflow
:
432 if (! ((*info
->callbacks
->reloc_overflow
)
433 (info
, name
, howto
->name
, (bfd_vma
) 0,
434 input_bfd
, input_section
, rel
->r_offset
)))
438 case bfd_reloc_undefined
:
439 if (! ((*info
->callbacks
->undefined_symbol
)
440 (info
, name
, input_bfd
, input_section
,
441 rel
->r_offset
, true)))
445 case bfd_reloc_outofrange
:
446 msg
= _("internal error: out of range error");
449 case bfd_reloc_notsupported
:
450 msg
= _("internal error: unsupported relocation error");
453 case bfd_reloc_dangerous
:
454 msg
= _("internal error: dangerous error");
458 msg
= _("internal error: unknown error");
462 if (!((*info
->callbacks
->warning
)
463 (info
, msg
, name
, input_bfd
, input_section
,
474 /* This function handles relaxing for the mn10200.
476 There's quite a few relaxing opportunites available on the mn10200:
478 * jsr:24 -> jsr:16 2 bytes
480 * jmp:24 -> jmp:16 2 bytes
481 * jmp:16 -> bra:8 1 byte
483 * If the previous instruction is a conditional branch
484 around the jump/bra, we may be able to reverse its condition
485 and change its target to the jump's target. The jump/bra
486 can then be deleted. 2 bytes
488 * mov abs24 -> mov abs16 2 byte savings
490 * Most instructions which accept imm24 can relax to imm16 2 bytes
491 - Most instructions which accept imm16 can relax to imm8 1 byte
493 * Most instructions which accept d24 can relax to d16 2 bytes
494 - Most instructions which accept d16 can relax to d8 1 byte
496 abs24, imm24, d24 all look the same at the reloc level. It
497 might make the code simpler if we had different relocs for
498 the various relaxable operand types.
500 We don't handle imm16->imm8 or d16->d8 as they're very rare
501 and somewhat more difficult to support. */
504 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
507 struct bfd_link_info
*link_info
;
510 Elf_Internal_Shdr
*symtab_hdr
;
511 Elf_Internal_Rela
*internal_relocs
;
512 Elf_Internal_Rela
*irel
, *irelend
;
513 bfd_byte
*contents
= NULL
;
514 Elf_Internal_Sym
*isymbuf
= NULL
;
516 /* Assume nothing changes. */
519 /* We don't have to do anything for a relocateable link, if
520 this section does not have relocs, or if this is not a
522 if (link_info
->relocateable
523 || (sec
->flags
& SEC_RELOC
) == 0
524 || sec
->reloc_count
== 0
525 || (sec
->flags
& SEC_CODE
) == 0)
528 /* If this is the first time we have been called for this section,
529 initialize the cooked size. */
530 if (sec
->_cooked_size
== 0)
531 sec
->_cooked_size
= sec
->_raw_size
;
533 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
535 /* Get a copy of the native relocations. */
536 internal_relocs
= (_bfd_elf32_link_read_relocs
537 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
538 link_info
->keep_memory
));
539 if (internal_relocs
== NULL
)
542 /* Walk through them looking for relaxing opportunities. */
543 irelend
= internal_relocs
+ sec
->reloc_count
;
544 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
548 /* If this isn't something that can be relaxed, then ignore
550 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
551 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
552 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
555 /* Get the section contents if we haven't done so already. */
556 if (contents
== NULL
)
558 /* Get cached copy if it exists. */
559 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
560 contents
= elf_section_data (sec
)->this_hdr
.contents
;
563 /* Go get them off disk. */
564 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
565 if (contents
== NULL
)
568 if (! bfd_get_section_contents (abfd
, sec
, contents
,
569 (file_ptr
) 0, sec
->_raw_size
))
574 /* Read this BFD's local symbols if we haven't done so already. */
575 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
577 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
579 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
580 symtab_hdr
->sh_info
, 0,
586 /* Get the value of the symbol referred to by the reloc. */
587 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
589 /* A local symbol. */
590 Elf_Internal_Sym
*isym
;
593 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
594 if (isym
->st_shndx
== SHN_UNDEF
)
595 sym_sec
= bfd_und_section_ptr
;
596 else if (isym
->st_shndx
== SHN_ABS
)
597 sym_sec
= bfd_abs_section_ptr
;
598 else if (isym
->st_shndx
== SHN_COMMON
)
599 sym_sec
= bfd_com_section_ptr
;
601 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
602 symval
= (isym
->st_value
603 + sym_sec
->output_section
->vma
604 + sym_sec
->output_offset
);
609 struct elf_link_hash_entry
*h
;
611 /* An external symbol. */
612 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
613 h
= elf_sym_hashes (abfd
)[indx
];
614 BFD_ASSERT (h
!= NULL
);
615 if (h
->root
.type
!= bfd_link_hash_defined
616 && h
->root
.type
!= bfd_link_hash_defweak
)
618 /* This appears to be a reference to an undefined
619 symbol. Just ignore it--it will be caught by the
620 regular reloc processing. */
624 symval
= (h
->root
.u
.def
.value
625 + h
->root
.u
.def
.section
->output_section
->vma
626 + h
->root
.u
.def
.section
->output_offset
);
629 /* For simplicity of coding, we are going to modify the section
630 contents, the section relocs, and the BFD symbol table. We
631 must tell the rest of the code not to free up this
632 information. It would be possible to instead create a table
633 of changes which have to be made, as is done in coff-mips.c;
634 that would be more work, but would require less memory when
635 the linker is run. */
637 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
639 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
641 bfd_vma value
= symval
;
643 /* Deal with pc-relative gunk. */
644 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
645 value
-= (irel
->r_offset
+ 3);
646 value
+= irel
->r_addend
;
648 /* See if the value will fit in 16 bits, note the high value is
649 0x7fff + 2 as the target will be two bytes closer if we are
651 if ((long) value
< 0x8001 && (long) value
> -0x8000)
655 /* Get the opcode. */
656 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
658 if (code
!= 0xe0 && code
!= 0xe1)
661 /* Note that we've changed the relocs, section contents, etc. */
662 elf_section_data (sec
)->relocs
= internal_relocs
;
663 elf_section_data (sec
)->this_hdr
.contents
= contents
;
664 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
666 /* Fix the opcode. */
668 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
669 else if (code
== 0xe1)
670 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
672 /* Fix the relocation's type. */
673 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
676 /* The opcode got shorter too, so we have to fix the offset. */
679 /* Delete two bytes of data. */
680 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
681 irel
->r_offset
+ 1, 2))
684 /* That will change things, so, we should relax again.
685 Note that this is not required, and it may be slow. */
690 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
692 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
694 bfd_vma value
= symval
;
696 /* Deal with pc-relative gunk. */
697 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
698 value
-= (irel
->r_offset
+ 2);
699 value
+= irel
->r_addend
;
701 /* See if the value will fit in 8 bits, note the high value is
702 0x7f + 1 as the target will be one bytes closer if we are
704 if ((long) value
< 0x80 && (long) value
> -0x80)
708 /* Get the opcode. */
709 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
714 /* Note that we've changed the relocs, section contents, etc. */
715 elf_section_data (sec
)->relocs
= internal_relocs
;
716 elf_section_data (sec
)->this_hdr
.contents
= contents
;
717 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
719 /* Fix the opcode. */
720 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
722 /* Fix the relocation's type. */
723 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
726 /* Delete one byte of data. */
727 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
728 irel
->r_offset
+ 1, 1))
731 /* That will change things, so, we should relax again.
732 Note that this is not required, and it may be slow. */
737 /* Try to eliminate an unconditional 8 bit pc-relative branch
738 which immediately follows a conditional 8 bit pc-relative
739 branch around the unconditional branch.
746 This happens when the bCC can't reach lab2 at assembly time,
747 but due to other relaxations it can reach at link time. */
748 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
750 Elf_Internal_Rela
*nrel
;
751 bfd_vma value
= symval
;
754 /* Deal with pc-relative gunk. */
755 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
756 value
-= (irel
->r_offset
+ 1);
757 value
+= irel
->r_addend
;
759 /* Do nothing if this reloc is the last byte in the section. */
760 if (irel
->r_offset
== sec
->_cooked_size
)
763 /* See if the next instruction is an unconditional pc-relative
764 branch, more often than not this test will fail, so we
765 test it first to speed things up. */
766 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
770 /* Also make sure the next relocation applies to the next
771 instruction and that it's a pc-relative 8 bit branch. */
774 || irel
->r_offset
+ 2 != nrel
->r_offset
775 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
778 /* Make sure our destination immediately follows the
779 unconditional branch. */
780 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
781 + irel
->r_offset
+ 3))
784 /* Now make sure we are a conditional branch. This may not
785 be necessary, but why take the chance.
787 Note these checks assume that R_MN10200_PCREL8 relocs
788 only occur on bCC and bCCx insns. If they occured
789 elsewhere, we'd need to know the start of this insn
790 for this check to be accurate. */
791 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
792 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
793 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
794 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
795 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
796 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
797 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
800 /* We also have to be sure there is no symbol/label
801 at the unconditional branch. */
802 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
806 /* Note that we've changed the relocs, section contents, etc. */
807 elf_section_data (sec
)->relocs
= internal_relocs
;
808 elf_section_data (sec
)->this_hdr
.contents
= contents
;
809 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
811 /* Reverse the condition of the first branch. */
869 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
871 /* Set the reloc type and symbol for the first branch
872 from the second branch. */
873 irel
->r_info
= nrel
->r_info
;
875 /* Make the reloc for the second branch a null reloc. */
876 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
879 /* Delete two bytes of data. */
880 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
881 irel
->r_offset
+ 1, 2))
884 /* That will change things, so, we should relax again.
885 Note that this is not required, and it may be slow. */
889 /* Try to turn a 24bit immediate, displacement or absolute address
890 into a 16bit immediate, displacement or absolute address. */
891 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
893 bfd_vma value
= symval
;
895 /* See if the value will fit in 16 bits.
896 We allow any 16bit match here. We prune those we can't
898 if ((long) value
< 0x7fff && (long) value
> -0x8000)
902 /* All insns which have 24bit operands are 5 bytes long,
903 the first byte will always be 0xf4, but we double check
906 /* Get the first opcode. */
907 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
912 /* Get the second opcode. */
913 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
917 /* mov imm24,dn -> mov imm16,dn */
919 /* Not safe if the high bit is on as relaxing may
920 move the value out of high mem and thus not fit
921 in a signed 16bit value. */
925 /* Note that we've changed the reldection contents, etc. */
926 elf_section_data (sec
)->relocs
= internal_relocs
;
927 elf_section_data (sec
)->this_hdr
.contents
= contents
;
928 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
930 /* Fix the opcode. */
931 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
932 contents
+ irel
->r_offset
- 2);
934 /* Fix the relocation's type. */
935 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
938 /* The opcode got shorter too, so we have to fix the
942 /* Delete two bytes of data. */
943 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
944 irel
->r_offset
+ 1, 2))
947 /* That will change things, so, we should relax again.
948 Note that this is not required, and it may be slow. */
952 /* mov imm24,an -> mov imm16,an
953 cmp imm24,an -> cmp imm16,an
954 mov (abs24),dn -> mov (abs16),dn
955 mov dn,(abs24) -> mov dn,(abs16)
956 movb dn,(abs24) -> movb dn,(abs16)
957 movbu (abs24),dn -> movbu (abs16),dn */
964 /* Note that we've changed the reldection contents, etc. */
965 elf_section_data (sec
)->relocs
= internal_relocs
;
966 elf_section_data (sec
)->this_hdr
.contents
= contents
;
967 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
969 if ((code
& 0xfc) == 0x74)
970 code
= 0xdc + (code
& 0x03);
971 else if ((code
& 0xfc) == 0x7c)
972 code
= 0xec + (code
& 0x03);
973 else if ((code
& 0xfc) == 0xc0)
974 code
= 0xc8 + (code
& 0x03);
975 else if ((code
& 0xfc) == 0x40)
976 code
= 0xc0 + (code
& 0x03);
977 else if ((code
& 0xfc) == 0x44)
978 code
= 0xc4 + (code
& 0x03);
979 else if ((code
& 0xfc) == 0xc8)
980 code
= 0xcc + (code
& 0x03);
982 /* Fix the opcode. */
983 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
985 /* Fix the relocation's type. */
986 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
989 /* The opcode got shorter too, so we have to fix the
993 /* Delete two bytes of data. */
994 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
995 irel
->r_offset
+ 1, 2))
998 /* That will change things, so, we should relax again.
999 Note that this is not required, and it may be slow. */
1003 /* cmp imm24,dn -> cmp imm16,dn
1004 mov (abs24),an -> mov (abs16),an
1005 mov an,(abs24) -> mov an,(abs16)
1006 add imm24,dn -> add imm16,dn
1007 add imm24,an -> add imm16,an
1008 sub imm24,dn -> sub imm16,dn
1009 sub imm24,an -> sub imm16,an
1010 And all d24->d16 in memory ops. */
1027 /* Not safe if the high bit is on as relaxing may
1028 move the value out of high mem and thus not fit
1029 in a signed 16bit value. */
1030 if (((code
& 0xfc) == 0x78
1031 || (code
& 0xfc) == 0x60
1032 || (code
& 0xfc) == 0x64
1033 || (code
& 0xfc) == 0x68
1034 || (code
& 0xfc) == 0x6c
1035 || (code
& 0xfc) == 0x80
1036 || (code
& 0xfc) == 0xf0
1037 || (code
& 0xfc) == 0x00
1038 || (code
& 0xfc) == 0x10
1039 || (code
& 0xfc) == 0xb0
1040 || (code
& 0xfc) == 0x30
1041 || (code
& 0xfc) == 0xa0
1042 || (code
& 0xfc) == 0x20
1043 || (code
& 0xfc) == 0x90)
1044 && (value
& 0x8000) != 0)
1047 /* Note that we've changed the reldection contents, etc. */
1048 elf_section_data (sec
)->relocs
= internal_relocs
;
1049 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1050 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1052 /* Fix the opcode. */
1053 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1055 if ((code
& 0xfc) == 0x78)
1056 code
= 0x48 + (code
& 0x03);
1057 else if ((code
& 0xfc) == 0xd0)
1058 code
= 0x30 + (code
& 0x03);
1059 else if ((code
& 0xfc) == 0x50)
1060 code
= 0x20 + (code
& 0x03);
1061 else if ((code
& 0xfc) == 0x60)
1062 code
= 0x18 + (code
& 0x03);
1063 else if ((code
& 0xfc) == 0x64)
1064 code
= 0x08 + (code
& 0x03);
1065 else if ((code
& 0xfc) == 0x68)
1066 code
= 0x1c + (code
& 0x03);
1067 else if ((code
& 0xfc) == 0x6c)
1068 code
= 0x0c + (code
& 0x03);
1069 else if ((code
& 0xfc) == 0x80)
1070 code
= 0xc0 + (code
& 0x07);
1071 else if ((code
& 0xfc) == 0xf0)
1072 code
= 0xb0 + (code
& 0x07);
1073 else if ((code
& 0xfc) == 0x00)
1074 code
= 0x80 + (code
& 0x07);
1075 else if ((code
& 0xfc) == 0x10)
1076 code
= 0xa0 + (code
& 0x07);
1077 else if ((code
& 0xfc) == 0xb0)
1078 code
= 0x70 + (code
& 0x07);
1079 else if ((code
& 0xfc) == 0x30)
1080 code
= 0x60 + (code
& 0x07);
1081 else if ((code
& 0xfc) == 0xa0)
1082 code
= 0xd0 + (code
& 0x07);
1083 else if ((code
& 0xfc) == 0x20)
1084 code
= 0x90 + (code
& 0x07);
1085 else if ((code
& 0xfc) == 0x90)
1086 code
= 0x50 + (code
& 0x07);
1088 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1090 /* Fix the relocation's type. */
1091 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1094 /* Delete one bytes of data. */
1095 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1096 irel
->r_offset
+ 2, 1))
1099 /* That will change things, so, we should relax again.
1100 Note that this is not required, and it may be slow. */
1104 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1106 /* Note that we've changed the reldection contents, etc. */
1107 elf_section_data (sec
)->relocs
= internal_relocs
;
1108 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1109 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1111 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1112 contents
+ irel
->r_offset
- 2);
1114 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1115 contents
+ irel
->r_offset
- 1);
1117 /* Fix the relocation's type. */
1118 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1121 /* The reloc will be applied one byte in front of its
1122 current location. */
1123 irel
->r_offset
-= 1;
1125 /* Delete one bytes of data. */
1126 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1127 irel
->r_offset
+ 2, 1))
1130 /* That will change things, so, we should relax again.
1131 Note that this is not required, and it may be slow. */
1140 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1142 if (! link_info
->keep_memory
)
1146 /* Cache the symbols for elf_link_input_bfd. */
1147 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1151 if (contents
!= NULL
1152 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1154 if (! link_info
->keep_memory
)
1158 /* Cache the section contents for elf_link_input_bfd. */
1159 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1163 if (internal_relocs
!= NULL
1164 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1165 free (internal_relocs
);
1171 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1173 if (contents
!= NULL
1174 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1176 if (internal_relocs
!= NULL
1177 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1178 free (internal_relocs
);
1183 /* Delete some bytes from a section while relaxing. */
1186 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1192 Elf_Internal_Shdr
*symtab_hdr
;
1193 unsigned int sec_shndx
;
1195 Elf_Internal_Rela
*irel
, *irelend
;
1196 Elf_Internal_Rela
*irelalign
;
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
;
1208 /* The deletion must stop at the next ALIGN reloc for an aligment
1209 power larger than the number of bytes we are deleting. */
1212 toaddr
= sec
->_cooked_size
;
1214 irel
= elf_section_data (sec
)->relocs
;
1215 irelend
= irel
+ sec
->reloc_count
;
1217 /* Actually delete the bytes. */
1218 memmove (contents
+ addr
, contents
+ addr
+ count
,
1219 (size_t) (toaddr
- addr
- count
));
1220 sec
->_cooked_size
-= count
;
1222 /* Adjust all the relocs. */
1223 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1225 /* Get the new reloc address. */
1226 if ((irel
->r_offset
> addr
1227 && irel
->r_offset
< toaddr
))
1228 irel
->r_offset
-= count
;
1231 /* Adjust the local symbols defined in this section. */
1232 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1233 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1234 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1236 if (isym
->st_shndx
== sec_shndx
1237 && isym
->st_value
> addr
1238 && isym
->st_value
< toaddr
)
1239 isym
->st_value
-= count
;
1242 /* Now adjust the global symbols defined in this section. */
1243 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1244 - symtab_hdr
->sh_info
);
1245 sym_hashes
= elf_sym_hashes (abfd
);
1246 end_hashes
= sym_hashes
+ symcount
;
1247 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1249 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1250 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1251 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1252 && sym_hash
->root
.u
.def
.section
== sec
1253 && sym_hash
->root
.u
.def
.value
> addr
1254 && sym_hash
->root
.u
.def
.value
< toaddr
)
1256 sym_hash
->root
.u
.def
.value
-= count
;
1263 /* Return true if a symbol exists at the given address, else return
1266 mn10200_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
1269 Elf_Internal_Sym
*isym
;
1272 Elf_Internal_Shdr
*symtab_hdr
;
1273 unsigned int sec_shndx
;
1274 Elf_Internal_Sym
*isymend
;
1275 struct elf_link_hash_entry
**sym_hashes
;
1276 struct elf_link_hash_entry
**end_hashes
;
1277 unsigned int symcount
;
1279 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1281 /* Examine all the local symbols. */
1282 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1283 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1285 if (isym
->st_shndx
== sec_shndx
1286 && isym
->st_value
== addr
)
1290 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1291 - symtab_hdr
->sh_info
);
1292 sym_hashes
= elf_sym_hashes (abfd
);
1293 end_hashes
= sym_hashes
+ symcount
;
1294 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1296 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1297 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1298 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1299 && sym_hash
->root
.u
.def
.section
== sec
1300 && sym_hash
->root
.u
.def
.value
== addr
)
1307 /* This is a version of bfd_generic_get_relocated_section_contents
1308 which uses mn10200_elf_relocate_section. */
1311 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1312 data
, relocateable
, symbols
)
1314 struct bfd_link_info
*link_info
;
1315 struct bfd_link_order
*link_order
;
1317 boolean relocateable
;
1320 Elf_Internal_Shdr
*symtab_hdr
;
1321 asection
*input_section
= link_order
->u
.indirect
.section
;
1322 bfd
*input_bfd
= input_section
->owner
;
1323 asection
**sections
= NULL
;
1324 Elf_Internal_Rela
*internal_relocs
= NULL
;
1325 Elf_Internal_Sym
*isymbuf
= NULL
;
1327 /* We only need to handle the case of relaxing, or of having a
1328 particular set of section contents, specially. */
1330 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1331 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1336 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1338 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1339 (size_t) input_section
->_raw_size
);
1341 if ((input_section
->flags
& SEC_RELOC
) != 0
1342 && input_section
->reloc_count
> 0)
1344 Elf_Internal_Sym
*isym
;
1345 Elf_Internal_Sym
*isymend
;
1349 internal_relocs
= (_bfd_elf32_link_read_relocs
1350 (input_bfd
, input_section
, (PTR
) NULL
,
1351 (Elf_Internal_Rela
*) NULL
, false));
1352 if (internal_relocs
== NULL
)
1355 if (symtab_hdr
->sh_info
!= 0)
1357 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1358 if (isymbuf
== NULL
)
1359 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1360 symtab_hdr
->sh_info
, 0,
1362 if (isymbuf
== NULL
)
1366 amt
= symtab_hdr
->sh_info
;
1367 amt
*= sizeof (asection
*);
1368 sections
= (asection
**) bfd_malloc (amt
);
1369 if (sections
== NULL
&& amt
!= 0)
1372 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1373 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1377 if (isym
->st_shndx
== SHN_UNDEF
)
1378 isec
= bfd_und_section_ptr
;
1379 else if (isym
->st_shndx
== SHN_ABS
)
1380 isec
= bfd_abs_section_ptr
;
1381 else if (isym
->st_shndx
== SHN_COMMON
)
1382 isec
= bfd_com_section_ptr
;
1384 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1389 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1390 input_section
, data
, internal_relocs
,
1394 if (sections
!= NULL
)
1397 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1399 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1400 free (internal_relocs
);
1406 if (sections
!= NULL
)
1409 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1411 if (internal_relocs
!= NULL
1412 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1413 free (internal_relocs
);
1417 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1418 #define TARGET_LITTLE_NAME "elf32-mn10200"
1419 #define ELF_ARCH bfd_arch_mn10200
1420 #define ELF_MACHINE_CODE EM_MN10200
1421 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1422 #define ELF_MAXPAGESIZE 0x1000
1424 #define elf_backend_rela_normal 1
1425 #define elf_info_to_howto mn10200_info_to_howto
1426 #define elf_info_to_howto_rel 0
1427 #define elf_backend_relocate_section mn10200_elf_relocate_section
1428 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1429 #define bfd_elf32_bfd_get_relocated_section_contents \
1430 mn10200_elf_get_relocated_section_contents
1432 #define elf_symbol_leading_char '_'
1434 #include "elf32-target.h"