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
*, 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_Shdr
*shndx_hdr
;
512 Elf_Internal_Rela
*internal_relocs
;
513 Elf_Internal_Rela
*free_relocs
= NULL
;
514 Elf_Internal_Rela
*irel
, *irelend
;
515 bfd_byte
*contents
= NULL
;
516 bfd_byte
*free_contents
= NULL
;
517 Elf32_External_Sym
*extsyms
= NULL
;
518 Elf32_External_Sym
*free_extsyms
= NULL
;
519 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
521 /* Assume nothing changes. */
524 /* We don't have to do anything for a relocateable link, if
525 this section does not have relocs, or if this is not a
527 if (link_info
->relocateable
528 || (sec
->flags
& SEC_RELOC
) == 0
529 || sec
->reloc_count
== 0
530 || (sec
->flags
& SEC_CODE
) == 0)
533 /* If this is the first time we have been called for this section,
534 initialize the cooked size. */
535 if (sec
->_cooked_size
== 0)
536 sec
->_cooked_size
= sec
->_raw_size
;
538 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
539 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
541 /* Get a copy of the native relocations. */
542 internal_relocs
= (_bfd_elf32_link_read_relocs
543 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
544 link_info
->keep_memory
));
545 if (internal_relocs
== NULL
)
547 if (! link_info
->keep_memory
)
548 free_relocs
= internal_relocs
;
550 /* Walk through them looking for relaxing opportunities. */
551 irelend
= internal_relocs
+ sec
->reloc_count
;
552 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
556 /* If this isn't something that can be relaxed, then ignore
558 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
559 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
560 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
563 /* Get the section contents if we haven't done so already. */
564 if (contents
== NULL
)
566 /* Get cached copy if it exists. */
567 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
568 contents
= elf_section_data (sec
)->this_hdr
.contents
;
571 /* Go get them off disk. */
572 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
573 if (contents
== NULL
)
575 free_contents
= contents
;
577 if (! bfd_get_section_contents (abfd
, sec
, contents
,
578 (file_ptr
) 0, sec
->_raw_size
))
583 /* Read this BFD's symbols if we haven't done so already. */
586 /* Get cached copy if it exists. */
587 if (symtab_hdr
->contents
!= NULL
)
588 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
591 /* Go get them off disk. */
594 amt
= symtab_hdr
->sh_info
;
595 amt
*= sizeof (Elf32_External_Sym
);
596 extsyms
= (Elf32_External_Sym
*) bfd_malloc (amt
);
599 free_extsyms
= extsyms
;
600 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
601 || bfd_bread ((PTR
) extsyms
, amt
, abfd
) != amt
)
603 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
606 if (shndx_hdr
->sh_size
!= 0)
610 amt
= symtab_hdr
->sh_info
;
611 amt
*= sizeof (Elf_External_Sym_Shndx
);
612 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
613 if (shndx_buf
== NULL
)
615 if (bfd_seek (abfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
616 || bfd_bread ((PTR
) shndx_buf
, amt
, abfd
) != amt
)
618 shndx_hdr
->contents
= (bfd_byte
*) shndx_buf
;
622 /* Get the value of the symbol referred to by the reloc. */
623 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
625 /* A local symbol. */
626 Elf32_External_Sym
*esym
;
627 Elf_External_Sym_Shndx
*shndx
;
628 Elf_Internal_Sym isym
;
631 esym
= extsyms
+ ELF32_R_SYM (irel
->r_info
);
632 shndx
= shndx_buf
+ (shndx_buf
? ELF32_R_SYM (irel
->r_info
) : 0);
633 bfd_elf32_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
,
636 if (isym
.st_shndx
== SHN_UNDEF
)
637 sym_sec
= bfd_und_section_ptr
;
638 else if (isym
.st_shndx
== SHN_ABS
)
639 sym_sec
= bfd_abs_section_ptr
;
640 else if (isym
.st_shndx
== SHN_COMMON
)
641 sym_sec
= bfd_com_section_ptr
;
643 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
644 symval
= (isym
.st_value
645 + sym_sec
->output_section
->vma
646 + sym_sec
->output_offset
);
651 struct elf_link_hash_entry
*h
;
653 /* An external symbol. */
654 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
655 h
= elf_sym_hashes (abfd
)[indx
];
656 BFD_ASSERT (h
!= NULL
);
657 if (h
->root
.type
!= bfd_link_hash_defined
658 && h
->root
.type
!= bfd_link_hash_defweak
)
660 /* This appears to be a reference to an undefined
661 symbol. Just ignore it--it will be caught by the
662 regular reloc processing. */
666 symval
= (h
->root
.u
.def
.value
667 + h
->root
.u
.def
.section
->output_section
->vma
668 + h
->root
.u
.def
.section
->output_offset
);
671 /* For simplicity of coding, we are going to modify the section
672 contents, the section relocs, and the BFD symbol table. We
673 must tell the rest of the code not to free up this
674 information. It would be possible to instead create a table
675 of changes which have to be made, as is done in coff-mips.c;
676 that would be more work, but would require less memory when
677 the linker is run. */
679 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
681 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
683 bfd_vma value
= symval
;
685 /* Deal with pc-relative gunk. */
686 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
687 value
-= (irel
->r_offset
+ 3);
688 value
+= irel
->r_addend
;
690 /* See if the value will fit in 16 bits, note the high value is
691 0x7fff + 2 as the target will be two bytes closer if we are
693 if ((long) value
< 0x8001 && (long) value
> -0x8000)
697 /* Get the opcode. */
698 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
700 if (code
!= 0xe0 && code
!= 0xe1)
703 /* Note that we've changed the relocs, section contents, etc. */
704 elf_section_data (sec
)->relocs
= internal_relocs
;
707 elf_section_data (sec
)->this_hdr
.contents
= contents
;
708 free_contents
= NULL
;
712 /* Fix the opcode. */
714 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
715 else if (code
== 0xe1)
716 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
718 /* Fix the relocation's type. */
719 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
722 /* The opcode got shorter too, so we have to fix the offset. */
725 /* Delete two bytes of data. */
726 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
727 irel
->r_offset
+ 1, 2))
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 turn a 16bit pc-relative branch into a 8bit pc-relative
738 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
740 bfd_vma value
= symval
;
742 /* Deal with pc-relative gunk. */
743 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
744 value
-= (irel
->r_offset
+ 2);
745 value
+= irel
->r_addend
;
747 /* See if the value will fit in 8 bits, note the high value is
748 0x7f + 1 as the target will be one bytes closer if we are
750 if ((long) value
< 0x80 && (long) value
> -0x80)
754 /* Get the opcode. */
755 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
760 /* Note that we've changed the relocs, section contents, etc. */
761 elf_section_data (sec
)->relocs
= internal_relocs
;
764 elf_section_data (sec
)->this_hdr
.contents
= contents
;
765 free_contents
= NULL
;
769 /* Fix the opcode. */
770 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
772 /* Fix the relocation's type. */
773 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
776 /* Delete one byte of data. */
777 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
778 irel
->r_offset
+ 1, 1))
781 /* That will change things, so, we should relax again.
782 Note that this is not required, and it may be slow. */
787 /* Try to eliminate an unconditional 8 bit pc-relative branch
788 which immediately follows a conditional 8 bit pc-relative
789 branch around the unconditional branch.
796 This happens when the bCC can't reach lab2 at assembly time,
797 but due to other relaxations it can reach at link time. */
798 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
800 Elf_Internal_Rela
*nrel
;
801 bfd_vma value
= symval
;
804 /* Deal with pc-relative gunk. */
805 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
806 value
-= (irel
->r_offset
+ 1);
807 value
+= irel
->r_addend
;
809 /* Do nothing if this reloc is the last byte in the section. */
810 if (irel
->r_offset
== sec
->_cooked_size
)
813 /* See if the next instruction is an unconditional pc-relative
814 branch, more often than not this test will fail, so we
815 test it first to speed things up. */
816 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
820 /* Also make sure the next relocation applies to the next
821 instruction and that it's a pc-relative 8 bit branch. */
824 || irel
->r_offset
+ 2 != nrel
->r_offset
825 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
828 /* Make sure our destination immediately follows the
829 unconditional branch. */
830 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
831 + irel
->r_offset
+ 3))
834 /* Now make sure we are a conditional branch. This may not
835 be necessary, but why take the chance.
837 Note these checks assume that R_MN10200_PCREL8 relocs
838 only occur on bCC and bCCx insns. If they occured
839 elsewhere, we'd need to know the start of this insn
840 for this check to be accurate. */
841 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
842 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
843 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
844 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
845 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
846 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
847 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
850 /* We also have to be sure there is no symbol/label
851 at the unconditional branch. */
852 if (mn10200_elf_symbol_address_p (abfd
, sec
, irel
->r_offset
+ 1))
855 /* Note that we've changed the relocs, section contents, etc. */
856 elf_section_data (sec
)->relocs
= internal_relocs
;
859 elf_section_data (sec
)->this_hdr
.contents
= contents
;
860 free_contents
= NULL
;
864 /* Reverse the condition of the first branch. */
922 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
924 /* Set the reloc type and symbol for the first branch
925 from the second branch. */
926 irel
->r_info
= nrel
->r_info
;
928 /* Make the reloc for the second branch a null reloc. */
929 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
932 /* Delete two bytes of data. */
933 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
934 irel
->r_offset
+ 1, 2))
937 /* That will change things, so, we should relax again.
938 Note that this is not required, and it may be slow. */
942 /* Try to turn a 24bit immediate, displacement or absolute address
943 into a 16bit immediate, displacement or absolute address. */
944 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
946 bfd_vma value
= symval
;
948 /* See if the value will fit in 16 bits.
949 We allow any 16bit match here. We prune those we can't
951 if ((long) value
< 0x7fff && (long) value
> -0x8000)
955 /* All insns which have 24bit operands are 5 bytes long,
956 the first byte will always be 0xf4, but we double check
959 /* Get the first opcode. */
960 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
965 /* Get the second opcode. */
966 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
970 /* mov imm24,dn -> mov imm16,dn */
972 /* Not safe if the high bit is on as relaxing may
973 move the value out of high mem and thus not fit
974 in a signed 16bit value. */
978 /* Note that we've changed the reldection contents, etc. */
979 elf_section_data (sec
)->relocs
= internal_relocs
;
982 elf_section_data (sec
)->this_hdr
.contents
= contents
;
983 free_contents
= NULL
;
987 /* Fix the opcode. */
988 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
989 contents
+ irel
->r_offset
- 2);
991 /* Fix the relocation's type. */
992 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
995 /* The opcode got shorter too, so we have to fix the
999 /* Delete two bytes of data. */
1000 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1001 irel
->r_offset
+ 1, 2))
1004 /* That will change things, so, we should relax again.
1005 Note that this is not required, and it may be slow. */
1009 /* mov imm24,an -> mov imm16,an
1010 cmp imm24,an -> cmp imm16,an
1011 mov (abs24),dn -> mov (abs16),dn
1012 mov dn,(abs24) -> mov dn,(abs16)
1013 movb dn,(abs24) -> movb dn,(abs16)
1014 movbu (abs24),dn -> movbu (abs16),dn */
1021 /* Note that we've changed the reldection contents, etc. */
1022 elf_section_data (sec
)->relocs
= internal_relocs
;
1025 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1026 free_contents
= NULL
;
1028 free_extsyms
= NULL
;
1030 if ((code
& 0xfc) == 0x74)
1031 code
= 0xdc + (code
& 0x03);
1032 else if ((code
& 0xfc) == 0x7c)
1033 code
= 0xec + (code
& 0x03);
1034 else if ((code
& 0xfc) == 0xc0)
1035 code
= 0xc8 + (code
& 0x03);
1036 else if ((code
& 0xfc) == 0x40)
1037 code
= 0xc0 + (code
& 0x03);
1038 else if ((code
& 0xfc) == 0x44)
1039 code
= 0xc4 + (code
& 0x03);
1040 else if ((code
& 0xfc) == 0xc8)
1041 code
= 0xcc + (code
& 0x03);
1043 /* Fix the opcode. */
1044 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1046 /* Fix the relocation's type. */
1047 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1050 /* The opcode got shorter too, so we have to fix the
1052 irel
->r_offset
-= 1;
1054 /* Delete two bytes of data. */
1055 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1056 irel
->r_offset
+ 1, 2))
1059 /* That will change things, so, we should relax again.
1060 Note that this is not required, and it may be slow. */
1064 /* cmp imm24,dn -> cmp imm16,dn
1065 mov (abs24),an -> mov (abs16),an
1066 mov an,(abs24) -> mov an,(abs16)
1067 add imm24,dn -> add imm16,dn
1068 add imm24,an -> add imm16,an
1069 sub imm24,dn -> sub imm16,dn
1070 sub imm24,an -> sub imm16,an
1071 And all d24->d16 in memory ops. */
1088 /* Not safe if the high bit is on as relaxing may
1089 move the value out of high mem and thus not fit
1090 in a signed 16bit value. */
1091 if (((code
& 0xfc) == 0x78
1092 || (code
& 0xfc) == 0x60
1093 || (code
& 0xfc) == 0x64
1094 || (code
& 0xfc) == 0x68
1095 || (code
& 0xfc) == 0x6c
1096 || (code
& 0xfc) == 0x80
1097 || (code
& 0xfc) == 0xf0
1098 || (code
& 0xfc) == 0x00
1099 || (code
& 0xfc) == 0x10
1100 || (code
& 0xfc) == 0xb0
1101 || (code
& 0xfc) == 0x30
1102 || (code
& 0xfc) == 0xa0
1103 || (code
& 0xfc) == 0x20
1104 || (code
& 0xfc) == 0x90)
1105 && (value
& 0x8000) != 0)
1108 /* Note that we've changed the reldection contents, etc. */
1109 elf_section_data (sec
)->relocs
= internal_relocs
;
1112 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1113 free_contents
= NULL
;
1115 free_extsyms
= NULL
;
1117 /* Fix the opcode. */
1118 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1120 if ((code
& 0xfc) == 0x78)
1121 code
= 0x48 + (code
& 0x03);
1122 else if ((code
& 0xfc) == 0xd0)
1123 code
= 0x30 + (code
& 0x03);
1124 else if ((code
& 0xfc) == 0x50)
1125 code
= 0x20 + (code
& 0x03);
1126 else if ((code
& 0xfc) == 0x60)
1127 code
= 0x18 + (code
& 0x03);
1128 else if ((code
& 0xfc) == 0x64)
1129 code
= 0x08 + (code
& 0x03);
1130 else if ((code
& 0xfc) == 0x68)
1131 code
= 0x1c + (code
& 0x03);
1132 else if ((code
& 0xfc) == 0x6c)
1133 code
= 0x0c + (code
& 0x03);
1134 else if ((code
& 0xfc) == 0x80)
1135 code
= 0xc0 + (code
& 0x07);
1136 else if ((code
& 0xfc) == 0xf0)
1137 code
= 0xb0 + (code
& 0x07);
1138 else if ((code
& 0xfc) == 0x00)
1139 code
= 0x80 + (code
& 0x07);
1140 else if ((code
& 0xfc) == 0x10)
1141 code
= 0xa0 + (code
& 0x07);
1142 else if ((code
& 0xfc) == 0xb0)
1143 code
= 0x70 + (code
& 0x07);
1144 else if ((code
& 0xfc) == 0x30)
1145 code
= 0x60 + (code
& 0x07);
1146 else if ((code
& 0xfc) == 0xa0)
1147 code
= 0xd0 + (code
& 0x07);
1148 else if ((code
& 0xfc) == 0x20)
1149 code
= 0x90 + (code
& 0x07);
1150 else if ((code
& 0xfc) == 0x90)
1151 code
= 0x50 + (code
& 0x07);
1153 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1155 /* Fix the relocation's type. */
1156 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1159 /* Delete one bytes of data. */
1160 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1161 irel
->r_offset
+ 2, 1))
1164 /* That will change things, so, we should relax again.
1165 Note that this is not required, and it may be slow. */
1169 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1171 /* Note that we've changed the reldection contents, etc. */
1172 elf_section_data (sec
)->relocs
= internal_relocs
;
1175 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1176 free_contents
= NULL
;
1178 free_extsyms
= NULL
;
1180 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1181 contents
+ irel
->r_offset
- 2);
1183 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1184 contents
+ irel
->r_offset
- 1);
1186 /* Fix the relocation's type. */
1187 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1190 /* The reloc will be applied one byte in front of its
1191 current location. */
1192 irel
->r_offset
-= 1;
1194 /* Delete one bytes of data. */
1195 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1196 irel
->r_offset
+ 2, 1))
1199 /* That will change things, so, we should relax again.
1200 Note that this is not required, and it may be slow. */
1208 if (free_relocs
!= NULL
)
1211 if (free_contents
!= NULL
)
1213 if (! link_info
->keep_memory
)
1214 free (free_contents
);
1217 /* Cache the section contents for elf_link_input_bfd. */
1218 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1222 if (shndx_buf
!= NULL
)
1224 shndx_hdr
->contents
= NULL
;
1228 if (free_extsyms
!= NULL
)
1230 if (! link_info
->keep_memory
)
1232 symtab_hdr
->contents
= NULL
;
1233 free (free_extsyms
);
1240 if (free_relocs
!= NULL
)
1242 if (free_contents
!= NULL
)
1243 free (free_contents
);
1244 if (shndx_buf
!= NULL
)
1246 shndx_hdr
->contents
= NULL
;
1249 if (free_extsyms
!= NULL
)
1251 symtab_hdr
->contents
= NULL
;
1252 free (free_extsyms
);
1258 /* Delete some bytes from a section while relaxing. */
1261 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1267 Elf_Internal_Shdr
*symtab_hdr
;
1268 Elf_Internal_Shdr
*shndx_hdr
;
1269 Elf32_External_Sym
*extsyms
;
1270 unsigned int sec_shndx
;
1272 Elf_Internal_Rela
*irel
, *irelend
;
1273 Elf_Internal_Rela
*irelalign
;
1275 Elf32_External_Sym
*esym
, *esymend
;
1276 Elf_External_Sym_Shndx
*shndx
;
1277 struct elf_link_hash_entry
**sym_hashes
;
1278 struct elf_link_hash_entry
**end_hashes
;
1279 unsigned int symcount
;
1281 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1282 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1284 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1286 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1288 /* The deletion must stop at the next ALIGN reloc for an aligment
1289 power larger than the number of bytes we are deleting. */
1292 toaddr
= sec
->_cooked_size
;
1294 irel
= elf_section_data (sec
)->relocs
;
1295 irelend
= irel
+ sec
->reloc_count
;
1297 /* Actually delete the bytes. */
1298 memmove (contents
+ addr
, contents
+ addr
+ count
,
1299 (size_t) (toaddr
- addr
- count
));
1300 sec
->_cooked_size
-= count
;
1302 /* Adjust all the relocs. */
1303 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1305 /* Get the new reloc address. */
1306 if ((irel
->r_offset
> addr
1307 && irel
->r_offset
< toaddr
))
1308 irel
->r_offset
-= count
;
1311 /* Adjust the local symbols defined in this section. */
1312 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1313 shndx
= (Elf_External_Sym_Shndx
*) shndx_hdr
->contents
;
1315 esymend
= esym
+ symtab_hdr
->sh_info
;
1316 for (; esym
< esymend
; esym
++, shndx
= (shndx
? shndx
+ 1 : NULL
))
1318 Elf_Internal_Sym isym
;
1319 Elf_External_Sym_Shndx dummy
;
1321 bfd_elf32_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
,
1324 if (isym
.st_shndx
== sec_shndx
1325 && isym
.st_value
> addr
1326 && isym
.st_value
< toaddr
)
1328 isym
.st_value
-= count
;
1329 bfd_elf32_swap_symbol_out (abfd
, &isym
, (PTR
) esym
, (PTR
) &dummy
);
1333 /* Now adjust the global symbols defined in this section. */
1334 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1335 - symtab_hdr
->sh_info
);
1336 sym_hashes
= elf_sym_hashes (abfd
);
1337 end_hashes
= sym_hashes
+ symcount
;
1338 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1340 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1341 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1342 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1343 && sym_hash
->root
.u
.def
.section
== sec
1344 && sym_hash
->root
.u
.def
.value
> addr
1345 && sym_hash
->root
.u
.def
.value
< toaddr
)
1347 sym_hash
->root
.u
.def
.value
-= count
;
1354 /* Return true if a symbol exists at the given address, else return
1357 mn10200_elf_symbol_address_p (abfd
, sec
, addr
)
1362 Elf_Internal_Shdr
*symtab_hdr
;
1363 Elf_Internal_Shdr
*shndx_hdr
;
1364 unsigned int sec_shndx
;
1365 Elf32_External_Sym
*esym
, *esymend
;
1366 Elf_External_Sym_Shndx
*shndx
;
1367 struct elf_link_hash_entry
**sym_hashes
;
1368 struct elf_link_hash_entry
**end_hashes
;
1369 unsigned int symcount
;
1371 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1373 /* Examine all the symbols. */
1374 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1375 shndx_hdr
= &elf_tdata (abfd
)->symtab_shndx_hdr
;
1376 shndx
= (Elf_External_Sym_Shndx
*) shndx_hdr
->contents
;
1377 esym
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1378 esymend
= esym
+ symtab_hdr
->sh_info
;
1379 for (; esym
< esymend
; esym
++, shndx
= (shndx
? shndx
+ 1 : NULL
))
1381 Elf_Internal_Sym isym
;
1383 bfd_elf32_swap_symbol_in (abfd
, (const PTR
) esym
, (const PTR
) shndx
,
1386 if (isym
.st_shndx
== sec_shndx
1387 && isym
.st_value
== addr
)
1391 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1392 - symtab_hdr
->sh_info
);
1393 sym_hashes
= elf_sym_hashes (abfd
);
1394 end_hashes
= sym_hashes
+ symcount
;
1395 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1397 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1398 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1399 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1400 && sym_hash
->root
.u
.def
.section
== sec
1401 && sym_hash
->root
.u
.def
.value
== addr
)
1408 /* This is a version of bfd_generic_get_relocated_section_contents
1409 which uses mn10200_elf_relocate_section. */
1412 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1413 data
, relocateable
, symbols
)
1415 struct bfd_link_info
*link_info
;
1416 struct bfd_link_order
*link_order
;
1418 boolean relocateable
;
1421 Elf_Internal_Shdr
*symtab_hdr
;
1422 Elf_Internal_Shdr
*shndx_hdr
;
1423 asection
*input_section
= link_order
->u
.indirect
.section
;
1424 bfd
*input_bfd
= input_section
->owner
;
1425 asection
**sections
= NULL
;
1426 Elf_Internal_Rela
*internal_relocs
= NULL
;
1427 Elf32_External_Sym
*external_syms
= NULL
;
1428 Elf_External_Sym_Shndx
*shndx_buf
= NULL
;
1429 Elf_External_Sym_Shndx
*shndx
;
1430 Elf_Internal_Sym
*internal_syms
= NULL
;
1432 /* We only need to handle the case of relaxing, or of having a
1433 particular set of section contents, specially. */
1435 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1436 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1441 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1442 shndx_hdr
= &elf_tdata (input_bfd
)->symtab_shndx_hdr
;
1444 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1445 (size_t) input_section
->_raw_size
);
1447 if ((input_section
->flags
& SEC_RELOC
) != 0
1448 && input_section
->reloc_count
> 0)
1450 Elf_Internal_Sym
*isymp
;
1452 Elf32_External_Sym
*esym
, *esymend
;
1455 if (symtab_hdr
->contents
!= NULL
)
1456 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1457 else if (symtab_hdr
->sh_info
!= 0)
1459 amt
= symtab_hdr
->sh_info
;
1460 amt
*= sizeof (Elf32_External_Sym
);
1461 external_syms
= (Elf32_External_Sym
*) bfd_malloc (amt
);
1462 if (external_syms
== NULL
)
1464 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1465 || bfd_bread ((PTR
) external_syms
, amt
, input_bfd
) != amt
)
1469 if (symtab_hdr
->sh_info
!= 0 && shndx_hdr
->sh_size
!= 0)
1471 amt
= symtab_hdr
->sh_info
;
1472 amt
*= sizeof (Elf_External_Sym_Shndx
);
1473 shndx_buf
= (Elf_External_Sym_Shndx
*) bfd_malloc (amt
);
1474 if (shndx_buf
== NULL
)
1476 if (bfd_seek (input_bfd
, shndx_hdr
->sh_offset
, SEEK_SET
) != 0
1477 || bfd_bread ((PTR
) shndx_buf
, amt
, input_bfd
) != amt
)
1481 internal_relocs
= (_bfd_elf32_link_read_relocs
1482 (input_bfd
, input_section
, (PTR
) NULL
,
1483 (Elf_Internal_Rela
*) NULL
, false));
1484 if (internal_relocs
== NULL
)
1487 amt
= symtab_hdr
->sh_info
;
1488 amt
*= sizeof (Elf_Internal_Sym
);
1489 internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (amt
);
1490 if (internal_syms
== NULL
&& amt
!= 0)
1493 amt
= symtab_hdr
->sh_info
;
1494 amt
*= sizeof (asection
*);
1495 sections
= (asection
**) bfd_malloc (amt
);
1496 if (sections
== NULL
&& amt
!= 0)
1499 for (isymp
= internal_syms
, secpp
= sections
, shndx
= shndx_buf
,
1500 esym
= external_syms
, esymend
= esym
+ symtab_hdr
->sh_info
;
1502 ++esym
, ++isymp
, ++secpp
, shndx
= (shndx
? shndx
+ 1 : NULL
))
1506 bfd_elf32_swap_symbol_in (input_bfd
, (const PTR
) esym
,
1507 (const PTR
) shndx
, isymp
);
1509 if (isymp
->st_shndx
== SHN_UNDEF
)
1510 isec
= bfd_und_section_ptr
;
1511 else if (isymp
->st_shndx
== SHN_ABS
)
1512 isec
= bfd_abs_section_ptr
;
1513 else if (isymp
->st_shndx
== SHN_COMMON
)
1514 isec
= bfd_com_section_ptr
;
1516 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1521 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1522 input_section
, data
, internal_relocs
,
1523 internal_syms
, sections
))
1526 if (sections
!= NULL
)
1528 if (internal_syms
!= NULL
)
1529 free (internal_syms
);
1530 if (shndx_buf
!= NULL
)
1532 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1533 free (external_syms
);
1534 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1535 free (internal_relocs
);
1541 if (internal_relocs
!= NULL
1542 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1543 free (internal_relocs
);
1544 if (shndx_buf
!= NULL
)
1546 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1547 free (external_syms
);
1548 if (internal_syms
!= NULL
)
1549 free (internal_syms
);
1550 if (sections
!= NULL
)
1555 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1556 #define TARGET_LITTLE_NAME "elf32-mn10200"
1557 #define ELF_ARCH bfd_arch_mn10200
1558 #define ELF_MACHINE_CODE EM_MN10200
1559 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1560 #define ELF_MAXPAGESIZE 0x1000
1562 #define elf_backend_rela_normal 1
1563 #define elf_info_to_howto mn10200_info_to_howto
1564 #define elf_info_to_howto_rel 0
1565 #define elf_backend_relocate_section mn10200_elf_relocate_section
1566 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1567 #define bfd_elf32_bfd_get_relocated_section_contents \
1568 mn10200_elf_get_relocated_section_contents
1570 #define elf_symbol_leading_char '_'
1572 #include "elf32-target.h"