1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997 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 mn10300_info_to_howto
28 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
29 static boolean mn10300_elf_relax_delete_bytes
30 PARAMS ((bfd *, asection *, bfd_vma, int));
31 static boolean mn10300_elf_symbol_address_p
32 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
36 /* We have to use RELA instructions since md_apply_fix3 in the assembler
37 does absolutely nothing. */
52 static reloc_howto_type elf_mn10300_howto_table[] =
54 /* Dummy relocation. Does nothing. */
55 HOWTO (R_MN10300_NONE,
61 complain_overflow_bitfield,
62 bfd_elf_generic_reloc,
68 /* Standard 32 bit reloc. */
75 complain_overflow_bitfield,
76 bfd_elf_generic_reloc,
82 /* Standard 16 bit reloc. */
89 complain_overflow_bitfield,
90 bfd_elf_generic_reloc,
96 /* Standard 8 bit reloc. */
103 complain_overflow_bitfield,
104 bfd_elf_generic_reloc,
110 /* Standard 32bit pc-relative reloc. */
111 HOWTO (R_MN10300_PCREL32,
117 complain_overflow_bitfield,
118 bfd_elf_generic_reloc,
124 /* Standard 16bit pc-relative reloc. */
125 HOWTO (R_MN10300_PCREL16,
131 complain_overflow_bitfield,
132 bfd_elf_generic_reloc,
138 /* Standard 8 pc-relative reloc. */
139 HOWTO (R_MN10300_PCREL8,
145 complain_overflow_bitfield,
146 bfd_elf_generic_reloc,
154 struct mn10300_reloc_map
156 unsigned char bfd_reloc_val;
157 unsigned char elf_reloc_val;
160 static const struct mn10300_reloc_map mn10300_reloc_map[] =
162 { BFD_RELOC_NONE, R_MN10300_NONE, },
163 { BFD_RELOC_32, R_MN10300_32, },
164 { BFD_RELOC_16, R_MN10300_16, },
165 { BFD_RELOC_8, R_MN10300_8, },
166 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
167 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
168 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
171 static reloc_howto_type *
172 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
174 bfd_reloc_code_real_type code;
179 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
182 if (mn10300_reloc_map[i].bfd_reloc_val == code)
183 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
189 /* Set the howto pointer for an MN10300 ELF reloc. */
192 mn10300_info_to_howto (abfd, cache_ptr, dst)
195 Elf32_Internal_Rela *dst;
199 r_type = ELF32_R_TYPE (dst->r_info);
200 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
201 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
204 /* Perform a relocation as part of a final link. */
205 static bfd_reloc_status_type
206 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
207 input_section, contents, offset, value,
208 addend, info, sym_sec, is_local)
209 reloc_howto_type *howto;
212 asection *input_section;
217 struct bfd_link_info *info;
221 unsigned long r_type = howto->type;
222 bfd_byte *hit_data = contents + offset;
232 bfd_put_32 (input_bfd, value, hit_data);
238 if ((long)value > 0x7fff || (long)value < -0x8000)
239 return bfd_reloc_overflow;
241 bfd_put_16 (input_bfd, value, hit_data);
247 if ((long)value > 0x7fff || (long)value < -0x8000)
248 return bfd_reloc_overflow;
250 bfd_put_8 (input_bfd, value, hit_data);
253 case R_MN10300_PCREL8:
254 value -= (input_section->output_section->vma
255 + input_section->output_offset);
259 if ((long)value > 0xff || (long)value < -0x100)
260 return bfd_reloc_overflow;
262 bfd_put_8 (input_bfd, value, hit_data);
265 case R_MN10300_PCREL16:
266 value -= (input_section->output_section->vma
267 + input_section->output_offset);
271 if ((long)value > 0xffff || (long)value < -0x10000)
272 return bfd_reloc_overflow;
274 bfd_put_16 (input_bfd, value, hit_data);
277 case R_MN10300_PCREL32:
278 value -= (input_section->output_section->vma
279 + input_section->output_offset);
283 bfd_put_32 (input_bfd, value, hit_data);
287 return bfd_reloc_notsupported;
292 /* Relocate an MN10300 ELF section. */
294 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
295 contents, relocs, local_syms, local_sections)
297 struct bfd_link_info *info;
299 asection *input_section;
301 Elf_Internal_Rela *relocs;
302 Elf_Internal_Sym *local_syms;
303 asection **local_sections;
305 Elf_Internal_Shdr *symtab_hdr;
306 struct elf_link_hash_entry **sym_hashes;
307 Elf_Internal_Rela *rel, *relend;
309 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
310 sym_hashes = elf_sym_hashes (input_bfd);
313 relend = relocs + input_section->reloc_count;
314 for (; rel < relend; rel++)
317 reloc_howto_type *howto;
318 unsigned long r_symndx;
319 Elf_Internal_Sym *sym;
321 struct elf_link_hash_entry *h;
323 bfd_reloc_status_type r;
325 r_symndx = ELF32_R_SYM (rel->r_info);
326 r_type = ELF32_R_TYPE (rel->r_info);
327 howto = elf_mn10300_howto_table + r_type;
329 if (info->relocateable)
331 /* This is a relocateable link. We don't have to change
332 anything, unless the reloc is against a section symbol,
333 in which case we have to adjust according to where the
334 section symbol winds up in the output section. */
335 if (r_symndx < symtab_hdr->sh_info)
337 sym = local_syms + r_symndx;
338 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
340 sec = local_sections[r_symndx];
341 rel->r_addend += sec->output_offset + sym->st_value;
348 /* This is a final link. */
352 if (r_symndx < symtab_hdr->sh_info)
354 sym = local_syms + r_symndx;
355 sec = local_sections[r_symndx];
356 relocation = (sec->output_section->vma
362 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
363 while (h->root.type == bfd_link_hash_indirect
364 || h->root.type == bfd_link_hash_warning)
365 h = (struct elf_link_hash_entry *) h->root.u.i.link;
366 if (h->root.type == bfd_link_hash_defined
367 || h->root.type == bfd_link_hash_defweak)
369 sec = h->root.u.def.section;
370 relocation = (h->root.u.def.value
371 + sec->output_section->vma
372 + sec->output_offset);
374 else if (h->root.type == bfd_link_hash_undefweak)
378 if (! ((*info->callbacks->undefined_symbol)
379 (info, h->root.root.string, input_bfd,
380 input_section, rel->r_offset)))
386 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
388 contents, rel->r_offset,
389 relocation, rel->r_addend,
390 info, sec, h == NULL);
392 if (r != bfd_reloc_ok)
395 const char *msg = (const char *)0;
398 name = h->root.root.string;
401 name = (bfd_elf_string_from_elf_section
402 (input_bfd, symtab_hdr->sh_link, sym->st_name));
403 if (name == NULL || *name == '\0')
404 name = bfd_section_name (input_bfd, sec);
409 case bfd_reloc_overflow:
410 if (! ((*info->callbacks->reloc_overflow)
411 (info, name, howto->name, (bfd_vma) 0,
412 input_bfd, input_section, rel->r_offset)))
416 case bfd_reloc_undefined:
417 if (! ((*info->callbacks->undefined_symbol)
418 (info, name, input_bfd, input_section,
423 case bfd_reloc_outofrange:
424 msg = "internal error: out of range error";
427 case bfd_reloc_notsupported:
428 msg = "internal error: unsupported relocation error";
431 case bfd_reloc_dangerous:
432 msg = "internal error: dangerous error";
436 msg = "internal error: unknown error";
440 if (!((*info->callbacks->warning)
441 (info, msg, name, input_bfd, input_section,
452 /* This function handles relaxing for the mn10300.
454 There's quite a few relaxing opportunites available on the mn10300:
456 * calls:32 -> calls:16 2 bytes
457 - call:32 -> call:16 2 bytes
459 * jmp:24 -> jmp:16 2 bytes
460 * jmp:16 -> bra:8 1 byte
462 * If the previous instruction is a conditional branch
463 around the jump/bra, we may be able to reverse its condition
464 and change its target to the jump's target. The jump/bra
465 can then be deleted. 2 bytes
467 XXX These are mn10200 relaxing things
468 * mov abs24 -> mov abs16 2 byte savings
470 * Most instructions which accept imm24 can relax to imm16 2 bytes
471 - Most instructions which accept imm16 can relax to imm8 1 byte
473 * Most instructions which accept d24 can relax to d16 2 bytes
474 - Most instructions which accept d16 can relax to d8 1 byte
476 abs24, imm24, d24 all look the same at the reloc level. It
477 might make the code simpler if we had different relocs for
478 the various relaxable operand types.
480 We don't handle imm16->imm8 or d16->d8 as they're very rare
481 and somewhat more difficult to support. */
484 mn10300_elf_relax_section (abfd, sec, link_info, again)
487 struct bfd_link_info *link_info;
490 Elf_Internal_Shdr *symtab_hdr;
491 Elf_Internal_Rela *internal_relocs;
492 Elf_Internal_Rela *free_relocs = NULL;
493 Elf_Internal_Rela *irel, *irelend;
494 bfd_byte *contents = NULL;
495 bfd_byte *free_contents = NULL;
496 Elf32_External_Sym *extsyms = NULL;
497 Elf32_External_Sym *free_extsyms = NULL;
499 /* Assume nothing changes. */
502 /* We don't have to do anything for a relocateable link, if
503 this section does not have relocs, or if this is not a
505 if (link_info->relocateable
506 || (sec->flags & SEC_RELOC) == 0
507 || sec->reloc_count == 0
508 || (sec->flags & SEC_CODE) == 0)
511 /* If this is the first time we have been called for this section,
512 initialize the cooked size. */
513 if (sec->_cooked_size == 0)
514 sec->_cooked_size = sec->_raw_size;
516 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
518 /* Get a copy of the native relocations. */
519 internal_relocs = (_bfd_elf32_link_read_relocs
520 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
521 link_info->keep_memory));
522 if (internal_relocs == NULL)
524 if (! link_info->keep_memory)
525 free_relocs = internal_relocs;
527 /* Walk through them looking for relaxing opportunities. */
528 irelend = internal_relocs + sec->reloc_count;
529 for (irel = internal_relocs; irel < irelend; irel++)
533 /* If this isn't something that can be relaxed, then ignore
535 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
536 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
537 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
540 /* Get the section contents if we haven't done so already. */
541 if (contents == NULL)
543 /* Get cached copy if it exists. */
544 if (elf_section_data (sec)->this_hdr.contents != NULL)
545 contents = elf_section_data (sec)->this_hdr.contents;
548 /* Go get them off disk. */
549 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
550 if (contents == NULL)
552 free_contents = contents;
554 if (! bfd_get_section_contents (abfd, sec, contents,
555 (file_ptr) 0, sec->_raw_size))
560 /* Read the local symbols if we haven't done so already. */
563 /* Get cached copy if it exists. */
564 if (symtab_hdr->contents != NULL)
565 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
568 /* Go get them off disk. */
569 extsyms = ((Elf32_External_Sym *)
570 bfd_malloc (symtab_hdr->sh_info
571 * sizeof (Elf32_External_Sym)));
574 free_extsyms = extsyms;
575 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
576 || (bfd_read (extsyms, sizeof (Elf32_External_Sym),
577 symtab_hdr->sh_info, abfd)
578 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
583 /* Get the value of the symbol referred to by the reloc. */
584 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
586 Elf_Internal_Sym isym;
589 /* A local symbol. */
590 bfd_elf32_swap_symbol_in (abfd,
591 extsyms + ELF32_R_SYM (irel->r_info),
594 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
595 symval = (isym.st_value
596 + sym_sec->output_section->vma
597 + sym_sec->output_offset);
602 struct elf_link_hash_entry *h;
604 /* An external symbol. */
605 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
606 h = elf_sym_hashes (abfd)[indx];
607 BFD_ASSERT (h != NULL);
608 if (h->root.type != bfd_link_hash_defined
609 && h->root.type != bfd_link_hash_defweak)
611 /* This appears to be a reference to an undefined
612 symbol. Just ignore it--it will be caught by the
613 regular reloc processing. */
617 symval = (h->root.u.def.value
618 + h->root.u.def.section->output_section->vma
619 + h->root.u.def.section->output_offset);
622 /* For simplicity of coding, we are going to modify the section
623 contents, the section relocs, and the BFD symbol table. We
624 must tell the rest of the code not to free up this
625 information. It would be possible to instead create a table
626 of changes which have to be made, as is done in coff-mips.c;
627 that would be more work, but would require less memory when
628 the linker is run. */
631 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
633 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
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;
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 != 0xdc && code != 0xff)
655 /* Note that we've changed the relocs, section contents, etc. */
656 elf_section_data (sec)->relocs = internal_relocs;
659 elf_section_data (sec)->this_hdr.contents = contents;
660 free_contents = NULL;
662 symtab_hdr->contents = (bfd_byte *) extsyms;
665 /* Fix the opcode. */
667 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
668 else if (code == 0xff)
669 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
671 /* Fix the relocation's type. */
672 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
675 /* Delete two bytes of data. */
676 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
677 irel->r_offset + 1, 2))
680 /* That will change things, so, we should relax again.
681 Note that this is not required, and it may be slow. */
686 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
688 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
690 bfd_vma value = symval;
692 /* Deal with pc-relative gunk. */
693 value -= (sec->output_section->vma + sec->output_offset);
694 value -= irel->r_offset;
695 value += irel->r_addend;
697 /* See if the value will fit in 8 bits, note the high value is
698 0x7f + 1 as the target will be one bytes closer if we are
700 if ((long)value < 0x80 && (long)value > -0x80)
704 /* Get the opcode. */
705 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
710 /* Note that we've changed the relocs, section contents, etc. */
711 elf_section_data (sec)->relocs = internal_relocs;
714 elf_section_data (sec)->this_hdr.contents = contents;
715 free_contents = NULL;
717 symtab_hdr->contents = (bfd_byte *) extsyms;
720 /* Fix the opcode. */
721 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
723 /* Fix the relocation's type. */
724 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
727 /* Delete one byte of data. */
728 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
729 irel->r_offset + 1, 1))
732 /* That will change things, so, we should relax again.
733 Note that this is not required, and it may be slow. */
738 /* Try to eliminate an unconditional 8 bit pc-relative branch
739 which immediately follows a conditional 8 bit pc-relative
740 branch around the unconditional branch.
748 This happens when the bCC can't reach lab2 at assembly time,
749 but due to other relaxations it can reach at link time. */
750 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
752 Elf_Internal_Rela *nrel;
753 bfd_vma value = symval;
756 /* Deal with pc-relative gunk. */
757 value -= (sec->output_section->vma + sec->output_offset);
758 value -= irel->r_offset;
759 value += irel->r_addend;
761 /* Do nothing if this reloc is the last byte in the section. */
762 if (irel->r_offset == sec->_cooked_size)
765 /* See if the next instruction is an unconditional pc-relative
766 branch, more often than not this test will fail, so we
767 test it first to speed things up. */
768 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
772 /* Also make sure the next relocation applies to the next
773 instruction and that it's a pc-relative 8 bit branch. */
776 || irel->r_offset + 2 != nrel->r_offset
777 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
780 /* Make sure our destination immediately follows the
781 unconditional branch. */
782 if (symval != (sec->output_section->vma + sec->output_offset
783 + irel->r_offset + 3))
786 /* Now make sure we are a conditional branch. This may not
787 be necessary, but why take the chance.
789 Note these checks assume that R_MN10300_PCREL8 relocs
790 only occur on bCC and bCCx insns. If they occured
791 elsewhere, we'd need to know the start of this insn
792 for this check to be accurate. */
793 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
794 if (code != 0xc0 && code != 0xc1 && code != 0xc2
795 && code != 0xc3 && code != 0xc4 && code != 0xc5
796 && code != 0xc6 && code != 0xc7 && code != 0xc8
797 && code != 0xc9 && code != 0xe8 && code != 0xe9
798 && code != 0xea && code != 0xeb)
801 /* We also have to be sure there is no symbol/label
802 at the unconditional branch. */
803 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
807 /* Note that we've changed the relocs, section contents, etc. */
808 elf_section_data (sec)->relocs = internal_relocs;
811 elf_section_data (sec)->this_hdr.contents = contents;
812 free_contents = NULL;
814 symtab_hdr->contents = (bfd_byte *) extsyms;
817 /* 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 (!mn10300_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 32bit immediate, displacement or absolute address
884 into a 16bit immediate, displacement or absolute address. */
885 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
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 /* Most insns which have 32bit operands are 6 bytes long;
897 exceptions are pcrel insns and bit insns.
899 We handle pcrel insns above. We don't bother trying
900 to handle the bit insns here.
902 The first byte of the remaining insns will be 0xfc. */
904 /* Get the first opcode. */
905 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
910 /* Get the second opcode. */
911 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
913 if ((code & 0xf0) < 0x80)
916 /* mov (d32,am),dn -> mov (d32,am),dn
917 mov dm,(d32,am) -> mov dn,(d32,am)
918 mov (d32,am),an -> mov (d32,am),an
919 mov dm,(d32,am) -> mov dn,(d32,am)
920 movbu (d32,am),dn -> movbu (d32,am),dn
921 movbu dm,(d32,am) -> movbu dn,(d32,am)
922 movhu (d32,am),dn -> movhu (d32,am),dn
923 movhu dm,(d32,am) -> movhu dn,(d32,am) */
932 /* Not safe if the high bit is on as relaxing may
933 move the value out of high mem and thus not fit
934 in a signed 16bit value. */
939 /* Note that we've changed the relocation contents, etc. */
940 elf_section_data (sec)->relocs = internal_relocs;
943 elf_section_data (sec)->this_hdr.contents = contents;
944 free_contents = NULL;
946 symtab_hdr->contents = (bfd_byte *) extsyms;
949 /* Fix the opcode. */
950 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
951 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
953 /* Fix the relocation's type. */
954 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
957 /* Delete two bytes of data. */
958 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
959 irel->r_offset + 2, 2))
962 /* That will change things, so, we should relax again.
963 Note that this is not required, and it may be slow. */
967 else if ((code & 0xf0) == 0x80
968 || (code & 0xf0) == 0x90)
971 /* mov dn,(abs32) -> mov dn,(abs16)
972 movbu dn,(abs32) -> movbu dn,(abs16)
973 movhu dn,(abs32) -> movhu dn,(abs16) */
977 /* Note that we've changed the relocation contents, etc. */
978 elf_section_data (sec)->relocs = internal_relocs;
981 elf_section_data (sec)->this_hdr.contents = contents;
982 free_contents = NULL;
984 symtab_hdr->contents = (bfd_byte *) extsyms;
987 if ((code & 0xf3) == 0x81)
988 code = 0x01 + (code & 0x0c);
989 else if ((code & 0xf3) == 0x82)
990 code = 0x02 + (code & 0x0c);
991 else if ((code & 0xf3) == 0x83)
992 code = 0x03 + (code & 0x0c);
996 /* Fix the opcode. */
997 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
999 /* Fix the relocation's type. */
1000 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1003 /* The opcode got shorter too, so we have to fix the
1004 addend and offset too! */
1005 irel->r_offset -= 1;
1007 /* Delete three bytes of data. */
1008 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1009 irel->r_offset + 1, 3))
1012 /* That will change things, so, we should relax again.
1013 Note that this is not required, and it may be slow. */
1017 /* mov am,(abs32) -> mov am,(abs16)
1018 mov am,(d32,sp) -> mov am,(d16,sp)
1019 mov dm,(d32,sp) -> mov dm,(d32,sp)
1020 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
1021 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
1027 /* Note that we've changed the relocation contents, etc. */
1028 elf_section_data (sec)->relocs = internal_relocs;
1031 elf_section_data (sec)->this_hdr.contents = contents;
1032 free_contents = NULL;
1034 symtab_hdr->contents = (bfd_byte *) extsyms;
1035 free_extsyms = NULL;
1037 /* Fix the opcode. */
1038 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1039 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1041 /* Fix the relocation's type. */
1042 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1045 /* Delete two bytes of data. */
1046 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1047 irel->r_offset + 2, 2))
1050 /* That will change things, so, we should relax again.
1051 Note that this is not required, and it may be slow. */
1055 else if ((code & 0xf0) < 0xf0)
1056 switch (code & 0xfc)
1058 /* mov imm32,dn -> mov imm16,dn
1059 mov imm32,an -> mov imm16,an
1060 mov (abs32),dn -> mov (abs16),dn
1061 movbu (abs32),dn -> movbu (abs16),dn
1062 movhu (abs32),dn -> movhu (abs16),dn */
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. */
1072 && (value & 0x8000))
1075 /* Note that we've changed the relocation contents, etc. */
1076 elf_section_data (sec)->relocs = internal_relocs;
1079 elf_section_data (sec)->this_hdr.contents = contents;
1080 free_contents = NULL;
1082 symtab_hdr->contents = (bfd_byte *) extsyms;
1083 free_extsyms = NULL;
1085 if ((code & 0xfc) == 0xcc)
1086 code = 0x2c + (code & 0x03);
1087 else if ((code & 0xfc) == 0xdc)
1088 code = 0x24 + (code & 0x03);
1089 else if ((code & 0xfc) == 0xa4)
1090 code = 0x30 + (code & 0x03);
1091 else if ((code & 0xfc) == 0xa8)
1092 code = 0x34 + (code & 0x03);
1093 else if ((code & 0xfc) == 0xac)
1094 code = 0x38 + (code & 0x03);
1098 /* Fix the opcode. */
1099 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1101 /* Fix the relocation's type. */
1102 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1105 /* The opcode got shorter too, so we have to fix the
1106 addend and offset too! */
1107 irel->r_offset -= 1;
1109 /* Delete three bytes of data. */
1110 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1111 irel->r_offset + 1, 3))
1114 /* That will change things, so, we should relax again.
1115 Note that this is not required, and it may be slow. */
1119 /* mov (abs32),an -> mov (abs16),an
1120 mov (d32,sp),an -> mov (d32,sp),an
1121 mov (d32,sp),dn -> mov (d32,sp),dn
1122 movbu (d32,sp),dn -> movbu (d32,sp),dn
1123 movhu (d32,sp),dn -> movhu (d32,sp),dn
1124 add imm32,dn -> add imm16,dn
1125 cmp imm32,dn -> cmp imm16,dn
1126 add imm32,an -> add imm16,an
1127 cmp imm32,an -> cmp imm16,an
1128 and imm32,dn -> and imm32,dn
1129 or imm32,dn -> or imm32,dn
1130 xor imm32,dn -> xor imm32,dn
1131 btst imm32,dn -> btst imm32,dn */
1149 /* Note that we've changed the relocation contents, etc. */
1150 elf_section_data (sec)->relocs = internal_relocs;
1153 elf_section_data (sec)->this_hdr.contents = contents;
1154 free_contents = NULL;
1156 symtab_hdr->contents = (bfd_byte *) extsyms;
1157 free_extsyms = NULL;
1159 /* Fix the opcode. */
1160 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1161 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1163 /* Fix the relocation's type. */
1164 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1167 /* Delete two bytes of data. */
1168 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1169 irel->r_offset + 2, 2))
1172 /* That will change things, so, we should relax again.
1173 Note that this is not required, and it may be slow. */
1177 else if (code == 0xfe)
1179 /* add imm32,sp -> add imm16,sp */
1181 /* Note that we've changed the relocation contents, etc. */
1182 elf_section_data (sec)->relocs = internal_relocs;
1185 elf_section_data (sec)->this_hdr.contents = contents;
1186 free_contents = NULL;
1188 symtab_hdr->contents = (bfd_byte *) extsyms;
1189 free_extsyms = NULL;
1191 /* Fix the opcode. */
1192 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1193 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
1195 /* Fix the relocation's type. */
1196 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1199 /* Delete two bytes of data. */
1200 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1201 irel->r_offset + 2, 2))
1204 /* That will change things, so, we should relax again.
1205 Note that this is not required, and it may be slow. */
1213 if (free_relocs != NULL)
1219 if (free_contents != NULL)
1221 if (! link_info->keep_memory)
1222 free (free_contents);
1225 /* Cache the section contents for elf_link_input_bfd. */
1226 elf_section_data (sec)->this_hdr.contents = contents;
1228 free_contents = NULL;
1231 if (free_extsyms != NULL)
1233 if (! link_info->keep_memory)
1234 free (free_extsyms);
1237 /* Cache the symbols for elf_link_input_bfd. */
1238 symtab_hdr->contents = extsyms;
1240 free_extsyms = NULL;
1246 if (free_relocs != NULL)
1248 if (free_contents != NULL)
1249 free (free_contents);
1250 if (free_extsyms != NULL)
1251 free (free_extsyms);
1255 /* Delete some bytes from a section while relaxing. */
1258 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
1264 Elf_Internal_Shdr *symtab_hdr;
1265 Elf32_External_Sym *extsyms;
1268 Elf_Internal_Rela *irel, *irelend;
1269 Elf_Internal_Rela *irelalign;
1271 Elf32_External_Sym *esym, *esymend;
1272 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1275 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1277 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1279 contents = elf_section_data (sec)->this_hdr.contents;
1281 /* The deletion must stop at the next ALIGN reloc for an aligment
1282 power larger than the number of bytes we are deleting. */
1285 toaddr = sec->_cooked_size;
1287 irel = elf_section_data (sec)->relocs;
1288 irelend = irel + sec->reloc_count;
1290 /* Actually delete the bytes. */
1291 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1292 sec->_cooked_size -= count;
1294 /* Adjust all the relocs. */
1295 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1297 /* Get the new reloc address. */
1298 if ((irel->r_offset > addr
1299 && irel->r_offset < toaddr))
1300 irel->r_offset -= count;
1303 /* Adjust all the symbols. */
1305 esymend = esym + symtab_hdr->sh_info;
1306 for (; esym < esymend; esym++)
1308 Elf_Internal_Sym isym;
1310 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1312 if (isym.st_shndx == shndx
1313 && isym.st_value > addr
1314 && isym.st_value < toaddr)
1316 isym.st_value -= count;
1317 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1321 sym_hash = elf_sym_hashes (abfd);
1322 sym_hash_end = (sym_hash
1323 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1324 - symtab_hdr->sh_info));
1325 for (; sym_hash < sym_hash_end; sym_hash++)
1327 if (((*sym_hash)->root.type == bfd_link_hash_defined
1328 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1329 && (*sym_hash)->root.u.def.section == sec
1330 && (*sym_hash)->root.u.def.value > addr
1331 && (*sym_hash)->root.u.def.value < toaddr)
1333 (*sym_hash)->root.u.def.value -= count;
1340 /* Return true if a symbol exists at the given address, else return
1343 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
1346 Elf32_External_Sym *extsyms;
1349 Elf_Internal_Shdr *symtab_hdr;
1351 Elf32_External_Sym *esym, *esymend;
1352 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1355 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1357 /* Examine all the symbols. */
1359 esymend = esym + symtab_hdr->sh_info;
1360 for (; esym < esymend; esym++)
1362 Elf_Internal_Sym isym;
1364 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1366 if (isym.st_shndx == shndx
1367 && isym.st_value == addr)
1371 sym_hash = elf_sym_hashes (abfd);
1372 sym_hash_end = (sym_hash
1373 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1374 - symtab_hdr->sh_info));
1375 for (; sym_hash < sym_hash_end; sym_hash++)
1377 if (((*sym_hash)->root.type == bfd_link_hash_defined
1378 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1379 && (*sym_hash)->root.u.def.section == sec
1380 && (*sym_hash)->root.u.def.value == addr)
1386 /* This is a version of bfd_generic_get_relocated_section_contents
1387 which uses mn10300_elf_relocate_section. */
1390 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1391 data, relocateable, symbols)
1393 struct bfd_link_info *link_info;
1394 struct bfd_link_order *link_order;
1396 boolean relocateable;
1399 Elf_Internal_Shdr *symtab_hdr;
1400 asection *input_section = link_order->u.indirect.section;
1401 bfd *input_bfd = input_section->owner;
1402 asection **sections = NULL;
1403 Elf_Internal_Rela *internal_relocs = NULL;
1404 Elf32_External_Sym *external_syms = NULL;
1405 Elf_Internal_Sym *internal_syms = NULL;
1407 /* We only need to handle the case of relaxing, or of having a
1408 particular set of section contents, specially. */
1410 || elf_section_data (input_section)->this_hdr.contents == NULL)
1411 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1416 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1418 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1419 input_section->_raw_size);
1421 if ((input_section->flags & SEC_RELOC) != 0
1422 && input_section->reloc_count > 0)
1424 Elf_Internal_Sym *isymp;
1426 Elf32_External_Sym *esym, *esymend;
1428 if (symtab_hdr->contents != NULL)
1429 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1432 external_syms = ((Elf32_External_Sym *)
1433 bfd_malloc (symtab_hdr->sh_info
1434 * sizeof (Elf32_External_Sym)));
1435 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1437 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1438 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1439 symtab_hdr->sh_info, input_bfd)
1440 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1444 internal_relocs = (_bfd_elf32_link_read_relocs
1445 (input_bfd, input_section, (PTR) NULL,
1446 (Elf_Internal_Rela *) NULL, false));
1447 if (internal_relocs == NULL)
1450 internal_syms = ((Elf_Internal_Sym *)
1451 bfd_malloc (symtab_hdr->sh_info
1452 * sizeof (Elf_Internal_Sym)));
1453 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1456 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1457 * sizeof (asection *));
1458 if (sections == NULL && symtab_hdr->sh_info > 0)
1461 isymp = internal_syms;
1463 esym = external_syms;
1464 esymend = esym + symtab_hdr->sh_info;
1465 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1469 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1471 if (isymp->st_shndx == SHN_UNDEF)
1472 isec = bfd_und_section_ptr;
1473 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1474 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1475 else if (isymp->st_shndx == SHN_ABS)
1476 isec = bfd_abs_section_ptr;
1477 else if (isymp->st_shndx == SHN_COMMON)
1478 isec = bfd_com_section_ptr;
1488 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
1489 input_section, data, internal_relocs,
1490 internal_syms, sections))
1493 if (sections != NULL)
1496 if (internal_syms != NULL)
1497 free (internal_syms);
1498 internal_syms = NULL;
1499 if (external_syms != NULL && symtab_hdr->contents == NULL)
1500 free (external_syms);
1501 external_syms = NULL;
1502 if (internal_relocs != elf_section_data (input_section)->relocs)
1503 free (internal_relocs);
1504 internal_relocs = NULL;
1510 if (internal_relocs != NULL
1511 && internal_relocs != elf_section_data (input_section)->relocs)
1512 free (internal_relocs);
1513 if (external_syms != NULL && symtab_hdr->contents == NULL)
1514 free (external_syms);
1515 if (internal_syms != NULL)
1516 free (internal_syms);
1517 if (sections != NULL)
1524 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
1525 #define TARGET_LITTLE_NAME "elf32-mn10300"
1526 #define ELF_ARCH bfd_arch_mn10300
1527 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
1528 #define ELF_MAXPAGESIZE 0x1000
1530 #define elf_info_to_howto mn10300_info_to_howto
1531 #define elf_info_to_howto_rel 0
1532 #define elf_backend_relocate_section mn10300_elf_relocate_section
1533 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
1534 #define bfd_elf32_bfd_get_relocated_section_contents \
1535 mn10300_elf_get_relocated_section_contents
1537 #define elf_symbol_leading_char '_'
1539 #include "elf32-target.h"