1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "elf/mn10300.h"
27 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 {
66 /* The main hash table. */
67 struct elf_link_hash_table root;
69 /* A hash table for static functions. We could derive a new hash table
70 instead of using the full elf32_mn10300_link_hash_table if we wanted
71 to save some memory. */
72 struct elf32_mn10300_link_hash_table *static_hash_table;
74 /* Random linker state flags. */
75 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
79 /* For MN10300 linker hash table. */
81 /* Get the MN10300 ELF linker hash table from a link_info structure. */
83 #define elf32_mn10300_hash_table(p) \
84 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
86 #define elf32_mn10300_link_hash_traverse(table, func, info) \
87 (elf_link_hash_traverse \
89 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
92 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
93 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
94 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
97 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
98 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
99 static void mn10300_info_to_howto
100 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
101 static boolean mn10300_elf_check_relocs
102 PARAMS ((bfd *, struct bfd_link_info *, asection *,
103 const Elf_Internal_Rela *));
104 static asection *mn10300_elf_gc_mark_hook
105 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
106 struct elf_link_hash_entry *, Elf_Internal_Sym *));
107 static boolean mn10300_elf_relax_delete_bytes
108 PARAMS ((bfd *, asection *, bfd_vma, int));
109 static boolean mn10300_elf_symbol_address_p
110 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
111 static boolean elf32_mn10300_finish_hash_table_entry
112 PARAMS ((struct bfd_hash_entry *, PTR));
113 static void compute_function_info
114 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
115 bfd_vma, unsigned char *));
117 /* We have to use RELA instructions since md_apply_fix3 in the assembler
118 does absolutely nothing. */
121 static reloc_howto_type elf_mn10300_howto_table[] = {
122 /* Dummy relocation. Does nothing. */
123 HOWTO (R_MN10300_NONE,
129 complain_overflow_bitfield,
130 bfd_elf_generic_reloc,
136 /* Standard 32 bit reloc. */
143 complain_overflow_bitfield,
144 bfd_elf_generic_reloc,
150 /* Standard 16 bit reloc. */
157 complain_overflow_bitfield,
158 bfd_elf_generic_reloc,
164 /* Standard 8 bit reloc. */
171 complain_overflow_bitfield,
172 bfd_elf_generic_reloc,
178 /* Standard 32bit pc-relative reloc. */
179 HOWTO (R_MN10300_PCREL32,
185 complain_overflow_bitfield,
186 bfd_elf_generic_reloc,
192 /* Standard 16bit pc-relative reloc. */
193 HOWTO (R_MN10300_PCREL16,
199 complain_overflow_bitfield,
200 bfd_elf_generic_reloc,
206 /* Standard 8 pc-relative reloc. */
207 HOWTO (R_MN10300_PCREL8,
213 complain_overflow_bitfield,
214 bfd_elf_generic_reloc,
221 /* GNU extension to record C++ vtable hierarchy */
222 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
224 0, /* size (0 = byte, 1 = short, 2 = long) */
226 false, /* pc_relative */
228 complain_overflow_dont, /* complain_on_overflow */
229 NULL, /* special_function */
230 "R_MN10300_GNU_VTINHERIT", /* name */
231 false, /* partial_inplace */
234 false), /* pcrel_offset */
236 /* GNU extension to record C++ vtable member usage */
237 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
239 0, /* size (0 = byte, 1 = short, 2 = long) */
241 false, /* pc_relative */
243 complain_overflow_dont, /* complain_on_overflow */
244 NULL, /* special_function */
245 "R_MN10300_GNU_VTENTRY", /* name */
246 false, /* partial_inplace */
249 false), /* pcrel_offset */
251 /* Standard 24 bit reloc. */
258 complain_overflow_bitfield,
259 bfd_elf_generic_reloc,
267 struct mn10300_reloc_map {
268 bfd_reloc_code_real_type bfd_reloc_val;
269 unsigned char elf_reloc_val;
272 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
273 { BFD_RELOC_NONE, R_MN10300_NONE, },
274 { BFD_RELOC_32, R_MN10300_32, },
275 { BFD_RELOC_16, R_MN10300_16, },
276 { BFD_RELOC_8, R_MN10300_8, },
277 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
278 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
279 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
280 { BFD_RELOC_24, R_MN10300_24, },
281 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
282 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
285 static reloc_howto_type *
286 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
287 bfd *abfd ATTRIBUTE_UNUSED;
288 bfd_reloc_code_real_type code;
293 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
296 if (mn10300_reloc_map[i].bfd_reloc_val == code)
297 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
303 /* Set the howto pointer for an MN10300 ELF reloc. */
306 mn10300_info_to_howto (abfd, cache_ptr, dst)
307 bfd *abfd ATTRIBUTE_UNUSED;
309 Elf32_Internal_Rela *dst;
313 r_type = ELF32_R_TYPE (dst->r_info);
314 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
315 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
318 /* Look through the relocs for a section during the first phase.
319 Since we don't do .gots or .plts, we just need to consider the
320 virtual table relocs for gc. */
323 mn10300_elf_check_relocs (abfd, info, sec, relocs)
325 struct bfd_link_info *info;
327 const Elf_Internal_Rela *relocs;
329 Elf_Internal_Shdr *symtab_hdr;
330 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
331 const Elf_Internal_Rela *rel;
332 const Elf_Internal_Rela *rel_end;
334 if (info->relocateable)
337 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
338 sym_hashes = elf_sym_hashes (abfd);
339 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
340 if (!elf_bad_symtab (abfd))
341 sym_hashes_end -= symtab_hdr->sh_info;
343 rel_end = relocs + sec->reloc_count;
344 for (rel = relocs; rel < rel_end; rel++)
346 struct elf_link_hash_entry *h;
347 unsigned long r_symndx;
349 r_symndx = ELF32_R_SYM (rel->r_info);
350 if (r_symndx < symtab_hdr->sh_info)
353 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
355 switch (ELF32_R_TYPE (rel->r_info))
357 /* This relocation describes the C++ object vtable hierarchy.
358 Reconstruct it for later use during GC. */
359 case R_MN10300_GNU_VTINHERIT:
360 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
364 /* This relocation describes which C++ vtable entries are actually
365 used. Record for later use during GC. */
366 case R_MN10300_GNU_VTENTRY:
367 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
376 /* Return the section that should be marked against GC for a given
380 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
382 struct bfd_link_info *info ATTRIBUTE_UNUSED;
383 Elf_Internal_Rela *rel;
384 struct elf_link_hash_entry *h;
385 Elf_Internal_Sym *sym;
389 switch (ELF32_R_TYPE (rel->r_info))
391 case R_MN10300_GNU_VTINHERIT:
392 case R_MN10300_GNU_VTENTRY:
396 switch (h->root.type)
398 case bfd_link_hash_defined:
399 case bfd_link_hash_defweak:
400 return h->root.u.def.section;
402 case bfd_link_hash_common:
403 return h->root.u.c.p->section;
412 if (!(elf_bad_symtab (abfd)
413 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
414 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
415 && sym->st_shndx != SHN_COMMON))
417 return bfd_section_from_elf_index (abfd, sym->st_shndx);
424 /* Perform a relocation as part of a final link. */
425 static bfd_reloc_status_type
426 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
427 input_section, contents, offset, value,
428 addend, info, sym_sec, is_local)
429 reloc_howto_type *howto;
431 bfd *output_bfd ATTRIBUTE_UNUSED;
432 asection *input_section;
437 struct bfd_link_info *info ATTRIBUTE_UNUSED;
438 asection *sym_sec ATTRIBUTE_UNUSED;
439 int is_local ATTRIBUTE_UNUSED;
441 unsigned long r_type = howto->type;
442 bfd_byte *hit_data = contents + offset;
451 bfd_put_32 (input_bfd, value, hit_data);
457 if ((long) value > 0x7fffff || (long) value < -0x800000)
458 return bfd_reloc_overflow;
460 bfd_put_8 (input_bfd, value & 0xff, hit_data);
461 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
462 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
468 if ((long) value > 0x7fff || (long) value < -0x8000)
469 return bfd_reloc_overflow;
471 bfd_put_16 (input_bfd, value, hit_data);
477 if ((long) value > 0x7f || (long) value < -0x80)
478 return bfd_reloc_overflow;
480 bfd_put_8 (input_bfd, value, hit_data);
483 case R_MN10300_PCREL8:
484 value -= (input_section->output_section->vma
485 + input_section->output_offset);
489 if ((long) value > 0xff || (long) value < -0x100)
490 return bfd_reloc_overflow;
492 bfd_put_8 (input_bfd, value, hit_data);
495 case R_MN10300_PCREL16:
496 value -= (input_section->output_section->vma
497 + input_section->output_offset);
501 if ((long) value > 0xffff || (long) value < -0x10000)
502 return bfd_reloc_overflow;
504 bfd_put_16 (input_bfd, value, hit_data);
507 case R_MN10300_PCREL32:
508 value -= (input_section->output_section->vma
509 + input_section->output_offset);
513 bfd_put_32 (input_bfd, value, hit_data);
516 case R_MN10300_GNU_VTINHERIT:
517 case R_MN10300_GNU_VTENTRY:
521 return bfd_reloc_notsupported;
525 /* Relocate an MN10300 ELF section. */
527 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
528 contents, relocs, local_syms, local_sections)
530 struct bfd_link_info *info;
532 asection *input_section;
534 Elf_Internal_Rela *relocs;
535 Elf_Internal_Sym *local_syms;
536 asection **local_sections;
538 Elf_Internal_Shdr *symtab_hdr;
539 struct elf32_mn10300_link_hash_entry **sym_hashes;
540 Elf_Internal_Rela *rel, *relend;
542 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
543 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
544 (elf_sym_hashes (input_bfd));
547 relend = relocs + input_section->reloc_count;
548 for (; rel < relend; rel++)
551 reloc_howto_type *howto;
552 unsigned long r_symndx;
553 Elf_Internal_Sym *sym;
555 struct elf32_mn10300_link_hash_entry *h;
557 bfd_reloc_status_type r;
559 r_symndx = ELF32_R_SYM (rel->r_info);
560 r_type = ELF32_R_TYPE (rel->r_info);
561 howto = elf_mn10300_howto_table + r_type;
563 /* Just skip the vtable gc relocs. */
564 if (r_type == R_MN10300_GNU_VTINHERIT
565 || r_type == R_MN10300_GNU_VTENTRY)
568 if (info->relocateable)
570 /* This is a relocateable link. We don't have to change
571 anything, unless the reloc is against a section symbol,
572 in which case we have to adjust according to where the
573 section symbol winds up in the output section. */
574 if (r_symndx < symtab_hdr->sh_info)
576 sym = local_syms + r_symndx;
577 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
579 sec = local_sections[r_symndx];
580 rel->r_addend += sec->output_offset + sym->st_value;
587 /* This is a final link. */
591 if (r_symndx < symtab_hdr->sh_info)
593 sym = local_syms + r_symndx;
594 sec = local_sections[r_symndx];
595 relocation = (sec->output_section->vma
601 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
602 while (h->root.type == bfd_link_hash_indirect
603 || h->root.type == bfd_link_hash_warning)
604 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
605 if (h->root.root.type == bfd_link_hash_defined
606 || h->root.root.type == bfd_link_hash_defweak)
608 sec = h->root.root.u.def.section;
609 relocation = (h->root.root.u.def.value
610 + sec->output_section->vma
611 + sec->output_offset);
613 else if (h->root.root.type == bfd_link_hash_undefweak)
617 if (! ((*info->callbacks->undefined_symbol)
618 (info, h->root.root.root.string, input_bfd,
619 input_section, rel->r_offset, true)))
625 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
627 contents, rel->r_offset,
628 relocation, rel->r_addend,
629 info, sec, h == NULL);
631 if (r != bfd_reloc_ok)
634 const char *msg = (const char *) 0;
637 name = h->root.root.root.string;
640 name = (bfd_elf_string_from_elf_section
641 (input_bfd, symtab_hdr->sh_link, sym->st_name));
642 if (name == NULL || *name == '\0')
643 name = bfd_section_name (input_bfd, sec);
648 case bfd_reloc_overflow:
649 if (! ((*info->callbacks->reloc_overflow)
650 (info, name, howto->name, (bfd_vma) 0,
651 input_bfd, input_section, rel->r_offset)))
655 case bfd_reloc_undefined:
656 if (! ((*info->callbacks->undefined_symbol)
657 (info, name, input_bfd, input_section,
658 rel->r_offset, true)))
662 case bfd_reloc_outofrange:
663 msg = _("internal error: out of range error");
666 case bfd_reloc_notsupported:
667 msg = _("internal error: unsupported relocation error");
670 case bfd_reloc_dangerous:
671 msg = _("internal error: dangerous error");
675 msg = _("internal error: unknown error");
679 if (!((*info->callbacks->warning)
680 (info, msg, name, input_bfd, input_section,
691 /* Finish initializing one hash table entry. */
693 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
694 struct bfd_hash_entry *gen_entry;
695 PTR in_args ATTRIBUTE_UNUSED;
697 struct elf32_mn10300_link_hash_entry *entry;
698 unsigned int byte_count = 0;
700 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
702 /* If we already know we want to convert "call" to "calls" for calls
703 to this symbol, then return now. */
704 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
707 /* If there are no named calls to this symbol, or there's nothing we
708 can move from the function itself into the "call" instruction, then
709 note that all "call" instructions should be converted into "calls"
710 instructions and return. */
711 if (entry->direct_calls == 0
712 || (entry->stack_size == 0 && entry->movm_args == 0))
714 /* Make a note that we should convert "call" instructions to "calls"
715 instructions for calls to this symbol. */
716 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
720 /* We may be able to move some instructions from the function itself into
721 the "call" instruction. Count how many bytes we might be able to
722 eliminate in the function itself. */
724 /* A movm instruction is two bytes. */
725 if (entry->movm_args)
728 /* Count the insn to allocate stack space too. */
729 if (entry->stack_size > 0 && entry->stack_size <= 128)
731 else if (entry->stack_size > 0 && entry->stack_size < 256)
734 /* If using "call" will result in larger code, then turn all
735 the associated "call" instructions into "calls" instrutions. */
736 if (byte_count < entry->direct_calls)
737 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
739 /* This routine never fails. */
743 /* This function handles relaxing for the mn10300.
745 There's quite a few relaxing opportunites available on the mn10300:
747 * calls:32 -> calls:16 2 bytes
748 * call:32 -> call:16 2 bytes
750 * call:32 -> calls:32 1 byte
751 * call:16 -> calls:16 1 byte
752 * These are done anytime using "calls" would result
753 in smaller code, or when necessary to preserve the
754 meaning of the program.
758 * In some circumstances we can move instructions
759 from a function prologue into a "call" instruction.
760 This is only done if the resulting code is no larger
761 than the original code.
763 * jmp:32 -> jmp:16 2 bytes
764 * jmp:16 -> bra:8 1 byte
766 * If the previous instruction is a conditional branch
767 around the jump/bra, we may be able to reverse its condition
768 and change its target to the jump's target. The jump/bra
769 can then be deleted. 2 bytes
771 * mov abs32 -> mov abs16 1 or 2 bytes
773 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
774 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
776 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
777 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
779 We don't handle imm16->imm8 or d16->d8 as they're very rare
780 and somewhat more difficult to support. */
783 mn10300_elf_relax_section (abfd, sec, link_info, again)
786 struct bfd_link_info *link_info;
789 Elf_Internal_Shdr *symtab_hdr;
790 Elf_Internal_Rela *internal_relocs = NULL;
791 Elf_Internal_Rela *free_relocs = NULL;
792 Elf_Internal_Rela *irel, *irelend;
793 bfd_byte *contents = NULL;
794 bfd_byte *free_contents = NULL;
795 Elf32_External_Sym *extsyms = NULL;
796 Elf32_External_Sym *free_extsyms = NULL;
797 struct elf32_mn10300_link_hash_table *hash_table;
799 /* Assume nothing changes. */
802 /* We need a pointer to the mn10300 specific hash table. */
803 hash_table = elf32_mn10300_hash_table (link_info);
805 /* Initialize fields in each hash table entry the first time through. */
806 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
810 /* Iterate over all the input bfds. */
811 for (input_bfd = link_info->input_bfds;
813 input_bfd = input_bfd->link_next)
817 /* We're going to need all the symbols for each bfd. */
818 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
820 /* Get cached copy if it exists. */
821 if (symtab_hdr->contents != NULL)
822 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
825 /* Go get them off disk. */
826 extsyms = ((Elf32_External_Sym *)
827 bfd_malloc (symtab_hdr->sh_size));
830 free_extsyms = extsyms;
831 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
832 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
833 != symtab_hdr->sh_size))
837 /* Iterate over each section in this bfd. */
838 for (section = input_bfd->sections;
840 section = section->next)
842 struct elf32_mn10300_link_hash_entry *hash;
843 Elf_Internal_Sym *sym;
844 asection *sym_sec = NULL;
845 const char *sym_name;
848 /* Get cached copy of section contents if it exists. */
849 if (elf_section_data (section)->this_hdr.contents != NULL)
850 contents = elf_section_data (section)->this_hdr.contents;
851 else if (section->_raw_size != 0)
853 /* Go get them off disk. */
854 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
855 if (contents == NULL)
857 free_contents = contents;
859 if (!bfd_get_section_contents (input_bfd, section,
860 contents, (file_ptr) 0,
867 free_contents = NULL;
870 /* If there aren't any relocs, then there's nothing to do. */
871 if ((section->flags & SEC_RELOC) != 0
872 && section->reloc_count != 0)
875 /* Get a copy of the native relocations. */
876 internal_relocs = (_bfd_elf32_link_read_relocs
877 (input_bfd, section, (PTR) NULL,
878 (Elf_Internal_Rela *) NULL,
879 link_info->keep_memory));
880 if (internal_relocs == NULL)
882 if (! link_info->keep_memory)
883 free_relocs = internal_relocs;
885 /* Now examine each relocation. */
886 irel = internal_relocs;
887 irelend = irel + section->reloc_count;
888 for (; irel < irelend; irel++)
891 unsigned long r_index;
894 r_type = ELF32_R_TYPE (irel->r_info);
895 r_index = ELF32_R_SYM (irel->r_info);
897 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
900 /* We need the name and hash table entry of the target
906 if (r_index < symtab_hdr->sh_info)
908 /* A local symbol. */
909 Elf_Internal_Sym isym;
911 bfd_elf32_swap_symbol_in (input_bfd,
912 extsyms + r_index, &isym);
914 if (isym.st_shndx == SHN_UNDEF)
915 sym_sec = bfd_und_section_ptr;
916 else if (isym.st_shndx > 0
917 && isym.st_shndx < SHN_LORESERVE)
919 = bfd_section_from_elf_index (input_bfd,
921 else if (isym.st_shndx == SHN_ABS)
922 sym_sec = bfd_abs_section_ptr;
923 else if (isym.st_shndx == SHN_COMMON)
924 sym_sec = bfd_com_section_ptr;
926 sym_name = bfd_elf_string_from_elf_section (input_bfd,
930 /* If it isn't a function, then we don't care
932 if (r_index < symtab_hdr->sh_info
933 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
936 /* Tack on an ID so we can uniquely identify this
937 local symbol in the global hash table. */
938 new_name = bfd_malloc (strlen (sym_name) + 10);
942 sprintf (new_name, "%s_%08x",
943 sym_name, (int) sym_sec);
946 hash = (struct elf32_mn10300_link_hash_entry *)
947 elf_link_hash_lookup (&hash_table->static_hash_table->root,
954 r_index -= symtab_hdr->sh_info;
955 hash = (struct elf32_mn10300_link_hash_entry *)
956 elf_sym_hashes (input_bfd)[r_index];
959 /* If this is not a "call" instruction, then we
960 should convert "call" instructions to "calls"
962 code = bfd_get_8 (input_bfd,
963 contents + irel->r_offset - 1);
964 if (code != 0xdd && code != 0xcd)
965 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
967 /* If this is a jump/call, then bump the direct_calls
968 counter. Else force "call" to "calls" conversions. */
969 if (r_type == R_MN10300_PCREL32
970 || r_type == R_MN10300_PCREL16)
971 hash->direct_calls++;
973 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
977 /* Now look at the actual contents to get the stack size,
978 and a list of what registers were saved in the prologue
980 if ((section->flags & SEC_CODE) != 0)
983 Elf32_External_Sym *esym, *esymend;
986 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
989 /* Look at each function defined in this section and
990 update info for that function. */
992 esymend = esym + symtab_hdr->sh_info;
993 for (; esym < esymend; esym++)
995 Elf_Internal_Sym isym;
997 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
998 if (isym.st_shndx == shndx
999 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1001 if (isym.st_shndx == SHN_UNDEF)
1002 sym_sec = bfd_und_section_ptr;
1003 else if (isym.st_shndx > 0
1004 && isym.st_shndx < SHN_LORESERVE)
1006 = bfd_section_from_elf_index (input_bfd,
1008 else if (isym.st_shndx == SHN_ABS)
1009 sym_sec = bfd_abs_section_ptr;
1010 else if (isym.st_shndx == SHN_COMMON)
1011 sym_sec = bfd_com_section_ptr;
1013 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1014 symtab_hdr->sh_link,
1017 /* Tack on an ID so we can uniquely identify this
1018 local symbol in the global hash table. */
1019 new_name = bfd_malloc (strlen (sym_name) + 10);
1023 sprintf (new_name, "%s_%08x",
1024 sym_name, (int) sym_sec);
1025 sym_name = new_name;
1027 hash = (struct elf32_mn10300_link_hash_entry *)
1028 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1032 compute_function_info (input_bfd, hash,
1033 isym.st_value, contents);
1037 esym = extsyms + symtab_hdr->sh_info;
1038 esymend = extsyms + (symtab_hdr->sh_size
1039 / sizeof (Elf32_External_Sym));
1040 for (idx = 0; esym < esymend; esym++, idx++)
1042 Elf_Internal_Sym isym;
1044 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1045 hash = (struct elf32_mn10300_link_hash_entry *)
1046 elf_sym_hashes (input_bfd)[idx];
1047 if (isym.st_shndx == shndx
1048 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1049 && (hash)->root.root.u.def.section == section
1050 && ((hash)->root.root.type == bfd_link_hash_defined
1051 || (hash)->root.root.type == bfd_link_hash_defweak))
1052 compute_function_info (input_bfd, hash,
1053 (hash)->root.root.u.def.value,
1058 /* Cache or free any memory we allocated for the relocs. */
1059 if (free_relocs != NULL)
1065 /* Cache or free any memory we allocated for the contents. */
1066 if (free_contents != NULL)
1068 if (! link_info->keep_memory)
1069 free (free_contents);
1072 /* Cache the section contents for elf_link_input_bfd. */
1073 elf_section_data (section)->this_hdr.contents = contents;
1075 free_contents = NULL;
1079 /* Cache or free any memory we allocated for the symbols. */
1080 if (free_extsyms != NULL)
1082 if (! link_info->keep_memory)
1083 free (free_extsyms);
1086 /* Cache the symbols for elf_link_input_bfd. */
1087 symtab_hdr->contents = extsyms;
1089 free_extsyms = NULL;
1093 /* Now iterate on each symbol in the hash table and perform
1094 the final initialization steps on each. */
1095 elf32_mn10300_link_hash_traverse (hash_table,
1096 elf32_mn10300_finish_hash_table_entry,
1098 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1099 elf32_mn10300_finish_hash_table_entry,
1102 /* All entries in the hash table are fully initialized. */
1103 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1105 /* Now that everything has been initialized, go through each
1106 code section and delete any prologue insns which will be
1107 redundant because their operations will be performed by
1108 a "call" instruction. */
1109 for (input_bfd = link_info->input_bfds;
1111 input_bfd = input_bfd->link_next)
1115 /* We're going to need all the symbols for each bfd. */
1116 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1118 /* Get cached copy if it exists. */
1119 if (symtab_hdr->contents != NULL)
1120 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1123 /* Go get them off disk. */
1124 extsyms = ((Elf32_External_Sym *)
1125 bfd_malloc (symtab_hdr->sh_size));
1126 if (extsyms == NULL)
1128 free_extsyms = extsyms;
1129 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1130 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1131 != symtab_hdr->sh_size))
1135 /* Walk over each section in this bfd. */
1136 for (section = input_bfd->sections;
1138 section = section->next)
1141 Elf32_External_Sym *esym, *esymend;
1144 /* Skip non-code sections and empty sections. */
1145 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1148 if (section->reloc_count != 0)
1150 /* Get a copy of the native relocations. */
1151 internal_relocs = (_bfd_elf32_link_read_relocs
1152 (input_bfd, section, (PTR) NULL,
1153 (Elf_Internal_Rela *) NULL,
1154 link_info->keep_memory));
1155 if (internal_relocs == NULL)
1157 if (! link_info->keep_memory)
1158 free_relocs = internal_relocs;
1161 /* Get cached copy of section contents if it exists. */
1162 if (elf_section_data (section)->this_hdr.contents != NULL)
1163 contents = elf_section_data (section)->this_hdr.contents;
1166 /* Go get them off disk. */
1167 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1168 if (contents == NULL)
1170 free_contents = contents;
1172 if (!bfd_get_section_contents (input_bfd, section,
1173 contents, (file_ptr) 0,
1174 section->_raw_size))
1178 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1180 /* Now look for any function in this section which needs
1181 insns deleted from its prologue. */
1183 esymend = esym + symtab_hdr->sh_info;
1184 for (; esym < esymend; esym++)
1186 Elf_Internal_Sym isym;
1187 struct elf32_mn10300_link_hash_entry *sym_hash;
1188 asection *sym_sec = NULL;
1189 const char *sym_name;
1192 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1194 if (isym.st_shndx != shndx)
1197 if (isym.st_shndx == SHN_UNDEF)
1198 sym_sec = bfd_und_section_ptr;
1199 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1201 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1202 else if (isym.st_shndx == SHN_ABS)
1203 sym_sec = bfd_abs_section_ptr;
1204 else if (isym.st_shndx == SHN_COMMON)
1205 sym_sec = bfd_com_section_ptr;
1209 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1210 symtab_hdr->sh_link,
1213 /* Tack on an ID so we can uniquely identify this
1214 local symbol in the global hash table. */
1215 new_name = bfd_malloc (strlen (sym_name) + 10);
1218 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1219 sym_name = new_name;
1221 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1222 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1227 if (sym_hash == NULL)
1230 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1231 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1235 /* Note that we've changed things. */
1236 elf_section_data (section)->relocs = internal_relocs;
1239 elf_section_data (section)->this_hdr.contents = contents;
1240 free_contents = NULL;
1242 symtab_hdr->contents = (bfd_byte *) extsyms;
1243 free_extsyms = NULL;
1245 /* Count how many bytes we're going to delete. */
1246 if (sym_hash->movm_args)
1249 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1251 else if (sym_hash->stack_size
1252 && sym_hash->stack_size < 256)
1255 /* Note that we've deleted prologue bytes for this
1257 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1259 /* Actually delete the bytes. */
1260 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1266 /* Something changed. Not strictly necessary, but
1267 may lead to more relaxing opportunities. */
1272 /* Look for any global functions in this section which
1273 need insns deleted from their prologues. */
1274 esym = extsyms + symtab_hdr->sh_info;
1275 esymend = extsyms + (symtab_hdr->sh_size
1276 / sizeof (Elf32_External_Sym));
1277 for (idx = 0; esym < esymend; esym++, idx++)
1279 Elf_Internal_Sym isym;
1280 struct elf32_mn10300_link_hash_entry *sym_hash;
1282 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1283 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1284 (elf_sym_hashes (input_bfd)[idx]);
1285 if (isym.st_shndx == shndx
1286 && (sym_hash)->root.root.u.def.section == section
1287 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1288 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1292 /* Note that we've changed things. */
1293 elf_section_data (section)->relocs = internal_relocs;
1296 elf_section_data (section)->this_hdr.contents = contents;
1297 free_contents = NULL;
1299 symtab_hdr->contents = (bfd_byte *) extsyms;
1300 free_extsyms = NULL;
1302 /* Count how many bytes we're going to delete. */
1303 if (sym_hash->movm_args)
1306 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1308 else if (sym_hash->stack_size
1309 && sym_hash->stack_size < 256)
1312 /* Note that we've deleted prologue bytes for this
1314 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1316 /* Actually delete the bytes. */
1317 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1319 (sym_hash)->root.root.u.def.value,
1323 /* Something changed. Not strictly necessary, but
1324 may lead to more relaxing opportunities. */
1329 /* Cache or free any memory we allocated for the relocs. */
1330 if (free_relocs != NULL)
1336 /* Cache or free any memory we allocated for the contents. */
1337 if (free_contents != NULL)
1339 if (! link_info->keep_memory)
1340 free (free_contents);
1343 /* Cache the section contents for elf_link_input_bfd. */
1344 elf_section_data (section)->this_hdr.contents = contents;
1346 free_contents = NULL;
1350 /* Cache or free any memory we allocated for the symbols. */
1351 if (free_extsyms != NULL)
1353 if (! link_info->keep_memory)
1354 free (free_extsyms);
1357 /* Cache the symbols for elf_link_input_bfd. */
1358 symtab_hdr->contents = extsyms;
1360 free_extsyms = NULL;
1365 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1368 internal_relocs = NULL;
1370 free_contents = NULL;
1371 free_extsyms = NULL;
1373 /* We don't have to do anything for a relocateable link, if
1374 this section does not have relocs, or if this is not a
1376 if (link_info->relocateable
1377 || (sec->flags & SEC_RELOC) == 0
1378 || sec->reloc_count == 0
1379 || (sec->flags & SEC_CODE) == 0)
1382 /* If this is the first time we have been called for this section,
1383 initialize the cooked size. */
1384 if (sec->_cooked_size == 0)
1385 sec->_cooked_size = sec->_raw_size;
1387 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1389 /* Get a copy of the native relocations. */
1390 internal_relocs = (_bfd_elf32_link_read_relocs
1391 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1392 link_info->keep_memory));
1393 if (internal_relocs == NULL)
1395 if (! link_info->keep_memory)
1396 free_relocs = internal_relocs;
1398 /* Walk through them looking for relaxing opportunities. */
1399 irelend = internal_relocs + sec->reloc_count;
1400 for (irel = internal_relocs; irel < irelend; irel++)
1403 struct elf32_mn10300_link_hash_entry *h = NULL;
1405 /* If this isn't something that can be relaxed, then ignore
1407 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1408 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1409 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1412 /* Get the section contents if we haven't done so already. */
1413 if (contents == NULL)
1415 /* Get cached copy if it exists. */
1416 if (elf_section_data (sec)->this_hdr.contents != NULL)
1417 contents = elf_section_data (sec)->this_hdr.contents;
1420 /* Go get them off disk. */
1421 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1422 if (contents == NULL)
1424 free_contents = contents;
1426 if (! bfd_get_section_contents (abfd, sec, contents,
1427 (file_ptr) 0, sec->_raw_size))
1432 /* Read this BFD's symbols if we haven't done so already. */
1433 if (extsyms == NULL)
1435 /* Get cached copy if it exists. */
1436 if (symtab_hdr->contents != NULL)
1437 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1440 /* Go get them off disk. */
1441 extsyms = ((Elf32_External_Sym *)
1442 bfd_malloc (symtab_hdr->sh_size));
1443 if (extsyms == NULL)
1445 free_extsyms = extsyms;
1446 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1447 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1448 != symtab_hdr->sh_size))
1453 /* Get the value of the symbol referred to by the reloc. */
1454 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1456 Elf_Internal_Sym isym;
1457 asection *sym_sec = NULL;
1458 const char *sym_name;
1461 /* A local symbol. */
1462 bfd_elf32_swap_symbol_in (abfd,
1463 extsyms + ELF32_R_SYM (irel->r_info),
1466 if (isym.st_shndx == SHN_UNDEF)
1467 sym_sec = bfd_und_section_ptr;
1468 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1469 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1470 else if (isym.st_shndx == SHN_ABS)
1471 sym_sec = bfd_abs_section_ptr;
1472 else if (isym.st_shndx == SHN_COMMON)
1473 sym_sec = bfd_com_section_ptr;
1477 symval = (isym.st_value
1478 + sym_sec->output_section->vma
1479 + sym_sec->output_offset);
1480 sym_name = bfd_elf_string_from_elf_section (abfd,
1481 symtab_hdr->sh_link,
1484 /* Tack on an ID so we can uniquely identify this
1485 local symbol in the global hash table. */
1486 new_name = bfd_malloc (strlen (sym_name) + 10);
1489 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1490 sym_name = new_name;
1492 h = (struct elf32_mn10300_link_hash_entry *)
1493 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1494 sym_name, false, false, false);
1501 /* An external symbol. */
1502 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1503 h = (struct elf32_mn10300_link_hash_entry *)
1504 (elf_sym_hashes (abfd)[indx]);
1505 BFD_ASSERT (h != NULL);
1506 if (h->root.root.type != bfd_link_hash_defined
1507 && h->root.root.type != bfd_link_hash_defweak)
1509 /* This appears to be a reference to an undefined
1510 symbol. Just ignore it--it will be caught by the
1511 regular reloc processing. */
1515 symval = (h->root.root.u.def.value
1516 + h->root.root.u.def.section->output_section->vma
1517 + h->root.root.u.def.section->output_offset);
1520 /* For simplicity of coding, we are going to modify the section
1521 contents, the section relocs, and the BFD symbol table. We
1522 must tell the rest of the code not to free up this
1523 information. It would be possible to instead create a table
1524 of changes which have to be made, as is done in coff-mips.c;
1525 that would be more work, but would require less memory when
1526 the linker is run. */
1528 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1529 branch/call, also deal with "call" -> "calls" conversions and
1530 insertion of prologue data into "call" instructions. */
1531 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1533 bfd_vma value = symval;
1535 /* If we've got a "call" instruction that needs to be turned
1536 into a "calls" instruction, do so now. It saves a byte. */
1537 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1541 /* Get the opcode. */
1542 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1544 /* Make sure we're working with a "call" instruction! */
1547 /* Note that we've changed the relocs, section contents,
1549 elf_section_data (sec)->relocs = internal_relocs;
1552 elf_section_data (sec)->this_hdr.contents = contents;
1553 free_contents = NULL;
1555 symtab_hdr->contents = (bfd_byte *) extsyms;
1556 free_extsyms = NULL;
1558 /* Fix the opcode. */
1559 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1560 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1562 /* Fix irel->r_offset and irel->r_addend. */
1563 irel->r_offset += 1;
1564 irel->r_addend += 1;
1566 /* Delete one byte of data. */
1567 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1568 irel->r_offset + 3, 1))
1571 /* That will change things, so, we should relax again.
1572 Note that this is not required, and it may be slow. */
1578 /* We've got a "call" instruction which needs some data
1579 from target function filled in. */
1582 /* Get the opcode. */
1583 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1585 /* Insert data from the target function into the "call"
1586 instruction if needed. */
1589 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1590 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1591 contents + irel->r_offset + 5);
1595 /* Deal with pc-relative gunk. */
1596 value -= (sec->output_section->vma + sec->output_offset);
1597 value -= irel->r_offset;
1598 value += irel->r_addend;
1600 /* See if the value will fit in 16 bits, note the high value is
1601 0x7fff + 2 as the target will be two bytes closer if we are
1603 if ((long) value < 0x8001 && (long) value > -0x8000)
1607 /* Get the opcode. */
1608 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1610 if (code != 0xdc && code != 0xdd && code != 0xff)
1613 /* Note that we've changed the relocs, section contents, etc. */
1614 elf_section_data (sec)->relocs = internal_relocs;
1617 elf_section_data (sec)->this_hdr.contents = contents;
1618 free_contents = NULL;
1620 symtab_hdr->contents = (bfd_byte *) extsyms;
1621 free_extsyms = NULL;
1623 /* Fix the opcode. */
1625 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1626 else if (code == 0xdd)
1627 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1628 else if (code == 0xff)
1629 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1631 /* Fix the relocation's type. */
1632 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1635 /* Delete two bytes of data. */
1636 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1637 irel->r_offset + 1, 2))
1640 /* That will change things, so, we should relax again.
1641 Note that this is not required, and it may be slow. */
1646 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1648 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1650 bfd_vma value = symval;
1652 /* If we've got a "call" instruction that needs to be turned
1653 into a "calls" instruction, do so now. It saves a byte. */
1654 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1658 /* Get the opcode. */
1659 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1661 /* Make sure we're working with a "call" instruction! */
1664 /* Note that we've changed the relocs, section contents,
1666 elf_section_data (sec)->relocs = internal_relocs;
1669 elf_section_data (sec)->this_hdr.contents = contents;
1670 free_contents = NULL;
1672 symtab_hdr->contents = (bfd_byte *) extsyms;
1673 free_extsyms = NULL;
1675 /* Fix the opcode. */
1676 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1677 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1679 /* Fix irel->r_offset and irel->r_addend. */
1680 irel->r_offset += 1;
1681 irel->r_addend += 1;
1683 /* Delete one byte of data. */
1684 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1685 irel->r_offset + 1, 1))
1688 /* That will change things, so, we should relax again.
1689 Note that this is not required, and it may be slow. */
1697 /* Get the opcode. */
1698 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1700 /* Insert data from the target function into the "call"
1701 instruction if needed. */
1704 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1705 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1706 contents + irel->r_offset + 3);
1710 /* Deal with pc-relative gunk. */
1711 value -= (sec->output_section->vma + sec->output_offset);
1712 value -= irel->r_offset;
1713 value += irel->r_addend;
1715 /* See if the value will fit in 8 bits, note the high value is
1716 0x7f + 1 as the target will be one bytes closer if we are
1718 if ((long) value < 0x80 && (long) value > -0x80)
1722 /* Get the opcode. */
1723 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1728 /* Note that we've changed the relocs, section contents, etc. */
1729 elf_section_data (sec)->relocs = internal_relocs;
1732 elf_section_data (sec)->this_hdr.contents = contents;
1733 free_contents = NULL;
1735 symtab_hdr->contents = (bfd_byte *) extsyms;
1736 free_extsyms = NULL;
1738 /* Fix the opcode. */
1739 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1741 /* Fix the relocation's type. */
1742 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1745 /* Delete one byte of data. */
1746 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1747 irel->r_offset + 1, 1))
1750 /* That will change things, so, we should relax again.
1751 Note that this is not required, and it may be slow. */
1756 /* Try to eliminate an unconditional 8 bit pc-relative branch
1757 which immediately follows a conditional 8 bit pc-relative
1758 branch around the unconditional branch.
1765 This happens when the bCC can't reach lab2 at assembly time,
1766 but due to other relaxations it can reach at link time. */
1767 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1769 Elf_Internal_Rela *nrel;
1770 bfd_vma value = symval;
1773 /* Deal with pc-relative gunk. */
1774 value -= (sec->output_section->vma + sec->output_offset);
1775 value -= irel->r_offset;
1776 value += irel->r_addend;
1778 /* Do nothing if this reloc is the last byte in the section. */
1779 if (irel->r_offset == sec->_cooked_size)
1782 /* See if the next instruction is an unconditional pc-relative
1783 branch, more often than not this test will fail, so we
1784 test it first to speed things up. */
1785 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1789 /* Also make sure the next relocation applies to the next
1790 instruction and that it's a pc-relative 8 bit branch. */
1793 || irel->r_offset + 2 != nrel->r_offset
1794 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1797 /* Make sure our destination immediately follows the
1798 unconditional branch. */
1799 if (symval != (sec->output_section->vma + sec->output_offset
1800 + irel->r_offset + 3))
1803 /* Now make sure we are a conditional branch. This may not
1804 be necessary, but why take the chance.
1806 Note these checks assume that R_MN10300_PCREL8 relocs
1807 only occur on bCC and bCCx insns. If they occured
1808 elsewhere, we'd need to know the start of this insn
1809 for this check to be accurate. */
1810 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1811 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1812 && code != 0xc3 && code != 0xc4 && code != 0xc5
1813 && code != 0xc6 && code != 0xc7 && code != 0xc8
1814 && code != 0xc9 && code != 0xe8 && code != 0xe9
1815 && code != 0xea && code != 0xeb)
1818 /* We also have to be sure there is no symbol/label
1819 at the unconditional branch. */
1820 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1821 irel->r_offset + 1))
1824 /* Note that we've changed the relocs, section contents, etc. */
1825 elf_section_data (sec)->relocs = internal_relocs;
1828 elf_section_data (sec)->this_hdr.contents = contents;
1829 free_contents = NULL;
1831 symtab_hdr->contents = (bfd_byte *) extsyms;
1832 free_extsyms = NULL;
1834 /* Reverse the condition of the first branch. */
1880 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1882 /* Set the reloc type and symbol for the first branch
1883 from the second branch. */
1884 irel->r_info = nrel->r_info;
1886 /* Make the reloc for the second branch a null reloc. */
1887 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1890 /* Delete two bytes of data. */
1891 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1892 irel->r_offset + 1, 2))
1895 /* That will change things, so, we should relax again.
1896 Note that this is not required, and it may be slow. */
1900 /* Try to turn a 24 immediate, displacement or absolute address
1901 into a 8 immediate, displacement or absolute address. */
1902 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1904 bfd_vma value = symval;
1905 value += irel->r_addend;
1907 /* See if the value will fit in 8 bits. */
1908 if ((long) value < 0x7f && (long) value > -0x80)
1912 /* AM33 insns which have 24 operands are 6 bytes long and
1913 will have 0xfd as the first byte. */
1915 /* Get the first opcode. */
1916 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1920 /* Get the second opcode. */
1921 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1923 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1924 equivalent instructions exists. */
1925 if (code != 0x6b && code != 0x7b
1926 && code != 0x8b && code != 0x9b
1927 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1928 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1929 || (code & 0x0f) == 0x0e))
1931 /* Not safe if the high bit is on as relaxing may
1932 move the value out of high mem and thus not fit
1933 in a signed 8bit value. This is currently over
1935 if ((value & 0x80) == 0)
1937 /* Note that we've changed the relocation contents,
1939 elf_section_data (sec)->relocs = internal_relocs;
1942 elf_section_data (sec)->this_hdr.contents = contents;
1943 free_contents = NULL;
1945 symtab_hdr->contents = (bfd_byte *) extsyms;
1946 free_extsyms = NULL;
1948 /* Fix the opcode. */
1949 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1950 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1952 /* Fix the relocation's type. */
1954 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1957 /* Delete two bytes of data. */
1958 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1959 irel->r_offset + 1, 2))
1962 /* That will change things, so, we should relax
1963 again. Note that this is not required, and it
1973 /* Try to turn a 32bit immediate, displacement or absolute address
1974 into a 16bit immediate, displacement or absolute address. */
1975 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1977 bfd_vma value = symval;
1978 value += irel->r_addend;
1980 /* See if the value will fit in 24 bits.
1981 We allow any 16bit match here. We prune those we can't
1983 if ((long) value < 0x7fffff && (long) value > -0x800000)
1987 /* AM33 insns which have 32bit operands are 7 bytes long and
1988 will have 0xfe as the first byte. */
1990 /* Get the first opcode. */
1991 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1995 /* Get the second opcode. */
1996 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1998 /* All the am33 32 -> 24 relaxing possibilities. */
1999 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2000 equivalent instructions exists. */
2001 if (code != 0x6b && code != 0x7b
2002 && code != 0x8b && code != 0x9b
2003 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2004 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2005 || (code & 0x0f) == 0x0e))
2007 /* Not safe if the high bit is on as relaxing may
2008 move the value out of high mem and thus not fit
2009 in a signed 16bit value. This is currently over
2011 if ((value & 0x8000) == 0)
2013 /* Note that we've changed the relocation contents,
2015 elf_section_data (sec)->relocs = internal_relocs;
2018 elf_section_data (sec)->this_hdr.contents = contents;
2019 free_contents = NULL;
2021 symtab_hdr->contents = (bfd_byte *) extsyms;
2022 free_extsyms = NULL;
2024 /* Fix the opcode. */
2025 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2026 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2028 /* Fix the relocation's type. */
2030 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2033 /* Delete one byte of data. */
2034 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2035 irel->r_offset + 3, 1))
2038 /* That will change things, so, we should relax
2039 again. Note that this is not required, and it
2048 /* See if the value will fit in 16 bits.
2049 We allow any 16bit match here. We prune those we can't
2051 if ((long) value < 0x7fff && (long) value > -0x8000)
2055 /* Most insns which have 32bit operands are 6 bytes long;
2056 exceptions are pcrel insns and bit insns.
2058 We handle pcrel insns above. We don't bother trying
2059 to handle the bit insns here.
2061 The first byte of the remaining insns will be 0xfc. */
2063 /* Get the first opcode. */
2064 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2069 /* Get the second opcode. */
2070 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2072 if ((code & 0xf0) < 0x80)
2073 switch (code & 0xf0)
2075 /* mov (d32,am),dn -> mov (d32,am),dn
2076 mov dm,(d32,am) -> mov dn,(d32,am)
2077 mov (d32,am),an -> mov (d32,am),an
2078 mov dm,(d32,am) -> mov dn,(d32,am)
2079 movbu (d32,am),dn -> movbu (d32,am),dn
2080 movbu dm,(d32,am) -> movbu dn,(d32,am)
2081 movhu (d32,am),dn -> movhu (d32,am),dn
2082 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2091 /* Not safe if the high bit is on as relaxing may
2092 move the value out of high mem and thus not fit
2093 in a signed 16bit value. */
2095 && (value & 0x8000))
2098 /* Note that we've changed the relocation contents, etc. */
2099 elf_section_data (sec)->relocs = internal_relocs;
2102 elf_section_data (sec)->this_hdr.contents = contents;
2103 free_contents = NULL;
2105 symtab_hdr->contents = (bfd_byte *) extsyms;
2106 free_extsyms = NULL;
2108 /* Fix the opcode. */
2109 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2110 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2112 /* Fix the relocation's type. */
2113 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2116 /* Delete two bytes of data. */
2117 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2118 irel->r_offset + 2, 2))
2121 /* That will change things, so, we should relax again.
2122 Note that this is not required, and it may be slow. */
2126 else if ((code & 0xf0) == 0x80
2127 || (code & 0xf0) == 0x90)
2128 switch (code & 0xf3)
2130 /* mov dn,(abs32) -> mov dn,(abs16)
2131 movbu dn,(abs32) -> movbu dn,(abs16)
2132 movhu dn,(abs32) -> movhu dn,(abs16) */
2136 /* Note that we've changed the relocation contents, etc. */
2137 elf_section_data (sec)->relocs = internal_relocs;
2140 elf_section_data (sec)->this_hdr.contents = contents;
2141 free_contents = NULL;
2143 symtab_hdr->contents = (bfd_byte *) extsyms;
2144 free_extsyms = NULL;
2146 if ((code & 0xf3) == 0x81)
2147 code = 0x01 + (code & 0x0c);
2148 else if ((code & 0xf3) == 0x82)
2149 code = 0x02 + (code & 0x0c);
2150 else if ((code & 0xf3) == 0x83)
2151 code = 0x03 + (code & 0x0c);
2155 /* Fix the opcode. */
2156 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2158 /* Fix the relocation's type. */
2159 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2162 /* The opcode got shorter too, so we have to fix the
2163 addend and offset too! */
2164 irel->r_offset -= 1;
2166 /* Delete three bytes of data. */
2167 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2168 irel->r_offset + 1, 3))
2171 /* That will change things, so, we should relax again.
2172 Note that this is not required, and it may be slow. */
2176 /* mov am,(abs32) -> mov am,(abs16)
2177 mov am,(d32,sp) -> mov am,(d16,sp)
2178 mov dm,(d32,sp) -> mov dm,(d32,sp)
2179 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2180 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2186 /* sp-based offsets are zero-extended. */
2187 if (code >= 0x90 && code <= 0x93
2191 /* Note that we've changed the relocation contents, etc. */
2192 elf_section_data (sec)->relocs = internal_relocs;
2195 elf_section_data (sec)->this_hdr.contents = contents;
2196 free_contents = NULL;
2198 symtab_hdr->contents = (bfd_byte *) extsyms;
2199 free_extsyms = NULL;
2201 /* Fix the opcode. */
2202 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2203 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2205 /* Fix the relocation's type. */
2206 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2209 /* Delete two bytes of data. */
2210 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2211 irel->r_offset + 2, 2))
2214 /* That will change things, so, we should relax again.
2215 Note that this is not required, and it may be slow. */
2219 else if ((code & 0xf0) < 0xf0)
2220 switch (code & 0xfc)
2222 /* mov imm32,dn -> mov imm16,dn
2223 mov imm32,an -> mov imm16,an
2224 mov (abs32),dn -> mov (abs16),dn
2225 movbu (abs32),dn -> movbu (abs16),dn
2226 movhu (abs32),dn -> movhu (abs16),dn */
2232 /* Not safe if the high bit is on as relaxing may
2233 move the value out of high mem and thus not fit
2234 in a signed 16bit value. */
2236 && (value & 0x8000))
2239 /* mov imm16, an zero-extends the immediate. */
2244 /* Note that we've changed the relocation contents, etc. */
2245 elf_section_data (sec)->relocs = internal_relocs;
2248 elf_section_data (sec)->this_hdr.contents = contents;
2249 free_contents = NULL;
2251 symtab_hdr->contents = (bfd_byte *) extsyms;
2252 free_extsyms = NULL;
2254 if ((code & 0xfc) == 0xcc)
2255 code = 0x2c + (code & 0x03);
2256 else if ((code & 0xfc) == 0xdc)
2257 code = 0x24 + (code & 0x03);
2258 else if ((code & 0xfc) == 0xa4)
2259 code = 0x30 + (code & 0x03);
2260 else if ((code & 0xfc) == 0xa8)
2261 code = 0x34 + (code & 0x03);
2262 else if ((code & 0xfc) == 0xac)
2263 code = 0x38 + (code & 0x03);
2267 /* Fix the opcode. */
2268 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2270 /* Fix the relocation's type. */
2271 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2274 /* The opcode got shorter too, so we have to fix the
2275 addend and offset too! */
2276 irel->r_offset -= 1;
2278 /* Delete three bytes of data. */
2279 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2280 irel->r_offset + 1, 3))
2283 /* That will change things, so, we should relax again.
2284 Note that this is not required, and it may be slow. */
2288 /* mov (abs32),an -> mov (abs16),an
2289 mov (d32,sp),an -> mov (d16,sp),an
2290 mov (d32,sp),dn -> mov (d16,sp),dn
2291 movbu (d32,sp),dn -> movbu (d16,sp),dn
2292 movhu (d32,sp),dn -> movhu (d16,sp),dn
2293 add imm32,dn -> add imm16,dn
2294 cmp imm32,dn -> cmp imm16,dn
2295 add imm32,an -> add imm16,an
2296 cmp imm32,an -> cmp imm16,an
2297 and imm32,dn -> and imm16,dn
2298 or imm32,dn -> or imm16,dn
2299 xor imm32,dn -> xor imm16,dn
2300 btst imm32,dn -> btst imm16,dn */
2316 /* cmp imm16, an zero-extends the immediate. */
2321 /* So do sp-based offsets. */
2322 if (code >= 0xb0 && code <= 0xb3
2326 /* Note that we've changed the relocation contents, etc. */
2327 elf_section_data (sec)->relocs = internal_relocs;
2330 elf_section_data (sec)->this_hdr.contents = contents;
2331 free_contents = NULL;
2333 symtab_hdr->contents = (bfd_byte *) extsyms;
2334 free_extsyms = NULL;
2336 /* Fix the opcode. */
2337 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2338 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2340 /* Fix the relocation's type. */
2341 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2344 /* Delete two bytes of data. */
2345 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2346 irel->r_offset + 2, 2))
2349 /* That will change things, so, we should relax again.
2350 Note that this is not required, and it may be slow. */
2354 else if (code == 0xfe)
2356 /* add imm32,sp -> add imm16,sp */
2358 /* Note that we've changed the relocation contents, etc. */
2359 elf_section_data (sec)->relocs = internal_relocs;
2362 elf_section_data (sec)->this_hdr.contents = contents;
2363 free_contents = NULL;
2365 symtab_hdr->contents = (bfd_byte *) extsyms;
2366 free_extsyms = NULL;
2368 /* Fix the opcode. */
2369 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2370 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2372 /* Fix the relocation's type. */
2373 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2376 /* Delete two bytes of data. */
2377 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2378 irel->r_offset + 2, 2))
2381 /* That will change things, so, we should relax again.
2382 Note that this is not required, and it may be slow. */
2390 if (free_relocs != NULL)
2396 if (free_contents != NULL)
2398 if (! link_info->keep_memory)
2399 free (free_contents);
2402 /* Cache the section contents for elf_link_input_bfd. */
2403 elf_section_data (sec)->this_hdr.contents = contents;
2405 free_contents = NULL;
2408 if (free_extsyms != NULL)
2410 if (! link_info->keep_memory)
2411 free (free_extsyms);
2414 /* Cache the symbols for elf_link_input_bfd. */
2415 symtab_hdr->contents = extsyms;
2417 free_extsyms = NULL;
2423 if (free_relocs != NULL)
2425 if (free_contents != NULL)
2426 free (free_contents);
2427 if (free_extsyms != NULL)
2428 free (free_extsyms);
2432 /* Compute the stack size and movm arguments for the function
2433 referred to by HASH at address ADDR in section with
2434 contents CONTENTS, store the information in the hash table. */
2436 compute_function_info (abfd, hash, addr, contents)
2438 struct elf32_mn10300_link_hash_entry *hash;
2440 unsigned char *contents;
2442 unsigned char byte1, byte2;
2443 /* We only care about a very small subset of the possible prologue
2444 sequences here. Basically we look for:
2446 movm [d2,d3,a2,a3],sp (optional)
2447 add <size>,sp (optional, and only for sizes which fit in an unsigned
2450 If we find anything else, we quit. */
2452 /* Look for movm [regs],sp */
2453 byte1 = bfd_get_8 (abfd, contents + addr);
2454 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2458 hash->movm_args = byte2;
2460 byte1 = bfd_get_8 (abfd, contents + addr);
2461 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2464 /* Now figure out how much stack space will be allocated by the movm
2465 instruction. We need this kept separate from the funtion's normal
2467 if (hash->movm_args)
2470 if (hash->movm_args & 0x80)
2471 hash->movm_stack_size += 4;
2474 if (hash->movm_args & 0x40)
2475 hash->movm_stack_size += 4;
2478 if (hash->movm_args & 0x20)
2479 hash->movm_stack_size += 4;
2482 if (hash->movm_args & 0x10)
2483 hash->movm_stack_size += 4;
2485 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2486 if (hash->movm_args & 0x08)
2487 hash->movm_stack_size += 8 * 4;
2489 if (bfd_get_mach (abfd) == bfd_mach_am33)
2491 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2492 if (hash->movm_args & 0x1)
2493 hash->movm_stack_size += 6 * 4;
2495 /* exreg1 space. e4, e5, e6, e7 */
2496 if (hash->movm_args & 0x2)
2497 hash->movm_stack_size += 4 * 4;
2499 /* exreg0 space. e2, e3 */
2500 if (hash->movm_args & 0x4)
2501 hash->movm_stack_size += 2 * 4;
2505 /* Now look for the two stack adjustment variants. */
2506 if (byte1 == 0xf8 && byte2 == 0xfe)
2508 int temp = bfd_get_8 (abfd, contents + addr + 2);
2509 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2511 hash->stack_size = -temp;
2513 else if (byte1 == 0xfa && byte2 == 0xfe)
2515 int temp = bfd_get_16 (abfd, contents + addr + 2);
2516 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2520 hash->stack_size = temp;
2523 /* If the total stack to be allocated by the call instruction is more
2524 than 255 bytes, then we can't remove the stack adjustment by using
2525 "call" (we might still be able to remove the "movm" instruction. */
2526 if (hash->stack_size + hash->movm_stack_size > 255)
2527 hash->stack_size = 0;
2532 /* Delete some bytes from a section while relaxing. */
2535 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2541 Elf_Internal_Shdr *symtab_hdr;
2542 Elf32_External_Sym *extsyms;
2545 Elf_Internal_Rela *irel, *irelend;
2546 Elf_Internal_Rela *irelalign;
2548 Elf32_External_Sym *esym, *esymend;
2549 struct elf32_mn10300_link_hash_entry *sym_hash;
2551 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2552 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2554 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2556 contents = elf_section_data (sec)->this_hdr.contents;
2558 /* The deletion must stop at the next ALIGN reloc for an aligment
2559 power larger than the number of bytes we are deleting. */
2562 toaddr = sec->_cooked_size;
2564 irel = elf_section_data (sec)->relocs;
2565 irelend = irel + sec->reloc_count;
2567 /* Actually delete the bytes. */
2568 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2569 sec->_cooked_size -= count;
2571 /* Adjust all the relocs. */
2572 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2574 /* Get the new reloc address. */
2575 if ((irel->r_offset > addr
2576 && irel->r_offset < toaddr))
2577 irel->r_offset -= count;
2580 /* Adjust the local symbols defined in this section. */
2582 esymend = esym + symtab_hdr->sh_info;
2583 for (; esym < esymend; esym++)
2585 Elf_Internal_Sym isym;
2587 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2589 if (isym.st_shndx == shndx
2590 && isym.st_value > addr
2591 && isym.st_value < toaddr)
2593 isym.st_value -= count;
2594 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2598 /* Now adjust the global symbols defined in this section. */
2599 esym = extsyms + symtab_hdr->sh_info;
2600 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2601 for (index = 0; esym < esymend; esym++, index++)
2603 Elf_Internal_Sym isym;
2605 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2606 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2607 (elf_sym_hashes (abfd)[index]);
2608 if (isym.st_shndx == shndx
2609 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2610 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2611 && (sym_hash)->root.root.u.def.section == sec
2612 && (sym_hash)->root.root.u.def.value > addr
2613 && (sym_hash)->root.root.u.def.value < toaddr)
2615 (sym_hash)->root.root.u.def.value -= count;
2622 /* Return true if a symbol exists at the given address, else return
2625 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2628 Elf32_External_Sym *extsyms;
2631 Elf_Internal_Shdr *symtab_hdr;
2633 Elf32_External_Sym *esym, *esymend;
2634 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2636 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2637 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2639 /* Examine all the symbols. */
2641 esymend = esym + symtab_hdr->sh_info;
2642 for (; esym < esymend; esym++)
2644 Elf_Internal_Sym isym;
2646 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2648 if (isym.st_shndx == shndx
2649 && isym.st_value == addr)
2653 sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd));
2654 sym_hash_end = (sym_hash
2655 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2656 - symtab_hdr->sh_info));
2657 for (; sym_hash < sym_hash_end; sym_hash++)
2659 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2660 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2661 && (*sym_hash)->root.root.u.def.section == sec
2662 && (*sym_hash)->root.root.u.def.value == addr)
2668 /* This is a version of bfd_generic_get_relocated_section_contents
2669 which uses mn10300_elf_relocate_section. */
2672 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2673 data, relocateable, symbols)
2675 struct bfd_link_info *link_info;
2676 struct bfd_link_order *link_order;
2678 boolean relocateable;
2681 Elf_Internal_Shdr *symtab_hdr;
2682 asection *input_section = link_order->u.indirect.section;
2683 bfd *input_bfd = input_section->owner;
2684 asection **sections = NULL;
2685 Elf_Internal_Rela *internal_relocs = NULL;
2686 Elf32_External_Sym *external_syms = NULL;
2687 Elf_Internal_Sym *internal_syms = NULL;
2689 /* We only need to handle the case of relaxing, or of having a
2690 particular set of section contents, specially. */
2692 || elf_section_data (input_section)->this_hdr.contents == NULL)
2693 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2698 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2700 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2701 input_section->_raw_size);
2703 if ((input_section->flags & SEC_RELOC) != 0
2704 && input_section->reloc_count > 0)
2706 Elf_Internal_Sym *isymp;
2708 Elf32_External_Sym *esym, *esymend;
2710 if (symtab_hdr->contents != NULL)
2711 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2714 external_syms = ((Elf32_External_Sym *)
2715 bfd_malloc (symtab_hdr->sh_info
2716 * sizeof (Elf32_External_Sym)));
2717 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2719 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2720 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2721 symtab_hdr->sh_info, input_bfd)
2722 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2726 internal_relocs = (_bfd_elf32_link_read_relocs
2727 (input_bfd, input_section, (PTR) NULL,
2728 (Elf_Internal_Rela *) NULL, false));
2729 if (internal_relocs == NULL)
2732 internal_syms = ((Elf_Internal_Sym *)
2733 bfd_malloc (symtab_hdr->sh_info
2734 * sizeof (Elf_Internal_Sym)));
2735 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2738 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2739 * sizeof (asection *));
2740 if (sections == NULL && symtab_hdr->sh_info > 0)
2743 isymp = internal_syms;
2745 esym = external_syms;
2746 esymend = esym + symtab_hdr->sh_info;
2747 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2751 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2753 if (isymp->st_shndx == SHN_UNDEF)
2754 isec = bfd_und_section_ptr;
2755 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2756 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2757 else if (isymp->st_shndx == SHN_ABS)
2758 isec = bfd_abs_section_ptr;
2759 else if (isymp->st_shndx == SHN_COMMON)
2760 isec = bfd_com_section_ptr;
2770 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2771 input_section, data, internal_relocs,
2772 internal_syms, sections))
2775 if (sections != NULL)
2778 if (internal_syms != NULL)
2779 free (internal_syms);
2780 internal_syms = NULL;
2781 if (external_syms != NULL && symtab_hdr->contents == NULL)
2782 free (external_syms);
2783 external_syms = NULL;
2784 if (internal_relocs != elf_section_data (input_section)->relocs)
2785 free (internal_relocs);
2786 internal_relocs = NULL;
2792 if (internal_relocs != NULL
2793 && internal_relocs != elf_section_data (input_section)->relocs)
2794 free (internal_relocs);
2795 if (external_syms != NULL && symtab_hdr->contents == NULL)
2796 free (external_syms);
2797 if (internal_syms != NULL)
2798 free (internal_syms);
2799 if (sections != NULL)
2804 /* Assorted hash table functions. */
2806 /* Initialize an entry in the link hash table. */
2808 /* Create an entry in an MN10300 ELF linker hash table. */
2810 static struct bfd_hash_entry *
2811 elf32_mn10300_link_hash_newfunc (entry, table, string)
2812 struct bfd_hash_entry *entry;
2813 struct bfd_hash_table *table;
2816 struct elf32_mn10300_link_hash_entry *ret =
2817 (struct elf32_mn10300_link_hash_entry *) entry;
2819 /* Allocate the structure if it has not already been allocated by a
2821 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2822 ret = ((struct elf32_mn10300_link_hash_entry *)
2823 bfd_hash_allocate (table,
2824 sizeof (struct elf32_mn10300_link_hash_entry)));
2825 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2826 return (struct bfd_hash_entry *) ret;
2828 /* Call the allocation method of the superclass. */
2829 ret = ((struct elf32_mn10300_link_hash_entry *)
2830 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2832 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2834 ret->direct_calls = 0;
2835 ret->stack_size = 0;
2836 ret->movm_stack_size = 0;
2841 return (struct bfd_hash_entry *) ret;
2844 /* Create an mn10300 ELF linker hash table. */
2846 static struct bfd_link_hash_table *
2847 elf32_mn10300_link_hash_table_create (abfd)
2850 struct elf32_mn10300_link_hash_table *ret;
2852 ret = ((struct elf32_mn10300_link_hash_table *)
2853 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2854 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2857 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2858 elf32_mn10300_link_hash_newfunc))
2860 bfd_release (abfd, ret);
2865 ret->static_hash_table
2866 = ((struct elf32_mn10300_link_hash_table *)
2867 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2868 if (ret->static_hash_table == NULL)
2870 bfd_release (abfd, ret);
2874 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2875 elf32_mn10300_link_hash_newfunc))
2877 bfd_release (abfd, ret->static_hash_table);
2878 bfd_release (abfd, ret);
2881 return &ret->root.root;
2885 elf_mn10300_mach (flags)
2888 switch (flags & EF_MN10300_MACH)
2890 case E_MN10300_MACH_MN10300:
2892 return bfd_mach_mn10300;
2894 case E_MN10300_MACH_AM33:
2895 return bfd_mach_am33;
2899 /* The final processing done just before writing out a MN10300 ELF object
2900 file. This gets the MN10300 architecture right based on the machine
2904 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2906 boolean linker ATTRIBUTE_UNUSED;
2910 switch (bfd_get_mach (abfd))
2913 case bfd_mach_mn10300:
2914 val = E_MN10300_MACH_MN10300;
2918 val = E_MN10300_MACH_AM33;
2922 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2923 elf_elfheader (abfd)->e_flags |= val;
2927 _bfd_mn10300_elf_object_p (abfd)
2930 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2931 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2935 /* Merge backend specific data from an object file to the output
2936 object file when linking. */
2939 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2943 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2944 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2947 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2948 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2950 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2951 bfd_get_mach (ibfd)))
2958 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2959 #define TARGET_LITTLE_NAME "elf32-mn10300"
2960 #define ELF_ARCH bfd_arch_mn10300
2961 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2962 #define ELF_MAXPAGESIZE 0x1000
2964 #define elf_info_to_howto mn10300_info_to_howto
2965 #define elf_info_to_howto_rel 0
2966 #define elf_backend_can_gc_sections 1
2967 #define elf_backend_check_relocs mn10300_elf_check_relocs
2968 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2969 #define elf_backend_relocate_section mn10300_elf_relocate_section
2970 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2971 #define bfd_elf32_bfd_get_relocated_section_contents \
2972 mn10300_elf_get_relocated_section_contents
2973 #define bfd_elf32_bfd_link_hash_table_create \
2974 elf32_mn10300_link_hash_table_create
2976 #define elf_symbol_leading_char '_'
2978 /* So we can set bits in e_flags. */
2979 #define elf_backend_final_write_processing \
2980 _bfd_mn10300_elf_final_write_processing
2981 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2983 #define bfd_elf32_bfd_merge_private_bfd_data \
2984 _bfd_mn10300_elf_merge_private_bfd_data
2986 #include "elf32-target.h"