1 /* Matsushita 10200 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 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. */
51 static reloc_howto_type elf_mn10200_howto_table[] =
53 /* Dummy relocation. Does nothing. */
54 HOWTO (R_MN10200_NONE,
60 complain_overflow_bitfield,
61 bfd_elf_generic_reloc,
67 /* Standard 32 bit reloc. */
74 complain_overflow_bitfield,
75 bfd_elf_generic_reloc,
81 /* Standard 16 bit reloc. */
88 complain_overflow_bitfield,
89 bfd_elf_generic_reloc,
95 /* Standard 8 bit reloc. */
102 complain_overflow_bitfield,
103 bfd_elf_generic_reloc,
109 /* Standard 24 bit reloc. */
116 complain_overflow_bitfield,
117 bfd_elf_generic_reloc,
123 /* Simple 8 pc-relative reloc. */
124 HOWTO (R_MN10200_PCREL8,
130 complain_overflow_bitfield,
131 bfd_elf_generic_reloc,
137 /* Simple 16 pc-relative reloc. */
138 HOWTO (R_MN10200_PCREL16,
144 complain_overflow_bitfield,
145 bfd_elf_generic_reloc,
151 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
152 to get the pc-relative offset correct. */
153 HOWTO (R_MN10200_PCREL24,
159 complain_overflow_bitfield,
160 bfd_elf_generic_reloc,
168 struct mn10200_reloc_map
170 unsigned char bfd_reloc_val;
171 unsigned char elf_reloc_val;
174 static const struct mn10200_reloc_map mn10200_reloc_map[] =
176 { BFD_RELOC_NONE, R_MN10200_NONE, },
177 { BFD_RELOC_32, R_MN10200_32, },
178 { BFD_RELOC_16, R_MN10200_16, },
179 { BFD_RELOC_8, R_MN10200_8, },
180 { BFD_RELOC_24, R_MN10200_24, },
181 { BFD_RELOC_8_PCREL, R_MN10200_PCREL8, },
182 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
183 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
186 static reloc_howto_type *
187 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
189 bfd_reloc_code_real_type code;
194 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
197 if (mn10200_reloc_map[i].bfd_reloc_val == code)
198 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
204 /* Set the howto pointer for an MN10200 ELF reloc. */
207 mn10200_info_to_howto (abfd, cache_ptr, dst)
210 Elf32_Internal_Rela *dst;
214 r_type = ELF32_R_TYPE (dst->r_info);
215 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
216 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
219 /* Perform a relocation as part of a final link. */
220 static bfd_reloc_status_type
221 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
222 input_section, contents, offset, value,
223 addend, info, sym_sec, is_local)
224 reloc_howto_type *howto;
227 asection *input_section;
232 struct bfd_link_info *info;
236 unsigned long r_type = howto->type;
237 bfd_byte *hit_data = contents + offset;
246 value += bfd_get_32 (input_bfd, hit_data);
248 bfd_put_32 (input_bfd, value, hit_data);
252 value += (short)bfd_get_16 (input_bfd, hit_data);
255 if ((long)value > 0x7fff || (long)value < -0x8000)
256 return bfd_reloc_overflow;
258 bfd_put_16 (input_bfd, value, hit_data);
262 value += (char)bfd_get_8 (input_bfd, hit_data);
265 if ((long)value > 0x7fff || (long)value < -0x8000)
266 return bfd_reloc_overflow;
268 bfd_put_8 (input_bfd, value, hit_data);
272 value += (bfd_get_32 (input_bfd, hit_data) & 0xffffff);
275 if ((long)value > 0x7fffff || (long)value < -0x800000)
276 return bfd_reloc_overflow;
279 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
280 bfd_put_32 (input_bfd, value, hit_data);
283 case R_MN10200_PCREL8:
284 value -= (input_section->output_section->vma
285 + input_section->output_offset);
289 if ((long)value > 0xff || (long)value < -0x100)
290 return bfd_reloc_overflow;
292 bfd_put_8 (input_bfd, value, hit_data);
295 case R_MN10200_PCREL16:
296 value -= (input_section->output_section->vma
297 + input_section->output_offset);
301 if ((long)value > 0xffff || (long)value < -0x10000)
302 return bfd_reloc_overflow;
304 bfd_put_16 (input_bfd, value, hit_data);
307 case R_MN10200_PCREL24:
308 value -= (input_section->output_section->vma
309 + input_section->output_offset);
313 if ((long)value > 0xffffff || (long)value < -0x1000000)
314 return bfd_reloc_overflow;
317 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
318 bfd_put_32 (input_bfd, value, hit_data);
322 return bfd_reloc_notsupported;
327 /* Relocate an MN10200 ELF section. */
329 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
330 contents, relocs, local_syms, local_sections)
332 struct bfd_link_info *info;
334 asection *input_section;
336 Elf_Internal_Rela *relocs;
337 Elf_Internal_Sym *local_syms;
338 asection **local_sections;
340 Elf_Internal_Shdr *symtab_hdr;
341 struct elf_link_hash_entry **sym_hashes;
342 Elf_Internal_Rela *rel, *relend;
344 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
345 sym_hashes = elf_sym_hashes (input_bfd);
348 relend = relocs + input_section->reloc_count;
349 for (; rel < relend; rel++)
352 reloc_howto_type *howto;
353 unsigned long r_symndx;
354 Elf_Internal_Sym *sym;
356 struct elf_link_hash_entry *h;
358 bfd_reloc_status_type r;
360 r_symndx = ELF32_R_SYM (rel->r_info);
361 r_type = ELF32_R_TYPE (rel->r_info);
362 howto = elf_mn10200_howto_table + r_type;
364 if (info->relocateable)
366 /* This is a relocateable link. We don't have to change
367 anything, unless the reloc is against a section symbol,
368 in which case we have to adjust according to where the
369 section symbol winds up in the output section. */
370 if (r_symndx < symtab_hdr->sh_info)
372 sym = local_syms + r_symndx;
373 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
375 sec = local_sections[r_symndx];
376 rel->r_addend += sec->output_offset + sym->st_value;
383 /* This is a final link. */
387 if (r_symndx < symtab_hdr->sh_info)
389 sym = local_syms + r_symndx;
390 sec = local_sections[r_symndx];
391 relocation = (sec->output_section->vma
397 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
398 while (h->root.type == bfd_link_hash_indirect
399 || h->root.type == bfd_link_hash_warning)
400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
401 if (h->root.type == bfd_link_hash_defined
402 || h->root.type == bfd_link_hash_defweak)
404 sec = h->root.u.def.section;
405 relocation = (h->root.u.def.value
406 + sec->output_section->vma
407 + sec->output_offset);
409 else if (h->root.type == bfd_link_hash_undefweak)
413 if (! ((*info->callbacks->undefined_symbol)
414 (info, h->root.root.string, input_bfd,
415 input_section, rel->r_offset)))
421 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
423 contents, rel->r_offset,
424 relocation, rel->r_addend,
425 info, sec, h == NULL);
427 if (r != bfd_reloc_ok)
430 const char *msg = (const char *)0;
433 name = h->root.root.string;
436 name = (bfd_elf_string_from_elf_section
437 (input_bfd, symtab_hdr->sh_link, sym->st_name));
438 if (name == NULL || *name == '\0')
439 name = bfd_section_name (input_bfd, sec);
444 case bfd_reloc_overflow:
445 if (! ((*info->callbacks->reloc_overflow)
446 (info, name, howto->name, (bfd_vma) 0,
447 input_bfd, input_section, rel->r_offset)))
451 case bfd_reloc_undefined:
452 if (! ((*info->callbacks->undefined_symbol)
453 (info, name, input_bfd, input_section,
458 case bfd_reloc_outofrange:
459 msg = "internal error: out of range error";
462 case bfd_reloc_notsupported:
463 msg = "internal error: unsupported relocation error";
466 case bfd_reloc_dangerous:
467 msg = "internal error: dangerous error";
471 msg = "internal error: unknown error";
475 if (!((*info->callbacks->warning)
476 (info, msg, name, input_bfd, input_section,
487 /* This function handles relaxing for the mn10200.
489 There's quite a few relaxing opportunites available on the mn10200:
491 * jsr:24 -> jsr:16 2 bytes
493 * jmp:24 -> jmp:16 2 bytes
494 - jmp:16 -> bra:8 1 byte
496 - If the previous instruction is a conditional branch
497 around the jump/bra, we may be able to reverse its condition
498 and change its target to the jump's target. The jump/bra
499 can then be deleted. 1 byte
501 - mov abs24 -> mov abs16 2 byte savings
503 - Most instructions which accept imm24 can relax to imm16 2 bytes
504 - Most instructions which accept imm16 can relax to imm8 1 byte
506 - Most instructions which accept d24 can relax to d16 2 bytes
507 - Most instructions which accept d16 can relax to d8 1 byte
510 A '*' indicates a case this code can handle. */
514 mn10200_elf_relax_section (abfd, sec, link_info, again)
517 struct bfd_link_info *link_info;
520 Elf_Internal_Shdr *symtab_hdr;
521 Elf_Internal_Rela *internal_relocs;
522 Elf_Internal_Rela *free_relocs = NULL;
523 Elf_Internal_Rela *irel, *irelend;
524 bfd_byte *contents = NULL;
525 bfd_byte *free_contents = NULL;
526 Elf32_External_Sym *extsyms = NULL;
527 Elf32_External_Sym *free_extsyms = NULL;
529 /* Assume nothing changes. */
532 /* We don't have to do anything for a relocateable link, if
533 this section does not have relocs, or if this is not a
535 if (link_info->relocateable
536 || (sec->flags & SEC_RELOC) == 0
537 || sec->reloc_count == 0
538 || (sec->flags & SEC_CODE) == 0)
541 /* If this is the first time we have been called for this section,
542 initialize the cooked size. */
543 if (sec->_cooked_size == 0)
544 sec->_cooked_size = sec->_raw_size;
546 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
548 /* Get a copy of the native relocations. */
549 internal_relocs = (_bfd_elf32_link_read_relocs
550 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
551 link_info->keep_memory));
552 if (internal_relocs == NULL)
554 if (! link_info->keep_memory)
555 free_relocs = internal_relocs;
557 /* Walk through them looking for relaxing opportunities. */
558 irelend = internal_relocs + sec->reloc_count;
559 for (irel = internal_relocs; irel < irelend; irel++)
563 /* If this isn't something that can be relaxed, then ignore
565 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
566 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
567 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
570 /* Get the section contents if we haven't done so already. */
571 if (contents == NULL)
573 /* Get cached copy if it exists. */
574 if (elf_section_data (sec)->this_hdr.contents != NULL)
575 contents = elf_section_data (sec)->this_hdr.contents;
578 /* Go get them off disk. */
579 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
580 if (contents == NULL)
582 free_contents = contents;
584 if (! bfd_get_section_contents (abfd, sec, contents,
585 (file_ptr) 0, sec->_raw_size))
590 /* Read the local symbols if we haven't done so already. */
593 /* Get cached copy if it exists. */
594 if (symtab_hdr->contents != NULL)
595 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
598 /* Go get them off disk. */
599 extsyms = ((Elf32_External_Sym *)
600 bfd_malloc (symtab_hdr->sh_info
601 * sizeof (Elf32_External_Sym)));
604 free_extsyms = extsyms;
605 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
606 || (bfd_read (extsyms, sizeof (Elf32_External_Sym),
607 symtab_hdr->sh_info, abfd)
608 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
613 /* Get the value of the symbol referred to by the reloc. */
614 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
616 Elf_Internal_Sym isym;
618 /* A local symbol. */
619 bfd_elf32_swap_symbol_in (abfd,
620 extsyms + ELF32_R_SYM (irel->r_info),
624 if (isym.st_shndx != _bfd_elf_section_from_bfd_section (abfd, sec))
626 ((*_bfd_error_handler)
627 ("%s: 0x%lx: warning: symbol in unexpected section",
628 bfd_get_filename (abfd), (unsigned long) 0));
633 symval = (isym.st_value
634 + sec->output_section->vma
635 + sec->output_offset);
640 struct elf_link_hash_entry *h;
642 /* An external symbol. */
643 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
644 h = elf_sym_hashes (abfd)[indx];
645 BFD_ASSERT (h != NULL);
646 if (h->root.type != bfd_link_hash_defined
647 && h->root.type != bfd_link_hash_defweak)
649 /* This appears to be a reference to an undefined
650 symbol. Just ignore it--it will be caught by the
651 regular reloc processing. */
655 symval = (h->root.u.def.value
656 + h->root.u.def.section->output_section->vma
657 + h->root.u.def.section->output_offset);
660 /* For simplicity of coding, we are going to modify the section
661 contents, the section relocs, and the BFD symbol table. We
662 must tell the rest of the code not to free up this
663 information. It would be possible to instead create a table
664 of changes which have to be made, as is done in coff-mips.c;
665 that would be more work, but would require less memory when
666 the linker is run. */
669 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
671 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
673 bfd_vma value = symval;
675 /* Deal with pc-relative gunk. */
676 value -= (sec->output_section->vma + sec->output_offset);
677 value -= irel->r_offset;
678 value += irel->r_addend;
680 /* See if the value will fit in 16 bits, note the high value is
681 0x7fff + 2 as the target will be two bytes closer if we are
683 if ((long)value < 0x8001 && (long)value > -0x8000)
687 /* Get the opcode. */
688 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
690 if (code != 0xe0 && code != 0xe1)
693 /* Note that we've changed the relocs, section contents, etc. */
694 elf_section_data (sec)->relocs = internal_relocs;
697 elf_section_data (sec)->this_hdr.contents = contents;
698 free_contents = NULL;
700 symtab_hdr->contents = (bfd_byte *) extsyms;
703 /* Fix the opcode. */
705 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
706 else if (code == 0xe1)
707 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
709 /* Fix the relocation's type. */
710 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
713 /* The opcode got shorter too, so we have to fix the
714 addend and offset too! */
718 /* Delete two bytes of data. */
719 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
720 irel->r_offset + 1, 2))
723 /* That will change things, so, we should relax again.
724 Note that this is not required, and it may be slow. */
729 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
731 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
733 bfd_vma value = symval;
735 /* Deal with pc-relative gunk. */
736 value -= (sec->output_section->vma + sec->output_offset);
737 value -= irel->r_offset;
738 value += irel->r_addend;
740 /* See if the value will fit in 8 bits, note the high value is
741 0x7f + 1 as the target will be one bytes closer if we are
743 if ((long)value < 0x80 && (long)value > -0x80)
747 /* Get the opcode. */
748 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
753 /* Note that we've changed the relocs, section contents, etc. */
754 elf_section_data (sec)->relocs = internal_relocs;
757 elf_section_data (sec)->this_hdr.contents = contents;
758 free_contents = NULL;
760 symtab_hdr->contents = (bfd_byte *) extsyms;
763 /* Fix the opcode. */
764 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
766 /* Fix the relocation's type. */
767 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
770 /* Delete one byte of data. */
771 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
772 irel->r_offset + 1, 1))
775 /* That will change things, so, we should relax again.
776 Note that this is not required, and it may be slow. */
781 /* Try to eliminate an unconditional 8 bit pc-relative branch
782 which immediately follows a conditional 8 bit pc-relative
783 branch around the unconditional branch.
791 This happens when the bCC can't reach lab2 at assembly time,
792 but due to other relaxations it can reach at link time. */
793 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
795 Elf_Internal_Rela *nrel;
796 bfd_vma value = symval;
799 /* Deal with pc-relative gunk. */
800 value -= (sec->output_section->vma + sec->output_offset);
801 value -= irel->r_offset;
802 value += irel->r_addend;
804 /* Do nothing if this reloc is the last byte in the section. */
805 if (irel->r_offset == sec->_cooked_size)
808 /* See if the next instruction is an unconditional pc-relative
809 branch, more often than not this test will fail, so we
810 test it first to speed things up. */
811 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
815 /* Also make sure the next relocation applies to the next
816 instruction and that it's a pc-relative 8 bit branch. */
819 || irel->r_offset + 2 != nrel->r_offset
820 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
823 /* Make sure our destination immediately follows the
824 unconditional branch. */
825 if (symval != (sec->output_section->vma + sec->output_offset
826 + irel->r_offset + 3))
829 /* Now make sure we are a conditional branch. This may not
830 be necessary, but why take the chance.
832 Note these checks assume that R_MN10200_PCREL8 relocs
833 only occur on bCC and bCCx insns. If they occured
834 elsewhere, we'd need to know the start of this insn
835 for this check to be accurate. */
836 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
837 if (code != 0xe0 && code != 0xe1 && code != 0xe2
838 && code != 0xe3 && code != 0xe4 && code != 0xe5
839 && code != 0xe6 && code != 0xe7 && code != 0xe8
840 && code != 0xe9 && code != 0xec && code != 0xed
841 && code != 0xee && code != 0xef && code != 0xfc
842 && code != 0xfd && code != 0xfe && code != 0xff)
845 /* We also have to be sure there is no symbol/label
846 at the unconditional branch. */
847 if (mn10200_elf_symbol_address_p (abfd, sec, extsyms,
851 /* Note that we've changed the relocs, section contents, etc. */
852 elf_section_data (sec)->relocs = internal_relocs;
855 elf_section_data (sec)->this_hdr.contents = contents;
856 free_contents = NULL;
858 symtab_hdr->contents = (bfd_byte *) extsyms;
861 /* Reverse the condition of the first branch. */
919 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
921 /* Set the reloc type and symbol for the first branch
922 from the second branch. */
923 irel->r_info = nrel->r_info;
925 /* Make the reloc for the second branch a null reloc. */
926 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
929 /* Delete two bytes of data. */
930 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
931 irel->r_offset + 1, 2))
935 /* That will change things, so, we should relax again.
936 Note that this is not required, and it may be slow. */
942 if (free_relocs != NULL)
948 if (free_contents != NULL)
950 if (! link_info->keep_memory)
951 free (free_contents);
954 /* Cache the section contents for elf_link_input_bfd. */
955 elf_section_data (sec)->this_hdr.contents = contents;
957 free_contents = NULL;
960 if (free_extsyms != NULL)
962 if (! link_info->keep_memory)
966 /* Cache the symbols for elf_link_input_bfd. */
967 symtab_hdr->contents = extsyms;
975 if (free_relocs != NULL)
977 if (free_contents != NULL)
978 free (free_contents);
979 if (free_extsyms != NULL)
984 /* Delete some bytes from a section while relaxing. */
987 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
993 Elf_Internal_Shdr *symtab_hdr;
994 Elf32_External_Sym *extsyms;
997 Elf_Internal_Rela *irel, *irelend;
998 Elf_Internal_Rela *irelalign;
1000 Elf32_External_Sym *esym, *esymend;
1001 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1003 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1004 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1006 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1008 contents = elf_section_data (sec)->this_hdr.contents;
1010 /* The deletion must stop at the next ALIGN reloc for an aligment
1011 power larger than the number of bytes we are deleting. */
1014 toaddr = sec->_cooked_size;
1016 irel = elf_section_data (sec)->relocs;
1017 irelend = irel + sec->reloc_count;
1019 /* Actually delete the bytes. */
1020 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
1021 sec->_cooked_size -= count;
1023 /* Adjust all the relocs. */
1024 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1026 /* Get the new reloc address. */
1027 if ((irel->r_offset > addr
1028 && irel->r_offset < toaddr))
1029 irel->r_offset -= count;
1032 /* Adjust all the symbols. */
1034 esymend = esym + symtab_hdr->sh_info;
1035 for (; esym < esymend; esym++)
1037 Elf_Internal_Sym isym;
1039 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1041 if (isym.st_shndx == shndx
1042 && isym.st_value > addr
1043 && isym.st_value < toaddr)
1045 isym.st_value -= count;
1046 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
1050 sym_hash = elf_sym_hashes (abfd);
1051 sym_hash_end = (sym_hash
1052 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1053 - symtab_hdr->sh_info));
1054 for (; sym_hash < sym_hash_end; sym_hash++)
1056 if (((*sym_hash)->root.type == bfd_link_hash_defined
1057 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1058 && (*sym_hash)->root.u.def.section == sec
1059 && (*sym_hash)->root.u.def.value > addr
1060 && (*sym_hash)->root.u.def.value < toaddr)
1062 (*sym_hash)->root.u.def.value -= count;
1069 /* Return true if a symbol exists at the given address, else return
1072 mn10200_elf_symbol_address_p (abfd, sec, extsyms, addr)
1075 Elf32_External_Sym *extsyms;
1078 Elf_Internal_Shdr *symtab_hdr;
1080 Elf32_External_Sym *esym, *esymend;
1081 struct elf_link_hash_entry **sym_hash, **sym_hash_end;
1083 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1084 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1086 /* Examine all the symbols. */
1088 esymend = esym + symtab_hdr->sh_info;
1089 for (; esym < esymend; esym++)
1091 Elf_Internal_Sym isym;
1093 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
1095 if (isym.st_shndx == shndx
1096 && isym.st_value == addr)
1100 sym_hash = elf_sym_hashes (abfd);
1101 sym_hash_end = (sym_hash
1102 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1103 - symtab_hdr->sh_info));
1104 for (; sym_hash < sym_hash_end; sym_hash++)
1106 if (((*sym_hash)->root.type == bfd_link_hash_defined
1107 || (*sym_hash)->root.type == bfd_link_hash_defweak)
1108 && (*sym_hash)->root.u.def.section == sec
1109 && (*sym_hash)->root.u.def.value == addr)
1115 /* This is a version of bfd_generic_get_relocated_section_contents
1116 which uses mn10200_elf_relocate_section. */
1119 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1120 data, relocateable, symbols)
1122 struct bfd_link_info *link_info;
1123 struct bfd_link_order *link_order;
1125 boolean relocateable;
1128 Elf_Internal_Shdr *symtab_hdr;
1129 asection *input_section = link_order->u.indirect.section;
1130 bfd *input_bfd = input_section->owner;
1131 asection **sections = NULL;
1132 Elf_Internal_Rela *internal_relocs = NULL;
1133 Elf32_External_Sym *external_syms = NULL;
1134 Elf_Internal_Sym *internal_syms = NULL;
1136 /* We only need to handle the case of relaxing, or of having a
1137 particular set of section contents, specially. */
1139 || elf_section_data (input_section)->this_hdr.contents == NULL)
1140 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1145 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1147 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1148 input_section->_raw_size);
1150 if ((input_section->flags & SEC_RELOC) != 0
1151 && input_section->reloc_count > 0)
1153 Elf_Internal_Sym *isymp;
1155 Elf32_External_Sym *esym, *esymend;
1157 if (symtab_hdr->contents != NULL)
1158 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1161 external_syms = ((Elf32_External_Sym *)
1162 bfd_malloc (symtab_hdr->sh_info
1163 * sizeof (Elf32_External_Sym)));
1164 if (external_syms == NULL && symtab_hdr->sh_info > 0)
1166 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1167 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
1168 symtab_hdr->sh_info, input_bfd)
1169 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
1173 internal_relocs = (_bfd_elf32_link_read_relocs
1174 (input_bfd, input_section, (PTR) NULL,
1175 (Elf_Internal_Rela *) NULL, false));
1176 if (internal_relocs == NULL)
1179 internal_syms = ((Elf_Internal_Sym *)
1180 bfd_malloc (symtab_hdr->sh_info
1181 * sizeof (Elf_Internal_Sym)));
1182 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
1185 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
1186 * sizeof (asection *));
1187 if (sections == NULL && symtab_hdr->sh_info > 0)
1190 isymp = internal_syms;
1192 esym = external_syms;
1193 esymend = esym + symtab_hdr->sh_info;
1194 for (; esym < esymend; ++esym, ++isymp, ++secpp)
1198 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
1200 if (isymp->st_shndx == SHN_UNDEF)
1201 isec = bfd_und_section_ptr;
1202 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
1203 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1204 else if (isymp->st_shndx == SHN_ABS)
1205 isec = bfd_abs_section_ptr;
1206 else if (isymp->st_shndx == SHN_COMMON)
1207 isec = bfd_com_section_ptr;
1217 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1218 input_section, data, internal_relocs,
1219 internal_syms, sections))
1222 if (sections != NULL)
1225 if (internal_syms != NULL)
1226 free (internal_syms);
1227 internal_syms = NULL;
1228 if (external_syms != NULL && symtab_hdr->contents == NULL)
1229 free (external_syms);
1230 external_syms = NULL;
1231 if (internal_relocs != elf_section_data (input_section)->relocs)
1232 free (internal_relocs);
1233 internal_relocs = NULL;
1239 if (internal_relocs != NULL
1240 && internal_relocs != elf_section_data (input_section)->relocs)
1241 free (internal_relocs);
1242 if (external_syms != NULL && symtab_hdr->contents == NULL)
1243 free (external_syms);
1244 if (internal_syms != NULL)
1245 free (internal_syms);
1246 if (sections != NULL)
1252 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1253 #define TARGET_LITTLE_NAME "elf32-mn10200"
1254 #define ELF_ARCH bfd_arch_mn10200
1255 #define ELF_MACHINE_CODE EM_CYGNUS_MN10200
1256 #define ELF_MAXPAGESIZE 0x1000
1258 #define elf_info_to_howto mn10200_info_to_howto
1259 #define elf_info_to_howto_rel 0
1260 #define elf_backend_relocate_section mn10200_elf_relocate_section
1261 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1262 #define bfd_elf32_bfd_get_relocated_section_contents \
1263 mn10200_elf_get_relocated_section_contents
1265 #define elf_symbol_leading_char '_'
1267 #include "elf32-target.h"