1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 Free Software Foundation, Inc.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
29 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
30 static void mn10200_info_to_howto
31 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
32 static bfd_boolean mn10200_elf_relax_delete_bytes
33 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
34 static bfd_boolean mn10200_elf_symbol_address_p
35 PARAMS ((bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
));
36 static bfd_reloc_status_type mn10200_elf_final_link_relocate
37 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*,
38 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
39 struct bfd_link_info
*, asection
*, int));
40 static bfd_boolean mn10200_elf_relocate_section
41 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
42 bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*,
44 static bfd_boolean mn10200_elf_relax_section
45 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
46 static bfd_byte
* mn10200_elf_get_relocated_section_contents
47 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
48 bfd_byte
*, bfd_boolean
, asymbol
**));
62 static reloc_howto_type elf_mn10200_howto_table
[] = {
63 /* Dummy relocation. Does nothing. */
64 HOWTO (R_MN10200_NONE
,
70 complain_overflow_bitfield
,
71 bfd_elf_generic_reloc
,
77 /* Standard 32 bit reloc. */
84 complain_overflow_bitfield
,
85 bfd_elf_generic_reloc
,
91 /* Standard 16 bit reloc. */
98 complain_overflow_bitfield
,
99 bfd_elf_generic_reloc
,
105 /* Standard 8 bit reloc. */
112 complain_overflow_bitfield
,
113 bfd_elf_generic_reloc
,
119 /* Standard 24 bit reloc. */
126 complain_overflow_bitfield
,
127 bfd_elf_generic_reloc
,
133 /* Simple 8 pc-relative reloc. */
134 HOWTO (R_MN10200_PCREL8
,
140 complain_overflow_bitfield
,
141 bfd_elf_generic_reloc
,
147 /* Simple 16 pc-relative reloc. */
148 HOWTO (R_MN10200_PCREL16
,
154 complain_overflow_bitfield
,
155 bfd_elf_generic_reloc
,
161 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
162 to get the pc-relative offset correct. */
163 HOWTO (R_MN10200_PCREL24
,
169 complain_overflow_bitfield
,
170 bfd_elf_generic_reloc
,
178 struct mn10200_reloc_map
{
179 bfd_reloc_code_real_type bfd_reloc_val
;
180 unsigned char elf_reloc_val
;
183 static const struct mn10200_reloc_map mn10200_reloc_map
[] = {
184 { BFD_RELOC_NONE
, R_MN10200_NONE
, },
185 { BFD_RELOC_32
, R_MN10200_32
, },
186 { BFD_RELOC_16
, R_MN10200_16
, },
187 { BFD_RELOC_8
, R_MN10200_8
, },
188 { BFD_RELOC_24
, R_MN10200_24
, },
189 { BFD_RELOC_8_PCREL
, R_MN10200_PCREL8
, },
190 { BFD_RELOC_16_PCREL
, R_MN10200_PCREL16
, },
191 { BFD_RELOC_24_PCREL
, R_MN10200_PCREL24
, },
194 static reloc_howto_type
*
195 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
196 bfd
*abfd ATTRIBUTE_UNUSED
;
197 bfd_reloc_code_real_type code
;
202 i
< sizeof (mn10200_reloc_map
) / sizeof (struct mn10200_reloc_map
);
205 if (mn10200_reloc_map
[i
].bfd_reloc_val
== code
)
206 return &elf_mn10200_howto_table
[mn10200_reloc_map
[i
].elf_reloc_val
];
212 static reloc_howto_type
*
213 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
219 i
< (sizeof (elf_mn10200_howto_table
)
220 / sizeof (elf_mn10200_howto_table
[0]));
222 if (elf_mn10200_howto_table
[i
].name
!= NULL
223 && strcasecmp (elf_mn10200_howto_table
[i
].name
, r_name
) == 0)
224 return &elf_mn10200_howto_table
[i
];
229 /* Set the howto pointer for an MN10200 ELF reloc. */
232 mn10200_info_to_howto (abfd
, cache_ptr
, dst
)
233 bfd
*abfd ATTRIBUTE_UNUSED
;
235 Elf_Internal_Rela
*dst
;
239 r_type
= ELF32_R_TYPE (dst
->r_info
);
240 BFD_ASSERT (r_type
< (unsigned int) R_MN10200_MAX
);
241 cache_ptr
->howto
= &elf_mn10200_howto_table
[r_type
];
244 /* Perform a relocation as part of a final link. */
246 static bfd_reloc_status_type
247 mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
248 input_section
, contents
, offset
, value
,
249 addend
, info
, sym_sec
, is_local
)
250 reloc_howto_type
*howto
;
252 bfd
*output_bfd ATTRIBUTE_UNUSED
;
253 asection
*input_section
;
258 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
259 asection
*sym_sec ATTRIBUTE_UNUSED
;
260 int is_local ATTRIBUTE_UNUSED
;
262 unsigned long r_type
= howto
->type
;
263 bfd_byte
*hit_data
= contents
+ offset
;
273 bfd_put_32 (input_bfd
, value
, hit_data
);
279 if ((long) value
> 0x7fff || (long) value
< -0x8000)
280 return bfd_reloc_overflow
;
282 bfd_put_16 (input_bfd
, value
, hit_data
);
288 if ((long) value
> 0x7f || (long) value
< -0x80)
289 return bfd_reloc_overflow
;
291 bfd_put_8 (input_bfd
, value
, hit_data
);
297 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
298 return bfd_reloc_overflow
;
301 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
302 bfd_put_32 (input_bfd
, value
, hit_data
);
305 case R_MN10200_PCREL8
:
306 value
-= (input_section
->output_section
->vma
307 + input_section
->output_offset
);
308 value
-= (offset
+ 1);
311 if ((long) value
> 0xff || (long) value
< -0x100)
312 return bfd_reloc_overflow
;
314 bfd_put_8 (input_bfd
, value
, hit_data
);
317 case R_MN10200_PCREL16
:
318 value
-= (input_section
->output_section
->vma
319 + input_section
->output_offset
);
320 value
-= (offset
+ 2);
323 if ((long) value
> 0xffff || (long) value
< -0x10000)
324 return bfd_reloc_overflow
;
326 bfd_put_16 (input_bfd
, value
, hit_data
);
329 case R_MN10200_PCREL24
:
330 value
-= (input_section
->output_section
->vma
331 + input_section
->output_offset
);
332 value
-= (offset
+ 3);
335 if ((long) value
> 0xffffff || (long) value
< -0x1000000)
336 return bfd_reloc_overflow
;
339 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
340 bfd_put_32 (input_bfd
, value
, hit_data
);
344 return bfd_reloc_notsupported
;
348 /* Relocate an MN10200 ELF section. */
350 mn10200_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
351 contents
, relocs
, local_syms
, local_sections
)
353 struct bfd_link_info
*info
;
355 asection
*input_section
;
357 Elf_Internal_Rela
*relocs
;
358 Elf_Internal_Sym
*local_syms
;
359 asection
**local_sections
;
361 Elf_Internal_Shdr
*symtab_hdr
;
362 struct elf_link_hash_entry
**sym_hashes
;
363 Elf_Internal_Rela
*rel
, *relend
;
365 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
366 sym_hashes
= elf_sym_hashes (input_bfd
);
369 relend
= relocs
+ input_section
->reloc_count
;
370 for (; rel
< relend
; rel
++)
373 reloc_howto_type
*howto
;
374 unsigned long r_symndx
;
375 Elf_Internal_Sym
*sym
;
377 struct elf_link_hash_entry
*h
;
379 bfd_reloc_status_type r
;
381 r_symndx
= ELF32_R_SYM (rel
->r_info
);
382 r_type
= ELF32_R_TYPE (rel
->r_info
);
383 howto
= elf_mn10200_howto_table
+ r_type
;
388 if (r_symndx
< symtab_hdr
->sh_info
)
390 sym
= local_syms
+ r_symndx
;
391 sec
= local_sections
[r_symndx
];
392 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
396 bfd_boolean unresolved_reloc
, warned
;
398 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
399 r_symndx
, symtab_hdr
, sym_hashes
,
401 unresolved_reloc
, warned
);
404 if (sec
!= NULL
&& elf_discarded_section (sec
))
405 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
406 rel
, relend
, howto
, contents
);
408 if (info
->relocatable
)
411 r
= mn10200_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
413 contents
, rel
->r_offset
,
414 relocation
, rel
->r_addend
,
415 info
, sec
, h
== NULL
);
417 if (r
!= bfd_reloc_ok
)
420 const char *msg
= (const char *) 0;
423 name
= h
->root
.root
.string
;
426 name
= (bfd_elf_string_from_elf_section
427 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
428 if (name
== NULL
|| *name
== '\0')
429 name
= bfd_section_name (input_bfd
, sec
);
434 case bfd_reloc_overflow
:
435 if (! ((*info
->callbacks
->reloc_overflow
)
436 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
437 (bfd_vma
) 0, input_bfd
, input_section
,
442 case bfd_reloc_undefined
:
443 if (! ((*info
->callbacks
->undefined_symbol
)
444 (info
, name
, input_bfd
, input_section
,
445 rel
->r_offset
, TRUE
)))
449 case bfd_reloc_outofrange
:
450 msg
= _("internal error: out of range error");
453 case bfd_reloc_notsupported
:
454 msg
= _("internal error: unsupported relocation error");
457 case bfd_reloc_dangerous
:
458 msg
= _("internal error: dangerous error");
462 msg
= _("internal error: unknown error");
466 if (!((*info
->callbacks
->warning
)
467 (info
, msg
, name
, input_bfd
, input_section
,
478 /* This function handles relaxing for the mn10200.
480 There are quite a few relaxing opportunities available on the mn10200:
482 * jsr:24 -> jsr:16 2 bytes
484 * jmp:24 -> jmp:16 2 bytes
485 * jmp:16 -> bra:8 1 byte
487 * If the previous instruction is a conditional branch
488 around the jump/bra, we may be able to reverse its condition
489 and change its target to the jump's target. The jump/bra
490 can then be deleted. 2 bytes
492 * mov abs24 -> mov abs16 2 byte savings
494 * Most instructions which accept imm24 can relax to imm16 2 bytes
495 - Most instructions which accept imm16 can relax to imm8 1 byte
497 * Most instructions which accept d24 can relax to d16 2 bytes
498 - Most instructions which accept d16 can relax to d8 1 byte
500 abs24, imm24, d24 all look the same at the reloc level. It
501 might make the code simpler if we had different relocs for
502 the various relaxable operand types.
504 We don't handle imm16->imm8 or d16->d8 as they're very rare
505 and somewhat more difficult to support. */
508 mn10200_elf_relax_section (abfd
, sec
, link_info
, again
)
511 struct bfd_link_info
*link_info
;
514 Elf_Internal_Shdr
*symtab_hdr
;
515 Elf_Internal_Rela
*internal_relocs
;
516 Elf_Internal_Rela
*irel
, *irelend
;
517 bfd_byte
*contents
= NULL
;
518 Elf_Internal_Sym
*isymbuf
= NULL
;
520 /* Assume nothing changes. */
523 /* We don't have to do anything for a relocatable link, if
524 this section does not have relocs, or if this is not a
526 if (link_info
->relocatable
527 || (sec
->flags
& SEC_RELOC
) == 0
528 || sec
->reloc_count
== 0
529 || (sec
->flags
& SEC_CODE
) == 0)
532 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
534 /* Get a copy of the native relocations. */
535 internal_relocs
= (_bfd_elf_link_read_relocs
536 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
537 link_info
->keep_memory
));
538 if (internal_relocs
== NULL
)
541 /* Walk through them looking for relaxing opportunities. */
542 irelend
= internal_relocs
+ sec
->reloc_count
;
543 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
547 /* If this isn't something that can be relaxed, then ignore
549 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_NONE
550 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_8
551 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_MAX
)
554 /* Get the section contents if we haven't done so already. */
555 if (contents
== NULL
)
557 /* Get cached copy if it exists. */
558 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
559 contents
= elf_section_data (sec
)->this_hdr
.contents
;
562 /* Go get them off disk. */
563 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
568 /* Read this BFD's local symbols if we haven't done so already. */
569 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
571 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
573 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
574 symtab_hdr
->sh_info
, 0,
580 /* Get the value of the symbol referred to by the reloc. */
581 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
583 /* A local symbol. */
584 Elf_Internal_Sym
*isym
;
587 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
588 if (isym
->st_shndx
== SHN_UNDEF
)
589 sym_sec
= bfd_und_section_ptr
;
590 else if (isym
->st_shndx
== SHN_ABS
)
591 sym_sec
= bfd_abs_section_ptr
;
592 else if (isym
->st_shndx
== SHN_COMMON
)
593 sym_sec
= bfd_com_section_ptr
;
595 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
596 symval
= (isym
->st_value
597 + sym_sec
->output_section
->vma
598 + sym_sec
->output_offset
);
603 struct elf_link_hash_entry
*h
;
605 /* An external symbol. */
606 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
607 h
= elf_sym_hashes (abfd
)[indx
];
608 BFD_ASSERT (h
!= NULL
);
609 if (h
->root
.type
!= bfd_link_hash_defined
610 && h
->root
.type
!= bfd_link_hash_defweak
)
612 /* This appears to be a reference to an undefined
613 symbol. Just ignore it--it will be caught by the
614 regular reloc processing. */
618 symval
= (h
->root
.u
.def
.value
619 + h
->root
.u
.def
.section
->output_section
->vma
620 + h
->root
.u
.def
.section
->output_offset
);
623 /* For simplicity of coding, we are going to modify the section
624 contents, the section relocs, and the BFD symbol table. We
625 must tell the rest of the code not to free up this
626 information. It would be possible to instead create a table
627 of changes which have to be made, as is done in coff-mips.c;
628 that would be more work, but would require less memory when
629 the linker is run. */
631 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
633 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL24
)
635 bfd_vma value
= symval
;
637 /* Deal with pc-relative gunk. */
638 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
639 value
-= (irel
->r_offset
+ 3);
640 value
+= irel
->r_addend
;
642 /* See if the value will fit in 16 bits, note the high value is
643 0x7fff + 2 as the target will be two bytes closer if we are
645 if ((long) value
< 0x8001 && (long) value
> -0x8000)
649 /* Get the opcode. */
650 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
652 if (code
!= 0xe0 && code
!= 0xe1)
655 /* Note that we've changed the relocs, section contents, etc. */
656 elf_section_data (sec
)->relocs
= internal_relocs
;
657 elf_section_data (sec
)->this_hdr
.contents
= contents
;
658 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
660 /* Fix the opcode. */
662 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 2);
663 else if (code
== 0xe1)
664 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 2);
666 /* Fix the relocation's type. */
667 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
670 /* The opcode got shorter too, so we have to fix the offset. */
673 /* Delete two bytes of data. */
674 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
675 irel
->r_offset
+ 1, 2))
678 /* That will change things, so, we should relax again.
679 Note that this is not required, and it may be slow. */
684 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
686 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL16
)
688 bfd_vma value
= symval
;
690 /* Deal with pc-relative gunk. */
691 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
692 value
-= (irel
->r_offset
+ 2);
693 value
+= irel
->r_addend
;
695 /* See if the value will fit in 8 bits, note the high value is
696 0x7f + 1 as the target will be one bytes closer if we are
698 if ((long) value
< 0x80 && (long) value
> -0x80)
702 /* Get the opcode. */
703 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
708 /* Note that we've changed the relocs, section contents, etc. */
709 elf_section_data (sec
)->relocs
= internal_relocs
;
710 elf_section_data (sec
)->this_hdr
.contents
= contents
;
711 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
713 /* Fix the opcode. */
714 bfd_put_8 (abfd
, 0xea, contents
+ irel
->r_offset
- 1);
716 /* Fix the relocation's type. */
717 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
720 /* Delete one byte of data. */
721 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
722 irel
->r_offset
+ 1, 1))
725 /* That will change things, so, we should relax again.
726 Note that this is not required, and it may be slow. */
731 /* Try to eliminate an unconditional 8 bit pc-relative branch
732 which immediately follows a conditional 8 bit pc-relative
733 branch around the unconditional branch.
740 This happens when the bCC can't reach lab2 at assembly time,
741 but due to other relaxations it can reach at link time. */
742 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_PCREL8
)
744 Elf_Internal_Rela
*nrel
;
745 bfd_vma value
= symval
;
748 /* Deal with pc-relative gunk. */
749 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
750 value
-= (irel
->r_offset
+ 1);
751 value
+= irel
->r_addend
;
753 /* Do nothing if this reloc is the last byte in the section. */
754 if (irel
->r_offset
== sec
->size
)
757 /* See if the next instruction is an unconditional pc-relative
758 branch, more often than not this test will fail, so we
759 test it first to speed things up. */
760 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
764 /* Also make sure the next relocation applies to the next
765 instruction and that it's a pc-relative 8 bit branch. */
768 || irel
->r_offset
+ 2 != nrel
->r_offset
769 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10200_PCREL8
)
772 /* Make sure our destination immediately follows the
773 unconditional branch. */
774 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
775 + irel
->r_offset
+ 3))
778 /* Now make sure we are a conditional branch. This may not
779 be necessary, but why take the chance.
781 Note these checks assume that R_MN10200_PCREL8 relocs
782 only occur on bCC and bCCx insns. If they occured
783 elsewhere, we'd need to know the start of this insn
784 for this check to be accurate. */
785 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
786 if (code
!= 0xe0 && code
!= 0xe1 && code
!= 0xe2
787 && code
!= 0xe3 && code
!= 0xe4 && code
!= 0xe5
788 && code
!= 0xe6 && code
!= 0xe7 && code
!= 0xe8
789 && code
!= 0xe9 && code
!= 0xec && code
!= 0xed
790 && code
!= 0xee && code
!= 0xef && code
!= 0xfc
791 && code
!= 0xfd && code
!= 0xfe && code
!= 0xff)
794 /* We also have to be sure there is no symbol/label
795 at the unconditional branch. */
796 if (mn10200_elf_symbol_address_p (abfd
, sec
, isymbuf
,
800 /* Note that we've changed the relocs, section contents, etc. */
801 elf_section_data (sec
)->relocs
= internal_relocs
;
802 elf_section_data (sec
)->this_hdr
.contents
= contents
;
803 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
805 /* Reverse the condition of the first branch. */
863 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
865 /* Set the reloc type and symbol for the first branch
866 from the second branch. */
867 irel
->r_info
= nrel
->r_info
;
869 /* Make the reloc for the second branch a null reloc. */
870 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
873 /* Delete two bytes of data. */
874 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
875 irel
->r_offset
+ 1, 2))
878 /* That will change things, so, we should relax again.
879 Note that this is not required, and it may be slow. */
883 /* Try to turn a 24bit immediate, displacement or absolute address
884 into a 16bit immediate, displacement or absolute address. */
885 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10200_24
)
887 bfd_vma value
= symval
;
889 /* See if the value will fit in 16 bits.
890 We allow any 16bit match here. We prune those we can't
892 if ((long) value
< 0x7fff && (long) value
> -0x8000)
896 /* All insns which have 24bit operands are 5 bytes long,
897 the first byte will always be 0xf4, but we double check
900 /* Get the first opcode. */
901 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
906 /* Get the second opcode. */
907 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
911 /* mov imm24,dn -> mov imm16,dn */
913 /* Not safe if the high bit is on as relaxing may
914 move the value out of high mem and thus not fit
915 in a signed 16bit value. */
919 /* Note that we've changed the relocation contents, etc. */
920 elf_section_data (sec
)->relocs
= internal_relocs
;
921 elf_section_data (sec
)->this_hdr
.contents
= contents
;
922 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
924 /* Fix the opcode. */
925 bfd_put_8 (abfd
, 0xf8 + (code
& 0x03),
926 contents
+ irel
->r_offset
- 2);
928 /* Fix the relocation's type. */
929 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
932 /* The opcode got shorter too, so we have to fix the
936 /* Delete two bytes of data. */
937 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
938 irel
->r_offset
+ 1, 2))
941 /* That will change things, so, we should relax again.
942 Note that this is not required, and it may be slow. */
946 /* mov imm24,an -> mov imm16,an
947 cmp imm24,an -> cmp imm16,an
948 mov (abs24),dn -> mov (abs16),dn
949 mov dn,(abs24) -> mov dn,(abs16)
950 movb dn,(abs24) -> movb dn,(abs16)
951 movbu (abs24),dn -> movbu (abs16),dn */
958 /* Note that we've changed the relocation contents, etc. */
959 elf_section_data (sec
)->relocs
= internal_relocs
;
960 elf_section_data (sec
)->this_hdr
.contents
= contents
;
961 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
963 if ((code
& 0xfc) == 0x74)
964 code
= 0xdc + (code
& 0x03);
965 else if ((code
& 0xfc) == 0x7c)
966 code
= 0xec + (code
& 0x03);
967 else if ((code
& 0xfc) == 0xc0)
968 code
= 0xc8 + (code
& 0x03);
969 else if ((code
& 0xfc) == 0x40)
970 code
= 0xc0 + (code
& 0x03);
971 else if ((code
& 0xfc) == 0x44)
972 code
= 0xc4 + (code
& 0x03);
973 else if ((code
& 0xfc) == 0xc8)
974 code
= 0xcc + (code
& 0x03);
976 /* Fix the opcode. */
977 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
979 /* Fix the relocation's type. */
980 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
983 /* The opcode got shorter too, so we have to fix the
987 /* Delete two bytes of data. */
988 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
989 irel
->r_offset
+ 1, 2))
992 /* That will change things, so, we should relax again.
993 Note that this is not required, and it may be slow. */
997 /* cmp imm24,dn -> cmp imm16,dn
998 mov (abs24),an -> mov (abs16),an
999 mov an,(abs24) -> mov an,(abs16)
1000 add imm24,dn -> add imm16,dn
1001 add imm24,an -> add imm16,an
1002 sub imm24,dn -> sub imm16,dn
1003 sub imm24,an -> sub imm16,an
1004 And all d24->d16 in memory ops. */
1021 /* Not safe if the high bit is on as relaxing may
1022 move the value out of high mem and thus not fit
1023 in a signed 16bit value. */
1024 if (((code
& 0xfc) == 0x78
1025 || (code
& 0xfc) == 0x60
1026 || (code
& 0xfc) == 0x64
1027 || (code
& 0xfc) == 0x68
1028 || (code
& 0xfc) == 0x6c
1029 || (code
& 0xfc) == 0x80
1030 || (code
& 0xfc) == 0xf0
1031 || (code
& 0xfc) == 0x00
1032 || (code
& 0xfc) == 0x10
1033 || (code
& 0xfc) == 0xb0
1034 || (code
& 0xfc) == 0x30
1035 || (code
& 0xfc) == 0xa0
1036 || (code
& 0xfc) == 0x20
1037 || (code
& 0xfc) == 0x90)
1038 && (value
& 0x8000) != 0)
1041 /* Note that we've changed the relocation contents, etc. */
1042 elf_section_data (sec
)->relocs
= internal_relocs
;
1043 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1044 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1046 /* Fix the opcode. */
1047 bfd_put_8 (abfd
, 0xf7, contents
+ irel
->r_offset
- 2);
1049 if ((code
& 0xfc) == 0x78)
1050 code
= 0x48 + (code
& 0x03);
1051 else if ((code
& 0xfc) == 0xd0)
1052 code
= 0x30 + (code
& 0x03);
1053 else if ((code
& 0xfc) == 0x50)
1054 code
= 0x20 + (code
& 0x03);
1055 else if ((code
& 0xfc) == 0x60)
1056 code
= 0x18 + (code
& 0x03);
1057 else if ((code
& 0xfc) == 0x64)
1058 code
= 0x08 + (code
& 0x03);
1059 else if ((code
& 0xfc) == 0x68)
1060 code
= 0x1c + (code
& 0x03);
1061 else if ((code
& 0xfc) == 0x6c)
1062 code
= 0x0c + (code
& 0x03);
1063 else if ((code
& 0xfc) == 0x80)
1064 code
= 0xc0 + (code
& 0x07);
1065 else if ((code
& 0xfc) == 0xf0)
1066 code
= 0xb0 + (code
& 0x07);
1067 else if ((code
& 0xfc) == 0x00)
1068 code
= 0x80 + (code
& 0x07);
1069 else if ((code
& 0xfc) == 0x10)
1070 code
= 0xa0 + (code
& 0x07);
1071 else if ((code
& 0xfc) == 0xb0)
1072 code
= 0x70 + (code
& 0x07);
1073 else if ((code
& 0xfc) == 0x30)
1074 code
= 0x60 + (code
& 0x07);
1075 else if ((code
& 0xfc) == 0xa0)
1076 code
= 0xd0 + (code
& 0x07);
1077 else if ((code
& 0xfc) == 0x20)
1078 code
= 0x90 + (code
& 0x07);
1079 else if ((code
& 0xfc) == 0x90)
1080 code
= 0x50 + (code
& 0x07);
1082 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1084 /* Fix the relocation's type. */
1085 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1088 /* Delete one bytes of data. */
1089 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1090 irel
->r_offset
+ 2, 1))
1093 /* That will change things, so, we should relax again.
1094 Note that this is not required, and it may be slow. */
1098 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1100 /* Note that we've changed the reldection contents, etc. */
1101 elf_section_data (sec
)->relocs
= internal_relocs
;
1102 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1103 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1105 bfd_put_8 (abfd
, 0xcc + (code
& 0x03),
1106 contents
+ irel
->r_offset
- 2);
1108 bfd_put_8 (abfd
, 0xb8 + (code
& 0x03),
1109 contents
+ irel
->r_offset
- 1);
1111 /* Fix the relocation's type. */
1112 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1115 /* The reloc will be applied one byte in front of its
1116 current location. */
1117 irel
->r_offset
-= 1;
1119 /* Delete one bytes of data. */
1120 if (!mn10200_elf_relax_delete_bytes (abfd
, sec
,
1121 irel
->r_offset
+ 2, 1))
1124 /* That will change things, so, we should relax again.
1125 Note that this is not required, and it may be slow. */
1134 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1136 if (! link_info
->keep_memory
)
1140 /* Cache the symbols for elf_link_input_bfd. */
1141 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1145 if (contents
!= NULL
1146 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1148 if (! link_info
->keep_memory
)
1152 /* Cache the section contents for elf_link_input_bfd. */
1153 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1157 if (internal_relocs
!= NULL
1158 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1159 free (internal_relocs
);
1165 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1167 if (contents
!= NULL
1168 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1170 if (internal_relocs
!= NULL
1171 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1172 free (internal_relocs
);
1177 /* Delete some bytes from a section while relaxing. */
1180 mn10200_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
1186 Elf_Internal_Shdr
*symtab_hdr
;
1187 unsigned int sec_shndx
;
1189 Elf_Internal_Rela
*irel
, *irelend
;
1191 Elf_Internal_Sym
*isym
;
1192 Elf_Internal_Sym
*isymend
;
1193 struct elf_link_hash_entry
**sym_hashes
;
1194 struct elf_link_hash_entry
**end_hashes
;
1195 unsigned int symcount
;
1197 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1199 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1203 irel
= elf_section_data (sec
)->relocs
;
1204 irelend
= irel
+ sec
->reloc_count
;
1206 /* Actually delete the bytes. */
1207 memmove (contents
+ addr
, contents
+ addr
+ count
,
1208 (size_t) (toaddr
- addr
- count
));
1211 /* Adjust all the relocs. */
1212 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1214 /* Get the new reloc address. */
1215 if ((irel
->r_offset
> addr
1216 && irel
->r_offset
< toaddr
))
1217 irel
->r_offset
-= count
;
1220 /* Adjust the local symbols defined in this section. */
1221 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1222 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1223 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1225 if (isym
->st_shndx
== sec_shndx
1226 && isym
->st_value
> addr
1227 && isym
->st_value
< toaddr
)
1228 isym
->st_value
-= count
;
1231 /* Now adjust the global symbols defined in this section. */
1232 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1233 - symtab_hdr
->sh_info
);
1234 sym_hashes
= elf_sym_hashes (abfd
);
1235 end_hashes
= sym_hashes
+ symcount
;
1236 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1238 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1239 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1240 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1241 && sym_hash
->root
.u
.def
.section
== sec
1242 && sym_hash
->root
.u
.def
.value
> addr
1243 && sym_hash
->root
.u
.def
.value
< toaddr
)
1245 sym_hash
->root
.u
.def
.value
-= count
;
1252 /* Return TRUE if a symbol exists at the given address, else return
1255 mn10200_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
1258 Elf_Internal_Sym
*isym
;
1261 Elf_Internal_Shdr
*symtab_hdr
;
1262 unsigned int sec_shndx
;
1263 Elf_Internal_Sym
*isymend
;
1264 struct elf_link_hash_entry
**sym_hashes
;
1265 struct elf_link_hash_entry
**end_hashes
;
1266 unsigned int symcount
;
1268 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1270 /* Examine all the local symbols. */
1271 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1272 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
1274 if (isym
->st_shndx
== sec_shndx
1275 && isym
->st_value
== addr
)
1279 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1280 - symtab_hdr
->sh_info
);
1281 sym_hashes
= elf_sym_hashes (abfd
);
1282 end_hashes
= sym_hashes
+ symcount
;
1283 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1285 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1286 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1287 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1288 && sym_hash
->root
.u
.def
.section
== sec
1289 && sym_hash
->root
.u
.def
.value
== addr
)
1296 /* This is a version of bfd_generic_get_relocated_section_contents
1297 which uses mn10200_elf_relocate_section. */
1300 mn10200_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1301 data
, relocatable
, symbols
)
1303 struct bfd_link_info
*link_info
;
1304 struct bfd_link_order
*link_order
;
1306 bfd_boolean relocatable
;
1309 Elf_Internal_Shdr
*symtab_hdr
;
1310 asection
*input_section
= link_order
->u
.indirect
.section
;
1311 bfd
*input_bfd
= input_section
->owner
;
1312 asection
**sections
= NULL
;
1313 Elf_Internal_Rela
*internal_relocs
= NULL
;
1314 Elf_Internal_Sym
*isymbuf
= NULL
;
1316 /* We only need to handle the case of relaxing, or of having a
1317 particular set of section contents, specially. */
1319 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1320 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1325 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1327 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1328 (size_t) input_section
->size
);
1330 if ((input_section
->flags
& SEC_RELOC
) != 0
1331 && input_section
->reloc_count
> 0)
1333 Elf_Internal_Sym
*isym
;
1334 Elf_Internal_Sym
*isymend
;
1338 internal_relocs
= (_bfd_elf_link_read_relocs
1339 (input_bfd
, input_section
, (PTR
) NULL
,
1340 (Elf_Internal_Rela
*) NULL
, FALSE
));
1341 if (internal_relocs
== NULL
)
1344 if (symtab_hdr
->sh_info
!= 0)
1346 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1347 if (isymbuf
== NULL
)
1348 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1349 symtab_hdr
->sh_info
, 0,
1351 if (isymbuf
== NULL
)
1355 amt
= symtab_hdr
->sh_info
;
1356 amt
*= sizeof (asection
*);
1357 sections
= (asection
**) bfd_malloc (amt
);
1358 if (sections
== NULL
&& amt
!= 0)
1361 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1362 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1366 if (isym
->st_shndx
== SHN_UNDEF
)
1367 isec
= bfd_und_section_ptr
;
1368 else if (isym
->st_shndx
== SHN_ABS
)
1369 isec
= bfd_abs_section_ptr
;
1370 else if (isym
->st_shndx
== SHN_COMMON
)
1371 isec
= bfd_com_section_ptr
;
1373 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1378 if (! mn10200_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
1379 input_section
, data
, internal_relocs
,
1383 if (sections
!= NULL
)
1386 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1388 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1389 free (internal_relocs
);
1395 if (sections
!= NULL
)
1398 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1400 if (internal_relocs
!= NULL
1401 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1402 free (internal_relocs
);
1406 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1407 #define TARGET_LITTLE_NAME "elf32-mn10200"
1408 #define ELF_ARCH bfd_arch_mn10200
1409 #define ELF_MACHINE_CODE EM_MN10200
1410 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1411 #define ELF_MAXPAGESIZE 0x1000
1413 #define elf_backend_rela_normal 1
1414 #define elf_info_to_howto mn10200_info_to_howto
1415 #define elf_info_to_howto_rel 0
1416 #define elf_backend_relocate_section mn10200_elf_relocate_section
1417 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1418 #define bfd_elf32_bfd_get_relocated_section_contents \
1419 mn10200_elf_get_relocated_section_contents
1421 #define elf_symbol_leading_char '_'
1423 #include "elf32-target.h"