1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
28 static void mn10200_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd *, asection *, bfd_vma, int));
32 static boolean mn10200_elf_symbol_address_p
33 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
34 static bfd_reloc_status_type mn10200_elf_final_link_relocate
35 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
36 bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
37 struct bfd_link_info *, asection *, int));
38 static boolean mn10200_elf_relocate_section
39 PARAMS (( bfd *, struct bfd_link_info *, bfd *, asection *,
40 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *,
42 static boolean mn10200_elf_relax_section
43 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
44 static bfd_byte * mn10200_elf_get_relocated_section_contents
45 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
46 bfd_byte *, boolean, asymbol **));
48 /* We have to use RELA instructions since md_apply_fix3 in the assembler
49 does absolutely nothing. */
64 static reloc_howto_type elf_mn10200_howto_table[] = {
65 /* Dummy relocation. Does nothing. */
66 HOWTO (R_MN10200_NONE,
72 complain_overflow_bitfield,
73 bfd_elf_generic_reloc,
79 /* Standard 32 bit reloc. */
86 complain_overflow_bitfield,
87 bfd_elf_generic_reloc,
93 /* Standard 16 bit reloc. */
100 complain_overflow_bitfield,
101 bfd_elf_generic_reloc,
107 /* Standard 8 bit reloc. */
114 complain_overflow_bitfield,
115 bfd_elf_generic_reloc,
121 /* Standard 24 bit reloc. */
128 complain_overflow_bitfield,
129 bfd_elf_generic_reloc,
135 /* Simple 8 pc-relative reloc. */
136 HOWTO (R_MN10200_PCREL8,
142 complain_overflow_bitfield,
143 bfd_elf_generic_reloc,
149 /* Simple 16 pc-relative reloc. */
150 HOWTO (R_MN10200_PCREL16,
156 complain_overflow_bitfield,
157 bfd_elf_generic_reloc,
163 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
164 to get the pc-relative offset correct. */
165 HOWTO (R_MN10200_PCREL24,
171 complain_overflow_bitfield,
172 bfd_elf_generic_reloc,
180 struct mn10200_reloc_map {
181 bfd_reloc_code_real_type bfd_reloc_val;
182 unsigned char elf_reloc_val;
185 static const struct mn10200_reloc_map mn10200_reloc_map[] = {
186 { BFD_RELOC_NONE , R_MN10200_NONE , },
187 { BFD_RELOC_32 , R_MN10200_32 , },
188 { BFD_RELOC_16 , R_MN10200_16 , },
189 { BFD_RELOC_8 , R_MN10200_8 , },
190 { BFD_RELOC_24 , R_MN10200_24 , },
191 { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
192 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
193 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
196 static reloc_howto_type *
197 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
198 bfd *abfd ATTRIBUTE_UNUSED;
199 bfd_reloc_code_real_type code;
204 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
207 if (mn10200_reloc_map[i].bfd_reloc_val == code)
208 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
214 /* Set the howto pointer for an MN10200 ELF reloc. */
217 mn10200_info_to_howto (abfd, cache_ptr, dst)
218 bfd *abfd ATTRIBUTE_UNUSED;
220 Elf32_Internal_Rela *dst;
224 r_type = ELF32_R_TYPE (dst->r_info);
225 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
226 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
229 /* Perform a relocation as part of a final link. */
231 static bfd_reloc_status_type
232 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
233 input_section, contents, offset, value,
234 addend, info, sym_sec, is_local)
235 reloc_howto_type *howto;
237 bfd *output_bfd ATTRIBUTE_UNUSED;
238 asection *input_section;
243 struct bfd_link_info *info ATTRIBUTE_UNUSED;
244 asection *sym_sec ATTRIBUTE_UNUSED;
245 int is_local ATTRIBUTE_UNUSED;
247 unsigned long r_type = howto->type;
248 bfd_byte *hit_data = contents + offset;
258 bfd_put_32 (input_bfd, value, hit_data);
264 if ((long) value > 0x7fff || (long) value < -0x8000)
265 return bfd_reloc_overflow;
267 bfd_put_16 (input_bfd, value, hit_data);
273 if ((long) value > 0x7f || (long) value < -0x80)
274 return bfd_reloc_overflow;
276 bfd_put_8 (input_bfd, value, hit_data);
282 if ((long) value > 0x7fffff || (long) value < -0x800000)
283 return bfd_reloc_overflow;
286 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
287 bfd_put_32 (input_bfd, value, hit_data);
290 case R_MN10200_PCREL8:
291 value -= (input_section->output_section->vma
292 + input_section->output_offset);
293 value -= (offset + 1);
296 if ((long) value > 0xff || (long) value < -0x100)
297 return bfd_reloc_overflow;
299 bfd_put_8 (input_bfd, value, hit_data);
302 case R_MN10200_PCREL16:
303 value -= (input_section->output_section->vma
304 + input_section->output_offset);
305 value -= (offset + 2);
308 if ((long) value > 0xffff || (long) value < -0x10000)
309 return bfd_reloc_overflow;
311 bfd_put_16 (input_bfd, value, hit_data);
314 case R_MN10200_PCREL24:
315 value -= (input_section->output_section->vma
316 + input_section->output_offset);
317 value -= (offset + 3);
320 if ((long) value > 0xffffff || (long) value < -0x1000000)
321 return bfd_reloc_overflow;
324 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
325 bfd_put_32 (input_bfd, value, hit_data);
329 return bfd_reloc_notsupported;
333 /* Relocate an MN10200 ELF section. */
335 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
336 contents, relocs, local_syms, local_sections)
338 struct bfd_link_info *info;
340 asection *input_section;
342 Elf_Internal_Rela *relocs;
343 Elf_Internal_Sym *local_syms;
344 asection **local_sections;
346 Elf_Internal_Shdr *symtab_hdr;
347 struct elf_link_hash_entry **sym_hashes;
348 Elf_Internal_Rela *rel, *relend;
350 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
351 sym_hashes = elf_sym_hashes (input_bfd);
354 relend = relocs + input_section->reloc_count;
355 for (; rel < relend; rel++)
358 reloc_howto_type *howto;
359 unsigned long r_symndx;
360 Elf_Internal_Sym *sym;
362 struct elf_link_hash_entry *h;
364 bfd_reloc_status_type r;
366 r_symndx = ELF32_R_SYM (rel->r_info);
367 r_type = ELF32_R_TYPE (rel->r_info);
368 howto = elf_mn10200_howto_table + r_type;
370 if (info->relocateable)
372 /* This is a relocateable link. We don't have to change
373 anything, unless the reloc is against a section symbol,
374 in which case we have to adjust according to where the
375 section symbol winds up in the output section. */
376 if (r_symndx < symtab_hdr->sh_info)
378 sym = local_syms + r_symndx;
379 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
381 sec = local_sections[r_symndx];
382 rel->r_addend += sec->output_offset + sym->st_value;
389 /* This is a final link. */
393 if (r_symndx < symtab_hdr->sh_info)
395 sym = local_syms + r_symndx;
396 sec = local_sections[r_symndx];
397 relocation = (sec->output_section->vma
403 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
404 while (h->root.type == bfd_link_hash_indirect
405 || h->root.type == bfd_link_hash_warning)
406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
407 if (h->root.type == bfd_link_hash_defined
408 || h->root.type == bfd_link_hash_defweak)
410 sec = h->root.u.def.section;
411 relocation = (h->root.u.def.value
412 + sec->output_section->vma
413 + sec->output_offset);
415 else if (h->root.type == bfd_link_hash_undefweak)
419 if (! ((*info->callbacks->undefined_symbol)
420 (info, h->root.root.string, input_bfd,
421 input_section, rel->r_offset, true)))
427 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
429 contents, rel->r_offset,
430 relocation, rel->r_addend,
431 info, sec, h == NULL);
433 if (r != bfd_reloc_ok)
436 const char *msg = (const char *) 0;
439 name = h->root.root.string;
442 name = (bfd_elf_string_from_elf_section
443 (input_bfd, symtab_hdr->sh_link, sym->st_name));
444 if (name == NULL || *name == '\0')
445 name = bfd_section_name (input_bfd, sec);
450 case bfd_reloc_overflow:
451 if (! ((*info->callbacks->reloc_overflow)
452 (info, name, howto->name, (bfd_vma) 0,
453 input_bfd, input_section, rel->r_offset)))
457 case bfd_reloc_undefined:
458 if (! ((*info->callbacks->undefined_symbol)
459 (info, name, input_bfd, input_section,
460 rel->r_offset, true)))
464 case bfd_reloc_outofrange:
465 msg = _("internal error: out of range error");
468 case bfd_reloc_notsupported:
469 msg = _("internal error: unsupported relocation error");
472 case bfd_reloc_dangerous:
473 msg = _("internal error: dangerous error");
477 msg = _("internal error: unknown error");
481 if (!((*info->callbacks->warning)
482 (info, msg, name, input_bfd, input_section,
493 /* This function handles relaxing for the mn10200.
495 There's quite a few relaxing opportunites available on the mn10200:
497 * jsr:24 -> jsr:16 2 bytes
499 * jmp:24 -> jmp:16 2 bytes
500 * jmp:16 -> bra:8 1 byte
502 * If the previous instruction is a conditional branch
503 around the jump/bra, we may be able to reverse its condition
504 and change its target to the jump's target. The jump/bra
505 can then be deleted. 2 bytes
507 * mov abs24 -> mov abs16 2 byte savings
509 * Most instructions which accept imm24 can relax to imm16 2 bytes
510 - Most instructions which accept imm16 can relax to imm8 1 byte
512 * Most instructions which accept d24 can relax to d16 2 bytes
513 - Most instructions which accept d16 can relax to d8 1 byte
515 abs24, imm24, d24 all look the same at the reloc level. It
516 might make the code simpler if we had different relocs for
517 the various relaxable operand types.
519 We don't handle imm16->imm8 or d16->d8 as they're very rare
520 and somewhat more difficult to support. */
523 mn10200_elf_relax_section (abfd, sec, link_info, again)
526 struct bfd_link_info *link_info;
529 Elf_Internal_Shdr *symtab_hdr;
530 Elf_Internal_Rela *internal_relocs;
531 Elf_Internal_Rela *free_relocs = NULL;
532 Elf_Internal_Rela *irel, *irelend;
533 bfd_byte *contents = NULL;
534 bfd_byte *free_contents = NULL;
535 Elf32_External_Sym *extsyms = NULL;
536 Elf32_External_Sym *free_extsyms = NULL;
538 /* Assume nothing changes. */
541 /* We don't have to do anything for a relocateable link, if
542 this section does not have relocs, or if this is not a
544 if (link_info->relocateable
545 || (sec->flags & SEC_RELOC) == 0
546 || sec->reloc_count == 0
547 || (sec->flags & SEC_CODE) == 0)
550 /* If this is the first time we have been called for this section,
551 initialize the cooked size. */
552 if (sec->_cooked_size == 0)
553 sec->_cooked_size = sec->_raw_size;
555 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
557 /* Get a copy of the native relocations. */
558 internal_relocs = (_bfd_elf32_link_read_relocs
559 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
560 link_info->keep_memory));
561 if (internal_relocs == NULL)
563 if (! link_info->keep_memory)
564 free_relocs = internal_relocs;
566 /* Walk through them looking for relaxing opportunities. */
567 irelend = internal_relocs + sec->reloc_count;
568 for (irel = internal_relocs; irel < irelend; irel++)
572 /* If this isn't something that can be relaxed, then ignore
574 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
575 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
576 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
579 /* Get the section contents if we haven't done so already. */
580 if (contents == NULL)
582 /* Get cached copy if it exists. */
583 if (elf_section_data (sec)->this_hdr.contents != NULL)
584 contents = elf_section_data (sec)->this_hdr.contents;
587 /* Go get them off disk. */
588 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
589 if (contents == NULL)
591 free_contents = contents;
593 if (! bfd_get_section_contents (abfd, sec, contents,
594 (file_ptr) 0, sec->_raw_size))
599 /* Read this BFD's symbols if we haven't done so already. */
602 /* Get cached copy if it exists. */
603 if (symtab_hdr->contents != NULL)
604 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
607 /* Go get them off disk. */
608 extsyms = ((Elf32_External_Sym *)
609 bfd_malloc (symtab_hdr->sh_size));
612 free_extsyms = extsyms;
613 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
614 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
615 != symtab_hdr->sh_size))
620 /* Get the value of the symbol referred to by the reloc. */
621 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
623 Elf_Internal_Sym isym;
626 /* A local symbol. */
627 bfd_elf32_swap_symbol_in (abfd,
628 extsyms + ELF32_R_SYM (irel->r_info),
631 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
632 symval = (isym.st_value
633 + sym_sec->output_section->vma
634 + sym_sec->output_offset);
639 struct elf_link_hash_entry *h;
641 /* An external symbol. */
642 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
643 h = elf_sym_hashes (abfd)[indx];
644 BFD_ASSERT (h != NULL);
645 if (h->root.type != bfd_link_hash_defined
646 && h->root.type != bfd_link_hash_defweak)
648 /* This appears to be a reference to an undefined
649 symbol. Just ignore it--it will be caught by the
650 regular reloc processing. */
654 symval = (h->root.u.def.value
655 + h->root.u.def.section->output_section->vma
656 + h->root.u.def.section->output_offset);
659 /* For simplicity of coding, we are going to modify the section
660 contents, the section relocs, and the BFD symbol table. We
661 must tell the rest of the code not to free up this
662 information. It would be possible to instead create a table
663 of changes which have to be made, as is done in coff-mips.c;
664 that would be more work, but would require less memory when
665 the linker is run. */
667 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
669 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
671 bfd_vma value = symval;
673 /* Deal with pc-relative gunk. */
674 value -= (sec->output_section->vma + sec->output_offset);
675 value -= (irel->r_offset + 3);
676 value += irel->r_addend;
678 /* See if the value will fit in 16 bits, note the high value is
679 0x7fff + 2 as the target will be two bytes closer if we are
681 if ((long) value < 0x8001 && (long) value > -0x8000)
685 /* Get the opcode. */
686 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
688 if (code != 0xe0 && code != 0xe1)
691 /* Note that we've changed the relocs, section contents, etc. */
692 elf_section_data (sec)->relocs = internal_relocs;
695 elf_section_data (sec)->this_hdr.contents = contents;
696 free_contents = NULL;
698 symtab_hdr->contents = (bfd_byte *) extsyms;
701 /* Fix the opcode. */
703 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
704 else if (code == 0xe1)
705 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
707 /* Fix the relocation's type. */
708 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
711 /* The opcode got shorter too, so we have to fix the offset. */
714 /* Delete two bytes of data. */
715 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
716 irel->r_offset + 1, 2))
719 /* That will change things, so, we should relax again.
720 Note that this is not required, and it may be slow. */
725 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
727 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
729 bfd_vma value = symval;
731 /* Deal with pc-relative gunk. */
732 value -= (sec->output_section->vma + sec->output_offset);
733 value -= (irel->r_offset + 2);
734 value += irel->r_addend;
736 /* See if the value will fit in 8 bits, note the high value is
737 0x7f + 1 as the target will be one bytes closer if we are
739 if ((long) value < 0x80 && (long) value > -0x80)
743 /* Get the opcode. */
744 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
749 /* Note that we've changed the relocs, section contents, etc. */
750 elf_section_data (sec)->relocs = internal_relocs;
753 elf_section_data (sec)->this_hdr.contents = contents;
754 free_contents = NULL;
756 symtab_hdr->contents = (bfd_byte *) extsyms;
759 /* Fix the opcode. */
760 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
762 /* Fix the relocation's type. */
763 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
766 /* Delete one byte of data. */
767 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
768 irel->r_offset + 1, 1))
771 /* That will change things, so, we should relax again.
772 Note that this is not required, and it may be slow. */
777 /* Try to eliminate an unconditional 8 bit pc-relative branch
778 which immediately follows a conditional 8 bit pc-relative
779 branch around the unconditional branch.
786 This happens when the bCC can't reach lab2 at assembly time,
787 but due to other relaxations it can reach at link time. */
788 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
790 Elf_Internal_Rela *nrel;
791 bfd_vma value = symval;
794 /* Deal with pc-relative gunk. */
795 value -= (sec->output_section->vma + sec->output_offset);
796 value -= (irel->r_offset + 1);
797 value += irel->r_addend;
799 /* Do nothing if this reloc is the last byte in the section. */
800 if (irel->r_offset == sec->_cooked_size)
803 /* See if the next instruction is an unconditional pc-relative
804 branch, more often than not this test will fail, so we
805 test it first to speed things up. */
806 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
810 /* Also make sure the next relocation applies to the next
811 instruction and that it's a pc-relative 8 bit branch. */
814 || irel->r_offset + 2 != nrel->r_offset
815 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
818 /* Make sure our destination immediately follows the
819 unconditional branch. */
820 if (symval != (sec->output_section->vma + sec->output_offset
821 + irel->r_offset + 3))
824 /* Now make sure we are a conditional branch. This may not
825 be necessary, but why take the chance.
827 Note these checks assume that R_MN10200_PCREL8 relocs
828 only occur on bCC and bCCx insns. If they occured
829 elsewhere, we'd need to know the start of this insn
830 for this check to be accurate. */
831 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
832 if (code != 0xe0 && code != 0xe1 && code != 0xe2
833 && code != 0xe3 && code != 0xe4 && code != 0xe5
834 && code != 0xe6 && code != 0xe7 && code != 0xe8
835 && code != 0xe9 && code != 0xec && code != 0xed
836 && code != 0xee && code != 0xef && code != 0xfc
837 && code != 0xfd && code != 0xfe && code != 0xff)
840 /* We also have to be sure there is no symbol/label
841 at the unconditional branch. */
842 if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
846 /* Note that we've changed the relocs, section contents, etc. */
847 elf_section_data (sec)->relocs = internal_relocs;
850 elf_section_data (sec)->this_hdr.contents = contents;
851 free_contents = NULL;
853 symtab_hdr->contents = (bfd_byte *) extsyms;
856 /* Reverse the condition of the first branch. */
914 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
916 /* Set the reloc type and symbol for the first branch
917 from the second branch. */
918 irel->r_info = nrel->r_info;
920 /* Make the reloc for the second branch a null reloc. */
921 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
924 /* Delete two bytes of data. */
925 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
926 irel->r_offset + 1, 2))
929 /* That will change things, so, we should relax again.
930 Note that this is not required, and it may be slow. */
934 /* Try to turn a 24bit immediate, displacement or absolute address
935 into a 16bit immediate, displacement or absolute address. */
936 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
938 bfd_vma value = symval;
940 /* See if the value will fit in 16 bits.
941 We allow any 16bit match here. We prune those we can't
943 if ((long) value < 0x7fff && (long) value > -0x8000)
947 /* All insns which have 24bit operands are 5 bytes long,
948 the first byte will always be 0xf4, but we double check
951 /* Get the first opcode. */
952 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
957 /* Get the second opcode. */
958 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
962 /* mov imm24,dn -> mov imm16,dn */
964 /* Not safe if the high bit is on as relaxing may
965 move the value out of high mem and thus not fit
966 in a signed 16bit value. */
970 /* Note that we've changed the reldection contents, etc. */
971 elf_section_data (sec)->relocs = internal_relocs;
974 elf_section_data (sec)->this_hdr.contents = contents;
975 free_contents = NULL;
977 symtab_hdr->contents = (bfd_byte *) extsyms;
980 /* Fix the opcode. */
981 bfd_put_8 (abfd, 0xf8 + (code & 0x03),
982 contents + irel->r_offset - 2);
984 /* Fix the relocation's type. */
985 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
988 /* The opcode got shorter too, so we have to fix the
992 /* Delete two bytes of data. */
993 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
994 irel->r_offset + 1, 2))
997 /* That will change things, so, we should relax again.
998 Note that this is not required, and it may be slow. */
1002 /* mov imm24,an -> mov imm16,an
1003 cmp imm24,an -> cmp imm16,an
1004 mov (abs24),dn -> mov (abs16),dn
1005 mov dn,(abs24) -> mov dn,(abs16)
1006 movb dn,(abs24) -> movb dn,(abs16)
1007 movbu (abs24),dn -> movbu (abs16),dn */
1014 /* Note that we've changed the reldection contents, etc. */
1015 elf_section_data (sec)->relocs = internal_relocs;
1018 elf_section_data (sec)->this_hdr.contents = contents;
1019 free_contents = NULL;
1021 symtab_hdr->contents = (bfd_byte *) extsyms;
1022 free_extsyms = NULL;
1024 if ((code & 0xfc) == 0x74)
1025 code = 0xdc + (code & 0x03);
1026 else if ((code & 0xfc) == 0x7c)
1027 code = 0xec + (code & 0x03);
1028 else if ((code & 0xfc) == 0xc0)
1029 code = 0xc8 + (code & 0x03);
1030 else if ((code & 0xfc) == 0x40)
1031 code = 0xc0 + (code & 0x03);
1032 else if ((code & 0xfc) == 0x44)
1033 code = 0xc4 + (code & 0x03);
1034 else if ((code & 0xfc) == 0xc8)
1035 code = 0xcc + (code & 0x03);
1037 /* Fix the opcode. */
1038 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1040 /* Fix the relocation's type. */
1041 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1044 /* The opcode got shorter too, so we have to fix the
1046 irel->r_offset -= 1;
1048 /* Delete two bytes of data. */
1049 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1050 irel->r_offset + 1, 2))
1053 /* That will change things, so, we should relax again.
1054 Note that this is not required, and it may be slow. */
1058 /* cmp imm24,dn -> cmp imm16,dn
1059 mov (abs24),an -> mov (abs16),an
1060 mov an,(abs24) -> mov an,(abs16)
1061 add imm24,dn -> add imm16,dn
1062 add imm24,an -> add imm16,an
1063 sub imm24,dn -> sub imm16,dn
1064 sub imm24,an -> sub imm16,an
1065 And all d24->d16 in memory ops. */
1082 /* Not safe if the high bit is on as relaxing may
1083 move the value out of high mem and thus not fit
1084 in a signed 16bit value. */
1085 if (((code & 0xfc) == 0x78
1086 || (code & 0xfc) == 0x60
1087 || (code & 0xfc) == 0x64
1088 || (code & 0xfc) == 0x68
1089 || (code & 0xfc) == 0x6c
1090 || (code & 0xfc) == 0x80
1091 || (code & 0xfc) == 0xf0
1092 || (code & 0xfc) == 0x00
1093 || (code & 0xfc) == 0x10
1094 || (code & 0xfc) == 0xb0
1095 || (code & 0xfc) == 0x30
1096 || (code & 0xfc) == 0xa0
1097 || (code & 0xfc) == 0x20
1098 || (code & 0xfc) == 0x90)
1099 && (value & 0x8000) != 0)
1102 /* Note that we've changed the reldection contents, etc. */
1103 elf_section_data (sec)->relocs = internal_relocs;
1106 elf_section_data (sec)->this_hdr.contents = contents;
1107 free_contents = NULL;
1109 symtab_hdr->contents = (bfd_byte *) extsyms;
1110 free_extsyms = NULL;
1112 /* Fix the opcode. */
1113 bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1115 if ((code & 0xfc) == 0x78)
1116 code = 0x48 + (code & 0x03);
1117 else if ((code & 0xfc) == 0xd0)
1118 code = 0x30 + (code & 0x03);
1119 else if ((code & 0xfc) == 0x50)
1120 code = 0x20 + (code & 0x03);
1121 else if ((code & 0xfc) == 0x60)
1122 code = 0x18 + (code & 0x03);
1123 else if ((code & 0xfc) == 0x64)
1124 code = 0x08 + (code & 0x03);
1125 else if ((code & 0xfc) == 0x68)
1126 code = 0x1c + (code & 0x03);
1127 else if ((code & 0xfc) == 0x6c)
1128 code = 0x0c + (code & 0x03);
1129 else if ((code & 0xfc) == 0x80)
1130 code = 0xc0 + (code & 0x07);
1131 else if ((code & 0xfc) == 0xf0)
1132 code = 0xb0 + (code & 0x07);
1133 else if ((code & 0xfc) == 0x00)
1134 code = 0x80 + (code & 0x07);
1135 else if ((code & 0xfc) == 0x10)
1136 code = 0xa0 + (code & 0x07);
1137 else if ((code & 0xfc) == 0xb0)
1138 code = 0x70 + (code & 0x07);
1139 else if ((code & 0xfc) == 0x30)
1140 code = 0x60 + (code & 0x07);
1141 else if ((code & 0xfc) == 0xa0)
1142 code = 0xd0 + (code & 0x07);
1143 else if ((code & 0xfc) == 0x20)
1144 code = 0x90 + (code & 0x07);
1145 else if ((code & 0xfc) == 0x90)
1146 code = 0x50 + (code & 0x07);
1148 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1150 /* Fix the relocation's type. */
1151 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1154 /* Delete one bytes of data. */
1155 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1156 irel->r_offset + 2, 1))
1159 /* That will change things, so, we should relax again.
1160 Note that this is not required, and it may be slow. */
1164 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1166 /* Note that we've changed the reldection contents, etc. */
1167 elf_section_data (sec)->relocs = internal_relocs;
1170 elf_section_data (sec)->this_hdr.contents = contents;
1171 free_contents = NULL;
1173 symtab_hdr->contents = (bfd_byte *) extsyms;
1174 free_extsyms = NULL;
1176 bfd_put_8 (abfd, 0xcc + (code & 0x03),
1177 contents + irel->r_offset - 2);
1179 bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1180 contents + irel->r_offset - 1);
1182 /* Fix the relocation's type. */
1183 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1186 /* The reloc will be applied one byte in front of its
1187 current location. */
1188 irel->r_offset -= 1;
1190 /* Delete one bytes of data. */
1191 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1192 irel->r_offset + 2, 1))
1195 /* That will change things, so, we should relax again.
1196 Note that this is not required, and it may be slow. */
1204 if (free_relocs != NULL)
1210 if (free_contents != NULL)
1212 if (! link_info->keep_memory)
1213 free (free_contents);
1216 /* Cache the section contents for elf_link_input_bfd. */
1217 elf_section_data (sec)->this_hdr.contents = contents;
1219 free_contents = NULL;
1222 if (free_extsyms != NULL)
1224 if (! link_info->keep_memory)
1225 free (free_extsyms);
1228 /* Cache the symbols for elf_link_input_bfd. */
1229 symtab_hdr->contents = extsyms;
1231 free_extsyms = NULL;
1237 if (free_relocs != NULL)
1239 if (free_contents != NULL)
1240 free (free_contents);
1241 if (free_extsyms != NULL)
1242 free (free_extsyms);
1246 /* Delete some bytes from a section while relaxing. */
1249 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1255 Elf_Internal_Shdr *symtab_hdr;
1256 Elf32_External_Sym *extsyms;
1259 Elf_Internal_Rela *irel, *irelend;
1260 Elf_Internal_Rela *irelalign;
1262 Elf32_External_Sym *esym, *esymend;
1263 struct elf_link_hash_entry *sym_hash;
1265 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1266 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1268 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1270 contents = elf_section_data (sec)->this_hdr.contents;
1272 /* The deletion must stop at the next ALIGN reloc for an aligment
1273 power larger than the number of bytes we are deleting. */
1276 toaddr = sec->_cooked_size;
1278 irel = elf_section_data (sec)->relocs;
1279 irelend = irel + sec->reloc_count;
1281 /* Actually delete the bytes. */
1282 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1283 sec->_cooked_size -= count;
1285 /* Adjust all the relocs. */
1286 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1288 /* Get the new reloc address. */
1289 if ((irel->r_offset > addr
1290 && irel->r_offset < toaddr))
1291 irel->r_offset -= count;
1294 /* Adjust the local symbols defined in this section. */
1296 esymend = esym + symtab_hdr->sh_info;
1297 for (; esym < esymend; esym++)
1299 Elf_Internal_Sym isym;
1301 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1303 if (isym.st_shndx == shndx
1304 && isym.st_value > addr
1305 && isym.st_value < toaddr)
1307 isym.st_value -= count;
1308 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1312 /* Now adjust the global symbols defined in this section. */
1313 esym = extsyms + symtab_hdr->sh_info;
1314 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
1315 for (index = 0; esym < esymend; esym++, index++)
1317 Elf_Internal_Sym isym;
1319 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1320 sym_hash = elf_sym_hashes (abfd)[index];
1321 if (isym.st_shndx == shndx
1322 && ((sym_hash)->root.type == bfd_link_hash_defined
1323 || (sym_hash)->root.type == bfd_link_hash_defweak)
1324 && (sym_hash)->root.u.def.section == sec
1325 && (sym_hash)->root.u.def.value > addr
1326 && (sym_hash)->root.u.def.value < toaddr)
1328 (sym_hash)->root.u.def.value -= count;
1335 /* Return true if a symbol exists at the given address, else return
1338 mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1341 Elf32_External_Sym *extsyms;
1344 Elf_Internal_Shdr *symtab_hdr;
1346 Elf32_External_Sym *esym, *esymend;
1347 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1349 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1350 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1352 /* Examine all the symbols. */
1354 esymend = esym + symtab_hdr->sh_info;
1355 for (; esym < esymend; esym++)
1357 Elf_Internal_Sym isym;
1359 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1361 if (isym.st_shndx == shndx
1362 && isym.st_value == addr)
1366 sym_hash = elf_sym_hashes (abfd);
1367 sym_hash_end = (sym_hash
1368 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1369 - symtab_hdr->sh_info));
1370 for (; sym_hash < sym_hash_end; sym_hash++)
1372 if (((*sym_hash)->root.type == bfd_link_hash_defined
1373 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1374 && (*sym_hash)->root.u.def.section == sec
1375 && (*sym_hash)->root.u.def.value == addr)
1381 /* This is a version of bfd_generic_get_relocated_section_contents
1382 which uses mn10200_elf_relocate_section. */
1385 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1386 data, relocateable, symbols)
1388 struct bfd_link_info *link_info;
1389 struct bfd_link_order *link_order;
1391 boolean relocateable;
1394 Elf_Internal_Shdr *symtab_hdr;
1395 asection *input_section = link_order->u.indirect.section;
1396 bfd *input_bfd = input_section->owner;
1397 asection **sections = NULL;
1398 Elf_Internal_Rela *internal_relocs = NULL;
1399 Elf32_External_Sym *external_syms = NULL;
1400 Elf_Internal_Sym *internal_syms = NULL;
1402 /* We only need to handle the case of relaxing, or of having a
1403 particular set of section contents, specially. */
1405 || elf_section_data (input_section)->this_hdr.contents == NULL)
1406 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1411 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1413 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1414 input_section->_raw_size);
1416 if ((input_section->flags & SEC_RELOC) != 0
1417 && input_section->reloc_count > 0)
1419 Elf_Internal_Sym *isymp;
1421 Elf32_External_Sym *esym, *esymend;
1423 if (symtab_hdr->contents != NULL)
1424 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1427 external_syms = ((Elf32_External_Sym *)
1428 bfd_malloc (symtab_hdr->sh_info
1429 * sizeof (Elf32_External_Sym)));
1430 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1432 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1433 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1434 symtab_hdr->sh_info, input_bfd)
1435 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1439 internal_relocs = (_bfd_elf32_link_read_relocs
1440 (input_bfd, input_section, (PTR) NULL,
1441 (Elf_Internal_Rela *) NULL, false));
1442 if (internal_relocs == NULL)
1445 internal_syms = ((Elf_Internal_Sym *)
1446 bfd_malloc (symtab_hdr->sh_info
1447 * sizeof (Elf_Internal_Sym)));
1448 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1451 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1452 * sizeof (asection *));
1453 if (sections == NULL && symtab_hdr->sh_info > 0)
1456 isymp = internal_syms;
1458 esym = external_syms;
1459 esymend = esym + symtab_hdr->sh_info;
1460 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1464 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1466 if (isymp->st_shndx == SHN_UNDEF)
1467 isec = bfd_und_section_ptr;
1468 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1469 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1470 else if (isymp->st_shndx == SHN_ABS)
1471 isec = bfd_abs_section_ptr;
1472 else if (isymp->st_shndx == SHN_COMMON)
1473 isec = bfd_com_section_ptr;
1483 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1484 input_section, data, internal_relocs,
1485 internal_syms, sections))
1488 if (sections != NULL)
1491 if (internal_syms != NULL)
1492 free (internal_syms);
1493 internal_syms = NULL;
1494 if (external_syms != NULL && symtab_hdr->contents == NULL)
1495 free (external_syms);
1496 external_syms = NULL;
1497 if (internal_relocs != elf_section_data (input_section)->relocs)
1498 free (internal_relocs);
1499 internal_relocs = NULL;
1505 if (internal_relocs != NULL
1506 && internal_relocs != elf_section_data (input_section)->relocs)
1507 free (internal_relocs);
1508 if (external_syms != NULL && symtab_hdr->contents == NULL)
1509 free (external_syms);
1510 if (internal_syms != NULL)
1511 free (internal_syms);
1512 if (sections != NULL)
1517 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1518 #define TARGET_LITTLE_NAME "elf32-mn10200"
1519 #define ELF_ARCH bfd_arch_mn10200
1520 #define ELF_MACHINE_CODE EM_MN10200
1521 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1522 #define ELF_MAXPAGESIZE 0x1000
1524 #define elf_info_to_howto mn10200_info_to_howto
1525 #define elf_info_to_howto_rel 0
1526 #define elf_backend_relocate_section mn10200_elf_relocate_section
1527 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1528 #define bfd_elf32_bfd_get_relocated_section_contents \
1529 mn10200_elf_get_relocated_section_contents
1531 #define elf_symbol_leading_char '_'
1533 #include "elf32-target.h"