1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
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
*, Elf32_External_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 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
351 sym_hashes
= elf_sym_hashes (input_bfd
);
354 relend
= relocs
+ input_section
->reloc_count
;
355 for (; rel
< relend
; rel
++)
358 reloc_howto_type
*howto
;
359 unsigned long r_symndx
;
360 Elf_Internal_Sym
*sym
;
362 struct elf_link_hash_entry
*h
;
364 bfd_reloc_status_type r
;
366 r_symndx
= ELF32_R_SYM (rel
->r_info
);
367 r_type
= ELF32_R_TYPE (rel
->r_info
);
368 howto
= elf_mn10200_howto_table
+ r_type
;
370 if (info
->relocateable
)
372 /* This is a relocateable link. We don't have to change
373 anything, unless the reloc is against a section symbol,
374 in which case we have to adjust according to where the
375 section symbol winds up in the output section. */
376 if (r_symndx
< symtab_hdr
->sh_info
)
378 sym
= local_syms
+ r_symndx
;
379 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
381 sec
= local_sections
[r_symndx
];
382 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
389 /* This is a final link. */
393 if (r_symndx
< symtab_hdr
->sh_info
)
395 sym
= local_syms
+ r_symndx
;
396 sec
= local_sections
[r_symndx
];
397 relocation
= (sec
->output_section
->vma
403 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
404 while (h
->root
.type
== bfd_link_hash_indirect
405 || h
->root
.type
== bfd_link_hash_warning
)
406 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
407 if (h
->root
.type
== bfd_link_hash_defined
408 || h
->root
.type
== bfd_link_hash_defweak
)
410 sec
= h
->root
.u
.def
.section
;
411 relocation
= (h
->root
.u
.def
.value
412 + sec
->output_section
->vma
413 + sec
->output_offset
);
415 else if (h
->root
.type
== bfd_link_hash_undefweak
)
419 if (! ((*info
->callbacks
->undefined_symbol
)
420 (info
, h
->root
.root
.string
, input_bfd
,
421 input_section
, rel
->r_offset
, true)))
427 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
429 contents
, rel
->r_offset
,
430 relocation
, rel
->r_addend
,
431 info
, sec
, h
== NULL
);
433 if (r
!= bfd_reloc_ok
)
436 const char *msg
= (const char *) 0;
439 name
= h
->root
.root
.string
;
442 name
= (bfd_elf_string_from_elf_section
443 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
444 if (name
== NULL
|| *name
== '\0')
445 name
= bfd_section_name (input_bfd
, sec
);
450 case bfd_reloc_overflow
:
451 if (! ((*info
->callbacks
->reloc_overflow
)
452 (info
, name
, howto
->name
, (bfd_vma
) 0,
453 input_bfd
, input_section
, rel
->r_offset
)))
457 case bfd_reloc_undefined
:
458 if (! ((*info
->callbacks
->undefined_symbol
)
459 (info
, name
, input_bfd
, input_section
,
460 rel
->r_offset
, true)))
464 case bfd_reloc_outofrange
:
465 msg
= _("internal error: out of range error");
468 case bfd_reloc_notsupported
:
469 msg
= _("internal error: unsupported relocation error");
472 case bfd_reloc_dangerous
:
473 msg
= _("internal error: dangerous error");
477 msg
= _("internal error: unknown error");
481 if (!((*info
->callbacks
->warning
)
482 (info
, msg
, name
, input_bfd
, input_section
,
493 /* This function handles relaxing for the mn10200.
495 There's quite a few relaxing opportunites available on the mn10200:
497 * jsr:24 -> jsr:16 2 bytes
499 * jmp:24 -> jmp:16 2 bytes
500 * jmp:16 -> bra:8 1 byte
502 * If the previous instruction is a conditional branch
503 around the jump/bra, we may be able to reverse its condition
504 and change its target to the jump's target. The jump/bra
505 can then be deleted. 2 bytes
507 * mov abs24 -> mov abs16 2 byte savings
509 * Most instructions which accept imm24 can relax to imm16 2 bytes
510 - Most instructions which accept imm16 can relax to imm8 1 byte
512 * Most instructions which accept d24 can relax to d16 2 bytes
513 - Most instructions which accept d16 can relax to d8 1 byte
515 abs24, imm24, d24 all look the same at the reloc level. It
516 might make the code simpler if we had different relocs for
517 the various relaxable operand types.
519 We don't handle imm16->imm8 or d16->d8 as they're very rare
520 and somewhat more difficult to support. */
523 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
526 struct bfd_link_info
*link_info
;
529 Elf_Internal_Shdr
*symtab_hdr
;
530 Elf_Internal_Rela
*internal_relocs
;
531 Elf_Internal_Rela
*free_relocs
= NULL
;
532 Elf_Internal_Rela
*irel
, *irelend
;
533 bfd_byte
*contents
= NULL
;
534 bfd_byte
*free_contents
= NULL
;
535 Elf32_External_Sym
*extsyms
= NULL
;
536 Elf32_External_Sym
*free_extsyms
= NULL
;
538 /* Assume nothing changes. */
541 /* We don't have to do anything for a relocateable link, if
542 this section does not have relocs, or if this is not a
544 if (link_info
->relocateable
545 || (sec
->flags
& SEC_RELOC
) == 0
546 || sec
->reloc_count
== 0
547 || (sec
->flags
& SEC_CODE
) == 0)
550 /* If this is the first time we have been called for this section,
551 initialize the cooked size. */
552 if (sec
->_cooked_size
== 0)
553 sec
->_cooked_size
= sec
->_raw_size
;
555 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
557 /* Get a copy of the native relocations. */
558 internal_relocs
= (_bfd_elf32_link_read_relocs
559 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
560 link_info
->keep_memory
));
561 if (internal_relocs
== NULL
)
563 if (! link_info
->keep_memory
)
564 free_relocs
= internal_relocs
;
566 /* Walk through them looking for relaxing opportunities. */
567 irelend
= internal_relocs
+ sec
->reloc_count
;
568 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
572 /* If this isn't something that can be relaxed, then ignore
574 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
575 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
576 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
579 /* Get the section contents if we haven't done so already. */
580 if (contents
== NULL
)
582 /* Get cached copy if it exists. */
583 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
584 contents
= elf_section_data (sec
)->this_hdr
.contents
;
587 /* Go get them off disk. */
588 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
589 if (contents
== NULL
)
591 free_contents
= contents
;
593 if (! bfd_get_section_contents (abfd
, sec
, contents
,
594 (file_ptr
) 0, sec
->_raw_size
))
599 /* Read this BFD's symbols if we haven't done so already. */
602 /* Get cached copy if it exists. */
603 if (symtab_hdr
->contents
!= NULL
)
604 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
607 /* Go get them off disk. */
608 bfd_size_type amt
= symtab_hdr
->sh_size
;
609 extsyms
= (Elf32_External_Sym
*) bfd_malloc (amt
);
612 free_extsyms
= extsyms
;
613 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
614 || bfd_bread (extsyms
, amt
, abfd
) != amt
)
619 /* Get the value of the symbol referred to by the reloc. */
620 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
622 Elf_Internal_Sym isym
;
625 /* A local symbol. */
626 bfd_elf32_swap_symbol_in (abfd
,
627 extsyms
+ ELF32_R_SYM (irel
->r_info
),
630 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
631 symval
= (isym
.st_value
632 + sym_sec
->output_section
->vma
633 + sym_sec
->output_offset
);
638 struct elf_link_hash_entry
*h
;
640 /* An external symbol. */
641 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
642 h
= elf_sym_hashes (abfd
)[indx
];
643 BFD_ASSERT (h
!= NULL
);
644 if (h
->root
.type
!= bfd_link_hash_defined
645 && h
->root
.type
!= bfd_link_hash_defweak
)
647 /* This appears to be a reference to an undefined
648 symbol. Just ignore it--it will be caught by the
649 regular reloc processing. */
653 symval
= (h
->root
.u
.def
.value
654 + h
->root
.u
.def
.section
->output_section
->vma
655 + h
->root
.u
.def
.section
->output_offset
);
658 /* For simplicity of coding, we are going to modify the section
659 contents, the section relocs, and the BFD symbol table. We
660 must tell the rest of the code not to free up this
661 information. It would be possible to instead create a table
662 of changes which have to be made, as is done in coff-mips.c;
663 that would be more work, but would require less memory when
664 the linker is run. */
666 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
668 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
670 bfd_vma value
= symval
;
672 /* Deal with pc-relative gunk. */
673 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
674 value
-= (irel
->r_offset
+ 3);
675 value
+= irel
->r_addend
;
677 /* See if the value will fit in 16 bits, note the high value is
678 0x7fff + 2 as the target will be two bytes closer if we are
680 if ((long) value
< 0x8001 && (long) value
> -0x8000)
684 /* Get the opcode. */
685 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
687 if (code
!= 0xe0 && code
!= 0xe1)
690 /* Note that we've changed the relocs, section contents, etc. */
691 elf_section_data (sec
)->relocs
= internal_relocs
;
694 elf_section_data (sec
)->this_hdr
.contents
= contents
;
695 free_contents
= NULL
;
697 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
700 /* Fix the opcode. */
702 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
703 else if (code
== 0xe1)
704 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
706 /* Fix the relocation's type. */
707 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
710 /* The opcode got shorter too, so we have to fix the offset. */
713 /* Delete two bytes of data. */
714 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
715 irel
->r_offset
+ 1, 2))
718 /* That will change things, so, we should relax again.
719 Note that this is not required, and it may be slow. */
724 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
726 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
728 bfd_vma value
= symval
;
730 /* Deal with pc-relative gunk. */
731 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
732 value
-= (irel
->r_offset
+ 2);
733 value
+= irel
->r_addend
;
735 /* See if the value will fit in 8 bits, note the high value is
736 0x7f + 1 as the target will be one bytes closer if we are
738 if ((long) value
< 0x80 && (long) value
> -0x80)
742 /* Get the opcode. */
743 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
748 /* Note that we've changed the relocs, section contents, etc. */
749 elf_section_data (sec
)->relocs
= internal_relocs
;
752 elf_section_data (sec
)->this_hdr
.contents
= contents
;
753 free_contents
= NULL
;
755 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
758 /* Fix the opcode. */
759 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
761 /* Fix the relocation's type. */
762 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
765 /* Delete one byte of data. */
766 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
767 irel
->r_offset
+ 1, 1))
770 /* That will change things, so, we should relax again.
771 Note that this is not required, and it may be slow. */
776 /* Try to eliminate an unconditional 8 bit pc-relative branch
777 which immediately follows a conditional 8 bit pc-relative
778 branch around the unconditional branch.
785 This happens when the bCC can't reach lab2 at assembly time,
786 but due to other relaxations it can reach at link time. */
787 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
789 Elf_Internal_Rela
*nrel
;
790 bfd_vma value
= symval
;
793 /* Deal with pc-relative gunk. */
794 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
795 value
-= (irel
->r_offset
+ 1);
796 value
+= irel
->r_addend
;
798 /* Do nothing if this reloc is the last byte in the section. */
799 if (irel
->r_offset
== sec
->_cooked_size
)
802 /* See if the next instruction is an unconditional pc-relative
803 branch, more often than not this test will fail, so we
804 test it first to speed things up. */
805 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
809 /* Also make sure the next relocation applies to the next
810 instruction and that it's a pc-relative 8 bit branch. */
813 || irel
->r_offset
+ 2 != nrel
->r_offset
814 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
817 /* Make sure our destination immediately follows the
818 unconditional branch. */
819 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
820 + irel
->r_offset
+ 3))
823 /* Now make sure we are a conditional branch. This may not
824 be necessary, but why take the chance.
826 Note these checks assume that R_MN10200_PCREL8 relocs
827 only occur on bCC and bCCx insns. If they occured
828 elsewhere, we'd need to know the start of this insn
829 for this check to be accurate. */
830 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
831 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
832 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
833 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
834 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
835 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
836 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
839 /* We also have to be sure there is no symbol/label
840 at the unconditional branch. */
841 if (mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
,
845 /* Note that we've changed the relocs, section contents, etc. */
846 elf_section_data (sec
)->relocs
= internal_relocs
;
849 elf_section_data (sec
)->this_hdr
.contents
= contents
;
850 free_contents
= NULL
;
852 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
855 /* Reverse the condition of the first branch. */
913 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
915 /* Set the reloc type and symbol for the first branch
916 from the second branch. */
917 irel
->r_info
= nrel
->r_info
;
919 /* Make the reloc for the second branch a null reloc. */
920 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
923 /* Delete two bytes of data. */
924 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
925 irel
->r_offset
+ 1, 2))
928 /* That will change things, so, we should relax again.
929 Note that this is not required, and it may be slow. */
933 /* Try to turn a 24bit immediate, displacement or absolute address
934 into a 16bit immediate, displacement or absolute address. */
935 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
937 bfd_vma value
= symval
;
939 /* See if the value will fit in 16 bits.
940 We allow any 16bit match here. We prune those we can't
942 if ((long) value
< 0x7fff && (long) value
> -0x8000)
946 /* All insns which have 24bit operands are 5 bytes long,
947 the first byte will always be 0xf4, but we double check
950 /* Get the first opcode. */
951 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
956 /* Get the second opcode. */
957 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
961 /* mov imm24,dn -> mov imm16,dn */
963 /* Not safe if the high bit is on as relaxing may
964 move the value out of high mem and thus not fit
965 in a signed 16bit value. */
969 /* Note that we've changed the reldection contents, etc. */
970 elf_section_data (sec
)->relocs
= internal_relocs
;
973 elf_section_data (sec
)->this_hdr
.contents
= contents
;
974 free_contents
= NULL
;
976 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
979 /* Fix the opcode. */
980 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
981 contents
+ irel
->r_offset
- 2);
983 /* Fix the relocation's type. */
984 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
987 /* The opcode got shorter too, so we have to fix the
991 /* Delete two bytes of data. */
992 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
993 irel
->r_offset
+ 1, 2))
996 /* That will change things, so, we should relax again.
997 Note that this is not required, and it may be slow. */
1001 /* mov imm24,an -> mov imm16,an
1002 cmp imm24,an -> cmp imm16,an
1003 mov (abs24),dn -> mov (abs16),dn
1004 mov dn,(abs24) -> mov dn,(abs16)
1005 movb dn,(abs24) -> movb dn,(abs16)
1006 movbu (abs24),dn -> movbu (abs16),dn */
1013 /* Note that we've changed the reldection contents, etc. */
1014 elf_section_data (sec
)->relocs
= internal_relocs
;
1017 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1018 free_contents
= NULL
;
1020 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1021 free_extsyms
= NULL
;
1023 if ((code
& 0xfc) == 0x74)
1024 code
= 0xdc + (code
& 0x03);
1025 else if ((code
& 0xfc) == 0x7c)
1026 code
= 0xec + (code
& 0x03);
1027 else if ((code
& 0xfc) == 0xc0)
1028 code
= 0xc8 + (code
& 0x03);
1029 else if ((code
& 0xfc) == 0x40)
1030 code
= 0xc0 + (code
& 0x03);
1031 else if ((code
& 0xfc) == 0x44)
1032 code
= 0xc4 + (code
& 0x03);
1033 else if ((code
& 0xfc) == 0xc8)
1034 code
= 0xcc + (code
& 0x03);
1036 /* Fix the opcode. */
1037 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1039 /* Fix the relocation's type. */
1040 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1043 /* The opcode got shorter too, so we have to fix the
1045 irel
->r_offset
-= 1;
1047 /* Delete two bytes of data. */
1048 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1049 irel
->r_offset
+ 1, 2))
1052 /* That will change things, so, we should relax again.
1053 Note that this is not required, and it may be slow. */
1057 /* cmp imm24,dn -> cmp imm16,dn
1058 mov (abs24),an -> mov (abs16),an
1059 mov an,(abs24) -> mov an,(abs16)
1060 add imm24,dn -> add imm16,dn
1061 add imm24,an -> add imm16,an
1062 sub imm24,dn -> sub imm16,dn
1063 sub imm24,an -> sub imm16,an
1064 And all d24->d16 in memory ops. */
1081 /* Not safe if the high bit is on as relaxing may
1082 move the value out of high mem and thus not fit
1083 in a signed 16bit value. */
1084 if (((code
& 0xfc) == 0x78
1085 || (code
& 0xfc) == 0x60
1086 || (code
& 0xfc) == 0x64
1087 || (code
& 0xfc) == 0x68
1088 || (code
& 0xfc) == 0x6c
1089 || (code
& 0xfc) == 0x80
1090 || (code
& 0xfc) == 0xf0
1091 || (code
& 0xfc) == 0x00
1092 || (code
& 0xfc) == 0x10
1093 || (code
& 0xfc) == 0xb0
1094 || (code
& 0xfc) == 0x30
1095 || (code
& 0xfc) == 0xa0
1096 || (code
& 0xfc) == 0x20
1097 || (code
& 0xfc) == 0x90)
1098 && (value
& 0x8000) != 0)
1101 /* Note that we've changed the reldection contents, etc. */
1102 elf_section_data (sec
)->relocs
= internal_relocs
;
1105 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1106 free_contents
= NULL
;
1108 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1109 free_extsyms
= NULL
;
1111 /* Fix the opcode. */
1112 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1114 if ((code
& 0xfc) == 0x78)
1115 code
= 0x48 + (code
& 0x03);
1116 else if ((code
& 0xfc) == 0xd0)
1117 code
= 0x30 + (code
& 0x03);
1118 else if ((code
& 0xfc) == 0x50)
1119 code
= 0x20 + (code
& 0x03);
1120 else if ((code
& 0xfc) == 0x60)
1121 code
= 0x18 + (code
& 0x03);
1122 else if ((code
& 0xfc) == 0x64)
1123 code
= 0x08 + (code
& 0x03);
1124 else if ((code
& 0xfc) == 0x68)
1125 code
= 0x1c + (code
& 0x03);
1126 else if ((code
& 0xfc) == 0x6c)
1127 code
= 0x0c + (code
& 0x03);
1128 else if ((code
& 0xfc) == 0x80)
1129 code
= 0xc0 + (code
& 0x07);
1130 else if ((code
& 0xfc) == 0xf0)
1131 code
= 0xb0 + (code
& 0x07);
1132 else if ((code
& 0xfc) == 0x00)
1133 code
= 0x80 + (code
& 0x07);
1134 else if ((code
& 0xfc) == 0x10)
1135 code
= 0xa0 + (code
& 0x07);
1136 else if ((code
& 0xfc) == 0xb0)
1137 code
= 0x70 + (code
& 0x07);
1138 else if ((code
& 0xfc) == 0x30)
1139 code
= 0x60 + (code
& 0x07);
1140 else if ((code
& 0xfc) == 0xa0)
1141 code
= 0xd0 + (code
& 0x07);
1142 else if ((code
& 0xfc) == 0x20)
1143 code
= 0x90 + (code
& 0x07);
1144 else if ((code
& 0xfc) == 0x90)
1145 code
= 0x50 + (code
& 0x07);
1147 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1149 /* Fix the relocation's type. */
1150 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1153 /* Delete one bytes of data. */
1154 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1155 irel
->r_offset
+ 2, 1))
1158 /* That will change things, so, we should relax again.
1159 Note that this is not required, and it may be slow. */
1163 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1165 /* Note that we've changed the reldection contents, etc. */
1166 elf_section_data (sec
)->relocs
= internal_relocs
;
1169 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1170 free_contents
= NULL
;
1172 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1173 free_extsyms
= NULL
;
1175 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1176 contents
+ irel
->r_offset
- 2);
1178 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1179 contents
+ irel
->r_offset
- 1);
1181 /* Fix the relocation's type. */
1182 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1185 /* The reloc will be applied one byte in front of its
1186 current location. */
1187 irel
->r_offset
-= 1;
1189 /* Delete one bytes of data. */
1190 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1191 irel
->r_offset
+ 2, 1))
1194 /* That will change things, so, we should relax again.
1195 Note that this is not required, and it may be slow. */
1203 if (free_relocs
!= NULL
)
1209 if (free_contents
!= NULL
)
1211 if (! link_info
->keep_memory
)
1212 free (free_contents
);
1215 /* Cache the section contents for elf_link_input_bfd. */
1216 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1218 free_contents
= NULL
;
1221 if (free_extsyms
!= NULL
)
1223 if (! link_info
->keep_memory
)
1224 free (free_extsyms
);
1227 /* Cache the symbols for elf_link_input_bfd. */
1228 symtab_hdr
->contents
= extsyms
;
1230 free_extsyms
= NULL
;
1236 if (free_relocs
!= NULL
)
1238 if (free_contents
!= NULL
)
1239 free (free_contents
);
1240 if (free_extsyms
!= NULL
)
1241 free (free_extsyms
);
1245 /* Delete some bytes from a section while relaxing. */
1248 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1254 Elf_Internal_Shdr
*symtab_hdr
;
1255 Elf32_External_Sym
*extsyms
;
1258 Elf_Internal_Rela
*irel
, *irelend
;
1259 Elf_Internal_Rela
*irelalign
;
1261 Elf32_External_Sym
*esym
, *esymend
;
1262 struct elf_link_hash_entry
*sym_hash
;
1264 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1265 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1267 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1269 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1271 /* The deletion must stop at the next ALIGN reloc for an aligment
1272 power larger than the number of bytes we are deleting. */
1275 toaddr
= sec
->_cooked_size
;
1277 irel
= elf_section_data (sec
)->relocs
;
1278 irelend
= irel
+ sec
->reloc_count
;
1280 /* Actually delete the bytes. */
1281 memmove (contents
+ addr
, contents
+ addr
+ count
,
1282 (size_t) (toaddr
- addr
- count
));
1283 sec
->_cooked_size
-= count
;
1285 /* Adjust all the relocs. */
1286 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1288 /* Get the new reloc address. */
1289 if ((irel
->r_offset
> addr
1290 && irel
->r_offset
< toaddr
))
1291 irel
->r_offset
-= count
;
1294 /* Adjust the local symbols defined in this section. */
1296 esymend
= esym
+ symtab_hdr
->sh_info
;
1297 for (; esym
< esymend
; esym
++)
1299 Elf_Internal_Sym isym
;
1301 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1303 if (isym
.st_shndx
== shndx
1304 && isym
.st_value
> addr
1305 && isym
.st_value
< toaddr
)
1307 isym
.st_value
-= count
;
1308 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1312 /* Now adjust the global symbols defined in this section. */
1313 esym
= extsyms
+ symtab_hdr
->sh_info
;
1314 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1315 for (index
= 0; esym
< esymend
; esym
++, index
++)
1317 Elf_Internal_Sym isym
;
1319 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1320 sym_hash
= elf_sym_hashes (abfd
)[index
];
1321 if (isym
.st_shndx
== shndx
1322 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1323 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1324 && (sym_hash
)->root
.u
.def
.section
== sec
1325 && (sym_hash
)->root
.u
.def
.value
> addr
1326 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1328 (sym_hash
)->root
.u
.def
.value
-= count
;
1335 /* Return true if a symbol exists at the given address, else return
1338 mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
1341 Elf32_External_Sym
*extsyms
;
1344 Elf_Internal_Shdr
*symtab_hdr
;
1346 Elf32_External_Sym
*esym
, *esymend
;
1347 struct elf_link_hash_entry
**sym_hash
, **sym_hash_end
;
1349 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1350 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1352 /* Examine all the symbols. */
1354 esymend
= esym
+ symtab_hdr
->sh_info
;
1355 for (; esym
< esymend
; esym
++)
1357 Elf_Internal_Sym isym
;
1359 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1361 if (isym
.st_shndx
== shndx
1362 && isym
.st_value
== addr
)
1366 sym_hash
= elf_sym_hashes (abfd
);
1367 sym_hash_end
= (sym_hash
1368 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1369 - symtab_hdr
->sh_info
));
1370 for (; sym_hash
< sym_hash_end
; sym_hash
++)
1372 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1373 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1374 && (*sym_hash
)->root
.u
.def
.section
== sec
1375 && (*sym_hash
)->root
.u
.def
.value
== addr
)
1381 /* This is a version of bfd_generic_get_relocated_section_contents
1382 which uses mn10200_elf_relocate_section. */
1385 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1386 data
, relocateable
, symbols
)
1388 struct bfd_link_info
*link_info
;
1389 struct bfd_link_order
*link_order
;
1391 boolean relocateable
;
1394 Elf_Internal_Shdr
*symtab_hdr
;
1395 asection
*input_section
= link_order
->u
.indirect
.section
;
1396 bfd
*input_bfd
= input_section
->owner
;
1397 asection
**sections
= NULL
;
1398 Elf_Internal_Rela
*internal_relocs
= NULL
;
1399 Elf32_External_Sym
*external_syms
= NULL
;
1400 Elf_Internal_Sym
*internal_syms
= NULL
;
1402 /* We only need to handle the case of relaxing, or of having a
1403 particular set of section contents, specially. */
1405 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1406 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1411 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1413 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1414 (size_t) input_section
->_raw_size
);
1416 if ((input_section
->flags
& SEC_RELOC
) != 0
1417 && input_section
->reloc_count
> 0)
1419 Elf_Internal_Sym
*isymp
;
1421 Elf32_External_Sym
*esym
, *esymend
;
1424 if (symtab_hdr
->contents
!= NULL
)
1425 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1428 size
= symtab_hdr
->sh_info
;
1429 size
*= sizeof (Elf32_External_Sym
);
1430 external_syms
= (Elf32_External_Sym
*) bfd_malloc (size
);
1431 if (external_syms
== NULL
&& size
!= 0)
1433 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1434 || bfd_bread (external_syms
, size
, input_bfd
) != size
)
1438 internal_relocs
= (_bfd_elf32_link_read_relocs
1439 (input_bfd
, input_section
, (PTR
) NULL
,
1440 (Elf_Internal_Rela
*) NULL
, false));
1441 if (internal_relocs
== NULL
)
1444 size
= symtab_hdr
->sh_info
;
1445 size
*= sizeof (Elf_Internal_Sym
);
1446 internal_syms
= (Elf_Internal_Sym
*) bfd_malloc (size
);
1447 if (internal_syms
== NULL
&& size
!= 0)
1450 size
= symtab_hdr
->sh_info
;
1451 size
*= sizeof (asection
*);
1452 sections
= (asection
**) bfd_malloc (size
);
1453 if (sections
== NULL
&& size
!= 0)
1456 isymp
= internal_syms
;
1458 esym
= external_syms
;
1459 esymend
= esym
+ symtab_hdr
->sh_info
;
1460 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
1464 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
1466 if (isymp
->st_shndx
== SHN_UNDEF
)
1467 isec
= bfd_und_section_ptr
;
1468 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
1469 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1470 else if (isymp
->st_shndx
== SHN_ABS
)
1471 isec
= bfd_abs_section_ptr
;
1472 else if (isymp
->st_shndx
== SHN_COMMON
)
1473 isec
= bfd_com_section_ptr
;
1483 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1484 input_section
, data
, internal_relocs
,
1485 internal_syms
, sections
))
1488 if (sections
!= NULL
)
1491 if (internal_syms
!= NULL
)
1492 free (internal_syms
);
1493 internal_syms
= NULL
;
1494 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1495 free (external_syms
);
1496 external_syms
= NULL
;
1497 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1498 free (internal_relocs
);
1499 internal_relocs
= NULL
;
1505 if (internal_relocs
!= NULL
1506 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1507 free (internal_relocs
);
1508 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1509 free (external_syms
);
1510 if (internal_syms
!= NULL
)
1511 free (internal_syms
);
1512 if (sections
!= NULL
)
1517 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1518 #define TARGET_LITTLE_NAME "elf32-mn10200"
1519 #define ELF_ARCH bfd_arch_mn10200
1520 #define ELF_MACHINE_CODE EM_MN10200
1521 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1522 #define ELF_MAXPAGESIZE 0x1000
1524 #define elf_info_to_howto mn10200_info_to_howto
1525 #define elf_info_to_howto_rel 0
1526 #define elf_backend_relocate_section mn10200_elf_relocate_section
1527 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1528 #define bfd_elf32_bfd_get_relocated_section_contents \
1529 mn10200_elf_get_relocated_section_contents
1531 #define elf_symbol_leading_char '_'
1533 #include "elf32-target.h"