1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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. */
24 #include "elf/mn10300.h"
26 struct elf32_mn10300_link_hash_entry
28 /* The basic elf link hash table entry. */
29 struct elf_link_hash_entry root;
31 /* For function symbols, the number of times this function is
32 called directly (ie by name). */
33 unsigned int direct_calls;
35 /* For function symbols, the size of this function's stack
36 (if <= 255 bytes). We stuff this into "call" instructions
37 to this target when it's valid and profitable to do so.
39 This does not include stack allocated by movm! */
40 unsigned char stack_size;
42 /* For function symbols, arguments (if any) for movm instruction
43 in the prologue. We stuff this value into "call" instructions
44 to the target when it's valid and profitable to do so. */
45 unsigned char movm_args;
47 /* For funtion symbols, the amount of stack space that would be allocated
48 by the movm instruction. This is redundant with movm_args, but we
49 add it to the hash table to avoid computing it over and over. */
50 unsigned char movm_stack_size;
52 /* When set, convert all "call" instructions to this target into "calls"
54 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
56 /* Used to mark functions which have had redundant parts of their
58 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
62 /* We derive a hash table from the main elf linker hash table so
63 we can store state variables and a secondary hash table without
64 resorting to global variables. */
65 struct elf32_mn10300_link_hash_table
67 /* The main hash table. */
68 struct elf_link_hash_table root;
70 /* A hash table for static functions. We could derive a new hash table
71 instead of using the full elf32_mn10300_link_hash_table if we wanted
72 to save some memory. */
73 struct elf32_mn10300_link_hash_table *static_hash_table;
75 /* Random linker state flags. */
76 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
80 /* For MN10300 linker hash table. */
82 /* Get the MN10300 ELF linker hash table from a link_info structure. */
84 #define elf32_mn10300_hash_table(p) \
85 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
87 #define elf32_mn10300_link_hash_traverse(table, func, info) \
88 (elf_link_hash_traverse \
90 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
93 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
94 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
95 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
98 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
99 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
100 static void mn10300_info_to_howto
101 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
102 static boolean mn10300_elf_check_relocs
103 PARAMS ((bfd *, struct bfd_link_info *, asection *,
104 const Elf_Internal_Rela *));
105 static asection *mn10300_elf_gc_mark_hook
106 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
107 struct elf_link_hash_entry *, Elf_Internal_Sym *));
108 static boolean mn10300_elf_relax_delete_bytes
109 PARAMS ((bfd *, asection *, bfd_vma, int));
110 static boolean mn10300_elf_symbol_address_p
111 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
112 static boolean elf32_mn10300_finish_hash_table_entry
113 PARAMS ((struct bfd_hash_entry *, PTR));
114 static void compute_function_info
115 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
116 bfd_vma, unsigned char *));
118 /* We have to use RELA instructions since md_apply_fix3 in the assembler
119 does absolutely nothing. */
122 static reloc_howto_type elf_mn10300_howto_table[] =
124 /* Dummy relocation. Does nothing. */
125 HOWTO (R_MN10300_NONE,
131 complain_overflow_bitfield,
132 bfd_elf_generic_reloc,
138 /* Standard 32 bit reloc. */
145 complain_overflow_bitfield,
146 bfd_elf_generic_reloc,
152 /* Standard 16 bit reloc. */
159 complain_overflow_bitfield,
160 bfd_elf_generic_reloc,
166 /* Standard 8 bit reloc. */
173 complain_overflow_bitfield,
174 bfd_elf_generic_reloc,
180 /* Standard 32bit pc-relative reloc. */
181 HOWTO (R_MN10300_PCREL32,
187 complain_overflow_bitfield,
188 bfd_elf_generic_reloc,
194 /* Standard 16bit pc-relative reloc. */
195 HOWTO (R_MN10300_PCREL16,
201 complain_overflow_bitfield,
202 bfd_elf_generic_reloc,
208 /* Standard 8 pc-relative reloc. */
209 HOWTO (R_MN10300_PCREL8,
215 complain_overflow_bitfield,
216 bfd_elf_generic_reloc,
223 /* GNU extension to record C++ vtable hierarchy */
224 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
226 0, /* size (0 = byte, 1 = short, 2 = long) */
228 false, /* pc_relative */
230 complain_overflow_dont, /* complain_on_overflow */
231 NULL, /* special_function */
232 "R_MN10300_GNU_VTINHERIT", /* name */
233 false, /* partial_inplace */
236 false), /* pcrel_offset */
238 /* GNU extension to record C++ vtable member usage */
239 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
241 0, /* size (0 = byte, 1 = short, 2 = long) */
243 false, /* pc_relative */
245 complain_overflow_dont, /* complain_on_overflow */
246 NULL, /* special_function */
247 "R_MN10300_GNU_VTENTRY", /* name */
248 false, /* partial_inplace */
251 false), /* pcrel_offset */
253 /* Standard 24 bit reloc. */
260 complain_overflow_bitfield,
261 bfd_elf_generic_reloc,
270 struct mn10300_reloc_map
272 bfd_reloc_code_real_type bfd_reloc_val;
273 unsigned char elf_reloc_val;
276 static const struct mn10300_reloc_map mn10300_reloc_map[] =
278 { BFD_RELOC_NONE, R_MN10300_NONE, },
279 { BFD_RELOC_32, R_MN10300_32, },
280 { BFD_RELOC_16, R_MN10300_16, },
281 { BFD_RELOC_8, R_MN10300_8, },
282 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
283 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
284 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
285 { BFD_RELOC_24, R_MN10300_24, },
286 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
287 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
290 static reloc_howto_type *
291 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
292 bfd *abfd ATTRIBUTE_UNUSED;
293 bfd_reloc_code_real_type code;
298 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
301 if (mn10300_reloc_map[i].bfd_reloc_val == code)
302 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
308 /* Set the howto pointer for an MN10300 ELF reloc. */
311 mn10300_info_to_howto (abfd, cache_ptr, dst)
312 bfd *abfd ATTRIBUTE_UNUSED;
314 Elf32_Internal_Rela *dst;
318 r_type = ELF32_R_TYPE (dst->r_info);
319 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
320 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
323 /* Look through the relocs for a section during the first phase.
324 Since we don't do .gots or .plts, we just need to consider the
325 virtual table relocs for gc. */
328 mn10300_elf_check_relocs (abfd, info, sec, relocs)
330 struct bfd_link_info *info;
332 const Elf_Internal_Rela *relocs;
334 Elf_Internal_Shdr *symtab_hdr;
335 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
336 const Elf_Internal_Rela *rel;
337 const Elf_Internal_Rela *rel_end;
339 if (info->relocateable)
342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
343 sym_hashes = elf_sym_hashes (abfd);
344 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
345 if (!elf_bad_symtab (abfd))
346 sym_hashes_end -= symtab_hdr->sh_info;
348 rel_end = relocs + sec->reloc_count;
349 for (rel = relocs; rel < rel_end; rel++)
351 struct elf_link_hash_entry *h;
352 unsigned long r_symndx;
354 r_symndx = ELF32_R_SYM (rel->r_info);
355 if (r_symndx < symtab_hdr->sh_info)
358 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
360 switch (ELF32_R_TYPE (rel->r_info))
362 /* This relocation describes the C++ object vtable hierarchy.
363 Reconstruct it for later use during GC. */
364 case R_MN10300_GNU_VTINHERIT:
365 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
369 /* This relocation describes which C++ vtable entries are actually
370 used. Record for later use during GC. */
371 case R_MN10300_GNU_VTENTRY:
372 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
381 /* Return the section that should be marked against GC for a given
385 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
387 struct bfd_link_info *info ATTRIBUTE_UNUSED;
388 Elf_Internal_Rela *rel;
389 struct elf_link_hash_entry *h;
390 Elf_Internal_Sym *sym;
394 switch (ELF32_R_TYPE (rel->r_info))
396 case R_MN10300_GNU_VTINHERIT:
397 case R_MN10300_GNU_VTENTRY:
401 switch (h->root.type)
403 case bfd_link_hash_defined:
404 case bfd_link_hash_defweak:
405 return h->root.u.def.section;
407 case bfd_link_hash_common:
408 return h->root.u.c.p->section;
417 if (!(elf_bad_symtab (abfd)
418 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
419 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
420 && sym->st_shndx != SHN_COMMON))
422 return bfd_section_from_elf_index (abfd, sym->st_shndx);
429 /* Perform a relocation as part of a final link. */
430 static bfd_reloc_status_type
431 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
432 input_section, contents, offset, value,
433 addend, info, sym_sec, is_local)
434 reloc_howto_type *howto;
436 bfd *output_bfd ATTRIBUTE_UNUSED;
437 asection *input_section;
442 struct bfd_link_info *info ATTRIBUTE_UNUSED;
443 asection *sym_sec ATTRIBUTE_UNUSED;
444 int is_local ATTRIBUTE_UNUSED;
446 unsigned long r_type = howto->type;
447 bfd_byte *hit_data = contents + offset;
456 bfd_put_32 (input_bfd, value, hit_data);
462 if ((long)value > 0x7fffff || (long)value < -0x800000)
463 return bfd_reloc_overflow;
465 bfd_put_8 (input_bfd, value & 0xff, hit_data);
466 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
467 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
473 if ((long)value > 0x7fff || (long)value < -0x8000)
474 return bfd_reloc_overflow;
476 bfd_put_16 (input_bfd, value, hit_data);
482 if ((long)value > 0x7f || (long)value < -0x80)
483 return bfd_reloc_overflow;
485 bfd_put_8 (input_bfd, value, hit_data);
488 case R_MN10300_PCREL8:
489 value -= (input_section->output_section->vma
490 + input_section->output_offset);
494 if ((long)value > 0xff || (long)value < -0x100)
495 return bfd_reloc_overflow;
497 bfd_put_8 (input_bfd, value, hit_data);
500 case R_MN10300_PCREL16:
501 value -= (input_section->output_section->vma
502 + input_section->output_offset);
506 if ((long)value > 0xffff || (long)value < -0x10000)
507 return bfd_reloc_overflow;
509 bfd_put_16 (input_bfd, value, hit_data);
512 case R_MN10300_PCREL32:
513 value -= (input_section->output_section->vma
514 + input_section->output_offset);
518 bfd_put_32 (input_bfd, value, hit_data);
521 case R_MN10300_GNU_VTINHERIT:
522 case R_MN10300_GNU_VTENTRY:
526 return bfd_reloc_notsupported;
531 /* Relocate an MN10300 ELF section. */
533 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
534 contents, relocs, local_syms, local_sections)
536 struct bfd_link_info *info;
538 asection *input_section;
540 Elf_Internal_Rela *relocs;
541 Elf_Internal_Sym *local_syms;
542 asection **local_sections;
544 Elf_Internal_Shdr *symtab_hdr;
545 struct elf32_mn10300_link_hash_entry **sym_hashes;
546 Elf_Internal_Rela *rel, *relend;
548 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
549 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
550 (elf_sym_hashes (input_bfd));
553 relend = relocs + input_section->reloc_count;
554 for (; rel < relend; rel++)
557 reloc_howto_type *howto;
558 unsigned long r_symndx;
559 Elf_Internal_Sym *sym;
561 struct elf32_mn10300_link_hash_entry *h;
563 bfd_reloc_status_type r;
565 r_symndx = ELF32_R_SYM (rel->r_info);
566 r_type = ELF32_R_TYPE (rel->r_info);
567 howto = elf_mn10300_howto_table + r_type;
569 /* Just skip the vtable gc relocs. */
570 if (r_type == R_MN10300_GNU_VTINHERIT
571 || r_type == R_MN10300_GNU_VTENTRY)
574 if (info->relocateable)
576 /* This is a relocateable link. We don't have to change
577 anything, unless the reloc is against a section symbol,
578 in which case we have to adjust according to where the
579 section symbol winds up in the output section. */
580 if (r_symndx < symtab_hdr->sh_info)
582 sym = local_syms + r_symndx;
583 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
585 sec = local_sections[r_symndx];
586 rel->r_addend += sec->output_offset + sym->st_value;
593 /* This is a final link. */
597 if (r_symndx < symtab_hdr->sh_info)
599 sym = local_syms + r_symndx;
600 sec = local_sections[r_symndx];
601 relocation = (sec->output_section->vma
607 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
608 while (h->root.type == bfd_link_hash_indirect
609 || h->root.type == bfd_link_hash_warning)
610 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
611 if (h->root.root.type == bfd_link_hash_defined
612 || h->root.root.type == bfd_link_hash_defweak)
614 sec = h->root.root.u.def.section;
615 relocation = (h->root.root.u.def.value
616 + sec->output_section->vma
617 + sec->output_offset);
619 else if (h->root.root.type == bfd_link_hash_undefweak)
623 if (! ((*info->callbacks->undefined_symbol)
624 (info, h->root.root.root.string, input_bfd,
625 input_section, rel->r_offset, true)))
631 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
633 contents, rel->r_offset,
634 relocation, rel->r_addend,
635 info, sec, h == NULL);
637 if (r != bfd_reloc_ok)
640 const char *msg = (const char *)0;
643 name = h->root.root.root.string;
646 name = (bfd_elf_string_from_elf_section
647 (input_bfd, symtab_hdr->sh_link, sym->st_name));
648 if (name == NULL || *name == '\0')
649 name = bfd_section_name (input_bfd, sec);
654 case bfd_reloc_overflow:
655 if (! ((*info->callbacks->reloc_overflow)
656 (info, name, howto->name, (bfd_vma) 0,
657 input_bfd, input_section, rel->r_offset)))
661 case bfd_reloc_undefined:
662 if (! ((*info->callbacks->undefined_symbol)
663 (info, name, input_bfd, input_section,
664 rel->r_offset, true)))
668 case bfd_reloc_outofrange:
669 msg = _("internal error: out of range error");
672 case bfd_reloc_notsupported:
673 msg = _("internal error: unsupported relocation error");
676 case bfd_reloc_dangerous:
677 msg = _("internal error: dangerous error");
681 msg = _("internal error: unknown error");
685 if (!((*info->callbacks->warning)
686 (info, msg, name, input_bfd, input_section,
697 /* Finish initializing one hash table entry. */
699 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
700 struct bfd_hash_entry *gen_entry;
701 PTR in_args ATTRIBUTE_UNUSED;
703 struct elf32_mn10300_link_hash_entry *entry;
704 unsigned int byte_count = 0;
706 entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
708 /* If we already know we want to convert "call" to "calls" for calls
709 to this symbol, then return now. */
710 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
713 /* If there are no named calls to this symbol, or there's nothing we
714 can move from the function itself into the "call" instruction, then
715 note that all "call" instructions should be converted into "calls"
716 instructions and return. */
717 if (entry->direct_calls == 0
718 || (entry->stack_size == 0 && entry->movm_args == 0))
720 /* Make a note that we should convert "call" instructions to "calls"
721 instructions for calls to this symbol. */
722 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
726 /* We may be able to move some instructions from the function itself into
727 the "call" instruction. Count how many bytes we might be able to
728 eliminate in the function itself. */
730 /* A movm instruction is two bytes. */
731 if (entry->movm_args)
734 /* Count the insn to allocate stack space too. */
735 if (entry->stack_size > 0 && entry->stack_size <= 128)
737 else if (entry->stack_size > 0 && entry->stack_size < 256)
740 /* If using "call" will result in larger code, then turn all
741 the associated "call" instructions into "calls" instrutions. */
742 if (byte_count < entry->direct_calls)
743 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
745 /* This routine never fails. */
749 /* This function handles relaxing for the mn10300.
751 There's quite a few relaxing opportunites available on the mn10300:
753 * calls:32 -> calls:16 2 bytes
754 * call:32 -> call:16 2 bytes
756 * call:32 -> calls:32 1 byte
757 * call:16 -> calls:16 1 byte
758 * These are done anytime using "calls" would result
759 in smaller code, or when necessary to preserve the
760 meaning of the program.
764 * In some circumstances we can move instructions
765 from a function prologue into a "call" instruction.
766 This is only done if the resulting code is no larger
767 than the original code.
769 * jmp:32 -> jmp:16 2 bytes
770 * jmp:16 -> bra:8 1 byte
772 * If the previous instruction is a conditional branch
773 around the jump/bra, we may be able to reverse its condition
774 and change its target to the jump's target. The jump/bra
775 can then be deleted. 2 bytes
777 * mov abs32 -> mov abs16 1 or 2 bytes
779 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
780 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
782 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
783 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
785 We don't handle imm16->imm8 or d16->d8 as they're very rare
786 and somewhat more difficult to support. */
789 mn10300_elf_relax_section (abfd, sec, link_info, again)
792 struct bfd_link_info *link_info;
795 Elf_Internal_Shdr *symtab_hdr;
796 Elf_Internal_Rela *internal_relocs = NULL;
797 Elf_Internal_Rela *free_relocs = NULL;
798 Elf_Internal_Rela *irel, *irelend;
799 bfd_byte *contents = NULL;
800 bfd_byte *free_contents = NULL;
801 Elf32_External_Sym *extsyms = NULL;
802 Elf32_External_Sym *free_extsyms = NULL;
803 struct elf32_mn10300_link_hash_table *hash_table;
805 /* Assume nothing changes. */
808 /* We need a pointer to the mn10300 specific hash table. */
809 hash_table = elf32_mn10300_hash_table (link_info);
811 /* Initialize fields in each hash table entry the first time through. */
812 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
816 /* Iterate over all the input bfds. */
817 for (input_bfd = link_info->input_bfds;
819 input_bfd = input_bfd->link_next)
823 /* We're going to need all the symbols for each bfd. */
824 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
826 /* Get cached copy if it exists. */
827 if (symtab_hdr->contents != NULL)
828 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
831 /* Go get them off disk. */
832 extsyms = ((Elf32_External_Sym *)
833 bfd_malloc (symtab_hdr->sh_size));
836 free_extsyms = extsyms;
837 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
838 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
839 != symtab_hdr->sh_size))
843 /* Iterate over each section in this bfd. */
844 for (section = input_bfd->sections;
846 section = section->next)
848 struct elf32_mn10300_link_hash_entry *hash;
849 Elf_Internal_Sym *sym;
850 asection *sym_sec = NULL;
851 const char *sym_name;
854 /* Get cached copy of section contents if it exists. */
855 if (elf_section_data (section)->this_hdr.contents != NULL)
856 contents = elf_section_data (section)->this_hdr.contents;
857 else if (section->_raw_size != 0)
859 /* Go get them off disk. */
860 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
861 if (contents == NULL)
863 free_contents = contents;
865 if (!bfd_get_section_contents (input_bfd, section,
866 contents, (file_ptr) 0,
873 free_contents = NULL;
876 /* If there aren't any relocs, then there's nothing to do. */
877 if ((section->flags & SEC_RELOC) != 0
878 && section->reloc_count != 0)
881 /* Get a copy of the native relocations. */
882 internal_relocs = (_bfd_elf32_link_read_relocs
883 (input_bfd, section, (PTR) NULL,
884 (Elf_Internal_Rela *) NULL,
885 link_info->keep_memory));
886 if (internal_relocs == NULL)
888 if (! link_info->keep_memory)
889 free_relocs = internal_relocs;
891 /* Now examine each relocation. */
892 irel = internal_relocs;
893 irelend = irel + section->reloc_count;
894 for (; irel < irelend; irel++)
897 unsigned long r_index;
900 r_type = ELF32_R_TYPE (irel->r_info);
901 r_index = ELF32_R_SYM (irel->r_info);
903 if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
906 /* We need the name and hash table entry of the target
912 if (r_index < symtab_hdr->sh_info)
914 /* A local symbol. */
915 Elf_Internal_Sym isym;
917 bfd_elf32_swap_symbol_in (input_bfd,
918 extsyms + r_index, &isym);
920 if (isym.st_shndx == SHN_UNDEF)
921 sym_sec = bfd_und_section_ptr;
922 else if (isym.st_shndx > 0
923 && isym.st_shndx < SHN_LORESERVE)
925 = bfd_section_from_elf_index (input_bfd,
927 else if (isym.st_shndx == SHN_ABS)
928 sym_sec = bfd_abs_section_ptr;
929 else if (isym.st_shndx == SHN_COMMON)
930 sym_sec = bfd_com_section_ptr;
932 sym_name = bfd_elf_string_from_elf_section (input_bfd,
936 /* If it isn't a function, then we don't care
938 if (r_index < symtab_hdr->sh_info
939 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
942 /* Tack on an ID so we can uniquely identify this
943 local symbol in the global hash table. */
944 new_name = bfd_malloc (strlen (sym_name) + 10);
948 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
951 hash = (struct elf32_mn10300_link_hash_entry *)
952 elf_link_hash_lookup (&hash_table->static_hash_table->root,
959 r_index -= symtab_hdr->sh_info;
960 hash = (struct elf32_mn10300_link_hash_entry *)
961 elf_sym_hashes (input_bfd)[r_index];
964 /* If this is not a "call" instruction, then we
965 should convert "call" instructions to "calls"
967 code = bfd_get_8 (input_bfd,
968 contents + irel->r_offset - 1);
969 if (code != 0xdd && code != 0xcd)
970 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
972 /* If this is a jump/call, then bump the direct_calls
973 counter. Else force "call" to "calls" conversions. */
974 if (r_type == R_MN10300_PCREL32
975 || r_type == R_MN10300_PCREL16)
976 hash->direct_calls++;
978 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
982 /* Now look at the actual contents to get the stack size,
983 and a list of what registers were saved in the prologue
985 if ((section->flags & SEC_CODE) != 0)
988 Elf32_External_Sym *esym, *esymend;
991 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
994 /* Look at each function defined in this section and
995 update info for that function. */
997 esymend = esym + symtab_hdr->sh_info;
998 for (; esym < esymend; esym++)
1000 Elf_Internal_Sym isym;
1002 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1003 if (isym.st_shndx == shndx
1004 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1006 if (isym.st_shndx == SHN_UNDEF)
1007 sym_sec = bfd_und_section_ptr;
1008 else if (isym.st_shndx > 0
1009 && isym.st_shndx < SHN_LORESERVE)
1011 = bfd_section_from_elf_index (input_bfd,
1013 else if (isym.st_shndx == SHN_ABS)
1014 sym_sec = bfd_abs_section_ptr;
1015 else if (isym.st_shndx == SHN_COMMON)
1016 sym_sec = bfd_com_section_ptr;
1018 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1019 symtab_hdr->sh_link,
1022 /* Tack on an ID so we can uniquely identify this
1023 local symbol in the global hash table. */
1024 new_name = bfd_malloc (strlen (sym_name) + 10);
1028 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1029 sym_name = new_name;
1031 hash = (struct elf32_mn10300_link_hash_entry *)
1032 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1036 compute_function_info (input_bfd, hash,
1037 isym.st_value, contents);
1041 esym = extsyms + symtab_hdr->sh_info;
1042 esymend = extsyms + (symtab_hdr->sh_size
1043 / sizeof (Elf32_External_Sym));
1044 for (idx = 0; esym < esymend; esym++, idx++)
1046 Elf_Internal_Sym isym;
1048 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1049 hash = (struct elf32_mn10300_link_hash_entry *)
1050 elf_sym_hashes (input_bfd)[idx];
1051 if (isym.st_shndx == shndx
1052 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1053 && (hash)->root.root.u.def.section == section
1054 && ((hash)->root.root.type == bfd_link_hash_defined
1055 || (hash)->root.root.type == bfd_link_hash_defweak))
1056 compute_function_info (input_bfd, hash,
1057 (hash)->root.root.u.def.value,
1062 /* Cache or free any memory we allocated for the relocs. */
1063 if (free_relocs != NULL)
1069 /* Cache or free any memory we allocated for the contents. */
1070 if (free_contents != NULL)
1072 if (! link_info->keep_memory)
1073 free (free_contents);
1076 /* Cache the section contents for elf_link_input_bfd. */
1077 elf_section_data (section)->this_hdr.contents = contents;
1079 free_contents = NULL;
1083 /* Cache or free any memory we allocated for the symbols. */
1084 if (free_extsyms != NULL)
1086 if (! link_info->keep_memory)
1087 free (free_extsyms);
1090 /* Cache the symbols for elf_link_input_bfd. */
1091 symtab_hdr->contents = extsyms;
1093 free_extsyms = NULL;
1097 /* Now iterate on each symbol in the hash table and perform
1098 the final initialization steps on each. */
1099 elf32_mn10300_link_hash_traverse (hash_table,
1100 elf32_mn10300_finish_hash_table_entry,
1102 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1103 elf32_mn10300_finish_hash_table_entry,
1106 /* All entries in the hash table are fully initialized. */
1107 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1109 /* Now that everything has been initialized, go through each
1110 code section and delete any prologue insns which will be
1111 redundant because their operations will be performed by
1112 a "call" instruction. */
1113 for (input_bfd = link_info->input_bfds;
1115 input_bfd = input_bfd->link_next)
1119 /* We're going to need all the symbols for each bfd. */
1120 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1122 /* Get cached copy if it exists. */
1123 if (symtab_hdr->contents != NULL)
1124 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1127 /* Go get them off disk. */
1128 extsyms = ((Elf32_External_Sym *)
1129 bfd_malloc (symtab_hdr->sh_size));
1130 if (extsyms == NULL)
1132 free_extsyms = extsyms;
1133 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1134 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1135 != symtab_hdr->sh_size))
1139 /* Walk over each section in this bfd. */
1140 for (section = input_bfd->sections;
1142 section = section->next)
1145 Elf32_External_Sym *esym, *esymend;
1148 /* Skip non-code sections and empty sections. */
1149 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1152 if (section->reloc_count != 0)
1154 /* Get a copy of the native relocations. */
1155 internal_relocs = (_bfd_elf32_link_read_relocs
1156 (input_bfd, section, (PTR) NULL,
1157 (Elf_Internal_Rela *) NULL,
1158 link_info->keep_memory));
1159 if (internal_relocs == NULL)
1161 if (! link_info->keep_memory)
1162 free_relocs = internal_relocs;
1165 /* Get cached copy of section contents if it exists. */
1166 if (elf_section_data (section)->this_hdr.contents != NULL)
1167 contents = elf_section_data (section)->this_hdr.contents;
1170 /* Go get them off disk. */
1171 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1172 if (contents == NULL)
1174 free_contents = contents;
1176 if (!bfd_get_section_contents (input_bfd, section,
1177 contents, (file_ptr) 0,
1178 section->_raw_size))
1182 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1184 /* Now look for any function in this section which needs
1185 insns deleted from its prologue. */
1187 esymend = esym + symtab_hdr->sh_info;
1188 for (; esym < esymend; esym++)
1190 Elf_Internal_Sym isym;
1191 struct elf32_mn10300_link_hash_entry *sym_hash;
1192 asection *sym_sec = NULL;
1193 const char *sym_name;
1196 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1198 if (isym.st_shndx != shndx)
1201 if (isym.st_shndx == SHN_UNDEF)
1202 sym_sec = bfd_und_section_ptr;
1203 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1205 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1206 else if (isym.st_shndx == SHN_ABS)
1207 sym_sec = bfd_abs_section_ptr;
1208 else if (isym.st_shndx == SHN_COMMON)
1209 sym_sec = bfd_com_section_ptr;
1213 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1214 symtab_hdr->sh_link,
1217 /* Tack on an ID so we can uniquely identify this
1218 local symbol in the global hash table. */
1219 new_name = bfd_malloc (strlen (sym_name) + 10);
1222 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1223 sym_name = new_name;
1225 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1226 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1231 if (sym_hash == NULL)
1234 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1235 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1239 /* Note that we've changed things. */
1240 elf_section_data (section)->relocs = internal_relocs;
1243 elf_section_data (section)->this_hdr.contents = contents;
1244 free_contents = NULL;
1246 symtab_hdr->contents = (bfd_byte *)extsyms;
1247 free_extsyms = NULL;
1249 /* Count how many bytes we're going to delete. */
1250 if (sym_hash->movm_args)
1253 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1255 else if (sym_hash->stack_size
1256 && sym_hash->stack_size < 256)
1259 /* Note that we've deleted prologue bytes for this
1261 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1263 /* Actually delete the bytes. */
1264 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1270 /* Something changed. Not strictly necessary, but
1271 may lead to more relaxing opportunities. */
1276 /* Look for any global functions in this section which
1277 need insns deleted from their prologues. */
1278 esym = extsyms + symtab_hdr->sh_info;
1279 esymend = extsyms + (symtab_hdr->sh_size
1280 / sizeof (Elf32_External_Sym));
1281 for (idx = 0; esym < esymend; esym++, idx++)
1283 Elf_Internal_Sym isym;
1284 struct elf32_mn10300_link_hash_entry *sym_hash;
1286 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1287 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1288 (elf_sym_hashes (input_bfd)[idx]);
1289 if (isym.st_shndx == shndx
1290 && (sym_hash)->root.root.u.def.section == section
1291 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1292 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1296 /* Note that we've changed things. */
1297 elf_section_data (section)->relocs = internal_relocs;
1300 elf_section_data (section)->this_hdr.contents = contents;
1301 free_contents = NULL;
1303 symtab_hdr->contents = (bfd_byte *)extsyms;
1304 free_extsyms = NULL;
1306 /* Count how many bytes we're going to delete. */
1307 if (sym_hash->movm_args)
1310 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1312 else if (sym_hash->stack_size
1313 && sym_hash->stack_size < 256)
1316 /* Note that we've deleted prologue bytes for this
1318 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1320 /* Actually delete the bytes. */
1321 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1323 (sym_hash)->root.root.u.def.value,
1327 /* Something changed. Not strictly necessary, but
1328 may lead to more relaxing opportunities. */
1333 /* Cache or free any memory we allocated for the relocs. */
1334 if (free_relocs != NULL)
1340 /* Cache or free any memory we allocated for the contents. */
1341 if (free_contents != NULL)
1343 if (! link_info->keep_memory)
1344 free (free_contents);
1347 /* Cache the section contents for elf_link_input_bfd. */
1348 elf_section_data (section)->this_hdr.contents = contents;
1350 free_contents = NULL;
1354 /* Cache or free any memory we allocated for the symbols. */
1355 if (free_extsyms != NULL)
1357 if (! link_info->keep_memory)
1358 free (free_extsyms);
1361 /* Cache the symbols for elf_link_input_bfd. */
1362 symtab_hdr->contents = extsyms;
1364 free_extsyms = NULL;
1369 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1372 internal_relocs = NULL;
1374 free_contents = NULL;
1375 free_extsyms = NULL;
1377 /* We don't have to do anything for a relocateable link, if
1378 this section does not have relocs, or if this is not a
1380 if (link_info->relocateable
1381 || (sec->flags & SEC_RELOC) == 0
1382 || sec->reloc_count == 0
1383 || (sec->flags & SEC_CODE) == 0)
1386 /* If this is the first time we have been called for this section,
1387 initialize the cooked size. */
1388 if (sec->_cooked_size == 0)
1389 sec->_cooked_size = sec->_raw_size;
1391 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1393 /* Get a copy of the native relocations. */
1394 internal_relocs = (_bfd_elf32_link_read_relocs
1395 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1396 link_info->keep_memory));
1397 if (internal_relocs == NULL)
1399 if (! link_info->keep_memory)
1400 free_relocs = internal_relocs;
1402 /* Walk through them looking for relaxing opportunities. */
1403 irelend = internal_relocs + sec->reloc_count;
1404 for (irel = internal_relocs; irel < irelend; irel++)
1407 struct elf32_mn10300_link_hash_entry *h = NULL;
1409 /* If this isn't something that can be relaxed, then ignore
1411 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1412 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1413 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1416 /* Get the section contents if we haven't done so already. */
1417 if (contents == NULL)
1419 /* Get cached copy if it exists. */
1420 if (elf_section_data (sec)->this_hdr.contents != NULL)
1421 contents = elf_section_data (sec)->this_hdr.contents;
1424 /* Go get them off disk. */
1425 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1426 if (contents == NULL)
1428 free_contents = contents;
1430 if (! bfd_get_section_contents (abfd, sec, contents,
1431 (file_ptr) 0, sec->_raw_size))
1436 /* Read this BFD's symbols if we haven't done so already. */
1437 if (extsyms == NULL)
1439 /* Get cached copy if it exists. */
1440 if (symtab_hdr->contents != NULL)
1441 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1444 /* Go get them off disk. */
1445 extsyms = ((Elf32_External_Sym *)
1446 bfd_malloc (symtab_hdr->sh_size));
1447 if (extsyms == NULL)
1449 free_extsyms = extsyms;
1450 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1451 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1452 != symtab_hdr->sh_size))
1457 /* Get the value of the symbol referred to by the reloc. */
1458 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1460 Elf_Internal_Sym isym;
1461 asection *sym_sec = NULL;
1462 const char *sym_name;
1465 /* A local symbol. */
1466 bfd_elf32_swap_symbol_in (abfd,
1467 extsyms + ELF32_R_SYM (irel->r_info),
1470 if (isym.st_shndx == SHN_UNDEF)
1471 sym_sec = bfd_und_section_ptr;
1472 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1473 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1474 else if (isym.st_shndx == SHN_ABS)
1475 sym_sec = bfd_abs_section_ptr;
1476 else if (isym.st_shndx == SHN_COMMON)
1477 sym_sec = bfd_com_section_ptr;
1481 symval = (isym.st_value
1482 + sym_sec->output_section->vma
1483 + sym_sec->output_offset);
1484 sym_name = bfd_elf_string_from_elf_section (abfd,
1485 symtab_hdr->sh_link,
1488 /* Tack on an ID so we can uniquely identify this
1489 local symbol in the global hash table. */
1490 new_name = bfd_malloc (strlen (sym_name) + 10);
1493 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1494 sym_name = new_name;
1496 h = (struct elf32_mn10300_link_hash_entry *)
1497 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1498 sym_name, false, false, false);
1505 /* An external symbol. */
1506 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1507 h = (struct elf32_mn10300_link_hash_entry *)
1508 (elf_sym_hashes (abfd)[indx]);
1509 BFD_ASSERT (h != NULL);
1510 if (h->root.root.type != bfd_link_hash_defined
1511 && h->root.root.type != bfd_link_hash_defweak)
1513 /* This appears to be a reference to an undefined
1514 symbol. Just ignore it--it will be caught by the
1515 regular reloc processing. */
1519 symval = (h->root.root.u.def.value
1520 + h->root.root.u.def.section->output_section->vma
1521 + h->root.root.u.def.section->output_offset);
1524 /* For simplicity of coding, we are going to modify the section
1525 contents, the section relocs, and the BFD symbol table. We
1526 must tell the rest of the code not to free up this
1527 information. It would be possible to instead create a table
1528 of changes which have to be made, as is done in coff-mips.c;
1529 that would be more work, but would require less memory when
1530 the linker is run. */
1532 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1533 branch/call, also deal with "call" -> "calls" conversions and
1534 insertion of prologue data into "call" instructions. */
1535 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1537 bfd_vma value = symval;
1539 /* If we've got a "call" instruction that needs to be turned
1540 into a "calls" instruction, do so now. It saves a byte. */
1541 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1545 /* Get the opcode. */
1546 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1548 /* Make sure we're working with a "call" instruction! */
1551 /* Note that we've changed the relocs, section contents,
1553 elf_section_data (sec)->relocs = internal_relocs;
1556 elf_section_data (sec)->this_hdr.contents = contents;
1557 free_contents = NULL;
1559 symtab_hdr->contents = (bfd_byte *) extsyms;
1560 free_extsyms = NULL;
1562 /* Fix the opcode. */
1563 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1564 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1566 /* Fix irel->r_offset and irel->r_addend. */
1567 irel->r_offset += 1;
1568 irel->r_addend += 1;
1570 /* Delete one byte of data. */
1571 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1572 irel->r_offset + 3, 1))
1575 /* That will change things, so, we should relax again.
1576 Note that this is not required, and it may be slow. */
1582 /* We've got a "call" instruction which needs some data
1583 from target function filled in. */
1586 /* Get the opcode. */
1587 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1589 /* Insert data from the target function into the "call"
1590 instruction if needed. */
1593 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1594 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1595 contents + irel->r_offset + 5);
1599 /* Deal with pc-relative gunk. */
1600 value -= (sec->output_section->vma + sec->output_offset);
1601 value -= irel->r_offset;
1602 value += irel->r_addend;
1604 /* See if the value will fit in 16 bits, note the high value is
1605 0x7fff + 2 as the target will be two bytes closer if we are
1607 if ((long)value < 0x8001 && (long)value > -0x8000)
1611 /* Get the opcode. */
1612 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1614 if (code != 0xdc && code != 0xdd && code != 0xff)
1617 /* Note that we've changed the relocs, section contents, etc. */
1618 elf_section_data (sec)->relocs = internal_relocs;
1621 elf_section_data (sec)->this_hdr.contents = contents;
1622 free_contents = NULL;
1624 symtab_hdr->contents = (bfd_byte *) extsyms;
1625 free_extsyms = NULL;
1627 /* Fix the opcode. */
1629 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1630 else if (code == 0xdd)
1631 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1632 else if (code == 0xff)
1633 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1635 /* Fix the relocation's type. */
1636 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1639 /* Delete two bytes of data. */
1640 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1641 irel->r_offset + 1, 2))
1644 /* That will change things, so, we should relax again.
1645 Note that this is not required, and it may be slow. */
1650 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1652 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1654 bfd_vma value = symval;
1656 /* If we've got a "call" instruction that needs to be turned
1657 into a "calls" instruction, do so now. It saves a byte. */
1658 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1662 /* Get the opcode. */
1663 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1665 /* Make sure we're working with a "call" instruction! */
1668 /* Note that we've changed the relocs, section contents,
1670 elf_section_data (sec)->relocs = internal_relocs;
1673 elf_section_data (sec)->this_hdr.contents = contents;
1674 free_contents = NULL;
1676 symtab_hdr->contents = (bfd_byte *) extsyms;
1677 free_extsyms = NULL;
1679 /* Fix the opcode. */
1680 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1681 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1683 /* Fix irel->r_offset and irel->r_addend. */
1684 irel->r_offset += 1;
1685 irel->r_addend += 1;
1687 /* Delete one byte of data. */
1688 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1689 irel->r_offset + 1, 1))
1692 /* That will change things, so, we should relax again.
1693 Note that this is not required, and it may be slow. */
1701 /* Get the opcode. */
1702 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1704 /* Insert data from the target function into the "call"
1705 instruction if needed. */
1708 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1709 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1710 contents + irel->r_offset + 3);
1714 /* Deal with pc-relative gunk. */
1715 value -= (sec->output_section->vma + sec->output_offset);
1716 value -= irel->r_offset;
1717 value += irel->r_addend;
1719 /* See if the value will fit in 8 bits, note the high value is
1720 0x7f + 1 as the target will be one bytes closer if we are
1722 if ((long)value < 0x80 && (long)value > -0x80)
1726 /* Get the opcode. */
1727 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1732 /* Note that we've changed the relocs, section contents, etc. */
1733 elf_section_data (sec)->relocs = internal_relocs;
1736 elf_section_data (sec)->this_hdr.contents = contents;
1737 free_contents = NULL;
1739 symtab_hdr->contents = (bfd_byte *) extsyms;
1740 free_extsyms = NULL;
1742 /* Fix the opcode. */
1743 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1745 /* Fix the relocation's type. */
1746 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1749 /* Delete one byte of data. */
1750 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1751 irel->r_offset + 1, 1))
1754 /* That will change things, so, we should relax again.
1755 Note that this is not required, and it may be slow. */
1760 /* Try to eliminate an unconditional 8 bit pc-relative branch
1761 which immediately follows a conditional 8 bit pc-relative
1762 branch around the unconditional branch.
1769 This happens when the bCC can't reach lab2 at assembly time,
1770 but due to other relaxations it can reach at link time. */
1771 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1773 Elf_Internal_Rela *nrel;
1774 bfd_vma value = symval;
1777 /* Deal with pc-relative gunk. */
1778 value -= (sec->output_section->vma + sec->output_offset);
1779 value -= irel->r_offset;
1780 value += irel->r_addend;
1782 /* Do nothing if this reloc is the last byte in the section. */
1783 if (irel->r_offset == sec->_cooked_size)
1786 /* See if the next instruction is an unconditional pc-relative
1787 branch, more often than not this test will fail, so we
1788 test it first to speed things up. */
1789 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1793 /* Also make sure the next relocation applies to the next
1794 instruction and that it's a pc-relative 8 bit branch. */
1797 || irel->r_offset + 2 != nrel->r_offset
1798 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1801 /* Make sure our destination immediately follows the
1802 unconditional branch. */
1803 if (symval != (sec->output_section->vma + sec->output_offset
1804 + irel->r_offset + 3))
1807 /* Now make sure we are a conditional branch. This may not
1808 be necessary, but why take the chance.
1810 Note these checks assume that R_MN10300_PCREL8 relocs
1811 only occur on bCC and bCCx insns. If they occured
1812 elsewhere, we'd need to know the start of this insn
1813 for this check to be accurate. */
1814 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1815 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1816 && code != 0xc3 && code != 0xc4 && code != 0xc5
1817 && code != 0xc6 && code != 0xc7 && code != 0xc8
1818 && code != 0xc9 && code != 0xe8 && code != 0xe9
1819 && code != 0xea && code != 0xeb)
1822 /* We also have to be sure there is no symbol/label
1823 at the unconditional branch. */
1824 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1825 irel->r_offset + 1))
1828 /* Note that we've changed the relocs, section contents, etc. */
1829 elf_section_data (sec)->relocs = internal_relocs;
1832 elf_section_data (sec)->this_hdr.contents = contents;
1833 free_contents = NULL;
1835 symtab_hdr->contents = (bfd_byte *) extsyms;
1836 free_extsyms = NULL;
1838 /* Reverse the condition of the first branch. */
1884 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1886 /* Set the reloc type and symbol for the first branch
1887 from the second branch. */
1888 irel->r_info = nrel->r_info;
1890 /* Make the reloc for the second branch a null reloc. */
1891 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1894 /* Delete two bytes of data. */
1895 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1896 irel->r_offset + 1, 2))
1899 /* That will change things, so, we should relax again.
1900 Note that this is not required, and it may be slow. */
1904 /* Try to turn a 24 immediate, displacement or absolute address
1905 into a 8 immediate, displacement or absolute address. */
1906 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1908 bfd_vma value = symval;
1909 value += irel->r_addend;
1911 /* See if the value will fit in 8 bits. */
1912 if ((long)value < 0x7f && (long)value > -0x80)
1916 /* AM33 insns which have 24 operands are 6 bytes long and
1917 will have 0xfd as the first byte. */
1919 /* Get the first opcode. */
1920 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1924 /* Get the second opcode. */
1925 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1927 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1928 equivalent instructions exists. */
1929 if (code != 0x6b && code != 0x7b
1930 && code != 0x8b && code != 0x9b
1931 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1932 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1933 || (code & 0x0f) == 0x0e))
1935 /* Not safe if the high bit is on as relaxing may
1936 move the value out of high mem and thus not fit
1937 in a signed 8bit value. This is currently over
1939 if ((value & 0x80) == 0)
1941 /* Note that we've changed the relocation contents,
1943 elf_section_data (sec)->relocs = internal_relocs;
1946 elf_section_data (sec)->this_hdr.contents = contents;
1947 free_contents = NULL;
1949 symtab_hdr->contents = (bfd_byte *) extsyms;
1950 free_extsyms = NULL;
1952 /* Fix the opcode. */
1953 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1954 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1956 /* Fix the relocation's type. */
1958 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1961 /* Delete two bytes of data. */
1962 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1963 irel->r_offset + 1, 2))
1966 /* That will change things, so, we should relax
1967 again. Note that this is not required, and it
1978 /* Try to turn a 32bit immediate, displacement or absolute address
1979 into a 16bit immediate, displacement or absolute address. */
1980 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1982 bfd_vma value = symval;
1983 value += irel->r_addend;
1985 /* See if the value will fit in 24 bits.
1986 We allow any 16bit match here. We prune those we can't
1988 if ((long)value < 0x7fffff && (long)value > -0x800000)
1992 /* AM33 insns which have 32bit operands are 7 bytes long and
1993 will have 0xfe as the first byte. */
1995 /* Get the first opcode. */
1996 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2000 /* Get the second opcode. */
2001 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2003 /* All the am33 32 -> 24 relaxing possibilities. */
2004 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2005 equivalent instructions exists. */
2006 if (code != 0x6b && code != 0x7b
2007 && code != 0x8b && code != 0x9b
2008 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2009 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2010 || (code & 0x0f) == 0x0e))
2012 /* Not safe if the high bit is on as relaxing may
2013 move the value out of high mem and thus not fit
2014 in a signed 16bit value. This is currently over
2016 if ((value & 0x8000) == 0)
2018 /* Note that we've changed the relocation contents,
2020 elf_section_data (sec)->relocs = internal_relocs;
2023 elf_section_data (sec)->this_hdr.contents = contents;
2024 free_contents = NULL;
2026 symtab_hdr->contents = (bfd_byte *) extsyms;
2027 free_extsyms = NULL;
2029 /* Fix the opcode. */
2030 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2031 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2033 /* Fix the relocation's type. */
2035 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2038 /* Delete one byte of data. */
2039 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2040 irel->r_offset + 3, 1))
2043 /* That will change things, so, we should relax
2044 again. Note that this is not required, and it
2054 /* See if the value will fit in 16 bits.
2055 We allow any 16bit match here. We prune those we can't
2057 if ((long)value < 0x7fff && (long)value > -0x8000)
2061 /* Most insns which have 32bit operands are 6 bytes long;
2062 exceptions are pcrel insns and bit insns.
2064 We handle pcrel insns above. We don't bother trying
2065 to handle the bit insns here.
2067 The first byte of the remaining insns will be 0xfc. */
2069 /* Get the first opcode. */
2070 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2075 /* Get the second opcode. */
2076 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2078 if ((code & 0xf0) < 0x80)
2079 switch (code & 0xf0)
2081 /* mov (d32,am),dn -> mov (d32,am),dn
2082 mov dm,(d32,am) -> mov dn,(d32,am)
2083 mov (d32,am),an -> mov (d32,am),an
2084 mov dm,(d32,am) -> mov dn,(d32,am)
2085 movbu (d32,am),dn -> movbu (d32,am),dn
2086 movbu dm,(d32,am) -> movbu dn,(d32,am)
2087 movhu (d32,am),dn -> movhu (d32,am),dn
2088 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2097 /* Not safe if the high bit is on as relaxing may
2098 move the value out of high mem and thus not fit
2099 in a signed 16bit value. */
2101 && (value & 0x8000))
2104 /* Note that we've changed the relocation contents, etc. */
2105 elf_section_data (sec)->relocs = internal_relocs;
2108 elf_section_data (sec)->this_hdr.contents = contents;
2109 free_contents = NULL;
2111 symtab_hdr->contents = (bfd_byte *) extsyms;
2112 free_extsyms = NULL;
2114 /* Fix the opcode. */
2115 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2116 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2118 /* Fix the relocation's type. */
2119 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2122 /* Delete two bytes of data. */
2123 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2124 irel->r_offset + 2, 2))
2127 /* That will change things, so, we should relax again.
2128 Note that this is not required, and it may be slow. */
2132 else if ((code & 0xf0) == 0x80
2133 || (code & 0xf0) == 0x90)
2134 switch (code & 0xf3)
2136 /* mov dn,(abs32) -> mov dn,(abs16)
2137 movbu dn,(abs32) -> movbu dn,(abs16)
2138 movhu dn,(abs32) -> movhu dn,(abs16) */
2142 /* Note that we've changed the relocation contents, etc. */
2143 elf_section_data (sec)->relocs = internal_relocs;
2146 elf_section_data (sec)->this_hdr.contents = contents;
2147 free_contents = NULL;
2149 symtab_hdr->contents = (bfd_byte *) extsyms;
2150 free_extsyms = NULL;
2152 if ((code & 0xf3) == 0x81)
2153 code = 0x01 + (code & 0x0c);
2154 else if ((code & 0xf3) == 0x82)
2155 code = 0x02 + (code & 0x0c);
2156 else if ((code & 0xf3) == 0x83)
2157 code = 0x03 + (code & 0x0c);
2161 /* Fix the opcode. */
2162 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2164 /* Fix the relocation's type. */
2165 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2168 /* The opcode got shorter too, so we have to fix the
2169 addend and offset too! */
2170 irel->r_offset -= 1;
2172 /* Delete three bytes of data. */
2173 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2174 irel->r_offset + 1, 3))
2177 /* That will change things, so, we should relax again.
2178 Note that this is not required, and it may be slow. */
2182 /* mov am,(abs32) -> mov am,(abs16)
2183 mov am,(d32,sp) -> mov am,(d16,sp)
2184 mov dm,(d32,sp) -> mov dm,(d32,sp)
2185 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2186 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2192 /* Note that we've changed the relocation contents, etc. */
2193 elf_section_data (sec)->relocs = internal_relocs;
2196 elf_section_data (sec)->this_hdr.contents = contents;
2197 free_contents = NULL;
2199 symtab_hdr->contents = (bfd_byte *) extsyms;
2200 free_extsyms = NULL;
2202 /* Fix the opcode. */
2203 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2204 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2206 /* Fix the relocation's type. */
2207 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2210 /* Delete two bytes of data. */
2211 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2212 irel->r_offset + 2, 2))
2215 /* That will change things, so, we should relax again.
2216 Note that this is not required, and it may be slow. */
2220 else if ((code & 0xf0) < 0xf0)
2221 switch (code & 0xfc)
2223 /* mov imm32,dn -> mov imm16,dn
2224 mov imm32,an -> mov imm16,an
2225 mov (abs32),dn -> mov (abs16),dn
2226 movbu (abs32),dn -> movbu (abs16),dn
2227 movhu (abs32),dn -> movhu (abs16),dn */
2233 /* Not safe if the high bit is on as relaxing may
2234 move the value out of high mem and thus not fit
2235 in a signed 16bit value. */
2237 && (value & 0x8000))
2240 /* Note that we've changed the relocation contents, etc. */
2241 elf_section_data (sec)->relocs = internal_relocs;
2244 elf_section_data (sec)->this_hdr.contents = contents;
2245 free_contents = NULL;
2247 symtab_hdr->contents = (bfd_byte *) extsyms;
2248 free_extsyms = NULL;
2250 if ((code & 0xfc) == 0xcc)
2251 code = 0x2c + (code & 0x03);
2252 else if ((code & 0xfc) == 0xdc)
2253 code = 0x24 + (code & 0x03);
2254 else if ((code & 0xfc) == 0xa4)
2255 code = 0x30 + (code & 0x03);
2256 else if ((code & 0xfc) == 0xa8)
2257 code = 0x34 + (code & 0x03);
2258 else if ((code & 0xfc) == 0xac)
2259 code = 0x38 + (code & 0x03);
2263 /* Fix the opcode. */
2264 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2266 /* Fix the relocation's type. */
2267 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2270 /* The opcode got shorter too, so we have to fix the
2271 addend and offset too! */
2272 irel->r_offset -= 1;
2274 /* Delete three bytes of data. */
2275 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2276 irel->r_offset + 1, 3))
2279 /* That will change things, so, we should relax again.
2280 Note that this is not required, and it may be slow. */
2284 /* mov (abs32),an -> mov (abs16),an
2285 mov (d32,sp),an -> mov (d32,sp),an
2286 mov (d32,sp),dn -> mov (d32,sp),dn
2287 movbu (d32,sp),dn -> movbu (d32,sp),dn
2288 movhu (d32,sp),dn -> movhu (d32,sp),dn
2289 add imm32,dn -> add imm16,dn
2290 cmp imm32,dn -> cmp imm16,dn
2291 add imm32,an -> add imm16,an
2292 cmp imm32,an -> cmp imm16,an
2293 and imm32,dn -> and imm32,dn
2294 or imm32,dn -> or imm32,dn
2295 xor imm32,dn -> xor imm32,dn
2296 btst imm32,dn -> btst imm32,dn */
2312 /* Note that we've changed the relocation contents, etc. */
2313 elf_section_data (sec)->relocs = internal_relocs;
2316 elf_section_data (sec)->this_hdr.contents = contents;
2317 free_contents = NULL;
2319 symtab_hdr->contents = (bfd_byte *) extsyms;
2320 free_extsyms = NULL;
2322 /* Fix the opcode. */
2323 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2324 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2326 /* Fix the relocation's type. */
2327 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2330 /* Delete two bytes of data. */
2331 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2332 irel->r_offset + 2, 2))
2335 /* That will change things, so, we should relax again.
2336 Note that this is not required, and it may be slow. */
2340 else if (code == 0xfe)
2342 /* add imm32,sp -> add imm16,sp */
2344 /* Note that we've changed the relocation contents, etc. */
2345 elf_section_data (sec)->relocs = internal_relocs;
2348 elf_section_data (sec)->this_hdr.contents = contents;
2349 free_contents = NULL;
2351 symtab_hdr->contents = (bfd_byte *) extsyms;
2352 free_extsyms = NULL;
2354 /* Fix the opcode. */
2355 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2356 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2358 /* Fix the relocation's type. */
2359 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2362 /* Delete two bytes of data. */
2363 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2364 irel->r_offset + 2, 2))
2367 /* That will change things, so, we should relax again.
2368 Note that this is not required, and it may be slow. */
2376 if (free_relocs != NULL)
2382 if (free_contents != NULL)
2384 if (! link_info->keep_memory)
2385 free (free_contents);
2388 /* Cache the section contents for elf_link_input_bfd. */
2389 elf_section_data (sec)->this_hdr.contents = contents;
2391 free_contents = NULL;
2394 if (free_extsyms != NULL)
2396 if (! link_info->keep_memory)
2397 free (free_extsyms);
2400 /* Cache the symbols for elf_link_input_bfd. */
2401 symtab_hdr->contents = extsyms;
2403 free_extsyms = NULL;
2409 if (free_relocs != NULL)
2411 if (free_contents != NULL)
2412 free (free_contents);
2413 if (free_extsyms != NULL)
2414 free (free_extsyms);
2418 /* Compute the stack size and movm arguments for the function
2419 referred to by HASH at address ADDR in section with
2420 contents CONTENTS, store the information in the hash table. */
2422 compute_function_info (abfd, hash, addr, contents)
2424 struct elf32_mn10300_link_hash_entry *hash;
2426 unsigned char *contents;
2428 unsigned char byte1, byte2;
2429 /* We only care about a very small subset of the possible prologue
2430 sequences here. Basically we look for:
2432 movm [d2,d3,a2,a3],sp (optional)
2433 add <size>,sp (optional, and only for sizes which fit in an unsigned
2436 If we find anything else, we quit. */
2438 /* Look for movm [regs],sp */
2439 byte1 = bfd_get_8 (abfd, contents + addr);
2440 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2444 hash->movm_args = byte2;
2446 byte1 = bfd_get_8 (abfd, contents + addr);
2447 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2450 /* Now figure out how much stack space will be allocated by the movm
2451 instruction. We need this kept separate from the funtion's normal
2453 if (hash->movm_args)
2456 if (hash->movm_args & 0x80)
2457 hash->movm_stack_size += 4;
2460 if (hash->movm_args & 0x40)
2461 hash->movm_stack_size += 4;
2464 if (hash->movm_args & 0x20)
2465 hash->movm_stack_size += 4;
2468 if (hash->movm_args & 0x10)
2469 hash->movm_stack_size += 4;
2471 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2472 if (hash->movm_args & 0x08)
2473 hash->movm_stack_size += 8 * 4;
2475 if (bfd_get_mach (abfd) == bfd_mach_am33)
2477 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2478 if (hash->movm_args & 0x1)
2479 hash->movm_stack_size += 6 * 4;
2481 /* exreg1 space. e4, e5, e6, e7 */
2482 if (hash->movm_args & 0x2)
2483 hash->movm_stack_size += 4 * 4;
2485 /* exreg0 space. e2, e3 */
2486 if (hash->movm_args & 0x4)
2487 hash->movm_stack_size += 2 * 4;
2491 /* Now look for the two stack adjustment variants. */
2492 if (byte1 == 0xf8 && byte2 == 0xfe)
2494 int temp = bfd_get_8 (abfd, contents + addr + 2);
2495 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2497 hash->stack_size = -temp;
2499 else if (byte1 == 0xfa && byte2 == 0xfe)
2501 int temp = bfd_get_16 (abfd, contents + addr + 2);
2502 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2506 hash->stack_size = temp;
2509 /* If the total stack to be allocated by the call instruction is more
2510 than 255 bytes, then we can't remove the stack adjustment by using
2511 "call" (we might still be able to remove the "movm" instruction. */
2512 if (hash->stack_size + hash->movm_stack_size > 255)
2513 hash->stack_size = 0;
2518 /* Delete some bytes from a section while relaxing. */
2521 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2527 Elf_Internal_Shdr *symtab_hdr;
2528 Elf32_External_Sym *extsyms;
2531 Elf_Internal_Rela *irel, *irelend;
2532 Elf_Internal_Rela *irelalign;
2534 Elf32_External_Sym *esym, *esymend;
2535 struct elf32_mn10300_link_hash_entry *sym_hash;
2537 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2538 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2540 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2542 contents = elf_section_data (sec)->this_hdr.contents;
2544 /* The deletion must stop at the next ALIGN reloc for an aligment
2545 power larger than the number of bytes we are deleting. */
2548 toaddr = sec->_cooked_size;
2550 irel = elf_section_data (sec)->relocs;
2551 irelend = irel + sec->reloc_count;
2553 /* Actually delete the bytes. */
2554 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2555 sec->_cooked_size -= count;
2557 /* Adjust all the relocs. */
2558 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2560 /* Get the new reloc address. */
2561 if ((irel->r_offset > addr
2562 && irel->r_offset < toaddr))
2563 irel->r_offset -= count;
2566 /* Adjust the local symbols defined in this section. */
2568 esymend = esym + symtab_hdr->sh_info;
2569 for (; esym < esymend; esym++)
2571 Elf_Internal_Sym isym;
2573 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2575 if (isym.st_shndx == shndx
2576 && isym.st_value > addr
2577 && isym.st_value < toaddr)
2579 isym.st_value -= count;
2580 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2584 /* Now adjust the global symbols defined in this section. */
2585 esym = extsyms + symtab_hdr->sh_info;
2586 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2587 for (index = 0; esym < esymend; esym++, index++)
2589 Elf_Internal_Sym isym;
2591 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2592 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2593 (elf_sym_hashes (abfd)[index]);
2594 if (isym.st_shndx == shndx
2595 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2596 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2597 && (sym_hash)->root.root.u.def.section == sec
2598 && (sym_hash)->root.root.u.def.value > addr
2599 && (sym_hash)->root.root.u.def.value < toaddr)
2601 (sym_hash)->root.root.u.def.value -= count;
2608 /* Return true if a symbol exists at the given address, else return
2611 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2614 Elf32_External_Sym *extsyms;
2617 Elf_Internal_Shdr *symtab_hdr;
2619 Elf32_External_Sym *esym, *esymend;
2620 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2622 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2623 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2625 /* Examine all the symbols. */
2627 esymend = esym + symtab_hdr->sh_info;
2628 for (; esym < esymend; esym++)
2630 Elf_Internal_Sym isym;
2632 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2634 if (isym.st_shndx == shndx
2635 && isym.st_value == addr)
2639 sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd));
2640 sym_hash_end = (sym_hash
2641 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2642 - symtab_hdr->sh_info));
2643 for (; sym_hash < sym_hash_end; sym_hash++)
2645 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2646 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2647 && (*sym_hash)->root.root.u.def.section == sec
2648 && (*sym_hash)->root.root.u.def.value == addr)
2654 /* This is a version of bfd_generic_get_relocated_section_contents
2655 which uses mn10300_elf_relocate_section. */
2658 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2659 data, relocateable, symbols)
2661 struct bfd_link_info *link_info;
2662 struct bfd_link_order *link_order;
2664 boolean relocateable;
2667 Elf_Internal_Shdr *symtab_hdr;
2668 asection *input_section = link_order->u.indirect.section;
2669 bfd *input_bfd = input_section->owner;
2670 asection **sections = NULL;
2671 Elf_Internal_Rela *internal_relocs = NULL;
2672 Elf32_External_Sym *external_syms = NULL;
2673 Elf_Internal_Sym *internal_syms = NULL;
2675 /* We only need to handle the case of relaxing, or of having a
2676 particular set of section contents, specially. */
2678 || elf_section_data (input_section)->this_hdr.contents == NULL)
2679 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2684 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2686 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2687 input_section->_raw_size);
2689 if ((input_section->flags & SEC_RELOC) != 0
2690 && input_section->reloc_count > 0)
2692 Elf_Internal_Sym *isymp;
2694 Elf32_External_Sym *esym, *esymend;
2696 if (symtab_hdr->contents != NULL)
2697 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2700 external_syms = ((Elf32_External_Sym *)
2701 bfd_malloc (symtab_hdr->sh_info
2702 * sizeof (Elf32_External_Sym)));
2703 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2705 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2706 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2707 symtab_hdr->sh_info, input_bfd)
2708 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2712 internal_relocs = (_bfd_elf32_link_read_relocs
2713 (input_bfd, input_section, (PTR) NULL,
2714 (Elf_Internal_Rela *) NULL, false));
2715 if (internal_relocs == NULL)
2718 internal_syms = ((Elf_Internal_Sym *)
2719 bfd_malloc (symtab_hdr->sh_info
2720 * sizeof (Elf_Internal_Sym)));
2721 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2724 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2725 * sizeof (asection *));
2726 if (sections == NULL && symtab_hdr->sh_info > 0)
2729 isymp = internal_syms;
2731 esym = external_syms;
2732 esymend = esym + symtab_hdr->sh_info;
2733 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2737 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2739 if (isymp->st_shndx == SHN_UNDEF)
2740 isec = bfd_und_section_ptr;
2741 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2742 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2743 else if (isymp->st_shndx == SHN_ABS)
2744 isec = bfd_abs_section_ptr;
2745 else if (isymp->st_shndx == SHN_COMMON)
2746 isec = bfd_com_section_ptr;
2756 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2757 input_section, data, internal_relocs,
2758 internal_syms, sections))
2761 if (sections != NULL)
2764 if (internal_syms != NULL)
2765 free (internal_syms);
2766 internal_syms = NULL;
2767 if (external_syms != NULL && symtab_hdr->contents == NULL)
2768 free (external_syms);
2769 external_syms = NULL;
2770 if (internal_relocs != elf_section_data (input_section)->relocs)
2771 free (internal_relocs);
2772 internal_relocs = NULL;
2778 if (internal_relocs != NULL
2779 && internal_relocs != elf_section_data (input_section)->relocs)
2780 free (internal_relocs);
2781 if (external_syms != NULL && symtab_hdr->contents == NULL)
2782 free (external_syms);
2783 if (internal_syms != NULL)
2784 free (internal_syms);
2785 if (sections != NULL)
2790 /* Assorted hash table functions. */
2792 /* Initialize an entry in the link hash table. */
2794 /* Create an entry in an MN10300 ELF linker hash table. */
2796 static struct bfd_hash_entry *
2797 elf32_mn10300_link_hash_newfunc (entry, table, string)
2798 struct bfd_hash_entry *entry;
2799 struct bfd_hash_table *table;
2802 struct elf32_mn10300_link_hash_entry *ret =
2803 (struct elf32_mn10300_link_hash_entry *) entry;
2805 /* Allocate the structure if it has not already been allocated by a
2807 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2808 ret = ((struct elf32_mn10300_link_hash_entry *)
2809 bfd_hash_allocate (table,
2810 sizeof (struct elf32_mn10300_link_hash_entry)));
2811 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2812 return (struct bfd_hash_entry *) ret;
2814 /* Call the allocation method of the superclass. */
2815 ret = ((struct elf32_mn10300_link_hash_entry *)
2816 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2818 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2820 ret->direct_calls = 0;
2821 ret->stack_size = 0;
2822 ret->movm_stack_size = 0;
2827 return (struct bfd_hash_entry *) ret;
2830 /* Create an mn10300 ELF linker hash table. */
2832 static struct bfd_link_hash_table *
2833 elf32_mn10300_link_hash_table_create (abfd)
2836 struct elf32_mn10300_link_hash_table *ret;
2838 ret = ((struct elf32_mn10300_link_hash_table *)
2839 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2840 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2843 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2844 elf32_mn10300_link_hash_newfunc))
2846 bfd_release (abfd, ret);
2851 ret->static_hash_table
2852 = ((struct elf32_mn10300_link_hash_table *)
2853 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2854 if (ret->static_hash_table == NULL)
2856 bfd_release (abfd, ret);
2860 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2861 elf32_mn10300_link_hash_newfunc))
2863 bfd_release (abfd, ret->static_hash_table);
2864 bfd_release (abfd, ret);
2867 return &ret->root.root;
2871 elf_mn10300_mach (flags)
2874 switch (flags & EF_MN10300_MACH)
2876 case E_MN10300_MACH_MN10300:
2878 return bfd_mach_mn10300;
2880 case E_MN10300_MACH_AM33:
2881 return bfd_mach_am33;
2885 /* The final processing done just before writing out a MN10300 ELF object
2886 file. This gets the MN10300 architecture right based on the machine
2891 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2893 boolean linker ATTRIBUTE_UNUSED;
2897 switch (bfd_get_mach (abfd))
2900 case bfd_mach_mn10300:
2901 val = E_MN10300_MACH_MN10300;
2905 val = E_MN10300_MACH_AM33;
2909 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2910 elf_elfheader (abfd)->e_flags |= val;
2914 _bfd_mn10300_elf_object_p (abfd)
2917 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2918 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2922 /* Merge backend specific data from an object file to the output
2923 object file when linking. */
2926 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2930 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2931 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2934 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2935 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2937 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2938 bfd_get_mach (ibfd)))
2945 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2946 #define TARGET_LITTLE_NAME "elf32-mn10300"
2947 #define ELF_ARCH bfd_arch_mn10300
2948 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2949 #define ELF_MAXPAGESIZE 0x1000
2951 #define elf_info_to_howto mn10300_info_to_howto
2952 #define elf_info_to_howto_rel 0
2953 #define elf_backend_can_gc_sections 1
2954 #define elf_backend_check_relocs mn10300_elf_check_relocs
2955 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2956 #define elf_backend_relocate_section mn10300_elf_relocate_section
2957 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2958 #define bfd_elf32_bfd_get_relocated_section_contents \
2959 mn10300_elf_get_relocated_section_contents
2960 #define bfd_elf32_bfd_link_hash_table_create \
2961 elf32_mn10300_link_hash_table_create
2963 #define elf_symbol_leading_char '_'
2965 /* So we can set bits in e_flags. */
2966 #define elf_backend_final_write_processing \
2967 _bfd_mn10300_elf_final_write_processing
2968 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2970 #define bfd_elf32_bfd_merge_private_bfd_data \
2971 _bfd_mn10300_elf_merge_private_bfd_data
2973 #include "elf32-target.h"