1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998, 1999, 2001 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
26 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
27 static void mn10200_info_to_howto
28 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
29 static boolean mn10200_elf_relax_delete_bytes
30 PARAMS ((bfd *, asection *, bfd_vma, int));
31 static boolean mn10200_elf_symbol_address_p
32 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
34 /* We have to use RELA instructions since md_apply_fix3 in the assembler
35 does absolutely nothing. */
50 static reloc_howto_type elf_mn10200_howto_table[] = {
51 /* Dummy relocation. Does nothing. */
52 HOWTO (R_MN10200_NONE,
58 complain_overflow_bitfield,
59 bfd_elf_generic_reloc,
65 /* Standard 32 bit reloc. */
72 complain_overflow_bitfield,
73 bfd_elf_generic_reloc,
79 /* Standard 16 bit reloc. */
86 complain_overflow_bitfield,
87 bfd_elf_generic_reloc,
93 /* Standard 8 bit reloc. */
100 complain_overflow_bitfield,
101 bfd_elf_generic_reloc,
107 /* Standard 24 bit reloc. */
114 complain_overflow_bitfield,
115 bfd_elf_generic_reloc,
121 /* Simple 8 pc-relative reloc. */
122 HOWTO (R_MN10200_PCREL8,
128 complain_overflow_bitfield,
129 bfd_elf_generic_reloc,
135 /* Simple 16 pc-relative reloc. */
136 HOWTO (R_MN10200_PCREL16,
142 complain_overflow_bitfield,
143 bfd_elf_generic_reloc,
149 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
150 to get the pc-relative offset correct. */
151 HOWTO (R_MN10200_PCREL24,
157 complain_overflow_bitfield,
158 bfd_elf_generic_reloc,
166 struct mn10200_reloc_map {
167 bfd_reloc_code_real_type bfd_reloc_val;
168 unsigned char elf_reloc_val;
171 static const struct mn10200_reloc_map mn10200_reloc_map[] = {
172 { BFD_RELOC_NONE , R_MN10200_NONE , },
173 { BFD_RELOC_32 , R_MN10200_32 , },
174 { BFD_RELOC_16 , R_MN10200_16 , },
175 { BFD_RELOC_8 , R_MN10200_8 , },
176 { BFD_RELOC_24 , R_MN10200_24 , },
177 { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
178 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
179 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
182 static reloc_howto_type *
183 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
184 bfd *abfd ATTRIBUTE_UNUSED;
185 bfd_reloc_code_real_type code;
190 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
193 if (mn10200_reloc_map[i].bfd_reloc_val == code)
194 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
200 /* Set the howto pointer for an MN10200 ELF reloc. */
203 mn10200_info_to_howto (abfd, cache_ptr, dst)
204 bfd *abfd ATTRIBUTE_UNUSED;
206 Elf32_Internal_Rela *dst;
210 r_type = ELF32_R_TYPE (dst->r_info);
211 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
212 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
215 /* Perform a relocation as part of a final link. */
216 static bfd_reloc_status_type
217 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
218 input_section, contents, offset, value,
219 addend, info, sym_sec, is_local)
220 reloc_howto_type *howto;
222 bfd *output_bfd ATTRIBUTE_UNUSED;
223 asection *input_section;
228 struct bfd_link_info *info ATTRIBUTE_UNUSED;
229 asection *sym_sec ATTRIBUTE_UNUSED;
230 int is_local ATTRIBUTE_UNUSED;
232 unsigned long r_type = howto->type;
233 bfd_byte *hit_data = contents + offset;
243 bfd_put_32 (input_bfd, value, hit_data);
249 if ((long) value > 0x7fff || (long) value < -0x8000)
250 return bfd_reloc_overflow;
252 bfd_put_16 (input_bfd, value, hit_data);
258 if ((long) value > 0x7f || (long) value < -0x80)
259 return bfd_reloc_overflow;
261 bfd_put_8 (input_bfd, value, hit_data);
267 if ((long) value > 0x7fffff || (long) value < -0x800000)
268 return bfd_reloc_overflow;
271 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
272 bfd_put_32 (input_bfd, value, hit_data);
275 case R_MN10200_PCREL8:
276 value -= (input_section->output_section->vma
277 + input_section->output_offset);
278 value -= (offset + 1);
281 if ((long) value > 0xff || (long) value < -0x100)
282 return bfd_reloc_overflow;
284 bfd_put_8 (input_bfd, value, hit_data);
287 case R_MN10200_PCREL16:
288 value -= (input_section->output_section->vma
289 + input_section->output_offset);
290 value -= (offset + 2);
293 if ((long) value > 0xffff || (long) value < -0x10000)
294 return bfd_reloc_overflow;
296 bfd_put_16 (input_bfd, value, hit_data);
299 case R_MN10200_PCREL24:
300 value -= (input_section->output_section->vma
301 + input_section->output_offset);
302 value -= (offset + 3);
305 if ((long) value > 0xffffff || (long) value < -0x1000000)
306 return bfd_reloc_overflow;
309 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
310 bfd_put_32 (input_bfd, value, hit_data);
314 return bfd_reloc_notsupported;
318 /* Relocate an MN10200 ELF section. */
320 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
321 contents, relocs, local_syms, local_sections)
323 struct bfd_link_info *info;
325 asection *input_section;
327 Elf_Internal_Rela *relocs;
328 Elf_Internal_Sym *local_syms;
329 asection **local_sections;
331 Elf_Internal_Shdr *symtab_hdr;
332 struct elf_link_hash_entry **sym_hashes;
333 Elf_Internal_Rela *rel, *relend;
335 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
336 sym_hashes = elf_sym_hashes (input_bfd);
339 relend = relocs + input_section->reloc_count;
340 for (; rel < relend; rel++)
343 reloc_howto_type *howto;
344 unsigned long r_symndx;
345 Elf_Internal_Sym *sym;
347 struct elf_link_hash_entry *h;
349 bfd_reloc_status_type r;
351 r_symndx = ELF32_R_SYM (rel->r_info);
352 r_type = ELF32_R_TYPE (rel->r_info);
353 howto = elf_mn10200_howto_table + r_type;
355 if (info->relocateable)
357 /* This is a relocateable link. We don't have to change
358 anything, unless the reloc is against a section symbol,
359 in which case we have to adjust according to where the
360 section symbol winds up in the output section. */
361 if (r_symndx < symtab_hdr->sh_info)
363 sym = local_syms + r_symndx;
364 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
366 sec = local_sections[r_symndx];
367 rel->r_addend += sec->output_offset + sym->st_value;
374 /* This is a final link. */
378 if (r_symndx < symtab_hdr->sh_info)
380 sym = local_syms + r_symndx;
381 sec = local_sections[r_symndx];
382 relocation = (sec->output_section->vma
388 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
389 while (h->root.type == bfd_link_hash_indirect
390 || h->root.type == bfd_link_hash_warning)
391 h = (struct elf_link_hash_entry *) h->root.u.i.link;
392 if (h->root.type == bfd_link_hash_defined
393 || h->root.type == bfd_link_hash_defweak)
395 sec = h->root.u.def.section;
396 relocation = (h->root.u.def.value
397 + sec->output_section->vma
398 + sec->output_offset);
400 else if (h->root.type == bfd_link_hash_undefweak)
404 if (! ((*info->callbacks->undefined_symbol)
405 (info, h->root.root.string, input_bfd,
406 input_section, rel->r_offset, true)))
412 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
414 contents, rel->r_offset,
415 relocation, rel->r_addend,
416 info, sec, h == NULL);
418 if (r != bfd_reloc_ok)
421 const char *msg = (const char *) 0;
424 name = h->root.root.string;
427 name = (bfd_elf_string_from_elf_section
428 (input_bfd, symtab_hdr->sh_link, sym->st_name));
429 if (name == NULL || *name == '\0')
430 name = bfd_section_name (input_bfd, sec);
435 case bfd_reloc_overflow:
436 if (! ((*info->callbacks->reloc_overflow)
437 (info, name, howto->name, (bfd_vma) 0,
438 input_bfd, input_section, rel->r_offset)))
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's quite a few relaxing opportunites 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 *free_relocs = NULL;
517 Elf_Internal_Rela *irel, *irelend;
518 bfd_byte *contents = NULL;
519 bfd_byte *free_contents = NULL;
520 Elf32_External_Sym *extsyms = NULL;
521 Elf32_External_Sym *free_extsyms = NULL;
523 /* Assume nothing changes. */
526 /* We don't have to do anything for a relocateable link, if
527 this section does not have relocs, or if this is not a
529 if (link_info->relocateable
530 || (sec->flags & SEC_RELOC) == 0
531 || sec->reloc_count == 0
532 || (sec->flags & SEC_CODE) == 0)
535 /* If this is the first time we have been called for this section,
536 initialize the cooked size. */
537 if (sec->_cooked_size == 0)
538 sec->_cooked_size = sec->_raw_size;
540 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
542 /* Get a copy of the native relocations. */
543 internal_relocs = (_bfd_elf32_link_read_relocs
544 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
545 link_info->keep_memory));
546 if (internal_relocs == NULL)
548 if (! link_info->keep_memory)
549 free_relocs = internal_relocs;
551 /* Walk through them looking for relaxing opportunities. */
552 irelend = internal_relocs + sec->reloc_count;
553 for (irel = internal_relocs; irel < irelend; irel++)
557 /* If this isn't something that can be relaxed, then ignore
559 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
560 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
561 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
564 /* Get the section contents if we haven't done so already. */
565 if (contents == NULL)
567 /* Get cached copy if it exists. */
568 if (elf_section_data (sec)->this_hdr.contents != NULL)
569 contents = elf_section_data (sec)->this_hdr.contents;
572 /* Go get them off disk. */
573 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
574 if (contents == NULL)
576 free_contents = contents;
578 if (! bfd_get_section_contents (abfd, sec, contents,
579 (file_ptr) 0, sec->_raw_size))
584 /* Read this BFD's symbols if we haven't done so already. */
587 /* Get cached copy if it exists. */
588 if (symtab_hdr->contents != NULL)
589 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
592 /* Go get them off disk. */
593 extsyms = ((Elf32_External_Sym *)
594 bfd_malloc (symtab_hdr->sh_size));
597 free_extsyms = extsyms;
598 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
599 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
600 != symtab_hdr->sh_size))
605 /* Get the value of the symbol referred to by the reloc. */
606 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
608 Elf_Internal_Sym isym;
611 /* A local symbol. */
612 bfd_elf32_swap_symbol_in (abfd,
613 extsyms + ELF32_R_SYM (irel->r_info),
616 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
617 symval = (isym.st_value
618 + sym_sec->output_section->vma
619 + sym_sec->output_offset);
624 struct elf_link_hash_entry *h;
626 /* An external symbol. */
627 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
628 h = elf_sym_hashes (abfd)[indx];
629 BFD_ASSERT (h != NULL);
630 if (h->root.type != bfd_link_hash_defined
631 && h->root.type != bfd_link_hash_defweak)
633 /* This appears to be a reference to an undefined
634 symbol. Just ignore it--it will be caught by the
635 regular reloc processing. */
639 symval = (h->root.u.def.value
640 + h->root.u.def.section->output_section->vma
641 + h->root.u.def.section->output_offset);
644 /* For simplicity of coding, we are going to modify the section
645 contents, the section relocs, and the BFD symbol table. We
646 must tell the rest of the code not to free up this
647 information. It would be possible to instead create a table
648 of changes which have to be made, as is done in coff-mips.c;
649 that would be more work, but would require less memory when
650 the linker is run. */
652 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
654 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
656 bfd_vma value = symval;
658 /* Deal with pc-relative gunk. */
659 value -= (sec->output_section->vma + sec->output_offset);
660 value -= (irel->r_offset + 3);
661 value += irel->r_addend;
663 /* See if the value will fit in 16 bits, note the high value is
664 0x7fff + 2 as the target will be two bytes closer if we are
666 if ((long) value < 0x8001 && (long) value > -0x8000)
670 /* Get the opcode. */
671 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
673 if (code != 0xe0 && code != 0xe1)
676 /* Note that we've changed the relocs, section contents, etc. */
677 elf_section_data (sec)->relocs = internal_relocs;
680 elf_section_data (sec)->this_hdr.contents = contents;
681 free_contents = NULL;
683 symtab_hdr->contents = (bfd_byte *) extsyms;
686 /* Fix the opcode. */
688 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
689 else if (code == 0xe1)
690 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
692 /* Fix the relocation's type. */
693 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
696 /* The opcode got shorter too, so we have to fix the offset. */
699 /* Delete two bytes of data. */
700 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
701 irel->r_offset + 1, 2))
704 /* That will change things, so, we should relax again.
705 Note that this is not required, and it may be slow. */
710 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
712 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
714 bfd_vma value = symval;
716 /* Deal with pc-relative gunk. */
717 value -= (sec->output_section->vma + sec->output_offset);
718 value -= (irel->r_offset + 2);
719 value += irel->r_addend;
721 /* See if the value will fit in 8 bits, note the high value is
722 0x7f + 1 as the target will be one bytes closer if we are
724 if ((long) value < 0x80 && (long) value > -0x80)
728 /* Get the opcode. */
729 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
734 /* Note that we've changed the relocs, section contents, etc. */
735 elf_section_data (sec)->relocs = internal_relocs;
738 elf_section_data (sec)->this_hdr.contents = contents;
739 free_contents = NULL;
741 symtab_hdr->contents = (bfd_byte *) extsyms;
744 /* Fix the opcode. */
745 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
747 /* Fix the relocation's type. */
748 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
751 /* Delete one byte of data. */
752 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
753 irel->r_offset + 1, 1))
756 /* That will change things, so, we should relax again.
757 Note that this is not required, and it may be slow. */
762 /* Try to eliminate an unconditional 8 bit pc-relative branch
763 which immediately follows a conditional 8 bit pc-relative
764 branch around the unconditional branch.
771 This happens when the bCC can't reach lab2 at assembly time,
772 but due to other relaxations it can reach at link time. */
773 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
775 Elf_Internal_Rela *nrel;
776 bfd_vma value = symval;
779 /* Deal with pc-relative gunk. */
780 value -= (sec->output_section->vma + sec->output_offset);
781 value -= (irel->r_offset + 1);
782 value += irel->r_addend;
784 /* Do nothing if this reloc is the last byte in the section. */
785 if (irel->r_offset == sec->_cooked_size)
788 /* See if the next instruction is an unconditional pc-relative
789 branch, more often than not this test will fail, so we
790 test it first to speed things up. */
791 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
795 /* Also make sure the next relocation applies to the next
796 instruction and that it's a pc-relative 8 bit branch. */
799 || irel->r_offset + 2 != nrel->r_offset
800 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
803 /* Make sure our destination immediately follows the
804 unconditional branch. */
805 if (symval != (sec->output_section->vma + sec->output_offset
806 + irel->r_offset + 3))
809 /* Now make sure we are a conditional branch. This may not
810 be necessary, but why take the chance.
812 Note these checks assume that R_MN10200_PCREL8 relocs
813 only occur on bCC and bCCx insns. If they occured
814 elsewhere, we'd need to know the start of this insn
815 for this check to be accurate. */
816 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
817 if (code != 0xe0 && code != 0xe1 && code != 0xe2
818 && code != 0xe3 && code != 0xe4 && code != 0xe5
819 && code != 0xe6 && code != 0xe7 && code != 0xe8
820 && code != 0xe9 && code != 0xec && code != 0xed
821 && code != 0xee && code != 0xef && code != 0xfc
822 && code != 0xfd && code != 0xfe && code != 0xff)
825 /* We also have to be sure there is no symbol/label
826 at the unconditional branch. */
827 if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
831 /* Note that we've changed the relocs, section contents, etc. */
832 elf_section_data (sec)->relocs = internal_relocs;
835 elf_section_data (sec)->this_hdr.contents = contents;
836 free_contents = NULL;
838 symtab_hdr->contents = (bfd_byte *) extsyms;
841 /* Reverse the condition of the first branch. */
899 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
901 /* Set the reloc type and symbol for the first branch
902 from the second branch. */
903 irel->r_info = nrel->r_info;
905 /* Make the reloc for the second branch a null reloc. */
906 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
909 /* Delete two bytes of data. */
910 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
911 irel->r_offset + 1, 2))
914 /* That will change things, so, we should relax again.
915 Note that this is not required, and it may be slow. */
919 /* Try to turn a 24bit immediate, displacement or absolute address
920 into a 16bit immediate, displacement or absolute address. */
921 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
923 bfd_vma value = symval;
925 /* See if the value will fit in 16 bits.
926 We allow any 16bit match here. We prune those we can't
928 if ((long) value < 0x7fff && (long) value > -0x8000)
932 /* All insns which have 24bit operands are 5 bytes long,
933 the first byte will always be 0xf4, but we double check
936 /* Get the first opcode. */
937 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
942 /* Get the second opcode. */
943 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
947 /* mov imm24,dn -> mov imm16,dn */
949 /* Not safe if the high bit is on as relaxing may
950 move the value out of high mem and thus not fit
951 in a signed 16bit value. */
955 /* Note that we've changed the reldection contents, etc. */
956 elf_section_data (sec)->relocs = internal_relocs;
959 elf_section_data (sec)->this_hdr.contents = contents;
960 free_contents = NULL;
962 symtab_hdr->contents = (bfd_byte *) extsyms;
965 /* Fix the opcode. */
966 bfd_put_8 (abfd, 0xf8 + (code & 0x03),
967 contents + irel->r_offset - 2);
969 /* Fix the relocation's type. */
970 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
973 /* The opcode got shorter too, so we have to fix the
977 /* Delete two bytes of data. */
978 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
979 irel->r_offset + 1, 2))
982 /* That will change things, so, we should relax again.
983 Note that this is not required, and it may be slow. */
987 /* mov imm24,an -> mov imm16,an
988 cmp imm24,an -> cmp imm16,an
989 mov (abs24),dn -> mov (abs16),dn
990 mov dn,(abs24) -> mov dn,(abs16)
991 movb dn,(abs24) -> movb dn,(abs16)
992 movbu (abs24),dn -> movbu (abs16),dn */
999 /* Note that we've changed the reldection contents, etc. */
1000 elf_section_data (sec)->relocs = internal_relocs;
1003 elf_section_data (sec)->this_hdr.contents = contents;
1004 free_contents = NULL;
1006 symtab_hdr->contents = (bfd_byte *) extsyms;
1007 free_extsyms = NULL;
1009 if ((code & 0xfc) == 0x74)
1010 code = 0xdc + (code & 0x03);
1011 else if ((code & 0xfc) == 0x7c)
1012 code = 0xec + (code & 0x03);
1013 else if ((code & 0xfc) == 0xc0)
1014 code = 0xc8 + (code & 0x03);
1015 else if ((code & 0xfc) == 0x40)
1016 code = 0xc0 + (code & 0x03);
1017 else if ((code & 0xfc) == 0x44)
1018 code = 0xc4 + (code & 0x03);
1019 else if ((code & 0xfc) == 0xc8)
1020 code = 0xcc + (code & 0x03);
1022 /* Fix the opcode. */
1023 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1025 /* Fix the relocation's type. */
1026 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1029 /* The opcode got shorter too, so we have to fix the
1031 irel->r_offset -= 1;
1033 /* Delete two bytes of data. */
1034 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1035 irel->r_offset + 1, 2))
1038 /* That will change things, so, we should relax again.
1039 Note that this is not required, and it may be slow. */
1043 /* cmp imm24,dn -> cmp imm16,dn
1044 mov (abs24),an -> mov (abs16),an
1045 mov an,(abs24) -> mov an,(abs16)
1046 add imm24,dn -> add imm16,dn
1047 add imm24,an -> add imm16,an
1048 sub imm24,dn -> sub imm16,dn
1049 sub imm24,an -> sub imm16,an
1050 And all d24->d16 in memory ops. */
1067 /* Not safe if the high bit is on as relaxing may
1068 move the value out of high mem and thus not fit
1069 in a signed 16bit value. */
1070 if (((code & 0xfc) == 0x78
1071 || (code & 0xfc) == 0x60
1072 || (code & 0xfc) == 0x64
1073 || (code & 0xfc) == 0x68
1074 || (code & 0xfc) == 0x6c
1075 || (code & 0xfc) == 0x80
1076 || (code & 0xfc) == 0xf0
1077 || (code & 0xfc) == 0x00
1078 || (code & 0xfc) == 0x10
1079 || (code & 0xfc) == 0xb0
1080 || (code & 0xfc) == 0x30
1081 || (code & 0xfc) == 0xa0
1082 || (code & 0xfc) == 0x20
1083 || (code & 0xfc) == 0x90)
1084 && (value & 0x8000) != 0)
1087 /* Note that we've changed the reldection contents, etc. */
1088 elf_section_data (sec)->relocs = internal_relocs;
1091 elf_section_data (sec)->this_hdr.contents = contents;
1092 free_contents = NULL;
1094 symtab_hdr->contents = (bfd_byte *) extsyms;
1095 free_extsyms = NULL;
1097 /* Fix the opcode. */
1098 bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1100 if ((code & 0xfc) == 0x78)
1101 code = 0x48 + (code & 0x03);
1102 else if ((code & 0xfc) == 0xd0)
1103 code = 0x30 + (code & 0x03);
1104 else if ((code & 0xfc) == 0x50)
1105 code = 0x20 + (code & 0x03);
1106 else if ((code & 0xfc) == 0x60)
1107 code = 0x18 + (code & 0x03);
1108 else if ((code & 0xfc) == 0x64)
1109 code = 0x08 + (code & 0x03);
1110 else if ((code & 0xfc) == 0x68)
1111 code = 0x1c + (code & 0x03);
1112 else if ((code & 0xfc) == 0x6c)
1113 code = 0x0c + (code & 0x03);
1114 else if ((code & 0xfc) == 0x80)
1115 code = 0xc0 + (code & 0x07);
1116 else if ((code & 0xfc) == 0xf0)
1117 code = 0xb0 + (code & 0x07);
1118 else if ((code & 0xfc) == 0x00)
1119 code = 0x80 + (code & 0x07);
1120 else if ((code & 0xfc) == 0x10)
1121 code = 0xa0 + (code & 0x07);
1122 else if ((code & 0xfc) == 0xb0)
1123 code = 0x70 + (code & 0x07);
1124 else if ((code & 0xfc) == 0x30)
1125 code = 0x60 + (code & 0x07);
1126 else if ((code & 0xfc) == 0xa0)
1127 code = 0xd0 + (code & 0x07);
1128 else if ((code & 0xfc) == 0x20)
1129 code = 0x90 + (code & 0x07);
1130 else if ((code & 0xfc) == 0x90)
1131 code = 0x50 + (code & 0x07);
1133 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1135 /* Fix the relocation's type. */
1136 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1139 /* Delete one bytes of data. */
1140 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1141 irel->r_offset + 2, 1))
1144 /* That will change things, so, we should relax again.
1145 Note that this is not required, and it may be slow. */
1149 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1151 /* Note that we've changed the reldection contents, etc. */
1152 elf_section_data (sec)->relocs = internal_relocs;
1155 elf_section_data (sec)->this_hdr.contents = contents;
1156 free_contents = NULL;
1158 symtab_hdr->contents = (bfd_byte *) extsyms;
1159 free_extsyms = NULL;
1161 bfd_put_8 (abfd, 0xcc + (code & 0x03),
1162 contents + irel->r_offset - 2);
1164 bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1165 contents + irel->r_offset - 1);
1167 /* Fix the relocation's type. */
1168 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1171 /* The reloc will be applied one byte in front of its
1172 current location. */
1173 irel->r_offset -= 1;
1175 /* Delete one bytes of data. */
1176 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1177 irel->r_offset + 2, 1))
1180 /* That will change things, so, we should relax again.
1181 Note that this is not required, and it may be slow. */
1189 if (free_relocs != NULL)
1195 if (free_contents != NULL)
1197 if (! link_info->keep_memory)
1198 free (free_contents);
1201 /* Cache the section contents for elf_link_input_bfd. */
1202 elf_section_data (sec)->this_hdr.contents = contents;
1204 free_contents = NULL;
1207 if (free_extsyms != NULL)
1209 if (! link_info->keep_memory)
1210 free (free_extsyms);
1213 /* Cache the symbols for elf_link_input_bfd. */
1214 symtab_hdr->contents = extsyms;
1216 free_extsyms = NULL;
1222 if (free_relocs != NULL)
1224 if (free_contents != NULL)
1225 free (free_contents);
1226 if (free_extsyms != NULL)
1227 free (free_extsyms);
1231 /* Delete some bytes from a section while relaxing. */
1234 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1240 Elf_Internal_Shdr *symtab_hdr;
1241 Elf32_External_Sym *extsyms;
1244 Elf_Internal_Rela *irel, *irelend;
1245 Elf_Internal_Rela *irelalign;
1247 Elf32_External_Sym *esym, *esymend;
1248 struct elf_link_hash_entry *sym_hash;
1250 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1251 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1253 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1255 contents = elf_section_data (sec)->this_hdr.contents;
1257 /* The deletion must stop at the next ALIGN reloc for an aligment
1258 power larger than the number of bytes we are deleting. */
1261 toaddr = sec->_cooked_size;
1263 irel = elf_section_data (sec)->relocs;
1264 irelend = irel + sec->reloc_count;
1266 /* Actually delete the bytes. */
1267 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1268 sec->_cooked_size -= count;
1270 /* Adjust all the relocs. */
1271 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1273 /* Get the new reloc address. */
1274 if ((irel->r_offset > addr
1275 && irel->r_offset < toaddr))
1276 irel->r_offset -= count;
1279 /* Adjust the local symbols defined in this section. */
1281 esymend = esym + symtab_hdr->sh_info;
1282 for (; esym < esymend; esym++)
1284 Elf_Internal_Sym isym;
1286 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1288 if (isym.st_shndx == shndx
1289 && isym.st_value > addr
1290 && isym.st_value < toaddr)
1292 isym.st_value -= count;
1293 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1297 /* Now adjust the global symbols defined in this section. */
1298 esym = extsyms + symtab_hdr->sh_info;
1299 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1300 for (index = 0; esym < esymend; esym++, index++)
1302 Elf_Internal_Sym isym;
1304 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1305 sym_hash = elf_sym_hashes (abfd)[index];
1306 if (isym.st_shndx == shndx
1307 && ((sym_hash)->root.type == bfd_link_hash_defined
1308 || (sym_hash)->root.type == bfd_link_hash_defweak)
1309 && (sym_hash)->root.u.def.section == sec
1310 && (sym_hash)->root.u.def.value > addr
1311 && (sym_hash)->root.u.def.value < toaddr)
1313 (sym_hash)->root.u.def.value -= count;
1320 /* Return true if a symbol exists at the given address, else return
1323 mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1326 Elf32_External_Sym *extsyms;
1329 Elf_Internal_Shdr *symtab_hdr;
1331 Elf32_External_Sym *esym, *esymend;
1332 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1334 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1335 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1337 /* Examine all the symbols. */
1339 esymend = esym + symtab_hdr->sh_info;
1340 for (; esym < esymend; esym++)
1342 Elf_Internal_Sym isym;
1344 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1346 if (isym.st_shndx == shndx
1347 && isym.st_value == addr)
1351 sym_hash = elf_sym_hashes (abfd);
1352 sym_hash_end = (sym_hash
1353 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1354 - symtab_hdr->sh_info));
1355 for (; sym_hash < sym_hash_end; sym_hash++)
1357 if (((*sym_hash)->root.type == bfd_link_hash_defined
1358 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1359 && (*sym_hash)->root.u.def.section == sec
1360 && (*sym_hash)->root.u.def.value == addr)
1366 /* This is a version of bfd_generic_get_relocated_section_contents
1367 which uses mn10200_elf_relocate_section. */
1370 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1371 data, relocateable, symbols)
1373 struct bfd_link_info *link_info;
1374 struct bfd_link_order *link_order;
1376 boolean relocateable;
1379 Elf_Internal_Shdr *symtab_hdr;
1380 asection *input_section = link_order->u.indirect.section;
1381 bfd *input_bfd = input_section->owner;
1382 asection **sections = NULL;
1383 Elf_Internal_Rela *internal_relocs = NULL;
1384 Elf32_External_Sym *external_syms = NULL;
1385 Elf_Internal_Sym *internal_syms = NULL;
1387 /* We only need to handle the case of relaxing, or of having a
1388 particular set of section contents, specially. */
1390 || elf_section_data (input_section)->this_hdr.contents == NULL)
1391 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1396 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1398 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1399 input_section->_raw_size);
1401 if ((input_section->flags & SEC_RELOC) != 0
1402 && input_section->reloc_count > 0)
1404 Elf_Internal_Sym *isymp;
1406 Elf32_External_Sym *esym, *esymend;
1408 if (symtab_hdr->contents != NULL)
1409 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1412 external_syms = ((Elf32_External_Sym *)
1413 bfd_malloc (symtab_hdr->sh_info
1414 * sizeof (Elf32_External_Sym)));
1415 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1417 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1418 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1419 symtab_hdr->sh_info, input_bfd)
1420 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1424 internal_relocs = (_bfd_elf32_link_read_relocs
1425 (input_bfd, input_section, (PTR) NULL,
1426 (Elf_Internal_Rela *) NULL, false));
1427 if (internal_relocs == NULL)
1430 internal_syms = ((Elf_Internal_Sym *)
1431 bfd_malloc (symtab_hdr->sh_info
1432 * sizeof (Elf_Internal_Sym)));
1433 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1436 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1437 * sizeof (asection *));
1438 if (sections == NULL && symtab_hdr->sh_info > 0)
1441 isymp = internal_syms;
1443 esym = external_syms;
1444 esymend = esym + symtab_hdr->sh_info;
1445 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1449 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1451 if (isymp->st_shndx == SHN_UNDEF)
1452 isec = bfd_und_section_ptr;
1453 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1454 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1455 else if (isymp->st_shndx == SHN_ABS)
1456 isec = bfd_abs_section_ptr;
1457 else if (isymp->st_shndx == SHN_COMMON)
1458 isec = bfd_com_section_ptr;
1468 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1469 input_section, data, internal_relocs,
1470 internal_syms, sections))
1473 if (sections != NULL)
1476 if (internal_syms != NULL)
1477 free (internal_syms);
1478 internal_syms = NULL;
1479 if (external_syms != NULL && symtab_hdr->contents == NULL)
1480 free (external_syms);
1481 external_syms = NULL;
1482 if (internal_relocs != elf_section_data (input_section)->relocs)
1483 free (internal_relocs);
1484 internal_relocs = NULL;
1490 if (internal_relocs != NULL
1491 && internal_relocs != elf_section_data (input_section)->relocs)
1492 free (internal_relocs);
1493 if (external_syms != NULL && symtab_hdr->contents == NULL)
1494 free (external_syms);
1495 if (internal_syms != NULL)
1496 free (internal_syms);
1497 if (sections != NULL)
1502 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1503 #define TARGET_LITTLE_NAME "elf32-mn10200"
1504 #define ELF_ARCH bfd_arch_mn10200
1505 #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
1506 #define ELF_MAXPAGESIZE 0x1000
1508 #define elf_info_to_howto mn10200_info_to_howto
1509 #define elf_info_to_howto_rel 0
1510 #define elf_backend_relocate_section mn10200_elf_relocate_section
1511 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1512 #define bfd_elf32_bfd_get_relocated_section_contents \
1513 mn10200_elf_get_relocated_section_contents
1515 #define elf_symbol_leading_char '_'
1517 #include "elf32-target.h"