1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
115 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
116 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
117 static void mn10300_info_to_howto
118 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
119 static boolean mn10300_elf_check_relocs
120 PARAMS ((bfd *, struct bfd_link_info *, asection *,
121 const Elf_Internal_Rela *));
122 static asection *mn10300_elf_gc_mark_hook
123 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
124 struct elf_link_hash_entry *, Elf_Internal_Sym *));
125 static boolean mn10300_elf_relax_delete_bytes
126 PARAMS ((bfd *, asection *, bfd_vma, int));
127 static boolean mn10300_elf_symbol_address_p
128 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
129 static boolean elf32_mn10300_finish_hash_table_entry
130 PARAMS ((struct bfd_hash_entry *, PTR));
131 static void compute_function_info
132 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
133 bfd_vma, unsigned char *));
135 /* We have to use RELA instructions since md_apply_fix3 in the assembler
136 does absolutely nothing. */
139 static reloc_howto_type elf_mn10300_howto_table[] = {
140 /* Dummy relocation. Does nothing. */
141 HOWTO (R_MN10300_NONE,
147 complain_overflow_bitfield,
148 bfd_elf_generic_reloc,
154 /* Standard 32 bit reloc. */
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
168 /* Standard 16 bit reloc. */
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
182 /* Standard 8 bit reloc. */
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
196 /* Standard 32bit pc-relative reloc. */
197 HOWTO (R_MN10300_PCREL32,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
210 /* Standard 16bit pc-relative reloc. */
211 HOWTO (R_MN10300_PCREL16,
217 complain_overflow_bitfield,
218 bfd_elf_generic_reloc,
224 /* Standard 8 pc-relative reloc. */
225 HOWTO (R_MN10300_PCREL8,
231 complain_overflow_bitfield,
232 bfd_elf_generic_reloc,
239 /* GNU extension to record C++ vtable hierarchy */
240 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
242 0, /* size (0 = byte, 1 = short, 2 = long) */
244 false, /* pc_relative */
246 complain_overflow_dont, /* complain_on_overflow */
247 NULL, /* special_function */
248 "R_MN10300_GNU_VTINHERIT", /* name */
249 false, /* partial_inplace */
252 false), /* pcrel_offset */
254 /* GNU extension to record C++ vtable member usage */
255 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
257 0, /* size (0 = byte, 1 = short, 2 = long) */
259 false, /* pc_relative */
261 complain_overflow_dont, /* complain_on_overflow */
262 NULL, /* special_function */
263 "R_MN10300_GNU_VTENTRY", /* name */
264 false, /* partial_inplace */
267 false), /* pcrel_offset */
269 /* Standard 24 bit reloc. */
276 complain_overflow_bitfield,
277 bfd_elf_generic_reloc,
285 struct mn10300_reloc_map {
286 bfd_reloc_code_real_type bfd_reloc_val;
287 unsigned char elf_reloc_val;
290 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
291 { BFD_RELOC_NONE, R_MN10300_NONE, },
292 { BFD_RELOC_32, R_MN10300_32, },
293 { BFD_RELOC_16, R_MN10300_16, },
294 { BFD_RELOC_8, R_MN10300_8, },
295 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
296 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
297 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
298 { BFD_RELOC_24, R_MN10300_24, },
299 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
300 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
303 static reloc_howto_type *
304 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
305 bfd *abfd ATTRIBUTE_UNUSED;
306 bfd_reloc_code_real_type code;
311 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
314 if (mn10300_reloc_map[i].bfd_reloc_val == code)
315 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
321 /* Set the howto pointer for an MN10300 ELF reloc. */
324 mn10300_info_to_howto (abfd, cache_ptr, dst)
325 bfd *abfd ATTRIBUTE_UNUSED;
327 Elf32_Internal_Rela *dst;
331 r_type = ELF32_R_TYPE (dst->r_info);
332 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
333 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
336 /* Look through the relocs for a section during the first phase.
337 Since we don't do .gots or .plts, we just need to consider the
338 virtual table relocs for gc. */
341 mn10300_elf_check_relocs (abfd, info, sec, relocs)
343 struct bfd_link_info *info;
345 const Elf_Internal_Rela *relocs;
347 Elf_Internal_Shdr *symtab_hdr;
348 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
349 const Elf_Internal_Rela *rel;
350 const Elf_Internal_Rela *rel_end;
352 if (info->relocateable)
355 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
356 sym_hashes = elf_sym_hashes (abfd);
357 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
358 if (!elf_bad_symtab (abfd))
359 sym_hashes_end -= symtab_hdr->sh_info;
361 rel_end = relocs + sec->reloc_count;
362 for (rel = relocs; rel < rel_end; rel++)
364 struct elf_link_hash_entry *h;
365 unsigned long r_symndx;
367 r_symndx = ELF32_R_SYM (rel->r_info);
368 if (r_symndx < symtab_hdr->sh_info)
371 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
373 switch (ELF32_R_TYPE (rel->r_info))
375 /* This relocation describes the C++ object vtable hierarchy.
376 Reconstruct it for later use during GC. */
377 case R_MN10300_GNU_VTINHERIT:
378 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
382 /* This relocation describes which C++ vtable entries are actually
383 used. Record for later use during GC. */
384 case R_MN10300_GNU_VTENTRY:
385 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
394 /* Return the section that should be marked against GC for a given
398 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
400 struct bfd_link_info *info ATTRIBUTE_UNUSED;
401 Elf_Internal_Rela *rel;
402 struct elf_link_hash_entry *h;
403 Elf_Internal_Sym *sym;
407 switch (ELF32_R_TYPE (rel->r_info))
409 case R_MN10300_GNU_VTINHERIT:
410 case R_MN10300_GNU_VTENTRY:
414 switch (h->root.type)
416 case bfd_link_hash_defined:
417 case bfd_link_hash_defweak:
418 return h->root.u.def.section;
420 case bfd_link_hash_common:
421 return h->root.u.c.p->section;
430 if (!(elf_bad_symtab (abfd)
431 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
432 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
433 && sym->st_shndx != SHN_COMMON))
435 return bfd_section_from_elf_index (abfd, sym->st_shndx);
442 /* Perform a relocation as part of a final link. */
443 static bfd_reloc_status_type
444 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
445 input_section, contents, offset, value,
446 addend, info, sym_sec, is_local)
447 reloc_howto_type *howto;
449 bfd *output_bfd ATTRIBUTE_UNUSED;
450 asection *input_section;
455 struct bfd_link_info *info ATTRIBUTE_UNUSED;
456 asection *sym_sec ATTRIBUTE_UNUSED;
457 int is_local ATTRIBUTE_UNUSED;
459 unsigned long r_type = howto->type;
460 bfd_byte *hit_data = contents + offset;
469 bfd_put_32 (input_bfd, value, hit_data);
475 if ((long) value > 0x7fffff || (long) value < -0x800000)
476 return bfd_reloc_overflow;
478 bfd_put_8 (input_bfd, value & 0xff, hit_data);
479 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
480 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
486 if ((long) value > 0x7fff || (long) value < -0x8000)
487 return bfd_reloc_overflow;
489 bfd_put_16 (input_bfd, value, hit_data);
495 if ((long) value > 0x7f || (long) value < -0x80)
496 return bfd_reloc_overflow;
498 bfd_put_8 (input_bfd, value, hit_data);
501 case R_MN10300_PCREL8:
502 value -= (input_section->output_section->vma
503 + input_section->output_offset);
507 if ((long) value > 0xff || (long) value < -0x100)
508 return bfd_reloc_overflow;
510 bfd_put_8 (input_bfd, value, hit_data);
513 case R_MN10300_PCREL16:
514 value -= (input_section->output_section->vma
515 + input_section->output_offset);
519 if ((long) value > 0xffff || (long) value < -0x10000)
520 return bfd_reloc_overflow;
522 bfd_put_16 (input_bfd, value, hit_data);
525 case R_MN10300_PCREL32:
526 value -= (input_section->output_section->vma
527 + input_section->output_offset);
531 bfd_put_32 (input_bfd, value, hit_data);
534 case R_MN10300_GNU_VTINHERIT:
535 case R_MN10300_GNU_VTENTRY:
539 return bfd_reloc_notsupported;
543 /* Relocate an MN10300 ELF section. */
545 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
546 contents, relocs, local_syms, local_sections)
548 struct bfd_link_info *info;
550 asection *input_section;
552 Elf_Internal_Rela *relocs;
553 Elf_Internal_Sym *local_syms;
554 asection **local_sections;
556 Elf_Internal_Shdr *symtab_hdr;
557 struct elf32_mn10300_link_hash_entry **sym_hashes;
558 Elf_Internal_Rela *rel, *relend;
560 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
561 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
562 (elf_sym_hashes (input_bfd));
565 relend = relocs + input_section->reloc_count;
566 for (; rel < relend; rel++)
569 reloc_howto_type *howto;
570 unsigned long r_symndx;
571 Elf_Internal_Sym *sym;
573 struct elf32_mn10300_link_hash_entry *h;
575 bfd_reloc_status_type r;
577 r_symndx = ELF32_R_SYM (rel->r_info);
578 r_type = ELF32_R_TYPE (rel->r_info);
579 howto = elf_mn10300_howto_table + r_type;
581 /* Just skip the vtable gc relocs. */
582 if (r_type == R_MN10300_GNU_VTINHERIT
583 || r_type == R_MN10300_GNU_VTENTRY)
586 if (info->relocateable)
588 /* This is a relocateable link. We don't have to change
589 anything, unless the reloc is against a section symbol,
590 in which case we have to adjust according to where the
591 section symbol winds up in the output section. */
592 if (r_symndx < symtab_hdr->sh_info)
594 sym = local_syms + r_symndx;
595 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
597 sec = local_sections[r_symndx];
598 rel->r_addend += sec->output_offset + sym->st_value;
605 /* This is a final link. */
609 if (r_symndx < symtab_hdr->sh_info)
611 sym = local_syms + r_symndx;
612 sec = local_sections[r_symndx];
613 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
617 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
618 while (h->root.root.type == bfd_link_hash_indirect
619 || h->root.root.type == bfd_link_hash_warning)
620 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
621 if (h->root.root.type == bfd_link_hash_defined
622 || h->root.root.type == bfd_link_hash_defweak)
624 sec = h->root.root.u.def.section;
625 relocation = (h->root.root.u.def.value
626 + sec->output_section->vma
627 + sec->output_offset);
629 else if (h->root.root.type == bfd_link_hash_undefweak)
633 if (! ((*info->callbacks->undefined_symbol)
634 (info, h->root.root.root.string, input_bfd,
635 input_section, rel->r_offset, true)))
641 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
643 contents, rel->r_offset,
644 relocation, rel->r_addend,
645 info, sec, h == NULL);
647 if (r != bfd_reloc_ok)
650 const char *msg = (const char *) 0;
653 name = h->root.root.root.string;
656 name = (bfd_elf_string_from_elf_section
657 (input_bfd, symtab_hdr->sh_link, sym->st_name));
658 if (name == NULL || *name == '\0')
659 name = bfd_section_name (input_bfd, sec);
664 case bfd_reloc_overflow:
665 if (! ((*info->callbacks->reloc_overflow)
666 (info, name, howto->name, (bfd_vma) 0,
667 input_bfd, input_section, rel->r_offset)))
671 case bfd_reloc_undefined:
672 if (! ((*info->callbacks->undefined_symbol)
673 (info, name, input_bfd, input_section,
674 rel->r_offset, true)))
678 case bfd_reloc_outofrange:
679 msg = _("internal error: out of range error");
682 case bfd_reloc_notsupported:
683 msg = _("internal error: unsupported relocation error");
686 case bfd_reloc_dangerous:
687 msg = _("internal error: dangerous error");
691 msg = _("internal error: unknown error");
695 if (!((*info->callbacks->warning)
696 (info, msg, name, input_bfd, input_section,
707 /* Finish initializing one hash table entry. */
709 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
710 struct bfd_hash_entry *gen_entry;
711 PTR in_args ATTRIBUTE_UNUSED;
713 struct elf32_mn10300_link_hash_entry *entry;
714 unsigned int byte_count = 0;
716 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
718 /* If we already know we want to convert "call" to "calls" for calls
719 to this symbol, then return now. */
720 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
723 /* If there are no named calls to this symbol, or there's nothing we
724 can move from the function itself into the "call" instruction, then
725 note that all "call" instructions should be converted into "calls"
726 instructions and return. */
727 if (entry->direct_calls == 0
728 || (entry->stack_size == 0 && entry->movm_args == 0))
730 /* Make a note that we should convert "call" instructions to "calls"
731 instructions for calls to this symbol. */
732 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
736 /* We may be able to move some instructions from the function itself into
737 the "call" instruction. Count how many bytes we might be able to
738 eliminate in the function itself. */
740 /* A movm instruction is two bytes. */
741 if (entry->movm_args)
744 /* Count the insn to allocate stack space too. */
745 if (entry->stack_size > 0 && entry->stack_size <= 128)
747 else if (entry->stack_size > 0 && entry->stack_size < 256)
750 /* If using "call" will result in larger code, then turn all
751 the associated "call" instructions into "calls" instrutions. */
752 if (byte_count < entry->direct_calls)
753 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
755 /* This routine never fails. */
759 /* This function handles relaxing for the mn10300.
761 There's quite a few relaxing opportunites available on the mn10300:
763 * calls:32 -> calls:16 2 bytes
764 * call:32 -> call:16 2 bytes
766 * call:32 -> calls:32 1 byte
767 * call:16 -> calls:16 1 byte
768 * These are done anytime using "calls" would result
769 in smaller code, or when necessary to preserve the
770 meaning of the program.
774 * In some circumstances we can move instructions
775 from a function prologue into a "call" instruction.
776 This is only done if the resulting code is no larger
777 than the original code.
779 * jmp:32 -> jmp:16 2 bytes
780 * jmp:16 -> bra:8 1 byte
782 * If the previous instruction is a conditional branch
783 around the jump/bra, we may be able to reverse its condition
784 and change its target to the jump's target. The jump/bra
785 can then be deleted. 2 bytes
787 * mov abs32 -> mov abs16 1 or 2 bytes
789 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
790 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
792 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
793 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
795 We don't handle imm16->imm8 or d16->d8 as they're very rare
796 and somewhat more difficult to support. */
799 mn10300_elf_relax_section (abfd, sec, link_info, again)
802 struct bfd_link_info *link_info;
805 Elf_Internal_Shdr *symtab_hdr;
806 Elf_Internal_Rela *internal_relocs = NULL;
807 Elf_Internal_Rela *free_relocs = NULL;
808 Elf_Internal_Rela *irel, *irelend;
809 bfd_byte *contents = NULL;
810 bfd_byte *free_contents = NULL;
811 Elf32_External_Sym *extsyms = NULL;
812 Elf32_External_Sym *free_extsyms = NULL;
813 struct elf32_mn10300_link_hash_table *hash_table;
815 /* Assume nothing changes. */
818 /* We need a pointer to the mn10300 specific hash table. */
819 hash_table = elf32_mn10300_hash_table (link_info);
821 /* Initialize fields in each hash table entry the first time through. */
822 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
826 /* Iterate over all the input bfds. */
827 for (input_bfd = link_info->input_bfds;
829 input_bfd = input_bfd->link_next)
833 /* We're going to need all the symbols for each bfd. */
834 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
836 /* Get cached copy if it exists. */
837 if (symtab_hdr->contents != NULL)
838 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
841 bfd_size_type amt = symtab_hdr->sh_size;
842 /* Go get them off disk. */
843 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
846 free_extsyms = extsyms;
847 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
848 || bfd_bread (extsyms, amt, input_bfd) != amt)
852 /* Iterate over each section in this bfd. */
853 for (section = input_bfd->sections;
855 section = section->next)
857 struct elf32_mn10300_link_hash_entry *hash;
858 Elf_Internal_Sym *sym;
859 asection *sym_sec = NULL;
860 const char *sym_name;
863 /* Get cached copy of section contents if it exists. */
864 if (elf_section_data (section)->this_hdr.contents != NULL)
865 contents = elf_section_data (section)->this_hdr.contents;
866 else if (section->_raw_size != 0)
868 /* Go get them off disk. */
869 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
870 if (contents == NULL)
872 free_contents = contents;
874 if (!bfd_get_section_contents (input_bfd, section,
875 contents, (file_ptr) 0,
882 free_contents = NULL;
885 /* If there aren't any relocs, then there's nothing to do. */
886 if ((section->flags & SEC_RELOC) != 0
887 && section->reloc_count != 0)
890 /* Get a copy of the native relocations. */
891 internal_relocs = (_bfd_elf32_link_read_relocs
892 (input_bfd, section, (PTR) NULL,
893 (Elf_Internal_Rela *) NULL,
894 link_info->keep_memory));
895 if (internal_relocs == NULL)
897 if (! link_info->keep_memory)
898 free_relocs = internal_relocs;
900 /* Now examine each relocation. */
901 irel = internal_relocs;
902 irelend = irel + section->reloc_count;
903 for (; irel < irelend; irel++)
906 unsigned long r_index;
909 r_type = ELF32_R_TYPE (irel->r_info);
910 r_index = ELF32_R_SYM (irel->r_info);
912 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
915 /* We need the name and hash table entry of the target
921 if (r_index < symtab_hdr->sh_info)
923 /* A local symbol. */
924 Elf_Internal_Sym isym;
925 struct elf_link_hash_table *elftab;
928 bfd_elf32_swap_symbol_in (input_bfd,
929 extsyms + r_index, &isym);
931 if (isym.st_shndx == SHN_UNDEF)
932 sym_sec = bfd_und_section_ptr;
933 else if (isym.st_shndx > 0
934 && isym.st_shndx < SHN_LORESERVE)
936 = bfd_section_from_elf_index (input_bfd,
938 else if (isym.st_shndx == SHN_ABS)
939 sym_sec = bfd_abs_section_ptr;
940 else if (isym.st_shndx == SHN_COMMON)
941 sym_sec = bfd_com_section_ptr;
943 sym_name = bfd_elf_string_from_elf_section (input_bfd,
947 /* If it isn't a function, then we don't care
949 if (r_index < symtab_hdr->sh_info
950 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
953 /* Tack on an ID so we can uniquely identify this
954 local symbol in the global hash table. */
955 amt = strlen (sym_name) + 10;
956 new_name = bfd_malloc (amt);
960 sprintf (new_name, "%s_%08x",
961 sym_name, (int) sym_sec);
964 elftab = &hash_table->static_hash_table->root;
965 hash = ((struct elf32_mn10300_link_hash_entry *)
966 elf_link_hash_lookup (elftab, sym_name,
972 r_index -= symtab_hdr->sh_info;
973 hash = (struct elf32_mn10300_link_hash_entry *)
974 elf_sym_hashes (input_bfd)[r_index];
977 /* If this is not a "call" instruction, then we
978 should convert "call" instructions to "calls"
980 code = bfd_get_8 (input_bfd,
981 contents + irel->r_offset - 1);
982 if (code != 0xdd && code != 0xcd)
983 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
985 /* If this is a jump/call, then bump the direct_calls
986 counter. Else force "call" to "calls" conversions. */
987 if (r_type == R_MN10300_PCREL32
988 || r_type == R_MN10300_PCREL16)
989 hash->direct_calls++;
991 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
995 /* Now look at the actual contents to get the stack size,
996 and a list of what registers were saved in the prologue
998 if ((section->flags & SEC_CODE) != 0)
1001 Elf32_External_Sym *esym, *esymend;
1004 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1007 /* Look at each function defined in this section and
1008 update info for that function. */
1010 esymend = esym + symtab_hdr->sh_info;
1011 for (; esym < esymend; esym++)
1013 Elf_Internal_Sym isym;
1015 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1016 if (isym.st_shndx == shndx
1017 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1019 struct elf_link_hash_table *elftab;
1022 if (isym.st_shndx == SHN_UNDEF)
1023 sym_sec = bfd_und_section_ptr;
1024 else if (isym.st_shndx > 0
1025 && isym.st_shndx < SHN_LORESERVE)
1027 = bfd_section_from_elf_index (input_bfd,
1029 else if (isym.st_shndx == SHN_ABS)
1030 sym_sec = bfd_abs_section_ptr;
1031 else if (isym.st_shndx == SHN_COMMON)
1032 sym_sec = bfd_com_section_ptr;
1034 sym_name = (bfd_elf_string_from_elf_section
1035 (input_bfd, symtab_hdr->sh_link,
1038 /* Tack on an ID so we can uniquely identify this
1039 local symbol in the global hash table. */
1040 amt = strlen (sym_name) + 10;
1041 new_name = bfd_malloc (amt);
1045 sprintf (new_name, "%s_%08x",
1046 sym_name, (int) sym_sec);
1047 sym_name = new_name;
1049 elftab = &hash_table->static_hash_table->root;
1050 hash = ((struct elf32_mn10300_link_hash_entry *)
1051 elf_link_hash_lookup (elftab, sym_name,
1052 true, true, false));
1054 compute_function_info (input_bfd, hash,
1055 isym.st_value, contents);
1059 esym = extsyms + symtab_hdr->sh_info;
1060 esymend = extsyms + (symtab_hdr->sh_size
1061 / sizeof (Elf32_External_Sym));
1062 for (idx = 0; esym < esymend; esym++, idx++)
1064 Elf_Internal_Sym isym;
1066 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1067 hash = (struct elf32_mn10300_link_hash_entry *)
1068 elf_sym_hashes (input_bfd)[idx];
1069 if (isym.st_shndx == shndx
1070 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1071 && (hash)->root.root.u.def.section == section
1072 && ((hash)->root.root.type == bfd_link_hash_defined
1073 || (hash)->root.root.type == bfd_link_hash_defweak))
1074 compute_function_info (input_bfd, hash,
1075 (hash)->root.root.u.def.value,
1080 /* Cache or free any memory we allocated for the relocs. */
1081 if (free_relocs != NULL)
1087 /* Cache or free any memory we allocated for the contents. */
1088 if (free_contents != NULL)
1090 if (! link_info->keep_memory)
1091 free (free_contents);
1094 /* Cache the section contents for elf_link_input_bfd. */
1095 elf_section_data (section)->this_hdr.contents = contents;
1097 free_contents = NULL;
1101 /* Cache or free any memory we allocated for the symbols. */
1102 if (free_extsyms != NULL)
1104 if (! link_info->keep_memory)
1105 free (free_extsyms);
1108 /* Cache the symbols for elf_link_input_bfd. */
1109 symtab_hdr->contents = (unsigned char *) extsyms;
1111 free_extsyms = NULL;
1115 /* Now iterate on each symbol in the hash table and perform
1116 the final initialization steps on each. */
1117 elf32_mn10300_link_hash_traverse (hash_table,
1118 elf32_mn10300_finish_hash_table_entry,
1120 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1121 elf32_mn10300_finish_hash_table_entry,
1124 /* All entries in the hash table are fully initialized. */
1125 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1127 /* Now that everything has been initialized, go through each
1128 code section and delete any prologue insns which will be
1129 redundant because their operations will be performed by
1130 a "call" instruction. */
1131 for (input_bfd = link_info->input_bfds;
1133 input_bfd = input_bfd->link_next)
1137 /* We're going to need all the symbols for each bfd. */
1138 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1140 /* Get cached copy if it exists. */
1141 if (symtab_hdr->contents != NULL)
1142 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1145 bfd_size_type amt = symtab_hdr->sh_size;
1146 /* Go get them off disk. */
1147 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1148 if (extsyms == NULL)
1150 free_extsyms = extsyms;
1151 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1152 || bfd_bread (extsyms, amt, input_bfd) != amt)
1156 /* Walk over each section in this bfd. */
1157 for (section = input_bfd->sections;
1159 section = section->next)
1162 Elf32_External_Sym *esym, *esymend;
1165 /* Skip non-code sections and empty sections. */
1166 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1169 if (section->reloc_count != 0)
1171 /* Get a copy of the native relocations. */
1172 internal_relocs = (_bfd_elf32_link_read_relocs
1173 (input_bfd, section, (PTR) NULL,
1174 (Elf_Internal_Rela *) NULL,
1175 link_info->keep_memory));
1176 if (internal_relocs == NULL)
1178 if (! link_info->keep_memory)
1179 free_relocs = internal_relocs;
1182 /* Get cached copy of section contents if it exists. */
1183 if (elf_section_data (section)->this_hdr.contents != NULL)
1184 contents = elf_section_data (section)->this_hdr.contents;
1187 /* Go get them off disk. */
1188 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1189 if (contents == NULL)
1191 free_contents = contents;
1193 if (!bfd_get_section_contents (input_bfd, section,
1194 contents, (file_ptr) 0,
1195 section->_raw_size))
1199 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1201 /* Now look for any function in this section which needs
1202 insns deleted from its prologue. */
1204 esymend = esym + symtab_hdr->sh_info;
1205 for (; esym < esymend; esym++)
1207 Elf_Internal_Sym isym;
1208 struct elf32_mn10300_link_hash_entry *sym_hash;
1209 asection *sym_sec = NULL;
1210 const char *sym_name;
1212 struct elf_link_hash_table *elftab;
1215 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1217 if (isym.st_shndx != shndx)
1220 if (isym.st_shndx == SHN_UNDEF)
1221 sym_sec = bfd_und_section_ptr;
1222 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1224 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1225 else if (isym.st_shndx == SHN_ABS)
1226 sym_sec = bfd_abs_section_ptr;
1227 else if (isym.st_shndx == SHN_COMMON)
1228 sym_sec = bfd_com_section_ptr;
1232 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1233 symtab_hdr->sh_link,
1236 /* Tack on an ID so we can uniquely identify this
1237 local symbol in the global hash table. */
1238 amt = strlen (sym_name) + 10;
1239 new_name = bfd_malloc (amt);
1242 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1243 sym_name = new_name;
1245 elftab = &hash_table->static_hash_table->root;
1246 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1247 elf_link_hash_lookup (elftab, sym_name,
1248 false, false, false));
1251 if (sym_hash == NULL)
1254 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1255 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1259 /* Note that we've changed things. */
1260 elf_section_data (section)->relocs = internal_relocs;
1263 elf_section_data (section)->this_hdr.contents = contents;
1264 free_contents = NULL;
1266 symtab_hdr->contents = (bfd_byte *) extsyms;
1267 free_extsyms = NULL;
1269 /* Count how many bytes we're going to delete. */
1270 if (sym_hash->movm_args)
1273 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1275 else if (sym_hash->stack_size
1276 && sym_hash->stack_size < 256)
1279 /* Note that we've deleted prologue bytes for this
1281 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1283 /* Actually delete the bytes. */
1284 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1290 /* Something changed. Not strictly necessary, but
1291 may lead to more relaxing opportunities. */
1296 /* Look for any global functions in this section which
1297 need insns deleted from their prologues. */
1298 esym = extsyms + symtab_hdr->sh_info;
1299 esymend = extsyms + (symtab_hdr->sh_size
1300 / sizeof (Elf32_External_Sym));
1301 for (idx = 0; esym < esymend; esym++, idx++)
1303 Elf_Internal_Sym isym;
1304 struct elf32_mn10300_link_hash_entry *sym_hash;
1306 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1307 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1308 (elf_sym_hashes (input_bfd)[idx]);
1309 if (isym.st_shndx == shndx
1310 && (sym_hash)->root.root.u.def.section == section
1311 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1312 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1316 /* Note that we've changed things. */
1317 elf_section_data (section)->relocs = internal_relocs;
1320 elf_section_data (section)->this_hdr.contents = contents;
1321 free_contents = NULL;
1323 symtab_hdr->contents = (bfd_byte *) extsyms;
1324 free_extsyms = NULL;
1326 /* Count how many bytes we're going to delete. */
1327 if (sym_hash->movm_args)
1330 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1332 else if (sym_hash->stack_size
1333 && sym_hash->stack_size < 256)
1336 /* Note that we've deleted prologue bytes for this
1338 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1340 /* Actually delete the bytes. */
1341 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1343 (sym_hash)->root.root.u.def.value,
1347 /* Something changed. Not strictly necessary, but
1348 may lead to more relaxing opportunities. */
1353 /* Cache or free any memory we allocated for the relocs. */
1354 if (free_relocs != NULL)
1360 /* Cache or free any memory we allocated for the contents. */
1361 if (free_contents != NULL)
1363 if (! link_info->keep_memory)
1364 free (free_contents);
1367 /* Cache the section contents for elf_link_input_bfd. */
1368 elf_section_data (section)->this_hdr.contents = contents;
1370 free_contents = NULL;
1374 /* Cache or free any memory we allocated for the symbols. */
1375 if (free_extsyms != NULL)
1377 if (! link_info->keep_memory)
1378 free (free_extsyms);
1381 /* Cache the symbols for elf_link_input_bfd. */
1382 symtab_hdr->contents = (unsigned char *) extsyms;
1384 free_extsyms = NULL;
1389 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1392 internal_relocs = NULL;
1394 free_contents = NULL;
1395 free_extsyms = NULL;
1397 /* We don't have to do anything for a relocateable link, if
1398 this section does not have relocs, or if this is not a
1400 if (link_info->relocateable
1401 || (sec->flags & SEC_RELOC) == 0
1402 || sec->reloc_count == 0
1403 || (sec->flags & SEC_CODE) == 0)
1406 /* If this is the first time we have been called for this section,
1407 initialize the cooked size. */
1408 if (sec->_cooked_size == 0)
1409 sec->_cooked_size = sec->_raw_size;
1411 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1413 /* Get a copy of the native relocations. */
1414 internal_relocs = (_bfd_elf32_link_read_relocs
1415 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1416 link_info->keep_memory));
1417 if (internal_relocs == NULL)
1419 if (! link_info->keep_memory)
1420 free_relocs = internal_relocs;
1422 /* Walk through them looking for relaxing opportunities. */
1423 irelend = internal_relocs + sec->reloc_count;
1424 for (irel = internal_relocs; irel < irelend; irel++)
1427 struct elf32_mn10300_link_hash_entry *h = NULL;
1429 /* If this isn't something that can be relaxed, then ignore
1431 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1432 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1433 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1436 /* Get the section contents if we haven't done so already. */
1437 if (contents == NULL)
1439 /* Get cached copy if it exists. */
1440 if (elf_section_data (sec)->this_hdr.contents != NULL)
1441 contents = elf_section_data (sec)->this_hdr.contents;
1444 /* Go get them off disk. */
1445 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1446 if (contents == NULL)
1448 free_contents = contents;
1450 if (! bfd_get_section_contents (abfd, sec, contents,
1451 (file_ptr) 0, sec->_raw_size))
1456 /* Read this BFD's symbols if we haven't done so already. */
1457 if (extsyms == NULL)
1459 /* Get cached copy if it exists. */
1460 if (symtab_hdr->contents != NULL)
1461 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1464 bfd_size_type amt = symtab_hdr->sh_size;
1465 /* Go get them off disk. */
1466 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1467 if (extsyms == NULL)
1469 free_extsyms = extsyms;
1470 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1471 || bfd_bread (extsyms, amt, abfd) != amt)
1476 /* Get the value of the symbol referred to by the reloc. */
1477 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1479 Elf_Internal_Sym isym;
1480 asection *sym_sec = NULL;
1481 const char *sym_name;
1484 /* A local symbol. */
1485 bfd_elf32_swap_symbol_in (abfd,
1486 extsyms + ELF32_R_SYM (irel->r_info),
1489 if (isym.st_shndx == SHN_UNDEF)
1490 sym_sec = bfd_und_section_ptr;
1491 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1492 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1493 else if (isym.st_shndx == SHN_ABS)
1494 sym_sec = bfd_abs_section_ptr;
1495 else if (isym.st_shndx == SHN_COMMON)
1496 sym_sec = bfd_com_section_ptr;
1500 symval = (isym.st_value
1501 + sym_sec->output_section->vma
1502 + sym_sec->output_offset);
1503 sym_name = bfd_elf_string_from_elf_section (abfd,
1504 symtab_hdr->sh_link,
1507 /* Tack on an ID so we can uniquely identify this
1508 local symbol in the global hash table. */
1509 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1512 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1513 sym_name = new_name;
1515 h = (struct elf32_mn10300_link_hash_entry *)
1516 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1517 sym_name, false, false, false);
1524 /* An external symbol. */
1525 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1526 h = (struct elf32_mn10300_link_hash_entry *)
1527 (elf_sym_hashes (abfd)[indx]);
1528 BFD_ASSERT (h != NULL);
1529 if (h->root.root.type != bfd_link_hash_defined
1530 && h->root.root.type != bfd_link_hash_defweak)
1532 /* This appears to be a reference to an undefined
1533 symbol. Just ignore it--it will be caught by the
1534 regular reloc processing. */
1538 symval = (h->root.root.u.def.value
1539 + h->root.root.u.def.section->output_section->vma
1540 + h->root.root.u.def.section->output_offset);
1543 /* For simplicity of coding, we are going to modify the section
1544 contents, the section relocs, and the BFD symbol table. We
1545 must tell the rest of the code not to free up this
1546 information. It would be possible to instead create a table
1547 of changes which have to be made, as is done in coff-mips.c;
1548 that would be more work, but would require less memory when
1549 the linker is run. */
1551 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1552 branch/call, also deal with "call" -> "calls" conversions and
1553 insertion of prologue data into "call" instructions. */
1554 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1556 bfd_vma value = symval;
1558 /* If we've got a "call" instruction that needs to be turned
1559 into a "calls" instruction, do so now. It saves a byte. */
1560 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1564 /* Get the opcode. */
1565 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1567 /* Make sure we're working with a "call" instruction! */
1570 /* Note that we've changed the relocs, section contents,
1572 elf_section_data (sec)->relocs = internal_relocs;
1575 elf_section_data (sec)->this_hdr.contents = contents;
1576 free_contents = NULL;
1578 symtab_hdr->contents = (bfd_byte *) extsyms;
1579 free_extsyms = NULL;
1581 /* Fix the opcode. */
1582 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1583 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1585 /* Fix irel->r_offset and irel->r_addend. */
1586 irel->r_offset += 1;
1587 irel->r_addend += 1;
1589 /* Delete one byte of data. */
1590 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1591 irel->r_offset + 3, 1))
1594 /* That will change things, so, we should relax again.
1595 Note that this is not required, and it may be slow. */
1601 /* We've got a "call" instruction which needs some data
1602 from target function filled in. */
1605 /* Get the opcode. */
1606 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1608 /* Insert data from the target function into the "call"
1609 instruction if needed. */
1612 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1613 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1614 contents + irel->r_offset + 5);
1618 /* Deal with pc-relative gunk. */
1619 value -= (sec->output_section->vma + sec->output_offset);
1620 value -= irel->r_offset;
1621 value += irel->r_addend;
1623 /* See if the value will fit in 16 bits, note the high value is
1624 0x7fff + 2 as the target will be two bytes closer if we are
1626 if ((long) value < 0x8001 && (long) value > -0x8000)
1630 /* Get the opcode. */
1631 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1633 if (code != 0xdc && code != 0xdd && code != 0xff)
1636 /* Note that we've changed the relocs, section contents, etc. */
1637 elf_section_data (sec)->relocs = internal_relocs;
1640 elf_section_data (sec)->this_hdr.contents = contents;
1641 free_contents = NULL;
1643 symtab_hdr->contents = (bfd_byte *) extsyms;
1644 free_extsyms = NULL;
1646 /* Fix the opcode. */
1648 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1649 else if (code == 0xdd)
1650 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1651 else if (code == 0xff)
1652 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1654 /* Fix the relocation's type. */
1655 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1658 /* Delete two bytes of data. */
1659 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1660 irel->r_offset + 1, 2))
1663 /* That will change things, so, we should relax again.
1664 Note that this is not required, and it may be slow. */
1669 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1671 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1673 bfd_vma value = symval;
1675 /* If we've got a "call" instruction that needs to be turned
1676 into a "calls" instruction, do so now. It saves a byte. */
1677 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1681 /* Get the opcode. */
1682 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1684 /* Make sure we're working with a "call" instruction! */
1687 /* Note that we've changed the relocs, section contents,
1689 elf_section_data (sec)->relocs = internal_relocs;
1692 elf_section_data (sec)->this_hdr.contents = contents;
1693 free_contents = NULL;
1695 symtab_hdr->contents = (bfd_byte *) extsyms;
1696 free_extsyms = NULL;
1698 /* Fix the opcode. */
1699 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1700 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1702 /* Fix irel->r_offset and irel->r_addend. */
1703 irel->r_offset += 1;
1704 irel->r_addend += 1;
1706 /* Delete one byte of data. */
1707 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1708 irel->r_offset + 1, 1))
1711 /* That will change things, so, we should relax again.
1712 Note that this is not required, and it may be slow. */
1720 /* Get the opcode. */
1721 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1723 /* Insert data from the target function into the "call"
1724 instruction if needed. */
1727 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1728 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1729 contents + irel->r_offset + 3);
1733 /* Deal with pc-relative gunk. */
1734 value -= (sec->output_section->vma + sec->output_offset);
1735 value -= irel->r_offset;
1736 value += irel->r_addend;
1738 /* See if the value will fit in 8 bits, note the high value is
1739 0x7f + 1 as the target will be one bytes closer if we are
1741 if ((long) value < 0x80 && (long) value > -0x80)
1745 /* Get the opcode. */
1746 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1751 /* Note that we've changed the relocs, section contents, etc. */
1752 elf_section_data (sec)->relocs = internal_relocs;
1755 elf_section_data (sec)->this_hdr.contents = contents;
1756 free_contents = NULL;
1758 symtab_hdr->contents = (bfd_byte *) extsyms;
1759 free_extsyms = NULL;
1761 /* Fix the opcode. */
1762 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1764 /* Fix the relocation's type. */
1765 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1768 /* Delete one byte of data. */
1769 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1770 irel->r_offset + 1, 1))
1773 /* That will change things, so, we should relax again.
1774 Note that this is not required, and it may be slow. */
1779 /* Try to eliminate an unconditional 8 bit pc-relative branch
1780 which immediately follows a conditional 8 bit pc-relative
1781 branch around the unconditional branch.
1788 This happens when the bCC can't reach lab2 at assembly time,
1789 but due to other relaxations it can reach at link time. */
1790 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1792 Elf_Internal_Rela *nrel;
1793 bfd_vma value = symval;
1796 /* Deal with pc-relative gunk. */
1797 value -= (sec->output_section->vma + sec->output_offset);
1798 value -= irel->r_offset;
1799 value += irel->r_addend;
1801 /* Do nothing if this reloc is the last byte in the section. */
1802 if (irel->r_offset == sec->_cooked_size)
1805 /* See if the next instruction is an unconditional pc-relative
1806 branch, more often than not this test will fail, so we
1807 test it first to speed things up. */
1808 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1812 /* Also make sure the next relocation applies to the next
1813 instruction and that it's a pc-relative 8 bit branch. */
1816 || irel->r_offset + 2 != nrel->r_offset
1817 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1820 /* Make sure our destination immediately follows the
1821 unconditional branch. */
1822 if (symval != (sec->output_section->vma + sec->output_offset
1823 + irel->r_offset + 3))
1826 /* Now make sure we are a conditional branch. This may not
1827 be necessary, but why take the chance.
1829 Note these checks assume that R_MN10300_PCREL8 relocs
1830 only occur on bCC and bCCx insns. If they occured
1831 elsewhere, we'd need to know the start of this insn
1832 for this check to be accurate. */
1833 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1834 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1835 && code != 0xc3 && code != 0xc4 && code != 0xc5
1836 && code != 0xc6 && code != 0xc7 && code != 0xc8
1837 && code != 0xc9 && code != 0xe8 && code != 0xe9
1838 && code != 0xea && code != 0xeb)
1841 /* We also have to be sure there is no symbol/label
1842 at the unconditional branch. */
1843 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1844 irel->r_offset + 1))
1847 /* Note that we've changed the relocs, section contents, etc. */
1848 elf_section_data (sec)->relocs = internal_relocs;
1851 elf_section_data (sec)->this_hdr.contents = contents;
1852 free_contents = NULL;
1854 symtab_hdr->contents = (bfd_byte *) extsyms;
1855 free_extsyms = NULL;
1857 /* Reverse the condition of the first branch. */
1903 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1905 /* Set the reloc type and symbol for the first branch
1906 from the second branch. */
1907 irel->r_info = nrel->r_info;
1909 /* Make the reloc for the second branch a null reloc. */
1910 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1913 /* Delete two bytes of data. */
1914 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1915 irel->r_offset + 1, 2))
1918 /* That will change things, so, we should relax again.
1919 Note that this is not required, and it may be slow. */
1923 /* Try to turn a 24 immediate, displacement or absolute address
1924 into a 8 immediate, displacement or absolute address. */
1925 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1927 bfd_vma value = symval;
1928 value += irel->r_addend;
1930 /* See if the value will fit in 8 bits. */
1931 if ((long) value < 0x7f && (long) value > -0x80)
1935 /* AM33 insns which have 24 operands are 6 bytes long and
1936 will have 0xfd as the first byte. */
1938 /* Get the first opcode. */
1939 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1943 /* Get the second opcode. */
1944 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1946 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1947 equivalent instructions exists. */
1948 if (code != 0x6b && code != 0x7b
1949 && code != 0x8b && code != 0x9b
1950 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1951 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1952 || (code & 0x0f) == 0x0e))
1954 /* Not safe if the high bit is on as relaxing may
1955 move the value out of high mem and thus not fit
1956 in a signed 8bit value. This is currently over
1958 if ((value & 0x80) == 0)
1960 /* Note that we've changed the relocation contents,
1962 elf_section_data (sec)->relocs = internal_relocs;
1965 elf_section_data (sec)->this_hdr.contents = contents;
1966 free_contents = NULL;
1968 symtab_hdr->contents = (bfd_byte *) extsyms;
1969 free_extsyms = NULL;
1971 /* Fix the opcode. */
1972 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1973 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1975 /* Fix the relocation's type. */
1977 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1980 /* Delete two bytes of data. */
1981 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1982 irel->r_offset + 1, 2))
1985 /* That will change things, so, we should relax
1986 again. Note that this is not required, and it
1996 /* Try to turn a 32bit immediate, displacement or absolute address
1997 into a 16bit immediate, displacement or absolute address. */
1998 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
2000 bfd_vma value = symval;
2001 value += irel->r_addend;
2003 /* See if the value will fit in 24 bits.
2004 We allow any 16bit match here. We prune those we can't
2006 if ((long) value < 0x7fffff && (long) value > -0x800000)
2010 /* AM33 insns which have 32bit operands are 7 bytes long and
2011 will have 0xfe as the first byte. */
2013 /* Get the first opcode. */
2014 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2018 /* Get the second opcode. */
2019 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2021 /* All the am33 32 -> 24 relaxing possibilities. */
2022 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2023 equivalent instructions exists. */
2024 if (code != 0x6b && code != 0x7b
2025 && code != 0x8b && code != 0x9b
2026 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2027 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2028 || (code & 0x0f) == 0x0e))
2030 /* Not safe if the high bit is on as relaxing may
2031 move the value out of high mem and thus not fit
2032 in a signed 16bit value. This is currently over
2034 if ((value & 0x8000) == 0)
2036 /* Note that we've changed the relocation contents,
2038 elf_section_data (sec)->relocs = internal_relocs;
2041 elf_section_data (sec)->this_hdr.contents = contents;
2042 free_contents = NULL;
2044 symtab_hdr->contents = (bfd_byte *) extsyms;
2045 free_extsyms = NULL;
2047 /* Fix the opcode. */
2048 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2049 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2051 /* Fix the relocation's type. */
2053 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2056 /* Delete one byte of data. */
2057 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2058 irel->r_offset + 3, 1))
2061 /* That will change things, so, we should relax
2062 again. Note that this is not required, and it
2071 /* See if the value will fit in 16 bits.
2072 We allow any 16bit match here. We prune those we can't
2074 if ((long) value < 0x7fff && (long) value > -0x8000)
2078 /* Most insns which have 32bit operands are 6 bytes long;
2079 exceptions are pcrel insns and bit insns.
2081 We handle pcrel insns above. We don't bother trying
2082 to handle the bit insns here.
2084 The first byte of the remaining insns will be 0xfc. */
2086 /* Get the first opcode. */
2087 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2092 /* Get the second opcode. */
2093 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2095 if ((code & 0xf0) < 0x80)
2096 switch (code & 0xf0)
2098 /* mov (d32,am),dn -> mov (d32,am),dn
2099 mov dm,(d32,am) -> mov dn,(d32,am)
2100 mov (d32,am),an -> mov (d32,am),an
2101 mov dm,(d32,am) -> mov dn,(d32,am)
2102 movbu (d32,am),dn -> movbu (d32,am),dn
2103 movbu dm,(d32,am) -> movbu dn,(d32,am)
2104 movhu (d32,am),dn -> movhu (d32,am),dn
2105 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2114 /* Not safe if the high bit is on as relaxing may
2115 move the value out of high mem and thus not fit
2116 in a signed 16bit value. */
2118 && (value & 0x8000))
2121 /* Note that we've changed the relocation contents, etc. */
2122 elf_section_data (sec)->relocs = internal_relocs;
2125 elf_section_data (sec)->this_hdr.contents = contents;
2126 free_contents = NULL;
2128 symtab_hdr->contents = (bfd_byte *) extsyms;
2129 free_extsyms = NULL;
2131 /* Fix the opcode. */
2132 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2133 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2135 /* Fix the relocation's type. */
2136 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2139 /* Delete two bytes of data. */
2140 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2141 irel->r_offset + 2, 2))
2144 /* That will change things, so, we should relax again.
2145 Note that this is not required, and it may be slow. */
2149 else if ((code & 0xf0) == 0x80
2150 || (code & 0xf0) == 0x90)
2151 switch (code & 0xf3)
2153 /* mov dn,(abs32) -> mov dn,(abs16)
2154 movbu dn,(abs32) -> movbu dn,(abs16)
2155 movhu dn,(abs32) -> movhu dn,(abs16) */
2159 /* Note that we've changed the relocation contents, etc. */
2160 elf_section_data (sec)->relocs = internal_relocs;
2163 elf_section_data (sec)->this_hdr.contents = contents;
2164 free_contents = NULL;
2166 symtab_hdr->contents = (bfd_byte *) extsyms;
2167 free_extsyms = NULL;
2169 if ((code & 0xf3) == 0x81)
2170 code = 0x01 + (code & 0x0c);
2171 else if ((code & 0xf3) == 0x82)
2172 code = 0x02 + (code & 0x0c);
2173 else if ((code & 0xf3) == 0x83)
2174 code = 0x03 + (code & 0x0c);
2178 /* Fix the opcode. */
2179 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2181 /* Fix the relocation's type. */
2182 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2185 /* The opcode got shorter too, so we have to fix the
2186 addend and offset too! */
2187 irel->r_offset -= 1;
2189 /* Delete three bytes of data. */
2190 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2191 irel->r_offset + 1, 3))
2194 /* That will change things, so, we should relax again.
2195 Note that this is not required, and it may be slow. */
2199 /* mov am,(abs32) -> mov am,(abs16)
2200 mov am,(d32,sp) -> mov am,(d16,sp)
2201 mov dm,(d32,sp) -> mov dm,(d32,sp)
2202 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2203 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2209 /* sp-based offsets are zero-extended. */
2210 if (code >= 0x90 && code <= 0x93
2214 /* Note that we've changed the relocation contents, etc. */
2215 elf_section_data (sec)->relocs = internal_relocs;
2218 elf_section_data (sec)->this_hdr.contents = contents;
2219 free_contents = NULL;
2221 symtab_hdr->contents = (bfd_byte *) extsyms;
2222 free_extsyms = NULL;
2224 /* Fix the opcode. */
2225 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2226 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2228 /* Fix the relocation's type. */
2229 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2232 /* Delete two bytes of data. */
2233 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2234 irel->r_offset + 2, 2))
2237 /* That will change things, so, we should relax again.
2238 Note that this is not required, and it may be slow. */
2242 else if ((code & 0xf0) < 0xf0)
2243 switch (code & 0xfc)
2245 /* mov imm32,dn -> mov imm16,dn
2246 mov imm32,an -> mov imm16,an
2247 mov (abs32),dn -> mov (abs16),dn
2248 movbu (abs32),dn -> movbu (abs16),dn
2249 movhu (abs32),dn -> movhu (abs16),dn */
2255 /* Not safe if the high bit is on as relaxing may
2256 move the value out of high mem and thus not fit
2257 in a signed 16bit value. */
2259 && (value & 0x8000))
2262 /* mov imm16, an zero-extends the immediate. */
2267 /* Note that we've changed the relocation contents, etc. */
2268 elf_section_data (sec)->relocs = internal_relocs;
2271 elf_section_data (sec)->this_hdr.contents = contents;
2272 free_contents = NULL;
2274 symtab_hdr->contents = (bfd_byte *) extsyms;
2275 free_extsyms = NULL;
2277 if ((code & 0xfc) == 0xcc)
2278 code = 0x2c + (code & 0x03);
2279 else if ((code & 0xfc) == 0xdc)
2280 code = 0x24 + (code & 0x03);
2281 else if ((code & 0xfc) == 0xa4)
2282 code = 0x30 + (code & 0x03);
2283 else if ((code & 0xfc) == 0xa8)
2284 code = 0x34 + (code & 0x03);
2285 else if ((code & 0xfc) == 0xac)
2286 code = 0x38 + (code & 0x03);
2290 /* Fix the opcode. */
2291 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2293 /* Fix the relocation's type. */
2294 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2297 /* The opcode got shorter too, so we have to fix the
2298 addend and offset too! */
2299 irel->r_offset -= 1;
2301 /* Delete three bytes of data. */
2302 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2303 irel->r_offset + 1, 3))
2306 /* That will change things, so, we should relax again.
2307 Note that this is not required, and it may be slow. */
2311 /* mov (abs32),an -> mov (abs16),an
2312 mov (d32,sp),an -> mov (d16,sp),an
2313 mov (d32,sp),dn -> mov (d16,sp),dn
2314 movbu (d32,sp),dn -> movbu (d16,sp),dn
2315 movhu (d32,sp),dn -> movhu (d16,sp),dn
2316 add imm32,dn -> add imm16,dn
2317 cmp imm32,dn -> cmp imm16,dn
2318 add imm32,an -> add imm16,an
2319 cmp imm32,an -> cmp imm16,an
2320 and imm32,dn -> and imm16,dn
2321 or imm32,dn -> or imm16,dn
2322 xor imm32,dn -> xor imm16,dn
2323 btst imm32,dn -> btst imm16,dn */
2339 /* cmp imm16, an zero-extends the immediate. */
2344 /* So do sp-based offsets. */
2345 if (code >= 0xb0 && code <= 0xb3
2349 /* Note that we've changed the relocation contents, etc. */
2350 elf_section_data (sec)->relocs = internal_relocs;
2353 elf_section_data (sec)->this_hdr.contents = contents;
2354 free_contents = NULL;
2356 symtab_hdr->contents = (bfd_byte *) extsyms;
2357 free_extsyms = NULL;
2359 /* Fix the opcode. */
2360 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2361 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2363 /* Fix the relocation's type. */
2364 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2367 /* Delete two bytes of data. */
2368 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2369 irel->r_offset + 2, 2))
2372 /* That will change things, so, we should relax again.
2373 Note that this is not required, and it may be slow. */
2377 else if (code == 0xfe)
2379 /* add imm32,sp -> add imm16,sp */
2381 /* Note that we've changed the relocation contents, etc. */
2382 elf_section_data (sec)->relocs = internal_relocs;
2385 elf_section_data (sec)->this_hdr.contents = contents;
2386 free_contents = NULL;
2388 symtab_hdr->contents = (bfd_byte *) extsyms;
2389 free_extsyms = NULL;
2391 /* Fix the opcode. */
2392 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2393 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2395 /* Fix the relocation's type. */
2396 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2399 /* Delete two bytes of data. */
2400 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2401 irel->r_offset + 2, 2))
2404 /* That will change things, so, we should relax again.
2405 Note that this is not required, and it may be slow. */
2413 if (free_relocs != NULL)
2419 if (free_contents != NULL)
2421 if (! link_info->keep_memory)
2422 free (free_contents);
2425 /* Cache the section contents for elf_link_input_bfd. */
2426 elf_section_data (sec)->this_hdr.contents = contents;
2428 free_contents = NULL;
2431 if (free_extsyms != NULL)
2433 if (! link_info->keep_memory)
2434 free (free_extsyms);
2437 /* Cache the symbols for elf_link_input_bfd. */
2438 symtab_hdr->contents = (unsigned char *) extsyms;
2440 free_extsyms = NULL;
2446 if (free_relocs != NULL)
2448 if (free_contents != NULL)
2449 free (free_contents);
2450 if (free_extsyms != NULL)
2451 free (free_extsyms);
2455 /* Compute the stack size and movm arguments for the function
2456 referred to by HASH at address ADDR in section with
2457 contents CONTENTS, store the information in the hash table. */
2459 compute_function_info (abfd, hash, addr, contents)
2461 struct elf32_mn10300_link_hash_entry *hash;
2463 unsigned char *contents;
2465 unsigned char byte1, byte2;
2466 /* We only care about a very small subset of the possible prologue
2467 sequences here. Basically we look for:
2469 movm [d2,d3,a2,a3],sp (optional)
2470 add <size>,sp (optional, and only for sizes which fit in an unsigned
2473 If we find anything else, we quit. */
2475 /* Look for movm [regs],sp */
2476 byte1 = bfd_get_8 (abfd, contents + addr);
2477 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2481 hash->movm_args = byte2;
2483 byte1 = bfd_get_8 (abfd, contents + addr);
2484 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2487 /* Now figure out how much stack space will be allocated by the movm
2488 instruction. We need this kept separate from the funtion's normal
2490 if (hash->movm_args)
2493 if (hash->movm_args & 0x80)
2494 hash->movm_stack_size += 4;
2497 if (hash->movm_args & 0x40)
2498 hash->movm_stack_size += 4;
2501 if (hash->movm_args & 0x20)
2502 hash->movm_stack_size += 4;
2505 if (hash->movm_args & 0x10)
2506 hash->movm_stack_size += 4;
2508 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2509 if (hash->movm_args & 0x08)
2510 hash->movm_stack_size += 8 * 4;
2512 if (bfd_get_mach (abfd) == bfd_mach_am33)
2514 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2515 if (hash->movm_args & 0x1)
2516 hash->movm_stack_size += 6 * 4;
2518 /* exreg1 space. e4, e5, e6, e7 */
2519 if (hash->movm_args & 0x2)
2520 hash->movm_stack_size += 4 * 4;
2522 /* exreg0 space. e2, e3 */
2523 if (hash->movm_args & 0x4)
2524 hash->movm_stack_size += 2 * 4;
2528 /* Now look for the two stack adjustment variants. */
2529 if (byte1 == 0xf8 && byte2 == 0xfe)
2531 int temp = bfd_get_8 (abfd, contents + addr + 2);
2532 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2534 hash->stack_size = -temp;
2536 else if (byte1 == 0xfa && byte2 == 0xfe)
2538 int temp = bfd_get_16 (abfd, contents + addr + 2);
2539 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2543 hash->stack_size = temp;
2546 /* If the total stack to be allocated by the call instruction is more
2547 than 255 bytes, then we can't remove the stack adjustment by using
2548 "call" (we might still be able to remove the "movm" instruction. */
2549 if (hash->stack_size + hash->movm_stack_size > 255)
2550 hash->stack_size = 0;
2555 /* Delete some bytes from a section while relaxing. */
2558 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2564 Elf_Internal_Shdr *symtab_hdr;
2565 Elf32_External_Sym *extsyms;
2568 Elf_Internal_Rela *irel, *irelend;
2569 Elf_Internal_Rela *irelalign;
2571 Elf32_External_Sym *esym, *esymend;
2572 struct elf32_mn10300_link_hash_entry *sym_hash;
2574 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2575 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2577 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2579 contents = elf_section_data (sec)->this_hdr.contents;
2581 /* The deletion must stop at the next ALIGN reloc for an aligment
2582 power larger than the number of bytes we are deleting. */
2585 toaddr = sec->_cooked_size;
2587 irel = elf_section_data (sec)->relocs;
2588 irelend = irel + sec->reloc_count;
2590 /* Actually delete the bytes. */
2591 memmove (contents + addr, contents + addr + count,
2592 (size_t) (toaddr - addr - count));
2593 sec->_cooked_size -= count;
2595 /* Adjust all the relocs. */
2596 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2598 /* Get the new reloc address. */
2599 if ((irel->r_offset > addr
2600 && irel->r_offset < toaddr))
2601 irel->r_offset -= count;
2604 /* Adjust the local symbols defined in this section. */
2606 esymend = esym + symtab_hdr->sh_info;
2607 for (; esym < esymend; esym++)
2609 Elf_Internal_Sym isym;
2611 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2613 if (isym.st_shndx == shndx
2614 && isym.st_value > addr
2615 && isym.st_value < toaddr)
2617 isym.st_value -= count;
2618 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2622 /* Now adjust the global symbols defined in this section. */
2623 esym = extsyms + symtab_hdr->sh_info;
2624 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2625 for (index = 0; esym < esymend; esym++, index++)
2627 Elf_Internal_Sym isym;
2629 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2630 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2631 (elf_sym_hashes (abfd)[index]);
2632 if (isym.st_shndx == shndx
2633 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2634 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2635 && (sym_hash)->root.root.u.def.section == sec
2636 && (sym_hash)->root.root.u.def.value > addr
2637 && (sym_hash)->root.root.u.def.value < toaddr)
2639 (sym_hash)->root.root.u.def.value -= count;
2646 /* Return true if a symbol exists at the given address, else return
2649 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2652 Elf32_External_Sym *extsyms;
2655 Elf_Internal_Shdr *symtab_hdr;
2657 Elf32_External_Sym *esym, *esymend;
2658 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2660 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2661 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2663 /* Examine all the symbols. */
2665 esymend = esym + symtab_hdr->sh_info;
2666 for (; esym < esymend; esym++)
2668 Elf_Internal_Sym isym;
2670 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2672 if (isym.st_shndx == shndx
2673 && isym.st_value == addr)
2677 sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd));
2678 sym_hash_end = (sym_hash
2679 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2680 - symtab_hdr->sh_info));
2681 for (; sym_hash < sym_hash_end; sym_hash++)
2683 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2684 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2685 && (*sym_hash)->root.root.u.def.section == sec
2686 && (*sym_hash)->root.root.u.def.value == addr)
2692 /* This is a version of bfd_generic_get_relocated_section_contents
2693 which uses mn10300_elf_relocate_section. */
2696 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2697 data, relocateable, symbols)
2699 struct bfd_link_info *link_info;
2700 struct bfd_link_order *link_order;
2702 boolean relocateable;
2705 Elf_Internal_Shdr *symtab_hdr;
2706 asection *input_section = link_order->u.indirect.section;
2707 bfd *input_bfd = input_section->owner;
2708 asection **sections = NULL;
2709 Elf_Internal_Rela *internal_relocs = NULL;
2710 Elf32_External_Sym *external_syms = NULL;
2711 Elf_Internal_Sym *internal_syms = NULL;
2713 /* We only need to handle the case of relaxing, or of having a
2714 particular set of section contents, specially. */
2716 || elf_section_data (input_section)->this_hdr.contents == NULL)
2717 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2722 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2724 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2725 (size_t) input_section->_raw_size);
2727 if ((input_section->flags & SEC_RELOC) != 0
2728 && input_section->reloc_count > 0)
2730 Elf_Internal_Sym *isymp;
2732 Elf32_External_Sym *esym, *esymend;
2735 if (symtab_hdr->contents != NULL)
2736 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2739 size = symtab_hdr->sh_info;
2740 size *= sizeof (Elf32_External_Sym);
2741 external_syms = (Elf32_External_Sym *) bfd_malloc (size);
2742 if (external_syms == NULL && size != 0)
2744 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2745 || bfd_bread (external_syms, size, input_bfd) != size)
2749 internal_relocs = (_bfd_elf32_link_read_relocs
2750 (input_bfd, input_section, (PTR) NULL,
2751 (Elf_Internal_Rela *) NULL, false));
2752 if (internal_relocs == NULL)
2755 size = symtab_hdr->sh_info;
2756 size *= sizeof (Elf_Internal_Sym);
2757 internal_syms = (Elf_Internal_Sym *) bfd_malloc (size);
2758 if (internal_syms == NULL && size != 0)
2761 size = symtab_hdr->sh_info;
2762 size *= sizeof (asection *);
2763 sections = (asection **) bfd_malloc (size);
2764 if (sections == NULL && size != 0)
2767 isymp = internal_syms;
2769 esym = external_syms;
2770 esymend = esym + symtab_hdr->sh_info;
2771 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2775 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2777 if (isymp->st_shndx == SHN_UNDEF)
2778 isec = bfd_und_section_ptr;
2779 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2780 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2781 else if (isymp->st_shndx == SHN_ABS)
2782 isec = bfd_abs_section_ptr;
2783 else if (isymp->st_shndx == SHN_COMMON)
2784 isec = bfd_com_section_ptr;
2794 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2795 input_section, data, internal_relocs,
2796 internal_syms, sections))
2799 if (sections != NULL)
2802 if (internal_syms != NULL)
2803 free (internal_syms);
2804 internal_syms = NULL;
2805 if (external_syms != NULL && symtab_hdr->contents == NULL)
2806 free (external_syms);
2807 external_syms = NULL;
2808 if (internal_relocs != elf_section_data (input_section)->relocs)
2809 free (internal_relocs);
2810 internal_relocs = NULL;
2816 if (internal_relocs != NULL
2817 && internal_relocs != elf_section_data (input_section)->relocs)
2818 free (internal_relocs);
2819 if (external_syms != NULL && symtab_hdr->contents == NULL)
2820 free (external_syms);
2821 if (internal_syms != NULL)
2822 free (internal_syms);
2823 if (sections != NULL)
2828 /* Assorted hash table functions. */
2830 /* Initialize an entry in the link hash table. */
2832 /* Create an entry in an MN10300 ELF linker hash table. */
2834 static struct bfd_hash_entry *
2835 elf32_mn10300_link_hash_newfunc (entry, table, string)
2836 struct bfd_hash_entry *entry;
2837 struct bfd_hash_table *table;
2840 struct elf32_mn10300_link_hash_entry *ret =
2841 (struct elf32_mn10300_link_hash_entry *) entry;
2843 /* Allocate the structure if it has not already been allocated by a
2845 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2846 ret = ((struct elf32_mn10300_link_hash_entry *)
2847 bfd_hash_allocate (table,
2848 sizeof (struct elf32_mn10300_link_hash_entry)));
2849 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2850 return (struct bfd_hash_entry *) ret;
2852 /* Call the allocation method of the superclass. */
2853 ret = ((struct elf32_mn10300_link_hash_entry *)
2854 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2856 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2858 ret->direct_calls = 0;
2859 ret->stack_size = 0;
2860 ret->movm_stack_size = 0;
2865 return (struct bfd_hash_entry *) ret;
2868 /* Create an mn10300 ELF linker hash table. */
2870 static struct bfd_link_hash_table *
2871 elf32_mn10300_link_hash_table_create (abfd)
2874 struct elf32_mn10300_link_hash_table *ret;
2875 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2877 ret = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2878 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2881 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2882 elf32_mn10300_link_hash_newfunc))
2884 bfd_release (abfd, ret);
2889 amt = sizeof (struct elf_link_hash_table);
2890 ret->static_hash_table
2891 = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2892 if (ret->static_hash_table == NULL)
2894 bfd_release (abfd, ret);
2898 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2899 elf32_mn10300_link_hash_newfunc))
2901 bfd_release (abfd, ret->static_hash_table);
2902 bfd_release (abfd, ret);
2905 return &ret->root.root;
2908 static unsigned long
2909 elf_mn10300_mach (flags)
2912 switch (flags & EF_MN10300_MACH)
2914 case E_MN10300_MACH_MN10300:
2916 return bfd_mach_mn10300;
2918 case E_MN10300_MACH_AM33:
2919 return bfd_mach_am33;
2923 /* The final processing done just before writing out a MN10300 ELF object
2924 file. This gets the MN10300 architecture right based on the machine
2928 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2930 boolean linker ATTRIBUTE_UNUSED;
2934 switch (bfd_get_mach (abfd))
2937 case bfd_mach_mn10300:
2938 val = E_MN10300_MACH_MN10300;
2942 val = E_MN10300_MACH_AM33;
2946 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2947 elf_elfheader (abfd)->e_flags |= val;
2951 _bfd_mn10300_elf_object_p (abfd)
2954 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2955 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2959 /* Merge backend specific data from an object file to the output
2960 object file when linking. */
2963 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2967 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2968 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2971 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2972 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2974 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2975 bfd_get_mach (ibfd)))
2982 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2983 #define TARGET_LITTLE_NAME "elf32-mn10300"
2984 #define ELF_ARCH bfd_arch_mn10300
2985 #define ELF_MACHINE_CODE EM_MN10300
2986 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
2987 #define ELF_MAXPAGESIZE 0x1000
2989 #define elf_info_to_howto mn10300_info_to_howto
2990 #define elf_info_to_howto_rel 0
2991 #define elf_backend_can_gc_sections 1
2992 #define elf_backend_check_relocs mn10300_elf_check_relocs
2993 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2994 #define elf_backend_relocate_section mn10300_elf_relocate_section
2995 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2996 #define bfd_elf32_bfd_get_relocated_section_contents \
2997 mn10300_elf_get_relocated_section_contents
2998 #define bfd_elf32_bfd_link_hash_table_create \
2999 elf32_mn10300_link_hash_table_create
3001 #define elf_symbol_leading_char '_'
3003 /* So we can set bits in e_flags. */
3004 #define elf_backend_final_write_processing \
3005 _bfd_mn10300_elf_final_write_processing
3006 #define elf_backend_object_p _bfd_mn10300_elf_object_p
3008 #define bfd_elf32_bfd_merge_private_bfd_data \
3009 _bfd_mn10300_elf_merge_private_bfd_data
3011 #include "elf32-target.h"