1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "elf/mn10300.h"
26 struct elf32_mn10300_link_hash_entry
28 /* The basic elf link hash table entry. */
29 struct elf_link_hash_entry root;
31 /* For function symbols, the number of times this function is
32 called directly (ie by name). */
33 unsigned int direct_calls;
35 /* For function symbols, the size of this function's stack
36 (if <= 255 bytes). We stuff this into "call" instructions
37 to this target when it's valid and profitable to do so.
39 This does not include stack allocated by movm! */
40 unsigned char stack_size;
42 /* For function symbols, arguments (if any) for movm instruction
43 in the prologue. We stuff this value into "call" instructions
44 to the target when it's valid and profitable to do so. */
45 unsigned char movm_args;
47 /* For funtion symbols, the amount of stack space that would be allocated
48 by the movm instruction. This is redundant with movm_args, but we
49 add it to the hash table to avoid computing it over and over. */
50 unsigned char movm_stack_size;
52 /* When set, convert all "call" instructions to this target into "calls"
54 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
56 /* Used to mark functions which have had redundant parts of their
58 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
62 /* We derive a hash table from the main elf linker hash table so
63 we can store state variables and a secondary hash table without
64 resorting to global variables. */
65 struct elf32_mn10300_link_hash_table
67 /* The main hash table. */
68 struct elf_link_hash_table root;
70 /* A hash table for static functions. We could derive a new hash table
71 instead of using the full elf32_mn10300_link_hash_table if we wanted
72 to save some memory. */
73 struct elf32_mn10300_link_hash_table *static_hash_table;
75 /* Random linker state flags. */
76 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
80 /* For MN10300 linker hash table. */
82 /* Get the MN10300 ELF linker hash table from a link_info structure. */
84 #define elf32_mn10300_hash_table(p) \
85 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
87 #define elf32_mn10300_link_hash_traverse(table, func, info) \
88 (elf_link_hash_traverse \
90 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
93 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
94 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
95 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
98 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
99 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
100 static void mn10300_info_to_howto
101 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
102 static boolean mn10300_elf_check_relocs
103 PARAMS ((bfd *, struct bfd_link_info *, asection *,
104 const Elf_Internal_Rela *));
105 static asection *mn10300_elf_gc_mark_hook
106 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
107 struct elf_link_hash_entry *, Elf_Internal_Sym *));
108 static boolean mn10300_elf_relax_delete_bytes
109 PARAMS ((bfd *, asection *, bfd_vma, int));
110 static boolean mn10300_elf_symbol_address_p
111 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
112 static boolean elf32_mn10300_finish_hash_table_entry
113 PARAMS ((struct bfd_hash_entry *, PTR));
114 static void compute_function_info
115 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
116 bfd_vma, unsigned char *));
118 /* We have to use RELA instructions since md_apply_fix3 in the assembler
119 does absolutely nothing. */
123 static reloc_howto_type elf_mn10300_howto_table[] =
125 /* Dummy relocation. Does nothing. */
126 HOWTO (R_MN10300_NONE,
132 complain_overflow_bitfield,
133 bfd_elf_generic_reloc,
139 /* Standard 32 bit reloc. */
146 complain_overflow_bitfield,
147 bfd_elf_generic_reloc,
153 /* Standard 16 bit reloc. */
160 complain_overflow_bitfield,
161 bfd_elf_generic_reloc,
167 /* Standard 8 bit reloc. */
174 complain_overflow_bitfield,
175 bfd_elf_generic_reloc,
181 /* Standard 32bit pc-relative reloc. */
182 HOWTO (R_MN10300_PCREL32,
188 complain_overflow_bitfield,
189 bfd_elf_generic_reloc,
195 /* Standard 16bit pc-relative reloc. */
196 HOWTO (R_MN10300_PCREL16,
202 complain_overflow_bitfield,
203 bfd_elf_generic_reloc,
209 /* Standard 8 pc-relative reloc. */
210 HOWTO (R_MN10300_PCREL8,
216 complain_overflow_bitfield,
217 bfd_elf_generic_reloc,
224 /* GNU extension to record C++ vtable hierarchy */
225 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
227 0, /* size (0 = byte, 1 = short, 2 = long) */
229 false, /* pc_relative */
231 complain_overflow_dont, /* complain_on_overflow */
232 NULL, /* special_function */
233 "R_MN10300_GNU_VTINHERIT", /* name */
234 false, /* partial_inplace */
237 false), /* pcrel_offset */
239 /* GNU extension to record C++ vtable member usage */
240 HOWTO (R_MN10300_GNU_VTENTRY, /* 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_VTENTRY", /* name */
249 false, /* partial_inplace */
252 false), /* pcrel_offset */
255 struct mn10300_reloc_map
257 unsigned char bfd_reloc_val;
258 unsigned char elf_reloc_val;
261 static const struct mn10300_reloc_map mn10300_reloc_map[] =
263 { BFD_RELOC_NONE, R_MN10300_NONE, },
264 { BFD_RELOC_32, R_MN10300_32, },
265 { BFD_RELOC_16, R_MN10300_16, },
266 { BFD_RELOC_8, R_MN10300_8, },
267 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
268 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
269 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
270 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
271 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
274 static reloc_howto_type *
275 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
277 bfd_reloc_code_real_type code;
282 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
285 if (mn10300_reloc_map[i].bfd_reloc_val == code)
286 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
292 /* Set the howto pointer for an MN10300 ELF reloc. */
295 mn10300_info_to_howto (abfd, cache_ptr, dst)
298 Elf32_Internal_Rela *dst;
302 r_type = ELF32_R_TYPE (dst->r_info);
303 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
304 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
307 /* Look through the relocs for a section during the first phase.
308 Since we don't do .gots or .plts, we just need to consider the
309 virtual table relocs for gc. */
312 mn10300_elf_check_relocs (abfd, info, sec, relocs)
314 struct bfd_link_info *info;
316 const Elf_Internal_Rela *relocs;
318 Elf_Internal_Shdr *symtab_hdr;
319 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
320 const Elf_Internal_Rela *rel;
321 const Elf_Internal_Rela *rel_end;
323 if (info->relocateable)
326 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
327 sym_hashes = elf_sym_hashes (abfd);
328 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
329 if (!elf_bad_symtab (abfd))
330 sym_hashes_end -= symtab_hdr->sh_info;
332 rel_end = relocs + sec->reloc_count;
333 for (rel = relocs; rel < rel_end; rel++)
335 struct elf_link_hash_entry *h;
336 unsigned long r_symndx;
338 r_symndx = ELF32_R_SYM (rel->r_info);
339 if (r_symndx < symtab_hdr->sh_info)
342 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
344 switch (ELF32_R_TYPE (rel->r_info))
346 /* This relocation describes the C++ object vtable hierarchy.
347 Reconstruct it for later use during GC. */
348 case R_MN10300_GNU_VTINHERIT:
349 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
353 /* This relocation describes which C++ vtable entries are actually
354 used. Record for later use during GC. */
355 case R_MN10300_GNU_VTENTRY:
356 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
365 /* Return the section that should be marked against GC for a given
369 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
371 struct bfd_link_info *info;
372 Elf_Internal_Rela *rel;
373 struct elf_link_hash_entry *h;
374 Elf_Internal_Sym *sym;
378 switch (ELF32_R_TYPE (rel->r_info))
380 case R_MN10300_GNU_VTINHERIT:
381 case R_MN10300_GNU_VTENTRY:
385 switch (h->root.type)
387 case bfd_link_hash_defined:
388 case bfd_link_hash_defweak:
389 return h->root.u.def.section;
391 case bfd_link_hash_common:
392 return h->root.u.c.p->section;
398 if (!(elf_bad_symtab (abfd)
399 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
400 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
401 && sym->st_shndx != SHN_COMMON))
403 return bfd_section_from_elf_index (abfd, sym->st_shndx);
410 /* Perform a relocation as part of a final link. */
411 static bfd_reloc_status_type
412 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
413 input_section, contents, offset, value,
414 addend, info, sym_sec, is_local)
415 reloc_howto_type *howto;
418 asection *input_section;
423 struct bfd_link_info *info;
427 unsigned long r_type = howto->type;
428 bfd_byte *hit_data = contents + offset;
437 bfd_put_32 (input_bfd, value, hit_data);
443 if ((long)value > 0x7fff || (long)value < -0x8000)
444 return bfd_reloc_overflow;
446 bfd_put_16 (input_bfd, value, hit_data);
452 if ((long)value > 0x7f || (long)value < -0x80)
453 return bfd_reloc_overflow;
455 bfd_put_8 (input_bfd, value, hit_data);
458 case R_MN10300_PCREL8:
459 value -= (input_section->output_section->vma
460 + input_section->output_offset);
464 if ((long)value > 0xff || (long)value < -0x100)
465 return bfd_reloc_overflow;
467 bfd_put_8 (input_bfd, value, hit_data);
470 case R_MN10300_PCREL16:
471 value -= (input_section->output_section->vma
472 + input_section->output_offset);
476 if ((long)value > 0xffff || (long)value < -0x10000)
477 return bfd_reloc_overflow;
479 bfd_put_16 (input_bfd, value, hit_data);
482 case R_MN10300_PCREL32:
483 value -= (input_section->output_section->vma
484 + input_section->output_offset);
488 bfd_put_32 (input_bfd, value, hit_data);
491 case R_MN10300_GNU_VTINHERIT:
492 case R_MN10300_GNU_VTENTRY:
496 return bfd_reloc_notsupported;
501 /* Relocate an MN10300 ELF section. */
503 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
504 contents, relocs, local_syms, local_sections)
506 struct bfd_link_info *info;
508 asection *input_section;
510 Elf_Internal_Rela *relocs;
511 Elf_Internal_Sym *local_syms;
512 asection **local_sections;
514 Elf_Internal_Shdr *symtab_hdr;
515 struct elf32_mn10300_link_hash_entry **sym_hashes;
516 Elf_Internal_Rela *rel, *relend;
518 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
519 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
520 (elf_sym_hashes (input_bfd));
523 relend = relocs + input_section->reloc_count;
524 for (; rel < relend; rel++)
527 reloc_howto_type *howto;
528 unsigned long r_symndx;
529 Elf_Internal_Sym *sym;
531 struct elf32_mn10300_link_hash_entry *h;
533 bfd_reloc_status_type r;
535 r_symndx = ELF32_R_SYM (rel->r_info);
536 r_type = ELF32_R_TYPE (rel->r_info);
537 howto = elf_mn10300_howto_table + r_type;
539 /* Just skip the vtable gc relocs. */
540 if (r_type == R_MN10300_GNU_VTINHERIT
541 || r_type == R_MN10300_GNU_VTENTRY)
544 if (info->relocateable)
546 /* This is a relocateable link. We don't have to change
547 anything, unless the reloc is against a section symbol,
548 in which case we have to adjust according to where the
549 section symbol winds up in the output section. */
550 if (r_symndx < symtab_hdr->sh_info)
552 sym = local_syms + r_symndx;
553 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
555 sec = local_sections[r_symndx];
556 rel->r_addend += sec->output_offset + sym->st_value;
563 /* This is a final link. */
567 if (r_symndx < symtab_hdr->sh_info)
569 sym = local_syms + r_symndx;
570 sec = local_sections[r_symndx];
571 relocation = (sec->output_section->vma
577 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
578 while (h->root.type == bfd_link_hash_indirect
579 || h->root.type == bfd_link_hash_warning)
580 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
581 if (h->root.root.type == bfd_link_hash_defined
582 || h->root.root.type == bfd_link_hash_defweak)
584 sec = h->root.root.u.def.section;
585 relocation = (h->root.root.u.def.value
586 + sec->output_section->vma
587 + sec->output_offset);
589 else if (h->root.root.type == bfd_link_hash_undefweak)
593 if (! ((*info->callbacks->undefined_symbol)
594 (info, h->root.root.root.string, input_bfd,
595 input_section, rel->r_offset)))
601 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
603 contents, rel->r_offset,
604 relocation, rel->r_addend,
605 info, sec, h == NULL);
607 if (r != bfd_reloc_ok)
610 const char *msg = (const char *)0;
613 name = h->root.root.root.string;
616 name = (bfd_elf_string_from_elf_section
617 (input_bfd, symtab_hdr->sh_link, sym->st_name));
618 if (name == NULL || *name == '\0')
619 name = bfd_section_name (input_bfd, sec);
624 case bfd_reloc_overflow:
625 if (! ((*info->callbacks->reloc_overflow)
626 (info, name, howto->name, (bfd_vma) 0,
627 input_bfd, input_section, rel->r_offset)))
631 case bfd_reloc_undefined:
632 if (! ((*info->callbacks->undefined_symbol)
633 (info, name, input_bfd, input_section,
638 case bfd_reloc_outofrange:
639 msg = _("internal error: out of range error");
642 case bfd_reloc_notsupported:
643 msg = _("internal error: unsupported relocation error");
646 case bfd_reloc_dangerous:
647 msg = _("internal error: dangerous error");
651 msg = _("internal error: unknown error");
655 if (!((*info->callbacks->warning)
656 (info, msg, name, input_bfd, input_section,
667 /* Finish initializing one hash table entry. */
669 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
670 struct bfd_hash_entry *gen_entry;
673 struct elf32_mn10300_link_hash_entry *entry;
674 unsigned int byte_count = 0;
676 entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
678 /* If we already know we want to convert "call" to "calls" for calls
679 to this symbol, then return now. */
680 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
683 /* If there are no named calls to this symbol, or there's nothing we
684 can move from the function itself into the "call" instruction, then
685 note that all "call" instructions should be converted into "calls"
686 instructions and return. */
687 if (entry->direct_calls == 0
688 || (entry->stack_size == 0 && entry->movm_args == 0))
690 /* Make a note that we should convert "call" instructions to "calls"
691 instructions for calls to this symbol. */
692 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
696 /* We may be able to move some instructions from the function itself into
697 the "call" instruction. Count how many bytes we might be able to
698 eliminate in the function itself. */
700 /* A movm instruction is two bytes. */
701 if (entry->movm_args)
704 /* Count the insn to allocate stack space too. */
705 if (entry->stack_size > 0 && entry->stack_size <= 128)
707 else if (entry->stack_size > 0 && entry->stack_size < 256)
710 /* If using "call" will result in larger code, then turn all
711 the associated "call" instructions into "calls" instrutions. */
712 if (byte_count < entry->direct_calls)
713 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
715 /* This routine never fails. */
719 /* This function handles relaxing for the mn10300.
721 There's quite a few relaxing opportunites available on the mn10300:
723 * calls:32 -> calls:16 2 bytes
724 * call:32 -> call:16 2 bytes
726 * call:32 -> calls:32 1 byte
727 * call:16 -> calls:16 1 byte
728 * These are done anytime using "calls" would result
729 in smaller code, or when necessary to preserve the
730 meaning of the program.
734 * In some circumstances we can move instructions
735 from a function prologue into a "call" instruction.
736 This is only done if the resulting code is no larger
737 than the original code.
740 * jmp:32 -> jmp:16 2 bytes
741 * jmp:16 -> bra:8 1 byte
743 * If the previous instruction is a conditional branch
744 around the jump/bra, we may be able to reverse its condition
745 and change its target to the jump's target. The jump/bra
746 can then be deleted. 2 bytes
748 * mov abs32 -> mov abs16 1 or 2 bytes
750 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
751 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
753 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
754 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
756 We don't handle imm16->imm8 or d16->d8 as they're very rare
757 and somewhat more difficult to support. */
760 mn10300_elf_relax_section (abfd, sec, link_info, again)
763 struct bfd_link_info *link_info;
766 Elf_Internal_Shdr *symtab_hdr;
767 Elf_Internal_Rela *internal_relocs = NULL;
768 Elf_Internal_Rela *free_relocs = NULL;
769 Elf_Internal_Rela *irel, *irelend;
770 bfd_byte *contents = NULL;
771 bfd_byte *free_contents = NULL;
772 Elf32_External_Sym *extsyms = NULL;
773 Elf32_External_Sym *free_extsyms = NULL;
774 struct elf32_mn10300_link_hash_table *hash_table;
776 /* Assume nothing changes. */
779 /* We need a pointer to the mn10300 specific hash table. */
780 hash_table = elf32_mn10300_hash_table (link_info);
782 /* Initialize fields in each hash table entry the first time through. */
783 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
787 /* Iterate over all the input bfds. */
788 for (input_bfd = link_info->input_bfds;
790 input_bfd = input_bfd->link_next)
794 /* We're going to need all the symbols for each bfd. */
795 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
797 /* Get cached copy if it exists. */
798 if (symtab_hdr->contents != NULL)
799 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
802 /* Go get them off disk. */
803 extsyms = ((Elf32_External_Sym *)
804 bfd_malloc (symtab_hdr->sh_size));
807 free_extsyms = extsyms;
808 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
809 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
810 != symtab_hdr->sh_size))
814 /* Iterate over each section in this bfd. */
815 for (section = input_bfd->sections;
817 section = section->next)
819 struct elf32_mn10300_link_hash_entry *hash;
820 Elf_Internal_Sym *sym;
822 const char *sym_name;
824 Elf_Internal_Shdr *hdr;
826 /* Get cached copy of section contents if it exists. */
827 if (elf_section_data (section)->this_hdr.contents != NULL)
828 contents = elf_section_data (section)->this_hdr.contents;
829 else if (section->_raw_size != 0)
831 /* Go get them off disk. */
832 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
833 if (contents == NULL)
835 free_contents = contents;
837 if (!bfd_get_section_contents (input_bfd, section,
838 contents, (file_ptr) 0,
845 free_contents = NULL;
848 /* If there aren't any relocs, then there's nothing to do. */
849 if ((section->flags & SEC_RELOC) != 0
850 && section->reloc_count != 0)
853 /* Get a copy of the native relocations. */
854 internal_relocs = (_bfd_elf32_link_read_relocs
855 (input_bfd, section, (PTR) NULL,
856 (Elf_Internal_Rela *) NULL,
857 link_info->keep_memory));
858 if (internal_relocs == NULL)
860 if (! link_info->keep_memory)
861 free_relocs = internal_relocs;
863 /* Now examine each relocation. */
864 irel = internal_relocs;
865 irelend = irel + section->reloc_count;
866 for (; irel < irelend; irel++)
869 unsigned long r_index;
872 r_type = ELF32_R_TYPE (irel->r_info);
873 r_index = ELF32_R_SYM (irel->r_info);
875 if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
878 /* We need the name and hash table entry of the target
884 if (r_index < symtab_hdr->sh_info)
886 /* A local symbol. */
887 Elf_Internal_Sym isym;
889 bfd_elf32_swap_symbol_in (input_bfd,
890 extsyms + r_index, &isym);
892 if (isym.st_shndx == SHN_UNDEF)
893 sym_sec = bfd_und_section_ptr;
894 else if (isym.st_shndx > 0
895 && isym.st_shndx < SHN_LORESERVE)
897 = bfd_section_from_elf_index (input_bfd,
899 else if (isym.st_shndx == SHN_ABS)
900 sym_sec = bfd_abs_section_ptr;
901 else if (isym.st_shndx == SHN_COMMON)
902 sym_sec = bfd_com_section_ptr;
904 sym_name = bfd_elf_string_from_elf_section (input_bfd,
908 /* If it isn't a function, then we don't care
910 if (r_index < symtab_hdr->sh_info
911 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
914 /* Tack on an ID so we can uniquely identify this
915 local symbol in the global hash table. */
916 new_name = alloca (strlen (sym_name) + 10);
917 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
920 hash = (struct elf32_mn10300_link_hash_entry *)
921 elf_link_hash_lookup (&hash_table->static_hash_table->root,
927 r_index -= symtab_hdr->sh_info;
928 hash = (struct elf32_mn10300_link_hash_entry *)
929 elf_sym_hashes (input_bfd)[r_index];
932 /* If this is not a "call" instruction, then we
933 should convert "call" instructions to "calls"
935 code = bfd_get_8 (input_bfd,
936 contents + irel->r_offset - 1);
937 if (code != 0xdd && code != 0xcd)
938 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
940 /* If this is a jump/call, then bump the direct_calls
941 counter. Else force "call" to "calls" conversions. */
942 if (r_type == R_MN10300_PCREL32
943 || r_type == R_MN10300_PCREL16)
944 hash->direct_calls++;
946 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
950 /* Now look at the actual contents to get the stack size,
951 and a list of what registers were saved in the prologue
953 if ((section->flags & SEC_CODE) != 0)
956 Elf32_External_Sym *esym, *esymend;
959 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
963 /* Look at each function defined in this section and
964 update info for that function. */
966 esymend = esym + symtab_hdr->sh_info;
967 for (; esym < esymend; esym++)
969 Elf_Internal_Sym isym;
971 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
972 if (isym.st_shndx == shndx
973 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
975 if (isym.st_shndx == SHN_UNDEF)
976 sym_sec = bfd_und_section_ptr;
977 else if (isym.st_shndx > 0
978 && isym.st_shndx < SHN_LORESERVE)
980 = bfd_section_from_elf_index (input_bfd,
982 else if (isym.st_shndx == SHN_ABS)
983 sym_sec = bfd_abs_section_ptr;
984 else if (isym.st_shndx == SHN_COMMON)
985 sym_sec = bfd_com_section_ptr;
987 sym_name = bfd_elf_string_from_elf_section (input_bfd,
991 /* Tack on an ID so we can uniquely identify this
992 local symbol in the global hash table. */
993 new_name = alloca (strlen (sym_name) + 10);
994 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
997 hash = (struct elf32_mn10300_link_hash_entry *)
998 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1001 compute_function_info (input_bfd, hash,
1002 isym.st_value, contents);
1006 esym = extsyms + symtab_hdr->sh_info;
1007 esymend = extsyms + (symtab_hdr->sh_size
1008 / sizeof (Elf32_External_Sym));
1009 for (idx = 0; esym < esymend; esym++, idx++)
1011 Elf_Internal_Sym isym;
1013 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1014 hash = (struct elf32_mn10300_link_hash_entry *)
1015 elf_sym_hashes (input_bfd)[idx];
1016 if (isym.st_shndx == shndx
1017 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1018 && (hash)->root.root.u.def.section == section
1019 && ((hash)->root.root.type == bfd_link_hash_defined
1020 || (hash)->root.root.type == bfd_link_hash_defweak))
1021 compute_function_info (input_bfd, hash,
1022 (hash)->root.root.u.def.value,
1027 /* Cache or free any memory we allocated for the relocs. */
1028 if (free_relocs != NULL)
1034 /* Cache or free any memory we allocated for the contents. */
1035 if (free_contents != NULL)
1037 if (! link_info->keep_memory)
1038 free (free_contents);
1041 /* Cache the section contents for elf_link_input_bfd. */
1042 elf_section_data (section)->this_hdr.contents = contents;
1044 free_contents = NULL;
1048 /* Cache or free any memory we allocated for the symbols. */
1049 if (free_extsyms != NULL)
1051 if (! link_info->keep_memory)
1052 free (free_extsyms);
1055 /* Cache the symbols for elf_link_input_bfd. */
1056 symtab_hdr->contents = extsyms;
1058 free_extsyms = NULL;
1062 /* Now iterate on each symbol in the hash table and perform
1063 the final initialization steps on each. */
1064 elf32_mn10300_link_hash_traverse (hash_table,
1065 elf32_mn10300_finish_hash_table_entry,
1067 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1068 elf32_mn10300_finish_hash_table_entry,
1071 /* All entries in the hash table are fully initialized. */
1072 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1074 /* Now that everything has been initialized, go through each
1075 code section and delete any prologue insns which will be
1076 redundant because their operations will be performed by
1077 a "call" instruction. */
1078 for (input_bfd = link_info->input_bfds;
1080 input_bfd = input_bfd->link_next)
1084 /* We're going to need all the symbols for each bfd. */
1085 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1087 /* Get cached copy if it exists. */
1088 if (symtab_hdr->contents != NULL)
1089 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1092 /* Go get them off disk. */
1093 extsyms = ((Elf32_External_Sym *)
1094 bfd_malloc (symtab_hdr->sh_size));
1095 if (extsyms == NULL)
1097 free_extsyms = extsyms;
1098 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1099 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1100 != symtab_hdr->sh_size))
1104 /* Walk over each section in this bfd. */
1105 for (section = input_bfd->sections;
1107 section = section->next)
1110 Elf32_External_Sym *esym, *esymend;
1113 /* Skip non-code sections and empty sections. */
1114 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1117 if (section->reloc_count != 0)
1119 /* Get a copy of the native relocations. */
1120 internal_relocs = (_bfd_elf32_link_read_relocs
1121 (input_bfd, section, (PTR) NULL,
1122 (Elf_Internal_Rela *) NULL,
1123 link_info->keep_memory));
1124 if (internal_relocs == NULL)
1126 if (! link_info->keep_memory)
1127 free_relocs = internal_relocs;
1130 /* Get cached copy of section contents if it exists. */
1131 if (elf_section_data (section)->this_hdr.contents != NULL)
1132 contents = elf_section_data (section)->this_hdr.contents;
1135 /* Go get them off disk. */
1136 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1137 if (contents == NULL)
1139 free_contents = contents;
1141 if (!bfd_get_section_contents (input_bfd, section,
1142 contents, (file_ptr) 0,
1143 section->_raw_size))
1148 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1150 /* Now look for any function in this section which needs
1151 insns deleted from its prologue. */
1153 esymend = esym + symtab_hdr->sh_info;
1154 for (; esym < esymend; esym++)
1156 Elf_Internal_Sym isym;
1157 struct elf32_mn10300_link_hash_entry *sym_hash;
1159 const char *sym_name;
1160 Elf_Internal_Shdr *hdr;
1163 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1165 if (isym.st_shndx != shndx)
1168 if (isym.st_shndx == SHN_UNDEF)
1169 sym_sec = bfd_und_section_ptr;
1170 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1172 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1173 else if (isym.st_shndx == SHN_ABS)
1174 sym_sec = bfd_abs_section_ptr;
1175 else if (isym.st_shndx == SHN_COMMON)
1176 sym_sec = bfd_com_section_ptr;
1178 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1179 symtab_hdr->sh_link,
1182 /* Tack on an ID so we can uniquely identify this
1183 local symbol in the global hash table. */
1184 new_name = alloca (strlen (sym_name) + 10);
1185 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1186 sym_name = new_name;
1188 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1189 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1193 if (sym_hash == NULL)
1196 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1197 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1201 /* Note that we've changed things. */
1202 elf_section_data (section)->relocs = internal_relocs;
1205 elf_section_data (section)->this_hdr.contents = contents;
1206 free_contents = NULL;
1208 symtab_hdr->contents = (bfd_byte *)extsyms;
1209 free_extsyms = NULL;
1211 /* Count how many bytes we're going to delete. */
1212 if (sym_hash->movm_args)
1215 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1217 else if (sym_hash->stack_size
1218 && sym_hash->stack_size < 256)
1221 /* Note that we've deleted prologue bytes for this
1223 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1225 /* Actually delete the bytes. */
1226 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1232 /* Something changed. Not strictly necessary, but
1233 may lead to more relaxing opportunities. */
1238 /* Look for any global functions in this section which
1239 need insns deleted from their prologues. */
1240 esym = extsyms + symtab_hdr->sh_info;
1241 esymend = extsyms + (symtab_hdr->sh_size
1242 / sizeof (Elf32_External_Sym));
1243 for (idx = 0; esym < esymend; esym++, idx++)
1245 Elf_Internal_Sym isym;
1246 struct elf32_mn10300_link_hash_entry *sym_hash;
1248 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1249 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1250 (elf_sym_hashes (input_bfd)[idx]);
1251 if (isym.st_shndx == shndx
1252 && (sym_hash)->root.root.u.def.section == section
1253 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1254 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1258 /* Note that we've changed things. */
1259 elf_section_data (section)->relocs = internal_relocs;
1262 elf_section_data (section)->this_hdr.contents = contents;
1263 free_contents = NULL;
1265 symtab_hdr->contents = (bfd_byte *)extsyms;
1266 free_extsyms = NULL;
1268 /* Count how many bytes we're going to delete. */
1269 if (sym_hash->movm_args)
1272 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1274 else if (sym_hash->stack_size
1275 && sym_hash->stack_size < 256)
1278 /* Note that we've deleted prologue bytes for this
1280 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1282 /* Actually delete the bytes. */
1283 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1285 (sym_hash)->root.root.u.def.value,
1289 /* Something changed. Not strictly necessary, but
1290 may lead to more relaxing opportunities. */
1295 /* Cache or free any memory we allocated for the relocs. */
1296 if (free_relocs != NULL)
1302 /* Cache or free any memory we allocated for the contents. */
1303 if (free_contents != NULL)
1305 if (! link_info->keep_memory)
1306 free (free_contents);
1309 /* Cache the section contents for elf_link_input_bfd. */
1310 elf_section_data (section)->this_hdr.contents = contents;
1312 free_contents = NULL;
1316 /* Cache or free any memory we allocated for the symbols. */
1317 if (free_extsyms != NULL)
1319 if (! link_info->keep_memory)
1320 free (free_extsyms);
1323 /* Cache the symbols for elf_link_input_bfd. */
1324 symtab_hdr->contents = extsyms;
1326 free_extsyms = NULL;
1332 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1335 internal_relocs = NULL;
1337 free_contents = NULL;
1338 free_extsyms = NULL;
1340 /* We don't have to do anything for a relocateable link, if
1341 this section does not have relocs, or if this is not a
1343 if (link_info->relocateable
1344 || (sec->flags & SEC_RELOC) == 0
1345 || sec->reloc_count == 0
1346 || (sec->flags & SEC_CODE) == 0)
1349 /* If this is the first time we have been called for this section,
1350 initialize the cooked size. */
1351 if (sec->_cooked_size == 0)
1352 sec->_cooked_size = sec->_raw_size;
1354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1356 /* Get a copy of the native relocations. */
1357 internal_relocs = (_bfd_elf32_link_read_relocs
1358 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1359 link_info->keep_memory));
1360 if (internal_relocs == NULL)
1362 if (! link_info->keep_memory)
1363 free_relocs = internal_relocs;
1365 /* Walk through them looking for relaxing opportunities. */
1366 irelend = internal_relocs + sec->reloc_count;
1367 for (irel = internal_relocs; irel < irelend; irel++)
1370 struct elf32_mn10300_link_hash_entry *h = NULL;
1372 /* If this isn't something that can be relaxed, then ignore
1374 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1375 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1376 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1379 /* Get the section contents if we haven't done so already. */
1380 if (contents == NULL)
1382 /* Get cached copy if it exists. */
1383 if (elf_section_data (sec)->this_hdr.contents != NULL)
1384 contents = elf_section_data (sec)->this_hdr.contents;
1387 /* Go get them off disk. */
1388 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1389 if (contents == NULL)
1391 free_contents = contents;
1393 if (! bfd_get_section_contents (abfd, sec, contents,
1394 (file_ptr) 0, sec->_raw_size))
1399 /* Read this BFD's symbols if we haven't done so already. */
1400 if (extsyms == NULL)
1402 /* Get cached copy if it exists. */
1403 if (symtab_hdr->contents != NULL)
1404 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1407 /* Go get them off disk. */
1408 extsyms = ((Elf32_External_Sym *)
1409 bfd_malloc (symtab_hdr->sh_size));
1410 if (extsyms == NULL)
1412 free_extsyms = extsyms;
1413 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1414 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1415 != symtab_hdr->sh_size))
1420 /* Get the value of the symbol referred to by the reloc. */
1421 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1423 Elf_Internal_Sym isym;
1425 Elf_Internal_Shdr *hdr;
1426 const char *sym_name;
1429 /* A local symbol. */
1430 bfd_elf32_swap_symbol_in (abfd,
1431 extsyms + ELF32_R_SYM (irel->r_info),
1434 if (isym.st_shndx == SHN_UNDEF)
1435 sym_sec = bfd_und_section_ptr;
1436 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1437 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1438 else if (isym.st_shndx == SHN_ABS)
1439 sym_sec = bfd_abs_section_ptr;
1440 else if (isym.st_shndx == SHN_COMMON)
1441 sym_sec = bfd_com_section_ptr;
1443 symval = (isym.st_value
1444 + sym_sec->output_section->vma
1445 + sym_sec->output_offset);
1446 sym_name = bfd_elf_string_from_elf_section (abfd,
1447 symtab_hdr->sh_link,
1450 /* Tack on an ID so we can uniquely identify this
1451 local symbol in the global hash table. */
1452 new_name = alloca (strlen (sym_name) + 10);
1453 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1454 sym_name = new_name;
1456 h = (struct elf32_mn10300_link_hash_entry *)
1457 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1458 sym_name, false, false, false);
1464 /* An external symbol. */
1465 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1466 h = (struct elf32_mn10300_link_hash_entry *)
1467 (elf_sym_hashes (abfd)[indx]);
1468 BFD_ASSERT (h != NULL);
1469 if (h->root.root.type != bfd_link_hash_defined
1470 && h->root.root.type != bfd_link_hash_defweak)
1472 /* This appears to be a reference to an undefined
1473 symbol. Just ignore it--it will be caught by the
1474 regular reloc processing. */
1478 symval = (h->root.root.u.def.value
1479 + h->root.root.u.def.section->output_section->vma
1480 + h->root.root.u.def.section->output_offset);
1483 /* For simplicity of coding, we are going to modify the section
1484 contents, the section relocs, and the BFD symbol table. We
1485 must tell the rest of the code not to free up this
1486 information. It would be possible to instead create a table
1487 of changes which have to be made, as is done in coff-mips.c;
1488 that would be more work, but would require less memory when
1489 the linker is run. */
1491 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1492 branch/call, also deal with "call" -> "calls" conversions and
1493 insertion of prologue data into "call" instructions. */
1494 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1496 bfd_vma value = symval;
1498 /* If we've got a "call" instruction that needs to be turned
1499 into a "calls" instruction, do so now. It saves a byte. */
1500 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1504 /* Get the opcode. */
1505 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1507 /* Make sure we're working with a "call" instruction! */
1510 /* Note that we've changed the relocs, section contents,
1512 elf_section_data (sec)->relocs = internal_relocs;
1515 elf_section_data (sec)->this_hdr.contents = contents;
1516 free_contents = NULL;
1518 symtab_hdr->contents = (bfd_byte *) extsyms;
1519 free_extsyms = NULL;
1521 /* Fix the opcode. */
1522 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1523 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1525 /* Fix irel->r_offset and irel->r_addend. */
1526 irel->r_offset += 1;
1527 irel->r_addend += 1;
1529 /* Delete one byte of data. */
1530 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1531 irel->r_offset + 3, 1))
1534 /* That will change things, so, we should relax again.
1535 Note that this is not required, and it may be slow. */
1541 /* We've got a "call" instruction which needs some data
1542 from target function filled in. */
1545 /* Get the opcode. */
1546 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1548 /* Insert data from the target function into the "call"
1549 instruction if needed. */
1552 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1553 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1554 contents + irel->r_offset + 5);
1558 /* Deal with pc-relative gunk. */
1559 value -= (sec->output_section->vma + sec->output_offset);
1560 value -= irel->r_offset;
1561 value += irel->r_addend;
1563 /* See if the value will fit in 16 bits, note the high value is
1564 0x7fff + 2 as the target will be two bytes closer if we are
1566 if ((long)value < 0x8001 && (long)value > -0x8000)
1570 /* Get the opcode. */
1571 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1573 if (code != 0xdc && code != 0xdd && code != 0xff)
1576 /* Note that we've changed the relocs, section contents, etc. */
1577 elf_section_data (sec)->relocs = internal_relocs;
1580 elf_section_data (sec)->this_hdr.contents = contents;
1581 free_contents = NULL;
1583 symtab_hdr->contents = (bfd_byte *) extsyms;
1584 free_extsyms = NULL;
1586 /* Fix the opcode. */
1588 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1589 else if (code == 0xdd)
1590 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1591 else if (code == 0xff)
1592 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1594 /* Fix the relocation's type. */
1595 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1598 /* Delete two bytes of data. */
1599 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1600 irel->r_offset + 1, 2))
1603 /* That will change things, so, we should relax again.
1604 Note that this is not required, and it may be slow. */
1609 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1611 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1613 bfd_vma value = symval;
1615 /* If we've got a "call" instruction that needs to be turned
1616 into a "calls" instruction, do so now. It saves a byte. */
1617 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1621 /* Get the opcode. */
1622 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1624 /* Make sure we're working with a "call" instruction! */
1627 /* Note that we've changed the relocs, section contents,
1629 elf_section_data (sec)->relocs = internal_relocs;
1632 elf_section_data (sec)->this_hdr.contents = contents;
1633 free_contents = NULL;
1635 symtab_hdr->contents = (bfd_byte *) extsyms;
1636 free_extsyms = NULL;
1638 /* Fix the opcode. */
1639 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1640 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1642 /* Fix irel->r_offset and irel->r_addend. */
1643 irel->r_offset += 1;
1644 irel->r_addend += 1;
1646 /* Delete one byte of data. */
1647 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1648 irel->r_offset + 1, 1))
1651 /* That will change things, so, we should relax again.
1652 Note that this is not required, and it may be slow. */
1660 /* Get the opcode. */
1661 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1663 /* Insert data from the target function into the "call"
1664 instruction if needed. */
1667 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1668 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1669 contents + irel->r_offset + 3);
1673 /* Deal with pc-relative gunk. */
1674 value -= (sec->output_section->vma + sec->output_offset);
1675 value -= irel->r_offset;
1676 value += irel->r_addend;
1678 /* See if the value will fit in 8 bits, note the high value is
1679 0x7f + 1 as the target will be one bytes closer if we are
1681 if ((long)value < 0x80 && (long)value > -0x80)
1685 /* Get the opcode. */
1686 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1691 /* Note that we've changed the relocs, section contents, etc. */
1692 elf_section_data (sec)->relocs = internal_relocs;
1695 elf_section_data (sec)->this_hdr.contents = contents;
1696 free_contents = NULL;
1698 symtab_hdr->contents = (bfd_byte *) extsyms;
1699 free_extsyms = NULL;
1701 /* Fix the opcode. */
1702 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1704 /* Fix the relocation's type. */
1705 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1708 /* Delete one byte of data. */
1709 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1710 irel->r_offset + 1, 1))
1713 /* That will change things, so, we should relax again.
1714 Note that this is not required, and it may be slow. */
1719 /* Try to eliminate an unconditional 8 bit pc-relative branch
1720 which immediately follows a conditional 8 bit pc-relative
1721 branch around the unconditional branch.
1729 This happens when the bCC can't reach lab2 at assembly time,
1730 but due to other relaxations it can reach at link time. */
1731 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1733 Elf_Internal_Rela *nrel;
1734 bfd_vma value = symval;
1737 /* Deal with pc-relative gunk. */
1738 value -= (sec->output_section->vma + sec->output_offset);
1739 value -= irel->r_offset;
1740 value += irel->r_addend;
1742 /* Do nothing if this reloc is the last byte in the section. */
1743 if (irel->r_offset == sec->_cooked_size)
1746 /* See if the next instruction is an unconditional pc-relative
1747 branch, more often than not this test will fail, so we
1748 test it first to speed things up. */
1749 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1753 /* Also make sure the next relocation applies to the next
1754 instruction and that it's a pc-relative 8 bit branch. */
1757 || irel->r_offset + 2 != nrel->r_offset
1758 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1761 /* Make sure our destination immediately follows the
1762 unconditional branch. */
1763 if (symval != (sec->output_section->vma + sec->output_offset
1764 + irel->r_offset + 3))
1767 /* Now make sure we are a conditional branch. This may not
1768 be necessary, but why take the chance.
1770 Note these checks assume that R_MN10300_PCREL8 relocs
1771 only occur on bCC and bCCx insns. If they occured
1772 elsewhere, we'd need to know the start of this insn
1773 for this check to be accurate. */
1774 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1775 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1776 && code != 0xc3 && code != 0xc4 && code != 0xc5
1777 && code != 0xc6 && code != 0xc7 && code != 0xc8
1778 && code != 0xc9 && code != 0xe8 && code != 0xe9
1779 && code != 0xea && code != 0xeb)
1782 /* We also have to be sure there is no symbol/label
1783 at the unconditional branch. */
1784 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1785 irel->r_offset + 1))
1788 /* Note that we've changed the relocs, section contents, etc. */
1789 elf_section_data (sec)->relocs = internal_relocs;
1792 elf_section_data (sec)->this_hdr.contents = contents;
1793 free_contents = NULL;
1795 symtab_hdr->contents = (bfd_byte *) extsyms;
1796 free_extsyms = NULL;
1798 /* Reverse the condition of the first branch. */
1844 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1846 /* Set the reloc type and symbol for the first branch
1847 from the second branch. */
1848 irel->r_info = nrel->r_info;
1850 /* Make the reloc for the second branch a null reloc. */
1851 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1854 /* Delete two bytes of data. */
1855 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1856 irel->r_offset + 1, 2))
1859 /* That will change things, so, we should relax again.
1860 Note that this is not required, and it may be slow. */
1864 /* Try to turn a 32bit immediate, displacement or absolute address
1865 into a 16bit immediate, displacement or absolute address. */
1866 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1868 bfd_vma value = symval;
1869 value += irel->r_addend;
1871 /* See if the value will fit in 16 bits.
1872 We allow any 16bit match here. We prune those we can't
1874 if ((long)value < 0x7fff && (long)value > -0x8000)
1878 /* Most insns which have 32bit operands are 6 bytes long;
1879 exceptions are pcrel insns and bit insns.
1881 We handle pcrel insns above. We don't bother trying
1882 to handle the bit insns here.
1884 The first byte of the remaining insns will be 0xfc. */
1886 /* Get the first opcode. */
1887 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1892 /* Get the second opcode. */
1893 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1895 if ((code & 0xf0) < 0x80)
1896 switch (code & 0xf0)
1898 /* mov (d32,am),dn -> mov (d32,am),dn
1899 mov dm,(d32,am) -> mov dn,(d32,am)
1900 mov (d32,am),an -> mov (d32,am),an
1901 mov dm,(d32,am) -> mov dn,(d32,am)
1902 movbu (d32,am),dn -> movbu (d32,am),dn
1903 movbu dm,(d32,am) -> movbu dn,(d32,am)
1904 movhu (d32,am),dn -> movhu (d32,am),dn
1905 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1914 /* Not safe if the high bit is on as relaxing may
1915 move the value out of high mem and thus not fit
1916 in a signed 16bit value. */
1918 && (value & 0x8000))
1921 /* Note that we've changed the relocation contents, etc. */
1922 elf_section_data (sec)->relocs = internal_relocs;
1925 elf_section_data (sec)->this_hdr.contents = contents;
1926 free_contents = NULL;
1928 symtab_hdr->contents = (bfd_byte *) extsyms;
1929 free_extsyms = NULL;
1931 /* Fix the opcode. */
1932 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1933 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1935 /* Fix the relocation's type. */
1936 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1939 /* Delete two bytes of data. */
1940 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1941 irel->r_offset + 2, 2))
1944 /* That will change things, so, we should relax again.
1945 Note that this is not required, and it may be slow. */
1949 else if ((code & 0xf0) == 0x80
1950 || (code & 0xf0) == 0x90)
1951 switch (code & 0xf3)
1953 /* mov dn,(abs32) -> mov dn,(abs16)
1954 movbu dn,(abs32) -> movbu dn,(abs16)
1955 movhu dn,(abs32) -> movhu dn,(abs16) */
1959 /* Note that we've changed the relocation contents, etc. */
1960 elf_section_data (sec)->relocs = internal_relocs;
1963 elf_section_data (sec)->this_hdr.contents = contents;
1964 free_contents = NULL;
1966 symtab_hdr->contents = (bfd_byte *) extsyms;
1967 free_extsyms = NULL;
1969 if ((code & 0xf3) == 0x81)
1970 code = 0x01 + (code & 0x0c);
1971 else if ((code & 0xf3) == 0x82)
1972 code = 0x02 + (code & 0x0c);
1973 else if ((code & 0xf3) == 0x83)
1974 code = 0x03 + (code & 0x0c);
1978 /* Fix the opcode. */
1979 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1981 /* Fix the relocation's type. */
1982 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1985 /* The opcode got shorter too, so we have to fix the
1986 addend and offset too! */
1987 irel->r_offset -= 1;
1989 /* Delete three bytes of data. */
1990 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1991 irel->r_offset + 1, 3))
1994 /* That will change things, so, we should relax again.
1995 Note that this is not required, and it may be slow. */
1999 /* mov am,(abs32) -> mov am,(abs16)
2000 mov am,(d32,sp) -> mov am,(d16,sp)
2001 mov dm,(d32,sp) -> mov dm,(d32,sp)
2002 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2003 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2009 /* Note that we've changed the relocation contents, etc. */
2010 elf_section_data (sec)->relocs = internal_relocs;
2013 elf_section_data (sec)->this_hdr.contents = contents;
2014 free_contents = NULL;
2016 symtab_hdr->contents = (bfd_byte *) extsyms;
2017 free_extsyms = NULL;
2019 /* Fix the opcode. */
2020 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2021 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2023 /* Fix the relocation's type. */
2024 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2027 /* Delete two bytes of data. */
2028 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2029 irel->r_offset + 2, 2))
2032 /* That will change things, so, we should relax again.
2033 Note that this is not required, and it may be slow. */
2037 else if ((code & 0xf0) < 0xf0)
2038 switch (code & 0xfc)
2040 /* mov imm32,dn -> mov imm16,dn
2041 mov imm32,an -> mov imm16,an
2042 mov (abs32),dn -> mov (abs16),dn
2043 movbu (abs32),dn -> movbu (abs16),dn
2044 movhu (abs32),dn -> movhu (abs16),dn */
2050 /* Not safe if the high bit is on as relaxing may
2051 move the value out of high mem and thus not fit
2052 in a signed 16bit value. */
2054 && (value & 0x8000))
2057 /* Note that we've changed the relocation contents, etc. */
2058 elf_section_data (sec)->relocs = internal_relocs;
2061 elf_section_data (sec)->this_hdr.contents = contents;
2062 free_contents = NULL;
2064 symtab_hdr->contents = (bfd_byte *) extsyms;
2065 free_extsyms = NULL;
2067 if ((code & 0xfc) == 0xcc)
2068 code = 0x2c + (code & 0x03);
2069 else if ((code & 0xfc) == 0xdc)
2070 code = 0x24 + (code & 0x03);
2071 else if ((code & 0xfc) == 0xa4)
2072 code = 0x30 + (code & 0x03);
2073 else if ((code & 0xfc) == 0xa8)
2074 code = 0x34 + (code & 0x03);
2075 else if ((code & 0xfc) == 0xac)
2076 code = 0x38 + (code & 0x03);
2080 /* Fix the opcode. */
2081 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2083 /* Fix the relocation's type. */
2084 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2087 /* The opcode got shorter too, so we have to fix the
2088 addend and offset too! */
2089 irel->r_offset -= 1;
2091 /* Delete three bytes of data. */
2092 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2093 irel->r_offset + 1, 3))
2096 /* That will change things, so, we should relax again.
2097 Note that this is not required, and it may be slow. */
2101 /* mov (abs32),an -> mov (abs16),an
2102 mov (d32,sp),an -> mov (d32,sp),an
2103 mov (d32,sp),dn -> mov (d32,sp),dn
2104 movbu (d32,sp),dn -> movbu (d32,sp),dn
2105 movhu (d32,sp),dn -> movhu (d32,sp),dn
2106 add imm32,dn -> add imm16,dn
2107 cmp imm32,dn -> cmp imm16,dn
2108 add imm32,an -> add imm16,an
2109 cmp imm32,an -> cmp imm16,an
2110 and imm32,dn -> and imm32,dn
2111 or imm32,dn -> or imm32,dn
2112 xor imm32,dn -> xor imm32,dn
2113 btst imm32,dn -> btst imm32,dn */
2129 /* Note that we've changed the relocation contents, etc. */
2130 elf_section_data (sec)->relocs = internal_relocs;
2133 elf_section_data (sec)->this_hdr.contents = contents;
2134 free_contents = NULL;
2136 symtab_hdr->contents = (bfd_byte *) extsyms;
2137 free_extsyms = NULL;
2139 /* Fix the opcode. */
2140 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2141 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2143 /* Fix the relocation's type. */
2144 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2147 /* Delete two bytes of data. */
2148 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2149 irel->r_offset + 2, 2))
2152 /* That will change things, so, we should relax again.
2153 Note that this is not required, and it may be slow. */
2157 else if (code == 0xfe)
2159 /* add imm32,sp -> add imm16,sp */
2161 /* Note that we've changed the relocation contents, etc. */
2162 elf_section_data (sec)->relocs = internal_relocs;
2165 elf_section_data (sec)->this_hdr.contents = contents;
2166 free_contents = NULL;
2168 symtab_hdr->contents = (bfd_byte *) extsyms;
2169 free_extsyms = NULL;
2171 /* Fix the opcode. */
2172 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2173 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2175 /* Fix the relocation's type. */
2176 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2179 /* Delete two bytes of data. */
2180 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2181 irel->r_offset + 2, 2))
2184 /* That will change things, so, we should relax again.
2185 Note that this is not required, and it may be slow. */
2193 if (free_relocs != NULL)
2199 if (free_contents != NULL)
2201 if (! link_info->keep_memory)
2202 free (free_contents);
2205 /* Cache the section contents for elf_link_input_bfd. */
2206 elf_section_data (sec)->this_hdr.contents = contents;
2208 free_contents = NULL;
2211 if (free_extsyms != NULL)
2213 if (! link_info->keep_memory)
2214 free (free_extsyms);
2217 /* Cache the symbols for elf_link_input_bfd. */
2218 symtab_hdr->contents = extsyms;
2220 free_extsyms = NULL;
2226 if (free_relocs != NULL)
2228 if (free_contents != NULL)
2229 free (free_contents);
2230 if (free_extsyms != NULL)
2231 free (free_extsyms);
2235 /* Compute the stack size and movm arguments for the function
2236 referred to by HASH at address ADDR in section with
2237 contents CONTENTS, store the information in the hash table. */
2239 compute_function_info (abfd, hash, addr, contents)
2241 struct elf32_mn10300_link_hash_entry *hash;
2243 unsigned char *contents;
2245 unsigned char byte1, byte2;
2246 /* We only care about a very small subset of the possible prologue
2247 sequences here. Basically we look for:
2249 movm [d2,d3,a2,a3],sp (optional)
2250 add <size>,sp (optional, and only for sizes which fit in an unsigned
2253 If we find anything else, we quit. */
2255 /* Look for movm [regs],sp */
2256 byte1 = bfd_get_8 (abfd, contents + addr);
2257 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2261 hash->movm_args = byte2;
2263 byte1 = bfd_get_8 (abfd, contents + addr);
2264 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2267 /* Now figure out how much stack space will be allocated by the movm
2268 instruction. We need this kept separate from the funtion's normal
2270 if (hash->movm_args)
2273 if (hash->movm_args & 0x80)
2274 hash->movm_stack_size += 4;
2277 if (hash->movm_args & 0x40)
2278 hash->movm_stack_size += 4;
2281 if (hash->movm_args & 0x20)
2282 hash->movm_stack_size += 4;
2285 if (hash->movm_args & 0x10)
2286 hash->movm_stack_size += 4;
2288 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2289 if (hash->movm_args & 0x08)
2290 hash->movm_stack_size += 8 * 4;
2293 /* Now look for the two stack adjustment variants. */
2294 if (byte1 == 0xf8 && byte2 == 0xfe)
2296 int temp = bfd_get_8 (abfd, contents + addr + 2);
2297 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2299 hash->stack_size = -temp;
2301 else if (byte1 == 0xfa && byte2 == 0xfe)
2303 int temp = bfd_get_16 (abfd, contents + addr + 2);
2304 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2308 hash->stack_size = temp;
2311 /* If the total stack to be allocated by the call instruction is more
2312 than 255 bytes, then we can't remove the stack adjustment by using
2313 "call" (we might still be able to remove the "movm" instruction. */
2314 if (hash->stack_size + hash->movm_stack_size > 255)
2315 hash->stack_size = 0;
2320 /* Delete some bytes from a section while relaxing. */
2323 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2329 Elf_Internal_Shdr *symtab_hdr;
2330 Elf32_External_Sym *extsyms;
2333 Elf_Internal_Rela *irel, *irelend;
2334 Elf_Internal_Rela *irelalign;
2336 Elf32_External_Sym *esym, *esymend;
2337 struct elf32_mn10300_link_hash_entry *sym_hash;
2339 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2340 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2342 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2344 contents = elf_section_data (sec)->this_hdr.contents;
2346 /* The deletion must stop at the next ALIGN reloc for an aligment
2347 power larger than the number of bytes we are deleting. */
2350 toaddr = sec->_cooked_size;
2352 irel = elf_section_data (sec)->relocs;
2353 irelend = irel + sec->reloc_count;
2355 /* Actually delete the bytes. */
2356 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2357 sec->_cooked_size -= count;
2359 /* Adjust all the relocs. */
2360 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2362 /* Get the new reloc address. */
2363 if ((irel->r_offset > addr
2364 && irel->r_offset < toaddr))
2365 irel->r_offset -= count;
2368 /* Adjust the local symbols defined in this section. */
2370 esymend = esym + symtab_hdr->sh_info;
2371 for (; esym < esymend; esym++)
2373 Elf_Internal_Sym isym;
2375 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2377 if (isym.st_shndx == shndx
2378 && isym.st_value > addr
2379 && isym.st_value < toaddr)
2381 isym.st_value -= count;
2382 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2386 /* Now adjust the global symbols defined in this section. */
2387 esym = extsyms + symtab_hdr->sh_info;
2388 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2389 for (index = 0; esym < esymend; esym++, index++)
2391 Elf_Internal_Sym isym;
2393 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2394 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2395 (elf_sym_hashes (abfd)[index]);
2396 if (isym.st_shndx == shndx
2397 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2398 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2399 && (sym_hash)->root.root.u.def.section == sec
2400 && (sym_hash)->root.root.u.def.value > addr
2401 && (sym_hash)->root.root.u.def.value < toaddr)
2403 (sym_hash)->root.root.u.def.value -= count;
2410 /* Return true if a symbol exists at the given address, else return
2413 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2416 Elf32_External_Sym *extsyms;
2419 Elf_Internal_Shdr *symtab_hdr;
2421 Elf32_External_Sym *esym, *esymend;
2422 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2424 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2425 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2427 /* Examine all the symbols. */
2429 esymend = esym + symtab_hdr->sh_info;
2430 for (; esym < esymend; esym++)
2432 Elf_Internal_Sym isym;
2434 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2436 if (isym.st_shndx == shndx
2437 && isym.st_value == addr)
2441 sym_hash = (struct elf32_mn10300_link_hash_entry **)(elf_sym_hashes (abfd));
2442 sym_hash_end = (sym_hash
2443 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2444 - symtab_hdr->sh_info));
2445 for (; sym_hash < sym_hash_end; sym_hash++)
2447 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2448 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2449 && (*sym_hash)->root.root.u.def.section == sec
2450 && (*sym_hash)->root.root.u.def.value == addr)
2456 /* This is a version of bfd_generic_get_relocated_section_contents
2457 which uses mn10300_elf_relocate_section. */
2460 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2461 data, relocateable, symbols)
2463 struct bfd_link_info *link_info;
2464 struct bfd_link_order *link_order;
2466 boolean relocateable;
2469 Elf_Internal_Shdr *symtab_hdr;
2470 asection *input_section = link_order->u.indirect.section;
2471 bfd *input_bfd = input_section->owner;
2472 asection **sections = NULL;
2473 Elf_Internal_Rela *internal_relocs = NULL;
2474 Elf32_External_Sym *external_syms = NULL;
2475 Elf_Internal_Sym *internal_syms = NULL;
2477 /* We only need to handle the case of relaxing, or of having a
2478 particular set of section contents, specially. */
2480 || elf_section_data (input_section)->this_hdr.contents == NULL)
2481 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2486 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2488 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2489 input_section->_raw_size);
2491 if ((input_section->flags & SEC_RELOC) != 0
2492 && input_section->reloc_count > 0)
2494 Elf_Internal_Sym *isymp;
2496 Elf32_External_Sym *esym, *esymend;
2498 if (symtab_hdr->contents != NULL)
2499 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2502 external_syms = ((Elf32_External_Sym *)
2503 bfd_malloc (symtab_hdr->sh_info
2504 * sizeof (Elf32_External_Sym)));
2505 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2507 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2508 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2509 symtab_hdr->sh_info, input_bfd)
2510 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2514 internal_relocs = (_bfd_elf32_link_read_relocs
2515 (input_bfd, input_section, (PTR) NULL,
2516 (Elf_Internal_Rela *) NULL, false));
2517 if (internal_relocs == NULL)
2520 internal_syms = ((Elf_Internal_Sym *)
2521 bfd_malloc (symtab_hdr->sh_info
2522 * sizeof (Elf_Internal_Sym)));
2523 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2526 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2527 * sizeof (asection *));
2528 if (sections == NULL && symtab_hdr->sh_info > 0)
2531 isymp = internal_syms;
2533 esym = external_syms;
2534 esymend = esym + symtab_hdr->sh_info;
2535 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2539 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2541 if (isymp->st_shndx == SHN_UNDEF)
2542 isec = bfd_und_section_ptr;
2543 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2544 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2545 else if (isymp->st_shndx == SHN_ABS)
2546 isec = bfd_abs_section_ptr;
2547 else if (isymp->st_shndx == SHN_COMMON)
2548 isec = bfd_com_section_ptr;
2558 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2559 input_section, data, internal_relocs,
2560 internal_syms, sections))
2563 if (sections != NULL)
2566 if (internal_syms != NULL)
2567 free (internal_syms);
2568 internal_syms = NULL;
2569 if (external_syms != NULL && symtab_hdr->contents == NULL)
2570 free (external_syms);
2571 external_syms = NULL;
2572 if (internal_relocs != elf_section_data (input_section)->relocs)
2573 free (internal_relocs);
2574 internal_relocs = NULL;
2580 if (internal_relocs != NULL
2581 && internal_relocs != elf_section_data (input_section)->relocs)
2582 free (internal_relocs);
2583 if (external_syms != NULL && symtab_hdr->contents == NULL)
2584 free (external_syms);
2585 if (internal_syms != NULL)
2586 free (internal_syms);
2587 if (sections != NULL)
2592 /* Assorted hash table functions. */
2594 /* Initialize an entry in the link hash table. */
2596 /* Create an entry in an MN10300 ELF linker hash table. */
2598 static struct bfd_hash_entry *
2599 elf32_mn10300_link_hash_newfunc (entry, table, string)
2600 struct bfd_hash_entry *entry;
2601 struct bfd_hash_table *table;
2604 struct elf32_mn10300_link_hash_entry *ret =
2605 (struct elf32_mn10300_link_hash_entry *) entry;
2607 /* Allocate the structure if it has not already been allocated by a
2609 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2610 ret = ((struct elf32_mn10300_link_hash_entry *)
2611 bfd_hash_allocate (table,
2612 sizeof (struct elf32_mn10300_link_hash_entry)));
2613 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2614 return (struct bfd_hash_entry *) ret;
2616 /* Call the allocation method of the superclass. */
2617 ret = ((struct elf32_mn10300_link_hash_entry *)
2618 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2620 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2622 ret->direct_calls = 0;
2623 ret->stack_size = 0;
2624 ret->movm_stack_size = 0;
2629 return (struct bfd_hash_entry *) ret;
2632 /* Create an mn10300 ELF linker hash table. */
2634 static struct bfd_link_hash_table *
2635 elf32_mn10300_link_hash_table_create (abfd)
2638 struct elf32_mn10300_link_hash_table *ret;
2640 ret = ((struct elf32_mn10300_link_hash_table *)
2641 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2642 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2645 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2646 elf32_mn10300_link_hash_newfunc))
2648 bfd_release (abfd, ret);
2653 ret->static_hash_table
2654 = ((struct elf32_mn10300_link_hash_table *)
2655 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2656 if (ret->static_hash_table == NULL)
2658 bfd_release (abfd, ret);
2662 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2663 elf32_mn10300_link_hash_newfunc))
2665 bfd_release (abfd, ret->static_hash_table);
2666 bfd_release (abfd, ret);
2669 return &ret->root.root;
2673 elf_mn10300_mach (flags)
2676 switch (flags & EF_MN10300_MACH)
2678 case E_MN10300_MACH_MN10300:
2680 return bfd_mach_mn10300;
2682 /* start-sanitize-am33 */
2683 case E_MN10300_MACH_AM33:
2684 return bfd_mach_am33;
2685 /* end-sanitize-am33 */
2689 /* The final processing done just before writing out a MN10300 ELF object
2690 file. This gets the MN10300 architecture right based on the machine
2695 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2701 Elf_Internal_Shdr **hdrpp;
2705 switch (bfd_get_mach (abfd))
2708 case bfd_mach_mn10300:
2709 val = E_MN10300_MACH_MN10300;
2712 /* start-sanitize-am33 */
2714 val = E_MN10300_MACH_AM33;
2716 /* end-sanitize-am33 */
2719 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2720 elf_elfheader (abfd)->e_flags |= val;
2724 _bfd_mn10300_elf_object_p (abfd)
2727 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2728 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2732 /* Merge backend specific data from an object file to the output
2733 object file when linking. */
2736 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2740 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2741 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2744 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2745 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2747 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2748 bfd_get_mach (ibfd)))
2756 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2757 #define TARGET_LITTLE_NAME "elf32-mn10300"
2758 #define ELF_ARCH bfd_arch_mn10300
2759 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2760 #define ELF_MAXPAGESIZE 0x1000
2762 #define elf_info_to_howto mn10300_info_to_howto
2763 #define elf_info_to_howto_rel 0
2764 #define elf_backend_can_gc_sections 1
2765 #define elf_backend_check_relocs mn10300_elf_check_relocs
2766 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2767 #define elf_backend_relocate_section mn10300_elf_relocate_section
2768 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2769 #define bfd_elf32_bfd_get_relocated_section_contents \
2770 mn10300_elf_get_relocated_section_contents
2771 #define bfd_elf32_bfd_link_hash_table_create \
2772 elf32_mn10300_link_hash_table_create
2774 #define elf_symbol_leading_char '_'
2776 /* So we can set bits in e_flags. */
2777 #define elf_backend_final_write_processing \
2778 _bfd_mn10300_elf_final_write_processing
2779 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2781 #define bfd_elf32_bfd_merge_private_bfd_data \
2782 _bfd_mn10300_elf_merge_private_bfd_data
2785 #include "elf32-target.h"