1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "elf/mn10300.h"
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
29 bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
31 static boolean mn10300_elf_relocate_section
32 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
33 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
34 static boolean mn10300_elf_relax_section
35 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
36 static bfd_byte * mn10300_elf_get_relocated_section_contents
37 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
38 bfd_byte *, boolean, asymbol **));
39 static unsigned long elf_mn10300_mach PARAMS ((flagword));
41 void _bfd_mn10300_elf_final_write_processing PARAMS ((bfd *, boolean));
42 boolean _bfd_mn10300_elf_object_p PARAMS ((bfd *));
43 boolean _bfd_mn10300_elf_merge_private_bfd_data PARAMS ((bfd *,bfd *));
45 struct elf32_mn10300_link_hash_entry {
46 /* The basic elf link hash table entry. */
47 struct elf_link_hash_entry root;
49 /* For function symbols, the number of times this function is
50 called directly (ie by name). */
51 unsigned int direct_calls;
53 /* For function symbols, the size of this function's stack
54 (if <= 255 bytes). We stuff this into "call" instructions
55 to this target when it's valid and profitable to do so.
57 This does not include stack allocated by movm! */
58 unsigned char stack_size;
60 /* For function symbols, arguments (if any) for movm instruction
61 in the prologue. We stuff this value into "call" instructions
62 to the target when it's valid and profitable to do so. */
63 unsigned char movm_args;
65 /* For funtion symbols, the amount of stack space that would be allocated
66 by the movm instruction. This is redundant with movm_args, but we
67 add it to the hash table to avoid computing it over and over. */
68 unsigned char movm_stack_size;
70 /* When set, convert all "call" instructions to this target into "calls"
72 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
74 /* Used to mark functions which have had redundant parts of their
76 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
80 /* We derive a hash table from the main elf linker hash table so
81 we can store state variables and a secondary hash table without
82 resorting to global variables. */
83 struct elf32_mn10300_link_hash_table {
84 /* The main hash table. */
85 struct elf_link_hash_table root;
87 /* A hash table for static functions. We could derive a new hash table
88 instead of using the full elf32_mn10300_link_hash_table if we wanted
89 to save some memory. */
90 struct elf32_mn10300_link_hash_table *static_hash_table;
92 /* Random linker state flags. */
93 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
97 /* For MN10300 linker hash table. */
99 /* Get the MN10300 ELF linker hash table from a link_info structure. */
101 #define elf32_mn10300_hash_table(p) \
102 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
107 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
110 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
111 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
112 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
114 static void elf32_mn10300_link_hash_table_free
115 PARAMS ((struct bfd_link_hash_table *));
117 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
118 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
119 static void mn10300_info_to_howto
120 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
121 static boolean mn10300_elf_check_relocs
122 PARAMS ((bfd *, struct bfd_link_info *, asection *,
123 const Elf_Internal_Rela *));
124 static asection *mn10300_elf_gc_mark_hook
125 PARAMS ((asection *, struct bfd_link_info *info, Elf_Internal_Rela *,
126 struct elf_link_hash_entry *, Elf_Internal_Sym *));
127 static boolean mn10300_elf_relax_delete_bytes
128 PARAMS ((bfd *, asection *, bfd_vma, int));
129 static boolean mn10300_elf_symbol_address_p
130 PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
131 static boolean elf32_mn10300_finish_hash_table_entry
132 PARAMS ((struct bfd_hash_entry *, PTR));
133 static void compute_function_info
134 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
135 bfd_vma, unsigned char *));
137 static reloc_howto_type elf_mn10300_howto_table[] = {
138 /* Dummy relocation. Does nothing. */
139 HOWTO (R_MN10300_NONE,
145 complain_overflow_bitfield,
146 bfd_elf_generic_reloc,
152 /* Standard 32 bit reloc. */
159 complain_overflow_bitfield,
160 bfd_elf_generic_reloc,
166 /* Standard 16 bit reloc. */
173 complain_overflow_bitfield,
174 bfd_elf_generic_reloc,
180 /* Standard 8 bit reloc. */
187 complain_overflow_bitfield,
188 bfd_elf_generic_reloc,
194 /* Standard 32bit pc-relative reloc. */
195 HOWTO (R_MN10300_PCREL32,
201 complain_overflow_bitfield,
202 bfd_elf_generic_reloc,
208 /* Standard 16bit pc-relative reloc. */
209 HOWTO (R_MN10300_PCREL16,
215 complain_overflow_bitfield,
216 bfd_elf_generic_reloc,
222 /* Standard 8 pc-relative reloc. */
223 HOWTO (R_MN10300_PCREL8,
229 complain_overflow_bitfield,
230 bfd_elf_generic_reloc,
237 /* GNU extension to record C++ vtable hierarchy */
238 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
240 0, /* size (0 = byte, 1 = short, 2 = long) */
242 false, /* pc_relative */
244 complain_overflow_dont, /* complain_on_overflow */
245 NULL, /* special_function */
246 "R_MN10300_GNU_VTINHERIT", /* name */
247 false, /* partial_inplace */
250 false), /* pcrel_offset */
252 /* GNU extension to record C++ vtable member usage */
253 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
255 0, /* size (0 = byte, 1 = short, 2 = long) */
257 false, /* pc_relative */
259 complain_overflow_dont, /* complain_on_overflow */
260 NULL, /* special_function */
261 "R_MN10300_GNU_VTENTRY", /* name */
262 false, /* partial_inplace */
265 false), /* pcrel_offset */
267 /* Standard 24 bit reloc. */
274 complain_overflow_bitfield,
275 bfd_elf_generic_reloc,
283 struct mn10300_reloc_map {
284 bfd_reloc_code_real_type bfd_reloc_val;
285 unsigned char elf_reloc_val;
288 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
289 { BFD_RELOC_NONE, R_MN10300_NONE, },
290 { BFD_RELOC_32, R_MN10300_32, },
291 { BFD_RELOC_16, R_MN10300_16, },
292 { BFD_RELOC_8, R_MN10300_8, },
293 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
294 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
295 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
296 { BFD_RELOC_24, R_MN10300_24, },
297 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
298 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
301 static reloc_howto_type *
302 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
303 bfd *abfd ATTRIBUTE_UNUSED;
304 bfd_reloc_code_real_type code;
309 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
312 if (mn10300_reloc_map[i].bfd_reloc_val == code)
313 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
319 /* Set the howto pointer for an MN10300 ELF reloc. */
322 mn10300_info_to_howto (abfd, cache_ptr, dst)
323 bfd *abfd ATTRIBUTE_UNUSED;
325 Elf32_Internal_Rela *dst;
329 r_type = ELF32_R_TYPE (dst->r_info);
330 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
331 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
334 /* Look through the relocs for a section during the first phase.
335 Since we don't do .gots or .plts, we just need to consider the
336 virtual table relocs for gc. */
339 mn10300_elf_check_relocs (abfd, info, sec, relocs)
341 struct bfd_link_info *info;
343 const Elf_Internal_Rela *relocs;
345 Elf_Internal_Shdr *symtab_hdr;
346 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
347 const Elf_Internal_Rela *rel;
348 const Elf_Internal_Rela *rel_end;
350 if (info->relocateable)
353 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
354 sym_hashes = elf_sym_hashes (abfd);
355 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
356 if (!elf_bad_symtab (abfd))
357 sym_hashes_end -= symtab_hdr->sh_info;
359 rel_end = relocs + sec->reloc_count;
360 for (rel = relocs; rel < rel_end; rel++)
362 struct elf_link_hash_entry *h;
363 unsigned long r_symndx;
365 r_symndx = ELF32_R_SYM (rel->r_info);
366 if (r_symndx < symtab_hdr->sh_info)
369 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
371 switch (ELF32_R_TYPE (rel->r_info))
373 /* This relocation describes the C++ object vtable hierarchy.
374 Reconstruct it for later use during GC. */
375 case R_MN10300_GNU_VTINHERIT:
376 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
380 /* This relocation describes which C++ vtable entries are actually
381 used. Record for later use during GC. */
382 case R_MN10300_GNU_VTENTRY:
383 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
392 /* Return the section that should be marked against GC for a given
396 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
398 struct bfd_link_info *info ATTRIBUTE_UNUSED;
399 Elf_Internal_Rela *rel;
400 struct elf_link_hash_entry *h;
401 Elf_Internal_Sym *sym;
405 switch (ELF32_R_TYPE (rel->r_info))
407 case R_MN10300_GNU_VTINHERIT:
408 case R_MN10300_GNU_VTENTRY:
412 switch (h->root.type)
414 case bfd_link_hash_defined:
415 case bfd_link_hash_defweak:
416 return h->root.u.def.section;
418 case bfd_link_hash_common:
419 return h->root.u.c.p->section;
427 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
432 /* Perform a relocation as part of a final link. */
433 static bfd_reloc_status_type
434 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
435 input_section, contents, offset, value,
436 addend, info, sym_sec, is_local)
437 reloc_howto_type *howto;
439 bfd *output_bfd ATTRIBUTE_UNUSED;
440 asection *input_section;
445 struct bfd_link_info *info ATTRIBUTE_UNUSED;
446 asection *sym_sec ATTRIBUTE_UNUSED;
447 int is_local ATTRIBUTE_UNUSED;
449 unsigned long r_type = howto->type;
450 bfd_byte *hit_data = contents + offset;
459 bfd_put_32 (input_bfd, value, hit_data);
465 if ((long) value > 0x7fffff || (long) value < -0x800000)
466 return bfd_reloc_overflow;
468 bfd_put_8 (input_bfd, value & 0xff, hit_data);
469 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
470 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
476 if ((long) value > 0x7fff || (long) value < -0x8000)
477 return bfd_reloc_overflow;
479 bfd_put_16 (input_bfd, value, hit_data);
485 if ((long) value > 0x7f || (long) value < -0x80)
486 return bfd_reloc_overflow;
488 bfd_put_8 (input_bfd, value, hit_data);
491 case R_MN10300_PCREL8:
492 value -= (input_section->output_section->vma
493 + input_section->output_offset);
497 if ((long) value > 0xff || (long) value < -0x100)
498 return bfd_reloc_overflow;
500 bfd_put_8 (input_bfd, value, hit_data);
503 case R_MN10300_PCREL16:
504 value -= (input_section->output_section->vma
505 + input_section->output_offset);
509 if ((long) value > 0xffff || (long) value < -0x10000)
510 return bfd_reloc_overflow;
512 bfd_put_16 (input_bfd, value, hit_data);
515 case R_MN10300_PCREL32:
516 value -= (input_section->output_section->vma
517 + input_section->output_offset);
521 bfd_put_32 (input_bfd, value, hit_data);
524 case R_MN10300_GNU_VTINHERIT:
525 case R_MN10300_GNU_VTENTRY:
529 return bfd_reloc_notsupported;
533 /* Relocate an MN10300 ELF section. */
535 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
536 contents, relocs, local_syms, local_sections)
538 struct bfd_link_info *info;
540 asection *input_section;
542 Elf_Internal_Rela *relocs;
543 Elf_Internal_Sym *local_syms;
544 asection **local_sections;
546 Elf_Internal_Shdr *symtab_hdr;
547 struct elf32_mn10300_link_hash_entry **sym_hashes;
548 Elf_Internal_Rela *rel, *relend;
550 if (info->relocateable)
553 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
554 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
555 (elf_sym_hashes (input_bfd));
558 relend = relocs + input_section->reloc_count;
559 for (; rel < relend; rel++)
562 reloc_howto_type *howto;
563 unsigned long r_symndx;
564 Elf_Internal_Sym *sym;
566 struct elf32_mn10300_link_hash_entry *h;
568 bfd_reloc_status_type r;
570 r_symndx = ELF32_R_SYM (rel->r_info);
571 r_type = ELF32_R_TYPE (rel->r_info);
572 howto = elf_mn10300_howto_table + r_type;
574 /* Just skip the vtable gc relocs. */
575 if (r_type == R_MN10300_GNU_VTINHERIT
576 || r_type == R_MN10300_GNU_VTENTRY)
582 if (r_symndx < symtab_hdr->sh_info)
584 sym = local_syms + r_symndx;
585 sec = local_sections[r_symndx];
586 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
590 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
591 while (h->root.root.type == bfd_link_hash_indirect
592 || h->root.root.type == bfd_link_hash_warning)
593 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
594 if (h->root.root.type == bfd_link_hash_defined
595 || h->root.root.type == bfd_link_hash_defweak)
597 sec = h->root.root.u.def.section;
598 relocation = (h->root.root.u.def.value
599 + sec->output_section->vma
600 + sec->output_offset);
602 else if (h->root.root.type == bfd_link_hash_undefweak)
606 if (! ((*info->callbacks->undefined_symbol)
607 (info, h->root.root.root.string, input_bfd,
608 input_section, rel->r_offset, true)))
614 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
616 contents, rel->r_offset,
617 relocation, rel->r_addend,
618 info, sec, h == NULL);
620 if (r != bfd_reloc_ok)
623 const char *msg = (const char *) 0;
626 name = h->root.root.root.string;
629 name = (bfd_elf_string_from_elf_section
630 (input_bfd, symtab_hdr->sh_link, sym->st_name));
631 if (name == NULL || *name == '\0')
632 name = bfd_section_name (input_bfd, sec);
637 case bfd_reloc_overflow:
638 if (! ((*info->callbacks->reloc_overflow)
639 (info, name, howto->name, (bfd_vma) 0,
640 input_bfd, input_section, rel->r_offset)))
644 case bfd_reloc_undefined:
645 if (! ((*info->callbacks->undefined_symbol)
646 (info, name, input_bfd, input_section,
647 rel->r_offset, true)))
651 case bfd_reloc_outofrange:
652 msg = _("internal error: out of range error");
655 case bfd_reloc_notsupported:
656 msg = _("internal error: unsupported relocation error");
659 case bfd_reloc_dangerous:
660 msg = _("internal error: dangerous error");
664 msg = _("internal error: unknown error");
668 if (!((*info->callbacks->warning)
669 (info, msg, name, input_bfd, input_section,
680 /* Finish initializing one hash table entry. */
682 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
683 struct bfd_hash_entry *gen_entry;
684 PTR in_args ATTRIBUTE_UNUSED;
686 struct elf32_mn10300_link_hash_entry *entry;
687 unsigned int byte_count = 0;
689 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
691 if (entry->root.root.type == bfd_link_hash_warning)
692 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
694 /* If we already know we want to convert "call" to "calls" for calls
695 to this symbol, then return now. */
696 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
699 /* If there are no named calls to this symbol, or there's nothing we
700 can move from the function itself into the "call" instruction, then
701 note that all "call" instructions should be converted into "calls"
702 instructions and return. */
703 if (entry->direct_calls == 0
704 || (entry->stack_size == 0 && entry->movm_args == 0))
706 /* Make a note that we should convert "call" instructions to "calls"
707 instructions for calls to this symbol. */
708 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
712 /* We may be able to move some instructions from the function itself into
713 the "call" instruction. Count how many bytes we might be able to
714 eliminate in the function itself. */
716 /* A movm instruction is two bytes. */
717 if (entry->movm_args)
720 /* Count the insn to allocate stack space too. */
721 if (entry->stack_size > 0 && entry->stack_size <= 128)
723 else if (entry->stack_size > 0 && entry->stack_size < 256)
726 /* If using "call" will result in larger code, then turn all
727 the associated "call" instructions into "calls" instrutions. */
728 if (byte_count < entry->direct_calls)
729 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
731 /* This routine never fails. */
735 /* This function handles relaxing for the mn10300.
737 There's quite a few relaxing opportunites available on the mn10300:
739 * calls:32 -> calls:16 2 bytes
740 * call:32 -> call:16 2 bytes
742 * call:32 -> calls:32 1 byte
743 * call:16 -> calls:16 1 byte
744 * These are done anytime using "calls" would result
745 in smaller code, or when necessary to preserve the
746 meaning of the program.
750 * In some circumstances we can move instructions
751 from a function prologue into a "call" instruction.
752 This is only done if the resulting code is no larger
753 than the original code.
755 * jmp:32 -> jmp:16 2 bytes
756 * jmp:16 -> bra:8 1 byte
758 * If the previous instruction is a conditional branch
759 around the jump/bra, we may be able to reverse its condition
760 and change its target to the jump's target. The jump/bra
761 can then be deleted. 2 bytes
763 * mov abs32 -> mov abs16 1 or 2 bytes
765 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
766 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
768 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
769 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
771 We don't handle imm16->imm8 or d16->d8 as they're very rare
772 and somewhat more difficult to support. */
775 mn10300_elf_relax_section (abfd, sec, link_info, again)
778 struct bfd_link_info *link_info;
781 Elf_Internal_Shdr *symtab_hdr;
782 Elf_Internal_Rela *internal_relocs = NULL;
783 Elf_Internal_Rela *irel, *irelend;
784 bfd_byte *contents = NULL;
785 Elf_Internal_Sym *isymbuf = NULL;
786 struct elf32_mn10300_link_hash_table *hash_table;
787 asection *section = sec;
789 /* Assume nothing changes. */
792 /* We need a pointer to the mn10300 specific hash table. */
793 hash_table = elf32_mn10300_hash_table (link_info);
795 /* Initialize fields in each hash table entry the first time through. */
796 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
800 /* Iterate over all the input bfds. */
801 for (input_bfd = link_info->input_bfds;
803 input_bfd = input_bfd->link_next)
805 /* We're going to need all the symbols for each bfd. */
806 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
807 if (symtab_hdr->sh_info != 0)
809 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
811 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
812 symtab_hdr->sh_info, 0,
818 /* Iterate over each section in this bfd. */
819 for (section = input_bfd->sections;
821 section = section->next)
823 struct elf32_mn10300_link_hash_entry *hash;
824 Elf_Internal_Sym *sym;
825 asection *sym_sec = NULL;
826 const char *sym_name;
829 /* If there's nothing to do in this section, skip it. */
830 if (! (((section->flags & SEC_RELOC) != 0
831 && section->reloc_count != 0)
832 || (section->flags & SEC_CODE) != 0))
835 /* Get cached copy of section contents if it exists. */
836 if (elf_section_data (section)->this_hdr.contents != NULL)
837 contents = elf_section_data (section)->this_hdr.contents;
838 else if (section->_raw_size != 0)
840 /* Go get them off disk. */
841 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
842 if (contents == NULL)
845 if (!bfd_get_section_contents (input_bfd, section,
846 contents, (file_ptr) 0,
853 /* If there aren't any relocs, then there's nothing to do. */
854 if ((section->flags & SEC_RELOC) != 0
855 && section->reloc_count != 0)
858 /* Get a copy of the native relocations. */
859 internal_relocs = (_bfd_elf32_link_read_relocs
860 (input_bfd, section, (PTR) NULL,
861 (Elf_Internal_Rela *) NULL,
862 link_info->keep_memory));
863 if (internal_relocs == NULL)
866 /* Now examine each relocation. */
867 irel = internal_relocs;
868 irelend = irel + section->reloc_count;
869 for (; irel < irelend; irel++)
872 unsigned long r_index;
875 r_type = ELF32_R_TYPE (irel->r_info);
876 r_index = ELF32_R_SYM (irel->r_info);
878 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
881 /* We need the name and hash table entry of the target
887 if (r_index < symtab_hdr->sh_info)
889 /* A local symbol. */
890 Elf_Internal_Sym *isym;
891 struct elf_link_hash_table *elftab;
894 isym = isymbuf + r_index;
895 if (isym->st_shndx == SHN_UNDEF)
896 sym_sec = bfd_und_section_ptr;
897 else if (isym->st_shndx == SHN_ABS)
898 sym_sec = bfd_abs_section_ptr;
899 else if (isym->st_shndx == SHN_COMMON)
900 sym_sec = bfd_com_section_ptr;
903 = bfd_section_from_elf_index (input_bfd,
907 = bfd_elf_string_from_elf_section (input_bfd,
912 /* If it isn't a function, then we don't care
914 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
917 /* Tack on an ID so we can uniquely identify this
918 local symbol in the global hash table. */
919 amt = strlen (sym_name) + 10;
920 new_name = bfd_malloc (amt);
924 sprintf (new_name, "%s_%08x",
925 sym_name, (int) sym_sec);
928 elftab = &hash_table->static_hash_table->root;
929 hash = ((struct elf32_mn10300_link_hash_entry *)
930 elf_link_hash_lookup (elftab, sym_name,
936 r_index -= symtab_hdr->sh_info;
937 hash = (struct elf32_mn10300_link_hash_entry *)
938 elf_sym_hashes (input_bfd)[r_index];
941 /* If this is not a "call" instruction, then we
942 should convert "call" instructions to "calls"
944 code = bfd_get_8 (input_bfd,
945 contents + irel->r_offset - 1);
946 if (code != 0xdd && code != 0xcd)
947 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
949 /* If this is a jump/call, then bump the
950 direct_calls counter. Else force "call" to
951 "calls" conversions. */
952 if (r_type == R_MN10300_PCREL32
953 || r_type == R_MN10300_PCREL16)
954 hash->direct_calls++;
956 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
960 /* Now look at the actual contents to get the stack size,
961 and a list of what registers were saved in the prologue
963 if ((section->flags & SEC_CODE) != 0)
965 Elf_Internal_Sym *isym, *isymend;
966 unsigned int sec_shndx;
967 struct elf_link_hash_entry **hashes;
968 struct elf_link_hash_entry **end_hashes;
969 unsigned int symcount;
971 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
974 /* Look at each function defined in this section and
975 update info for that function. */
976 isymend = isymbuf + symtab_hdr->sh_info;
977 for (isym = isymbuf; isym < isymend; isym++)
979 if (isym->st_shndx == sec_shndx
980 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
982 struct elf_link_hash_table *elftab;
985 if (isym->st_shndx == SHN_UNDEF)
986 sym_sec = bfd_und_section_ptr;
987 else if (isym->st_shndx == SHN_ABS)
988 sym_sec = bfd_abs_section_ptr;
989 else if (isym->st_shndx == SHN_COMMON)
990 sym_sec = bfd_com_section_ptr;
993 = bfd_section_from_elf_index (input_bfd,
996 sym_name = (bfd_elf_string_from_elf_section
997 (input_bfd, symtab_hdr->sh_link,
1000 /* Tack on an ID so we can uniquely identify this
1001 local symbol in the global hash table. */
1002 amt = strlen (sym_name) + 10;
1003 new_name = bfd_malloc (amt);
1007 sprintf (new_name, "%s_%08x",
1008 sym_name, (int) sym_sec);
1009 sym_name = new_name;
1011 elftab = &hash_table->static_hash_table->root;
1012 hash = ((struct elf32_mn10300_link_hash_entry *)
1013 elf_link_hash_lookup (elftab, sym_name,
1014 true, true, false));
1016 compute_function_info (input_bfd, hash,
1017 isym->st_value, contents);
1021 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1022 - symtab_hdr->sh_info);
1023 hashes = elf_sym_hashes (abfd);
1024 end_hashes = hashes + symcount;
1025 for (; hashes < end_hashes; hashes++)
1027 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1028 if ((hash->root.root.type == bfd_link_hash_defined
1029 || hash->root.root.type == bfd_link_hash_defweak)
1030 && hash->root.root.u.def.section == section
1031 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1032 compute_function_info (input_bfd, hash,
1033 (hash)->root.root.u.def.value,
1038 /* Cache or free any memory we allocated for the relocs. */
1039 if (internal_relocs != NULL
1040 && elf_section_data (section)->relocs != internal_relocs)
1041 free (internal_relocs);
1042 internal_relocs = NULL;
1044 /* Cache or free any memory we allocated for the contents. */
1045 if (contents != NULL
1046 && elf_section_data (section)->this_hdr.contents != contents)
1048 if (! link_info->keep_memory)
1052 /* Cache the section contents for elf_link_input_bfd. */
1053 elf_section_data (section)->this_hdr.contents = contents;
1059 /* Cache or free any memory we allocated for the symbols. */
1061 && symtab_hdr->contents != (unsigned char *) isymbuf)
1063 if (! link_info->keep_memory)
1067 /* Cache the symbols for elf_link_input_bfd. */
1068 symtab_hdr->contents = (unsigned char *) isymbuf;
1074 /* Now iterate on each symbol in the hash table and perform
1075 the final initialization steps on each. */
1076 elf32_mn10300_link_hash_traverse (hash_table,
1077 elf32_mn10300_finish_hash_table_entry,
1079 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1080 elf32_mn10300_finish_hash_table_entry,
1083 /* All entries in the hash table are fully initialized. */
1084 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1086 /* Now that everything has been initialized, go through each
1087 code section and delete any prologue insns which will be
1088 redundant because their operations will be performed by
1089 a "call" instruction. */
1090 for (input_bfd = link_info->input_bfds;
1092 input_bfd = input_bfd->link_next)
1094 /* We're going to need all the local symbols for each bfd. */
1095 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1096 if (symtab_hdr->sh_info != 0)
1098 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1099 if (isymbuf == NULL)
1100 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1101 symtab_hdr->sh_info, 0,
1103 if (isymbuf == NULL)
1107 /* Walk over each section in this bfd. */
1108 for (section = input_bfd->sections;
1110 section = section->next)
1112 unsigned int sec_shndx;
1113 Elf_Internal_Sym *isym, *isymend;
1114 struct elf_link_hash_entry **hashes;
1115 struct elf_link_hash_entry **end_hashes;
1116 unsigned int symcount;
1118 /* Skip non-code sections and empty sections. */
1119 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1122 if (section->reloc_count != 0)
1124 /* Get a copy of the native relocations. */
1125 internal_relocs = (_bfd_elf32_link_read_relocs
1126 (input_bfd, section, (PTR) NULL,
1127 (Elf_Internal_Rela *) NULL,
1128 link_info->keep_memory));
1129 if (internal_relocs == NULL)
1133 /* Get cached copy of section contents if it exists. */
1134 if (elf_section_data (section)->this_hdr.contents != NULL)
1135 contents = elf_section_data (section)->this_hdr.contents;
1138 /* Go get them off disk. */
1139 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1140 if (contents == NULL)
1143 if (!bfd_get_section_contents (input_bfd, section,
1144 contents, (file_ptr) 0,
1145 section->_raw_size))
1149 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1152 /* Now look for any function in this section which needs
1153 insns deleted from its prologue. */
1154 isymend = isymbuf + symtab_hdr->sh_info;
1155 for (isym = isymbuf; isym < isymend; isym++)
1157 struct elf32_mn10300_link_hash_entry *sym_hash;
1158 asection *sym_sec = NULL;
1159 const char *sym_name;
1161 struct elf_link_hash_table *elftab;
1164 if (isym->st_shndx != sec_shndx)
1167 if (isym->st_shndx == SHN_UNDEF)
1168 sym_sec = bfd_und_section_ptr;
1169 else if (isym->st_shndx == SHN_ABS)
1170 sym_sec = bfd_abs_section_ptr;
1171 else if (isym->st_shndx == SHN_COMMON)
1172 sym_sec = bfd_com_section_ptr;
1175 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1178 = bfd_elf_string_from_elf_section (input_bfd,
1179 symtab_hdr->sh_link,
1182 /* Tack on an ID so we can uniquely identify this
1183 local symbol in the global hash table. */
1184 amt = strlen (sym_name) + 10;
1185 new_name = bfd_malloc (amt);
1188 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1189 sym_name = new_name;
1191 elftab = &hash_table->static_hash_table->root;
1192 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1193 elf_link_hash_lookup (elftab, sym_name,
1194 false, false, false));
1197 if (sym_hash == NULL)
1200 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1201 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1205 /* Note that we've changed things. */
1206 elf_section_data (section)->relocs = internal_relocs;
1207 elf_section_data (section)->this_hdr.contents = contents;
1208 symtab_hdr->contents = (unsigned char *) isymbuf;
1210 /* Count how many bytes we're going to delete. */
1211 if (sym_hash->movm_args)
1214 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1216 else if (sym_hash->stack_size
1217 && sym_hash->stack_size < 256)
1220 /* Note that we've deleted prologue bytes for this
1222 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1224 /* Actually delete the bytes. */
1225 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1231 /* Something changed. Not strictly necessary, but
1232 may lead to more relaxing opportunities. */
1237 /* Look for any global functions in this section which
1238 need insns deleted from their prologues. */
1239 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1240 - symtab_hdr->sh_info);
1241 hashes = elf_sym_hashes (abfd);
1242 end_hashes = hashes + symcount;
1243 for (; hashes < end_hashes; hashes++)
1245 struct elf32_mn10300_link_hash_entry *sym_hash;
1247 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1248 if ((sym_hash->root.root.type == bfd_link_hash_defined
1249 || sym_hash->root.root.type == bfd_link_hash_defweak)
1250 && sym_hash->root.root.u.def.section == section
1251 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1252 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1257 /* Note that we've changed things. */
1258 elf_section_data (section)->relocs = internal_relocs;
1259 elf_section_data (section)->this_hdr.contents = contents;
1260 symtab_hdr->contents = (unsigned char *) isymbuf;
1262 /* Count how many bytes we're going to delete. */
1263 if (sym_hash->movm_args)
1266 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1268 else if (sym_hash->stack_size
1269 && sym_hash->stack_size < 256)
1272 /* Note that we've deleted prologue bytes for this
1274 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1276 /* Actually delete the bytes. */
1277 symval = sym_hash->root.root.u.def.value;
1278 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1284 /* Something changed. Not strictly necessary, but
1285 may lead to more relaxing opportunities. */
1290 /* Cache or free any memory we allocated for the relocs. */
1291 if (internal_relocs != NULL
1292 && elf_section_data (section)->relocs != internal_relocs)
1293 free (internal_relocs);
1294 internal_relocs = NULL;
1296 /* Cache or free any memory we allocated for the contents. */
1297 if (contents != NULL
1298 && elf_section_data (section)->this_hdr.contents != contents)
1300 if (! link_info->keep_memory)
1304 /* Cache the section contents for elf_link_input_bfd. */
1305 elf_section_data (section)->this_hdr.contents = contents;
1311 /* Cache or free any memory we allocated for the symbols. */
1313 && symtab_hdr->contents != (unsigned char *) isymbuf)
1315 if (! link_info->keep_memory)
1319 /* Cache the symbols for elf_link_input_bfd. */
1320 symtab_hdr->contents = (unsigned char *) isymbuf;
1327 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1329 internal_relocs = NULL;
1331 /* For error_return. */
1334 /* We don't have to do anything for a relocateable link, if
1335 this section does not have relocs, or if this is not a
1337 if (link_info->relocateable
1338 || (sec->flags & SEC_RELOC) == 0
1339 || sec->reloc_count == 0
1340 || (sec->flags & SEC_CODE) == 0)
1343 /* If this is the first time we have been called for this section,
1344 initialize the cooked size. */
1345 if (sec->_cooked_size == 0)
1346 sec->_cooked_size = sec->_raw_size;
1348 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1350 /* Get a copy of the native relocations. */
1351 internal_relocs = (_bfd_elf32_link_read_relocs
1352 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1353 link_info->keep_memory));
1354 if (internal_relocs == NULL)
1357 /* Walk through them looking for relaxing opportunities. */
1358 irelend = internal_relocs + sec->reloc_count;
1359 for (irel = internal_relocs; irel < irelend; irel++)
1362 struct elf32_mn10300_link_hash_entry *h = NULL;
1364 /* If this isn't something that can be relaxed, then ignore
1366 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1367 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1368 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1371 /* Get the section contents if we haven't done so already. */
1372 if (contents == NULL)
1374 /* Get cached copy if it exists. */
1375 if (elf_section_data (sec)->this_hdr.contents != NULL)
1376 contents = elf_section_data (sec)->this_hdr.contents;
1379 /* Go get them off disk. */
1380 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1381 if (contents == NULL)
1384 if (! bfd_get_section_contents (abfd, sec, contents,
1385 (file_ptr) 0, sec->_raw_size))
1390 /* Read this BFD's symbols if we haven't done so already. */
1391 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1393 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1394 if (isymbuf == NULL)
1395 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1396 symtab_hdr->sh_info, 0,
1398 if (isymbuf == NULL)
1402 /* Get the value of the symbol referred to by the reloc. */
1403 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1405 Elf_Internal_Sym *isym;
1406 asection *sym_sec = NULL;
1407 const char *sym_name;
1410 /* A local symbol. */
1411 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1412 if (isym->st_shndx == SHN_UNDEF)
1413 sym_sec = bfd_und_section_ptr;
1414 else if (isym->st_shndx == SHN_ABS)
1415 sym_sec = bfd_abs_section_ptr;
1416 else if (isym->st_shndx == SHN_COMMON)
1417 sym_sec = bfd_com_section_ptr;
1419 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1421 symval = (isym->st_value
1422 + sym_sec->output_section->vma
1423 + sym_sec->output_offset);
1424 sym_name = bfd_elf_string_from_elf_section (abfd,
1425 symtab_hdr->sh_link,
1428 /* Tack on an ID so we can uniquely identify this
1429 local symbol in the global hash table. */
1430 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1433 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1434 sym_name = new_name;
1436 h = (struct elf32_mn10300_link_hash_entry *)
1437 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1438 sym_name, false, false, false);
1445 /* An external symbol. */
1446 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1447 h = (struct elf32_mn10300_link_hash_entry *)
1448 (elf_sym_hashes (abfd)[indx]);
1449 BFD_ASSERT (h != NULL);
1450 if (h->root.root.type != bfd_link_hash_defined
1451 && h->root.root.type != bfd_link_hash_defweak)
1453 /* This appears to be a reference to an undefined
1454 symbol. Just ignore it--it will be caught by the
1455 regular reloc processing. */
1459 symval = (h->root.root.u.def.value
1460 + h->root.root.u.def.section->output_section->vma
1461 + h->root.root.u.def.section->output_offset);
1464 /* For simplicity of coding, we are going to modify the section
1465 contents, the section relocs, and the BFD symbol table. We
1466 must tell the rest of the code not to free up this
1467 information. It would be possible to instead create a table
1468 of changes which have to be made, as is done in coff-mips.c;
1469 that would be more work, but would require less memory when
1470 the linker is run. */
1472 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1473 branch/call, also deal with "call" -> "calls" conversions and
1474 insertion of prologue data into "call" instructions. */
1475 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1477 bfd_vma value = symval;
1479 /* If we've got a "call" instruction that needs to be turned
1480 into a "calls" instruction, do so now. It saves a byte. */
1481 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1485 /* Get the opcode. */
1486 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1488 /* Make sure we're working with a "call" instruction! */
1491 /* Note that we've changed the relocs, section contents,
1493 elf_section_data (sec)->relocs = internal_relocs;
1494 elf_section_data (sec)->this_hdr.contents = contents;
1495 symtab_hdr->contents = (unsigned char *) isymbuf;
1497 /* Fix the opcode. */
1498 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1499 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1501 /* Fix irel->r_offset and irel->r_addend. */
1502 irel->r_offset += 1;
1503 irel->r_addend += 1;
1505 /* Delete one byte of data. */
1506 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1507 irel->r_offset + 3, 1))
1510 /* That will change things, so, we should relax again.
1511 Note that this is not required, and it may be slow. */
1517 /* We've got a "call" instruction which needs some data
1518 from target function filled in. */
1521 /* Get the opcode. */
1522 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1524 /* Insert data from the target function into the "call"
1525 instruction if needed. */
1528 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1529 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1530 contents + irel->r_offset + 5);
1534 /* Deal with pc-relative gunk. */
1535 value -= (sec->output_section->vma + sec->output_offset);
1536 value -= irel->r_offset;
1537 value += irel->r_addend;
1539 /* See if the value will fit in 16 bits, note the high value is
1540 0x7fff + 2 as the target will be two bytes closer if we are
1542 if ((long) value < 0x8001 && (long) value > -0x8000)
1546 /* Get the opcode. */
1547 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1549 if (code != 0xdc && code != 0xdd && code != 0xff)
1552 /* Note that we've changed the relocs, section contents, etc. */
1553 elf_section_data (sec)->relocs = internal_relocs;
1554 elf_section_data (sec)->this_hdr.contents = contents;
1555 symtab_hdr->contents = (unsigned char *) isymbuf;
1557 /* Fix the opcode. */
1559 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1560 else if (code == 0xdd)
1561 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1562 else if (code == 0xff)
1563 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1565 /* Fix the relocation's type. */
1566 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1569 /* Delete two bytes of data. */
1570 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1571 irel->r_offset + 1, 2))
1574 /* That will change things, so, we should relax again.
1575 Note that this is not required, and it may be slow. */
1580 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1582 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1584 bfd_vma value = symval;
1586 /* If we've got a "call" instruction that needs to be turned
1587 into a "calls" instruction, do so now. It saves a byte. */
1588 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1592 /* Get the opcode. */
1593 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1595 /* Make sure we're working with a "call" instruction! */
1598 /* Note that we've changed the relocs, section contents,
1600 elf_section_data (sec)->relocs = internal_relocs;
1601 elf_section_data (sec)->this_hdr.contents = contents;
1602 symtab_hdr->contents = (unsigned char *) isymbuf;
1604 /* Fix the opcode. */
1605 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1606 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1608 /* Fix irel->r_offset and irel->r_addend. */
1609 irel->r_offset += 1;
1610 irel->r_addend += 1;
1612 /* Delete one byte of data. */
1613 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1614 irel->r_offset + 1, 1))
1617 /* That will change things, so, we should relax again.
1618 Note that this is not required, and it may be slow. */
1626 /* Get the opcode. */
1627 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1629 /* Insert data from the target function into the "call"
1630 instruction if needed. */
1633 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1634 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1635 contents + irel->r_offset + 3);
1639 /* Deal with pc-relative gunk. */
1640 value -= (sec->output_section->vma + sec->output_offset);
1641 value -= irel->r_offset;
1642 value += irel->r_addend;
1644 /* See if the value will fit in 8 bits, note the high value is
1645 0x7f + 1 as the target will be one bytes closer if we are
1647 if ((long) value < 0x80 && (long) value > -0x80)
1651 /* Get the opcode. */
1652 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1657 /* Note that we've changed the relocs, section contents, etc. */
1658 elf_section_data (sec)->relocs = internal_relocs;
1659 elf_section_data (sec)->this_hdr.contents = contents;
1660 symtab_hdr->contents = (unsigned char *) isymbuf;
1662 /* Fix the opcode. */
1663 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1665 /* Fix the relocation's type. */
1666 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1669 /* Delete one byte of data. */
1670 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1671 irel->r_offset + 1, 1))
1674 /* That will change things, so, we should relax again.
1675 Note that this is not required, and it may be slow. */
1680 /* Try to eliminate an unconditional 8 bit pc-relative branch
1681 which immediately follows a conditional 8 bit pc-relative
1682 branch around the unconditional branch.
1689 This happens when the bCC can't reach lab2 at assembly time,
1690 but due to other relaxations it can reach at link time. */
1691 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1693 Elf_Internal_Rela *nrel;
1694 bfd_vma value = symval;
1697 /* Deal with pc-relative gunk. */
1698 value -= (sec->output_section->vma + sec->output_offset);
1699 value -= irel->r_offset;
1700 value += irel->r_addend;
1702 /* Do nothing if this reloc is the last byte in the section. */
1703 if (irel->r_offset == sec->_cooked_size)
1706 /* See if the next instruction is an unconditional pc-relative
1707 branch, more often than not this test will fail, so we
1708 test it first to speed things up. */
1709 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1713 /* Also make sure the next relocation applies to the next
1714 instruction and that it's a pc-relative 8 bit branch. */
1717 || irel->r_offset + 2 != nrel->r_offset
1718 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1721 /* Make sure our destination immediately follows the
1722 unconditional branch. */
1723 if (symval != (sec->output_section->vma + sec->output_offset
1724 + irel->r_offset + 3))
1727 /* Now make sure we are a conditional branch. This may not
1728 be necessary, but why take the chance.
1730 Note these checks assume that R_MN10300_PCREL8 relocs
1731 only occur on bCC and bCCx insns. If they occured
1732 elsewhere, we'd need to know the start of this insn
1733 for this check to be accurate. */
1734 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1735 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1736 && code != 0xc3 && code != 0xc4 && code != 0xc5
1737 && code != 0xc6 && code != 0xc7 && code != 0xc8
1738 && code != 0xc9 && code != 0xe8 && code != 0xe9
1739 && code != 0xea && code != 0xeb)
1742 /* We also have to be sure there is no symbol/label
1743 at the unconditional branch. */
1744 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
1745 irel->r_offset + 1))
1748 /* Note that we've changed the relocs, section contents, etc. */
1749 elf_section_data (sec)->relocs = internal_relocs;
1750 elf_section_data (sec)->this_hdr.contents = contents;
1751 symtab_hdr->contents = (unsigned char *) isymbuf;
1753 /* Reverse the condition of the first branch. */
1799 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1801 /* Set the reloc type and symbol for the first branch
1802 from the second branch. */
1803 irel->r_info = nrel->r_info;
1805 /* Make the reloc for the second branch a null reloc. */
1806 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1809 /* Delete two bytes of data. */
1810 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1811 irel->r_offset + 1, 2))
1814 /* That will change things, so, we should relax again.
1815 Note that this is not required, and it may be slow. */
1819 /* Try to turn a 24 immediate, displacement or absolute address
1820 into a 8 immediate, displacement or absolute address. */
1821 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1823 bfd_vma value = symval;
1824 value += irel->r_addend;
1826 /* See if the value will fit in 8 bits. */
1827 if ((long) value < 0x7f && (long) value > -0x80)
1831 /* AM33 insns which have 24 operands are 6 bytes long and
1832 will have 0xfd as the first byte. */
1834 /* Get the first opcode. */
1835 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1839 /* Get the second opcode. */
1840 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1842 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1843 equivalent instructions exists. */
1844 if (code != 0x6b && code != 0x7b
1845 && code != 0x8b && code != 0x9b
1846 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1847 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1848 || (code & 0x0f) == 0x0e))
1850 /* Not safe if the high bit is on as relaxing may
1851 move the value out of high mem and thus not fit
1852 in a signed 8bit value. This is currently over
1854 if ((value & 0x80) == 0)
1856 /* Note that we've changed the relocation contents,
1858 elf_section_data (sec)->relocs = internal_relocs;
1859 elf_section_data (sec)->this_hdr.contents = contents;
1860 symtab_hdr->contents = (unsigned char *) isymbuf;
1862 /* Fix the opcode. */
1863 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1864 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1866 /* Fix the relocation's type. */
1868 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1871 /* Delete two bytes of data. */
1872 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1873 irel->r_offset + 1, 2))
1876 /* That will change things, so, we should relax
1877 again. Note that this is not required, and it
1887 /* Try to turn a 32bit immediate, displacement or absolute address
1888 into a 16bit immediate, displacement or absolute address. */
1889 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1891 bfd_vma value = symval;
1892 value += irel->r_addend;
1894 /* See if the value will fit in 24 bits.
1895 We allow any 16bit match here. We prune those we can't
1897 if ((long) value < 0x7fffff && (long) value > -0x800000)
1901 /* AM33 insns which have 32bit operands are 7 bytes long and
1902 will have 0xfe as the first byte. */
1904 /* Get the first opcode. */
1905 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1909 /* Get the second opcode. */
1910 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1912 /* All the am33 32 -> 24 relaxing possibilities. */
1913 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1914 equivalent instructions exists. */
1915 if (code != 0x6b && code != 0x7b
1916 && code != 0x8b && code != 0x9b
1917 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1918 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1919 || (code & 0x0f) == 0x0e))
1921 /* Not safe if the high bit is on as relaxing may
1922 move the value out of high mem and thus not fit
1923 in a signed 16bit value. This is currently over
1925 if ((value & 0x8000) == 0)
1927 /* Note that we've changed the relocation contents,
1929 elf_section_data (sec)->relocs = internal_relocs;
1930 elf_section_data (sec)->this_hdr.contents = contents;
1931 symtab_hdr->contents = (unsigned char *) isymbuf;
1933 /* Fix the opcode. */
1934 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
1935 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1937 /* Fix the relocation's type. */
1939 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1942 /* Delete one byte of data. */
1943 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1944 irel->r_offset + 3, 1))
1947 /* That will change things, so, we should relax
1948 again. Note that this is not required, and it
1957 /* See if the value will fit in 16 bits.
1958 We allow any 16bit match here. We prune those we can't
1960 if ((long) value < 0x7fff && (long) value > -0x8000)
1964 /* Most insns which have 32bit operands are 6 bytes long;
1965 exceptions are pcrel insns and bit insns.
1967 We handle pcrel insns above. We don't bother trying
1968 to handle the bit insns here.
1970 The first byte of the remaining insns will be 0xfc. */
1972 /* Get the first opcode. */
1973 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1978 /* Get the second opcode. */
1979 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1981 if ((code & 0xf0) < 0x80)
1982 switch (code & 0xf0)
1984 /* mov (d32,am),dn -> mov (d32,am),dn
1985 mov dm,(d32,am) -> mov dn,(d32,am)
1986 mov (d32,am),an -> mov (d32,am),an
1987 mov dm,(d32,am) -> mov dn,(d32,am)
1988 movbu (d32,am),dn -> movbu (d32,am),dn
1989 movbu dm,(d32,am) -> movbu dn,(d32,am)
1990 movhu (d32,am),dn -> movhu (d32,am),dn
1991 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2000 /* Not safe if the high bit is on as relaxing may
2001 move the value out of high mem and thus not fit
2002 in a signed 16bit value. */
2004 && (value & 0x8000))
2007 /* Note that we've changed the relocation contents, etc. */
2008 elf_section_data (sec)->relocs = internal_relocs;
2009 elf_section_data (sec)->this_hdr.contents = contents;
2010 symtab_hdr->contents = (unsigned char *) isymbuf;
2012 /* Fix the opcode. */
2013 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2014 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2016 /* Fix the relocation's type. */
2017 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2020 /* Delete two bytes of data. */
2021 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2022 irel->r_offset + 2, 2))
2025 /* That will change things, so, we should relax again.
2026 Note that this is not required, and it may be slow. */
2030 else if ((code & 0xf0) == 0x80
2031 || (code & 0xf0) == 0x90)
2032 switch (code & 0xf3)
2034 /* mov dn,(abs32) -> mov dn,(abs16)
2035 movbu dn,(abs32) -> movbu dn,(abs16)
2036 movhu dn,(abs32) -> movhu dn,(abs16) */
2040 /* Note that we've changed the relocation contents, etc. */
2041 elf_section_data (sec)->relocs = internal_relocs;
2042 elf_section_data (sec)->this_hdr.contents = contents;
2043 symtab_hdr->contents = (unsigned char *) isymbuf;
2045 if ((code & 0xf3) == 0x81)
2046 code = 0x01 + (code & 0x0c);
2047 else if ((code & 0xf3) == 0x82)
2048 code = 0x02 + (code & 0x0c);
2049 else if ((code & 0xf3) == 0x83)
2050 code = 0x03 + (code & 0x0c);
2054 /* Fix the opcode. */
2055 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2057 /* Fix the relocation's type. */
2058 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2061 /* The opcode got shorter too, so we have to fix the
2062 addend and offset too! */
2063 irel->r_offset -= 1;
2065 /* Delete three bytes of data. */
2066 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2067 irel->r_offset + 1, 3))
2070 /* That will change things, so, we should relax again.
2071 Note that this is not required, and it may be slow. */
2075 /* mov am,(abs32) -> mov am,(abs16)
2076 mov am,(d32,sp) -> mov am,(d16,sp)
2077 mov dm,(d32,sp) -> mov dm,(d32,sp)
2078 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2079 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2085 /* sp-based offsets are zero-extended. */
2086 if (code >= 0x90 && code <= 0x93
2090 /* Note that we've changed the relocation contents, etc. */
2091 elf_section_data (sec)->relocs = internal_relocs;
2092 elf_section_data (sec)->this_hdr.contents = contents;
2093 symtab_hdr->contents = (unsigned char *) isymbuf;
2095 /* Fix the opcode. */
2096 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2097 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2099 /* Fix the relocation's type. */
2100 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2103 /* Delete two bytes of data. */
2104 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2105 irel->r_offset + 2, 2))
2108 /* That will change things, so, we should relax again.
2109 Note that this is not required, and it may be slow. */
2113 else if ((code & 0xf0) < 0xf0)
2114 switch (code & 0xfc)
2116 /* mov imm32,dn -> mov imm16,dn
2117 mov imm32,an -> mov imm16,an
2118 mov (abs32),dn -> mov (abs16),dn
2119 movbu (abs32),dn -> movbu (abs16),dn
2120 movhu (abs32),dn -> movhu (abs16),dn */
2126 /* Not safe if the high bit is on as relaxing may
2127 move the value out of high mem and thus not fit
2128 in a signed 16bit value. */
2130 && (value & 0x8000))
2133 /* mov imm16, an zero-extends the immediate. */
2138 /* Note that we've changed the relocation contents, etc. */
2139 elf_section_data (sec)->relocs = internal_relocs;
2140 elf_section_data (sec)->this_hdr.contents = contents;
2141 symtab_hdr->contents = (unsigned char *) isymbuf;
2143 if ((code & 0xfc) == 0xcc)
2144 code = 0x2c + (code & 0x03);
2145 else if ((code & 0xfc) == 0xdc)
2146 code = 0x24 + (code & 0x03);
2147 else if ((code & 0xfc) == 0xa4)
2148 code = 0x30 + (code & 0x03);
2149 else if ((code & 0xfc) == 0xa8)
2150 code = 0x34 + (code & 0x03);
2151 else if ((code & 0xfc) == 0xac)
2152 code = 0x38 + (code & 0x03);
2156 /* Fix the opcode. */
2157 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2159 /* Fix the relocation's type. */
2160 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2163 /* The opcode got shorter too, so we have to fix the
2164 addend and offset too! */
2165 irel->r_offset -= 1;
2167 /* Delete three bytes of data. */
2168 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2169 irel->r_offset + 1, 3))
2172 /* That will change things, so, we should relax again.
2173 Note that this is not required, and it may be slow. */
2177 /* mov (abs32),an -> mov (abs16),an
2178 mov (d32,sp),an -> mov (d16,sp),an
2179 mov (d32,sp),dn -> mov (d16,sp),dn
2180 movbu (d32,sp),dn -> movbu (d16,sp),dn
2181 movhu (d32,sp),dn -> movhu (d16,sp),dn
2182 add imm32,dn -> add imm16,dn
2183 cmp imm32,dn -> cmp imm16,dn
2184 add imm32,an -> add imm16,an
2185 cmp imm32,an -> cmp imm16,an
2186 and imm32,dn -> and imm16,dn
2187 or imm32,dn -> or imm16,dn
2188 xor imm32,dn -> xor imm16,dn
2189 btst imm32,dn -> btst imm16,dn */
2205 /* cmp imm16, an zero-extends the immediate. */
2210 /* So do sp-based offsets. */
2211 if (code >= 0xb0 && code <= 0xb3
2215 /* Note that we've changed the relocation contents, etc. */
2216 elf_section_data (sec)->relocs = internal_relocs;
2217 elf_section_data (sec)->this_hdr.contents = contents;
2218 symtab_hdr->contents = (unsigned char *) isymbuf;
2220 /* Fix the opcode. */
2221 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2222 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2224 /* Fix the relocation's type. */
2225 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2228 /* Delete two bytes of data. */
2229 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2230 irel->r_offset + 2, 2))
2233 /* That will change things, so, we should relax again.
2234 Note that this is not required, and it may be slow. */
2238 else if (code == 0xfe)
2240 /* add imm32,sp -> add imm16,sp */
2242 /* Note that we've changed the relocation contents, etc. */
2243 elf_section_data (sec)->relocs = internal_relocs;
2244 elf_section_data (sec)->this_hdr.contents = contents;
2245 symtab_hdr->contents = (unsigned char *) isymbuf;
2247 /* Fix the opcode. */
2248 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2249 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2251 /* Fix the relocation's type. */
2252 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2255 /* Delete two bytes of data. */
2256 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2257 irel->r_offset + 2, 2))
2260 /* That will change things, so, we should relax again.
2261 Note that this is not required, and it may be slow. */
2270 && symtab_hdr->contents != (unsigned char *) isymbuf)
2272 if (! link_info->keep_memory)
2276 /* Cache the symbols for elf_link_input_bfd. */
2277 symtab_hdr->contents = (unsigned char *) isymbuf;
2281 if (contents != NULL
2282 && elf_section_data (sec)->this_hdr.contents != contents)
2284 if (! link_info->keep_memory)
2288 /* Cache the section contents for elf_link_input_bfd. */
2289 elf_section_data (sec)->this_hdr.contents = contents;
2293 if (internal_relocs != NULL
2294 && elf_section_data (sec)->relocs != internal_relocs)
2295 free (internal_relocs);
2301 && symtab_hdr->contents != (unsigned char *) isymbuf)
2303 if (contents != NULL
2304 && elf_section_data (section)->this_hdr.contents != contents)
2306 if (internal_relocs != NULL
2307 && elf_section_data (section)->relocs != internal_relocs)
2308 free (internal_relocs);
2313 /* Compute the stack size and movm arguments for the function
2314 referred to by HASH at address ADDR in section with
2315 contents CONTENTS, store the information in the hash table. */
2317 compute_function_info (abfd, hash, addr, contents)
2319 struct elf32_mn10300_link_hash_entry *hash;
2321 unsigned char *contents;
2323 unsigned char byte1, byte2;
2324 /* We only care about a very small subset of the possible prologue
2325 sequences here. Basically we look for:
2327 movm [d2,d3,a2,a3],sp (optional)
2328 add <size>,sp (optional, and only for sizes which fit in an unsigned
2331 If we find anything else, we quit. */
2333 /* Look for movm [regs],sp */
2334 byte1 = bfd_get_8 (abfd, contents + addr);
2335 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2339 hash->movm_args = byte2;
2341 byte1 = bfd_get_8 (abfd, contents + addr);
2342 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2345 /* Now figure out how much stack space will be allocated by the movm
2346 instruction. We need this kept separate from the funtion's normal
2348 if (hash->movm_args)
2351 if (hash->movm_args & 0x80)
2352 hash->movm_stack_size += 4;
2355 if (hash->movm_args & 0x40)
2356 hash->movm_stack_size += 4;
2359 if (hash->movm_args & 0x20)
2360 hash->movm_stack_size += 4;
2363 if (hash->movm_args & 0x10)
2364 hash->movm_stack_size += 4;
2366 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2367 if (hash->movm_args & 0x08)
2368 hash->movm_stack_size += 8 * 4;
2370 if (bfd_get_mach (abfd) == bfd_mach_am33)
2372 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2373 if (hash->movm_args & 0x1)
2374 hash->movm_stack_size += 6 * 4;
2376 /* exreg1 space. e4, e5, e6, e7 */
2377 if (hash->movm_args & 0x2)
2378 hash->movm_stack_size += 4 * 4;
2380 /* exreg0 space. e2, e3 */
2381 if (hash->movm_args & 0x4)
2382 hash->movm_stack_size += 2 * 4;
2386 /* Now look for the two stack adjustment variants. */
2387 if (byte1 == 0xf8 && byte2 == 0xfe)
2389 int temp = bfd_get_8 (abfd, contents + addr + 2);
2390 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2392 hash->stack_size = -temp;
2394 else if (byte1 == 0xfa && byte2 == 0xfe)
2396 int temp = bfd_get_16 (abfd, contents + addr + 2);
2397 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2401 hash->stack_size = temp;
2404 /* If the total stack to be allocated by the call instruction is more
2405 than 255 bytes, then we can't remove the stack adjustment by using
2406 "call" (we might still be able to remove the "movm" instruction. */
2407 if (hash->stack_size + hash->movm_stack_size > 255)
2408 hash->stack_size = 0;
2413 /* Delete some bytes from a section while relaxing. */
2416 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2422 Elf_Internal_Shdr *symtab_hdr;
2423 unsigned int sec_shndx;
2425 Elf_Internal_Rela *irel, *irelend;
2426 Elf_Internal_Rela *irelalign;
2428 Elf_Internal_Sym *isym, *isymend;
2429 struct elf_link_hash_entry **sym_hashes;
2430 struct elf_link_hash_entry **end_hashes;
2431 unsigned int symcount;
2433 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2435 contents = elf_section_data (sec)->this_hdr.contents;
2437 /* The deletion must stop at the next ALIGN reloc for an aligment
2438 power larger than the number of bytes we are deleting. */
2441 toaddr = sec->_cooked_size;
2443 irel = elf_section_data (sec)->relocs;
2444 irelend = irel + sec->reloc_count;
2446 /* Actually delete the bytes. */
2447 memmove (contents + addr, contents + addr + count,
2448 (size_t) (toaddr - addr - count));
2449 sec->_cooked_size -= count;
2451 /* Adjust all the relocs. */
2452 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2454 /* Get the new reloc address. */
2455 if ((irel->r_offset > addr
2456 && irel->r_offset < toaddr))
2457 irel->r_offset -= count;
2460 /* Adjust the local symbols defined in this section. */
2461 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2462 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2463 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2465 if (isym->st_shndx == sec_shndx
2466 && isym->st_value > addr
2467 && isym->st_value < toaddr)
2468 isym->st_value -= count;
2471 /* Now adjust the global symbols defined in this section. */
2472 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2473 - symtab_hdr->sh_info);
2474 sym_hashes = elf_sym_hashes (abfd);
2475 end_hashes = sym_hashes + symcount;
2476 for (; sym_hashes < end_hashes; sym_hashes++)
2478 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2479 if ((sym_hash->root.type == bfd_link_hash_defined
2480 || sym_hash->root.type == bfd_link_hash_defweak)
2481 && sym_hash->root.u.def.section == sec
2482 && sym_hash->root.u.def.value > addr
2483 && sym_hash->root.u.def.value < toaddr)
2485 sym_hash->root.u.def.value -= count;
2492 /* Return true if a symbol exists at the given address, else return
2495 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
2498 Elf_Internal_Sym *isym;
2501 Elf_Internal_Shdr *symtab_hdr;
2502 unsigned int sec_shndx;
2503 Elf_Internal_Sym *isymend;
2504 struct elf_link_hash_entry **sym_hashes;
2505 struct elf_link_hash_entry **end_hashes;
2506 unsigned int symcount;
2508 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2510 /* Examine all the symbols. */
2511 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2512 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2514 if (isym->st_shndx == sec_shndx
2515 && isym->st_value == addr)
2519 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2520 - symtab_hdr->sh_info);
2521 sym_hashes = elf_sym_hashes (abfd);
2522 end_hashes = sym_hashes + symcount;
2523 for (; sym_hashes < end_hashes; sym_hashes++)
2525 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2526 if ((sym_hash->root.type == bfd_link_hash_defined
2527 || sym_hash->root.type == bfd_link_hash_defweak)
2528 && sym_hash->root.u.def.section == sec
2529 && sym_hash->root.u.def.value == addr)
2536 /* This is a version of bfd_generic_get_relocated_section_contents
2537 which uses mn10300_elf_relocate_section. */
2540 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2541 data, relocateable, symbols)
2543 struct bfd_link_info *link_info;
2544 struct bfd_link_order *link_order;
2546 boolean relocateable;
2549 Elf_Internal_Shdr *symtab_hdr;
2550 asection *input_section = link_order->u.indirect.section;
2551 bfd *input_bfd = input_section->owner;
2552 asection **sections = NULL;
2553 Elf_Internal_Rela *internal_relocs = NULL;
2554 Elf_Internal_Sym *isymbuf = NULL;
2556 /* We only need to handle the case of relaxing, or of having a
2557 particular set of section contents, specially. */
2559 || elf_section_data (input_section)->this_hdr.contents == NULL)
2560 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2565 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2567 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2568 (size_t) input_section->_raw_size);
2570 if ((input_section->flags & SEC_RELOC) != 0
2571 && input_section->reloc_count > 0)
2574 Elf_Internal_Sym *isym, *isymend;
2577 internal_relocs = (_bfd_elf32_link_read_relocs
2578 (input_bfd, input_section, (PTR) NULL,
2579 (Elf_Internal_Rela *) NULL, false));
2580 if (internal_relocs == NULL)
2583 if (symtab_hdr->sh_info != 0)
2585 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2586 if (isymbuf == NULL)
2587 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2588 symtab_hdr->sh_info, 0,
2590 if (isymbuf == NULL)
2594 amt = symtab_hdr->sh_info;
2595 amt *= sizeof (asection *);
2596 sections = (asection **) bfd_malloc (amt);
2597 if (sections == NULL && amt != 0)
2600 isymend = isymbuf + symtab_hdr->sh_info;
2601 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2605 if (isym->st_shndx == SHN_UNDEF)
2606 isec = bfd_und_section_ptr;
2607 else if (isym->st_shndx == SHN_ABS)
2608 isec = bfd_abs_section_ptr;
2609 else if (isym->st_shndx == SHN_COMMON)
2610 isec = bfd_com_section_ptr;
2612 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2617 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2618 input_section, data, internal_relocs,
2622 if (sections != NULL)
2624 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2626 if (internal_relocs != elf_section_data (input_section)->relocs)
2627 free (internal_relocs);
2633 if (sections != NULL)
2635 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2637 if (internal_relocs != NULL
2638 && internal_relocs != elf_section_data (input_section)->relocs)
2639 free (internal_relocs);
2643 /* Assorted hash table functions. */
2645 /* Initialize an entry in the link hash table. */
2647 /* Create an entry in an MN10300 ELF linker hash table. */
2649 static struct bfd_hash_entry *
2650 elf32_mn10300_link_hash_newfunc (entry, table, string)
2651 struct bfd_hash_entry *entry;
2652 struct bfd_hash_table *table;
2655 struct elf32_mn10300_link_hash_entry *ret =
2656 (struct elf32_mn10300_link_hash_entry *) entry;
2658 /* Allocate the structure if it has not already been allocated by a
2660 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2661 ret = ((struct elf32_mn10300_link_hash_entry *)
2662 bfd_hash_allocate (table,
2663 sizeof (struct elf32_mn10300_link_hash_entry)));
2664 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2665 return (struct bfd_hash_entry *) ret;
2667 /* Call the allocation method of the superclass. */
2668 ret = ((struct elf32_mn10300_link_hash_entry *)
2669 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2671 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2673 ret->direct_calls = 0;
2674 ret->stack_size = 0;
2675 ret->movm_stack_size = 0;
2680 return (struct bfd_hash_entry *) ret;
2683 /* Create an mn10300 ELF linker hash table. */
2685 static struct bfd_link_hash_table *
2686 elf32_mn10300_link_hash_table_create (abfd)
2689 struct elf32_mn10300_link_hash_table *ret;
2690 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2692 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2693 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2696 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2697 elf32_mn10300_link_hash_newfunc))
2704 amt = sizeof (struct elf_link_hash_table);
2705 ret->static_hash_table
2706 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2707 if (ret->static_hash_table == NULL)
2713 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2714 elf32_mn10300_link_hash_newfunc))
2716 free (ret->static_hash_table);
2720 return &ret->root.root;
2723 /* Free an mn10300 ELF linker hash table. */
2726 elf32_mn10300_link_hash_table_free (hash)
2727 struct bfd_link_hash_table *hash;
2729 struct elf32_mn10300_link_hash_table *ret
2730 = (struct elf32_mn10300_link_hash_table *) hash;
2732 _bfd_generic_link_hash_table_free
2733 ((struct bfd_link_hash_table *) ret->static_hash_table);
2734 _bfd_generic_link_hash_table_free
2735 ((struct bfd_link_hash_table *) ret);
2738 static unsigned long
2739 elf_mn10300_mach (flags)
2742 switch (flags & EF_MN10300_MACH)
2744 case E_MN10300_MACH_MN10300:
2746 return bfd_mach_mn10300;
2748 case E_MN10300_MACH_AM33:
2749 return bfd_mach_am33;
2753 /* The final processing done just before writing out a MN10300 ELF object
2754 file. This gets the MN10300 architecture right based on the machine
2758 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2760 boolean linker ATTRIBUTE_UNUSED;
2764 switch (bfd_get_mach (abfd))
2767 case bfd_mach_mn10300:
2768 val = E_MN10300_MACH_MN10300;
2772 val = E_MN10300_MACH_AM33;
2776 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2777 elf_elfheader (abfd)->e_flags |= val;
2781 _bfd_mn10300_elf_object_p (abfd)
2784 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2785 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2789 /* Merge backend specific data from an object file to the output
2790 object file when linking. */
2793 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2797 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2798 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2801 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2802 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2804 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2805 bfd_get_mach (ibfd)))
2812 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2813 #define TARGET_LITTLE_NAME "elf32-mn10300"
2814 #define ELF_ARCH bfd_arch_mn10300
2815 #define ELF_MACHINE_CODE EM_MN10300
2816 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
2817 #define ELF_MAXPAGESIZE 0x1000
2819 #define elf_info_to_howto mn10300_info_to_howto
2820 #define elf_info_to_howto_rel 0
2821 #define elf_backend_can_gc_sections 1
2822 #define elf_backend_rela_normal 1
2823 #define elf_backend_check_relocs mn10300_elf_check_relocs
2824 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2825 #define elf_backend_relocate_section mn10300_elf_relocate_section
2826 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2827 #define bfd_elf32_bfd_get_relocated_section_contents \
2828 mn10300_elf_get_relocated_section_contents
2829 #define bfd_elf32_bfd_link_hash_table_create \
2830 elf32_mn10300_link_hash_table_create
2831 #define bfd_elf32_bfd_link_hash_table_free \
2832 elf32_mn10300_link_hash_table_free
2834 #define elf_symbol_leading_char '_'
2836 /* So we can set bits in e_flags. */
2837 #define elf_backend_final_write_processing \
2838 _bfd_mn10300_elf_final_write_processing
2839 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2841 #define bfd_elf32_bfd_merge_private_bfd_data \
2842 _bfd_mn10300_elf_merge_private_bfd_data
2844 #include "elf32-target.h"