1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "elf/mn10300.h"
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*, bfd_byte
*,
29 bfd_vma
, bfd_vma
, bfd_vma
, struct bfd_link_info
*,
31 static boolean mn10300_elf_relocate_section
32 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
33 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
34 static boolean mn10300_elf_relax_section
35 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, boolean
*));
36 static bfd_byte
* mn10300_elf_get_relocated_section_contents
37 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
38 bfd_byte
*, boolean
, asymbol
**));
39 static int elf_mn10300_mach
PARAMS ((flagword
));
41 void _bfd_mn10300_elf_final_write_processing
PARAMS ((bfd
*, boolean
));
42 boolean _bfd_mn10300_elf_object_p
PARAMS ((bfd
*));
43 boolean _bfd_mn10300_elf_merge_private_bfd_data
PARAMS ((bfd
*,bfd
*));
45 struct elf32_mn10300_link_hash_entry
{
46 /* The basic elf link hash table entry. */
47 struct elf_link_hash_entry root
;
49 /* For function symbols, the number of times this function is
50 called directly (ie by name). */
51 unsigned int direct_calls
;
53 /* For function symbols, the size of this function's stack
54 (if <= 255 bytes). We stuff this into "call" instructions
55 to this target when it's valid and profitable to do so.
57 This does not include stack allocated by movm! */
58 unsigned char stack_size
;
60 /* For function symbols, arguments (if any) for movm instruction
61 in the prologue. We stuff this value into "call" instructions
62 to the target when it's valid and profitable to do so. */
63 unsigned char movm_args
;
65 /* For funtion symbols, the amount of stack space that would be allocated
66 by the movm instruction. This is redundant with movm_args, but we
67 add it to the hash table to avoid computing it over and over. */
68 unsigned char movm_stack_size
;
70 /* When set, convert all "call" instructions to this target into "calls"
72 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
74 /* Used to mark functions which have had redundant parts of their
76 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
80 /* We derive a hash table from the main elf linker hash table so
81 we can store state variables and a secondary hash table without
82 resorting to global variables. */
83 struct elf32_mn10300_link_hash_table
{
84 /* The main hash table. */
85 struct elf_link_hash_table root
;
87 /* A hash table for static functions. We could derive a new hash table
88 instead of using the full elf32_mn10300_link_hash_table if we wanted
89 to save some memory. */
90 struct elf32_mn10300_link_hash_table
*static_hash_table
;
92 /* Random linker state flags. */
93 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
97 /* For MN10300 linker hash table. */
99 /* Get the MN10300 ELF linker hash table from a link_info structure. */
101 #define elf32_mn10300_hash_table(p) \
102 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
107 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
110 static struct bfd_hash_entry
*elf32_mn10300_link_hash_newfunc
111 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
112 static struct bfd_link_hash_table
*elf32_mn10300_link_hash_table_create
115 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
116 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
117 static void mn10300_info_to_howto
118 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
119 static boolean mn10300_elf_check_relocs
120 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
121 const Elf_Internal_Rela
*));
122 static asection
*mn10300_elf_gc_mark_hook
123 PARAMS ((bfd
*, struct bfd_link_info
*info
, Elf_Internal_Rela
*,
124 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
125 static boolean mn10300_elf_relax_delete_bytes
126 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
127 static boolean mn10300_elf_symbol_address_p
128 PARAMS ((bfd
*, asection
*, Elf32_External_Sym
*, bfd_vma
));
129 static boolean elf32_mn10300_finish_hash_table_entry
130 PARAMS ((struct bfd_hash_entry
*, PTR
));
131 static void compute_function_info
132 PARAMS ((bfd
*, struct elf32_mn10300_link_hash_entry
*,
133 bfd_vma
, unsigned char *));
135 /* We have to use RELA instructions since md_apply_fix3 in the assembler
136 does absolutely nothing. */
139 static reloc_howto_type elf_mn10300_howto_table
[] = {
140 /* Dummy relocation. Does nothing. */
141 HOWTO (R_MN10300_NONE
,
147 complain_overflow_bitfield
,
148 bfd_elf_generic_reloc
,
154 /* Standard 32 bit reloc. */
161 complain_overflow_bitfield
,
162 bfd_elf_generic_reloc
,
168 /* Standard 16 bit reloc. */
175 complain_overflow_bitfield
,
176 bfd_elf_generic_reloc
,
182 /* Standard 8 bit reloc. */
189 complain_overflow_bitfield
,
190 bfd_elf_generic_reloc
,
196 /* Standard 32bit pc-relative reloc. */
197 HOWTO (R_MN10300_PCREL32
,
203 complain_overflow_bitfield
,
204 bfd_elf_generic_reloc
,
210 /* Standard 16bit pc-relative reloc. */
211 HOWTO (R_MN10300_PCREL16
,
217 complain_overflow_bitfield
,
218 bfd_elf_generic_reloc
,
224 /* Standard 8 pc-relative reloc. */
225 HOWTO (R_MN10300_PCREL8
,
231 complain_overflow_bitfield
,
232 bfd_elf_generic_reloc
,
239 /* GNU extension to record C++ vtable hierarchy */
240 HOWTO (R_MN10300_GNU_VTINHERIT
, /* type */
242 0, /* size (0 = byte, 1 = short, 2 = long) */
244 false, /* pc_relative */
246 complain_overflow_dont
, /* complain_on_overflow */
247 NULL
, /* special_function */
248 "R_MN10300_GNU_VTINHERIT", /* name */
249 false, /* partial_inplace */
252 false), /* pcrel_offset */
254 /* GNU extension to record C++ vtable member usage */
255 HOWTO (R_MN10300_GNU_VTENTRY
, /* type */
257 0, /* size (0 = byte, 1 = short, 2 = long) */
259 false, /* pc_relative */
261 complain_overflow_dont
, /* complain_on_overflow */
262 NULL
, /* special_function */
263 "R_MN10300_GNU_VTENTRY", /* name */
264 false, /* partial_inplace */
267 false), /* pcrel_offset */
269 /* Standard 24 bit reloc. */
276 complain_overflow_bitfield
,
277 bfd_elf_generic_reloc
,
285 struct mn10300_reloc_map
{
286 bfd_reloc_code_real_type bfd_reloc_val
;
287 unsigned char elf_reloc_val
;
290 static const struct mn10300_reloc_map mn10300_reloc_map
[] = {
291 { BFD_RELOC_NONE
, R_MN10300_NONE
, },
292 { BFD_RELOC_32
, R_MN10300_32
, },
293 { BFD_RELOC_16
, R_MN10300_16
, },
294 { BFD_RELOC_8
, R_MN10300_8
, },
295 { BFD_RELOC_32_PCREL
, R_MN10300_PCREL32
, },
296 { BFD_RELOC_16_PCREL
, R_MN10300_PCREL16
, },
297 { BFD_RELOC_8_PCREL
, R_MN10300_PCREL8
, },
298 { BFD_RELOC_24
, R_MN10300_24
, },
299 { BFD_RELOC_VTABLE_INHERIT
, R_MN10300_GNU_VTINHERIT
},
300 { BFD_RELOC_VTABLE_ENTRY
, R_MN10300_GNU_VTENTRY
},
303 static reloc_howto_type
*
304 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
305 bfd
*abfd ATTRIBUTE_UNUSED
;
306 bfd_reloc_code_real_type code
;
311 i
< sizeof (mn10300_reloc_map
) / sizeof (struct mn10300_reloc_map
);
314 if (mn10300_reloc_map
[i
].bfd_reloc_val
== code
)
315 return &elf_mn10300_howto_table
[mn10300_reloc_map
[i
].elf_reloc_val
];
321 /* Set the howto pointer for an MN10300 ELF reloc. */
324 mn10300_info_to_howto (abfd
, cache_ptr
, dst
)
325 bfd
*abfd ATTRIBUTE_UNUSED
;
327 Elf32_Internal_Rela
*dst
;
331 r_type
= ELF32_R_TYPE (dst
->r_info
);
332 BFD_ASSERT (r_type
< (unsigned int) R_MN10300_MAX
);
333 cache_ptr
->howto
= &elf_mn10300_howto_table
[r_type
];
336 /* Look through the relocs for a section during the first phase.
337 Since we don't do .gots or .plts, we just need to consider the
338 virtual table relocs for gc. */
341 mn10300_elf_check_relocs (abfd
, info
, sec
, relocs
)
343 struct bfd_link_info
*info
;
345 const Elf_Internal_Rela
*relocs
;
347 Elf_Internal_Shdr
*symtab_hdr
;
348 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
349 const Elf_Internal_Rela
*rel
;
350 const Elf_Internal_Rela
*rel_end
;
352 if (info
->relocateable
)
355 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
356 sym_hashes
= elf_sym_hashes (abfd
);
357 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
358 if (!elf_bad_symtab (abfd
))
359 sym_hashes_end
-= symtab_hdr
->sh_info
;
361 rel_end
= relocs
+ sec
->reloc_count
;
362 for (rel
= relocs
; rel
< rel_end
; rel
++)
364 struct elf_link_hash_entry
*h
;
365 unsigned long r_symndx
;
367 r_symndx
= ELF32_R_SYM (rel
->r_info
);
368 if (r_symndx
< symtab_hdr
->sh_info
)
371 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
373 switch (ELF32_R_TYPE (rel
->r_info
))
375 /* This relocation describes the C++ object vtable hierarchy.
376 Reconstruct it for later use during GC. */
377 case R_MN10300_GNU_VTINHERIT
:
378 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
382 /* This relocation describes which C++ vtable entries are actually
383 used. Record for later use during GC. */
384 case R_MN10300_GNU_VTENTRY
:
385 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
394 /* Return the section that should be marked against GC for a given
398 mn10300_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
400 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
401 Elf_Internal_Rela
*rel
;
402 struct elf_link_hash_entry
*h
;
403 Elf_Internal_Sym
*sym
;
407 switch (ELF32_R_TYPE (rel
->r_info
))
409 case R_MN10300_GNU_VTINHERIT
:
410 case R_MN10300_GNU_VTENTRY
:
414 switch (h
->root
.type
)
416 case bfd_link_hash_defined
:
417 case bfd_link_hash_defweak
:
418 return h
->root
.u
.def
.section
;
420 case bfd_link_hash_common
:
421 return h
->root
.u
.c
.p
->section
;
430 if (!(elf_bad_symtab (abfd
)
431 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
432 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
433 && sym
->st_shndx
!= SHN_COMMON
))
435 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
442 /* Perform a relocation as part of a final link. */
443 static bfd_reloc_status_type
444 mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
445 input_section
, contents
, offset
, value
,
446 addend
, info
, sym_sec
, is_local
)
447 reloc_howto_type
*howto
;
449 bfd
*output_bfd ATTRIBUTE_UNUSED
;
450 asection
*input_section
;
455 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
456 asection
*sym_sec ATTRIBUTE_UNUSED
;
457 int is_local ATTRIBUTE_UNUSED
;
459 unsigned long r_type
= howto
->type
;
460 bfd_byte
*hit_data
= contents
+ offset
;
469 bfd_put_32 (input_bfd
, value
, hit_data
);
475 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
476 return bfd_reloc_overflow
;
478 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
479 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
480 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
486 if ((long) value
> 0x7fff || (long) value
< -0x8000)
487 return bfd_reloc_overflow
;
489 bfd_put_16 (input_bfd
, value
, hit_data
);
495 if ((long) value
> 0x7f || (long) value
< -0x80)
496 return bfd_reloc_overflow
;
498 bfd_put_8 (input_bfd
, value
, hit_data
);
501 case R_MN10300_PCREL8
:
502 value
-= (input_section
->output_section
->vma
503 + input_section
->output_offset
);
507 if ((long) value
> 0xff || (long) value
< -0x100)
508 return bfd_reloc_overflow
;
510 bfd_put_8 (input_bfd
, value
, hit_data
);
513 case R_MN10300_PCREL16
:
514 value
-= (input_section
->output_section
->vma
515 + input_section
->output_offset
);
519 if ((long) value
> 0xffff || (long) value
< -0x10000)
520 return bfd_reloc_overflow
;
522 bfd_put_16 (input_bfd
, value
, hit_data
);
525 case R_MN10300_PCREL32
:
526 value
-= (input_section
->output_section
->vma
527 + input_section
->output_offset
);
531 bfd_put_32 (input_bfd
, value
, hit_data
);
534 case R_MN10300_GNU_VTINHERIT
:
535 case R_MN10300_GNU_VTENTRY
:
539 return bfd_reloc_notsupported
;
543 /* Relocate an MN10300 ELF section. */
545 mn10300_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
546 contents
, relocs
, local_syms
, local_sections
)
548 struct bfd_link_info
*info
;
550 asection
*input_section
;
552 Elf_Internal_Rela
*relocs
;
553 Elf_Internal_Sym
*local_syms
;
554 asection
**local_sections
;
556 Elf_Internal_Shdr
*symtab_hdr
;
557 struct elf32_mn10300_link_hash_entry
**sym_hashes
;
558 Elf_Internal_Rela
*rel
, *relend
;
560 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
561 sym_hashes
= (struct elf32_mn10300_link_hash_entry
**)
562 (elf_sym_hashes (input_bfd
));
565 relend
= relocs
+ input_section
->reloc_count
;
566 for (; rel
< relend
; rel
++)
569 reloc_howto_type
*howto
;
570 unsigned long r_symndx
;
571 Elf_Internal_Sym
*sym
;
573 struct elf32_mn10300_link_hash_entry
*h
;
575 bfd_reloc_status_type r
;
577 r_symndx
= ELF32_R_SYM (rel
->r_info
);
578 r_type
= ELF32_R_TYPE (rel
->r_info
);
579 howto
= elf_mn10300_howto_table
+ r_type
;
581 /* Just skip the vtable gc relocs. */
582 if (r_type
== R_MN10300_GNU_VTINHERIT
583 || r_type
== R_MN10300_GNU_VTENTRY
)
586 if (info
->relocateable
)
588 /* This is a relocateable link. We don't have to change
589 anything, unless the reloc is against a section symbol,
590 in which case we have to adjust according to where the
591 section symbol winds up in the output section. */
592 if (r_symndx
< symtab_hdr
->sh_info
)
594 sym
= local_syms
+ r_symndx
;
595 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
597 sec
= local_sections
[r_symndx
];
598 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
605 /* This is a final link. */
609 if (r_symndx
< symtab_hdr
->sh_info
)
611 sym
= local_syms
+ r_symndx
;
612 sec
= local_sections
[r_symndx
];
613 relocation
= (sec
->output_section
->vma
619 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
620 while (h
->root
.root
.type
== bfd_link_hash_indirect
621 || h
->root
.root
.type
== bfd_link_hash_warning
)
622 h
= (struct elf32_mn10300_link_hash_entry
*) h
->root
.root
.u
.i
.link
;
623 if (h
->root
.root
.type
== bfd_link_hash_defined
624 || h
->root
.root
.type
== bfd_link_hash_defweak
)
626 sec
= h
->root
.root
.u
.def
.section
;
627 relocation
= (h
->root
.root
.u
.def
.value
628 + sec
->output_section
->vma
629 + sec
->output_offset
);
631 else if (h
->root
.root
.type
== bfd_link_hash_undefweak
)
635 if (! ((*info
->callbacks
->undefined_symbol
)
636 (info
, h
->root
.root
.root
.string
, input_bfd
,
637 input_section
, rel
->r_offset
, true)))
643 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
645 contents
, rel
->r_offset
,
646 relocation
, rel
->r_addend
,
647 info
, sec
, h
== NULL
);
649 if (r
!= bfd_reloc_ok
)
652 const char *msg
= (const char *) 0;
655 name
= h
->root
.root
.root
.string
;
658 name
= (bfd_elf_string_from_elf_section
659 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
660 if (name
== NULL
|| *name
== '\0')
661 name
= bfd_section_name (input_bfd
, sec
);
666 case bfd_reloc_overflow
:
667 if (! ((*info
->callbacks
->reloc_overflow
)
668 (info
, name
, howto
->name
, (bfd_vma
) 0,
669 input_bfd
, input_section
, rel
->r_offset
)))
673 case bfd_reloc_undefined
:
674 if (! ((*info
->callbacks
->undefined_symbol
)
675 (info
, name
, input_bfd
, input_section
,
676 rel
->r_offset
, true)))
680 case bfd_reloc_outofrange
:
681 msg
= _("internal error: out of range error");
684 case bfd_reloc_notsupported
:
685 msg
= _("internal error: unsupported relocation error");
688 case bfd_reloc_dangerous
:
689 msg
= _("internal error: dangerous error");
693 msg
= _("internal error: unknown error");
697 if (!((*info
->callbacks
->warning
)
698 (info
, msg
, name
, input_bfd
, input_section
,
709 /* Finish initializing one hash table entry. */
711 elf32_mn10300_finish_hash_table_entry (gen_entry
, in_args
)
712 struct bfd_hash_entry
*gen_entry
;
713 PTR in_args ATTRIBUTE_UNUSED
;
715 struct elf32_mn10300_link_hash_entry
*entry
;
716 unsigned int byte_count
= 0;
718 entry
= (struct elf32_mn10300_link_hash_entry
*) gen_entry
;
720 /* If we already know we want to convert "call" to "calls" for calls
721 to this symbol, then return now. */
722 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
725 /* If there are no named calls to this symbol, or there's nothing we
726 can move from the function itself into the "call" instruction, then
727 note that all "call" instructions should be converted into "calls"
728 instructions and return. */
729 if (entry
->direct_calls
== 0
730 || (entry
->stack_size
== 0 && entry
->movm_args
== 0))
732 /* Make a note that we should convert "call" instructions to "calls"
733 instructions for calls to this symbol. */
734 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
738 /* We may be able to move some instructions from the function itself into
739 the "call" instruction. Count how many bytes we might be able to
740 eliminate in the function itself. */
742 /* A movm instruction is two bytes. */
743 if (entry
->movm_args
)
746 /* Count the insn to allocate stack space too. */
747 if (entry
->stack_size
> 0 && entry
->stack_size
<= 128)
749 else if (entry
->stack_size
> 0 && entry
->stack_size
< 256)
752 /* If using "call" will result in larger code, then turn all
753 the associated "call" instructions into "calls" instrutions. */
754 if (byte_count
< entry
->direct_calls
)
755 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
757 /* This routine never fails. */
761 /* This function handles relaxing for the mn10300.
763 There's quite a few relaxing opportunites available on the mn10300:
765 * calls:32 -> calls:16 2 bytes
766 * call:32 -> call:16 2 bytes
768 * call:32 -> calls:32 1 byte
769 * call:16 -> calls:16 1 byte
770 * These are done anytime using "calls" would result
771 in smaller code, or when necessary to preserve the
772 meaning of the program.
776 * In some circumstances we can move instructions
777 from a function prologue into a "call" instruction.
778 This is only done if the resulting code is no larger
779 than the original code.
781 * jmp:32 -> jmp:16 2 bytes
782 * jmp:16 -> bra:8 1 byte
784 * If the previous instruction is a conditional branch
785 around the jump/bra, we may be able to reverse its condition
786 and change its target to the jump's target. The jump/bra
787 can then be deleted. 2 bytes
789 * mov abs32 -> mov abs16 1 or 2 bytes
791 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
792 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
794 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
795 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
797 We don't handle imm16->imm8 or d16->d8 as they're very rare
798 and somewhat more difficult to support. */
801 mn10300_elf_relax_section (abfd
, sec
, link_info
, again
)
804 struct bfd_link_info
*link_info
;
807 Elf_Internal_Shdr
*symtab_hdr
;
808 Elf_Internal_Rela
*internal_relocs
= NULL
;
809 Elf_Internal_Rela
*free_relocs
= NULL
;
810 Elf_Internal_Rela
*irel
, *irelend
;
811 bfd_byte
*contents
= NULL
;
812 bfd_byte
*free_contents
= NULL
;
813 Elf32_External_Sym
*extsyms
= NULL
;
814 Elf32_External_Sym
*free_extsyms
= NULL
;
815 struct elf32_mn10300_link_hash_table
*hash_table
;
817 /* Assume nothing changes. */
820 /* We need a pointer to the mn10300 specific hash table. */
821 hash_table
= elf32_mn10300_hash_table (link_info
);
823 /* Initialize fields in each hash table entry the first time through. */
824 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
828 /* Iterate over all the input bfds. */
829 for (input_bfd
= link_info
->input_bfds
;
831 input_bfd
= input_bfd
->link_next
)
835 /* We're going to need all the symbols for each bfd. */
836 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
838 /* Get cached copy if it exists. */
839 if (symtab_hdr
->contents
!= NULL
)
840 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
843 /* Go get them off disk. */
844 extsyms
= ((Elf32_External_Sym
*)
845 bfd_malloc (symtab_hdr
->sh_size
));
848 free_extsyms
= extsyms
;
849 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
850 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
851 != symtab_hdr
->sh_size
))
855 /* Iterate over each section in this bfd. */
856 for (section
= input_bfd
->sections
;
858 section
= section
->next
)
860 struct elf32_mn10300_link_hash_entry
*hash
;
861 Elf_Internal_Sym
*sym
;
862 asection
*sym_sec
= NULL
;
863 const char *sym_name
;
866 /* Get cached copy of section contents if it exists. */
867 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
868 contents
= elf_section_data (section
)->this_hdr
.contents
;
869 else if (section
->_raw_size
!= 0)
871 /* Go get them off disk. */
872 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
873 if (contents
== NULL
)
875 free_contents
= contents
;
877 if (!bfd_get_section_contents (input_bfd
, section
,
878 contents
, (file_ptr
) 0,
885 free_contents
= NULL
;
888 /* If there aren't any relocs, then there's nothing to do. */
889 if ((section
->flags
& SEC_RELOC
) != 0
890 && section
->reloc_count
!= 0)
893 /* Get a copy of the native relocations. */
894 internal_relocs
= (_bfd_elf32_link_read_relocs
895 (input_bfd
, section
, (PTR
) NULL
,
896 (Elf_Internal_Rela
*) NULL
,
897 link_info
->keep_memory
));
898 if (internal_relocs
== NULL
)
900 if (! link_info
->keep_memory
)
901 free_relocs
= internal_relocs
;
903 /* Now examine each relocation. */
904 irel
= internal_relocs
;
905 irelend
= irel
+ section
->reloc_count
;
906 for (; irel
< irelend
; irel
++)
909 unsigned long r_index
;
912 r_type
= ELF32_R_TYPE (irel
->r_info
);
913 r_index
= ELF32_R_SYM (irel
->r_info
);
915 if (r_type
< 0 || r_type
>= (int) R_MN10300_MAX
)
918 /* We need the name and hash table entry of the target
924 if (r_index
< symtab_hdr
->sh_info
)
926 /* A local symbol. */
927 Elf_Internal_Sym isym
;
929 bfd_elf32_swap_symbol_in (input_bfd
,
930 extsyms
+ r_index
, &isym
);
932 if (isym
.st_shndx
== SHN_UNDEF
)
933 sym_sec
= bfd_und_section_ptr
;
934 else if (isym
.st_shndx
> 0
935 && isym
.st_shndx
< SHN_LORESERVE
)
937 = bfd_section_from_elf_index (input_bfd
,
939 else if (isym
.st_shndx
== SHN_ABS
)
940 sym_sec
= bfd_abs_section_ptr
;
941 else if (isym
.st_shndx
== SHN_COMMON
)
942 sym_sec
= bfd_com_section_ptr
;
944 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
948 /* If it isn't a function, then we don't care
950 if (r_index
< symtab_hdr
->sh_info
951 && ELF_ST_TYPE (isym
.st_info
) != STT_FUNC
)
954 /* Tack on an ID so we can uniquely identify this
955 local symbol in the global hash table. */
956 new_name
= bfd_malloc (strlen (sym_name
) + 10);
960 sprintf (new_name
, "%s_%08x",
961 sym_name
, (int) sym_sec
);
964 hash
= (struct elf32_mn10300_link_hash_entry
*)
965 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
972 r_index
-= symtab_hdr
->sh_info
;
973 hash
= (struct elf32_mn10300_link_hash_entry
*)
974 elf_sym_hashes (input_bfd
)[r_index
];
977 /* If this is not a "call" instruction, then we
978 should convert "call" instructions to "calls"
980 code
= bfd_get_8 (input_bfd
,
981 contents
+ irel
->r_offset
- 1);
982 if (code
!= 0xdd && code
!= 0xcd)
983 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
985 /* If this is a jump/call, then bump the direct_calls
986 counter. Else force "call" to "calls" conversions. */
987 if (r_type
== R_MN10300_PCREL32
988 || r_type
== R_MN10300_PCREL16
)
989 hash
->direct_calls
++;
991 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
995 /* Now look at the actual contents to get the stack size,
996 and a list of what registers were saved in the prologue
998 if ((section
->flags
& SEC_CODE
) != 0)
1001 Elf32_External_Sym
*esym
, *esymend
;
1004 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
1007 /* Look at each function defined in this section and
1008 update info for that function. */
1010 esymend
= esym
+ symtab_hdr
->sh_info
;
1011 for (; esym
< esymend
; esym
++)
1013 Elf_Internal_Sym isym
;
1015 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1016 if (isym
.st_shndx
== shndx
1017 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
)
1019 if (isym
.st_shndx
== SHN_UNDEF
)
1020 sym_sec
= bfd_und_section_ptr
;
1021 else if (isym
.st_shndx
> 0
1022 && isym
.st_shndx
< SHN_LORESERVE
)
1024 = bfd_section_from_elf_index (input_bfd
,
1026 else if (isym
.st_shndx
== SHN_ABS
)
1027 sym_sec
= bfd_abs_section_ptr
;
1028 else if (isym
.st_shndx
== SHN_COMMON
)
1029 sym_sec
= bfd_com_section_ptr
;
1031 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1032 symtab_hdr
->sh_link
,
1035 /* Tack on an ID so we can uniquely identify this
1036 local symbol in the global hash table. */
1037 new_name
= bfd_malloc (strlen (sym_name
) + 10);
1041 sprintf (new_name
, "%s_%08x",
1042 sym_name
, (int) sym_sec
);
1043 sym_name
= new_name
;
1045 hash
= (struct elf32_mn10300_link_hash_entry
*)
1046 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1050 compute_function_info (input_bfd
, hash
,
1051 isym
.st_value
, contents
);
1055 esym
= extsyms
+ symtab_hdr
->sh_info
;
1056 esymend
= extsyms
+ (symtab_hdr
->sh_size
1057 / sizeof (Elf32_External_Sym
));
1058 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1060 Elf_Internal_Sym isym
;
1062 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1063 hash
= (struct elf32_mn10300_link_hash_entry
*)
1064 elf_sym_hashes (input_bfd
)[idx
];
1065 if (isym
.st_shndx
== shndx
1066 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
1067 && (hash
)->root
.root
.u
.def
.section
== section
1068 && ((hash
)->root
.root
.type
== bfd_link_hash_defined
1069 || (hash
)->root
.root
.type
== bfd_link_hash_defweak
))
1070 compute_function_info (input_bfd
, hash
,
1071 (hash
)->root
.root
.u
.def
.value
,
1076 /* Cache or free any memory we allocated for the relocs. */
1077 if (free_relocs
!= NULL
)
1083 /* Cache or free any memory we allocated for the contents. */
1084 if (free_contents
!= NULL
)
1086 if (! link_info
->keep_memory
)
1087 free (free_contents
);
1090 /* Cache the section contents for elf_link_input_bfd. */
1091 elf_section_data (section
)->this_hdr
.contents
= contents
;
1093 free_contents
= NULL
;
1097 /* Cache or free any memory we allocated for the symbols. */
1098 if (free_extsyms
!= NULL
)
1100 if (! link_info
->keep_memory
)
1101 free (free_extsyms
);
1104 /* Cache the symbols for elf_link_input_bfd. */
1105 symtab_hdr
->contents
= extsyms
;
1107 free_extsyms
= NULL
;
1111 /* Now iterate on each symbol in the hash table and perform
1112 the final initialization steps on each. */
1113 elf32_mn10300_link_hash_traverse (hash_table
,
1114 elf32_mn10300_finish_hash_table_entry
,
1116 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
1117 elf32_mn10300_finish_hash_table_entry
,
1120 /* All entries in the hash table are fully initialized. */
1121 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
1123 /* Now that everything has been initialized, go through each
1124 code section and delete any prologue insns which will be
1125 redundant because their operations will be performed by
1126 a "call" instruction. */
1127 for (input_bfd
= link_info
->input_bfds
;
1129 input_bfd
= input_bfd
->link_next
)
1133 /* We're going to need all the symbols for each bfd. */
1134 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1136 /* Get cached copy if it exists. */
1137 if (symtab_hdr
->contents
!= NULL
)
1138 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1141 /* Go get them off disk. */
1142 extsyms
= ((Elf32_External_Sym
*)
1143 bfd_malloc (symtab_hdr
->sh_size
));
1144 if (extsyms
== NULL
)
1146 free_extsyms
= extsyms
;
1147 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1148 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
1149 != symtab_hdr
->sh_size
))
1153 /* Walk over each section in this bfd. */
1154 for (section
= input_bfd
->sections
;
1156 section
= section
->next
)
1159 Elf32_External_Sym
*esym
, *esymend
;
1162 /* Skip non-code sections and empty sections. */
1163 if ((section
->flags
& SEC_CODE
) == 0 || section
->_raw_size
== 0)
1166 if (section
->reloc_count
!= 0)
1168 /* Get a copy of the native relocations. */
1169 internal_relocs
= (_bfd_elf32_link_read_relocs
1170 (input_bfd
, section
, (PTR
) NULL
,
1171 (Elf_Internal_Rela
*) NULL
,
1172 link_info
->keep_memory
));
1173 if (internal_relocs
== NULL
)
1175 if (! link_info
->keep_memory
)
1176 free_relocs
= internal_relocs
;
1179 /* Get cached copy of section contents if it exists. */
1180 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
1181 contents
= elf_section_data (section
)->this_hdr
.contents
;
1184 /* Go get them off disk. */
1185 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
1186 if (contents
== NULL
)
1188 free_contents
= contents
;
1190 if (!bfd_get_section_contents (input_bfd
, section
,
1191 contents
, (file_ptr
) 0,
1192 section
->_raw_size
))
1196 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
, section
);
1198 /* Now look for any function in this section which needs
1199 insns deleted from its prologue. */
1201 esymend
= esym
+ symtab_hdr
->sh_info
;
1202 for (; esym
< esymend
; esym
++)
1204 Elf_Internal_Sym isym
;
1205 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1206 asection
*sym_sec
= NULL
;
1207 const char *sym_name
;
1210 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1212 if (isym
.st_shndx
!= shndx
)
1215 if (isym
.st_shndx
== SHN_UNDEF
)
1216 sym_sec
= bfd_und_section_ptr
;
1217 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1219 = bfd_section_from_elf_index (input_bfd
, isym
.st_shndx
);
1220 else if (isym
.st_shndx
== SHN_ABS
)
1221 sym_sec
= bfd_abs_section_ptr
;
1222 else if (isym
.st_shndx
== SHN_COMMON
)
1223 sym_sec
= bfd_com_section_ptr
;
1227 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1228 symtab_hdr
->sh_link
,
1231 /* Tack on an ID so we can uniquely identify this
1232 local symbol in the global hash table. */
1233 new_name
= bfd_malloc (strlen (sym_name
) + 10);
1236 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
1237 sym_name
= new_name
;
1239 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1240 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1245 if (sym_hash
== NULL
)
1248 if (! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1249 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1253 /* Note that we've changed things. */
1254 elf_section_data (section
)->relocs
= internal_relocs
;
1257 elf_section_data (section
)->this_hdr
.contents
= contents
;
1258 free_contents
= NULL
;
1260 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1261 free_extsyms
= NULL
;
1263 /* Count how many bytes we're going to delete. */
1264 if (sym_hash
->movm_args
)
1267 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1269 else if (sym_hash
->stack_size
1270 && sym_hash
->stack_size
< 256)
1273 /* Note that we've deleted prologue bytes for this
1275 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1277 /* Actually delete the bytes. */
1278 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1284 /* Something changed. Not strictly necessary, but
1285 may lead to more relaxing opportunities. */
1290 /* Look for any global functions in this section which
1291 need insns deleted from their prologues. */
1292 esym
= extsyms
+ symtab_hdr
->sh_info
;
1293 esymend
= extsyms
+ (symtab_hdr
->sh_size
1294 / sizeof (Elf32_External_Sym
));
1295 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1297 Elf_Internal_Sym isym
;
1298 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1300 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1301 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1302 (elf_sym_hashes (input_bfd
)[idx
]);
1303 if (isym
.st_shndx
== shndx
1304 && (sym_hash
)->root
.root
.u
.def
.section
== section
1305 && ! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1306 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1310 /* Note that we've changed things. */
1311 elf_section_data (section
)->relocs
= internal_relocs
;
1314 elf_section_data (section
)->this_hdr
.contents
= contents
;
1315 free_contents
= NULL
;
1317 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1318 free_extsyms
= NULL
;
1320 /* Count how many bytes we're going to delete. */
1321 if (sym_hash
->movm_args
)
1324 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1326 else if (sym_hash
->stack_size
1327 && sym_hash
->stack_size
< 256)
1330 /* Note that we've deleted prologue bytes for this
1332 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1334 /* Actually delete the bytes. */
1335 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1337 (sym_hash
)->root
.root
.u
.def
.value
,
1341 /* Something changed. Not strictly necessary, but
1342 may lead to more relaxing opportunities. */
1347 /* Cache or free any memory we allocated for the relocs. */
1348 if (free_relocs
!= NULL
)
1354 /* Cache or free any memory we allocated for the contents. */
1355 if (free_contents
!= NULL
)
1357 if (! link_info
->keep_memory
)
1358 free (free_contents
);
1361 /* Cache the section contents for elf_link_input_bfd. */
1362 elf_section_data (section
)->this_hdr
.contents
= contents
;
1364 free_contents
= NULL
;
1368 /* Cache or free any memory we allocated for the symbols. */
1369 if (free_extsyms
!= NULL
)
1371 if (! link_info
->keep_memory
)
1372 free (free_extsyms
);
1375 /* Cache the symbols for elf_link_input_bfd. */
1376 symtab_hdr
->contents
= extsyms
;
1378 free_extsyms
= NULL
;
1383 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1386 internal_relocs
= NULL
;
1388 free_contents
= NULL
;
1389 free_extsyms
= NULL
;
1391 /* We don't have to do anything for a relocateable link, if
1392 this section does not have relocs, or if this is not a
1394 if (link_info
->relocateable
1395 || (sec
->flags
& SEC_RELOC
) == 0
1396 || sec
->reloc_count
== 0
1397 || (sec
->flags
& SEC_CODE
) == 0)
1400 /* If this is the first time we have been called for this section,
1401 initialize the cooked size. */
1402 if (sec
->_cooked_size
== 0)
1403 sec
->_cooked_size
= sec
->_raw_size
;
1405 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1407 /* Get a copy of the native relocations. */
1408 internal_relocs
= (_bfd_elf32_link_read_relocs
1409 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1410 link_info
->keep_memory
));
1411 if (internal_relocs
== NULL
)
1413 if (! link_info
->keep_memory
)
1414 free_relocs
= internal_relocs
;
1416 /* Walk through them looking for relaxing opportunities. */
1417 irelend
= internal_relocs
+ sec
->reloc_count
;
1418 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1421 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
1423 /* If this isn't something that can be relaxed, then ignore
1425 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
1426 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
1427 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
1430 /* Get the section contents if we haven't done so already. */
1431 if (contents
== NULL
)
1433 /* Get cached copy if it exists. */
1434 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1435 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1438 /* Go get them off disk. */
1439 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1440 if (contents
== NULL
)
1442 free_contents
= contents
;
1444 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1445 (file_ptr
) 0, sec
->_raw_size
))
1450 /* Read this BFD's symbols if we haven't done so already. */
1451 if (extsyms
== NULL
)
1453 /* Get cached copy if it exists. */
1454 if (symtab_hdr
->contents
!= NULL
)
1455 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1458 /* Go get them off disk. */
1459 extsyms
= ((Elf32_External_Sym
*)
1460 bfd_malloc (symtab_hdr
->sh_size
));
1461 if (extsyms
== NULL
)
1463 free_extsyms
= extsyms
;
1464 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1465 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1466 != symtab_hdr
->sh_size
))
1471 /* Get the value of the symbol referred to by the reloc. */
1472 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1474 Elf_Internal_Sym isym
;
1475 asection
*sym_sec
= NULL
;
1476 const char *sym_name
;
1479 /* A local symbol. */
1480 bfd_elf32_swap_symbol_in (abfd
,
1481 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1484 if (isym
.st_shndx
== SHN_UNDEF
)
1485 sym_sec
= bfd_und_section_ptr
;
1486 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1487 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
1488 else if (isym
.st_shndx
== SHN_ABS
)
1489 sym_sec
= bfd_abs_section_ptr
;
1490 else if (isym
.st_shndx
== SHN_COMMON
)
1491 sym_sec
= bfd_com_section_ptr
;
1495 symval
= (isym
.st_value
1496 + sym_sec
->output_section
->vma
1497 + sym_sec
->output_offset
);
1498 sym_name
= bfd_elf_string_from_elf_section (abfd
,
1499 symtab_hdr
->sh_link
,
1502 /* Tack on an ID so we can uniquely identify this
1503 local symbol in the global hash table. */
1504 new_name
= bfd_malloc (strlen (sym_name
) + 10);
1507 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
1508 sym_name
= new_name
;
1510 h
= (struct elf32_mn10300_link_hash_entry
*)
1511 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1512 sym_name
, false, false, false);
1519 /* An external symbol. */
1520 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1521 h
= (struct elf32_mn10300_link_hash_entry
*)
1522 (elf_sym_hashes (abfd
)[indx
]);
1523 BFD_ASSERT (h
!= NULL
);
1524 if (h
->root
.root
.type
!= bfd_link_hash_defined
1525 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
1527 /* This appears to be a reference to an undefined
1528 symbol. Just ignore it--it will be caught by the
1529 regular reloc processing. */
1533 symval
= (h
->root
.root
.u
.def
.value
1534 + h
->root
.root
.u
.def
.section
->output_section
->vma
1535 + h
->root
.root
.u
.def
.section
->output_offset
);
1538 /* For simplicity of coding, we are going to modify the section
1539 contents, the section relocs, and the BFD symbol table. We
1540 must tell the rest of the code not to free up this
1541 information. It would be possible to instead create a table
1542 of changes which have to be made, as is done in coff-mips.c;
1543 that would be more work, but would require less memory when
1544 the linker is run. */
1546 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1547 branch/call, also deal with "call" -> "calls" conversions and
1548 insertion of prologue data into "call" instructions. */
1549 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
)
1551 bfd_vma value
= symval
;
1553 /* If we've got a "call" instruction that needs to be turned
1554 into a "calls" instruction, do so now. It saves a byte. */
1555 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1559 /* Get the opcode. */
1560 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1562 /* Make sure we're working with a "call" instruction! */
1565 /* Note that we've changed the relocs, section contents,
1567 elf_section_data (sec
)->relocs
= internal_relocs
;
1570 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1571 free_contents
= NULL
;
1573 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1574 free_extsyms
= NULL
;
1576 /* Fix the opcode. */
1577 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
1578 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1580 /* Fix irel->r_offset and irel->r_addend. */
1581 irel
->r_offset
+= 1;
1582 irel
->r_addend
+= 1;
1584 /* Delete one byte of data. */
1585 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1586 irel
->r_offset
+ 3, 1))
1589 /* That will change things, so, we should relax again.
1590 Note that this is not required, and it may be slow. */
1596 /* We've got a "call" instruction which needs some data
1597 from target function filled in. */
1600 /* Get the opcode. */
1601 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1603 /* Insert data from the target function into the "call"
1604 instruction if needed. */
1607 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
1608 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
1609 contents
+ irel
->r_offset
+ 5);
1613 /* Deal with pc-relative gunk. */
1614 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1615 value
-= irel
->r_offset
;
1616 value
+= irel
->r_addend
;
1618 /* See if the value will fit in 16 bits, note the high value is
1619 0x7fff + 2 as the target will be two bytes closer if we are
1621 if ((long) value
< 0x8001 && (long) value
> -0x8000)
1625 /* Get the opcode. */
1626 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1628 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
1631 /* Note that we've changed the relocs, section contents, etc. */
1632 elf_section_data (sec
)->relocs
= internal_relocs
;
1635 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1636 free_contents
= NULL
;
1638 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1639 free_extsyms
= NULL
;
1641 /* Fix the opcode. */
1643 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
1644 else if (code
== 0xdd)
1645 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
1646 else if (code
== 0xff)
1647 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
1649 /* Fix the relocation's type. */
1650 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1653 /* Delete two bytes of data. */
1654 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1655 irel
->r_offset
+ 1, 2))
1658 /* That will change things, so, we should relax again.
1659 Note that this is not required, and it may be slow. */
1664 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1666 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
1668 bfd_vma value
= symval
;
1670 /* If we've got a "call" instruction that needs to be turned
1671 into a "calls" instruction, do so now. It saves a byte. */
1672 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1676 /* Get the opcode. */
1677 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1679 /* Make sure we're working with a "call" instruction! */
1682 /* Note that we've changed the relocs, section contents,
1684 elf_section_data (sec
)->relocs
= internal_relocs
;
1687 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1688 free_contents
= NULL
;
1690 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1691 free_extsyms
= NULL
;
1693 /* Fix the opcode. */
1694 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
1695 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1697 /* Fix irel->r_offset and irel->r_addend. */
1698 irel
->r_offset
+= 1;
1699 irel
->r_addend
+= 1;
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. */
1715 /* Get the opcode. */
1716 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1718 /* Insert data from the target function into the "call"
1719 instruction if needed. */
1722 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
1723 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
1724 contents
+ irel
->r_offset
+ 3);
1728 /* Deal with pc-relative gunk. */
1729 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1730 value
-= irel
->r_offset
;
1731 value
+= irel
->r_addend
;
1733 /* See if the value will fit in 8 bits, note the high value is
1734 0x7f + 1 as the target will be one bytes closer if we are
1736 if ((long) value
< 0x80 && (long) value
> -0x80)
1740 /* Get the opcode. */
1741 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1746 /* Note that we've changed the relocs, section contents, etc. */
1747 elf_section_data (sec
)->relocs
= internal_relocs
;
1750 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1751 free_contents
= NULL
;
1753 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1754 free_extsyms
= NULL
;
1756 /* Fix the opcode. */
1757 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
1759 /* Fix the relocation's type. */
1760 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1763 /* Delete one byte of data. */
1764 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1765 irel
->r_offset
+ 1, 1))
1768 /* That will change things, so, we should relax again.
1769 Note that this is not required, and it may be slow. */
1774 /* Try to eliminate an unconditional 8 bit pc-relative branch
1775 which immediately follows a conditional 8 bit pc-relative
1776 branch around the unconditional branch.
1783 This happens when the bCC can't reach lab2 at assembly time,
1784 but due to other relaxations it can reach at link time. */
1785 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
1787 Elf_Internal_Rela
*nrel
;
1788 bfd_vma value
= symval
;
1791 /* Deal with pc-relative gunk. */
1792 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1793 value
-= irel
->r_offset
;
1794 value
+= irel
->r_addend
;
1796 /* Do nothing if this reloc is the last byte in the section. */
1797 if (irel
->r_offset
== sec
->_cooked_size
)
1800 /* See if the next instruction is an unconditional pc-relative
1801 branch, more often than not this test will fail, so we
1802 test it first to speed things up. */
1803 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1807 /* Also make sure the next relocation applies to the next
1808 instruction and that it's a pc-relative 8 bit branch. */
1811 || irel
->r_offset
+ 2 != nrel
->r_offset
1812 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
1815 /* Make sure our destination immediately follows the
1816 unconditional branch. */
1817 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
1818 + irel
->r_offset
+ 3))
1821 /* Now make sure we are a conditional branch. This may not
1822 be necessary, but why take the chance.
1824 Note these checks assume that R_MN10300_PCREL8 relocs
1825 only occur on bCC and bCCx insns. If they occured
1826 elsewhere, we'd need to know the start of this insn
1827 for this check to be accurate. */
1828 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1829 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
1830 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
1831 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
1832 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
1833 && code
!= 0xea && code
!= 0xeb)
1836 /* We also have to be sure there is no symbol/label
1837 at the unconditional branch. */
1838 if (mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
,
1839 irel
->r_offset
+ 1))
1842 /* Note that we've changed the relocs, section contents, etc. */
1843 elf_section_data (sec
)->relocs
= internal_relocs
;
1846 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1847 free_contents
= NULL
;
1849 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1850 free_extsyms
= NULL
;
1852 /* Reverse the condition of the first branch. */
1898 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1900 /* Set the reloc type and symbol for the first branch
1901 from the second branch. */
1902 irel
->r_info
= nrel
->r_info
;
1904 /* Make the reloc for the second branch a null reloc. */
1905 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
1908 /* Delete two bytes of data. */
1909 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1910 irel
->r_offset
+ 1, 2))
1913 /* That will change things, so, we should relax again.
1914 Note that this is not required, and it may be slow. */
1918 /* Try to turn a 24 immediate, displacement or absolute address
1919 into a 8 immediate, displacement or absolute address. */
1920 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_24
)
1922 bfd_vma value
= symval
;
1923 value
+= irel
->r_addend
;
1925 /* See if the value will fit in 8 bits. */
1926 if ((long) value
< 0x7f && (long) value
> -0x80)
1930 /* AM33 insns which have 24 operands are 6 bytes long and
1931 will have 0xfd as the first byte. */
1933 /* Get the first opcode. */
1934 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
1938 /* Get the second opcode. */
1939 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1941 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1942 equivalent instructions exists. */
1943 if (code
!= 0x6b && code
!= 0x7b
1944 && code
!= 0x8b && code
!= 0x9b
1945 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
1946 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
1947 || (code
& 0x0f) == 0x0e))
1949 /* Not safe if the high bit is on as relaxing may
1950 move the value out of high mem and thus not fit
1951 in a signed 8bit value. This is currently over
1953 if ((value
& 0x80) == 0)
1955 /* Note that we've changed the relocation contents,
1957 elf_section_data (sec
)->relocs
= internal_relocs
;
1960 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1961 free_contents
= NULL
;
1963 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1964 free_extsyms
= NULL
;
1966 /* Fix the opcode. */
1967 bfd_put_8 (abfd
, 0xfb, contents
+ irel
->r_offset
- 3);
1968 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1970 /* Fix the relocation's type. */
1972 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1975 /* Delete two bytes of data. */
1976 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1977 irel
->r_offset
+ 1, 2))
1980 /* That will change things, so, we should relax
1981 again. Note that this is not required, and it
1991 /* Try to turn a 32bit immediate, displacement or absolute address
1992 into a 16bit immediate, displacement or absolute address. */
1993 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
)
1995 bfd_vma value
= symval
;
1996 value
+= irel
->r_addend
;
1998 /* See if the value will fit in 24 bits.
1999 We allow any 16bit match here. We prune those we can't
2001 if ((long) value
< 0x7fffff && (long) value
> -0x800000)
2005 /* AM33 insns which have 32bit operands are 7 bytes long and
2006 will have 0xfe as the first byte. */
2008 /* Get the first opcode. */
2009 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
2013 /* Get the second opcode. */
2014 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2016 /* All the am33 32 -> 24 relaxing possibilities. */
2017 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2018 equivalent instructions exists. */
2019 if (code
!= 0x6b && code
!= 0x7b
2020 && code
!= 0x8b && code
!= 0x9b
2021 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
2022 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
2023 || (code
& 0x0f) == 0x0e))
2025 /* Not safe if the high bit is on as relaxing may
2026 move the value out of high mem and thus not fit
2027 in a signed 16bit value. This is currently over
2029 if ((value
& 0x8000) == 0)
2031 /* Note that we've changed the relocation contents,
2033 elf_section_data (sec
)->relocs
= internal_relocs
;
2036 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2037 free_contents
= NULL
;
2039 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2040 free_extsyms
= NULL
;
2042 /* Fix the opcode. */
2043 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 3);
2044 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2046 /* Fix the relocation's type. */
2048 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2051 /* Delete one byte of data. */
2052 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2053 irel
->r_offset
+ 3, 1))
2056 /* That will change things, so, we should relax
2057 again. Note that this is not required, and it
2066 /* See if the value will fit in 16 bits.
2067 We allow any 16bit match here. We prune those we can't
2069 if ((long) value
< 0x7fff && (long) value
> -0x8000)
2073 /* Most insns which have 32bit operands are 6 bytes long;
2074 exceptions are pcrel insns and bit insns.
2076 We handle pcrel insns above. We don't bother trying
2077 to handle the bit insns here.
2079 The first byte of the remaining insns will be 0xfc. */
2081 /* Get the first opcode. */
2082 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2087 /* Get the second opcode. */
2088 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2090 if ((code
& 0xf0) < 0x80)
2091 switch (code
& 0xf0)
2093 /* mov (d32,am),dn -> mov (d32,am),dn
2094 mov dm,(d32,am) -> mov dn,(d32,am)
2095 mov (d32,am),an -> mov (d32,am),an
2096 mov dm,(d32,am) -> mov dn,(d32,am)
2097 movbu (d32,am),dn -> movbu (d32,am),dn
2098 movbu dm,(d32,am) -> movbu dn,(d32,am)
2099 movhu (d32,am),dn -> movhu (d32,am),dn
2100 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2109 /* Not safe if the high bit is on as relaxing may
2110 move the value out of high mem and thus not fit
2111 in a signed 16bit value. */
2113 && (value
& 0x8000))
2116 /* Note that we've changed the relocation contents, etc. */
2117 elf_section_data (sec
)->relocs
= internal_relocs
;
2120 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2121 free_contents
= NULL
;
2123 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2124 free_extsyms
= NULL
;
2126 /* Fix the opcode. */
2127 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2128 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2130 /* Fix the relocation's type. */
2131 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2134 /* Delete two bytes of data. */
2135 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2136 irel
->r_offset
+ 2, 2))
2139 /* That will change things, so, we should relax again.
2140 Note that this is not required, and it may be slow. */
2144 else if ((code
& 0xf0) == 0x80
2145 || (code
& 0xf0) == 0x90)
2146 switch (code
& 0xf3)
2148 /* mov dn,(abs32) -> mov dn,(abs16)
2149 movbu dn,(abs32) -> movbu dn,(abs16)
2150 movhu dn,(abs32) -> movhu dn,(abs16) */
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 if ((code
& 0xf3) == 0x81)
2165 code
= 0x01 + (code
& 0x0c);
2166 else if ((code
& 0xf3) == 0x82)
2167 code
= 0x02 + (code
& 0x0c);
2168 else if ((code
& 0xf3) == 0x83)
2169 code
= 0x03 + (code
& 0x0c);
2173 /* Fix the opcode. */
2174 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2176 /* Fix the relocation's type. */
2177 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2180 /* The opcode got shorter too, so we have to fix the
2181 addend and offset too! */
2182 irel
->r_offset
-= 1;
2184 /* Delete three bytes of data. */
2185 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2186 irel
->r_offset
+ 1, 3))
2189 /* That will change things, so, we should relax again.
2190 Note that this is not required, and it may be slow. */
2194 /* mov am,(abs32) -> mov am,(abs16)
2195 mov am,(d32,sp) -> mov am,(d16,sp)
2196 mov dm,(d32,sp) -> mov dm,(d32,sp)
2197 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2198 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2204 /* sp-based offsets are zero-extended. */
2205 if (code
>= 0x90 && code
<= 0x93
2209 /* Note that we've changed the relocation contents, etc. */
2210 elf_section_data (sec
)->relocs
= internal_relocs
;
2213 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2214 free_contents
= NULL
;
2216 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2217 free_extsyms
= NULL
;
2219 /* Fix the opcode. */
2220 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2221 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2223 /* Fix the relocation's type. */
2224 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2227 /* Delete two bytes of data. */
2228 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2229 irel
->r_offset
+ 2, 2))
2232 /* That will change things, so, we should relax again.
2233 Note that this is not required, and it may be slow. */
2237 else if ((code
& 0xf0) < 0xf0)
2238 switch (code
& 0xfc)
2240 /* mov imm32,dn -> mov imm16,dn
2241 mov imm32,an -> mov imm16,an
2242 mov (abs32),dn -> mov (abs16),dn
2243 movbu (abs32),dn -> movbu (abs16),dn
2244 movhu (abs32),dn -> movhu (abs16),dn */
2250 /* Not safe if the high bit is on as relaxing may
2251 move the value out of high mem and thus not fit
2252 in a signed 16bit value. */
2254 && (value
& 0x8000))
2257 /* mov imm16, an zero-extends the immediate. */
2262 /* Note that we've changed the relocation contents, etc. */
2263 elf_section_data (sec
)->relocs
= internal_relocs
;
2266 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2267 free_contents
= NULL
;
2269 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2270 free_extsyms
= NULL
;
2272 if ((code
& 0xfc) == 0xcc)
2273 code
= 0x2c + (code
& 0x03);
2274 else if ((code
& 0xfc) == 0xdc)
2275 code
= 0x24 + (code
& 0x03);
2276 else if ((code
& 0xfc) == 0xa4)
2277 code
= 0x30 + (code
& 0x03);
2278 else if ((code
& 0xfc) == 0xa8)
2279 code
= 0x34 + (code
& 0x03);
2280 else if ((code
& 0xfc) == 0xac)
2281 code
= 0x38 + (code
& 0x03);
2285 /* Fix the opcode. */
2286 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2288 /* Fix the relocation's type. */
2289 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2292 /* The opcode got shorter too, so we have to fix the
2293 addend and offset too! */
2294 irel
->r_offset
-= 1;
2296 /* Delete three bytes of data. */
2297 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2298 irel
->r_offset
+ 1, 3))
2301 /* That will change things, so, we should relax again.
2302 Note that this is not required, and it may be slow. */
2306 /* mov (abs32),an -> mov (abs16),an
2307 mov (d32,sp),an -> mov (d16,sp),an
2308 mov (d32,sp),dn -> mov (d16,sp),dn
2309 movbu (d32,sp),dn -> movbu (d16,sp),dn
2310 movhu (d32,sp),dn -> movhu (d16,sp),dn
2311 add imm32,dn -> add imm16,dn
2312 cmp imm32,dn -> cmp imm16,dn
2313 add imm32,an -> add imm16,an
2314 cmp imm32,an -> cmp imm16,an
2315 and imm32,dn -> and imm16,dn
2316 or imm32,dn -> or imm16,dn
2317 xor imm32,dn -> xor imm16,dn
2318 btst imm32,dn -> btst imm16,dn */
2334 /* cmp imm16, an zero-extends the immediate. */
2339 /* So do sp-based offsets. */
2340 if (code
>= 0xb0 && code
<= 0xb3
2344 /* Note that we've changed the relocation contents, etc. */
2345 elf_section_data (sec
)->relocs
= internal_relocs
;
2348 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2349 free_contents
= NULL
;
2351 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2352 free_extsyms
= NULL
;
2354 /* Fix the opcode. */
2355 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2356 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2358 /* Fix the relocation's type. */
2359 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2362 /* Delete two bytes of data. */
2363 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2364 irel
->r_offset
+ 2, 2))
2367 /* That will change things, so, we should relax again.
2368 Note that this is not required, and it may be slow. */
2372 else if (code
== 0xfe)
2374 /* add imm32,sp -> add imm16,sp */
2376 /* Note that we've changed the relocation contents, etc. */
2377 elf_section_data (sec
)->relocs
= internal_relocs
;
2380 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2381 free_contents
= NULL
;
2383 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2384 free_extsyms
= NULL
;
2386 /* Fix the opcode. */
2387 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2388 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
2390 /* Fix the relocation's type. */
2391 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2394 /* Delete two bytes of data. */
2395 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2396 irel
->r_offset
+ 2, 2))
2399 /* That will change things, so, we should relax again.
2400 Note that this is not required, and it may be slow. */
2408 if (free_relocs
!= NULL
)
2414 if (free_contents
!= NULL
)
2416 if (! link_info
->keep_memory
)
2417 free (free_contents
);
2420 /* Cache the section contents for elf_link_input_bfd. */
2421 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2423 free_contents
= NULL
;
2426 if (free_extsyms
!= NULL
)
2428 if (! link_info
->keep_memory
)
2429 free (free_extsyms
);
2432 /* Cache the symbols for elf_link_input_bfd. */
2433 symtab_hdr
->contents
= extsyms
;
2435 free_extsyms
= NULL
;
2441 if (free_relocs
!= NULL
)
2443 if (free_contents
!= NULL
)
2444 free (free_contents
);
2445 if (free_extsyms
!= NULL
)
2446 free (free_extsyms
);
2450 /* Compute the stack size and movm arguments for the function
2451 referred to by HASH at address ADDR in section with
2452 contents CONTENTS, store the information in the hash table. */
2454 compute_function_info (abfd
, hash
, addr
, contents
)
2456 struct elf32_mn10300_link_hash_entry
*hash
;
2458 unsigned char *contents
;
2460 unsigned char byte1
, byte2
;
2461 /* We only care about a very small subset of the possible prologue
2462 sequences here. Basically we look for:
2464 movm [d2,d3,a2,a3],sp (optional)
2465 add <size>,sp (optional, and only for sizes which fit in an unsigned
2468 If we find anything else, we quit. */
2470 /* Look for movm [regs],sp */
2471 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2472 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2476 hash
->movm_args
= byte2
;
2478 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2479 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2482 /* Now figure out how much stack space will be allocated by the movm
2483 instruction. We need this kept separate from the funtion's normal
2485 if (hash
->movm_args
)
2488 if (hash
->movm_args
& 0x80)
2489 hash
->movm_stack_size
+= 4;
2492 if (hash
->movm_args
& 0x40)
2493 hash
->movm_stack_size
+= 4;
2496 if (hash
->movm_args
& 0x20)
2497 hash
->movm_stack_size
+= 4;
2500 if (hash
->movm_args
& 0x10)
2501 hash
->movm_stack_size
+= 4;
2503 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2504 if (hash
->movm_args
& 0x08)
2505 hash
->movm_stack_size
+= 8 * 4;
2507 if (bfd_get_mach (abfd
) == bfd_mach_am33
)
2509 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2510 if (hash
->movm_args
& 0x1)
2511 hash
->movm_stack_size
+= 6 * 4;
2513 /* exreg1 space. e4, e5, e6, e7 */
2514 if (hash
->movm_args
& 0x2)
2515 hash
->movm_stack_size
+= 4 * 4;
2517 /* exreg0 space. e2, e3 */
2518 if (hash
->movm_args
& 0x4)
2519 hash
->movm_stack_size
+= 2 * 4;
2523 /* Now look for the two stack adjustment variants. */
2524 if (byte1
== 0xf8 && byte2
== 0xfe)
2526 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
2527 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
2529 hash
->stack_size
= -temp
;
2531 else if (byte1
== 0xfa && byte2
== 0xfe)
2533 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
2534 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
2538 hash
->stack_size
= temp
;
2541 /* If the total stack to be allocated by the call instruction is more
2542 than 255 bytes, then we can't remove the stack adjustment by using
2543 "call" (we might still be able to remove the "movm" instruction. */
2544 if (hash
->stack_size
+ hash
->movm_stack_size
> 255)
2545 hash
->stack_size
= 0;
2550 /* Delete some bytes from a section while relaxing. */
2553 mn10300_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
2559 Elf_Internal_Shdr
*symtab_hdr
;
2560 Elf32_External_Sym
*extsyms
;
2563 Elf_Internal_Rela
*irel
, *irelend
;
2564 Elf_Internal_Rela
*irelalign
;
2566 Elf32_External_Sym
*esym
, *esymend
;
2567 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2569 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2570 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2572 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2574 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2576 /* The deletion must stop at the next ALIGN reloc for an aligment
2577 power larger than the number of bytes we are deleting. */
2580 toaddr
= sec
->_cooked_size
;
2582 irel
= elf_section_data (sec
)->relocs
;
2583 irelend
= irel
+ sec
->reloc_count
;
2585 /* Actually delete the bytes. */
2586 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
2587 sec
->_cooked_size
-= count
;
2589 /* Adjust all the relocs. */
2590 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
2592 /* Get the new reloc address. */
2593 if ((irel
->r_offset
> addr
2594 && irel
->r_offset
< toaddr
))
2595 irel
->r_offset
-= count
;
2598 /* Adjust the local symbols defined in this section. */
2600 esymend
= esym
+ symtab_hdr
->sh_info
;
2601 for (; esym
< esymend
; esym
++)
2603 Elf_Internal_Sym isym
;
2605 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2607 if (isym
.st_shndx
== shndx
2608 && isym
.st_value
> addr
2609 && isym
.st_value
< toaddr
)
2611 isym
.st_value
-= count
;
2612 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
2616 /* Now adjust the global symbols defined in this section. */
2617 esym
= extsyms
+ symtab_hdr
->sh_info
;
2618 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
2619 for (index
= 0; esym
< esymend
; esym
++, index
++)
2621 Elf_Internal_Sym isym
;
2623 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2624 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
2625 (elf_sym_hashes (abfd
)[index
]);
2626 if (isym
.st_shndx
== shndx
2627 && ((sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2628 || (sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2629 && (sym_hash
)->root
.root
.u
.def
.section
== sec
2630 && (sym_hash
)->root
.root
.u
.def
.value
> addr
2631 && (sym_hash
)->root
.root
.u
.def
.value
< toaddr
)
2633 (sym_hash
)->root
.root
.u
.def
.value
-= count
;
2640 /* Return true if a symbol exists at the given address, else return
2643 mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
2646 Elf32_External_Sym
*extsyms
;
2649 Elf_Internal_Shdr
*symtab_hdr
;
2651 Elf32_External_Sym
*esym
, *esymend
;
2652 struct elf32_mn10300_link_hash_entry
**sym_hash
, **sym_hash_end
;
2654 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2655 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2657 /* Examine all the symbols. */
2659 esymend
= esym
+ symtab_hdr
->sh_info
;
2660 for (; esym
< esymend
; esym
++)
2662 Elf_Internal_Sym isym
;
2664 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2666 if (isym
.st_shndx
== shndx
2667 && isym
.st_value
== addr
)
2671 sym_hash
= (struct elf32_mn10300_link_hash_entry
**) (elf_sym_hashes (abfd
));
2672 sym_hash_end
= (sym_hash
2673 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2674 - symtab_hdr
->sh_info
));
2675 for (; sym_hash
< sym_hash_end
; sym_hash
++)
2677 if (((*sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2678 || (*sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2679 && (*sym_hash
)->root
.root
.u
.def
.section
== sec
2680 && (*sym_hash
)->root
.root
.u
.def
.value
== addr
)
2686 /* This is a version of bfd_generic_get_relocated_section_contents
2687 which uses mn10300_elf_relocate_section. */
2690 mn10300_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
2691 data
, relocateable
, symbols
)
2693 struct bfd_link_info
*link_info
;
2694 struct bfd_link_order
*link_order
;
2696 boolean relocateable
;
2699 Elf_Internal_Shdr
*symtab_hdr
;
2700 asection
*input_section
= link_order
->u
.indirect
.section
;
2701 bfd
*input_bfd
= input_section
->owner
;
2702 asection
**sections
= NULL
;
2703 Elf_Internal_Rela
*internal_relocs
= NULL
;
2704 Elf32_External_Sym
*external_syms
= NULL
;
2705 Elf_Internal_Sym
*internal_syms
= NULL
;
2707 /* We only need to handle the case of relaxing, or of having a
2708 particular set of section contents, specially. */
2710 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
2711 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
2716 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2718 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
2719 input_section
->_raw_size
);
2721 if ((input_section
->flags
& SEC_RELOC
) != 0
2722 && input_section
->reloc_count
> 0)
2724 Elf_Internal_Sym
*isymp
;
2726 Elf32_External_Sym
*esym
, *esymend
;
2728 if (symtab_hdr
->contents
!= NULL
)
2729 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2732 external_syms
= ((Elf32_External_Sym
*)
2733 bfd_malloc (symtab_hdr
->sh_info
2734 * sizeof (Elf32_External_Sym
)));
2735 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2737 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
2738 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
2739 symtab_hdr
->sh_info
, input_bfd
)
2740 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
2744 internal_relocs
= (_bfd_elf32_link_read_relocs
2745 (input_bfd
, input_section
, (PTR
) NULL
,
2746 (Elf_Internal_Rela
*) NULL
, false));
2747 if (internal_relocs
== NULL
)
2750 internal_syms
= ((Elf_Internal_Sym
*)
2751 bfd_malloc (symtab_hdr
->sh_info
2752 * sizeof (Elf_Internal_Sym
)));
2753 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2756 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
2757 * sizeof (asection
*));
2758 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
2761 isymp
= internal_syms
;
2763 esym
= external_syms
;
2764 esymend
= esym
+ symtab_hdr
->sh_info
;
2765 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
2769 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
2771 if (isymp
->st_shndx
== SHN_UNDEF
)
2772 isec
= bfd_und_section_ptr
;
2773 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
2774 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
2775 else if (isymp
->st_shndx
== SHN_ABS
)
2776 isec
= bfd_abs_section_ptr
;
2777 else if (isymp
->st_shndx
== SHN_COMMON
)
2778 isec
= bfd_com_section_ptr
;
2788 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
2789 input_section
, data
, internal_relocs
,
2790 internal_syms
, sections
))
2793 if (sections
!= NULL
)
2796 if (internal_syms
!= NULL
)
2797 free (internal_syms
);
2798 internal_syms
= NULL
;
2799 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2800 free (external_syms
);
2801 external_syms
= NULL
;
2802 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
2803 free (internal_relocs
);
2804 internal_relocs
= NULL
;
2810 if (internal_relocs
!= NULL
2811 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
2812 free (internal_relocs
);
2813 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2814 free (external_syms
);
2815 if (internal_syms
!= NULL
)
2816 free (internal_syms
);
2817 if (sections
!= NULL
)
2822 /* Assorted hash table functions. */
2824 /* Initialize an entry in the link hash table. */
2826 /* Create an entry in an MN10300 ELF linker hash table. */
2828 static struct bfd_hash_entry
*
2829 elf32_mn10300_link_hash_newfunc (entry
, table
, string
)
2830 struct bfd_hash_entry
*entry
;
2831 struct bfd_hash_table
*table
;
2834 struct elf32_mn10300_link_hash_entry
*ret
=
2835 (struct elf32_mn10300_link_hash_entry
*) entry
;
2837 /* Allocate the structure if it has not already been allocated by a
2839 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2840 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2841 bfd_hash_allocate (table
,
2842 sizeof (struct elf32_mn10300_link_hash_entry
)));
2843 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2844 return (struct bfd_hash_entry
*) ret
;
2846 /* Call the allocation method of the superclass. */
2847 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2848 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2850 if (ret
!= (struct elf32_mn10300_link_hash_entry
*) NULL
)
2852 ret
->direct_calls
= 0;
2853 ret
->stack_size
= 0;
2854 ret
->movm_stack_size
= 0;
2859 return (struct bfd_hash_entry
*) ret
;
2862 /* Create an mn10300 ELF linker hash table. */
2864 static struct bfd_link_hash_table
*
2865 elf32_mn10300_link_hash_table_create (abfd
)
2868 struct elf32_mn10300_link_hash_table
*ret
;
2870 ret
= ((struct elf32_mn10300_link_hash_table
*)
2871 bfd_alloc (abfd
, sizeof (struct elf32_mn10300_link_hash_table
)));
2872 if (ret
== (struct elf32_mn10300_link_hash_table
*) NULL
)
2875 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2876 elf32_mn10300_link_hash_newfunc
))
2878 bfd_release (abfd
, ret
);
2883 ret
->static_hash_table
2884 = ((struct elf32_mn10300_link_hash_table
*)
2885 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
2886 if (ret
->static_hash_table
== NULL
)
2888 bfd_release (abfd
, ret
);
2892 if (! _bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
2893 elf32_mn10300_link_hash_newfunc
))
2895 bfd_release (abfd
, ret
->static_hash_table
);
2896 bfd_release (abfd
, ret
);
2899 return &ret
->root
.root
;
2903 elf_mn10300_mach (flags
)
2906 switch (flags
& EF_MN10300_MACH
)
2908 case E_MN10300_MACH_MN10300
:
2910 return bfd_mach_mn10300
;
2912 case E_MN10300_MACH_AM33
:
2913 return bfd_mach_am33
;
2917 /* The final processing done just before writing out a MN10300 ELF object
2918 file. This gets the MN10300 architecture right based on the machine
2922 _bfd_mn10300_elf_final_write_processing (abfd
, linker
)
2924 boolean linker ATTRIBUTE_UNUSED
;
2928 switch (bfd_get_mach (abfd
))
2931 case bfd_mach_mn10300
:
2932 val
= E_MN10300_MACH_MN10300
;
2936 val
= E_MN10300_MACH_AM33
;
2940 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
2941 elf_elfheader (abfd
)->e_flags
|= val
;
2945 _bfd_mn10300_elf_object_p (abfd
)
2948 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
2949 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
2953 /* Merge backend specific data from an object file to the output
2954 object file when linking. */
2957 _bfd_mn10300_elf_merge_private_bfd_data (ibfd
, obfd
)
2961 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2962 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2965 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
2966 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
2968 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
2969 bfd_get_mach (ibfd
)))
2976 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2977 #define TARGET_LITTLE_NAME "elf32-mn10300"
2978 #define ELF_ARCH bfd_arch_mn10300
2979 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2980 #define ELF_MAXPAGESIZE 0x1000
2982 #define elf_info_to_howto mn10300_info_to_howto
2983 #define elf_info_to_howto_rel 0
2984 #define elf_backend_can_gc_sections 1
2985 #define elf_backend_check_relocs mn10300_elf_check_relocs
2986 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2987 #define elf_backend_relocate_section mn10300_elf_relocate_section
2988 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2989 #define bfd_elf32_bfd_get_relocated_section_contents \
2990 mn10300_elf_get_relocated_section_contents
2991 #define bfd_elf32_bfd_link_hash_table_create \
2992 elf32_mn10300_link_hash_table_create
2994 #define elf_symbol_leading_char '_'
2996 /* So we can set bits in e_flags. */
2997 #define elf_backend_final_write_processing \
2998 _bfd_mn10300_elf_final_write_processing
2999 #define elf_backend_object_p _bfd_mn10300_elf_object_p
3001 #define bfd_elf32_bfd_merge_private_bfd_data \
3002 _bfd_mn10300_elf_merge_private_bfd_data
3004 #include "elf32-target.h"