1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
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 bfd_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 bfd_boolean mn10300_elf_relax_section
35 PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_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 *, bfd_boolean, asymbol **));
39 static unsigned long elf_mn10300_mach
41 void _bfd_mn10300_elf_final_write_processing
42 PARAMS ((bfd *, bfd_boolean));
43 bfd_boolean _bfd_mn10300_elf_object_p
45 bfd_boolean _bfd_mn10300_elf_merge_private_bfd_data
46 PARAMS ((bfd *,bfd *));
48 struct elf32_mn10300_link_hash_entry {
49 /* The basic elf link hash table entry. */
50 struct elf_link_hash_entry root;
52 /* For function symbols, the number of times this function is
53 called directly (ie by name). */
54 unsigned int direct_calls;
56 /* For function symbols, the size of this function's stack
57 (if <= 255 bytes). We stuff this into "call" instructions
58 to this target when it's valid and profitable to do so.
60 This does not include stack allocated by movm! */
61 unsigned char stack_size;
63 /* For function symbols, arguments (if any) for movm instruction
64 in the prologue. We stuff this value into "call" instructions
65 to the target when it's valid and profitable to do so. */
66 unsigned char movm_args;
68 /* For funtion symbols, the amount of stack space that would be allocated
69 by the movm instruction. This is redundant with movm_args, but we
70 add it to the hash table to avoid computing it over and over. */
71 unsigned char movm_stack_size;
73 /* When set, convert all "call" instructions to this target into "calls"
75 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
77 /* Used to mark functions which have had redundant parts of their
79 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
83 /* We derive a hash table from the main elf linker hash table so
84 we can store state variables and a secondary hash table without
85 resorting to global variables. */
86 struct elf32_mn10300_link_hash_table {
87 /* The main hash table. */
88 struct elf_link_hash_table root;
90 /* A hash table for static functions. We could derive a new hash table
91 instead of using the full elf32_mn10300_link_hash_table if we wanted
92 to save some memory. */
93 struct elf32_mn10300_link_hash_table *static_hash_table;
95 /* Random linker state flags. */
96 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
100 /* For MN10300 linker hash table. */
102 /* Get the MN10300 ELF linker hash table from a link_info structure. */
104 #define elf32_mn10300_hash_table(p) \
105 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
107 #define elf32_mn10300_link_hash_traverse(table, func, info) \
108 (elf_link_hash_traverse \
110 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
113 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
114 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
115 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
117 static void elf32_mn10300_link_hash_table_free
118 PARAMS ((struct bfd_link_hash_table *));
120 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
121 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
122 static void mn10300_info_to_howto
123 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
124 static bfd_boolean mn10300_elf_check_relocs
125 PARAMS ((bfd *, struct bfd_link_info *, asection *,
126 const Elf_Internal_Rela *));
127 static asection *mn10300_elf_gc_mark_hook
128 PARAMS ((asection *, struct bfd_link_info *info, Elf_Internal_Rela *,
129 struct elf_link_hash_entry *, Elf_Internal_Sym *));
130 static bfd_boolean mn10300_elf_relax_delete_bytes
131 PARAMS ((bfd *, asection *, bfd_vma, int));
132 static bfd_boolean mn10300_elf_symbol_address_p
133 PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
134 static bfd_boolean elf32_mn10300_finish_hash_table_entry
135 PARAMS ((struct bfd_hash_entry *, PTR));
136 static void compute_function_info
137 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
138 bfd_vma, unsigned char *));
140 static reloc_howto_type elf_mn10300_howto_table[] = {
141 /* Dummy relocation. Does nothing. */
142 HOWTO (R_MN10300_NONE,
148 complain_overflow_bitfield,
149 bfd_elf_generic_reloc,
155 /* Standard 32 bit reloc. */
162 complain_overflow_bitfield,
163 bfd_elf_generic_reloc,
169 /* Standard 16 bit reloc. */
176 complain_overflow_bitfield,
177 bfd_elf_generic_reloc,
183 /* Standard 8 bit reloc. */
190 complain_overflow_bitfield,
191 bfd_elf_generic_reloc,
197 /* Standard 32bit pc-relative reloc. */
198 HOWTO (R_MN10300_PCREL32,
204 complain_overflow_bitfield,
205 bfd_elf_generic_reloc,
211 /* Standard 16bit pc-relative reloc. */
212 HOWTO (R_MN10300_PCREL16,
218 complain_overflow_bitfield,
219 bfd_elf_generic_reloc,
225 /* Standard 8 pc-relative reloc. */
226 HOWTO (R_MN10300_PCREL8,
232 complain_overflow_bitfield,
233 bfd_elf_generic_reloc,
240 /* GNU extension to record C++ vtable hierarchy */
241 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
243 0, /* size (0 = byte, 1 = short, 2 = long) */
245 FALSE, /* pc_relative */
247 complain_overflow_dont, /* complain_on_overflow */
248 NULL, /* special_function */
249 "R_MN10300_GNU_VTINHERIT", /* name */
250 FALSE, /* partial_inplace */
253 FALSE), /* pcrel_offset */
255 /* GNU extension to record C++ vtable member usage */
256 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
258 0, /* size (0 = byte, 1 = short, 2 = long) */
260 FALSE, /* pc_relative */
262 complain_overflow_dont, /* complain_on_overflow */
263 NULL, /* special_function */
264 "R_MN10300_GNU_VTENTRY", /* name */
265 FALSE, /* partial_inplace */
268 FALSE), /* pcrel_offset */
270 /* Standard 24 bit reloc. */
277 complain_overflow_bitfield,
278 bfd_elf_generic_reloc,
286 struct mn10300_reloc_map {
287 bfd_reloc_code_real_type bfd_reloc_val;
288 unsigned char elf_reloc_val;
291 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
292 { BFD_RELOC_NONE, R_MN10300_NONE, },
293 { BFD_RELOC_32, R_MN10300_32, },
294 { BFD_RELOC_16, R_MN10300_16, },
295 { BFD_RELOC_8, R_MN10300_8, },
296 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
297 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
298 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
299 { BFD_RELOC_24, R_MN10300_24, },
300 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
301 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
304 static reloc_howto_type *
305 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
306 bfd *abfd ATTRIBUTE_UNUSED;
307 bfd_reloc_code_real_type code;
312 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
315 if (mn10300_reloc_map[i].bfd_reloc_val == code)
316 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
322 /* Set the howto pointer for an MN10300 ELF reloc. */
325 mn10300_info_to_howto (abfd, cache_ptr, dst)
326 bfd *abfd ATTRIBUTE_UNUSED;
328 Elf_Internal_Rela *dst;
332 r_type = ELF32_R_TYPE (dst->r_info);
333 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
334 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
337 /* Look through the relocs for a section during the first phase.
338 Since we don't do .gots or .plts, we just need to consider the
339 virtual table relocs for gc. */
342 mn10300_elf_check_relocs (abfd, info, sec, relocs)
344 struct bfd_link_info *info;
346 const Elf_Internal_Rela *relocs;
348 Elf_Internal_Shdr *symtab_hdr;
349 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
350 const Elf_Internal_Rela *rel;
351 const Elf_Internal_Rela *rel_end;
353 if (info->relocatable)
356 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
357 sym_hashes = elf_sym_hashes (abfd);
358 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
359 if (!elf_bad_symtab (abfd))
360 sym_hashes_end -= symtab_hdr->sh_info;
362 rel_end = relocs + sec->reloc_count;
363 for (rel = relocs; rel < rel_end; rel++)
365 struct elf_link_hash_entry *h;
366 unsigned long r_symndx;
368 r_symndx = ELF32_R_SYM (rel->r_info);
369 if (r_symndx < symtab_hdr->sh_info)
372 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
374 switch (ELF32_R_TYPE (rel->r_info))
376 /* This relocation describes the C++ object vtable hierarchy.
377 Reconstruct it for later use during GC. */
378 case R_MN10300_GNU_VTINHERIT:
379 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
383 /* This relocation describes which C++ vtable entries are actually
384 used. Record for later use during GC. */
385 case R_MN10300_GNU_VTENTRY:
386 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
395 /* Return the section that should be marked against GC for a given
399 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
401 struct bfd_link_info *info ATTRIBUTE_UNUSED;
402 Elf_Internal_Rela *rel;
403 struct elf_link_hash_entry *h;
404 Elf_Internal_Sym *sym;
408 switch (ELF32_R_TYPE (rel->r_info))
410 case R_MN10300_GNU_VTINHERIT:
411 case R_MN10300_GNU_VTENTRY:
415 switch (h->root.type)
417 case bfd_link_hash_defined:
418 case bfd_link_hash_defweak:
419 return h->root.u.def.section;
421 case bfd_link_hash_common:
422 return h->root.u.c.p->section;
430 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
435 /* Perform a relocation as part of a final link. */
436 static bfd_reloc_status_type
437 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
438 input_section, contents, offset, value,
439 addend, info, sym_sec, is_local)
440 reloc_howto_type *howto;
442 bfd *output_bfd ATTRIBUTE_UNUSED;
443 asection *input_section;
448 struct bfd_link_info *info ATTRIBUTE_UNUSED;
449 asection *sym_sec ATTRIBUTE_UNUSED;
450 int is_local ATTRIBUTE_UNUSED;
452 unsigned long r_type = howto->type;
453 bfd_byte *hit_data = contents + offset;
462 bfd_put_32 (input_bfd, value, hit_data);
468 if ((long) value > 0x7fffff || (long) value < -0x800000)
469 return bfd_reloc_overflow;
471 bfd_put_8 (input_bfd, value & 0xff, hit_data);
472 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
473 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
479 if ((long) value > 0x7fff || (long) value < -0x8000)
480 return bfd_reloc_overflow;
482 bfd_put_16 (input_bfd, value, hit_data);
488 if ((long) value > 0x7f || (long) value < -0x80)
489 return bfd_reloc_overflow;
491 bfd_put_8 (input_bfd, value, hit_data);
494 case R_MN10300_PCREL8:
495 value -= (input_section->output_section->vma
496 + input_section->output_offset);
500 if ((long) value > 0xff || (long) value < -0x100)
501 return bfd_reloc_overflow;
503 bfd_put_8 (input_bfd, value, hit_data);
506 case R_MN10300_PCREL16:
507 value -= (input_section->output_section->vma
508 + input_section->output_offset);
512 if ((long) value > 0xffff || (long) value < -0x10000)
513 return bfd_reloc_overflow;
515 bfd_put_16 (input_bfd, value, hit_data);
518 case R_MN10300_PCREL32:
519 value -= (input_section->output_section->vma
520 + input_section->output_offset);
524 bfd_put_32 (input_bfd, value, hit_data);
527 case R_MN10300_GNU_VTINHERIT:
528 case R_MN10300_GNU_VTENTRY:
532 return bfd_reloc_notsupported;
536 /* Relocate an MN10300 ELF section. */
538 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
539 contents, relocs, local_syms, local_sections)
541 struct bfd_link_info *info;
543 asection *input_section;
545 Elf_Internal_Rela *relocs;
546 Elf_Internal_Sym *local_syms;
547 asection **local_sections;
549 Elf_Internal_Shdr *symtab_hdr;
550 struct elf32_mn10300_link_hash_entry **sym_hashes;
551 Elf_Internal_Rela *rel, *relend;
553 if (info->relocatable)
556 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
557 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
558 (elf_sym_hashes (input_bfd));
561 relend = relocs + input_section->reloc_count;
562 for (; rel < relend; rel++)
565 reloc_howto_type *howto;
566 unsigned long r_symndx;
567 Elf_Internal_Sym *sym;
569 struct elf32_mn10300_link_hash_entry *h;
571 bfd_reloc_status_type r;
573 r_symndx = ELF32_R_SYM (rel->r_info);
574 r_type = ELF32_R_TYPE (rel->r_info);
575 howto = elf_mn10300_howto_table + r_type;
577 /* Just skip the vtable gc relocs. */
578 if (r_type == R_MN10300_GNU_VTINHERIT
579 || r_type == R_MN10300_GNU_VTENTRY)
585 if (r_symndx < symtab_hdr->sh_info)
587 sym = local_syms + r_symndx;
588 sec = local_sections[r_symndx];
589 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
593 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
594 while (h->root.root.type == bfd_link_hash_indirect
595 || h->root.root.type == bfd_link_hash_warning)
596 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
597 if (h->root.root.type == bfd_link_hash_defined
598 || h->root.root.type == bfd_link_hash_defweak)
600 sec = h->root.root.u.def.section;
601 relocation = (h->root.root.u.def.value
602 + sec->output_section->vma
603 + sec->output_offset);
605 else if (h->root.root.type == bfd_link_hash_undefweak)
609 if (! ((*info->callbacks->undefined_symbol)
610 (info, h->root.root.root.string, input_bfd,
611 input_section, rel->r_offset, TRUE)))
617 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
619 contents, rel->r_offset,
620 relocation, rel->r_addend,
621 info, sec, h == NULL);
623 if (r != bfd_reloc_ok)
626 const char *msg = (const char *) 0;
629 name = h->root.root.root.string;
632 name = (bfd_elf_string_from_elf_section
633 (input_bfd, symtab_hdr->sh_link, sym->st_name));
634 if (name == NULL || *name == '\0')
635 name = bfd_section_name (input_bfd, sec);
640 case bfd_reloc_overflow:
641 if (! ((*info->callbacks->reloc_overflow)
642 (info, name, howto->name, (bfd_vma) 0,
643 input_bfd, input_section, rel->r_offset)))
647 case bfd_reloc_undefined:
648 if (! ((*info->callbacks->undefined_symbol)
649 (info, name, input_bfd, input_section,
650 rel->r_offset, TRUE)))
654 case bfd_reloc_outofrange:
655 msg = _("internal error: out of range error");
658 case bfd_reloc_notsupported:
659 msg = _("internal error: unsupported relocation error");
662 case bfd_reloc_dangerous:
663 msg = _("internal error: dangerous error");
667 msg = _("internal error: unknown error");
671 if (!((*info->callbacks->warning)
672 (info, msg, name, input_bfd, input_section,
683 /* Finish initializing one hash table entry. */
685 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
686 struct bfd_hash_entry *gen_entry;
687 PTR in_args ATTRIBUTE_UNUSED;
689 struct elf32_mn10300_link_hash_entry *entry;
690 unsigned int byte_count = 0;
692 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
694 if (entry->root.root.type == bfd_link_hash_warning)
695 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
697 /* If we already know we want to convert "call" to "calls" for calls
698 to this symbol, then return now. */
699 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
702 /* If there are no named calls to this symbol, or there's nothing we
703 can move from the function itself into the "call" instruction, then
704 note that all "call" instructions should be converted into "calls"
705 instructions and return. */
706 if (entry->direct_calls == 0
707 || (entry->stack_size == 0 && entry->movm_args == 0))
709 /* Make a note that we should convert "call" instructions to "calls"
710 instructions for calls to this symbol. */
711 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
715 /* We may be able to move some instructions from the function itself into
716 the "call" instruction. Count how many bytes we might be able to
717 eliminate in the function itself. */
719 /* A movm instruction is two bytes. */
720 if (entry->movm_args)
723 /* Count the insn to allocate stack space too. */
724 if (entry->stack_size > 0 && entry->stack_size <= 128)
726 else if (entry->stack_size > 0 && entry->stack_size < 256)
729 /* If using "call" will result in larger code, then turn all
730 the associated "call" instructions into "calls" instrutions. */
731 if (byte_count < entry->direct_calls)
732 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
734 /* This routine never fails. */
738 /* This function handles relaxing for the mn10300.
740 There's quite a few relaxing opportunites available on the mn10300:
742 * calls:32 -> calls:16 2 bytes
743 * call:32 -> call:16 2 bytes
745 * call:32 -> calls:32 1 byte
746 * call:16 -> calls:16 1 byte
747 * These are done anytime using "calls" would result
748 in smaller code, or when necessary to preserve the
749 meaning of the program.
753 * In some circumstances we can move instructions
754 from a function prologue into a "call" instruction.
755 This is only done if the resulting code is no larger
756 than the original code.
758 * jmp:32 -> jmp:16 2 bytes
759 * jmp:16 -> bra:8 1 byte
761 * If the previous instruction is a conditional branch
762 around the jump/bra, we may be able to reverse its condition
763 and change its target to the jump's target. The jump/bra
764 can then be deleted. 2 bytes
766 * mov abs32 -> mov abs16 1 or 2 bytes
768 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
769 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
771 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
772 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
774 We don't handle imm16->imm8 or d16->d8 as they're very rare
775 and somewhat more difficult to support. */
778 mn10300_elf_relax_section (abfd, sec, link_info, again)
781 struct bfd_link_info *link_info;
784 Elf_Internal_Shdr *symtab_hdr;
785 Elf_Internal_Rela *internal_relocs = NULL;
786 Elf_Internal_Rela *irel, *irelend;
787 bfd_byte *contents = NULL;
788 Elf_Internal_Sym *isymbuf = NULL;
789 struct elf32_mn10300_link_hash_table *hash_table;
790 asection *section = sec;
792 /* Assume nothing changes. */
795 /* We need a pointer to the mn10300 specific hash table. */
796 hash_table = elf32_mn10300_hash_table (link_info);
798 /* Initialize fields in each hash table entry the first time through. */
799 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
803 /* Iterate over all the input bfds. */
804 for (input_bfd = link_info->input_bfds;
806 input_bfd = input_bfd->link_next)
808 /* We're going to need all the symbols for each bfd. */
809 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
810 if (symtab_hdr->sh_info != 0)
812 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
814 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
815 symtab_hdr->sh_info, 0,
821 /* Iterate over each section in this bfd. */
822 for (section = input_bfd->sections;
824 section = section->next)
826 struct elf32_mn10300_link_hash_entry *hash;
827 Elf_Internal_Sym *sym;
828 asection *sym_sec = NULL;
829 const char *sym_name;
832 /* If there's nothing to do in this section, skip it. */
833 if (! (((section->flags & SEC_RELOC) != 0
834 && section->reloc_count != 0)
835 || (section->flags & SEC_CODE) != 0))
838 /* Get cached copy of section contents if it exists. */
839 if (elf_section_data (section)->this_hdr.contents != NULL)
840 contents = elf_section_data (section)->this_hdr.contents;
841 else if (section->_raw_size != 0)
843 /* Go get them off disk. */
844 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
845 if (contents == NULL)
848 if (!bfd_get_section_contents (input_bfd, section,
849 contents, (file_ptr) 0,
856 /* If there aren't any relocs, then there's nothing to do. */
857 if ((section->flags & SEC_RELOC) != 0
858 && section->reloc_count != 0)
861 /* Get a copy of the native relocations. */
862 internal_relocs = (_bfd_elf_link_read_relocs
863 (input_bfd, section, (PTR) NULL,
864 (Elf_Internal_Rela *) NULL,
865 link_info->keep_memory));
866 if (internal_relocs == NULL)
869 /* Now examine each relocation. */
870 irel = internal_relocs;
871 irelend = irel + section->reloc_count;
872 for (; irel < irelend; irel++)
875 unsigned long r_index;
878 r_type = ELF32_R_TYPE (irel->r_info);
879 r_index = ELF32_R_SYM (irel->r_info);
881 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
884 /* We need the name and hash table entry of the target
890 if (r_index < symtab_hdr->sh_info)
892 /* A local symbol. */
893 Elf_Internal_Sym *isym;
894 struct elf_link_hash_table *elftab;
897 isym = isymbuf + r_index;
898 if (isym->st_shndx == SHN_UNDEF)
899 sym_sec = bfd_und_section_ptr;
900 else if (isym->st_shndx == SHN_ABS)
901 sym_sec = bfd_abs_section_ptr;
902 else if (isym->st_shndx == SHN_COMMON)
903 sym_sec = bfd_com_section_ptr;
906 = bfd_section_from_elf_index (input_bfd,
910 = bfd_elf_string_from_elf_section (input_bfd,
915 /* If it isn't a function, then we don't care
917 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
920 /* Tack on an ID so we can uniquely identify this
921 local symbol in the global hash table. */
922 amt = strlen (sym_name) + 10;
923 new_name = bfd_malloc (amt);
927 sprintf (new_name, "%s_%08x",
928 sym_name, (int) sym_sec);
931 elftab = &hash_table->static_hash_table->root;
932 hash = ((struct elf32_mn10300_link_hash_entry *)
933 elf_link_hash_lookup (elftab, sym_name,
939 r_index -= symtab_hdr->sh_info;
940 hash = (struct elf32_mn10300_link_hash_entry *)
941 elf_sym_hashes (input_bfd)[r_index];
944 /* If this is not a "call" instruction, then we
945 should convert "call" instructions to "calls"
947 code = bfd_get_8 (input_bfd,
948 contents + irel->r_offset - 1);
949 if (code != 0xdd && code != 0xcd)
950 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
952 /* If this is a jump/call, then bump the
953 direct_calls counter. Else force "call" to
954 "calls" conversions. */
955 if (r_type == R_MN10300_PCREL32
956 || r_type == R_MN10300_PCREL16)
957 hash->direct_calls++;
959 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
963 /* Now look at the actual contents to get the stack size,
964 and a list of what registers were saved in the prologue
966 if ((section->flags & SEC_CODE) != 0)
968 Elf_Internal_Sym *isym, *isymend;
969 unsigned int sec_shndx;
970 struct elf_link_hash_entry **hashes;
971 struct elf_link_hash_entry **end_hashes;
972 unsigned int symcount;
974 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
977 /* Look at each function defined in this section and
978 update info for that function. */
979 isymend = isymbuf + symtab_hdr->sh_info;
980 for (isym = isymbuf; isym < isymend; isym++)
982 if (isym->st_shndx == sec_shndx
983 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
985 struct elf_link_hash_table *elftab;
988 if (isym->st_shndx == SHN_UNDEF)
989 sym_sec = bfd_und_section_ptr;
990 else if (isym->st_shndx == SHN_ABS)
991 sym_sec = bfd_abs_section_ptr;
992 else if (isym->st_shndx == SHN_COMMON)
993 sym_sec = bfd_com_section_ptr;
996 = bfd_section_from_elf_index (input_bfd,
999 sym_name = (bfd_elf_string_from_elf_section
1000 (input_bfd, symtab_hdr->sh_link,
1003 /* Tack on an ID so we can uniquely identify this
1004 local symbol in the global hash table. */
1005 amt = strlen (sym_name) + 10;
1006 new_name = bfd_malloc (amt);
1010 sprintf (new_name, "%s_%08x",
1011 sym_name, (int) sym_sec);
1012 sym_name = new_name;
1014 elftab = &hash_table->static_hash_table->root;
1015 hash = ((struct elf32_mn10300_link_hash_entry *)
1016 elf_link_hash_lookup (elftab, sym_name,
1017 TRUE, TRUE, FALSE));
1019 compute_function_info (input_bfd, hash,
1020 isym->st_value, contents);
1024 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1025 - symtab_hdr->sh_info);
1026 hashes = elf_sym_hashes (abfd);
1027 end_hashes = hashes + symcount;
1028 for (; hashes < end_hashes; hashes++)
1030 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1031 if ((hash->root.root.type == bfd_link_hash_defined
1032 || hash->root.root.type == bfd_link_hash_defweak)
1033 && hash->root.root.u.def.section == section
1034 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1035 compute_function_info (input_bfd, hash,
1036 (hash)->root.root.u.def.value,
1041 /* Cache or free any memory we allocated for the relocs. */
1042 if (internal_relocs != NULL
1043 && elf_section_data (section)->relocs != internal_relocs)
1044 free (internal_relocs);
1045 internal_relocs = NULL;
1047 /* Cache or free any memory we allocated for the contents. */
1048 if (contents != NULL
1049 && elf_section_data (section)->this_hdr.contents != contents)
1051 if (! link_info->keep_memory)
1055 /* Cache the section contents for elf_link_input_bfd. */
1056 elf_section_data (section)->this_hdr.contents = contents;
1062 /* Cache or free any memory we allocated for the symbols. */
1064 && symtab_hdr->contents != (unsigned char *) isymbuf)
1066 if (! link_info->keep_memory)
1070 /* Cache the symbols for elf_link_input_bfd. */
1071 symtab_hdr->contents = (unsigned char *) isymbuf;
1077 /* Now iterate on each symbol in the hash table and perform
1078 the final initialization steps on each. */
1079 elf32_mn10300_link_hash_traverse (hash_table,
1080 elf32_mn10300_finish_hash_table_entry,
1082 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1083 elf32_mn10300_finish_hash_table_entry,
1086 /* All entries in the hash table are fully initialized. */
1087 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1089 /* Now that everything has been initialized, go through each
1090 code section and delete any prologue insns which will be
1091 redundant because their operations will be performed by
1092 a "call" instruction. */
1093 for (input_bfd = link_info->input_bfds;
1095 input_bfd = input_bfd->link_next)
1097 /* We're going to need all the local symbols for each bfd. */
1098 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1099 if (symtab_hdr->sh_info != 0)
1101 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1102 if (isymbuf == NULL)
1103 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1104 symtab_hdr->sh_info, 0,
1106 if (isymbuf == NULL)
1110 /* Walk over each section in this bfd. */
1111 for (section = input_bfd->sections;
1113 section = section->next)
1115 unsigned int sec_shndx;
1116 Elf_Internal_Sym *isym, *isymend;
1117 struct elf_link_hash_entry **hashes;
1118 struct elf_link_hash_entry **end_hashes;
1119 unsigned int symcount;
1121 /* Skip non-code sections and empty sections. */
1122 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1125 if (section->reloc_count != 0)
1127 /* Get a copy of the native relocations. */
1128 internal_relocs = (_bfd_elf_link_read_relocs
1129 (input_bfd, section, (PTR) NULL,
1130 (Elf_Internal_Rela *) NULL,
1131 link_info->keep_memory));
1132 if (internal_relocs == NULL)
1136 /* Get cached copy of section contents if it exists. */
1137 if (elf_section_data (section)->this_hdr.contents != NULL)
1138 contents = elf_section_data (section)->this_hdr.contents;
1141 /* Go get them off disk. */
1142 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1143 if (contents == NULL)
1146 if (!bfd_get_section_contents (input_bfd, section,
1147 contents, (file_ptr) 0,
1148 section->_raw_size))
1152 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1155 /* Now look for any function in this section which needs
1156 insns deleted from its prologue. */
1157 isymend = isymbuf + symtab_hdr->sh_info;
1158 for (isym = isymbuf; isym < isymend; isym++)
1160 struct elf32_mn10300_link_hash_entry *sym_hash;
1161 asection *sym_sec = NULL;
1162 const char *sym_name;
1164 struct elf_link_hash_table *elftab;
1167 if (isym->st_shndx != sec_shndx)
1170 if (isym->st_shndx == SHN_UNDEF)
1171 sym_sec = bfd_und_section_ptr;
1172 else if (isym->st_shndx == SHN_ABS)
1173 sym_sec = bfd_abs_section_ptr;
1174 else if (isym->st_shndx == SHN_COMMON)
1175 sym_sec = bfd_com_section_ptr;
1178 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1181 = bfd_elf_string_from_elf_section (input_bfd,
1182 symtab_hdr->sh_link,
1185 /* Tack on an ID so we can uniquely identify this
1186 local symbol in the global hash table. */
1187 amt = strlen (sym_name) + 10;
1188 new_name = bfd_malloc (amt);
1191 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1192 sym_name = new_name;
1194 elftab = &hash_table->static_hash_table->root;
1195 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1196 elf_link_hash_lookup (elftab, sym_name,
1197 FALSE, FALSE, FALSE));
1200 if (sym_hash == NULL)
1203 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1204 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1208 /* Note that we've changed things. */
1209 elf_section_data (section)->relocs = internal_relocs;
1210 elf_section_data (section)->this_hdr.contents = contents;
1211 symtab_hdr->contents = (unsigned char *) isymbuf;
1213 /* Count how many bytes we're going to delete. */
1214 if (sym_hash->movm_args)
1217 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1219 else if (sym_hash->stack_size
1220 && sym_hash->stack_size < 256)
1223 /* Note that we've deleted prologue bytes for this
1225 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1227 /* Actually delete the bytes. */
1228 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1234 /* Something changed. Not strictly necessary, but
1235 may lead to more relaxing opportunities. */
1240 /* Look for any global functions in this section which
1241 need insns deleted from their prologues. */
1242 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1243 - symtab_hdr->sh_info);
1244 hashes = elf_sym_hashes (abfd);
1245 end_hashes = hashes + symcount;
1246 for (; hashes < end_hashes; hashes++)
1248 struct elf32_mn10300_link_hash_entry *sym_hash;
1250 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1251 if ((sym_hash->root.root.type == bfd_link_hash_defined
1252 || sym_hash->root.root.type == bfd_link_hash_defweak)
1253 && sym_hash->root.root.u.def.section == section
1254 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1255 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1260 /* Note that we've changed things. */
1261 elf_section_data (section)->relocs = internal_relocs;
1262 elf_section_data (section)->this_hdr.contents = contents;
1263 symtab_hdr->contents = (unsigned char *) isymbuf;
1265 /* Count how many bytes we're going to delete. */
1266 if (sym_hash->movm_args)
1269 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1271 else if (sym_hash->stack_size
1272 && sym_hash->stack_size < 256)
1275 /* Note that we've deleted prologue bytes for this
1277 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1279 /* Actually delete the bytes. */
1280 symval = sym_hash->root.root.u.def.value;
1281 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1287 /* Something changed. Not strictly necessary, but
1288 may lead to more relaxing opportunities. */
1293 /* Cache or free any memory we allocated for the relocs. */
1294 if (internal_relocs != NULL
1295 && elf_section_data (section)->relocs != internal_relocs)
1296 free (internal_relocs);
1297 internal_relocs = NULL;
1299 /* Cache or free any memory we allocated for the contents. */
1300 if (contents != NULL
1301 && elf_section_data (section)->this_hdr.contents != contents)
1303 if (! link_info->keep_memory)
1307 /* Cache the section contents for elf_link_input_bfd. */
1308 elf_section_data (section)->this_hdr.contents = contents;
1314 /* Cache or free any memory we allocated for the symbols. */
1316 && symtab_hdr->contents != (unsigned char *) isymbuf)
1318 if (! link_info->keep_memory)
1322 /* Cache the symbols for elf_link_input_bfd. */
1323 symtab_hdr->contents = (unsigned char *) isymbuf;
1330 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1332 internal_relocs = NULL;
1334 /* For error_return. */
1337 /* We don't have to do anything for a relocatable link, if
1338 this section does not have relocs, or if this is not a
1340 if (link_info->relocatable
1341 || (sec->flags & SEC_RELOC) == 0
1342 || sec->reloc_count == 0
1343 || (sec->flags & SEC_CODE) == 0)
1346 /* If this is the first time we have been called for this section,
1347 initialize the cooked size. */
1348 if (sec->_cooked_size == 0)
1349 sec->_cooked_size = sec->_raw_size;
1351 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1353 /* Get a copy of the native relocations. */
1354 internal_relocs = (_bfd_elf_link_read_relocs
1355 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1356 link_info->keep_memory));
1357 if (internal_relocs == NULL)
1360 /* Walk through them looking for relaxing opportunities. */
1361 irelend = internal_relocs + sec->reloc_count;
1362 for (irel = internal_relocs; irel < irelend; irel++)
1365 struct elf32_mn10300_link_hash_entry *h = NULL;
1367 /* If this isn't something that can be relaxed, then ignore
1369 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1370 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1371 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1374 /* Get the section contents if we haven't done so already. */
1375 if (contents == NULL)
1377 /* Get cached copy if it exists. */
1378 if (elf_section_data (sec)->this_hdr.contents != NULL)
1379 contents = elf_section_data (sec)->this_hdr.contents;
1382 /* Go get them off disk. */
1383 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1384 if (contents == NULL)
1387 if (! bfd_get_section_contents (abfd, sec, contents,
1388 (file_ptr) 0, sec->_raw_size))
1393 /* Read this BFD's symbols if we haven't done so already. */
1394 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1396 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1397 if (isymbuf == NULL)
1398 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1399 symtab_hdr->sh_info, 0,
1401 if (isymbuf == NULL)
1405 /* Get the value of the symbol referred to by the reloc. */
1406 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1408 Elf_Internal_Sym *isym;
1409 asection *sym_sec = NULL;
1410 const char *sym_name;
1413 /* A local symbol. */
1414 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1415 if (isym->st_shndx == SHN_UNDEF)
1416 sym_sec = bfd_und_section_ptr;
1417 else if (isym->st_shndx == SHN_ABS)
1418 sym_sec = bfd_abs_section_ptr;
1419 else if (isym->st_shndx == SHN_COMMON)
1420 sym_sec = bfd_com_section_ptr;
1422 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1424 symval = (isym->st_value
1425 + sym_sec->output_section->vma
1426 + sym_sec->output_offset);
1427 sym_name = bfd_elf_string_from_elf_section (abfd,
1428 symtab_hdr->sh_link,
1431 /* Tack on an ID so we can uniquely identify this
1432 local symbol in the global hash table. */
1433 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1436 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1437 sym_name = new_name;
1439 h = (struct elf32_mn10300_link_hash_entry *)
1440 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1441 sym_name, FALSE, FALSE, FALSE);
1448 /* An external symbol. */
1449 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1450 h = (struct elf32_mn10300_link_hash_entry *)
1451 (elf_sym_hashes (abfd)[indx]);
1452 BFD_ASSERT (h != NULL);
1453 if (h->root.root.type != bfd_link_hash_defined
1454 && h->root.root.type != bfd_link_hash_defweak)
1456 /* This appears to be a reference to an undefined
1457 symbol. Just ignore it--it will be caught by the
1458 regular reloc processing. */
1462 symval = (h->root.root.u.def.value
1463 + h->root.root.u.def.section->output_section->vma
1464 + h->root.root.u.def.section->output_offset);
1467 /* For simplicity of coding, we are going to modify the section
1468 contents, the section relocs, and the BFD symbol table. We
1469 must tell the rest of the code not to free up this
1470 information. It would be possible to instead create a table
1471 of changes which have to be made, as is done in coff-mips.c;
1472 that would be more work, but would require less memory when
1473 the linker is run. */
1475 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1476 branch/call, also deal with "call" -> "calls" conversions and
1477 insertion of prologue data into "call" instructions. */
1478 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1480 bfd_vma value = symval;
1482 /* If we've got a "call" instruction that needs to be turned
1483 into a "calls" instruction, do so now. It saves a byte. */
1484 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1488 /* Get the opcode. */
1489 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1491 /* Make sure we're working with a "call" instruction! */
1494 /* Note that we've changed the relocs, section contents,
1496 elf_section_data (sec)->relocs = internal_relocs;
1497 elf_section_data (sec)->this_hdr.contents = contents;
1498 symtab_hdr->contents = (unsigned char *) isymbuf;
1500 /* Fix the opcode. */
1501 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1502 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1504 /* Fix irel->r_offset and irel->r_addend. */
1505 irel->r_offset += 1;
1506 irel->r_addend += 1;
1508 /* Delete one byte of data. */
1509 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1510 irel->r_offset + 3, 1))
1513 /* That will change things, so, we should relax again.
1514 Note that this is not required, and it may be slow. */
1520 /* We've got a "call" instruction which needs some data
1521 from target function filled in. */
1524 /* Get the opcode. */
1525 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1527 /* Insert data from the target function into the "call"
1528 instruction if needed. */
1531 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1532 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1533 contents + irel->r_offset + 5);
1537 /* Deal with pc-relative gunk. */
1538 value -= (sec->output_section->vma + sec->output_offset);
1539 value -= irel->r_offset;
1540 value += irel->r_addend;
1542 /* See if the value will fit in 16 bits, note the high value is
1543 0x7fff + 2 as the target will be two bytes closer if we are
1545 if ((long) value < 0x8001 && (long) value > -0x8000)
1549 /* Get the opcode. */
1550 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1552 if (code != 0xdc && code != 0xdd && code != 0xff)
1555 /* Note that we've changed the relocs, section contents, etc. */
1556 elf_section_data (sec)->relocs = internal_relocs;
1557 elf_section_data (sec)->this_hdr.contents = contents;
1558 symtab_hdr->contents = (unsigned char *) isymbuf;
1560 /* Fix the opcode. */
1562 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1563 else if (code == 0xdd)
1564 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1565 else if (code == 0xff)
1566 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1568 /* Fix the relocation's type. */
1569 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1572 /* Delete two bytes of data. */
1573 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1574 irel->r_offset + 1, 2))
1577 /* That will change things, so, we should relax again.
1578 Note that this is not required, and it may be slow. */
1583 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1585 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1587 bfd_vma value = symval;
1589 /* If we've got a "call" instruction that needs to be turned
1590 into a "calls" instruction, do so now. It saves a byte. */
1591 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1595 /* Get the opcode. */
1596 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1598 /* Make sure we're working with a "call" instruction! */
1601 /* Note that we've changed the relocs, section contents,
1603 elf_section_data (sec)->relocs = internal_relocs;
1604 elf_section_data (sec)->this_hdr.contents = contents;
1605 symtab_hdr->contents = (unsigned char *) isymbuf;
1607 /* Fix the opcode. */
1608 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1609 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1611 /* Fix irel->r_offset and irel->r_addend. */
1612 irel->r_offset += 1;
1613 irel->r_addend += 1;
1615 /* Delete one byte of data. */
1616 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1617 irel->r_offset + 1, 1))
1620 /* That will change things, so, we should relax again.
1621 Note that this is not required, and it may be slow. */
1629 /* Get the opcode. */
1630 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1632 /* Insert data from the target function into the "call"
1633 instruction if needed. */
1636 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1637 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1638 contents + irel->r_offset + 3);
1642 /* Deal with pc-relative gunk. */
1643 value -= (sec->output_section->vma + sec->output_offset);
1644 value -= irel->r_offset;
1645 value += irel->r_addend;
1647 /* See if the value will fit in 8 bits, note the high value is
1648 0x7f + 1 as the target will be one bytes closer if we are
1650 if ((long) value < 0x80 && (long) value > -0x80)
1654 /* Get the opcode. */
1655 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1660 /* Note that we've changed the relocs, section contents, etc. */
1661 elf_section_data (sec)->relocs = internal_relocs;
1662 elf_section_data (sec)->this_hdr.contents = contents;
1663 symtab_hdr->contents = (unsigned char *) isymbuf;
1665 /* Fix the opcode. */
1666 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1668 /* Fix the relocation's type. */
1669 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1672 /* Delete one byte of data. */
1673 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1674 irel->r_offset + 1, 1))
1677 /* That will change things, so, we should relax again.
1678 Note that this is not required, and it may be slow. */
1683 /* Try to eliminate an unconditional 8 bit pc-relative branch
1684 which immediately follows a conditional 8 bit pc-relative
1685 branch around the unconditional branch.
1692 This happens when the bCC can't reach lab2 at assembly time,
1693 but due to other relaxations it can reach at link time. */
1694 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1696 Elf_Internal_Rela *nrel;
1697 bfd_vma value = symval;
1700 /* Deal with pc-relative gunk. */
1701 value -= (sec->output_section->vma + sec->output_offset);
1702 value -= irel->r_offset;
1703 value += irel->r_addend;
1705 /* Do nothing if this reloc is the last byte in the section. */
1706 if (irel->r_offset == sec->_cooked_size)
1709 /* See if the next instruction is an unconditional pc-relative
1710 branch, more often than not this test will fail, so we
1711 test it first to speed things up. */
1712 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1716 /* Also make sure the next relocation applies to the next
1717 instruction and that it's a pc-relative 8 bit branch. */
1720 || irel->r_offset + 2 != nrel->r_offset
1721 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1724 /* Make sure our destination immediately follows the
1725 unconditional branch. */
1726 if (symval != (sec->output_section->vma + sec->output_offset
1727 + irel->r_offset + 3))
1730 /* Now make sure we are a conditional branch. This may not
1731 be necessary, but why take the chance.
1733 Note these checks assume that R_MN10300_PCREL8 relocs
1734 only occur on bCC and bCCx insns. If they occured
1735 elsewhere, we'd need to know the start of this insn
1736 for this check to be accurate. */
1737 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1738 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1739 && code != 0xc3 && code != 0xc4 && code != 0xc5
1740 && code != 0xc6 && code != 0xc7 && code != 0xc8
1741 && code != 0xc9 && code != 0xe8 && code != 0xe9
1742 && code != 0xea && code != 0xeb)
1745 /* We also have to be sure there is no symbol/label
1746 at the unconditional branch. */
1747 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
1748 irel->r_offset + 1))
1751 /* Note that we've changed the relocs, section contents, etc. */
1752 elf_section_data (sec)->relocs = internal_relocs;
1753 elf_section_data (sec)->this_hdr.contents = contents;
1754 symtab_hdr->contents = (unsigned char *) isymbuf;
1756 /* Reverse the condition of the first branch. */
1802 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1804 /* Set the reloc type and symbol for the first branch
1805 from the second branch. */
1806 irel->r_info = nrel->r_info;
1808 /* Make the reloc for the second branch a null reloc. */
1809 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1812 /* Delete two bytes of data. */
1813 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1814 irel->r_offset + 1, 2))
1817 /* That will change things, so, we should relax again.
1818 Note that this is not required, and it may be slow. */
1822 /* Try to turn a 24 immediate, displacement or absolute address
1823 into a 8 immediate, displacement or absolute address. */
1824 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1826 bfd_vma value = symval;
1827 value += irel->r_addend;
1829 /* See if the value will fit in 8 bits. */
1830 if ((long) value < 0x7f && (long) value > -0x80)
1834 /* AM33 insns which have 24 operands are 6 bytes long and
1835 will have 0xfd as the first byte. */
1837 /* Get the first opcode. */
1838 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1842 /* Get the second opcode. */
1843 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1845 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1846 equivalent instructions exists. */
1847 if (code != 0x6b && code != 0x7b
1848 && code != 0x8b && code != 0x9b
1849 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1850 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1851 || (code & 0x0f) == 0x0e))
1853 /* Not safe if the high bit is on as relaxing may
1854 move the value out of high mem and thus not fit
1855 in a signed 8bit value. This is currently over
1857 if ((value & 0x80) == 0)
1859 /* Note that we've changed the relocation contents,
1861 elf_section_data (sec)->relocs = internal_relocs;
1862 elf_section_data (sec)->this_hdr.contents = contents;
1863 symtab_hdr->contents = (unsigned char *) isymbuf;
1865 /* Fix the opcode. */
1866 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1867 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1869 /* Fix the relocation's type. */
1871 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1874 /* Delete two bytes of data. */
1875 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1876 irel->r_offset + 1, 2))
1879 /* That will change things, so, we should relax
1880 again. Note that this is not required, and it
1890 /* Try to turn a 32bit immediate, displacement or absolute address
1891 into a 16bit immediate, displacement or absolute address. */
1892 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1894 bfd_vma value = symval;
1895 value += irel->r_addend;
1897 /* See if the value will fit in 24 bits.
1898 We allow any 16bit match here. We prune those we can't
1900 if ((long) value < 0x7fffff && (long) value > -0x800000)
1904 /* AM33 insns which have 32bit operands are 7 bytes long and
1905 will have 0xfe as the first byte. */
1907 /* Get the first opcode. */
1908 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1912 /* Get the second opcode. */
1913 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1915 /* All the am33 32 -> 24 relaxing possibilities. */
1916 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1917 equivalent instructions exists. */
1918 if (code != 0x6b && code != 0x7b
1919 && code != 0x8b && code != 0x9b
1920 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1921 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1922 || (code & 0x0f) == 0x0e))
1924 /* Not safe if the high bit is on as relaxing may
1925 move the value out of high mem and thus not fit
1926 in a signed 16bit value. This is currently over
1928 if ((value & 0x8000) == 0)
1930 /* Note that we've changed the relocation contents,
1932 elf_section_data (sec)->relocs = internal_relocs;
1933 elf_section_data (sec)->this_hdr.contents = contents;
1934 symtab_hdr->contents = (unsigned char *) isymbuf;
1936 /* Fix the opcode. */
1937 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
1938 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1940 /* Fix the relocation's type. */
1942 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1945 /* Delete one byte of data. */
1946 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1947 irel->r_offset + 3, 1))
1950 /* That will change things, so, we should relax
1951 again. Note that this is not required, and it
1960 /* See if the value will fit in 16 bits.
1961 We allow any 16bit match here. We prune those we can't
1963 if ((long) value < 0x7fff && (long) value > -0x8000)
1967 /* Most insns which have 32bit operands are 6 bytes long;
1968 exceptions are pcrel insns and bit insns.
1970 We handle pcrel insns above. We don't bother trying
1971 to handle the bit insns here.
1973 The first byte of the remaining insns will be 0xfc. */
1975 /* Get the first opcode. */
1976 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1981 /* Get the second opcode. */
1982 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1984 if ((code & 0xf0) < 0x80)
1985 switch (code & 0xf0)
1987 /* mov (d32,am),dn -> mov (d32,am),dn
1988 mov dm,(d32,am) -> mov dn,(d32,am)
1989 mov (d32,am),an -> mov (d32,am),an
1990 mov dm,(d32,am) -> mov dn,(d32,am)
1991 movbu (d32,am),dn -> movbu (d32,am),dn
1992 movbu dm,(d32,am) -> movbu dn,(d32,am)
1993 movhu (d32,am),dn -> movhu (d32,am),dn
1994 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2003 /* Not safe if the high bit is on as relaxing may
2004 move the value out of high mem and thus not fit
2005 in a signed 16bit value. */
2007 && (value & 0x8000))
2010 /* Note that we've changed the relocation contents, etc. */
2011 elf_section_data (sec)->relocs = internal_relocs;
2012 elf_section_data (sec)->this_hdr.contents = contents;
2013 symtab_hdr->contents = (unsigned char *) isymbuf;
2015 /* Fix the opcode. */
2016 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2017 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2019 /* Fix the relocation's type. */
2020 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2023 /* Delete two bytes of data. */
2024 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2025 irel->r_offset + 2, 2))
2028 /* That will change things, so, we should relax again.
2029 Note that this is not required, and it may be slow. */
2033 else if ((code & 0xf0) == 0x80
2034 || (code & 0xf0) == 0x90)
2035 switch (code & 0xf3)
2037 /* mov dn,(abs32) -> mov dn,(abs16)
2038 movbu dn,(abs32) -> movbu dn,(abs16)
2039 movhu dn,(abs32) -> movhu dn,(abs16) */
2043 /* Note that we've changed the relocation contents, etc. */
2044 elf_section_data (sec)->relocs = internal_relocs;
2045 elf_section_data (sec)->this_hdr.contents = contents;
2046 symtab_hdr->contents = (unsigned char *) isymbuf;
2048 if ((code & 0xf3) == 0x81)
2049 code = 0x01 + (code & 0x0c);
2050 else if ((code & 0xf3) == 0x82)
2051 code = 0x02 + (code & 0x0c);
2052 else if ((code & 0xf3) == 0x83)
2053 code = 0x03 + (code & 0x0c);
2057 /* Fix the opcode. */
2058 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2060 /* Fix the relocation's type. */
2061 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2064 /* The opcode got shorter too, so we have to fix the
2065 addend and offset too! */
2066 irel->r_offset -= 1;
2068 /* Delete three bytes of data. */
2069 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2070 irel->r_offset + 1, 3))
2073 /* That will change things, so, we should relax again.
2074 Note that this is not required, and it may be slow. */
2078 /* mov am,(abs32) -> mov am,(abs16)
2079 mov am,(d32,sp) -> mov am,(d16,sp)
2080 mov dm,(d32,sp) -> mov dm,(d32,sp)
2081 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2082 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2088 /* sp-based offsets are zero-extended. */
2089 if (code >= 0x90 && code <= 0x93
2093 /* Note that we've changed the relocation contents, etc. */
2094 elf_section_data (sec)->relocs = internal_relocs;
2095 elf_section_data (sec)->this_hdr.contents = contents;
2096 symtab_hdr->contents = (unsigned char *) isymbuf;
2098 /* Fix the opcode. */
2099 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2100 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2102 /* Fix the relocation's type. */
2103 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2106 /* Delete two bytes of data. */
2107 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2108 irel->r_offset + 2, 2))
2111 /* That will change things, so, we should relax again.
2112 Note that this is not required, and it may be slow. */
2116 else if ((code & 0xf0) < 0xf0)
2117 switch (code & 0xfc)
2119 /* mov imm32,dn -> mov imm16,dn
2120 mov imm32,an -> mov imm16,an
2121 mov (abs32),dn -> mov (abs16),dn
2122 movbu (abs32),dn -> movbu (abs16),dn
2123 movhu (abs32),dn -> movhu (abs16),dn */
2129 /* Not safe if the high bit is on as relaxing may
2130 move the value out of high mem and thus not fit
2131 in a signed 16bit value. */
2133 && (value & 0x8000))
2136 /* mov imm16, an zero-extends the immediate. */
2141 /* Note that we've changed the relocation contents, etc. */
2142 elf_section_data (sec)->relocs = internal_relocs;
2143 elf_section_data (sec)->this_hdr.contents = contents;
2144 symtab_hdr->contents = (unsigned char *) isymbuf;
2146 if ((code & 0xfc) == 0xcc)
2147 code = 0x2c + (code & 0x03);
2148 else if ((code & 0xfc) == 0xdc)
2149 code = 0x24 + (code & 0x03);
2150 else if ((code & 0xfc) == 0xa4)
2151 code = 0x30 + (code & 0x03);
2152 else if ((code & 0xfc) == 0xa8)
2153 code = 0x34 + (code & 0x03);
2154 else if ((code & 0xfc) == 0xac)
2155 code = 0x38 + (code & 0x03);
2159 /* Fix the opcode. */
2160 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2162 /* Fix the relocation's type. */
2163 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2166 /* The opcode got shorter too, so we have to fix the
2167 addend and offset too! */
2168 irel->r_offset -= 1;
2170 /* Delete three bytes of data. */
2171 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2172 irel->r_offset + 1, 3))
2175 /* That will change things, so, we should relax again.
2176 Note that this is not required, and it may be slow. */
2180 /* mov (abs32),an -> mov (abs16),an
2181 mov (d32,sp),an -> mov (d16,sp),an
2182 mov (d32,sp),dn -> mov (d16,sp),dn
2183 movbu (d32,sp),dn -> movbu (d16,sp),dn
2184 movhu (d32,sp),dn -> movhu (d16,sp),dn
2185 add imm32,dn -> add imm16,dn
2186 cmp imm32,dn -> cmp imm16,dn
2187 add imm32,an -> add imm16,an
2188 cmp imm32,an -> cmp imm16,an
2189 and imm32,dn -> and imm16,dn
2190 or imm32,dn -> or imm16,dn
2191 xor imm32,dn -> xor imm16,dn
2192 btst imm32,dn -> btst imm16,dn */
2208 /* cmp imm16, an zero-extends the immediate. */
2213 /* So do sp-based offsets. */
2214 if (code >= 0xb0 && code <= 0xb3
2218 /* Note that we've changed the relocation contents, etc. */
2219 elf_section_data (sec)->relocs = internal_relocs;
2220 elf_section_data (sec)->this_hdr.contents = contents;
2221 symtab_hdr->contents = (unsigned char *) isymbuf;
2223 /* Fix the opcode. */
2224 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2225 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2227 /* Fix the relocation's type. */
2228 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2231 /* Delete two bytes of data. */
2232 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2233 irel->r_offset + 2, 2))
2236 /* That will change things, so, we should relax again.
2237 Note that this is not required, and it may be slow. */
2241 else if (code == 0xfe)
2243 /* add imm32,sp -> add imm16,sp */
2245 /* Note that we've changed the relocation contents, etc. */
2246 elf_section_data (sec)->relocs = internal_relocs;
2247 elf_section_data (sec)->this_hdr.contents = contents;
2248 symtab_hdr->contents = (unsigned char *) isymbuf;
2250 /* Fix the opcode. */
2251 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2252 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2254 /* Fix the relocation's type. */
2255 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2258 /* Delete two bytes of data. */
2259 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2260 irel->r_offset + 2, 2))
2263 /* That will change things, so, we should relax again.
2264 Note that this is not required, and it may be slow. */
2273 && symtab_hdr->contents != (unsigned char *) isymbuf)
2275 if (! link_info->keep_memory)
2279 /* Cache the symbols for elf_link_input_bfd. */
2280 symtab_hdr->contents = (unsigned char *) isymbuf;
2284 if (contents != NULL
2285 && elf_section_data (sec)->this_hdr.contents != contents)
2287 if (! link_info->keep_memory)
2291 /* Cache the section contents for elf_link_input_bfd. */
2292 elf_section_data (sec)->this_hdr.contents = contents;
2296 if (internal_relocs != NULL
2297 && elf_section_data (sec)->relocs != internal_relocs)
2298 free (internal_relocs);
2304 && symtab_hdr->contents != (unsigned char *) isymbuf)
2306 if (contents != NULL
2307 && elf_section_data (section)->this_hdr.contents != contents)
2309 if (internal_relocs != NULL
2310 && elf_section_data (section)->relocs != internal_relocs)
2311 free (internal_relocs);
2316 /* Compute the stack size and movm arguments for the function
2317 referred to by HASH at address ADDR in section with
2318 contents CONTENTS, store the information in the hash table. */
2320 compute_function_info (abfd, hash, addr, contents)
2322 struct elf32_mn10300_link_hash_entry *hash;
2324 unsigned char *contents;
2326 unsigned char byte1, byte2;
2327 /* We only care about a very small subset of the possible prologue
2328 sequences here. Basically we look for:
2330 movm [d2,d3,a2,a3],sp (optional)
2331 add <size>,sp (optional, and only for sizes which fit in an unsigned
2334 If we find anything else, we quit. */
2336 /* Look for movm [regs],sp */
2337 byte1 = bfd_get_8 (abfd, contents + addr);
2338 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2342 hash->movm_args = byte2;
2344 byte1 = bfd_get_8 (abfd, contents + addr);
2345 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2348 /* Now figure out how much stack space will be allocated by the movm
2349 instruction. We need this kept separate from the funtion's normal
2351 if (hash->movm_args)
2354 if (hash->movm_args & 0x80)
2355 hash->movm_stack_size += 4;
2358 if (hash->movm_args & 0x40)
2359 hash->movm_stack_size += 4;
2362 if (hash->movm_args & 0x20)
2363 hash->movm_stack_size += 4;
2366 if (hash->movm_args & 0x10)
2367 hash->movm_stack_size += 4;
2369 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2370 if (hash->movm_args & 0x08)
2371 hash->movm_stack_size += 8 * 4;
2373 if (bfd_get_mach (abfd) == bfd_mach_am33
2374 || bfd_get_mach (abfd) == bfd_mach_am33_2)
2376 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2377 if (hash->movm_args & 0x1)
2378 hash->movm_stack_size += 6 * 4;
2380 /* exreg1 space. e4, e5, e6, e7 */
2381 if (hash->movm_args & 0x2)
2382 hash->movm_stack_size += 4 * 4;
2384 /* exreg0 space. e2, e3 */
2385 if (hash->movm_args & 0x4)
2386 hash->movm_stack_size += 2 * 4;
2390 /* Now look for the two stack adjustment variants. */
2391 if (byte1 == 0xf8 && byte2 == 0xfe)
2393 int temp = bfd_get_8 (abfd, contents + addr + 2);
2394 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2396 hash->stack_size = -temp;
2398 else if (byte1 == 0xfa && byte2 == 0xfe)
2400 int temp = bfd_get_16 (abfd, contents + addr + 2);
2401 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2405 hash->stack_size = temp;
2408 /* If the total stack to be allocated by the call instruction is more
2409 than 255 bytes, then we can't remove the stack adjustment by using
2410 "call" (we might still be able to remove the "movm" instruction. */
2411 if (hash->stack_size + hash->movm_stack_size > 255)
2412 hash->stack_size = 0;
2417 /* Delete some bytes from a section while relaxing. */
2420 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2426 Elf_Internal_Shdr *symtab_hdr;
2427 unsigned int sec_shndx;
2429 Elf_Internal_Rela *irel, *irelend;
2430 Elf_Internal_Rela *irelalign;
2432 Elf_Internal_Sym *isym, *isymend;
2433 struct elf_link_hash_entry **sym_hashes;
2434 struct elf_link_hash_entry **end_hashes;
2435 unsigned int symcount;
2437 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2439 contents = elf_section_data (sec)->this_hdr.contents;
2441 /* The deletion must stop at the next ALIGN reloc for an aligment
2442 power larger than the number of bytes we are deleting. */
2445 toaddr = sec->_cooked_size;
2447 irel = elf_section_data (sec)->relocs;
2448 irelend = irel + sec->reloc_count;
2450 /* Actually delete the bytes. */
2451 memmove (contents + addr, contents + addr + count,
2452 (size_t) (toaddr - addr - count));
2453 sec->_cooked_size -= count;
2455 /* Adjust all the relocs. */
2456 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2458 /* Get the new reloc address. */
2459 if ((irel->r_offset > addr
2460 && irel->r_offset < toaddr))
2461 irel->r_offset -= count;
2464 /* Adjust the local symbols defined in this section. */
2465 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2466 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2467 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2469 if (isym->st_shndx == sec_shndx
2470 && isym->st_value > addr
2471 && isym->st_value < toaddr)
2472 isym->st_value -= count;
2475 /* Now adjust the global symbols defined in this section. */
2476 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2477 - symtab_hdr->sh_info);
2478 sym_hashes = elf_sym_hashes (abfd);
2479 end_hashes = sym_hashes + symcount;
2480 for (; sym_hashes < end_hashes; sym_hashes++)
2482 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2483 if ((sym_hash->root.type == bfd_link_hash_defined
2484 || sym_hash->root.type == bfd_link_hash_defweak)
2485 && sym_hash->root.u.def.section == sec
2486 && sym_hash->root.u.def.value > addr
2487 && sym_hash->root.u.def.value < toaddr)
2489 sym_hash->root.u.def.value -= count;
2496 /* Return TRUE if a symbol exists at the given address, else return
2499 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
2502 Elf_Internal_Sym *isym;
2505 Elf_Internal_Shdr *symtab_hdr;
2506 unsigned int sec_shndx;
2507 Elf_Internal_Sym *isymend;
2508 struct elf_link_hash_entry **sym_hashes;
2509 struct elf_link_hash_entry **end_hashes;
2510 unsigned int symcount;
2512 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2514 /* Examine all the symbols. */
2515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2516 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2518 if (isym->st_shndx == sec_shndx
2519 && isym->st_value == addr)
2523 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2524 - symtab_hdr->sh_info);
2525 sym_hashes = elf_sym_hashes (abfd);
2526 end_hashes = sym_hashes + symcount;
2527 for (; sym_hashes < end_hashes; sym_hashes++)
2529 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2530 if ((sym_hash->root.type == bfd_link_hash_defined
2531 || sym_hash->root.type == bfd_link_hash_defweak)
2532 && sym_hash->root.u.def.section == sec
2533 && sym_hash->root.u.def.value == addr)
2540 /* This is a version of bfd_generic_get_relocated_section_contents
2541 which uses mn10300_elf_relocate_section. */
2544 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2545 data, relocatable, symbols)
2547 struct bfd_link_info *link_info;
2548 struct bfd_link_order *link_order;
2550 bfd_boolean relocatable;
2553 Elf_Internal_Shdr *symtab_hdr;
2554 asection *input_section = link_order->u.indirect.section;
2555 bfd *input_bfd = input_section->owner;
2556 asection **sections = NULL;
2557 Elf_Internal_Rela *internal_relocs = NULL;
2558 Elf_Internal_Sym *isymbuf = NULL;
2560 /* We only need to handle the case of relaxing, or of having a
2561 particular set of section contents, specially. */
2563 || elf_section_data (input_section)->this_hdr.contents == NULL)
2564 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2569 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2571 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2572 (size_t) input_section->_raw_size);
2574 if ((input_section->flags & SEC_RELOC) != 0
2575 && input_section->reloc_count > 0)
2578 Elf_Internal_Sym *isym, *isymend;
2581 internal_relocs = (_bfd_elf_link_read_relocs
2582 (input_bfd, input_section, (PTR) NULL,
2583 (Elf_Internal_Rela *) NULL, FALSE));
2584 if (internal_relocs == NULL)
2587 if (symtab_hdr->sh_info != 0)
2589 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2590 if (isymbuf == NULL)
2591 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2592 symtab_hdr->sh_info, 0,
2594 if (isymbuf == NULL)
2598 amt = symtab_hdr->sh_info;
2599 amt *= sizeof (asection *);
2600 sections = (asection **) bfd_malloc (amt);
2601 if (sections == NULL && amt != 0)
2604 isymend = isymbuf + symtab_hdr->sh_info;
2605 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2609 if (isym->st_shndx == SHN_UNDEF)
2610 isec = bfd_und_section_ptr;
2611 else if (isym->st_shndx == SHN_ABS)
2612 isec = bfd_abs_section_ptr;
2613 else if (isym->st_shndx == SHN_COMMON)
2614 isec = bfd_com_section_ptr;
2616 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2621 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2622 input_section, data, internal_relocs,
2626 if (sections != NULL)
2628 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2630 if (internal_relocs != elf_section_data (input_section)->relocs)
2631 free (internal_relocs);
2637 if (sections != NULL)
2639 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2641 if (internal_relocs != NULL
2642 && internal_relocs != elf_section_data (input_section)->relocs)
2643 free (internal_relocs);
2647 /* Assorted hash table functions. */
2649 /* Initialize an entry in the link hash table. */
2651 /* Create an entry in an MN10300 ELF linker hash table. */
2653 static struct bfd_hash_entry *
2654 elf32_mn10300_link_hash_newfunc (entry, table, string)
2655 struct bfd_hash_entry *entry;
2656 struct bfd_hash_table *table;
2659 struct elf32_mn10300_link_hash_entry *ret =
2660 (struct elf32_mn10300_link_hash_entry *) entry;
2662 /* Allocate the structure if it has not already been allocated by a
2664 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2665 ret = ((struct elf32_mn10300_link_hash_entry *)
2666 bfd_hash_allocate (table,
2667 sizeof (struct elf32_mn10300_link_hash_entry)));
2668 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2669 return (struct bfd_hash_entry *) ret;
2671 /* Call the allocation method of the superclass. */
2672 ret = ((struct elf32_mn10300_link_hash_entry *)
2673 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2675 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2677 ret->direct_calls = 0;
2678 ret->stack_size = 0;
2680 ret->movm_stack_size = 0;
2684 return (struct bfd_hash_entry *) ret;
2687 /* Create an mn10300 ELF linker hash table. */
2689 static struct bfd_link_hash_table *
2690 elf32_mn10300_link_hash_table_create (abfd)
2693 struct elf32_mn10300_link_hash_table *ret;
2694 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2696 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2697 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2700 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2701 elf32_mn10300_link_hash_newfunc))
2708 amt = sizeof (struct elf_link_hash_table);
2709 ret->static_hash_table
2710 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2711 if (ret->static_hash_table == NULL)
2717 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2718 elf32_mn10300_link_hash_newfunc))
2720 free (ret->static_hash_table);
2724 return &ret->root.root;
2727 /* Free an mn10300 ELF linker hash table. */
2730 elf32_mn10300_link_hash_table_free (hash)
2731 struct bfd_link_hash_table *hash;
2733 struct elf32_mn10300_link_hash_table *ret
2734 = (struct elf32_mn10300_link_hash_table *) hash;
2736 _bfd_generic_link_hash_table_free
2737 ((struct bfd_link_hash_table *) ret->static_hash_table);
2738 _bfd_generic_link_hash_table_free
2739 ((struct bfd_link_hash_table *) ret);
2742 static unsigned long
2743 elf_mn10300_mach (flags)
2746 switch (flags & EF_MN10300_MACH)
2748 case E_MN10300_MACH_MN10300:
2750 return bfd_mach_mn10300;
2752 case E_MN10300_MACH_AM33:
2753 return bfd_mach_am33;
2755 case E_MN10300_MACH_AM33_2:
2756 return bfd_mach_am33_2;
2760 /* The final processing done just before writing out a MN10300 ELF object
2761 file. This gets the MN10300 architecture right based on the machine
2765 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2767 bfd_boolean linker ATTRIBUTE_UNUSED;
2771 switch (bfd_get_mach (abfd))
2774 case bfd_mach_mn10300:
2775 val = E_MN10300_MACH_MN10300;
2779 val = E_MN10300_MACH_AM33;
2782 case bfd_mach_am33_2:
2783 val = E_MN10300_MACH_AM33_2;
2787 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2788 elf_elfheader (abfd)->e_flags |= val;
2792 _bfd_mn10300_elf_object_p (abfd)
2795 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2796 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2800 /* Merge backend specific data from an object file to the output
2801 object file when linking. */
2804 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2808 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2809 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2812 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2813 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2815 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2816 bfd_get_mach (ibfd)))
2823 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2824 #define TARGET_LITTLE_NAME "elf32-mn10300"
2825 #define ELF_ARCH bfd_arch_mn10300
2826 #define ELF_MACHINE_CODE EM_MN10300
2827 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
2828 #define ELF_MAXPAGESIZE 0x1000
2830 #define elf_info_to_howto mn10300_info_to_howto
2831 #define elf_info_to_howto_rel 0
2832 #define elf_backend_can_gc_sections 1
2833 #define elf_backend_rela_normal 1
2834 #define elf_backend_check_relocs mn10300_elf_check_relocs
2835 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2836 #define elf_backend_relocate_section mn10300_elf_relocate_section
2837 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2838 #define bfd_elf32_bfd_get_relocated_section_contents \
2839 mn10300_elf_get_relocated_section_contents
2840 #define bfd_elf32_bfd_link_hash_table_create \
2841 elf32_mn10300_link_hash_table_create
2842 #define bfd_elf32_bfd_link_hash_table_free \
2843 elf32_mn10300_link_hash_table_free
2845 #define elf_symbol_leading_char '_'
2847 /* So we can set bits in e_flags. */
2848 #define elf_backend_final_write_processing \
2849 _bfd_mn10300_elf_final_write_processing
2850 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2852 #define bfd_elf32_bfd_merge_private_bfd_data \
2853 _bfd_mn10300_elf_merge_private_bfd_data
2855 #include "elf32-target.h"