1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "elf/mn10300.h"
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *,
29 bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
31 static boolean mn10300_elf_relocate_section
32 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
33 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
34 static boolean mn10300_elf_relax_section
35 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
36 static bfd_byte * mn10300_elf_get_relocated_section_contents
37 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
38 bfd_byte *, boolean, asymbol **));
39 static unsigned long elf_mn10300_mach PARAMS ((flagword));
41 void _bfd_mn10300_elf_final_write_processing PARAMS ((bfd *, boolean));
42 boolean _bfd_mn10300_elf_object_p PARAMS ((bfd *));
43 boolean _bfd_mn10300_elf_merge_private_bfd_data PARAMS ((bfd *,bfd *));
45 struct elf32_mn10300_link_hash_entry {
46 /* The basic elf link hash table entry. */
47 struct elf_link_hash_entry root;
49 /* For function symbols, the number of times this function is
50 called directly (ie by name). */
51 unsigned int direct_calls;
53 /* For function symbols, the size of this function's stack
54 (if <= 255 bytes). We stuff this into "call" instructions
55 to this target when it's valid and profitable to do so.
57 This does not include stack allocated by movm! */
58 unsigned char stack_size;
60 /* For function symbols, arguments (if any) for movm instruction
61 in the prologue. We stuff this value into "call" instructions
62 to the target when it's valid and profitable to do so. */
63 unsigned char movm_args;
65 /* For funtion symbols, the amount of stack space that would be allocated
66 by the movm instruction. This is redundant with movm_args, but we
67 add it to the hash table to avoid computing it over and over. */
68 unsigned char movm_stack_size;
70 /* When set, convert all "call" instructions to this target into "calls"
72 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
74 /* Used to mark functions which have had redundant parts of their
76 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
80 /* We derive a hash table from the main elf linker hash table so
81 we can store state variables and a secondary hash table without
82 resorting to global variables. */
83 struct elf32_mn10300_link_hash_table {
84 /* The main hash table. */
85 struct elf_link_hash_table root;
87 /* A hash table for static functions. We could derive a new hash table
88 instead of using the full elf32_mn10300_link_hash_table if we wanted
89 to save some memory. */
90 struct elf32_mn10300_link_hash_table *static_hash_table;
92 /* Random linker state flags. */
93 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
97 /* For MN10300 linker hash table. */
99 /* Get the MN10300 ELF linker hash table from a link_info structure. */
101 #define elf32_mn10300_hash_table(p) \
102 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
107 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
110 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
111 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
112 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
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 *, 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 return bfd_section_from_elf_index (abfd, sym->st_shndx);
436 /* Perform a relocation as part of a final link. */
437 static bfd_reloc_status_type
438 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
439 input_section, contents, offset, value,
440 addend, info, sym_sec, is_local)
441 reloc_howto_type *howto;
443 bfd *output_bfd ATTRIBUTE_UNUSED;
444 asection *input_section;
449 struct bfd_link_info *info ATTRIBUTE_UNUSED;
450 asection *sym_sec ATTRIBUTE_UNUSED;
451 int is_local ATTRIBUTE_UNUSED;
453 unsigned long r_type = howto->type;
454 bfd_byte *hit_data = contents + offset;
463 bfd_put_32 (input_bfd, value, hit_data);
469 if ((long) value > 0x7fffff || (long) value < -0x800000)
470 return bfd_reloc_overflow;
472 bfd_put_8 (input_bfd, value & 0xff, hit_data);
473 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
474 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
480 if ((long) value > 0x7fff || (long) value < -0x8000)
481 return bfd_reloc_overflow;
483 bfd_put_16 (input_bfd, value, hit_data);
489 if ((long) value > 0x7f || (long) value < -0x80)
490 return bfd_reloc_overflow;
492 bfd_put_8 (input_bfd, value, hit_data);
495 case R_MN10300_PCREL8:
496 value -= (input_section->output_section->vma
497 + input_section->output_offset);
501 if ((long) value > 0xff || (long) value < -0x100)
502 return bfd_reloc_overflow;
504 bfd_put_8 (input_bfd, value, hit_data);
507 case R_MN10300_PCREL16:
508 value -= (input_section->output_section->vma
509 + input_section->output_offset);
513 if ((long) value > 0xffff || (long) value < -0x10000)
514 return bfd_reloc_overflow;
516 bfd_put_16 (input_bfd, value, hit_data);
519 case R_MN10300_PCREL32:
520 value -= (input_section->output_section->vma
521 + input_section->output_offset);
525 bfd_put_32 (input_bfd, value, hit_data);
528 case R_MN10300_GNU_VTINHERIT:
529 case R_MN10300_GNU_VTENTRY:
533 return bfd_reloc_notsupported;
537 /* Relocate an MN10300 ELF section. */
539 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
540 contents, relocs, local_syms, local_sections)
542 struct bfd_link_info *info;
544 asection *input_section;
546 Elf_Internal_Rela *relocs;
547 Elf_Internal_Sym *local_syms;
548 asection **local_sections;
550 Elf_Internal_Shdr *symtab_hdr;
551 struct elf32_mn10300_link_hash_entry **sym_hashes;
552 Elf_Internal_Rela *rel, *relend;
554 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
555 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
556 (elf_sym_hashes (input_bfd));
559 relend = relocs + input_section->reloc_count;
560 for (; rel < relend; rel++)
563 reloc_howto_type *howto;
564 unsigned long r_symndx;
565 Elf_Internal_Sym *sym;
567 struct elf32_mn10300_link_hash_entry *h;
569 bfd_reloc_status_type r;
571 r_symndx = ELF32_R_SYM (rel->r_info);
572 r_type = ELF32_R_TYPE (rel->r_info);
573 howto = elf_mn10300_howto_table + r_type;
575 /* Just skip the vtable gc relocs. */
576 if (r_type == R_MN10300_GNU_VTINHERIT
577 || r_type == R_MN10300_GNU_VTENTRY)
580 if (info->relocateable)
582 /* This is a relocateable link. We don't have to change
583 anything, unless the reloc is against a section symbol,
584 in which case we have to adjust according to where the
585 section symbol winds up in the output section. */
586 if (r_symndx < symtab_hdr->sh_info)
588 sym = local_syms + r_symndx;
589 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
591 sec = local_sections[r_symndx];
592 rel->r_addend += sec->output_offset + sym->st_value;
599 /* This is a final link. */
603 if (r_symndx < symtab_hdr->sh_info)
605 sym = local_syms + r_symndx;
606 sec = local_sections[r_symndx];
607 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
611 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
612 while (h->root.root.type == bfd_link_hash_indirect
613 || h->root.root.type == bfd_link_hash_warning)
614 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
615 if (h->root.root.type == bfd_link_hash_defined
616 || h->root.root.type == bfd_link_hash_defweak)
618 sec = h->root.root.u.def.section;
619 relocation = (h->root.root.u.def.value
620 + sec->output_section->vma
621 + sec->output_offset);
623 else if (h->root.root.type == bfd_link_hash_undefweak)
627 if (! ((*info->callbacks->undefined_symbol)
628 (info, h->root.root.root.string, input_bfd,
629 input_section, rel->r_offset, true)))
635 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
637 contents, rel->r_offset,
638 relocation, rel->r_addend,
639 info, sec, h == NULL);
641 if (r != bfd_reloc_ok)
644 const char *msg = (const char *) 0;
647 name = h->root.root.root.string;
650 name = (bfd_elf_string_from_elf_section
651 (input_bfd, symtab_hdr->sh_link, sym->st_name));
652 if (name == NULL || *name == '\0')
653 name = bfd_section_name (input_bfd, sec);
658 case bfd_reloc_overflow:
659 if (! ((*info->callbacks->reloc_overflow)
660 (info, name, howto->name, (bfd_vma) 0,
661 input_bfd, input_section, rel->r_offset)))
665 case bfd_reloc_undefined:
666 if (! ((*info->callbacks->undefined_symbol)
667 (info, name, input_bfd, input_section,
668 rel->r_offset, true)))
672 case bfd_reloc_outofrange:
673 msg = _("internal error: out of range error");
676 case bfd_reloc_notsupported:
677 msg = _("internal error: unsupported relocation error");
680 case bfd_reloc_dangerous:
681 msg = _("internal error: dangerous error");
685 msg = _("internal error: unknown error");
689 if (!((*info->callbacks->warning)
690 (info, msg, name, input_bfd, input_section,
701 /* Finish initializing one hash table entry. */
703 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
704 struct bfd_hash_entry *gen_entry;
705 PTR in_args ATTRIBUTE_UNUSED;
707 struct elf32_mn10300_link_hash_entry *entry;
708 unsigned int byte_count = 0;
710 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
712 if (entry->root.root.type == bfd_link_hash_warning)
713 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
715 /* If we already know we want to convert "call" to "calls" for calls
716 to this symbol, then return now. */
717 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
720 /* If there are no named calls to this symbol, or there's nothing we
721 can move from the function itself into the "call" instruction, then
722 note that all "call" instructions should be converted into "calls"
723 instructions and return. */
724 if (entry->direct_calls == 0
725 || (entry->stack_size == 0 && entry->movm_args == 0))
727 /* Make a note that we should convert "call" instructions to "calls"
728 instructions for calls to this symbol. */
729 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
733 /* We may be able to move some instructions from the function itself into
734 the "call" instruction. Count how many bytes we might be able to
735 eliminate in the function itself. */
737 /* A movm instruction is two bytes. */
738 if (entry->movm_args)
741 /* Count the insn to allocate stack space too. */
742 if (entry->stack_size > 0 && entry->stack_size <= 128)
744 else if (entry->stack_size > 0 && entry->stack_size < 256)
747 /* If using "call" will result in larger code, then turn all
748 the associated "call" instructions into "calls" instrutions. */
749 if (byte_count < entry->direct_calls)
750 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
752 /* This routine never fails. */
756 /* This function handles relaxing for the mn10300.
758 There's quite a few relaxing opportunites available on the mn10300:
760 * calls:32 -> calls:16 2 bytes
761 * call:32 -> call:16 2 bytes
763 * call:32 -> calls:32 1 byte
764 * call:16 -> calls:16 1 byte
765 * These are done anytime using "calls" would result
766 in smaller code, or when necessary to preserve the
767 meaning of the program.
771 * In some circumstances we can move instructions
772 from a function prologue into a "call" instruction.
773 This is only done if the resulting code is no larger
774 than the original code.
776 * jmp:32 -> jmp:16 2 bytes
777 * jmp:16 -> bra:8 1 byte
779 * If the previous instruction is a conditional branch
780 around the jump/bra, we may be able to reverse its condition
781 and change its target to the jump's target. The jump/bra
782 can then be deleted. 2 bytes
784 * mov abs32 -> mov abs16 1 or 2 bytes
786 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
787 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
789 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
790 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
792 We don't handle imm16->imm8 or d16->d8 as they're very rare
793 and somewhat more difficult to support. */
796 mn10300_elf_relax_section (abfd, sec, link_info, again)
799 struct bfd_link_info *link_info;
802 Elf_Internal_Shdr *symtab_hdr;
803 Elf_Internal_Shdr *shndx_hdr;
804 Elf_Internal_Rela *internal_relocs = NULL;
805 Elf_Internal_Rela *free_relocs = NULL;
806 Elf_Internal_Rela *irel, *irelend;
807 bfd_byte *contents = NULL;
808 bfd_byte *free_contents = NULL;
809 Elf32_External_Sym *extsyms = NULL;
810 Elf32_External_Sym *free_extsyms = NULL;
811 Elf_External_Sym_Shndx *shndx_buf = NULL;
812 struct elf32_mn10300_link_hash_table *hash_table;
814 /* Assume nothing changes. */
817 /* We need a pointer to the mn10300 specific hash table. */
818 hash_table = elf32_mn10300_hash_table (link_info);
820 /* Initialize fields in each hash table entry the first time through. */
821 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
825 /* Iterate over all the input bfds. */
826 for (input_bfd = link_info->input_bfds;
828 input_bfd = input_bfd->link_next)
832 /* We're going to need all the symbols for each bfd. */
833 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
834 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
836 /* Get cached copy if it exists. */
837 if (symtab_hdr->contents != NULL)
838 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
841 /* Go get them off disk. */
844 amt = symtab_hdr->sh_info;
845 amt *= sizeof (Elf32_External_Sym);
846 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
849 free_extsyms = extsyms;
850 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
851 || bfd_bread ((PTR) extsyms, amt, input_bfd) != amt)
855 if (shndx_hdr->sh_size != 0)
859 amt = symtab_hdr->sh_info;
860 amt *= sizeof (Elf_External_Sym_Shndx);
861 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
862 if (shndx_buf == NULL)
864 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
865 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
869 /* Iterate over each section in this bfd. */
870 for (section = input_bfd->sections;
872 section = section->next)
874 struct elf32_mn10300_link_hash_entry *hash;
875 Elf_Internal_Sym *sym;
876 asection *sym_sec = NULL;
877 const char *sym_name;
880 /* If there's nothing to do in this section, skip it. */
881 if (! (((section->flags & SEC_RELOC) != 0
882 && section->reloc_count != 0)
883 || (section->flags & SEC_CODE) != 0))
886 /* Get cached copy of section contents if it exists. */
887 if (elf_section_data (section)->this_hdr.contents != NULL)
888 contents = elf_section_data (section)->this_hdr.contents;
889 else if (section->_raw_size != 0)
891 /* Go get them off disk. */
892 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
893 if (contents == NULL)
895 free_contents = contents;
897 if (!bfd_get_section_contents (input_bfd, section,
898 contents, (file_ptr) 0,
905 free_contents = NULL;
908 /* If there aren't any relocs, then there's nothing to do. */
909 if ((section->flags & SEC_RELOC) != 0
910 && section->reloc_count != 0)
913 /* Get a copy of the native relocations. */
914 internal_relocs = (_bfd_elf32_link_read_relocs
915 (input_bfd, section, (PTR) NULL,
916 (Elf_Internal_Rela *) NULL,
917 link_info->keep_memory));
918 if (internal_relocs == NULL)
920 if (! link_info->keep_memory)
921 free_relocs = internal_relocs;
923 /* Now examine each relocation. */
924 irel = internal_relocs;
925 irelend = irel + section->reloc_count;
926 for (; irel < irelend; irel++)
929 unsigned long r_index;
932 r_type = ELF32_R_TYPE (irel->r_info);
933 r_index = ELF32_R_SYM (irel->r_info);
935 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
938 /* We need the name and hash table entry of the target
944 if (r_index < symtab_hdr->sh_info)
946 /* A local symbol. */
947 Elf32_External_Sym *esym;
948 Elf_External_Sym_Shndx *shndx;
949 Elf_Internal_Sym isym;
950 struct elf_link_hash_table *elftab;
953 esym = extsyms + r_index;
954 shndx = shndx_buf + (shndx_buf ? r_index : 0);
955 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx,
958 if (isym.st_shndx == SHN_UNDEF)
959 sym_sec = bfd_und_section_ptr;
960 else if (isym.st_shndx == SHN_ABS)
961 sym_sec = bfd_abs_section_ptr;
962 else if (isym.st_shndx == SHN_COMMON)
963 sym_sec = bfd_com_section_ptr;
966 = bfd_section_from_elf_index (input_bfd,
970 = bfd_elf_string_from_elf_section (input_bfd,
975 /* If it isn't a function, then we don't care
977 if (r_index < symtab_hdr->sh_info
978 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
981 /* Tack on an ID so we can uniquely identify this
982 local symbol in the global hash table. */
983 amt = strlen (sym_name) + 10;
984 new_name = bfd_malloc (amt);
988 sprintf (new_name, "%s_%08x",
989 sym_name, (int) sym_sec);
992 elftab = &hash_table->static_hash_table->root;
993 hash = ((struct elf32_mn10300_link_hash_entry *)
994 elf_link_hash_lookup (elftab, sym_name,
1000 r_index -= symtab_hdr->sh_info;
1001 hash = (struct elf32_mn10300_link_hash_entry *)
1002 elf_sym_hashes (input_bfd)[r_index];
1005 /* If this is not a "call" instruction, then we
1006 should convert "call" instructions to "calls"
1008 code = bfd_get_8 (input_bfd,
1009 contents + irel->r_offset - 1);
1010 if (code != 0xdd && code != 0xcd)
1011 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1013 /* If this is a jump/call, then bump the direct_calls
1014 counter. Else force "call" to "calls" conversions. */
1015 if (r_type == R_MN10300_PCREL32
1016 || r_type == R_MN10300_PCREL16)
1017 hash->direct_calls++;
1019 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1023 /* Now look at the actual contents to get the stack size,
1024 and a list of what registers were saved in the prologue
1026 if ((section->flags & SEC_CODE) != 0)
1029 Elf32_External_Sym *esym, *esymend;
1030 Elf_External_Sym_Shndx *shndx;
1032 unsigned int sec_shndx;
1034 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1037 /* Look at each function defined in this section and
1038 update info for that function. */
1039 for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1042 esym++, shndx = (shndx ? shndx + 1 : NULL))
1044 Elf_Internal_Sym isym;
1046 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, &isym);
1047 if (isym.st_shndx == sec_shndx
1048 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1050 struct elf_link_hash_table *elftab;
1053 if (isym.st_shndx == SHN_UNDEF)
1054 sym_sec = bfd_und_section_ptr;
1055 else if (isym.st_shndx == SHN_ABS)
1056 sym_sec = bfd_abs_section_ptr;
1057 else if (isym.st_shndx == SHN_COMMON)
1058 sym_sec = bfd_com_section_ptr;
1061 = bfd_section_from_elf_index (input_bfd,
1064 sym_name = (bfd_elf_string_from_elf_section
1065 (input_bfd, symtab_hdr->sh_link,
1068 /* Tack on an ID so we can uniquely identify this
1069 local symbol in the global hash table. */
1070 amt = strlen (sym_name) + 10;
1071 new_name = bfd_malloc (amt);
1075 sprintf (new_name, "%s_%08x",
1076 sym_name, (int) sym_sec);
1077 sym_name = new_name;
1079 elftab = &hash_table->static_hash_table->root;
1080 hash = ((struct elf32_mn10300_link_hash_entry *)
1081 elf_link_hash_lookup (elftab, sym_name,
1082 true, true, false));
1084 compute_function_info (input_bfd, hash,
1085 isym.st_value, contents);
1089 esym = extsyms + symtab_hdr->sh_info;
1090 esymend = extsyms + (symtab_hdr->sh_size
1091 / sizeof (Elf32_External_Sym));
1092 for (idx = 0; esym < esymend; esym++, idx++)
1094 Elf_Internal_Sym isym;
1096 hash = (struct elf32_mn10300_link_hash_entry *)
1097 elf_sym_hashes (input_bfd)[idx];
1098 if ((hash->root.root.type == bfd_link_hash_defined
1099 || hash->root.root.type == bfd_link_hash_defweak)
1100 && hash->root.root.u.def.section == section
1101 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1102 compute_function_info (input_bfd, hash,
1103 (hash)->root.root.u.def.value,
1108 /* Cache or free any memory we allocated for the relocs. */
1109 if (free_relocs != NULL)
1115 /* Cache or free any memory we allocated for the contents. */
1116 if (free_contents != NULL)
1118 if (! link_info->keep_memory)
1119 free (free_contents);
1122 /* Cache the section contents for elf_link_input_bfd. */
1123 elf_section_data (section)->this_hdr.contents = contents;
1125 free_contents = NULL;
1129 if (shndx_buf != NULL)
1135 /* Cache or free any memory we allocated for the symbols. */
1136 if (free_extsyms != NULL)
1138 if (! link_info->keep_memory)
1139 free (free_extsyms);
1142 /* Cache the symbols for elf_link_input_bfd. */
1143 symtab_hdr->contents = (unsigned char *) extsyms;
1145 free_extsyms = NULL;
1149 /* Now iterate on each symbol in the hash table and perform
1150 the final initialization steps on each. */
1151 elf32_mn10300_link_hash_traverse (hash_table,
1152 elf32_mn10300_finish_hash_table_entry,
1154 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1155 elf32_mn10300_finish_hash_table_entry,
1158 /* All entries in the hash table are fully initialized. */
1159 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1161 /* Now that everything has been initialized, go through each
1162 code section and delete any prologue insns which will be
1163 redundant because their operations will be performed by
1164 a "call" instruction. */
1165 for (input_bfd = link_info->input_bfds;
1167 input_bfd = input_bfd->link_next)
1171 /* We're going to need all the local symbols for each bfd. */
1172 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1173 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1175 /* Get cached copy if it exists. */
1176 if (symtab_hdr->contents != NULL)
1177 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1180 /* Go get them off disk. */
1183 amt = symtab_hdr->sh_info;
1184 amt *= sizeof (Elf32_External_Sym);
1185 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1186 if (extsyms == NULL)
1188 free_extsyms = extsyms;
1189 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1190 || bfd_bread ((PTR) extsyms, amt, input_bfd) != amt)
1192 symtab_hdr->contents = (bfd_byte *) extsyms;
1195 if (shndx_hdr->sh_size != 0)
1199 amt = symtab_hdr->sh_info;
1200 amt *= sizeof (Elf_External_Sym_Shndx);
1201 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1202 if (shndx_buf == NULL)
1204 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1205 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1207 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1210 /* Walk over each section in this bfd. */
1211 for (section = input_bfd->sections;
1213 section = section->next)
1215 unsigned int sec_shndx;
1216 Elf32_External_Sym *esym, *esymend;
1217 Elf_External_Sym_Shndx *shndx;
1220 /* Skip non-code sections and empty sections. */
1221 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1224 if (section->reloc_count != 0)
1226 /* Get a copy of the native relocations. */
1227 internal_relocs = (_bfd_elf32_link_read_relocs
1228 (input_bfd, section, (PTR) NULL,
1229 (Elf_Internal_Rela *) NULL,
1230 link_info->keep_memory));
1231 if (internal_relocs == NULL)
1233 if (! link_info->keep_memory)
1234 free_relocs = internal_relocs;
1237 /* Get cached copy of section contents if it exists. */
1238 if (elf_section_data (section)->this_hdr.contents != NULL)
1239 contents = elf_section_data (section)->this_hdr.contents;
1242 /* Go get them off disk. */
1243 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1244 if (contents == NULL)
1246 free_contents = contents;
1248 if (!bfd_get_section_contents (input_bfd, section,
1249 contents, (file_ptr) 0,
1250 section->_raw_size))
1254 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1257 /* Now look for any function in this section which needs
1258 insns deleted from its prologue. */
1259 for (esym = extsyms, esymend = esym + symtab_hdr->sh_info,
1262 esym++, shndx = (shndx ? shndx + 1 : NULL))
1264 Elf_Internal_Sym isym;
1265 struct elf32_mn10300_link_hash_entry *sym_hash;
1266 asection *sym_sec = NULL;
1267 const char *sym_name;
1269 struct elf_link_hash_table *elftab;
1272 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, &isym);
1274 if (isym.st_shndx != sec_shndx)
1277 if (isym.st_shndx == SHN_UNDEF)
1278 sym_sec = bfd_und_section_ptr;
1279 else if (isym.st_shndx == SHN_ABS)
1280 sym_sec = bfd_abs_section_ptr;
1281 else if (isym.st_shndx == SHN_COMMON)
1282 sym_sec = bfd_com_section_ptr;
1285 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1288 = bfd_elf_string_from_elf_section (input_bfd,
1289 symtab_hdr->sh_link,
1292 /* Tack on an ID so we can uniquely identify this
1293 local symbol in the global hash table. */
1294 amt = strlen (sym_name) + 10;
1295 new_name = bfd_malloc (amt);
1298 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1299 sym_name = new_name;
1301 elftab = &hash_table->static_hash_table->root;
1302 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1303 elf_link_hash_lookup (elftab, sym_name,
1304 false, false, false));
1307 if (sym_hash == NULL)
1310 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1311 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1315 /* Note that we've changed things. */
1316 elf_section_data (section)->relocs = internal_relocs;
1319 elf_section_data (section)->this_hdr.contents = contents;
1320 free_contents = NULL;
1322 free_extsyms = NULL;
1324 /* Count how many bytes we're going to delete. */
1325 if (sym_hash->movm_args)
1328 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1330 else if (sym_hash->stack_size
1331 && sym_hash->stack_size < 256)
1334 /* Note that we've deleted prologue bytes for this
1336 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1338 /* Actually delete the bytes. */
1339 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1345 /* Something changed. Not strictly necessary, but
1346 may lead to more relaxing opportunities. */
1351 /* Look for any global functions in this section which
1352 need insns deleted from their prologues. */
1354 idx < (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1355 - symtab_hdr->sh_info);
1358 struct elf32_mn10300_link_hash_entry *sym_hash;
1360 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1361 (elf_sym_hashes (input_bfd)[idx]);
1362 if ((sym_hash->root.root.type == bfd_link_hash_defined
1363 || sym_hash->root.root.type == bfd_link_hash_defweak)
1364 && sym_hash->root.root.u.def.section == section
1365 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1366 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1371 /* Note that we've changed things. */
1372 elf_section_data (section)->relocs = internal_relocs;
1375 elf_section_data (section)->this_hdr.contents = contents;
1376 free_contents = NULL;
1378 free_extsyms = NULL;
1380 /* Count how many bytes we're going to delete. */
1381 if (sym_hash->movm_args)
1384 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1386 else if (sym_hash->stack_size
1387 && sym_hash->stack_size < 256)
1390 /* Note that we've deleted prologue bytes for this
1392 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1394 /* Actually delete the bytes. */
1395 symval = sym_hash->root.root.u.def.value;
1396 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1402 /* Something changed. Not strictly necessary, but
1403 may lead to more relaxing opportunities. */
1408 /* Cache or free any memory we allocated for the relocs. */
1409 if (free_relocs != NULL)
1415 /* Cache or free any memory we allocated for the contents. */
1416 if (free_contents != NULL)
1418 if (! link_info->keep_memory)
1419 free (free_contents);
1422 /* Cache the section contents for elf_link_input_bfd. */
1423 elf_section_data (section)->this_hdr.contents = contents;
1425 free_contents = NULL;
1429 if (shndx_buf != NULL)
1431 shndx_hdr->contents = NULL;
1436 /* Cache or free any memory we allocated for the symbols. */
1437 if (free_extsyms != NULL)
1439 if (! link_info->keep_memory)
1441 symtab_hdr->contents = NULL;
1442 free (free_extsyms);
1444 free_extsyms = NULL;
1449 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1452 internal_relocs = NULL;
1454 free_contents = NULL;
1455 free_extsyms = NULL;
1457 /* We don't have to do anything for a relocateable link, if
1458 this section does not have relocs, or if this is not a
1460 if (link_info->relocateable
1461 || (sec->flags & SEC_RELOC) == 0
1462 || sec->reloc_count == 0
1463 || (sec->flags & SEC_CODE) == 0)
1466 /* If this is the first time we have been called for this section,
1467 initialize the cooked size. */
1468 if (sec->_cooked_size == 0)
1469 sec->_cooked_size = sec->_raw_size;
1471 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1472 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1474 /* Get a copy of the native relocations. */
1475 internal_relocs = (_bfd_elf32_link_read_relocs
1476 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1477 link_info->keep_memory));
1478 if (internal_relocs == NULL)
1480 if (! link_info->keep_memory)
1481 free_relocs = internal_relocs;
1483 /* Walk through them looking for relaxing opportunities. */
1484 irelend = internal_relocs + sec->reloc_count;
1485 for (irel = internal_relocs; irel < irelend; irel++)
1488 struct elf32_mn10300_link_hash_entry *h = NULL;
1490 /* If this isn't something that can be relaxed, then ignore
1492 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1493 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1494 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1497 /* Get the section contents if we haven't done so already. */
1498 if (contents == NULL)
1500 /* Get cached copy if it exists. */
1501 if (elf_section_data (sec)->this_hdr.contents != NULL)
1502 contents = elf_section_data (sec)->this_hdr.contents;
1505 /* Go get them off disk. */
1506 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1507 if (contents == NULL)
1509 free_contents = contents;
1511 if (! bfd_get_section_contents (abfd, sec, contents,
1512 (file_ptr) 0, sec->_raw_size))
1517 /* Read this BFD's symbols if we haven't done so already. */
1518 if (extsyms == NULL)
1520 /* Get cached copy if it exists. */
1521 if (symtab_hdr->contents != NULL)
1522 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1525 /* Go get them off disk. */
1528 amt = symtab_hdr->sh_info;
1529 amt *= sizeof (Elf32_External_Sym);
1530 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
1531 if (extsyms == NULL)
1533 free_extsyms = extsyms;
1534 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1535 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
1537 symtab_hdr->contents = (bfd_byte *) extsyms;
1540 if (shndx_hdr->sh_size != 0)
1544 amt = symtab_hdr->sh_info;
1545 amt *= sizeof (Elf_External_Sym_Shndx);
1546 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1547 if (shndx_buf == NULL)
1549 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1550 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
1552 shndx_hdr->contents = (bfd_byte *) shndx_buf;
1556 /* Get the value of the symbol referred to by the reloc. */
1557 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1559 Elf32_External_Sym *esym;
1560 Elf_External_Sym_Shndx *shndx;
1561 Elf_Internal_Sym isym;
1562 asection *sym_sec = NULL;
1563 const char *sym_name;
1566 /* A local symbol. */
1567 esym = extsyms + ELF32_R_SYM (irel->r_info);
1568 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
1569 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1571 if (isym.st_shndx == SHN_UNDEF)
1572 sym_sec = bfd_und_section_ptr;
1573 else if (isym.st_shndx == SHN_ABS)
1574 sym_sec = bfd_abs_section_ptr;
1575 else if (isym.st_shndx == SHN_COMMON)
1576 sym_sec = bfd_com_section_ptr;
1578 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1580 symval = (isym.st_value
1581 + sym_sec->output_section->vma
1582 + sym_sec->output_offset);
1583 sym_name = bfd_elf_string_from_elf_section (abfd,
1584 symtab_hdr->sh_link,
1587 /* Tack on an ID so we can uniquely identify this
1588 local symbol in the global hash table. */
1589 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1592 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1593 sym_name = new_name;
1595 h = (struct elf32_mn10300_link_hash_entry *)
1596 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1597 sym_name, false, false, false);
1604 /* An external symbol. */
1605 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1606 h = (struct elf32_mn10300_link_hash_entry *)
1607 (elf_sym_hashes (abfd)[indx]);
1608 BFD_ASSERT (h != NULL);
1609 if (h->root.root.type != bfd_link_hash_defined
1610 && h->root.root.type != bfd_link_hash_defweak)
1612 /* This appears to be a reference to an undefined
1613 symbol. Just ignore it--it will be caught by the
1614 regular reloc processing. */
1618 symval = (h->root.root.u.def.value
1619 + h->root.root.u.def.section->output_section->vma
1620 + h->root.root.u.def.section->output_offset);
1623 /* For simplicity of coding, we are going to modify the section
1624 contents, the section relocs, and the BFD symbol table. We
1625 must tell the rest of the code not to free up this
1626 information. It would be possible to instead create a table
1627 of changes which have to be made, as is done in coff-mips.c;
1628 that would be more work, but would require less memory when
1629 the linker is run. */
1631 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1632 branch/call, also deal with "call" -> "calls" conversions and
1633 insertion of prologue data into "call" instructions. */
1634 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1636 bfd_vma value = symval;
1638 /* If we've got a "call" instruction that needs to be turned
1639 into a "calls" instruction, do so now. It saves a byte. */
1640 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1644 /* Get the opcode. */
1645 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1647 /* Make sure we're working with a "call" instruction! */
1650 /* Note that we've changed the relocs, section contents,
1652 elf_section_data (sec)->relocs = internal_relocs;
1655 elf_section_data (sec)->this_hdr.contents = contents;
1656 free_contents = NULL;
1658 free_extsyms = NULL;
1660 /* Fix the opcode. */
1661 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1662 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1664 /* Fix irel->r_offset and irel->r_addend. */
1665 irel->r_offset += 1;
1666 irel->r_addend += 1;
1668 /* Delete one byte of data. */
1669 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1670 irel->r_offset + 3, 1))
1673 /* That will change things, so, we should relax again.
1674 Note that this is not required, and it may be slow. */
1680 /* We've got a "call" instruction which needs some data
1681 from target function filled in. */
1684 /* Get the opcode. */
1685 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1687 /* Insert data from the target function into the "call"
1688 instruction if needed. */
1691 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1692 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1693 contents + irel->r_offset + 5);
1697 /* Deal with pc-relative gunk. */
1698 value -= (sec->output_section->vma + sec->output_offset);
1699 value -= irel->r_offset;
1700 value += irel->r_addend;
1702 /* See if the value will fit in 16 bits, note the high value is
1703 0x7fff + 2 as the target will be two bytes closer if we are
1705 if ((long) value < 0x8001 && (long) value > -0x8000)
1709 /* Get the opcode. */
1710 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1712 if (code != 0xdc && code != 0xdd && code != 0xff)
1715 /* Note that we've changed the relocs, section contents, etc. */
1716 elf_section_data (sec)->relocs = internal_relocs;
1719 elf_section_data (sec)->this_hdr.contents = contents;
1720 free_contents = NULL;
1722 free_extsyms = NULL;
1724 /* Fix the opcode. */
1726 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1727 else if (code == 0xdd)
1728 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1729 else if (code == 0xff)
1730 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1732 /* Fix the relocation's type. */
1733 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1736 /* Delete two bytes of data. */
1737 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1738 irel->r_offset + 1, 2))
1741 /* That will change things, so, we should relax again.
1742 Note that this is not required, and it may be slow. */
1747 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1749 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1751 bfd_vma value = symval;
1753 /* If we've got a "call" instruction that needs to be turned
1754 into a "calls" instruction, do so now. It saves a byte. */
1755 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1759 /* Get the opcode. */
1760 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1762 /* Make sure we're working with a "call" instruction! */
1765 /* Note that we've changed the relocs, section contents,
1767 elf_section_data (sec)->relocs = internal_relocs;
1770 elf_section_data (sec)->this_hdr.contents = contents;
1771 free_contents = NULL;
1773 free_extsyms = NULL;
1775 /* Fix the opcode. */
1776 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1777 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1779 /* Fix irel->r_offset and irel->r_addend. */
1780 irel->r_offset += 1;
1781 irel->r_addend += 1;
1783 /* Delete one byte of data. */
1784 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1785 irel->r_offset + 1, 1))
1788 /* That will change things, so, we should relax again.
1789 Note that this is not required, and it may be slow. */
1797 /* Get the opcode. */
1798 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1800 /* Insert data from the target function into the "call"
1801 instruction if needed. */
1804 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1805 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1806 contents + irel->r_offset + 3);
1810 /* Deal with pc-relative gunk. */
1811 value -= (sec->output_section->vma + sec->output_offset);
1812 value -= irel->r_offset;
1813 value += irel->r_addend;
1815 /* See if the value will fit in 8 bits, note the high value is
1816 0x7f + 1 as the target will be one bytes closer if we are
1818 if ((long) value < 0x80 && (long) value > -0x80)
1822 /* Get the opcode. */
1823 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1828 /* Note that we've changed the relocs, section contents, etc. */
1829 elf_section_data (sec)->relocs = internal_relocs;
1832 elf_section_data (sec)->this_hdr.contents = contents;
1833 free_contents = NULL;
1835 free_extsyms = NULL;
1837 /* Fix the opcode. */
1838 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1840 /* Fix the relocation's type. */
1841 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1844 /* Delete one byte of data. */
1845 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1846 irel->r_offset + 1, 1))
1849 /* That will change things, so, we should relax again.
1850 Note that this is not required, and it may be slow. */
1855 /* Try to eliminate an unconditional 8 bit pc-relative branch
1856 which immediately follows a conditional 8 bit pc-relative
1857 branch around the unconditional branch.
1864 This happens when the bCC can't reach lab2 at assembly time,
1865 but due to other relaxations it can reach at link time. */
1866 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1868 Elf_Internal_Rela *nrel;
1869 bfd_vma value = symval;
1872 /* Deal with pc-relative gunk. */
1873 value -= (sec->output_section->vma + sec->output_offset);
1874 value -= irel->r_offset;
1875 value += irel->r_addend;
1877 /* Do nothing if this reloc is the last byte in the section. */
1878 if (irel->r_offset == sec->_cooked_size)
1881 /* See if the next instruction is an unconditional pc-relative
1882 branch, more often than not this test will fail, so we
1883 test it first to speed things up. */
1884 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1888 /* Also make sure the next relocation applies to the next
1889 instruction and that it's a pc-relative 8 bit branch. */
1892 || irel->r_offset + 2 != nrel->r_offset
1893 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1896 /* Make sure our destination immediately follows the
1897 unconditional branch. */
1898 if (symval != (sec->output_section->vma + sec->output_offset
1899 + irel->r_offset + 3))
1902 /* Now make sure we are a conditional branch. This may not
1903 be necessary, but why take the chance.
1905 Note these checks assume that R_MN10300_PCREL8 relocs
1906 only occur on bCC and bCCx insns. If they occured
1907 elsewhere, we'd need to know the start of this insn
1908 for this check to be accurate. */
1909 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1910 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1911 && code != 0xc3 && code != 0xc4 && code != 0xc5
1912 && code != 0xc6 && code != 0xc7 && code != 0xc8
1913 && code != 0xc9 && code != 0xe8 && code != 0xe9
1914 && code != 0xea && code != 0xeb)
1917 /* We also have to be sure there is no symbol/label
1918 at the unconditional branch. */
1919 if (mn10300_elf_symbol_address_p (abfd, sec, irel->r_offset + 1))
1922 /* Note that we've changed the relocs, section contents, etc. */
1923 elf_section_data (sec)->relocs = internal_relocs;
1926 elf_section_data (sec)->this_hdr.contents = contents;
1927 free_contents = NULL;
1929 free_extsyms = NULL;
1931 /* Reverse the condition of the first branch. */
1977 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1979 /* Set the reloc type and symbol for the first branch
1980 from the second branch. */
1981 irel->r_info = nrel->r_info;
1983 /* Make the reloc for the second branch a null reloc. */
1984 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1987 /* Delete two bytes of data. */
1988 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1989 irel->r_offset + 1, 2))
1992 /* That will change things, so, we should relax again.
1993 Note that this is not required, and it may be slow. */
1997 /* Try to turn a 24 immediate, displacement or absolute address
1998 into a 8 immediate, displacement or absolute address. */
1999 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2001 bfd_vma value = symval;
2002 value += irel->r_addend;
2004 /* See if the value will fit in 8 bits. */
2005 if ((long) value < 0x7f && (long) value > -0x80)
2009 /* AM33 insns which have 24 operands are 6 bytes long and
2010 will have 0xfd as the first byte. */
2012 /* Get the first opcode. */
2013 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2017 /* Get the second opcode. */
2018 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2020 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2021 equivalent instructions exists. */
2022 if (code != 0x6b && code != 0x7b
2023 && code != 0x8b && code != 0x9b
2024 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2025 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2026 || (code & 0x0f) == 0x0e))
2028 /* Not safe if the high bit is on as relaxing may
2029 move the value out of high mem and thus not fit
2030 in a signed 8bit value. This is currently over
2032 if ((value & 0x80) == 0)
2034 /* Note that we've changed the relocation contents,
2036 elf_section_data (sec)->relocs = internal_relocs;
2039 elf_section_data (sec)->this_hdr.contents = contents;
2040 free_contents = NULL;
2042 free_extsyms = NULL;
2044 /* Fix the opcode. */
2045 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2046 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2048 /* Fix the relocation's type. */
2050 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2053 /* Delete two bytes of data. */
2054 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2055 irel->r_offset + 1, 2))
2058 /* That will change things, so, we should relax
2059 again. Note that this is not required, and it
2069 /* Try to turn a 32bit immediate, displacement or absolute address
2070 into a 16bit immediate, displacement or absolute address. */
2071 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
2073 bfd_vma value = symval;
2074 value += irel->r_addend;
2076 /* See if the value will fit in 24 bits.
2077 We allow any 16bit match here. We prune those we can't
2079 if ((long) value < 0x7fffff && (long) value > -0x800000)
2083 /* AM33 insns which have 32bit operands are 7 bytes long and
2084 will have 0xfe as the first byte. */
2086 /* Get the first opcode. */
2087 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2091 /* Get the second opcode. */
2092 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2094 /* All the am33 32 -> 24 relaxing possibilities. */
2095 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2096 equivalent instructions exists. */
2097 if (code != 0x6b && code != 0x7b
2098 && code != 0x8b && code != 0x9b
2099 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2100 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2101 || (code & 0x0f) == 0x0e))
2103 /* Not safe if the high bit is on as relaxing may
2104 move the value out of high mem and thus not fit
2105 in a signed 16bit value. This is currently over
2107 if ((value & 0x8000) == 0)
2109 /* Note that we've changed the relocation contents,
2111 elf_section_data (sec)->relocs = internal_relocs;
2114 elf_section_data (sec)->this_hdr.contents = contents;
2115 free_contents = NULL;
2117 free_extsyms = NULL;
2119 /* Fix the opcode. */
2120 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2121 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2123 /* Fix the relocation's type. */
2125 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2128 /* Delete one byte of data. */
2129 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2130 irel->r_offset + 3, 1))
2133 /* That will change things, so, we should relax
2134 again. Note that this is not required, and it
2143 /* See if the value will fit in 16 bits.
2144 We allow any 16bit match here. We prune those we can't
2146 if ((long) value < 0x7fff && (long) value > -0x8000)
2150 /* Most insns which have 32bit operands are 6 bytes long;
2151 exceptions are pcrel insns and bit insns.
2153 We handle pcrel insns above. We don't bother trying
2154 to handle the bit insns here.
2156 The first byte of the remaining insns will be 0xfc. */
2158 /* Get the first opcode. */
2159 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2164 /* Get the second opcode. */
2165 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2167 if ((code & 0xf0) < 0x80)
2168 switch (code & 0xf0)
2170 /* mov (d32,am),dn -> mov (d32,am),dn
2171 mov dm,(d32,am) -> mov dn,(d32,am)
2172 mov (d32,am),an -> mov (d32,am),an
2173 mov dm,(d32,am) -> mov dn,(d32,am)
2174 movbu (d32,am),dn -> movbu (d32,am),dn
2175 movbu dm,(d32,am) -> movbu dn,(d32,am)
2176 movhu (d32,am),dn -> movhu (d32,am),dn
2177 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2186 /* Not safe if the high bit is on as relaxing may
2187 move the value out of high mem and thus not fit
2188 in a signed 16bit value. */
2190 && (value & 0x8000))
2193 /* Note that we've changed the relocation contents, etc. */
2194 elf_section_data (sec)->relocs = internal_relocs;
2197 elf_section_data (sec)->this_hdr.contents = contents;
2198 free_contents = NULL;
2200 free_extsyms = NULL;
2202 /* Fix the opcode. */
2203 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2204 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2206 /* Fix the relocation's type. */
2207 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2210 /* Delete two bytes of data. */
2211 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2212 irel->r_offset + 2, 2))
2215 /* That will change things, so, we should relax again.
2216 Note that this is not required, and it may be slow. */
2220 else if ((code & 0xf0) == 0x80
2221 || (code & 0xf0) == 0x90)
2222 switch (code & 0xf3)
2224 /* mov dn,(abs32) -> mov dn,(abs16)
2225 movbu dn,(abs32) -> movbu dn,(abs16)
2226 movhu dn,(abs32) -> movhu dn,(abs16) */
2230 /* Note that we've changed the relocation contents, etc. */
2231 elf_section_data (sec)->relocs = internal_relocs;
2234 elf_section_data (sec)->this_hdr.contents = contents;
2235 free_contents = NULL;
2237 free_extsyms = NULL;
2239 if ((code & 0xf3) == 0x81)
2240 code = 0x01 + (code & 0x0c);
2241 else if ((code & 0xf3) == 0x82)
2242 code = 0x02 + (code & 0x0c);
2243 else if ((code & 0xf3) == 0x83)
2244 code = 0x03 + (code & 0x0c);
2248 /* Fix the opcode. */
2249 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2251 /* Fix the relocation's type. */
2252 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2255 /* The opcode got shorter too, so we have to fix the
2256 addend and offset too! */
2257 irel->r_offset -= 1;
2259 /* Delete three bytes of data. */
2260 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2261 irel->r_offset + 1, 3))
2264 /* That will change things, so, we should relax again.
2265 Note that this is not required, and it may be slow. */
2269 /* mov am,(abs32) -> mov am,(abs16)
2270 mov am,(d32,sp) -> mov am,(d16,sp)
2271 mov dm,(d32,sp) -> mov dm,(d32,sp)
2272 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2273 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2279 /* sp-based offsets are zero-extended. */
2280 if (code >= 0x90 && code <= 0x93
2284 /* Note that we've changed the relocation contents, etc. */
2285 elf_section_data (sec)->relocs = internal_relocs;
2288 elf_section_data (sec)->this_hdr.contents = contents;
2289 free_contents = NULL;
2291 free_extsyms = NULL;
2293 /* Fix the opcode. */
2294 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2295 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2297 /* Fix the relocation's type. */
2298 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2301 /* Delete two bytes of data. */
2302 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2303 irel->r_offset + 2, 2))
2306 /* That will change things, so, we should relax again.
2307 Note that this is not required, and it may be slow. */
2311 else if ((code & 0xf0) < 0xf0)
2312 switch (code & 0xfc)
2314 /* mov imm32,dn -> mov imm16,dn
2315 mov imm32,an -> mov imm16,an
2316 mov (abs32),dn -> mov (abs16),dn
2317 movbu (abs32),dn -> movbu (abs16),dn
2318 movhu (abs32),dn -> movhu (abs16),dn */
2324 /* Not safe if the high bit is on as relaxing may
2325 move the value out of high mem and thus not fit
2326 in a signed 16bit value. */
2328 && (value & 0x8000))
2331 /* mov imm16, an zero-extends the immediate. */
2336 /* Note that we've changed the relocation contents, etc. */
2337 elf_section_data (sec)->relocs = internal_relocs;
2340 elf_section_data (sec)->this_hdr.contents = contents;
2341 free_contents = NULL;
2343 free_extsyms = NULL;
2345 if ((code & 0xfc) == 0xcc)
2346 code = 0x2c + (code & 0x03);
2347 else if ((code & 0xfc) == 0xdc)
2348 code = 0x24 + (code & 0x03);
2349 else if ((code & 0xfc) == 0xa4)
2350 code = 0x30 + (code & 0x03);
2351 else if ((code & 0xfc) == 0xa8)
2352 code = 0x34 + (code & 0x03);
2353 else if ((code & 0xfc) == 0xac)
2354 code = 0x38 + (code & 0x03);
2358 /* Fix the opcode. */
2359 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2361 /* Fix the relocation's type. */
2362 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2365 /* The opcode got shorter too, so we have to fix the
2366 addend and offset too! */
2367 irel->r_offset -= 1;
2369 /* Delete three bytes of data. */
2370 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2371 irel->r_offset + 1, 3))
2374 /* That will change things, so, we should relax again.
2375 Note that this is not required, and it may be slow. */
2379 /* mov (abs32),an -> mov (abs16),an
2380 mov (d32,sp),an -> mov (d16,sp),an
2381 mov (d32,sp),dn -> mov (d16,sp),dn
2382 movbu (d32,sp),dn -> movbu (d16,sp),dn
2383 movhu (d32,sp),dn -> movhu (d16,sp),dn
2384 add imm32,dn -> add imm16,dn
2385 cmp imm32,dn -> cmp imm16,dn
2386 add imm32,an -> add imm16,an
2387 cmp imm32,an -> cmp imm16,an
2388 and imm32,dn -> and imm16,dn
2389 or imm32,dn -> or imm16,dn
2390 xor imm32,dn -> xor imm16,dn
2391 btst imm32,dn -> btst imm16,dn */
2407 /* cmp imm16, an zero-extends the immediate. */
2412 /* So do sp-based offsets. */
2413 if (code >= 0xb0 && code <= 0xb3
2417 /* Note that we've changed the relocation contents, etc. */
2418 elf_section_data (sec)->relocs = internal_relocs;
2421 elf_section_data (sec)->this_hdr.contents = contents;
2422 free_contents = NULL;
2424 free_extsyms = NULL;
2426 /* Fix the opcode. */
2427 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2428 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2430 /* Fix the relocation's type. */
2431 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2434 /* Delete two bytes of data. */
2435 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2436 irel->r_offset + 2, 2))
2439 /* That will change things, so, we should relax again.
2440 Note that this is not required, and it may be slow. */
2444 else if (code == 0xfe)
2446 /* add imm32,sp -> add imm16,sp */
2448 /* Note that we've changed the relocation contents, etc. */
2449 elf_section_data (sec)->relocs = internal_relocs;
2452 elf_section_data (sec)->this_hdr.contents = contents;
2453 free_contents = NULL;
2455 free_extsyms = NULL;
2457 /* Fix the opcode. */
2458 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2459 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2461 /* Fix the relocation's type. */
2462 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2465 /* Delete two bytes of data. */
2466 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2467 irel->r_offset + 2, 2))
2470 /* That will change things, so, we should relax again.
2471 Note that this is not required, and it may be slow. */
2479 if (free_relocs != NULL)
2482 if (free_contents != NULL)
2484 if (! link_info->keep_memory)
2485 free (free_contents);
2488 /* Cache the section contents for elf_link_input_bfd. */
2489 elf_section_data (sec)->this_hdr.contents = contents;
2493 if (shndx_buf != NULL)
2495 shndx_hdr->contents = NULL;
2499 if (free_extsyms != NULL)
2501 if (! link_info->keep_memory)
2503 symtab_hdr->contents = NULL;
2504 free (free_extsyms);
2511 if (free_relocs != NULL)
2513 if (free_contents != NULL)
2514 free (free_contents);
2515 if (shndx_buf != NULL)
2517 shndx_hdr->contents = NULL;
2520 if (free_extsyms != NULL)
2522 symtab_hdr->contents = NULL;
2523 free (free_extsyms);
2529 /* Compute the stack size and movm arguments for the function
2530 referred to by HASH at address ADDR in section with
2531 contents CONTENTS, store the information in the hash table. */
2533 compute_function_info (abfd, hash, addr, contents)
2535 struct elf32_mn10300_link_hash_entry *hash;
2537 unsigned char *contents;
2539 unsigned char byte1, byte2;
2540 /* We only care about a very small subset of the possible prologue
2541 sequences here. Basically we look for:
2543 movm [d2,d3,a2,a3],sp (optional)
2544 add <size>,sp (optional, and only for sizes which fit in an unsigned
2547 If we find anything else, we quit. */
2549 /* Look for movm [regs],sp */
2550 byte1 = bfd_get_8 (abfd, contents + addr);
2551 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2555 hash->movm_args = byte2;
2557 byte1 = bfd_get_8 (abfd, contents + addr);
2558 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2561 /* Now figure out how much stack space will be allocated by the movm
2562 instruction. We need this kept separate from the funtion's normal
2564 if (hash->movm_args)
2567 if (hash->movm_args & 0x80)
2568 hash->movm_stack_size += 4;
2571 if (hash->movm_args & 0x40)
2572 hash->movm_stack_size += 4;
2575 if (hash->movm_args & 0x20)
2576 hash->movm_stack_size += 4;
2579 if (hash->movm_args & 0x10)
2580 hash->movm_stack_size += 4;
2582 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2583 if (hash->movm_args & 0x08)
2584 hash->movm_stack_size += 8 * 4;
2586 if (bfd_get_mach (abfd) == bfd_mach_am33)
2588 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2589 if (hash->movm_args & 0x1)
2590 hash->movm_stack_size += 6 * 4;
2592 /* exreg1 space. e4, e5, e6, e7 */
2593 if (hash->movm_args & 0x2)
2594 hash->movm_stack_size += 4 * 4;
2596 /* exreg0 space. e2, e3 */
2597 if (hash->movm_args & 0x4)
2598 hash->movm_stack_size += 2 * 4;
2602 /* Now look for the two stack adjustment variants. */
2603 if (byte1 == 0xf8 && byte2 == 0xfe)
2605 int temp = bfd_get_8 (abfd, contents + addr + 2);
2606 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2608 hash->stack_size = -temp;
2610 else if (byte1 == 0xfa && byte2 == 0xfe)
2612 int temp = bfd_get_16 (abfd, contents + addr + 2);
2613 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2617 hash->stack_size = temp;
2620 /* If the total stack to be allocated by the call instruction is more
2621 than 255 bytes, then we can't remove the stack adjustment by using
2622 "call" (we might still be able to remove the "movm" instruction. */
2623 if (hash->stack_size + hash->movm_stack_size > 255)
2624 hash->stack_size = 0;
2629 /* Delete some bytes from a section while relaxing. */
2632 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2638 Elf_Internal_Shdr *symtab_hdr;
2639 Elf_Internal_Shdr *shndx_hdr;
2640 Elf32_External_Sym *extsyms;
2641 unsigned int sec_shndx;
2643 Elf_Internal_Rela *irel, *irelend;
2644 Elf_Internal_Rela *irelalign;
2646 Elf32_External_Sym *esym, *esymend;
2647 Elf_External_Sym_Shndx *shndx;
2648 struct elf_link_hash_entry **sym_hashes;
2649 struct elf_link_hash_entry **end_hashes;
2650 unsigned int symcount;
2652 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2653 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2655 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2657 contents = elf_section_data (sec)->this_hdr.contents;
2659 /* The deletion must stop at the next ALIGN reloc for an aligment
2660 power larger than the number of bytes we are deleting. */
2663 toaddr = sec->_cooked_size;
2665 irel = elf_section_data (sec)->relocs;
2666 irelend = irel + sec->reloc_count;
2668 /* Actually delete the bytes. */
2669 memmove (contents + addr, contents + addr + count,
2670 (size_t) (toaddr - addr - count));
2671 sec->_cooked_size -= count;
2673 /* Adjust all the relocs. */
2674 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2676 /* Get the new reloc address. */
2677 if ((irel->r_offset > addr
2678 && irel->r_offset < toaddr))
2679 irel->r_offset -= count;
2682 /* Adjust the local symbols defined in this section. */
2683 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2684 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2686 esymend = esym + symtab_hdr->sh_info;
2687 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2689 Elf_Internal_Sym isym;
2690 Elf_External_Sym_Shndx dummy;
2692 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2694 if (isym.st_shndx == sec_shndx
2695 && isym.st_value > addr
2696 && isym.st_value < toaddr)
2698 isym.st_value -= count;
2699 bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy);
2703 /* Now adjust the global symbols defined in this section. */
2704 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2705 - symtab_hdr->sh_info);
2706 sym_hashes = elf_sym_hashes (abfd);
2707 end_hashes = sym_hashes + symcount;
2708 for (; sym_hashes < end_hashes; sym_hashes++)
2710 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2711 if ((sym_hash->root.type == bfd_link_hash_defined
2712 || sym_hash->root.type == bfd_link_hash_defweak)
2713 && sym_hash->root.u.def.section == sec
2714 && sym_hash->root.u.def.value > addr
2715 && sym_hash->root.u.def.value < toaddr)
2717 sym_hash->root.u.def.value -= count;
2724 /* Return true if a symbol exists at the given address, else return
2727 mn10300_elf_symbol_address_p (abfd, sec, addr)
2732 Elf_Internal_Shdr *symtab_hdr;
2733 Elf_Internal_Shdr *shndx_hdr;
2734 unsigned int sec_shndx;
2735 Elf32_External_Sym *esym, *esymend;
2736 Elf_External_Sym_Shndx *shndx;
2737 struct elf_link_hash_entry **sym_hashes;
2738 struct elf_link_hash_entry **end_hashes;
2739 unsigned int symcount;
2741 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2743 /* Examine all the symbols. */
2744 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2745 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2746 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2747 esym = (Elf32_External_Sym *) symtab_hdr->contents;
2748 esymend = esym + symtab_hdr->sh_info;
2749 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
2751 Elf_Internal_Sym isym;
2753 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2755 if (isym.st_shndx == sec_shndx
2756 && isym.st_value == addr)
2760 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2761 - symtab_hdr->sh_info);
2762 sym_hashes = elf_sym_hashes (abfd);
2763 end_hashes = sym_hashes + symcount;
2764 for (; sym_hashes < end_hashes; sym_hashes++)
2766 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2767 if ((sym_hash->root.type == bfd_link_hash_defined
2768 || sym_hash->root.type == bfd_link_hash_defweak)
2769 && sym_hash->root.u.def.section == sec
2770 && sym_hash->root.u.def.value == addr)
2777 /* This is a version of bfd_generic_get_relocated_section_contents
2778 which uses mn10300_elf_relocate_section. */
2781 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2782 data, relocateable, symbols)
2784 struct bfd_link_info *link_info;
2785 struct bfd_link_order *link_order;
2787 boolean relocateable;
2790 Elf_Internal_Shdr *symtab_hdr;
2791 Elf_Internal_Shdr *shndx_hdr;
2792 asection *input_section = link_order->u.indirect.section;
2793 bfd *input_bfd = input_section->owner;
2794 asection **sections = NULL;
2795 Elf_Internal_Rela *internal_relocs = NULL;
2796 Elf32_External_Sym *external_syms = NULL;
2797 Elf_External_Sym_Shndx *shndx_buf = NULL;
2798 Elf_External_Sym_Shndx *shndx;
2799 Elf_Internal_Sym *internal_syms = NULL;
2801 /* We only need to handle the case of relaxing, or of having a
2802 particular set of section contents, specially. */
2804 || elf_section_data (input_section)->this_hdr.contents == NULL)
2805 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2810 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2811 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
2813 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2814 (size_t) input_section->_raw_size);
2816 if ((input_section->flags & SEC_RELOC) != 0
2817 && input_section->reloc_count > 0)
2819 Elf_Internal_Sym *isymp;
2821 Elf32_External_Sym *esym, *esymend;
2824 if (symtab_hdr->contents != NULL)
2825 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2826 else if (symtab_hdr->sh_info != 0)
2828 amt = symtab_hdr->sh_info;
2829 amt *= sizeof (Elf32_External_Sym);
2830 external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
2831 if (external_syms == NULL)
2833 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2834 || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
2838 if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
2840 amt = symtab_hdr->sh_info;
2841 amt *= sizeof (Elf_External_Sym_Shndx);
2842 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2843 if (shndx_buf == NULL)
2845 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2846 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
2850 internal_relocs = (_bfd_elf32_link_read_relocs
2851 (input_bfd, input_section, (PTR) NULL,
2852 (Elf_Internal_Rela *) NULL, false));
2853 if (internal_relocs == NULL)
2856 amt = symtab_hdr->sh_info;
2857 amt *= sizeof (Elf_Internal_Sym);
2858 internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
2859 if (internal_syms == NULL && amt != 0)
2862 amt = symtab_hdr->sh_info;
2863 amt *= sizeof (asection *);
2864 sections = (asection **) bfd_malloc (amt);
2865 if (sections == NULL && amt != 0)
2868 for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
2869 esym = external_syms, esymend = esym + symtab_hdr->sh_info;
2871 ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
2875 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, isymp);
2877 if (isymp->st_shndx == SHN_UNDEF)
2878 isec = bfd_und_section_ptr;
2879 else if (isymp->st_shndx == SHN_ABS)
2880 isec = bfd_abs_section_ptr;
2881 else if (isymp->st_shndx == SHN_COMMON)
2882 isec = bfd_com_section_ptr;
2884 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2889 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2890 input_section, data, internal_relocs,
2891 internal_syms, sections))
2894 if (sections != NULL)
2896 if (internal_syms != NULL)
2897 free (internal_syms);
2898 if (shndx_buf != NULL)
2900 if (external_syms != NULL && symtab_hdr->contents == NULL)
2901 free (external_syms);
2902 if (internal_relocs != elf_section_data (input_section)->relocs)
2903 free (internal_relocs);
2909 if (internal_relocs != NULL
2910 && internal_relocs != elf_section_data (input_section)->relocs)
2911 free (internal_relocs);
2912 if (shndx_buf != NULL)
2914 if (external_syms != NULL && symtab_hdr->contents == NULL)
2915 free (external_syms);
2916 if (internal_syms != NULL)
2917 free (internal_syms);
2918 if (sections != NULL)
2923 /* Assorted hash table functions. */
2925 /* Initialize an entry in the link hash table. */
2927 /* Create an entry in an MN10300 ELF linker hash table. */
2929 static struct bfd_hash_entry *
2930 elf32_mn10300_link_hash_newfunc (entry, table, string)
2931 struct bfd_hash_entry *entry;
2932 struct bfd_hash_table *table;
2935 struct elf32_mn10300_link_hash_entry *ret =
2936 (struct elf32_mn10300_link_hash_entry *) entry;
2938 /* Allocate the structure if it has not already been allocated by a
2940 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2941 ret = ((struct elf32_mn10300_link_hash_entry *)
2942 bfd_hash_allocate (table,
2943 sizeof (struct elf32_mn10300_link_hash_entry)));
2944 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2945 return (struct bfd_hash_entry *) ret;
2947 /* Call the allocation method of the superclass. */
2948 ret = ((struct elf32_mn10300_link_hash_entry *)
2949 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2951 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2953 ret->direct_calls = 0;
2954 ret->stack_size = 0;
2955 ret->movm_stack_size = 0;
2960 return (struct bfd_hash_entry *) ret;
2963 /* Create an mn10300 ELF linker hash table. */
2965 static struct bfd_link_hash_table *
2966 elf32_mn10300_link_hash_table_create (abfd)
2969 struct elf32_mn10300_link_hash_table *ret;
2970 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2972 ret = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2973 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2976 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2977 elf32_mn10300_link_hash_newfunc))
2979 bfd_release (abfd, ret);
2984 amt = sizeof (struct elf_link_hash_table);
2985 ret->static_hash_table
2986 = (struct elf32_mn10300_link_hash_table *) bfd_alloc (abfd, amt);
2987 if (ret->static_hash_table == NULL)
2989 bfd_release (abfd, ret);
2993 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2994 elf32_mn10300_link_hash_newfunc))
2996 bfd_release (abfd, ret->static_hash_table);
2997 bfd_release (abfd, ret);
3000 return &ret->root.root;
3003 static unsigned long
3004 elf_mn10300_mach (flags)
3007 switch (flags & EF_MN10300_MACH)
3009 case E_MN10300_MACH_MN10300:
3011 return bfd_mach_mn10300;
3013 case E_MN10300_MACH_AM33:
3014 return bfd_mach_am33;
3018 /* The final processing done just before writing out a MN10300 ELF object
3019 file. This gets the MN10300 architecture right based on the machine
3023 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3025 boolean linker ATTRIBUTE_UNUSED;
3029 switch (bfd_get_mach (abfd))
3032 case bfd_mach_mn10300:
3033 val = E_MN10300_MACH_MN10300;
3037 val = E_MN10300_MACH_AM33;
3041 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3042 elf_elfheader (abfd)->e_flags |= val;
3046 _bfd_mn10300_elf_object_p (abfd)
3049 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3050 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3054 /* Merge backend specific data from an object file to the output
3055 object file when linking. */
3058 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3062 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3063 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3066 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3067 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3069 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3070 bfd_get_mach (ibfd)))
3077 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
3078 #define TARGET_LITTLE_NAME "elf32-mn10300"
3079 #define ELF_ARCH bfd_arch_mn10300
3080 #define ELF_MACHINE_CODE EM_MN10300
3081 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
3082 #define ELF_MAXPAGESIZE 0x1000
3084 #define elf_info_to_howto mn10300_info_to_howto
3085 #define elf_info_to_howto_rel 0
3086 #define elf_backend_can_gc_sections 1
3087 #define elf_backend_check_relocs mn10300_elf_check_relocs
3088 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
3089 #define elf_backend_relocate_section mn10300_elf_relocate_section
3090 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
3091 #define bfd_elf32_bfd_get_relocated_section_contents \
3092 mn10300_elf_get_relocated_section_contents
3093 #define bfd_elf32_bfd_link_hash_table_create \
3094 elf32_mn10300_link_hash_table_create
3096 #define elf_symbol_leading_char '_'
3098 /* So we can set bits in e_flags. */
3099 #define elf_backend_final_write_processing \
3100 _bfd_mn10300_elf_final_write_processing
3101 #define elf_backend_object_p _bfd_mn10300_elf_object_p
3103 #define bfd_elf32_bfd_merge_private_bfd_data \
3104 _bfd_mn10300_elf_merge_private_bfd_data
3106 #include "elf32-target.h"