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
));
35 /* We have to use RELA instructions since md_apply_fix3 in the assembler
36 does absolutely nothing. */
51 static reloc_howto_type elf_mn10200_howto_table
[] = {
52 /* Dummy relocation. Does nothing. */
53 HOWTO (R_MN10200_NONE
,
59 complain_overflow_bitfield
,
60 bfd_elf_generic_reloc
,
66 /* Standard 32 bit reloc. */
73 complain_overflow_bitfield
,
74 bfd_elf_generic_reloc
,
80 /* Standard 16 bit reloc. */
87 complain_overflow_bitfield
,
88 bfd_elf_generic_reloc
,
94 /* Standard 8 bit reloc. */
101 complain_overflow_bitfield
,
102 bfd_elf_generic_reloc
,
108 /* Standard 24 bit reloc. */
115 complain_overflow_bitfield
,
116 bfd_elf_generic_reloc
,
122 /* Simple 8 pc-relative reloc. */
123 HOWTO (R_MN10200_PCREL8
,
129 complain_overflow_bitfield
,
130 bfd_elf_generic_reloc
,
136 /* Simple 16 pc-relative reloc. */
137 HOWTO (R_MN10200_PCREL16
,
143 complain_overflow_bitfield
,
144 bfd_elf_generic_reloc
,
150 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
151 to get the pc-relative offset correct. */
152 HOWTO (R_MN10200_PCREL24
,
158 complain_overflow_bitfield
,
159 bfd_elf_generic_reloc
,
167 struct mn10200_reloc_map
{
168 bfd_reloc_code_real_type bfd_reloc_val
;
169 unsigned char elf_reloc_val
;
172 static const struct mn10200_reloc_map mn10200_reloc_map
[] = {
173 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
174 { BFD_RELOC_32
, R_MN10200_32
, },
175 { BFD_RELOC_16
, R_MN10200_16
, },
176 { BFD_RELOC_8
, R_MN10200_8
, },
177 { BFD_RELOC_24
, R_MN10200_24
, },
178 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
179 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
180 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
183 static reloc_howto_type
*
184 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
185 bfd
*abfd ATTRIBUTE_UNUSED
;
186 bfd_reloc_code_real_type code
;
191 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
194 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
195 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
201 /* Set the howto pointer for an MN10200 ELF reloc. */
204 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
205 bfd
*abfd ATTRIBUTE_UNUSED
;
207 Elf32_Internal_Rela
*dst
;
211 r_type
= ELF32_R_TYPE (dst
->r_info
);
212 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
213 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
216 /* Perform a relocation as part of a final link. */
217 static bfd_reloc_status_type
218 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
219 input_section
, contents
, offset
, value
,
220 addend
, info
, sym_sec
, is_local
)
221 reloc_howto_type
*howto
;
223 bfd
*output_bfd ATTRIBUTE_UNUSED
;
224 asection
*input_section
;
229 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
230 asection
*sym_sec ATTRIBUTE_UNUSED
;
231 int is_local ATTRIBUTE_UNUSED
;
233 unsigned long r_type
= howto
->type
;
234 bfd_byte
*hit_data
= contents
+ offset
;
244 bfd_put_32 (input_bfd
, value
, hit_data
);
250 if ((long) value
> 0x7fff || (long) value
< -0x8000)
251 return bfd_reloc_overflow
;
253 bfd_put_16 (input_bfd
, value
, hit_data
);
259 if ((long) value
> 0x7f || (long) value
< -0x80)
260 return bfd_reloc_overflow
;
262 bfd_put_8 (input_bfd
, value
, hit_data
);
268 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
269 return bfd_reloc_overflow
;
272 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
273 bfd_put_32 (input_bfd
, value
, hit_data
);
276 case R_MN10200_PCREL8
:
277 value
-= (input_section
->output_section
->vma
278 + input_section
->output_offset
);
279 value
-= (offset
+ 1);
282 if ((long) value
> 0xff || (long) value
< -0x100)
283 return bfd_reloc_overflow
;
285 bfd_put_8 (input_bfd
, value
, hit_data
);
288 case R_MN10200_PCREL16
:
289 value
-= (input_section
->output_section
->vma
290 + input_section
->output_offset
);
291 value
-= (offset
+ 2);
294 if ((long) value
> 0xffff || (long) value
< -0x10000)
295 return bfd_reloc_overflow
;
297 bfd_put_16 (input_bfd
, value
, hit_data
);
300 case R_MN10200_PCREL24
:
301 value
-= (input_section
->output_section
->vma
302 + input_section
->output_offset
);
303 value
-= (offset
+ 3);
306 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
307 return bfd_reloc_overflow
;
310 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
311 bfd_put_32 (input_bfd
, value
, hit_data
);
315 return bfd_reloc_notsupported
;
319 /* Relocate an MN10200 ELF section. */
321 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
322 contents
, relocs
, local_syms
, local_sections
)
324 struct bfd_link_info
*info
;
326 asection
*input_section
;
328 Elf_Internal_Rela
*relocs
;
329 Elf_Internal_Sym
*local_syms
;
330 asection
**local_sections
;
332 Elf_Internal_Shdr
*symtab_hdr
;
333 struct elf_link_hash_entry
**sym_hashes
;
334 Elf_Internal_Rela
*rel
, *relend
;
336 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
337 sym_hashes
= elf_sym_hashes (input_bfd
);
340 relend
= relocs
+ input_section
->reloc_count
;
341 for (; rel
< relend
; rel
++)
344 reloc_howto_type
*howto
;
345 unsigned long r_symndx
;
346 Elf_Internal_Sym
*sym
;
348 struct elf_link_hash_entry
*h
;
350 bfd_reloc_status_type r
;
352 r_symndx
= ELF32_R_SYM (rel
->r_info
);
353 r_type
= ELF32_R_TYPE (rel
->r_info
);
354 howto
= elf_mn10200_howto_table
+ r_type
;
356 if (info
->relocateable
)
358 /* This is a relocateable link. We don't have to change
359 anything, unless the reloc is against a section symbol,
360 in which case we have to adjust according to where the
361 section symbol winds up in the output section. */
362 if (r_symndx
< symtab_hdr
->sh_info
)
364 sym
= local_syms
+ r_symndx
;
365 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
367 sec
= local_sections
[r_symndx
];
368 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
375 /* This is a final link. */
379 if (r_symndx
< symtab_hdr
->sh_info
)
381 sym
= local_syms
+ r_symndx
;
382 sec
= local_sections
[r_symndx
];
383 relocation
= (sec
->output_section
->vma
389 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
390 while (h
->root
.type
== bfd_link_hash_indirect
391 || h
->root
.type
== bfd_link_hash_warning
)
392 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
393 if (h
->root
.type
== bfd_link_hash_defined
394 || h
->root
.type
== bfd_link_hash_defweak
)
396 sec
= h
->root
.u
.def
.section
;
397 relocation
= (h
->root
.u
.def
.value
398 + sec
->output_section
->vma
399 + sec
->output_offset
);
401 else if (h
->root
.type
== bfd_link_hash_undefweak
)
405 if (! ((*info
->callbacks
->undefined_symbol
)
406 (info
, h
->root
.root
.string
, input_bfd
,
407 input_section
, rel
->r_offset
, true)))
413 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
415 contents
, rel
->r_offset
,
416 relocation
, rel
->r_addend
,
417 info
, sec
, h
== NULL
);
419 if (r
!= bfd_reloc_ok
)
422 const char *msg
= (const char *) 0;
425 name
= h
->root
.root
.string
;
428 name
= (bfd_elf_string_from_elf_section
429 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
430 if (name
== NULL
|| *name
== '\0')
431 name
= bfd_section_name (input_bfd
, sec
);
436 case bfd_reloc_overflow
:
437 if (! ((*info
->callbacks
->reloc_overflow
)
438 (info
, name
, howto
->name
, (bfd_vma
) 0,
439 input_bfd
, input_section
, rel
->r_offset
)))
443 case bfd_reloc_undefined
:
444 if (! ((*info
->callbacks
->undefined_symbol
)
445 (info
, name
, input_bfd
, input_section
,
446 rel
->r_offset
, true)))
450 case bfd_reloc_outofrange
:
451 msg
= _("internal error: out of range error");
454 case bfd_reloc_notsupported
:
455 msg
= _("internal error: unsupported relocation error");
458 case bfd_reloc_dangerous
:
459 msg
= _("internal error: dangerous error");
463 msg
= _("internal error: unknown error");
467 if (!((*info
->callbacks
->warning
)
468 (info
, msg
, name
, input_bfd
, input_section
,
479 /* This function handles relaxing for the mn10200.
481 There's quite a few relaxing opportunites available on the mn10200:
483 * jsr:24 -> jsr:16 2 bytes
485 * jmp:24 -> jmp:16 2 bytes
486 * jmp:16 -> bra:8 1 byte
488 * If the previous instruction is a conditional branch
489 around the jump/bra, we may be able to reverse its condition
490 and change its target to the jump's target. The jump/bra
491 can then be deleted. 2 bytes
493 * mov abs24 -> mov abs16 2 byte savings
495 * Most instructions which accept imm24 can relax to imm16 2 bytes
496 - Most instructions which accept imm16 can relax to imm8 1 byte
498 * Most instructions which accept d24 can relax to d16 2 bytes
499 - Most instructions which accept d16 can relax to d8 1 byte
501 abs24, imm24, d24 all look the same at the reloc level. It
502 might make the code simpler if we had different relocs for
503 the various relaxable operand types.
505 We don't handle imm16->imm8 or d16->d8 as they're very rare
506 and somewhat more difficult to support. */
509 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
512 struct bfd_link_info
*link_info
;
515 Elf_Internal_Shdr
*symtab_hdr
;
516 Elf_Internal_Rela
*internal_relocs
;
517 Elf_Internal_Rela
*free_relocs
= NULL
;
518 Elf_Internal_Rela
*irel
, *irelend
;
519 bfd_byte
*contents
= NULL
;
520 bfd_byte
*free_contents
= NULL
;
521 Elf32_External_Sym
*extsyms
= NULL
;
522 Elf32_External_Sym
*free_extsyms
= NULL
;
524 /* Assume nothing changes. */
527 /* We don't have to do anything for a relocateable link, if
528 this section does not have relocs, or if this is not a
530 if (link_info
->relocateable
531 || (sec
->flags
& SEC_RELOC
) == 0
532 || sec
->reloc_count
== 0
533 || (sec
->flags
& SEC_CODE
) == 0)
536 /* If this is the first time we have been called for this section,
537 initialize the cooked size. */
538 if (sec
->_cooked_size
== 0)
539 sec
->_cooked_size
= sec
->_raw_size
;
541 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
543 /* Get a copy of the native relocations. */
544 internal_relocs
= (_bfd_elf32_link_read_relocs
545 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
546 link_info
->keep_memory
));
547 if (internal_relocs
== NULL
)
549 if (! link_info
->keep_memory
)
550 free_relocs
= internal_relocs
;
552 /* Walk through them looking for relaxing opportunities. */
553 irelend
= internal_relocs
+ sec
->reloc_count
;
554 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
558 /* If this isn't something that can be relaxed, then ignore
560 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
561 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
562 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
565 /* Get the section contents if we haven't done so already. */
566 if (contents
== NULL
)
568 /* Get cached copy if it exists. */
569 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
570 contents
= elf_section_data (sec
)->this_hdr
.contents
;
573 /* Go get them off disk. */
574 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
575 if (contents
== NULL
)
577 free_contents
= contents
;
579 if (! bfd_get_section_contents (abfd
, sec
, contents
,
580 (file_ptr
) 0, sec
->_raw_size
))
585 /* Read this BFD's symbols if we haven't done so already. */
588 /* Get cached copy if it exists. */
589 if (symtab_hdr
->contents
!= NULL
)
590 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
593 /* Go get them off disk. */
594 extsyms
= ((Elf32_External_Sym
*)
595 bfd_malloc (symtab_hdr
->sh_size
));
598 free_extsyms
= extsyms
;
599 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
600 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
601 != symtab_hdr
->sh_size
))
606 /* Get the value of the symbol referred to by the reloc. */
607 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
609 Elf_Internal_Sym isym
;
612 /* A local symbol. */
613 bfd_elf32_swap_symbol_in (abfd
,
614 extsyms
+ ELF32_R_SYM (irel
->r_info
),
617 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
618 symval
= (isym
.st_value
619 + sym_sec
->output_section
->vma
620 + sym_sec
->output_offset
);
625 struct elf_link_hash_entry
*h
;
627 /* An external symbol. */
628 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
629 h
= elf_sym_hashes (abfd
)[indx
];
630 BFD_ASSERT (h
!= NULL
);
631 if (h
->root
.type
!= bfd_link_hash_defined
632 && h
->root
.type
!= bfd_link_hash_defweak
)
634 /* This appears to be a reference to an undefined
635 symbol. Just ignore it--it will be caught by the
636 regular reloc processing. */
640 symval
= (h
->root
.u
.def
.value
641 + h
->root
.u
.def
.section
->output_section
->vma
642 + h
->root
.u
.def
.section
->output_offset
);
645 /* For simplicity of coding, we are going to modify the section
646 contents, the section relocs, and the BFD symbol table. We
647 must tell the rest of the code not to free up this
648 information. It would be possible to instead create a table
649 of changes which have to be made, as is done in coff-mips.c;
650 that would be more work, but would require less memory when
651 the linker is run. */
653 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
655 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
657 bfd_vma value
= symval
;
659 /* Deal with pc-relative gunk. */
660 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
661 value
-= (irel
->r_offset
+ 3);
662 value
+= irel
->r_addend
;
664 /* See if the value will fit in 16 bits, note the high value is
665 0x7fff + 2 as the target will be two bytes closer if we are
667 if ((long) value
< 0x8001 && (long) value
> -0x8000)
671 /* Get the opcode. */
672 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
674 if (code
!= 0xe0 && code
!= 0xe1)
677 /* Note that we've changed the relocs, section contents, etc. */
678 elf_section_data (sec
)->relocs
= internal_relocs
;
681 elf_section_data (sec
)->this_hdr
.contents
= contents
;
682 free_contents
= NULL
;
684 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
687 /* Fix the opcode. */
689 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
690 else if (code
== 0xe1)
691 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
693 /* Fix the relocation's type. */
694 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
697 /* The opcode got shorter too, so we have to fix the offset. */
700 /* Delete two bytes of data. */
701 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
702 irel
->r_offset
+ 1, 2))
705 /* That will change things, so, we should relax again.
706 Note that this is not required, and it may be slow. */
711 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
713 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
715 bfd_vma value
= symval
;
717 /* Deal with pc-relative gunk. */
718 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
719 value
-= (irel
->r_offset
+ 2);
720 value
+= irel
->r_addend
;
722 /* See if the value will fit in 8 bits, note the high value is
723 0x7f + 1 as the target will be one bytes closer if we are
725 if ((long) value
< 0x80 && (long) value
> -0x80)
729 /* Get the opcode. */
730 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
735 /* Note that we've changed the relocs, section contents, etc. */
736 elf_section_data (sec
)->relocs
= internal_relocs
;
739 elf_section_data (sec
)->this_hdr
.contents
= contents
;
740 free_contents
= NULL
;
742 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
745 /* Fix the opcode. */
746 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
748 /* Fix the relocation's type. */
749 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
752 /* Delete one byte of data. */
753 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
754 irel
->r_offset
+ 1, 1))
757 /* That will change things, so, we should relax again.
758 Note that this is not required, and it may be slow. */
763 /* Try to eliminate an unconditional 8 bit pc-relative branch
764 which immediately follows a conditional 8 bit pc-relative
765 branch around the unconditional branch.
772 This happens when the bCC can't reach lab2 at assembly time,
773 but due to other relaxations it can reach at link time. */
774 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
776 Elf_Internal_Rela
*nrel
;
777 bfd_vma value
= symval
;
780 /* Deal with pc-relative gunk. */
781 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
782 value
-= (irel
->r_offset
+ 1);
783 value
+= irel
->r_addend
;
785 /* Do nothing if this reloc is the last byte in the section. */
786 if (irel
->r_offset
== sec
->_cooked_size
)
789 /* See if the next instruction is an unconditional pc-relative
790 branch, more often than not this test will fail, so we
791 test it first to speed things up. */
792 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
796 /* Also make sure the next relocation applies to the next
797 instruction and that it's a pc-relative 8 bit branch. */
800 || irel
->r_offset
+ 2 != nrel
->r_offset
801 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
804 /* Make sure our destination immediately follows the
805 unconditional branch. */
806 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
807 + irel
->r_offset
+ 3))
810 /* Now make sure we are a conditional branch. This may not
811 be necessary, but why take the chance.
813 Note these checks assume that R_MN10200_PCREL8 relocs
814 only occur on bCC and bCCx insns. If they occured
815 elsewhere, we'd need to know the start of this insn
816 for this check to be accurate. */
817 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
818 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
819 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
820 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
821 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
822 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
823 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
826 /* We also have to be sure there is no symbol/label
827 at the unconditional branch. */
828 if (mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
,
832 /* Note that we've changed the relocs, section contents, etc. */
833 elf_section_data (sec
)->relocs
= internal_relocs
;
836 elf_section_data (sec
)->this_hdr
.contents
= contents
;
837 free_contents
= NULL
;
839 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
842 /* Reverse the condition of the first branch. */
900 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
902 /* Set the reloc type and symbol for the first branch
903 from the second branch. */
904 irel
->r_info
= nrel
->r_info
;
906 /* Make the reloc for the second branch a null reloc. */
907 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
910 /* Delete two bytes of data. */
911 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
912 irel
->r_offset
+ 1, 2))
915 /* That will change things, so, we should relax again.
916 Note that this is not required, and it may be slow. */
920 /* Try to turn a 24bit immediate, displacement or absolute address
921 into a 16bit immediate, displacement or absolute address. */
922 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
924 bfd_vma value
= symval
;
926 /* See if the value will fit in 16 bits.
927 We allow any 16bit match here. We prune those we can't
929 if ((long) value
< 0x7fff && (long) value
> -0x8000)
933 /* All insns which have 24bit operands are 5 bytes long,
934 the first byte will always be 0xf4, but we double check
937 /* Get the first opcode. */
938 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
943 /* Get the second opcode. */
944 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
948 /* mov imm24,dn -> mov imm16,dn */
950 /* Not safe if the high bit is on as relaxing may
951 move the value out of high mem and thus not fit
952 in a signed 16bit value. */
956 /* Note that we've changed the reldection contents, etc. */
957 elf_section_data (sec
)->relocs
= internal_relocs
;
960 elf_section_data (sec
)->this_hdr
.contents
= contents
;
961 free_contents
= NULL
;
963 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
966 /* Fix the opcode. */
967 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
968 contents
+ irel
->r_offset
- 2);
970 /* Fix the relocation's type. */
971 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
974 /* The opcode got shorter too, so we have to fix the
978 /* Delete two bytes of data. */
979 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
980 irel
->r_offset
+ 1, 2))
983 /* That will change things, so, we should relax again.
984 Note that this is not required, and it may be slow. */
988 /* mov imm24,an -> mov imm16,an
989 cmp imm24,an -> cmp imm16,an
990 mov (abs24),dn -> mov (abs16),dn
991 mov dn,(abs24) -> mov dn,(abs16)
992 movb dn,(abs24) -> movb dn,(abs16)
993 movbu (abs24),dn -> movbu (abs16),dn */
1000 /* Note that we've changed the reldection contents, etc. */
1001 elf_section_data (sec
)->relocs
= internal_relocs
;
1004 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1005 free_contents
= NULL
;
1007 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1008 free_extsyms
= NULL
;
1010 if ((code
& 0xfc) == 0x74)
1011 code
= 0xdc + (code
& 0x03);
1012 else if ((code
& 0xfc) == 0x7c)
1013 code
= 0xec + (code
& 0x03);
1014 else if ((code
& 0xfc) == 0xc0)
1015 code
= 0xc8 + (code
& 0x03);
1016 else if ((code
& 0xfc) == 0x40)
1017 code
= 0xc0 + (code
& 0x03);
1018 else if ((code
& 0xfc) == 0x44)
1019 code
= 0xc4 + (code
& 0x03);
1020 else if ((code
& 0xfc) == 0xc8)
1021 code
= 0xcc + (code
& 0x03);
1023 /* Fix the opcode. */
1024 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1026 /* Fix the relocation's type. */
1027 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1030 /* The opcode got shorter too, so we have to fix the
1032 irel
->r_offset
-= 1;
1034 /* Delete two bytes of data. */
1035 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1036 irel
->r_offset
+ 1, 2))
1039 /* That will change things, so, we should relax again.
1040 Note that this is not required, and it may be slow. */
1044 /* cmp imm24,dn -> cmp imm16,dn
1045 mov (abs24),an -> mov (abs16),an
1046 mov an,(abs24) -> mov an,(abs16)
1047 add imm24,dn -> add imm16,dn
1048 add imm24,an -> add imm16,an
1049 sub imm24,dn -> sub imm16,dn
1050 sub imm24,an -> sub imm16,an
1051 And all d24->d16 in memory ops. */
1068 /* Not safe if the high bit is on as relaxing may
1069 move the value out of high mem and thus not fit
1070 in a signed 16bit value. */
1071 if (((code
& 0xfc) == 0x78
1072 || (code
& 0xfc) == 0x60
1073 || (code
& 0xfc) == 0x64
1074 || (code
& 0xfc) == 0x68
1075 || (code
& 0xfc) == 0x6c
1076 || (code
& 0xfc) == 0x80
1077 || (code
& 0xfc) == 0xf0
1078 || (code
& 0xfc) == 0x00
1079 || (code
& 0xfc) == 0x10
1080 || (code
& 0xfc) == 0xb0
1081 || (code
& 0xfc) == 0x30
1082 || (code
& 0xfc) == 0xa0
1083 || (code
& 0xfc) == 0x20
1084 || (code
& 0xfc) == 0x90)
1085 && (value
& 0x8000) != 0)
1088 /* Note that we've changed the reldection contents, etc. */
1089 elf_section_data (sec
)->relocs
= internal_relocs
;
1092 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1093 free_contents
= NULL
;
1095 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1096 free_extsyms
= NULL
;
1098 /* Fix the opcode. */
1099 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1101 if ((code
& 0xfc) == 0x78)
1102 code
= 0x48 + (code
& 0x03);
1103 else if ((code
& 0xfc) == 0xd0)
1104 code
= 0x30 + (code
& 0x03);
1105 else if ((code
& 0xfc) == 0x50)
1106 code
= 0x20 + (code
& 0x03);
1107 else if ((code
& 0xfc) == 0x60)
1108 code
= 0x18 + (code
& 0x03);
1109 else if ((code
& 0xfc) == 0x64)
1110 code
= 0x08 + (code
& 0x03);
1111 else if ((code
& 0xfc) == 0x68)
1112 code
= 0x1c + (code
& 0x03);
1113 else if ((code
& 0xfc) == 0x6c)
1114 code
= 0x0c + (code
& 0x03);
1115 else if ((code
& 0xfc) == 0x80)
1116 code
= 0xc0 + (code
& 0x07);
1117 else if ((code
& 0xfc) == 0xf0)
1118 code
= 0xb0 + (code
& 0x07);
1119 else if ((code
& 0xfc) == 0x00)
1120 code
= 0x80 + (code
& 0x07);
1121 else if ((code
& 0xfc) == 0x10)
1122 code
= 0xa0 + (code
& 0x07);
1123 else if ((code
& 0xfc) == 0xb0)
1124 code
= 0x70 + (code
& 0x07);
1125 else if ((code
& 0xfc) == 0x30)
1126 code
= 0x60 + (code
& 0x07);
1127 else if ((code
& 0xfc) == 0xa0)
1128 code
= 0xd0 + (code
& 0x07);
1129 else if ((code
& 0xfc) == 0x20)
1130 code
= 0x90 + (code
& 0x07);
1131 else if ((code
& 0xfc) == 0x90)
1132 code
= 0x50 + (code
& 0x07);
1134 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1136 /* Fix the relocation's type. */
1137 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1140 /* Delete one bytes of data. */
1141 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1142 irel
->r_offset
+ 2, 1))
1145 /* That will change things, so, we should relax again.
1146 Note that this is not required, and it may be slow. */
1150 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1152 /* Note that we've changed the reldection contents, etc. */
1153 elf_section_data (sec
)->relocs
= internal_relocs
;
1156 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1157 free_contents
= NULL
;
1159 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1160 free_extsyms
= NULL
;
1162 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1163 contents
+ irel
->r_offset
- 2);
1165 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1166 contents
+ irel
->r_offset
- 1);
1168 /* Fix the relocation's type. */
1169 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1172 /* The reloc will be applied one byte in front of its
1173 current location. */
1174 irel
->r_offset
-= 1;
1176 /* Delete one bytes of data. */
1177 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1178 irel
->r_offset
+ 2, 1))
1181 /* That will change things, so, we should relax again.
1182 Note that this is not required, and it may be slow. */
1190 if (free_relocs
!= NULL
)
1196 if (free_contents
!= NULL
)
1198 if (! link_info
->keep_memory
)
1199 free (free_contents
);
1202 /* Cache the section contents for elf_link_input_bfd. */
1203 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1205 free_contents
= NULL
;
1208 if (free_extsyms
!= NULL
)
1210 if (! link_info
->keep_memory
)
1211 free (free_extsyms
);
1214 /* Cache the symbols for elf_link_input_bfd. */
1215 symtab_hdr
->contents
= extsyms
;
1217 free_extsyms
= NULL
;
1223 if (free_relocs
!= NULL
)
1225 if (free_contents
!= NULL
)
1226 free (free_contents
);
1227 if (free_extsyms
!= NULL
)
1228 free (free_extsyms
);
1232 /* Delete some bytes from a section while relaxing. */
1235 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1241 Elf_Internal_Shdr
*symtab_hdr
;
1242 Elf32_External_Sym
*extsyms
;
1245 Elf_Internal_Rela
*irel
, *irelend
;
1246 Elf_Internal_Rela
*irelalign
;
1248 Elf32_External_Sym
*esym
, *esymend
;
1249 struct elf_link_hash_entry
*sym_hash
;
1251 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1252 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1254 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1256 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1258 /* The deletion must stop at the next ALIGN reloc for an aligment
1259 power larger than the number of bytes we are deleting. */
1262 toaddr
= sec
->_cooked_size
;
1264 irel
= elf_section_data (sec
)->relocs
;
1265 irelend
= irel
+ sec
->reloc_count
;
1267 /* Actually delete the bytes. */
1268 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1269 sec
->_cooked_size
-= count
;
1271 /* Adjust all the relocs. */
1272 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1274 /* Get the new reloc address. */
1275 if ((irel
->r_offset
> addr
1276 && irel
->r_offset
< toaddr
))
1277 irel
->r_offset
-= count
;
1280 /* Adjust the local symbols defined in this section. */
1282 esymend
= esym
+ symtab_hdr
->sh_info
;
1283 for (; esym
< esymend
; esym
++)
1285 Elf_Internal_Sym isym
;
1287 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1289 if (isym
.st_shndx
== shndx
1290 && isym
.st_value
> addr
1291 && isym
.st_value
< toaddr
)
1293 isym
.st_value
-= count
;
1294 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1298 /* Now adjust the global symbols defined in this section. */
1299 esym
= extsyms
+ symtab_hdr
->sh_info
;
1300 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1301 for (index
= 0; esym
< esymend
; esym
++, index
++)
1303 Elf_Internal_Sym isym
;
1305 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1306 sym_hash
= elf_sym_hashes (abfd
)[index
];
1307 if (isym
.st_shndx
== shndx
1308 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1309 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1310 && (sym_hash
)->root
.u
.def
.section
== sec
1311 && (sym_hash
)->root
.u
.def
.value
> addr
1312 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1314 (sym_hash
)->root
.u
.def
.value
-= count
;
1321 /* Return true if a symbol exists at the given address, else return
1324 mn10200_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
1327 Elf32_External_Sym
*extsyms
;
1330 Elf_Internal_Shdr
*symtab_hdr
;
1332 Elf32_External_Sym
*esym
, *esymend
;
1333 struct elf_link_hash_entry
**sym_hash
, **sym_hash_end
;
1335 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1336 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1338 /* Examine all the symbols. */
1340 esymend
= esym
+ symtab_hdr
->sh_info
;
1341 for (; esym
< esymend
; esym
++)
1343 Elf_Internal_Sym isym
;
1345 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1347 if (isym
.st_shndx
== shndx
1348 && isym
.st_value
== addr
)
1352 sym_hash
= elf_sym_hashes (abfd
);
1353 sym_hash_end
= (sym_hash
1354 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1355 - symtab_hdr
->sh_info
));
1356 for (; sym_hash
< sym_hash_end
; sym_hash
++)
1358 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1359 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1360 && (*sym_hash
)->root
.u
.def
.section
== sec
1361 && (*sym_hash
)->root
.u
.def
.value
== addr
)
1367 /* This is a version of bfd_generic_get_relocated_section_contents
1368 which uses mn10200_elf_relocate_section. */
1371 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1372 data
, relocateable
, symbols
)
1374 struct bfd_link_info
*link_info
;
1375 struct bfd_link_order
*link_order
;
1377 boolean relocateable
;
1380 Elf_Internal_Shdr
*symtab_hdr
;
1381 asection
*input_section
= link_order
->u
.indirect
.section
;
1382 bfd
*input_bfd
= input_section
->owner
;
1383 asection
**sections
= NULL
;
1384 Elf_Internal_Rela
*internal_relocs
= NULL
;
1385 Elf32_External_Sym
*external_syms
= NULL
;
1386 Elf_Internal_Sym
*internal_syms
= NULL
;
1388 /* We only need to handle the case of relaxing, or of having a
1389 particular set of section contents, specially. */
1391 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1392 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1397 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1399 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1400 input_section
->_raw_size
);
1402 if ((input_section
->flags
& SEC_RELOC
) != 0
1403 && input_section
->reloc_count
> 0)
1405 Elf_Internal_Sym
*isymp
;
1407 Elf32_External_Sym
*esym
, *esymend
;
1409 if (symtab_hdr
->contents
!= NULL
)
1410 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1413 external_syms
= ((Elf32_External_Sym
*)
1414 bfd_malloc (symtab_hdr
->sh_info
1415 * sizeof (Elf32_External_Sym
)));
1416 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1418 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1419 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
1420 symtab_hdr
->sh_info
, input_bfd
)
1421 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
1425 internal_relocs
= (_bfd_elf32_link_read_relocs
1426 (input_bfd
, input_section
, (PTR
) NULL
,
1427 (Elf_Internal_Rela
*) NULL
, false));
1428 if (internal_relocs
== NULL
)
1431 internal_syms
= ((Elf_Internal_Sym
*)
1432 bfd_malloc (symtab_hdr
->sh_info
1433 * sizeof (Elf_Internal_Sym
)));
1434 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1437 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
1438 * sizeof (asection
*));
1439 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
1442 isymp
= internal_syms
;
1444 esym
= external_syms
;
1445 esymend
= esym
+ symtab_hdr
->sh_info
;
1446 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
1450 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
1452 if (isymp
->st_shndx
== SHN_UNDEF
)
1453 isec
= bfd_und_section_ptr
;
1454 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
1455 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1456 else if (isymp
->st_shndx
== SHN_ABS
)
1457 isec
= bfd_abs_section_ptr
;
1458 else if (isymp
->st_shndx
== SHN_COMMON
)
1459 isec
= bfd_com_section_ptr
;
1469 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1470 input_section
, data
, internal_relocs
,
1471 internal_syms
, sections
))
1474 if (sections
!= NULL
)
1477 if (internal_syms
!= NULL
)
1478 free (internal_syms
);
1479 internal_syms
= NULL
;
1480 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1481 free (external_syms
);
1482 external_syms
= NULL
;
1483 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1484 free (internal_relocs
);
1485 internal_relocs
= NULL
;
1491 if (internal_relocs
!= NULL
1492 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1493 free (internal_relocs
);
1494 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1495 free (external_syms
);
1496 if (internal_syms
!= NULL
)
1497 free (internal_syms
);
1498 if (sections
!= NULL
)
1503 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1504 #define TARGET_LITTLE_NAME "elf32-mn10200"
1505 #define ELF_ARCH bfd_arch_mn10200
1506 #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
1507 #define ELF_MAXPAGESIZE 0x1000
1509 #define elf_info_to_howto mn10200_info_to_howto
1510 #define elf_info_to_howto_rel 0
1511 #define elf_backend_relocate_section mn10200_elf_relocate_section
1512 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1513 #define bfd_elf32_bfd_get_relocated_section_contents \
1514 mn10200_elf_get_relocated_section_contents
1516 #define elf_symbol_leading_char '_'
1518 #include "elf32-target.h"