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. */
38 unsigned char stack_size;
40 /* For function symbols, arguments (if any) for movm instruction
41 in the prologue. We stuff this value into "call" instructions
42 to the target when it's valid and profitable to do so. */
43 unsigned char movm_args;
45 /* When set, convert all "call" instructions to this target into "calls"
47 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
49 /* Used to mark functions which have had redundant parts of their
51 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
55 /* We derive a hash table from the main elf linker hash table so
56 we can store state variables and a secondary hash table without
57 resorting to global variables. */
58 struct elf32_mn10300_link_hash_table
60 /* The main hash table. */
61 struct elf_link_hash_table root;
63 /* A hash table for static functions. We could derive a new hash table
64 instead of using the full elf32_mn10300_link_hash_table if we wanted
65 to save some memory. */
66 struct elf32_mn10300_link_hash_table *static_hash_table;
68 /* Random linker state flags. */
69 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
73 /* For MN10300 linker hash table. */
75 /* Get the MN10300 ELF linker hash table from a link_info structure. */
77 #define elf32_mn10300_hash_table(p) \
78 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
80 #define elf32_mn10300_link_hash_traverse(table, func, info) \
81 (elf_link_hash_traverse \
83 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
86 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
87 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
88 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
91 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
92 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
93 static void mn10300_info_to_howto
94 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
95 static boolean mn10300_elf_check_relocs
96 PARAMS ((bfd *, struct bfd_link_info *, asection *,
97 const Elf_Internal_Rela *));
98 static asection *mn10300_elf_gc_mark_hook
99 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
100 struct elf_link_hash_entry *, Elf_Internal_Sym *));
101 static boolean mn10300_elf_relax_delete_bytes
102 PARAMS ((bfd *, asection *, bfd_vma, int));
103 static boolean mn10300_elf_symbol_address_p
104 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
105 static boolean elf32_mn10300_finish_hash_table_entry
106 PARAMS ((struct bfd_hash_entry *, PTR));
107 static void compute_function_info
108 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
109 bfd_vma, unsigned char *));
111 /* We have to use RELA instructions since md_apply_fix3 in the assembler
112 does absolutely nothing. */
116 static reloc_howto_type elf_mn10300_howto_table[] =
118 /* Dummy relocation. Does nothing. */
119 HOWTO (R_MN10300_NONE,
125 complain_overflow_bitfield,
126 bfd_elf_generic_reloc,
132 /* Standard 32 bit reloc. */
139 complain_overflow_bitfield,
140 bfd_elf_generic_reloc,
146 /* Standard 16 bit reloc. */
153 complain_overflow_bitfield,
154 bfd_elf_generic_reloc,
160 /* Standard 8 bit reloc. */
167 complain_overflow_bitfield,
168 bfd_elf_generic_reloc,
174 /* Standard 32bit pc-relative reloc. */
175 HOWTO (R_MN10300_PCREL32,
181 complain_overflow_bitfield,
182 bfd_elf_generic_reloc,
188 /* Standard 16bit pc-relative reloc. */
189 HOWTO (R_MN10300_PCREL16,
195 complain_overflow_bitfield,
196 bfd_elf_generic_reloc,
202 /* Standard 8 pc-relative reloc. */
203 HOWTO (R_MN10300_PCREL8,
209 complain_overflow_bitfield,
210 bfd_elf_generic_reloc,
217 /* GNU extension to record C++ vtable hierarchy */
218 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
220 0, /* size (0 = byte, 1 = short, 2 = long) */
222 false, /* pc_relative */
224 complain_overflow_dont, /* complain_on_overflow */
225 NULL, /* special_function */
226 "R_MN10300_GNU_VTINHERIT", /* name */
227 false, /* partial_inplace */
230 false), /* pcrel_offset */
232 /* GNU extension to record C++ vtable member usage */
233 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
235 0, /* size (0 = byte, 1 = short, 2 = long) */
237 false, /* pc_relative */
239 complain_overflow_dont, /* complain_on_overflow */
240 NULL, /* special_function */
241 "R_MN10300_GNU_VTENTRY", /* name */
242 false, /* partial_inplace */
245 false), /* pcrel_offset */
248 struct mn10300_reloc_map
250 unsigned char bfd_reloc_val;
251 unsigned char elf_reloc_val;
254 static const struct mn10300_reloc_map mn10300_reloc_map[] =
256 { BFD_RELOC_NONE, R_MN10300_NONE, },
257 { BFD_RELOC_32, R_MN10300_32, },
258 { BFD_RELOC_16, R_MN10300_16, },
259 { BFD_RELOC_8, R_MN10300_8, },
260 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
261 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
262 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
263 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
264 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
267 static reloc_howto_type *
268 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
270 bfd_reloc_code_real_type code;
275 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
278 if (mn10300_reloc_map[i].bfd_reloc_val == code)
279 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
285 /* Set the howto pointer for an MN10300 ELF reloc. */
288 mn10300_info_to_howto (abfd, cache_ptr, dst)
291 Elf32_Internal_Rela *dst;
295 r_type = ELF32_R_TYPE (dst->r_info);
296 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
297 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
300 /* Look through the relocs for a section during the first phase.
301 Since we don't do .gots or .plts, we just need to consider the
302 virtual table relocs for gc. */
305 mn10300_elf_check_relocs (abfd, info, sec, relocs)
307 struct bfd_link_info *info;
309 const Elf_Internal_Rela *relocs;
311 Elf_Internal_Shdr *symtab_hdr;
312 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
313 const Elf_Internal_Rela *rel;
314 const Elf_Internal_Rela *rel_end;
316 if (info->relocateable)
319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
320 sym_hashes = elf_sym_hashes (abfd);
321 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
322 if (!elf_bad_symtab (abfd))
323 sym_hashes_end -= symtab_hdr->sh_info;
325 rel_end = relocs + sec->reloc_count;
326 for (rel = relocs; rel < rel_end; rel++)
328 struct elf_link_hash_entry *h;
329 unsigned long r_symndx;
331 r_symndx = ELF32_R_SYM (rel->r_info);
332 if (r_symndx < symtab_hdr->sh_info)
335 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
337 switch (ELF32_R_TYPE (rel->r_info))
339 /* This relocation describes the C++ object vtable hierarchy.
340 Reconstruct it for later use during GC. */
341 case R_MN10300_GNU_VTINHERIT:
342 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
346 /* This relocation describes which C++ vtable entries are actually
347 used. Record for later use during GC. */
348 case R_MN10300_GNU_VTENTRY:
349 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
358 /* Return the section that should be marked against GC for a given
362 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
364 struct bfd_link_info *info;
365 Elf_Internal_Rela *rel;
366 struct elf_link_hash_entry *h;
367 Elf_Internal_Sym *sym;
371 switch (ELF32_R_TYPE (rel->r_info))
373 case R_MN10300_GNU_VTINHERIT:
374 case R_MN10300_GNU_VTENTRY:
378 switch (h->root.type)
380 case bfd_link_hash_defined:
381 case bfd_link_hash_defweak:
382 return h->root.u.def.section;
384 case bfd_link_hash_common:
385 return h->root.u.c.p->section;
391 if (!(elf_bad_symtab (abfd)
392 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
393 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
394 && sym->st_shndx != SHN_COMMON))
396 return bfd_section_from_elf_index (abfd, sym->st_shndx);
403 /* Perform a relocation as part of a final link. */
404 static bfd_reloc_status_type
405 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
406 input_section, contents, offset, value,
407 addend, info, sym_sec, is_local)
408 reloc_howto_type *howto;
411 asection *input_section;
416 struct bfd_link_info *info;
420 unsigned long r_type = howto->type;
421 bfd_byte *hit_data = contents + offset;
430 bfd_put_32 (input_bfd, value, hit_data);
436 if ((long)value > 0x7fff || (long)value < -0x8000)
437 return bfd_reloc_overflow;
439 bfd_put_16 (input_bfd, value, hit_data);
445 if ((long)value > 0x7f || (long)value < -0x80)
446 return bfd_reloc_overflow;
448 bfd_put_8 (input_bfd, value, hit_data);
451 case R_MN10300_PCREL8:
452 value -= (input_section->output_section->vma
453 + input_section->output_offset);
457 if ((long)value > 0xff || (long)value < -0x100)
458 return bfd_reloc_overflow;
460 bfd_put_8 (input_bfd, value, hit_data);
463 case R_MN10300_PCREL16:
464 value -= (input_section->output_section->vma
465 + input_section->output_offset);
469 if ((long)value > 0xffff || (long)value < -0x10000)
470 return bfd_reloc_overflow;
472 bfd_put_16 (input_bfd, value, hit_data);
475 case R_MN10300_PCREL32:
476 value -= (input_section->output_section->vma
477 + input_section->output_offset);
481 bfd_put_32 (input_bfd, value, hit_data);
484 case R_MN10300_GNU_VTINHERIT:
485 case R_MN10300_GNU_VTENTRY:
489 return bfd_reloc_notsupported;
494 /* Relocate an MN10300 ELF section. */
496 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
497 contents, relocs, local_syms, local_sections)
499 struct bfd_link_info *info;
501 asection *input_section;
503 Elf_Internal_Rela *relocs;
504 Elf_Internal_Sym *local_syms;
505 asection **local_sections;
507 Elf_Internal_Shdr *symtab_hdr;
508 struct elf32_mn10300_link_hash_entry **sym_hashes;
509 Elf_Internal_Rela *rel, *relend;
511 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
512 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
513 (elf_sym_hashes (input_bfd));
516 relend = relocs + input_section->reloc_count;
517 for (; rel < relend; rel++)
520 reloc_howto_type *howto;
521 unsigned long r_symndx;
522 Elf_Internal_Sym *sym;
524 struct elf32_mn10300_link_hash_entry *h;
526 bfd_reloc_status_type r;
528 r_symndx = ELF32_R_SYM (rel->r_info);
529 r_type = ELF32_R_TYPE (rel->r_info);
530 howto = elf_mn10300_howto_table + r_type;
532 /* Just skip the vtable gc relocs. */
533 if (r_type == R_MN10300_GNU_VTINHERIT
534 || r_type == R_MN10300_GNU_VTENTRY)
537 if (info->relocateable)
539 /* This is a relocateable link. We don't have to change
540 anything, unless the reloc is against a section symbol,
541 in which case we have to adjust according to where the
542 section symbol winds up in the output section. */
543 if (r_symndx < symtab_hdr->sh_info)
545 sym = local_syms + r_symndx;
546 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
548 sec = local_sections[r_symndx];
549 rel->r_addend += sec->output_offset + sym->st_value;
556 /* This is a final link. */
560 if (r_symndx < symtab_hdr->sh_info)
562 sym = local_syms + r_symndx;
563 sec = local_sections[r_symndx];
564 relocation = (sec->output_section->vma
570 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
571 while (h->root.type == bfd_link_hash_indirect
572 || h->root.type == bfd_link_hash_warning)
573 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
574 if (h->root.root.type == bfd_link_hash_defined
575 || h->root.root.type == bfd_link_hash_defweak)
577 sec = h->root.root.u.def.section;
578 relocation = (h->root.root.u.def.value
579 + sec->output_section->vma
580 + sec->output_offset);
582 else if (h->root.root.type == bfd_link_hash_undefweak)
586 if (! ((*info->callbacks->undefined_symbol)
587 (info, h->root.root.root.string, input_bfd,
588 input_section, rel->r_offset)))
594 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
596 contents, rel->r_offset,
597 relocation, rel->r_addend,
598 info, sec, h == NULL);
600 if (r != bfd_reloc_ok)
603 const char *msg = (const char *)0;
606 name = h->root.root.root.string;
609 name = (bfd_elf_string_from_elf_section
610 (input_bfd, symtab_hdr->sh_link, sym->st_name));
611 if (name == NULL || *name == '\0')
612 name = bfd_section_name (input_bfd, sec);
617 case bfd_reloc_overflow:
618 if (! ((*info->callbacks->reloc_overflow)
619 (info, name, howto->name, (bfd_vma) 0,
620 input_bfd, input_section, rel->r_offset)))
624 case bfd_reloc_undefined:
625 if (! ((*info->callbacks->undefined_symbol)
626 (info, name, input_bfd, input_section,
631 case bfd_reloc_outofrange:
632 msg = _("internal error: out of range error");
635 case bfd_reloc_notsupported:
636 msg = _("internal error: unsupported relocation error");
639 case bfd_reloc_dangerous:
640 msg = _("internal error: dangerous error");
644 msg = _("internal error: unknown error");
648 if (!((*info->callbacks->warning)
649 (info, msg, name, input_bfd, input_section,
660 /* Finish initializing one hash table entry. */
662 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
663 struct bfd_hash_entry *gen_entry;
666 struct elf32_mn10300_link_hash_entry *entry;
667 unsigned int byte_count = 0;
669 entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
671 /* If we already know we want to convert "call" to "calls" for calls
672 to this symbol, then return now. */
673 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
676 /* If there are no named calls to this symbol, or there's nothing we
677 can move from the function itself into the "call" instruction, then
678 note that all "call" instructions should be converted into "calls"
679 instructions and return. */
680 if (entry->direct_calls == 0
681 || (entry->stack_size == 0 && entry->movm_args == 0))
683 /* Make a note that we should convert "call" instructions to "calls"
684 instructions for calls to this symbol. */
685 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
689 /* We may be able to move some instructions from the function itself into
690 the "call" instruction. Count how many bytes we might be able to
691 eliminate in the function itself. */
693 /* A movm instruction is two bytes. */
694 if (entry->movm_args)
697 /* Count the insn to allocate stack space too. */
698 if (entry->stack_size > 0 && entry->stack_size <= 128)
700 else if (entry->stack_size > 0 && entry->stack_size < 256)
703 /* If using "call" will result in larger code, then turn all
704 the associated "call" instructions into "calls" instrutions. */
705 if (byte_count < entry->direct_calls)
706 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
708 /* This routine never fails. */
712 /* This function handles relaxing for the mn10300.
714 There's quite a few relaxing opportunites available on the mn10300:
716 * calls:32 -> calls:16 2 bytes
717 * call:32 -> call:16 2 bytes
719 * call:32 -> calls:32 1 byte
720 * call:16 -> calls:16 1 byte
721 * These are done anytime using "calls" would result
722 in smaller code, or when necessary to preserve the
723 meaning of the program.
727 * In some circumstances we can move instructions
728 from a function prologue into a "call" instruction.
729 This is only done if the resulting code is no larger
730 than the original code.
733 * jmp:32 -> jmp:16 2 bytes
734 * jmp:16 -> bra:8 1 byte
736 * If the previous instruction is a conditional branch
737 around the jump/bra, we may be able to reverse its condition
738 and change its target to the jump's target. The jump/bra
739 can then be deleted. 2 bytes
741 * mov abs32 -> mov abs16 1 or 2 bytes
743 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
744 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
746 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
747 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
749 We don't handle imm16->imm8 or d16->d8 as they're very rare
750 and somewhat more difficult to support. */
753 mn10300_elf_relax_section (abfd, sec, link_info, again)
756 struct bfd_link_info *link_info;
759 Elf_Internal_Shdr *symtab_hdr;
760 Elf_Internal_Rela *internal_relocs = NULL;
761 Elf_Internal_Rela *free_relocs = NULL;
762 Elf_Internal_Rela *irel, *irelend;
763 bfd_byte *contents = NULL;
764 bfd_byte *free_contents = NULL;
765 Elf32_External_Sym *extsyms = NULL;
766 Elf32_External_Sym *free_extsyms = NULL;
767 struct elf32_mn10300_link_hash_table *hash_table;
769 /* Assume nothing changes. */
772 /* We need a pointer to the mn10300 specific hash table. */
773 hash_table = elf32_mn10300_hash_table (link_info);
775 /* Initialize fields in each hash table entry the first time through. */
776 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
780 /* Iterate over all the input bfds. */
781 for (input_bfd = link_info->input_bfds;
783 input_bfd = input_bfd->link_next)
787 /* We're going to need all the symbols for each bfd. */
788 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
790 /* Get cached copy if it exists. */
791 if (symtab_hdr->contents != NULL)
792 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
795 /* Go get them off disk. */
796 extsyms = ((Elf32_External_Sym *)
797 bfd_malloc (symtab_hdr->sh_size));
800 free_extsyms = extsyms;
801 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
802 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
803 != symtab_hdr->sh_size))
807 /* Iterate over each section in this bfd. */
808 for (section = input_bfd->sections;
810 section = section->next)
812 struct elf32_mn10300_link_hash_entry *hash;
813 Elf_Internal_Sym *sym;
815 const char *sym_name;
817 Elf_Internal_Shdr *hdr;
819 /* Get cached copy of section contents if it exists. */
820 if (elf_section_data (section)->this_hdr.contents != NULL)
821 contents = elf_section_data (section)->this_hdr.contents;
822 else if (section->_raw_size != 0)
824 /* Go get them off disk. */
825 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
826 if (contents == NULL)
828 free_contents = contents;
830 if (!bfd_get_section_contents (input_bfd, section,
831 contents, (file_ptr) 0,
838 free_contents = NULL;
841 /* If there aren't any relocs, then there's nothing to do. */
842 if ((section->flags & SEC_RELOC) != 0
843 && section->reloc_count != 0)
846 /* Get a copy of the native relocations. */
847 internal_relocs = (_bfd_elf32_link_read_relocs
848 (input_bfd, section, (PTR) NULL,
849 (Elf_Internal_Rela *) NULL,
850 link_info->keep_memory));
851 if (internal_relocs == NULL)
853 if (! link_info->keep_memory)
854 free_relocs = internal_relocs;
856 /* Now examine each relocation. */
857 irel = internal_relocs;
858 irelend = irel + section->reloc_count;
859 for (; irel < irelend; irel++)
862 unsigned long r_index;
865 r_type = ELF32_R_TYPE (irel->r_info);
866 r_index = ELF32_R_SYM (irel->r_info);
868 if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
871 /* We need the name and hash table entry of the target
877 if (r_index < symtab_hdr->sh_info)
879 /* A local symbol. */
880 Elf_Internal_Sym isym;
882 bfd_elf32_swap_symbol_in (input_bfd,
883 extsyms + r_index, &isym);
885 if (isym.st_shndx == SHN_UNDEF)
886 sym_sec = bfd_und_section_ptr;
887 else if (isym.st_shndx > 0
888 && isym.st_shndx < SHN_LORESERVE)
890 = bfd_section_from_elf_index (input_bfd,
892 else if (isym.st_shndx == SHN_ABS)
893 sym_sec = bfd_abs_section_ptr;
894 else if (isym.st_shndx == SHN_COMMON)
895 sym_sec = bfd_com_section_ptr;
897 sym_name = bfd_elf_string_from_elf_section (input_bfd,
901 /* If it isn't a function, then we don't care
903 if (r_index < symtab_hdr->sh_info
904 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
907 /* Tack on an ID so we can uniquely identify this
908 local symbol in the global hash table. */
909 new_name = alloca (strlen (sym_name) + 10);
910 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
913 hash = (struct elf32_mn10300_link_hash_entry *)
914 elf_link_hash_lookup (&hash_table->static_hash_table->root,
920 r_index -= symtab_hdr->sh_info;
921 hash = (struct elf32_mn10300_link_hash_entry *)
922 elf_sym_hashes (input_bfd)[r_index];
925 /* If this is not a "call" instruction, then we
926 should convert "call" instructions to "calls"
928 code = bfd_get_8 (input_bfd,
929 contents + irel->r_offset - 1);
930 if (code != 0xdd && code != 0xcd)
931 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
933 /* If this is a jump/call, then bump the direct_calls
934 counter. Else force "call" to "calls" conversions. */
935 if (r_type == R_MN10300_PCREL32
936 || r_type == R_MN10300_PCREL16)
937 hash->direct_calls++;
939 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
943 /* Now look at the actual contents to get the stack size,
944 and a list of what registers were saved in the prologue
946 if ((section->flags & SEC_CODE) != 0)
949 Elf32_External_Sym *esym, *esymend;
952 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
956 /* Look at each function defined in this section and
957 update info for that function. */
959 esymend = esym + symtab_hdr->sh_info;
960 for (; esym < esymend; esym++)
962 Elf_Internal_Sym isym;
964 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
965 if (isym.st_shndx == shndx
966 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
968 if (isym.st_shndx == SHN_UNDEF)
969 sym_sec = bfd_und_section_ptr;
970 else if (isym.st_shndx > 0
971 && isym.st_shndx < SHN_LORESERVE)
973 = bfd_section_from_elf_index (input_bfd,
975 else if (isym.st_shndx == SHN_ABS)
976 sym_sec = bfd_abs_section_ptr;
977 else if (isym.st_shndx == SHN_COMMON)
978 sym_sec = bfd_com_section_ptr;
980 sym_name = bfd_elf_string_from_elf_section (input_bfd,
984 /* Tack on an ID so we can uniquely identify this
985 local symbol in the global hash table. */
986 new_name = alloca (strlen (sym_name) + 10);
987 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
990 hash = (struct elf32_mn10300_link_hash_entry *)
991 elf_link_hash_lookup (&hash_table->static_hash_table->root,
994 compute_function_info (input_bfd, hash,
995 isym.st_value, contents);
999 esym = extsyms + symtab_hdr->sh_info;
1000 esymend = extsyms + (symtab_hdr->sh_size
1001 / sizeof (Elf32_External_Sym));
1002 for (idx = 0; esym < esymend; esym++, idx++)
1004 Elf_Internal_Sym isym;
1006 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1007 hash = (struct elf32_mn10300_link_hash_entry *)
1008 elf_sym_hashes (input_bfd)[idx];
1009 if (isym.st_shndx == shndx
1010 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1011 && (hash)->root.root.u.def.section == section
1012 && ((hash)->root.root.type == bfd_link_hash_defined
1013 || (hash)->root.root.type == bfd_link_hash_defweak))
1014 compute_function_info (input_bfd, hash,
1015 (hash)->root.root.u.def.value,
1020 /* Cache or free any memory we allocated for the relocs. */
1021 if (free_relocs != NULL)
1027 /* Cache or free any memory we allocated for the contents. */
1028 if (free_contents != NULL)
1030 if (! link_info->keep_memory)
1031 free (free_contents);
1034 /* Cache the section contents for elf_link_input_bfd. */
1035 elf_section_data (section)->this_hdr.contents = contents;
1037 free_contents = NULL;
1041 /* Cache or free any memory we allocated for the symbols. */
1042 if (free_extsyms != NULL)
1044 if (! link_info->keep_memory)
1045 free (free_extsyms);
1048 /* Cache the symbols for elf_link_input_bfd. */
1049 symtab_hdr->contents = extsyms;
1051 free_extsyms = NULL;
1055 /* Now iterate on each symbol in the hash table and perform
1056 the final initialization steps on each. */
1057 elf32_mn10300_link_hash_traverse (hash_table,
1058 elf32_mn10300_finish_hash_table_entry,
1060 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1061 elf32_mn10300_finish_hash_table_entry,
1064 /* All entries in the hash table are fully initialized. */
1065 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1067 /* Now that everything has been initialized, go through each
1068 code section and delete any prologue insns which will be
1069 redundant because their operations will be performed by
1070 a "call" instruction. */
1071 for (input_bfd = link_info->input_bfds;
1073 input_bfd = input_bfd->link_next)
1077 /* We're going to need all the symbols for each bfd. */
1078 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1080 /* Get cached copy if it exists. */
1081 if (symtab_hdr->contents != NULL)
1082 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1085 /* Go get them off disk. */
1086 extsyms = ((Elf32_External_Sym *)
1087 bfd_malloc (symtab_hdr->sh_size));
1088 if (extsyms == NULL)
1090 free_extsyms = extsyms;
1091 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1092 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1093 != symtab_hdr->sh_size))
1097 /* Walk over each section in this bfd. */
1098 for (section = input_bfd->sections;
1100 section = section->next)
1103 Elf32_External_Sym *esym, *esymend;
1106 /* Skip non-code sections and empty sections. */
1107 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1110 if (section->reloc_count != 0)
1112 /* Get a copy of the native relocations. */
1113 internal_relocs = (_bfd_elf32_link_read_relocs
1114 (input_bfd, section, (PTR) NULL,
1115 (Elf_Internal_Rela *) NULL,
1116 link_info->keep_memory));
1117 if (internal_relocs == NULL)
1119 if (! link_info->keep_memory)
1120 free_relocs = internal_relocs;
1123 /* Get cached copy of section contents if it exists. */
1124 if (elf_section_data (section)->this_hdr.contents != NULL)
1125 contents = elf_section_data (section)->this_hdr.contents;
1128 /* Go get them off disk. */
1129 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1130 if (contents == NULL)
1132 free_contents = contents;
1134 if (!bfd_get_section_contents (input_bfd, section,
1135 contents, (file_ptr) 0,
1136 section->_raw_size))
1141 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1143 /* Now look for any function in this section which needs
1144 insns deleted from its prologue. */
1146 esymend = esym + symtab_hdr->sh_info;
1147 for (; esym < esymend; esym++)
1149 Elf_Internal_Sym isym;
1150 struct elf32_mn10300_link_hash_entry *sym_hash;
1152 const char *sym_name;
1153 Elf_Internal_Shdr *hdr;
1156 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1158 if (isym.st_shndx != shndx)
1161 if (isym.st_shndx == SHN_UNDEF)
1162 sym_sec = bfd_und_section_ptr;
1163 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1165 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1166 else if (isym.st_shndx == SHN_ABS)
1167 sym_sec = bfd_abs_section_ptr;
1168 else if (isym.st_shndx == SHN_COMMON)
1169 sym_sec = bfd_com_section_ptr;
1171 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1172 symtab_hdr->sh_link,
1175 /* Tack on an ID so we can uniquely identify this
1176 local symbol in the global hash table. */
1177 new_name = alloca (strlen (sym_name) + 10);
1178 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1179 sym_name = new_name;
1181 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1182 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1186 if (sym_hash == NULL)
1189 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1190 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1194 /* Note that we've changed things. */
1195 elf_section_data (section)->relocs = internal_relocs;
1198 elf_section_data (section)->this_hdr.contents = contents;
1199 free_contents = NULL;
1201 symtab_hdr->contents = (bfd_byte *)extsyms;
1202 free_extsyms = NULL;
1204 /* Count how many bytes we're going to delete. */
1205 if (sym_hash->movm_args)
1208 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1210 else if (sym_hash->stack_size
1211 && sym_hash->stack_size < 256)
1214 /* Note that we've deleted prologue bytes for this
1216 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1218 /* Actually delete the bytes. */
1219 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1225 /* Something changed. Not strictly necessary, but
1226 may lead to more relaxing opportunities. */
1231 /* Look for any global functions in this section which
1232 need insns deleted from their prologues. */
1233 esym = extsyms + symtab_hdr->sh_info;
1234 esymend = extsyms + (symtab_hdr->sh_size
1235 / sizeof (Elf32_External_Sym));
1236 for (idx = 0; esym < esymend; esym++, idx++)
1238 Elf_Internal_Sym isym;
1239 struct elf32_mn10300_link_hash_entry *sym_hash;
1241 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1242 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1243 (elf_sym_hashes (input_bfd)[idx]);
1244 if (isym.st_shndx == shndx
1245 && (sym_hash)->root.root.u.def.section == section
1246 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1247 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1251 /* Note that we've changed things. */
1252 elf_section_data (section)->relocs = internal_relocs;
1255 elf_section_data (section)->this_hdr.contents = contents;
1256 free_contents = NULL;
1258 symtab_hdr->contents = (bfd_byte *)extsyms;
1259 free_extsyms = NULL;
1261 /* Count how many bytes we're going to delete. */
1262 if (sym_hash->movm_args)
1265 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1267 else if (sym_hash->stack_size
1268 && sym_hash->stack_size < 256)
1271 /* Note that we've deleted prologue bytes for this
1273 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1275 /* Actually delete the bytes. */
1276 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1278 (sym_hash)->root.root.u.def.value,
1282 /* Something changed. Not strictly necessary, but
1283 may lead to more relaxing opportunities. */
1288 /* Cache or free any memory we allocated for the relocs. */
1289 if (free_relocs != NULL)
1295 /* Cache or free any memory we allocated for the contents. */
1296 if (free_contents != NULL)
1298 if (! link_info->keep_memory)
1299 free (free_contents);
1302 /* Cache the section contents for elf_link_input_bfd. */
1303 elf_section_data (section)->this_hdr.contents = contents;
1305 free_contents = NULL;
1309 /* Cache or free any memory we allocated for the symbols. */
1310 if (free_extsyms != NULL)
1312 if (! link_info->keep_memory)
1313 free (free_extsyms);
1316 /* Cache the symbols for elf_link_input_bfd. */
1317 symtab_hdr->contents = extsyms;
1319 free_extsyms = NULL;
1325 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1328 internal_relocs = NULL;
1330 free_contents = NULL;
1331 free_extsyms = NULL;
1333 /* We don't have to do anything for a relocateable link, if
1334 this section does not have relocs, or if this is not a
1336 if (link_info->relocateable
1337 || (sec->flags & SEC_RELOC) == 0
1338 || sec->reloc_count == 0
1339 || (sec->flags & SEC_CODE) == 0)
1342 /* If this is the first time we have been called for this section,
1343 initialize the cooked size. */
1344 if (sec->_cooked_size == 0)
1345 sec->_cooked_size = sec->_raw_size;
1347 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1349 /* Get a copy of the native relocations. */
1350 internal_relocs = (_bfd_elf32_link_read_relocs
1351 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1352 link_info->keep_memory));
1353 if (internal_relocs == NULL)
1355 if (! link_info->keep_memory)
1356 free_relocs = internal_relocs;
1358 /* Walk through them looking for relaxing opportunities. */
1359 irelend = internal_relocs + sec->reloc_count;
1360 for (irel = internal_relocs; irel < irelend; irel++)
1363 struct elf32_mn10300_link_hash_entry *h = NULL;
1365 /* If this isn't something that can be relaxed, then ignore
1367 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1368 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1369 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1372 /* Get the section contents if we haven't done so already. */
1373 if (contents == NULL)
1375 /* Get cached copy if it exists. */
1376 if (elf_section_data (sec)->this_hdr.contents != NULL)
1377 contents = elf_section_data (sec)->this_hdr.contents;
1380 /* Go get them off disk. */
1381 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1382 if (contents == NULL)
1384 free_contents = contents;
1386 if (! bfd_get_section_contents (abfd, sec, contents,
1387 (file_ptr) 0, sec->_raw_size))
1392 /* Read this BFD's symbols if we haven't done so already. */
1393 if (extsyms == NULL)
1395 /* Get cached copy if it exists. */
1396 if (symtab_hdr->contents != NULL)
1397 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1400 /* Go get them off disk. */
1401 extsyms = ((Elf32_External_Sym *)
1402 bfd_malloc (symtab_hdr->sh_size));
1403 if (extsyms == NULL)
1405 free_extsyms = extsyms;
1406 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1407 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1408 != symtab_hdr->sh_size))
1413 /* Get the value of the symbol referred to by the reloc. */
1414 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1416 Elf_Internal_Sym isym;
1418 Elf_Internal_Shdr *hdr;
1419 const char *sym_name;
1422 /* A local symbol. */
1423 bfd_elf32_swap_symbol_in (abfd,
1424 extsyms + ELF32_R_SYM (irel->r_info),
1427 if (isym.st_shndx == SHN_UNDEF)
1428 sym_sec = bfd_und_section_ptr;
1429 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1430 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1431 else if (isym.st_shndx == SHN_ABS)
1432 sym_sec = bfd_abs_section_ptr;
1433 else if (isym.st_shndx == SHN_COMMON)
1434 sym_sec = bfd_com_section_ptr;
1436 symval = (isym.st_value
1437 + sym_sec->output_section->vma
1438 + sym_sec->output_offset);
1439 sym_name = bfd_elf_string_from_elf_section (abfd,
1440 symtab_hdr->sh_link,
1443 /* Tack on an ID so we can uniquely identify this
1444 local symbol in the global hash table. */
1445 new_name = alloca (strlen (sym_name) + 10);
1446 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1447 sym_name = new_name;
1449 h = (struct elf32_mn10300_link_hash_entry *)
1450 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1451 sym_name, false, false, false);
1457 /* An external symbol. */
1458 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1459 h = (struct elf32_mn10300_link_hash_entry *)
1460 (elf_sym_hashes (abfd)[indx]);
1461 BFD_ASSERT (h != NULL);
1462 if (h->root.root.type != bfd_link_hash_defined
1463 && h->root.root.type != bfd_link_hash_defweak)
1465 /* This appears to be a reference to an undefined
1466 symbol. Just ignore it--it will be caught by the
1467 regular reloc processing. */
1471 symval = (h->root.root.u.def.value
1472 + h->root.root.u.def.section->output_section->vma
1473 + h->root.root.u.def.section->output_offset);
1476 /* For simplicity of coding, we are going to modify the section
1477 contents, the section relocs, and the BFD symbol table. We
1478 must tell the rest of the code not to free up this
1479 information. It would be possible to instead create a table
1480 of changes which have to be made, as is done in coff-mips.c;
1481 that would be more work, but would require less memory when
1482 the linker is run. */
1484 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1485 branch/call, also deal with "call" -> "calls" conversions and
1486 insertion of prologue data into "call" instructions. */
1487 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1489 bfd_vma value = symval;
1491 /* If we've got a "call" instruction that needs to be turned
1492 into a "calls" instruction, do so now. It saves a byte. */
1493 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1497 /* Get the opcode. */
1498 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1500 /* Make sure we're working with a "call" instruction! */
1503 /* Note that we've changed the relocs, section contents,
1505 elf_section_data (sec)->relocs = internal_relocs;
1508 elf_section_data (sec)->this_hdr.contents = contents;
1509 free_contents = NULL;
1511 symtab_hdr->contents = (bfd_byte *) extsyms;
1512 free_extsyms = NULL;
1514 /* Fix the opcode. */
1515 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1516 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1518 /* Fix irel->r_offset and irel->r_addend. */
1519 irel->r_offset += 1;
1520 irel->r_addend += 1;
1522 /* Delete one byte of data. */
1523 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1524 irel->r_offset + 3, 1))
1527 /* That will change things, so, we should relax again.
1528 Note that this is not required, and it may be slow. */
1534 /* We've got a "call" instruction which needs some data
1535 from target function filled in. */
1538 /* Get the opcode. */
1539 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1541 /* Insert data from the target function into the "call"
1542 instruction if needed. */
1545 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1546 bfd_put_8 (abfd, h->stack_size,
1547 contents + irel->r_offset + 5);
1551 /* Deal with pc-relative gunk. */
1552 value -= (sec->output_section->vma + sec->output_offset);
1553 value -= irel->r_offset;
1554 value += irel->r_addend;
1556 /* See if the value will fit in 16 bits, note the high value is
1557 0x7fff + 2 as the target will be two bytes closer if we are
1559 if ((long)value < 0x8001 && (long)value > -0x8000)
1563 /* Get the opcode. */
1564 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1566 if (code != 0xdc && code != 0xdd && code != 0xff)
1569 /* Note that we've changed the relocs, section contents, etc. */
1570 elf_section_data (sec)->relocs = internal_relocs;
1573 elf_section_data (sec)->this_hdr.contents = contents;
1574 free_contents = NULL;
1576 symtab_hdr->contents = (bfd_byte *) extsyms;
1577 free_extsyms = NULL;
1579 /* Fix the opcode. */
1581 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1582 else if (code == 0xdd)
1583 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1584 else if (code == 0xff)
1585 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1587 /* Fix the relocation's type. */
1588 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1591 /* Delete two bytes of data. */
1592 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1593 irel->r_offset + 1, 2))
1596 /* That will change things, so, we should relax again.
1597 Note that this is not required, and it may be slow. */
1602 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1604 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1606 bfd_vma value = symval;
1608 /* If we've got a "call" instruction that needs to be turned
1609 into a "calls" instruction, do so now. It saves a byte. */
1610 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1614 /* Get the opcode. */
1615 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1617 /* Make sure we're working with a "call" instruction! */
1620 /* Note that we've changed the relocs, section contents,
1622 elf_section_data (sec)->relocs = internal_relocs;
1625 elf_section_data (sec)->this_hdr.contents = contents;
1626 free_contents = NULL;
1628 symtab_hdr->contents = (bfd_byte *) extsyms;
1629 free_extsyms = NULL;
1631 /* Fix the opcode. */
1632 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1633 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1635 /* Fix irel->r_offset and irel->r_addend. */
1636 irel->r_offset += 1;
1637 irel->r_addend += 1;
1639 /* Delete one byte of data. */
1640 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1641 irel->r_offset + 1, 1))
1644 /* That will change things, so, we should relax again.
1645 Note that this is not required, and it may be slow. */
1653 /* Get the opcode. */
1654 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1656 /* Insert data from the target function into the "call"
1657 instruction if needed. */
1660 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1661 bfd_put_8 (abfd, h->stack_size,
1662 contents + irel->r_offset + 3);
1666 /* Deal with pc-relative gunk. */
1667 value -= (sec->output_section->vma + sec->output_offset);
1668 value -= irel->r_offset;
1669 value += irel->r_addend;
1671 /* See if the value will fit in 8 bits, note the high value is
1672 0x7f + 1 as the target will be one bytes closer if we are
1674 if ((long)value < 0x80 && (long)value > -0x80)
1678 /* Get the opcode. */
1679 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1684 /* Note that we've changed the relocs, section contents, etc. */
1685 elf_section_data (sec)->relocs = internal_relocs;
1688 elf_section_data (sec)->this_hdr.contents = contents;
1689 free_contents = NULL;
1691 symtab_hdr->contents = (bfd_byte *) extsyms;
1692 free_extsyms = NULL;
1694 /* Fix the opcode. */
1695 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1697 /* Fix the relocation's type. */
1698 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1701 /* Delete one byte of data. */
1702 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1703 irel->r_offset + 1, 1))
1706 /* That will change things, so, we should relax again.
1707 Note that this is not required, and it may be slow. */
1712 /* Try to eliminate an unconditional 8 bit pc-relative branch
1713 which immediately follows a conditional 8 bit pc-relative
1714 branch around the unconditional branch.
1722 This happens when the bCC can't reach lab2 at assembly time,
1723 but due to other relaxations it can reach at link time. */
1724 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1726 Elf_Internal_Rela *nrel;
1727 bfd_vma value = symval;
1730 /* Deal with pc-relative gunk. */
1731 value -= (sec->output_section->vma + sec->output_offset);
1732 value -= irel->r_offset;
1733 value += irel->r_addend;
1735 /* Do nothing if this reloc is the last byte in the section. */
1736 if (irel->r_offset == sec->_cooked_size)
1739 /* See if the next instruction is an unconditional pc-relative
1740 branch, more often than not this test will fail, so we
1741 test it first to speed things up. */
1742 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1746 /* Also make sure the next relocation applies to the next
1747 instruction and that it's a pc-relative 8 bit branch. */
1750 || irel->r_offset + 2 != nrel->r_offset
1751 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1754 /* Make sure our destination immediately follows the
1755 unconditional branch. */
1756 if (symval != (sec->output_section->vma + sec->output_offset
1757 + irel->r_offset + 3))
1760 /* Now make sure we are a conditional branch. This may not
1761 be necessary, but why take the chance.
1763 Note these checks assume that R_MN10300_PCREL8 relocs
1764 only occur on bCC and bCCx insns. If they occured
1765 elsewhere, we'd need to know the start of this insn
1766 for this check to be accurate. */
1767 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1768 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1769 && code != 0xc3 && code != 0xc4 && code != 0xc5
1770 && code != 0xc6 && code != 0xc7 && code != 0xc8
1771 && code != 0xc9 && code != 0xe8 && code != 0xe9
1772 && code != 0xea && code != 0xeb)
1775 /* We also have to be sure there is no symbol/label
1776 at the unconditional branch. */
1777 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1778 irel->r_offset + 1))
1781 /* Note that we've changed the relocs, section contents, etc. */
1782 elf_section_data (sec)->relocs = internal_relocs;
1785 elf_section_data (sec)->this_hdr.contents = contents;
1786 free_contents = NULL;
1788 symtab_hdr->contents = (bfd_byte *) extsyms;
1789 free_extsyms = NULL;
1791 /* Reverse the condition of the first branch. */
1837 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1839 /* Set the reloc type and symbol for the first branch
1840 from the second branch. */
1841 irel->r_info = nrel->r_info;
1843 /* Make the reloc for the second branch a null reloc. */
1844 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1847 /* Delete two bytes of data. */
1848 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1849 irel->r_offset + 1, 2))
1852 /* That will change things, so, we should relax again.
1853 Note that this is not required, and it may be slow. */
1857 /* Try to turn a 32bit immediate, displacement or absolute address
1858 into a 16bit immediate, displacement or absolute address. */
1859 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1861 bfd_vma value = symval;
1862 value += irel->r_addend;
1864 /* See if the value will fit in 16 bits.
1865 We allow any 16bit match here. We prune those we can't
1867 if ((long)value < 0x7fff && (long)value > -0x8000)
1871 /* Most insns which have 32bit operands are 6 bytes long;
1872 exceptions are pcrel insns and bit insns.
1874 We handle pcrel insns above. We don't bother trying
1875 to handle the bit insns here.
1877 The first byte of the remaining insns will be 0xfc. */
1879 /* Get the first opcode. */
1880 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1885 /* Get the second opcode. */
1886 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1888 if ((code & 0xf0) < 0x80)
1889 switch (code & 0xf0)
1891 /* mov (d32,am),dn -> mov (d32,am),dn
1892 mov dm,(d32,am) -> mov dn,(d32,am)
1893 mov (d32,am),an -> mov (d32,am),an
1894 mov dm,(d32,am) -> mov dn,(d32,am)
1895 movbu (d32,am),dn -> movbu (d32,am),dn
1896 movbu dm,(d32,am) -> movbu dn,(d32,am)
1897 movhu (d32,am),dn -> movhu (d32,am),dn
1898 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1907 /* Not safe if the high bit is on as relaxing may
1908 move the value out of high mem and thus not fit
1909 in a signed 16bit value. */
1911 && (value & 0x8000))
1914 /* Note that we've changed the relocation contents, etc. */
1915 elf_section_data (sec)->relocs = internal_relocs;
1918 elf_section_data (sec)->this_hdr.contents = contents;
1919 free_contents = NULL;
1921 symtab_hdr->contents = (bfd_byte *) extsyms;
1922 free_extsyms = NULL;
1924 /* Fix the opcode. */
1925 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1926 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1928 /* Fix the relocation's type. */
1929 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1932 /* Delete two bytes of data. */
1933 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1934 irel->r_offset + 2, 2))
1937 /* That will change things, so, we should relax again.
1938 Note that this is not required, and it may be slow. */
1942 else if ((code & 0xf0) == 0x80
1943 || (code & 0xf0) == 0x90)
1944 switch (code & 0xf3)
1946 /* mov dn,(abs32) -> mov dn,(abs16)
1947 movbu dn,(abs32) -> movbu dn,(abs16)
1948 movhu dn,(abs32) -> movhu dn,(abs16) */
1952 /* Note that we've changed the relocation contents, etc. */
1953 elf_section_data (sec)->relocs = internal_relocs;
1956 elf_section_data (sec)->this_hdr.contents = contents;
1957 free_contents = NULL;
1959 symtab_hdr->contents = (bfd_byte *) extsyms;
1960 free_extsyms = NULL;
1962 if ((code & 0xf3) == 0x81)
1963 code = 0x01 + (code & 0x0c);
1964 else if ((code & 0xf3) == 0x82)
1965 code = 0x02 + (code & 0x0c);
1966 else if ((code & 0xf3) == 0x83)
1967 code = 0x03 + (code & 0x0c);
1971 /* Fix the opcode. */
1972 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1974 /* Fix the relocation's type. */
1975 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1978 /* The opcode got shorter too, so we have to fix the
1979 addend and offset too! */
1980 irel->r_offset -= 1;
1982 /* Delete three bytes of data. */
1983 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1984 irel->r_offset + 1, 3))
1987 /* That will change things, so, we should relax again.
1988 Note that this is not required, and it may be slow. */
1992 /* mov am,(abs32) -> mov am,(abs16)
1993 mov am,(d32,sp) -> mov am,(d16,sp)
1994 mov dm,(d32,sp) -> mov dm,(d32,sp)
1995 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
1996 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2002 /* Note that we've changed the relocation contents, etc. */
2003 elf_section_data (sec)->relocs = internal_relocs;
2006 elf_section_data (sec)->this_hdr.contents = contents;
2007 free_contents = NULL;
2009 symtab_hdr->contents = (bfd_byte *) extsyms;
2010 free_extsyms = NULL;
2012 /* Fix the opcode. */
2013 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2014 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2016 /* Fix the relocation's type. */
2017 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2020 /* Delete two bytes of data. */
2021 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2022 irel->r_offset + 2, 2))
2025 /* That will change things, so, we should relax again.
2026 Note that this is not required, and it may be slow. */
2030 else if ((code & 0xf0) < 0xf0)
2031 switch (code & 0xfc)
2033 /* mov imm32,dn -> mov imm16,dn
2034 mov imm32,an -> mov imm16,an
2035 mov (abs32),dn -> mov (abs16),dn
2036 movbu (abs32),dn -> movbu (abs16),dn
2037 movhu (abs32),dn -> movhu (abs16),dn */
2043 /* Not safe if the high bit is on as relaxing may
2044 move the value out of high mem and thus not fit
2045 in a signed 16bit value. */
2047 && (value & 0x8000))
2050 /* Note that we've changed the relocation contents, etc. */
2051 elf_section_data (sec)->relocs = internal_relocs;
2054 elf_section_data (sec)->this_hdr.contents = contents;
2055 free_contents = NULL;
2057 symtab_hdr->contents = (bfd_byte *) extsyms;
2058 free_extsyms = NULL;
2060 if ((code & 0xfc) == 0xcc)
2061 code = 0x2c + (code & 0x03);
2062 else if ((code & 0xfc) == 0xdc)
2063 code = 0x24 + (code & 0x03);
2064 else if ((code & 0xfc) == 0xa4)
2065 code = 0x30 + (code & 0x03);
2066 else if ((code & 0xfc) == 0xa8)
2067 code = 0x34 + (code & 0x03);
2068 else if ((code & 0xfc) == 0xac)
2069 code = 0x38 + (code & 0x03);
2073 /* Fix the opcode. */
2074 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2076 /* Fix the relocation's type. */
2077 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2080 /* The opcode got shorter too, so we have to fix the
2081 addend and offset too! */
2082 irel->r_offset -= 1;
2084 /* Delete three bytes of data. */
2085 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2086 irel->r_offset + 1, 3))
2089 /* That will change things, so, we should relax again.
2090 Note that this is not required, and it may be slow. */
2094 /* mov (abs32),an -> mov (abs16),an
2095 mov (d32,sp),an -> mov (d32,sp),an
2096 mov (d32,sp),dn -> mov (d32,sp),dn
2097 movbu (d32,sp),dn -> movbu (d32,sp),dn
2098 movhu (d32,sp),dn -> movhu (d32,sp),dn
2099 add imm32,dn -> add imm16,dn
2100 cmp imm32,dn -> cmp imm16,dn
2101 add imm32,an -> add imm16,an
2102 cmp imm32,an -> cmp imm16,an
2103 and imm32,dn -> and imm32,dn
2104 or imm32,dn -> or imm32,dn
2105 xor imm32,dn -> xor imm32,dn
2106 btst imm32,dn -> btst imm32,dn */
2122 /* Note that we've changed the relocation contents, etc. */
2123 elf_section_data (sec)->relocs = internal_relocs;
2126 elf_section_data (sec)->this_hdr.contents = contents;
2127 free_contents = NULL;
2129 symtab_hdr->contents = (bfd_byte *) extsyms;
2130 free_extsyms = NULL;
2132 /* Fix the opcode. */
2133 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2134 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2136 /* Fix the relocation's type. */
2137 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2140 /* Delete two bytes of data. */
2141 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2142 irel->r_offset + 2, 2))
2145 /* That will change things, so, we should relax again.
2146 Note that this is not required, and it may be slow. */
2150 else if (code == 0xfe)
2152 /* add imm32,sp -> add imm16,sp */
2154 /* Note that we've changed the relocation contents, etc. */
2155 elf_section_data (sec)->relocs = internal_relocs;
2158 elf_section_data (sec)->this_hdr.contents = contents;
2159 free_contents = NULL;
2161 symtab_hdr->contents = (bfd_byte *) extsyms;
2162 free_extsyms = NULL;
2164 /* Fix the opcode. */
2165 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2166 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2168 /* Fix the relocation's type. */
2169 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2172 /* Delete two bytes of data. */
2173 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2174 irel->r_offset + 2, 2))
2177 /* That will change things, so, we should relax again.
2178 Note that this is not required, and it may be slow. */
2186 if (free_relocs != NULL)
2192 if (free_contents != NULL)
2194 if (! link_info->keep_memory)
2195 free (free_contents);
2198 /* Cache the section contents for elf_link_input_bfd. */
2199 elf_section_data (sec)->this_hdr.contents = contents;
2201 free_contents = NULL;
2204 if (free_extsyms != NULL)
2206 if (! link_info->keep_memory)
2207 free (free_extsyms);
2210 /* Cache the symbols for elf_link_input_bfd. */
2211 symtab_hdr->contents = extsyms;
2213 free_extsyms = NULL;
2219 if (free_relocs != NULL)
2221 if (free_contents != NULL)
2222 free (free_contents);
2223 if (free_extsyms != NULL)
2224 free (free_extsyms);
2228 /* Compute the stack size and movm arguments for the function
2229 referred to by HASH at address ADDR in section with
2230 contents CONTENTS, store the information in the hash table. */
2232 compute_function_info (abfd, hash, addr, contents)
2234 struct elf32_mn10300_link_hash_entry *hash;
2236 unsigned char *contents;
2238 unsigned char byte1, byte2;
2239 /* We only care about a very small subset of the possible prologue
2240 sequences here. Basically we look for:
2242 movm [d2,d3,a2,a3],sp (optional)
2243 add <size>,sp (optional, and only for sizes which fit in an unsigned
2246 If we find anything else, we quit. */
2248 /* Look for movm [regs],sp */
2249 byte1 = bfd_get_8 (abfd, contents + addr);
2250 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2254 hash->movm_args = byte2;
2256 byte1 = bfd_get_8 (abfd, contents + addr);
2257 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2260 /* Now look for the two stack adjustment variants. */
2261 if (byte1 == 0xf8 && byte2 == 0xfe)
2263 int temp = bfd_get_8 (abfd, contents + addr + 2);
2264 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2266 hash->stack_size = -temp;
2268 else if (byte1 == 0xfa && byte2 == 0xfe)
2270 int temp = bfd_get_16 (abfd, contents + addr + 2);
2271 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2275 hash->stack_size = temp;
2280 /* Delete some bytes from a section while relaxing. */
2283 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2289 Elf_Internal_Shdr *symtab_hdr;
2290 Elf32_External_Sym *extsyms;
2293 Elf_Internal_Rela *irel, *irelend;
2294 Elf_Internal_Rela *irelalign;
2296 Elf32_External_Sym *esym, *esymend;
2297 struct elf32_mn10300_link_hash_entry *sym_hash;
2299 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2300 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2302 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2304 contents = elf_section_data (sec)->this_hdr.contents;
2306 /* The deletion must stop at the next ALIGN reloc for an aligment
2307 power larger than the number of bytes we are deleting. */
2310 toaddr = sec->_cooked_size;
2312 irel = elf_section_data (sec)->relocs;
2313 irelend = irel + sec->reloc_count;
2315 /* Actually delete the bytes. */
2316 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2317 sec->_cooked_size -= count;
2319 /* Adjust all the relocs. */
2320 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2322 /* Get the new reloc address. */
2323 if ((irel->r_offset > addr
2324 && irel->r_offset < toaddr))
2325 irel->r_offset -= count;
2328 /* Adjust the local symbols defined in this section. */
2330 esymend = esym + symtab_hdr->sh_info;
2331 for (; esym < esymend; esym++)
2333 Elf_Internal_Sym isym;
2335 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2337 if (isym.st_shndx == shndx
2338 && isym.st_value > addr
2339 && isym.st_value < toaddr)
2341 isym.st_value -= count;
2342 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2346 /* Now adjust the global symbols defined in this section. */
2347 esym = extsyms + symtab_hdr->sh_info;
2348 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2349 for (index = 0; esym < esymend; esym++, index++)
2351 Elf_Internal_Sym isym;
2353 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2354 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2355 (elf_sym_hashes (abfd)[index]);
2356 if (isym.st_shndx == shndx
2357 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2358 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2359 && (sym_hash)->root.root.u.def.section == sec
2360 && (sym_hash)->root.root.u.def.value > addr
2361 && (sym_hash)->root.root.u.def.value < toaddr)
2363 (sym_hash)->root.root.u.def.value -= count;
2370 /* Return true if a symbol exists at the given address, else return
2373 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2376 Elf32_External_Sym *extsyms;
2379 Elf_Internal_Shdr *symtab_hdr;
2381 Elf32_External_Sym *esym, *esymend;
2382 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2385 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2387 /* Examine all the symbols. */
2389 esymend = esym + symtab_hdr->sh_info;
2390 for (; esym < esymend; esym++)
2392 Elf_Internal_Sym isym;
2394 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2396 if (isym.st_shndx == shndx
2397 && isym.st_value == addr)
2401 sym_hash = (struct elf32_mn10300_link_hash_entry **)(elf_sym_hashes (abfd));
2402 sym_hash_end = (sym_hash
2403 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2404 - symtab_hdr->sh_info));
2405 for (; sym_hash < sym_hash_end; sym_hash++)
2407 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2408 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2409 && (*sym_hash)->root.root.u.def.section == sec
2410 && (*sym_hash)->root.root.u.def.value == addr)
2416 /* This is a version of bfd_generic_get_relocated_section_contents
2417 which uses mn10300_elf_relocate_section. */
2420 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2421 data, relocateable, symbols)
2423 struct bfd_link_info *link_info;
2424 struct bfd_link_order *link_order;
2426 boolean relocateable;
2429 Elf_Internal_Shdr *symtab_hdr;
2430 asection *input_section = link_order->u.indirect.section;
2431 bfd *input_bfd = input_section->owner;
2432 asection **sections = NULL;
2433 Elf_Internal_Rela *internal_relocs = NULL;
2434 Elf32_External_Sym *external_syms = NULL;
2435 Elf_Internal_Sym *internal_syms = NULL;
2437 /* We only need to handle the case of relaxing, or of having a
2438 particular set of section contents, specially. */
2440 || elf_section_data (input_section)->this_hdr.contents == NULL)
2441 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2446 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2448 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2449 input_section->_raw_size);
2451 if ((input_section->flags & SEC_RELOC) != 0
2452 && input_section->reloc_count > 0)
2454 Elf_Internal_Sym *isymp;
2456 Elf32_External_Sym *esym, *esymend;
2458 if (symtab_hdr->contents != NULL)
2459 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2462 external_syms = ((Elf32_External_Sym *)
2463 bfd_malloc (symtab_hdr->sh_info
2464 * sizeof (Elf32_External_Sym)));
2465 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2467 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2468 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2469 symtab_hdr->sh_info, input_bfd)
2470 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2474 internal_relocs = (_bfd_elf32_link_read_relocs
2475 (input_bfd, input_section, (PTR) NULL,
2476 (Elf_Internal_Rela *) NULL, false));
2477 if (internal_relocs == NULL)
2480 internal_syms = ((Elf_Internal_Sym *)
2481 bfd_malloc (symtab_hdr->sh_info
2482 * sizeof (Elf_Internal_Sym)));
2483 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2486 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2487 * sizeof (asection *));
2488 if (sections == NULL && symtab_hdr->sh_info > 0)
2491 isymp = internal_syms;
2493 esym = external_syms;
2494 esymend = esym + symtab_hdr->sh_info;
2495 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2499 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2501 if (isymp->st_shndx == SHN_UNDEF)
2502 isec = bfd_und_section_ptr;
2503 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2504 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2505 else if (isymp->st_shndx == SHN_ABS)
2506 isec = bfd_abs_section_ptr;
2507 else if (isymp->st_shndx == SHN_COMMON)
2508 isec = bfd_com_section_ptr;
2518 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2519 input_section, data, internal_relocs,
2520 internal_syms, sections))
2523 if (sections != NULL)
2526 if (internal_syms != NULL)
2527 free (internal_syms);
2528 internal_syms = NULL;
2529 if (external_syms != NULL && symtab_hdr->contents == NULL)
2530 free (external_syms);
2531 external_syms = NULL;
2532 if (internal_relocs != elf_section_data (input_section)->relocs)
2533 free (internal_relocs);
2534 internal_relocs = NULL;
2540 if (internal_relocs != NULL
2541 && internal_relocs != elf_section_data (input_section)->relocs)
2542 free (internal_relocs);
2543 if (external_syms != NULL && symtab_hdr->contents == NULL)
2544 free (external_syms);
2545 if (internal_syms != NULL)
2546 free (internal_syms);
2547 if (sections != NULL)
2552 /* Assorted hash table functions. */
2554 /* Initialize an entry in the link hash table. */
2556 /* Create an entry in an MN10300 ELF linker hash table. */
2558 static struct bfd_hash_entry *
2559 elf32_mn10300_link_hash_newfunc (entry, table, string)
2560 struct bfd_hash_entry *entry;
2561 struct bfd_hash_table *table;
2564 struct elf32_mn10300_link_hash_entry *ret =
2565 (struct elf32_mn10300_link_hash_entry *) entry;
2567 /* Allocate the structure if it has not already been allocated by a
2569 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2570 ret = ((struct elf32_mn10300_link_hash_entry *)
2571 bfd_hash_allocate (table,
2572 sizeof (struct elf32_mn10300_link_hash_entry)));
2573 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2574 return (struct bfd_hash_entry *) ret;
2576 /* Call the allocation method of the superclass. */
2577 ret = ((struct elf32_mn10300_link_hash_entry *)
2578 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2580 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2582 ret->direct_calls = 0;
2583 ret->stack_size = 0;
2588 return (struct bfd_hash_entry *) ret;
2591 /* Create an mn10300 ELF linker hash table. */
2593 static struct bfd_link_hash_table *
2594 elf32_mn10300_link_hash_table_create (abfd)
2597 struct elf32_mn10300_link_hash_table *ret;
2599 ret = ((struct elf32_mn10300_link_hash_table *)
2600 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2601 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2604 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2605 elf32_mn10300_link_hash_newfunc))
2607 bfd_release (abfd, ret);
2612 ret->static_hash_table
2613 = ((struct elf32_mn10300_link_hash_table *)
2614 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2615 if (ret->static_hash_table == NULL)
2617 bfd_release (abfd, ret);
2621 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2622 elf32_mn10300_link_hash_newfunc))
2624 bfd_release (abfd, ret->static_hash_table);
2625 bfd_release (abfd, ret);
2628 return &ret->root.root;
2632 elf_mn10300_mach (flags)
2635 switch (flags & EF_MN10300_MACH)
2637 case E_MN10300_MACH_MN10300:
2639 return bfd_mach_mn10300;
2641 /* start-sanitize-am33 */
2642 case E_MN10300_MACH_AM33:
2643 return bfd_mach_am33;
2644 /* end-sanitize-am33 */
2648 /* The final processing done just before writing out a MN10300 ELF object
2649 file. This gets the MN10300 architecture right based on the machine
2654 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2660 Elf_Internal_Shdr **hdrpp;
2664 switch (bfd_get_mach (abfd))
2667 case bfd_mach_mn10300:
2668 val = E_MN10300_MACH_MN10300;
2671 /* start-sanitize-am33 */
2673 val = E_MN10300_MACH_AM33;
2675 /* end-sanitize-am33 */
2678 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2679 elf_elfheader (abfd)->e_flags |= val;
2683 _bfd_mn10300_elf_object_p (abfd)
2686 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2687 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2691 /* Merge backend specific data from an object file to the output
2692 object file when linking. */
2695 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2699 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2700 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2703 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2704 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2706 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2707 bfd_get_mach (ibfd)))
2715 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2716 #define TARGET_LITTLE_NAME "elf32-mn10300"
2717 #define ELF_ARCH bfd_arch_mn10300
2718 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2719 #define ELF_MAXPAGESIZE 0x1000
2721 #define elf_info_to_howto mn10300_info_to_howto
2722 #define elf_info_to_howto_rel 0
2723 #define elf_backend_can_gc_sections 1
2724 #define elf_backend_check_relocs mn10300_elf_check_relocs
2725 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2726 #define elf_backend_relocate_section mn10300_elf_relocate_section
2727 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2728 #define bfd_elf32_bfd_get_relocated_section_contents \
2729 mn10300_elf_get_relocated_section_contents
2730 #define bfd_elf32_bfd_link_hash_table_create \
2731 elf32_mn10300_link_hash_table_create
2733 #define elf_symbol_leading_char '_'
2735 /* So we can set bits in e_flags. */
2736 #define elf_backend_final_write_processing \
2737 _bfd_mn10300_elf_final_write_processing
2738 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2740 #define bfd_elf32_bfd_merge_private_bfd_data \
2741 _bfd_mn10300_elf_merge_private_bfd_data
2744 #include "elf32-target.h"