1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #define START_RELOC_NUMBERS(name) enum name {
26 #define RELOC_NUMBER(name, number) name = number ,
27 #define END_RELOC_NUMBERS R_MN10300_MAX };
28 #include "elf/mn10300.h"
30 struct elf32_mn10300_link_hash_entry
32 /* The basic elf link hash table entry. */
33 struct elf_link_hash_entry root
;
35 /* For function symbols, the number of times this function is
36 called directly (ie by name). */
37 unsigned int direct_calls
;
39 /* For function symbols, the size of this function's stack
40 (if <= 255 bytes). We stuff this into "call" instructions
41 to this target when it's valid and profitable to do so. */
42 unsigned char stack_size
;
44 /* For function symbols, arguments (if any) for movm instruction
45 in the prologue. We stuff this value into "call" instructions
46 to the target when it's valid and profitable to do so. */
47 unsigned char movm_args
;
49 /* When set, convert all "call" instructions to this target into "calls"
51 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
53 /* Used to mark functions which have had redundant parts of their
55 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
59 /* We derive a hash table from the main elf linker hash table so
60 we can store state variables and a secondary hash table without
61 resorting to global variables. */
62 struct elf32_mn10300_link_hash_table
64 /* The main hash table. */
65 struct elf_link_hash_table root
;
67 /* A hash table for static functions. We could derive a new hash table
68 instead of using the full elf32_mn10300_link_hash_table if we wanted
69 to save some memory. */
70 struct elf32_mn10300_link_hash_table
*static_hash_table
;
72 /* Random linker state flags. */
73 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
77 /* For MN10300 linker hash table. */
79 /* Get the MN10300 ELF linker hash table from a link_info structure. */
81 #define elf32_mn10300_hash_table(p) \
82 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
84 #define elf32_mn10300_link_hash_traverse(table, func, info) \
85 (elf_link_hash_traverse \
87 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
90 static struct bfd_hash_entry
*elf32_mn10300_link_hash_newfunc
91 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
92 static struct bfd_link_hash_table
*elf32_mn10300_link_hash_table_create
95 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
96 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
97 static void mn10300_info_to_howto
98 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
99 static boolean mn10300_elf_check_relocs
100 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
101 const Elf_Internal_Rela
*));
102 static asection
*mn10300_elf_gc_mark_hook
103 PARAMS ((bfd
*, struct bfd_link_info
*info
, Elf_Internal_Rela
*,
104 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
105 static boolean mn10300_elf_relax_delete_bytes
106 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
107 static boolean mn10300_elf_symbol_address_p
108 PARAMS ((bfd
*, asection
*, Elf32_External_Sym
*, bfd_vma
));
109 static boolean elf32_mn10300_finish_hash_table_entry
110 PARAMS ((struct bfd_hash_entry
*, PTR
));
111 static void compute_function_info
112 PARAMS ((bfd
*, struct elf32_mn10300_link_hash_entry
*,
113 bfd_vma
, unsigned char *));
115 /* We have to use RELA instructions since md_apply_fix3 in the assembler
116 does absolutely nothing. */
120 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 */
252 struct mn10300_reloc_map
254 unsigned char bfd_reloc_val
;
255 unsigned char elf_reloc_val
;
258 static const struct mn10300_reloc_map mn10300_reloc_map
[] =
260 { BFD_RELOC_NONE
, R_MN10300_NONE
, },
261 { BFD_RELOC_32
, R_MN10300_32
, },
262 { BFD_RELOC_16
, R_MN10300_16
, },
263 { BFD_RELOC_8
, R_MN10300_8
, },
264 { BFD_RELOC_32_PCREL
, R_MN10300_PCREL32
, },
265 { BFD_RELOC_16_PCREL
, R_MN10300_PCREL16
, },
266 { BFD_RELOC_8_PCREL
, R_MN10300_PCREL8
, },
267 { BFD_RELOC_VTABLE_INHERIT
, R_MN10300_GNU_VTINHERIT
},
268 { BFD_RELOC_VTABLE_ENTRY
, R_MN10300_GNU_VTENTRY
},
271 static reloc_howto_type
*
272 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
274 bfd_reloc_code_real_type code
;
279 i
< sizeof (mn10300_reloc_map
) / sizeof (struct mn10300_reloc_map
);
282 if (mn10300_reloc_map
[i
].bfd_reloc_val
== code
)
283 return &elf_mn10300_howto_table
[mn10300_reloc_map
[i
].elf_reloc_val
];
289 /* Set the howto pointer for an MN10300 ELF reloc. */
292 mn10300_info_to_howto (abfd
, cache_ptr
, dst
)
295 Elf32_Internal_Rela
*dst
;
299 r_type
= ELF32_R_TYPE (dst
->r_info
);
300 BFD_ASSERT (r_type
< (unsigned int) R_MN10300_MAX
);
301 cache_ptr
->howto
= &elf_mn10300_howto_table
[r_type
];
304 /* Look through the relocs for a section during the first phase.
305 Since we don't do .gots or .plts, we just need to consider the
306 virtual table relocs for gc. */
309 mn10300_elf_check_relocs (abfd
, info
, sec
, relocs
)
311 struct bfd_link_info
*info
;
313 const Elf_Internal_Rela
*relocs
;
315 Elf_Internal_Shdr
*symtab_hdr
;
316 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
317 const Elf_Internal_Rela
*rel
;
318 const Elf_Internal_Rela
*rel_end
;
320 if (info
->relocateable
)
323 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
324 sym_hashes
= elf_sym_hashes (abfd
);
325 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof(Elf32_External_Sym
);
326 if (!elf_bad_symtab (abfd
))
327 sym_hashes_end
-= symtab_hdr
->sh_info
;
329 rel_end
= relocs
+ sec
->reloc_count
;
330 for (rel
= relocs
; rel
< rel_end
; rel
++)
332 struct elf_link_hash_entry
*h
;
333 unsigned long r_symndx
;
335 r_symndx
= ELF32_R_SYM (rel
->r_info
);
336 if (r_symndx
< symtab_hdr
->sh_info
)
339 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
341 switch (ELF32_R_TYPE (rel
->r_info
))
343 /* This relocation describes the C++ object vtable hierarchy.
344 Reconstruct it for later use during GC. */
345 case R_MN10300_GNU_VTINHERIT
:
346 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
350 /* This relocation describes which C++ vtable entries are actually
351 used. Record for later use during GC. */
352 case R_MN10300_GNU_VTENTRY
:
353 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
362 /* Return the section that should be marked against GC for a given
366 mn10300_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
368 struct bfd_link_info
*info
;
369 Elf_Internal_Rela
*rel
;
370 struct elf_link_hash_entry
*h
;
371 Elf_Internal_Sym
*sym
;
375 switch (ELF32_R_TYPE (rel
->r_info
))
377 case R_MN10300_GNU_VTINHERIT
:
378 case R_MN10300_GNU_VTENTRY
:
382 switch (h
->root
.type
)
384 case bfd_link_hash_defined
:
385 case bfd_link_hash_defweak
:
386 return h
->root
.u
.def
.section
;
388 case bfd_link_hash_common
:
389 return h
->root
.u
.c
.p
->section
;
395 if (!(elf_bad_symtab (abfd
)
396 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
397 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
398 && sym
->st_shndx
!= SHN_COMMON
))
400 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
407 /* Perform a relocation as part of a final link. */
408 static bfd_reloc_status_type
409 mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
410 input_section
, contents
, offset
, value
,
411 addend
, info
, sym_sec
, is_local
)
412 reloc_howto_type
*howto
;
415 asection
*input_section
;
420 struct bfd_link_info
*info
;
424 unsigned long r_type
= howto
->type
;
425 bfd_byte
*hit_data
= contents
+ offset
;
434 bfd_put_32 (input_bfd
, value
, hit_data
);
440 if ((long)value
> 0x7fff || (long)value
< -0x8000)
441 return bfd_reloc_overflow
;
443 bfd_put_16 (input_bfd
, value
, hit_data
);
449 if ((long)value
> 0x7f || (long)value
< -0x80)
450 return bfd_reloc_overflow
;
452 bfd_put_8 (input_bfd
, value
, hit_data
);
455 case R_MN10300_PCREL8
:
456 value
-= (input_section
->output_section
->vma
457 + input_section
->output_offset
);
461 if ((long)value
> 0xff || (long)value
< -0x100)
462 return bfd_reloc_overflow
;
464 bfd_put_8 (input_bfd
, value
, hit_data
);
467 case R_MN10300_PCREL16
:
468 value
-= (input_section
->output_section
->vma
469 + input_section
->output_offset
);
473 if ((long)value
> 0xffff || (long)value
< -0x10000)
474 return bfd_reloc_overflow
;
476 bfd_put_16 (input_bfd
, value
, hit_data
);
479 case R_MN10300_PCREL32
:
480 value
-= (input_section
->output_section
->vma
481 + input_section
->output_offset
);
485 bfd_put_32 (input_bfd
, value
, hit_data
);
488 case R_MN10300_GNU_VTINHERIT
:
489 case R_MN10300_GNU_VTENTRY
:
493 return bfd_reloc_notsupported
;
498 /* Relocate an MN10300 ELF section. */
500 mn10300_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
501 contents
, relocs
, local_syms
, local_sections
)
503 struct bfd_link_info
*info
;
505 asection
*input_section
;
507 Elf_Internal_Rela
*relocs
;
508 Elf_Internal_Sym
*local_syms
;
509 asection
**local_sections
;
511 Elf_Internal_Shdr
*symtab_hdr
;
512 struct elf32_mn10300_link_hash_entry
**sym_hashes
;
513 Elf_Internal_Rela
*rel
, *relend
;
515 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
516 sym_hashes
= (struct elf32_mn10300_link_hash_entry
**)
517 (elf_sym_hashes (input_bfd
));
520 relend
= relocs
+ input_section
->reloc_count
;
521 for (; rel
< relend
; rel
++)
524 reloc_howto_type
*howto
;
525 unsigned long r_symndx
;
526 Elf_Internal_Sym
*sym
;
528 struct elf32_mn10300_link_hash_entry
*h
;
530 bfd_reloc_status_type r
;
532 r_symndx
= ELF32_R_SYM (rel
->r_info
);
533 r_type
= ELF32_R_TYPE (rel
->r_info
);
534 howto
= elf_mn10300_howto_table
+ r_type
;
536 /* Just skip the vtable gc relocs. */
537 if (r_type
== R_MN10300_GNU_VTINHERIT
538 || r_type
== R_MN10300_GNU_VTENTRY
)
541 if (info
->relocateable
)
543 /* This is a relocateable link. We don't have to change
544 anything, unless the reloc is against a section symbol,
545 in which case we have to adjust according to where the
546 section symbol winds up in the output section. */
547 if (r_symndx
< symtab_hdr
->sh_info
)
549 sym
= local_syms
+ r_symndx
;
550 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
552 sec
= local_sections
[r_symndx
];
553 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
560 /* This is a final link. */
564 if (r_symndx
< symtab_hdr
->sh_info
)
566 sym
= local_syms
+ r_symndx
;
567 sec
= local_sections
[r_symndx
];
568 relocation
= (sec
->output_section
->vma
574 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
575 while (h
->root
.type
== bfd_link_hash_indirect
576 || h
->root
.type
== bfd_link_hash_warning
)
577 h
= (struct elf32_mn10300_link_hash_entry
*) h
->root
.root
.u
.i
.link
;
578 if (h
->root
.root
.type
== bfd_link_hash_defined
579 || h
->root
.root
.type
== bfd_link_hash_defweak
)
581 sec
= h
->root
.root
.u
.def
.section
;
582 relocation
= (h
->root
.root
.u
.def
.value
583 + sec
->output_section
->vma
584 + sec
->output_offset
);
586 else if (h
->root
.root
.type
== bfd_link_hash_undefweak
)
590 if (! ((*info
->callbacks
->undefined_symbol
)
591 (info
, h
->root
.root
.root
.string
, input_bfd
,
592 input_section
, rel
->r_offset
)))
598 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
600 contents
, rel
->r_offset
,
601 relocation
, rel
->r_addend
,
602 info
, sec
, h
== NULL
);
604 if (r
!= bfd_reloc_ok
)
607 const char *msg
= (const char *)0;
610 name
= h
->root
.root
.root
.string
;
613 name
= (bfd_elf_string_from_elf_section
614 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
615 if (name
== NULL
|| *name
== '\0')
616 name
= bfd_section_name (input_bfd
, sec
);
621 case bfd_reloc_overflow
:
622 if (! ((*info
->callbacks
->reloc_overflow
)
623 (info
, name
, howto
->name
, (bfd_vma
) 0,
624 input_bfd
, input_section
, rel
->r_offset
)))
628 case bfd_reloc_undefined
:
629 if (! ((*info
->callbacks
->undefined_symbol
)
630 (info
, name
, input_bfd
, input_section
,
635 case bfd_reloc_outofrange
:
636 msg
= _("internal error: out of range error");
639 case bfd_reloc_notsupported
:
640 msg
= _("internal error: unsupported relocation error");
643 case bfd_reloc_dangerous
:
644 msg
= _("internal error: dangerous error");
648 msg
= _("internal error: unknown error");
652 if (!((*info
->callbacks
->warning
)
653 (info
, msg
, name
, input_bfd
, input_section
,
664 /* Finish initializing one hash table entry. */
666 elf32_mn10300_finish_hash_table_entry (gen_entry
, in_args
)
667 struct bfd_hash_entry
*gen_entry
;
670 struct elf32_mn10300_link_hash_entry
*entry
;
671 unsigned int byte_count
= 0;
673 entry
= (struct elf32_mn10300_link_hash_entry
*)gen_entry
;
675 /* If we already know we want to convert "call" to "calls" for calls
676 to this symbol, then return now. */
677 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
680 /* If there are no named calls to this symbol, or there's nothing we
681 can move from the function itself into the "call" instruction, then
682 note that all "call" instructions should be converted into "calls"
683 instructions and return. */
684 if (entry
->direct_calls
== 0
685 || (entry
->stack_size
== 0 && entry
->movm_args
== 0))
687 /* Make a note that we should convert "call" instructions to "calls"
688 instructions for calls to this symbol. */
689 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
693 /* We may be able to move some instructions from the function itself into
694 the "call" instruction. Count how many bytes we might be able to
695 eliminate in the function itself. */
697 /* A movm instruction is two bytes. */
698 if (entry
->movm_args
)
701 /* Count the insn to allocate stack space too. */
702 if (entry
->stack_size
> 0 && entry
->stack_size
<= 128)
704 else if (entry
->stack_size
> 0 && entry
->stack_size
< 256)
707 /* If using "call" will result in larger code, then turn all
708 the associated "call" instructions into "calls" instrutions. */
709 if (byte_count
< entry
->direct_calls
)
710 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
712 /* This routine never fails. */
716 /* This function handles relaxing for the mn10300.
718 There's quite a few relaxing opportunites available on the mn10300:
720 * calls:32 -> calls:16 2 bytes
721 * call:32 -> call:16 2 bytes
723 * call:32 -> calls:32 1 byte
724 * call:16 -> calls:16 1 byte
725 * These are done anytime using "calls" would result
726 in smaller code, or when necessary to preserve the
727 meaning of the program.
731 * In some circumstances we can move instructions
732 from a function prologue into a "call" instruction.
733 This is only done if the resulting code is no larger
734 than the original code.
737 * jmp:32 -> jmp:16 2 bytes
738 * jmp:16 -> bra:8 1 byte
740 * If the previous instruction is a conditional branch
741 around the jump/bra, we may be able to reverse its condition
742 and change its target to the jump's target. The jump/bra
743 can then be deleted. 2 bytes
745 * mov abs32 -> mov abs16 1 or 2 bytes
747 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
748 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
750 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
751 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
753 We don't handle imm16->imm8 or d16->d8 as they're very rare
754 and somewhat more difficult to support. */
757 mn10300_elf_relax_section (abfd
, sec
, link_info
, again
)
760 struct bfd_link_info
*link_info
;
763 Elf_Internal_Shdr
*symtab_hdr
;
764 Elf_Internal_Rela
*internal_relocs
= NULL
;
765 Elf_Internal_Rela
*free_relocs
= NULL
;
766 Elf_Internal_Rela
*irel
, *irelend
;
767 bfd_byte
*contents
= NULL
;
768 bfd_byte
*free_contents
= NULL
;
769 Elf32_External_Sym
*extsyms
= NULL
;
770 Elf32_External_Sym
*free_extsyms
= NULL
;
771 struct elf32_mn10300_link_hash_table
*hash_table
;
773 /* Assume nothing changes. */
776 /* We need a pointer to the mn10300 specific hash table. */
777 hash_table
= elf32_mn10300_hash_table (link_info
);
779 /* Initialize fields in each hash table entry the first time through. */
780 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
784 /* Iterate over all the input bfds. */
785 for (input_bfd
= link_info
->input_bfds
;
787 input_bfd
= input_bfd
->link_next
)
791 /* We're going to need all the symbols for each bfd. */
792 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
794 /* Get cached copy if it exists. */
795 if (symtab_hdr
->contents
!= NULL
)
796 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
799 /* Go get them off disk. */
800 extsyms
= ((Elf32_External_Sym
*)
801 bfd_malloc (symtab_hdr
->sh_size
));
804 free_extsyms
= extsyms
;
805 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
806 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
807 != symtab_hdr
->sh_size
))
811 /* Iterate over each section in this bfd. */
812 for (section
= input_bfd
->sections
;
814 section
= section
->next
)
816 struct elf32_mn10300_link_hash_entry
*hash
;
817 Elf_Internal_Sym
*sym
;
819 const char *sym_name
;
821 Elf_Internal_Shdr
*hdr
;
823 /* Get cached copy of section contents if it exists. */
824 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
825 contents
= elf_section_data (section
)->this_hdr
.contents
;
826 else if (section
->_raw_size
!= 0)
828 /* Go get them off disk. */
829 contents
= (bfd_byte
*)bfd_malloc (section
->_raw_size
);
830 if (contents
== NULL
)
832 free_contents
= contents
;
834 if (!bfd_get_section_contents (input_bfd
, section
,
835 contents
, (file_ptr
) 0,
842 free_contents
= NULL
;
845 /* If there aren't any relocs, then there's nothing to do. */
846 if ((section
->flags
& SEC_RELOC
) != 0
847 && section
->reloc_count
!= 0)
850 /* Get a copy of the native relocations. */
851 internal_relocs
= (_bfd_elf32_link_read_relocs
852 (input_bfd
, section
, (PTR
) NULL
,
853 (Elf_Internal_Rela
*) NULL
,
854 link_info
->keep_memory
));
855 if (internal_relocs
== NULL
)
857 if (! link_info
->keep_memory
)
858 free_relocs
= internal_relocs
;
860 /* Now examine each relocation. */
861 irel
= internal_relocs
;
862 irelend
= irel
+ section
->reloc_count
;
863 for (; irel
< irelend
; irel
++)
866 unsigned long r_index
;
869 r_type
= ELF32_R_TYPE (irel
->r_info
);
870 r_index
= ELF32_R_SYM (irel
->r_info
);
872 if (r_type
< 0 || r_type
>= (int)R_MN10300_MAX
)
875 /* We need the name and hash table entry of the target
881 if (r_index
< symtab_hdr
->sh_info
)
883 /* A local symbol. */
884 Elf_Internal_Sym isym
;
886 bfd_elf32_swap_symbol_in (input_bfd
,
887 extsyms
+ r_index
, &isym
);
889 if (isym
.st_shndx
== SHN_UNDEF
)
890 sym_sec
= bfd_und_section_ptr
;
891 else if (isym
.st_shndx
> 0
892 && isym
.st_shndx
< SHN_LORESERVE
)
894 = bfd_section_from_elf_index (input_bfd
,
896 else if (isym
.st_shndx
== SHN_ABS
)
897 sym_sec
= bfd_abs_section_ptr
;
898 else if (isym
.st_shndx
== SHN_COMMON
)
899 sym_sec
= bfd_com_section_ptr
;
901 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
905 /* If it isn't a function, then we don't care
907 if (r_index
< symtab_hdr
->sh_info
908 && ELF_ST_TYPE (isym
.st_info
) != STT_FUNC
)
911 /* Tack on an ID so we can uniquely identify this
912 local symbol in the global hash table. */
913 new_name
= alloca (strlen (sym_name
) + 10);
914 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
917 hash
= (struct elf32_mn10300_link_hash_entry
*)
918 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
924 r_index
-= symtab_hdr
->sh_info
;
925 hash
= (struct elf32_mn10300_link_hash_entry
*)
926 elf_sym_hashes (input_bfd
)[r_index
];
929 /* If this is not a "call" instruction, then we
930 should convert "call" instructions to "calls"
932 code
= bfd_get_8 (input_bfd
,
933 contents
+ irel
->r_offset
- 1);
934 if (code
!= 0xdd && code
!= 0xcd)
935 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
937 /* If this is a jump/call, then bump the direct_calls
938 counter. Else force "call" to "calls" conversions. */
939 if (r_type
== R_MN10300_PCREL32
940 || r_type
== R_MN10300_PCREL16
)
941 hash
->direct_calls
++;
943 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
947 /* Now look at the actual contents to get the stack size,
948 and a list of what registers were saved in the prologue
950 if ((section
->flags
& SEC_CODE
) != 0)
953 Elf32_External_Sym
*esym
, *esymend
;
956 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
960 /* Look at each function defined in this section and
961 update info for that function. */
963 esymend
= esym
+ symtab_hdr
->sh_info
;
964 for (; esym
< esymend
; esym
++)
966 Elf_Internal_Sym isym
;
968 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
969 if (isym
.st_shndx
== shndx
970 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
)
972 if (isym
.st_shndx
== SHN_UNDEF
)
973 sym_sec
= bfd_und_section_ptr
;
974 else if (isym
.st_shndx
> 0
975 && isym
.st_shndx
< SHN_LORESERVE
)
977 = bfd_section_from_elf_index (input_bfd
,
979 else if (isym
.st_shndx
== SHN_ABS
)
980 sym_sec
= bfd_abs_section_ptr
;
981 else if (isym
.st_shndx
== SHN_COMMON
)
982 sym_sec
= bfd_com_section_ptr
;
984 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
988 /* Tack on an ID so we can uniquely identify this
989 local symbol in the global hash table. */
990 new_name
= alloca (strlen (sym_name
) + 10);
991 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
994 hash
= (struct elf32_mn10300_link_hash_entry
*)
995 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
998 compute_function_info (input_bfd
, hash
,
999 isym
.st_value
, contents
);
1003 esym
= extsyms
+ symtab_hdr
->sh_info
;
1004 esymend
= extsyms
+ (symtab_hdr
->sh_size
1005 / sizeof (Elf32_External_Sym
));
1006 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1008 Elf_Internal_Sym isym
;
1010 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1011 hash
= (struct elf32_mn10300_link_hash_entry
*)
1012 elf_sym_hashes (input_bfd
)[idx
];
1013 if (isym
.st_shndx
== shndx
1014 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
1015 && (hash
)->root
.root
.u
.def
.section
== section
1016 && ((hash
)->root
.root
.type
== bfd_link_hash_defined
1017 || (hash
)->root
.root
.type
== bfd_link_hash_defweak
))
1018 compute_function_info (input_bfd
, hash
,
1019 (hash
)->root
.root
.u
.def
.value
,
1024 /* Cache or free any memory we allocated for the relocs. */
1025 if (free_relocs
!= NULL
)
1031 /* Cache or free any memory we allocated for the contents. */
1032 if (free_contents
!= NULL
)
1034 if (! link_info
->keep_memory
)
1035 free (free_contents
);
1038 /* Cache the section contents for elf_link_input_bfd. */
1039 elf_section_data (section
)->this_hdr
.contents
= contents
;
1041 free_contents
= NULL
;
1045 /* Cache or free any memory we allocated for the symbols. */
1046 if (free_extsyms
!= NULL
)
1048 if (! link_info
->keep_memory
)
1049 free (free_extsyms
);
1052 /* Cache the symbols for elf_link_input_bfd. */
1053 symtab_hdr
->contents
= extsyms
;
1055 free_extsyms
= NULL
;
1059 /* Now iterate on each symbol in the hash table and perform
1060 the final initialization steps on each. */
1061 elf32_mn10300_link_hash_traverse (hash_table
,
1062 elf32_mn10300_finish_hash_table_entry
,
1064 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
1065 elf32_mn10300_finish_hash_table_entry
,
1068 /* All entries in the hash table are fully initialized. */
1069 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
1071 /* Now that everything has been initialized, go through each
1072 code section and delete any prologue insns which will be
1073 redundant because their operations will be performed by
1074 a "call" instruction. */
1075 for (input_bfd
= link_info
->input_bfds
;
1077 input_bfd
= input_bfd
->link_next
)
1081 /* We're going to need all the symbols for each bfd. */
1082 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1084 /* Get cached copy if it exists. */
1085 if (symtab_hdr
->contents
!= NULL
)
1086 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1089 /* Go get them off disk. */
1090 extsyms
= ((Elf32_External_Sym
*)
1091 bfd_malloc (symtab_hdr
->sh_size
));
1092 if (extsyms
== NULL
)
1094 free_extsyms
= extsyms
;
1095 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1096 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
1097 != symtab_hdr
->sh_size
))
1101 /* Walk over each section in this bfd. */
1102 for (section
= input_bfd
->sections
;
1104 section
= section
->next
)
1107 Elf32_External_Sym
*esym
, *esymend
;
1110 /* Skip non-code sections and empty sections. */
1111 if ((section
->flags
& SEC_CODE
) == 0 || section
->_raw_size
== 0)
1114 if (section
->reloc_count
!= 0)
1116 /* Get a copy of the native relocations. */
1117 internal_relocs
= (_bfd_elf32_link_read_relocs
1118 (input_bfd
, section
, (PTR
) NULL
,
1119 (Elf_Internal_Rela
*) NULL
,
1120 link_info
->keep_memory
));
1121 if (internal_relocs
== NULL
)
1123 if (! link_info
->keep_memory
)
1124 free_relocs
= internal_relocs
;
1127 /* Get cached copy of section contents if it exists. */
1128 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
1129 contents
= elf_section_data (section
)->this_hdr
.contents
;
1132 /* Go get them off disk. */
1133 contents
= (bfd_byte
*)bfd_malloc (section
->_raw_size
);
1134 if (contents
== NULL
)
1136 free_contents
= contents
;
1138 if (!bfd_get_section_contents (input_bfd
, section
,
1139 contents
, (file_ptr
) 0,
1140 section
->_raw_size
))
1145 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
, section
);
1147 /* Now look for any function in this section which needs
1148 insns deleted from its prologue. */
1150 esymend
= esym
+ symtab_hdr
->sh_info
;
1151 for (; esym
< esymend
; esym
++)
1153 Elf_Internal_Sym isym
;
1154 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1156 const char *sym_name
;
1157 Elf_Internal_Shdr
*hdr
;
1160 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1162 if (isym
.st_shndx
!= shndx
)
1165 if (isym
.st_shndx
== SHN_UNDEF
)
1166 sym_sec
= bfd_und_section_ptr
;
1167 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1169 = bfd_section_from_elf_index (input_bfd
, isym
.st_shndx
);
1170 else if (isym
.st_shndx
== SHN_ABS
)
1171 sym_sec
= bfd_abs_section_ptr
;
1172 else if (isym
.st_shndx
== SHN_COMMON
)
1173 sym_sec
= bfd_com_section_ptr
;
1175 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1176 symtab_hdr
->sh_link
,
1179 /* Tack on an ID so we can uniquely identify this
1180 local symbol in the global hash table. */
1181 new_name
= alloca (strlen (sym_name
) + 10);
1182 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1183 sym_name
= new_name
;
1185 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1186 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1190 if (sym_hash
== NULL
)
1193 if (! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1194 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1198 /* Note that we've changed things. */
1199 elf_section_data (section
)->relocs
= internal_relocs
;
1202 elf_section_data (section
)->this_hdr
.contents
= contents
;
1203 free_contents
= NULL
;
1205 symtab_hdr
->contents
= (bfd_byte
*)extsyms
;
1206 free_extsyms
= NULL
;
1208 /* Count how many bytes we're going to delete. */
1209 if (sym_hash
->movm_args
)
1212 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1214 else if (sym_hash
->stack_size
1215 && sym_hash
->stack_size
< 256)
1218 /* Note that we've deleted prologue bytes for this
1220 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1222 /* Actually delete the bytes. */
1223 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1229 /* Something changed. Not strictly necessary, but
1230 may lead to more relaxing opportunities. */
1235 /* Look for any global functions in this section which
1236 need insns deleted from their prologues. */
1237 esym
= extsyms
+ symtab_hdr
->sh_info
;
1238 esymend
= extsyms
+ (symtab_hdr
->sh_size
1239 / sizeof (Elf32_External_Sym
));
1240 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1242 Elf_Internal_Sym isym
;
1243 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1245 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1246 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1247 (elf_sym_hashes (input_bfd
)[idx
]);
1248 if (isym
.st_shndx
== shndx
1249 && (sym_hash
)->root
.root
.u
.def
.section
== section
1250 && ! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1251 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1255 /* Note that we've changed things. */
1256 elf_section_data (section
)->relocs
= internal_relocs
;
1259 elf_section_data (section
)->this_hdr
.contents
= contents
;
1260 free_contents
= NULL
;
1262 symtab_hdr
->contents
= (bfd_byte
*)extsyms
;
1263 free_extsyms
= NULL
;
1265 /* Count how many bytes we're going to delete. */
1266 if (sym_hash
->movm_args
)
1269 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1271 else if (sym_hash
->stack_size
1272 && sym_hash
->stack_size
< 256)
1275 /* Note that we've deleted prologue bytes for this
1277 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1279 /* Actually delete the bytes. */
1280 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1282 (sym_hash
)->root
.root
.u
.def
.value
,
1286 /* Something changed. Not strictly necessary, but
1287 may lead to more relaxing opportunities. */
1292 /* Cache or free any memory we allocated for the relocs. */
1293 if (free_relocs
!= NULL
)
1299 /* Cache or free any memory we allocated for the contents. */
1300 if (free_contents
!= NULL
)
1302 if (! link_info
->keep_memory
)
1303 free (free_contents
);
1306 /* Cache the section contents for elf_link_input_bfd. */
1307 elf_section_data (section
)->this_hdr
.contents
= contents
;
1309 free_contents
= NULL
;
1313 /* Cache or free any memory we allocated for the symbols. */
1314 if (free_extsyms
!= NULL
)
1316 if (! link_info
->keep_memory
)
1317 free (free_extsyms
);
1320 /* Cache the symbols for elf_link_input_bfd. */
1321 symtab_hdr
->contents
= extsyms
;
1323 free_extsyms
= NULL
;
1329 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1332 internal_relocs
= NULL
;
1334 free_contents
= NULL
;
1335 free_extsyms
= NULL
;
1337 /* We don't have to do anything for a relocateable link, if
1338 this section does not have relocs, or if this is not a
1340 if (link_info
->relocateable
1341 || (sec
->flags
& SEC_RELOC
) == 0
1342 || sec
->reloc_count
== 0
1343 || (sec
->flags
& SEC_CODE
) == 0)
1346 /* If this is the first time we have been called for this section,
1347 initialize the cooked size. */
1348 if (sec
->_cooked_size
== 0)
1349 sec
->_cooked_size
= sec
->_raw_size
;
1351 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1353 /* Get a copy of the native relocations. */
1354 internal_relocs
= (_bfd_elf32_link_read_relocs
1355 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1356 link_info
->keep_memory
));
1357 if (internal_relocs
== NULL
)
1359 if (! link_info
->keep_memory
)
1360 free_relocs
= internal_relocs
;
1362 /* Walk through them looking for relaxing opportunities. */
1363 irelend
= internal_relocs
+ sec
->reloc_count
;
1364 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1367 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
1369 /* If this isn't something that can be relaxed, then ignore
1371 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
1372 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
1373 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
1376 /* Get the section contents if we haven't done so already. */
1377 if (contents
== NULL
)
1379 /* Get cached copy if it exists. */
1380 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1381 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1384 /* Go get them off disk. */
1385 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1386 if (contents
== NULL
)
1388 free_contents
= contents
;
1390 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1391 (file_ptr
) 0, sec
->_raw_size
))
1396 /* Read this BFD's symbols if we haven't done so already. */
1397 if (extsyms
== NULL
)
1399 /* Get cached copy if it exists. */
1400 if (symtab_hdr
->contents
!= NULL
)
1401 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1404 /* Go get them off disk. */
1405 extsyms
= ((Elf32_External_Sym
*)
1406 bfd_malloc (symtab_hdr
->sh_size
));
1407 if (extsyms
== NULL
)
1409 free_extsyms
= extsyms
;
1410 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1411 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1412 != symtab_hdr
->sh_size
))
1417 /* Get the value of the symbol referred to by the reloc. */
1418 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1420 Elf_Internal_Sym isym
;
1422 Elf_Internal_Shdr
*hdr
;
1423 const char *sym_name
;
1426 /* A local symbol. */
1427 bfd_elf32_swap_symbol_in (abfd
,
1428 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1431 if (isym
.st_shndx
== SHN_UNDEF
)
1432 sym_sec
= bfd_und_section_ptr
;
1433 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1434 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
1435 else if (isym
.st_shndx
== SHN_ABS
)
1436 sym_sec
= bfd_abs_section_ptr
;
1437 else if (isym
.st_shndx
== SHN_COMMON
)
1438 sym_sec
= bfd_com_section_ptr
;
1440 symval
= (isym
.st_value
1441 + sym_sec
->output_section
->vma
1442 + sym_sec
->output_offset
);
1443 sym_name
= bfd_elf_string_from_elf_section (abfd
,
1444 symtab_hdr
->sh_link
,
1447 /* Tack on an ID so we can uniquely identify this
1448 local symbol in the global hash table. */
1449 new_name
= alloca (strlen (sym_name
) + 10);
1450 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1451 sym_name
= new_name
;
1453 h
= (struct elf32_mn10300_link_hash_entry
*)
1454 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1455 sym_name
, false, false, false);
1461 /* An external symbol. */
1462 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1463 h
= (struct elf32_mn10300_link_hash_entry
*)
1464 (elf_sym_hashes (abfd
)[indx
]);
1465 BFD_ASSERT (h
!= NULL
);
1466 if (h
->root
.root
.type
!= bfd_link_hash_defined
1467 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
1469 /* This appears to be a reference to an undefined
1470 symbol. Just ignore it--it will be caught by the
1471 regular reloc processing. */
1475 symval
= (h
->root
.root
.u
.def
.value
1476 + h
->root
.root
.u
.def
.section
->output_section
->vma
1477 + h
->root
.root
.u
.def
.section
->output_offset
);
1480 /* For simplicity of coding, we are going to modify the section
1481 contents, the section relocs, and the BFD symbol table. We
1482 must tell the rest of the code not to free up this
1483 information. It would be possible to instead create a table
1484 of changes which have to be made, as is done in coff-mips.c;
1485 that would be more work, but would require less memory when
1486 the linker is run. */
1488 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1489 branch/call, also deal with "call" -> "calls" conversions and
1490 insertion of prologue data into "call" instructions. */
1491 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
)
1493 bfd_vma value
= symval
;
1495 /* If we've got a "call" instruction that needs to be turned
1496 into a "calls" instruction, do so now. It saves a byte. */
1497 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1501 /* Get the opcode. */
1502 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1504 /* Make sure we're working with a "call" instruction! */
1507 /* Note that we've changed the relocs, section contents,
1509 elf_section_data (sec
)->relocs
= internal_relocs
;
1512 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1513 free_contents
= NULL
;
1515 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1516 free_extsyms
= NULL
;
1518 /* Fix the opcode. */
1519 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
1520 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1522 /* Fix irel->r_offset and irel->r_addend. */
1523 irel
->r_offset
+= 1;
1524 irel
->r_addend
+= 1;
1526 /* Delete one byte of data. */
1527 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1528 irel
->r_offset
+ 3, 1))
1531 /* That will change things, so, we should relax again.
1532 Note that this is not required, and it may be slow. */
1538 /* We've got a "call" instruction which needs some data
1539 from target function filled in. */
1542 /* Get the opcode. */
1543 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1545 /* Insert data from the target function into the "call"
1546 instruction if needed. */
1549 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
1550 bfd_put_8 (abfd
, h
->stack_size
,
1551 contents
+ irel
->r_offset
+ 5);
1555 /* Deal with pc-relative gunk. */
1556 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1557 value
-= irel
->r_offset
;
1558 value
+= irel
->r_addend
;
1560 /* See if the value will fit in 16 bits, note the high value is
1561 0x7fff + 2 as the target will be two bytes closer if we are
1563 if ((long)value
< 0x8001 && (long)value
> -0x8000)
1567 /* Get the opcode. */
1568 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1570 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
1573 /* Note that we've changed the relocs, section contents, etc. */
1574 elf_section_data (sec
)->relocs
= internal_relocs
;
1577 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1578 free_contents
= NULL
;
1580 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1581 free_extsyms
= NULL
;
1583 /* Fix the opcode. */
1585 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
1586 else if (code
== 0xdd)
1587 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
1588 else if (code
== 0xff)
1589 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
1591 /* Fix the relocation's type. */
1592 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1595 /* Delete two bytes of data. */
1596 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1597 irel
->r_offset
+ 1, 2))
1600 /* That will change things, so, we should relax again.
1601 Note that this is not required, and it may be slow. */
1606 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1608 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
1610 bfd_vma value
= symval
;
1612 /* If we've got a "call" instruction that needs to be turned
1613 into a "calls" instruction, do so now. It saves a byte. */
1614 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1618 /* Get the opcode. */
1619 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1621 /* Make sure we're working with a "call" instruction! */
1624 /* Note that we've changed the relocs, section contents,
1626 elf_section_data (sec
)->relocs
= internal_relocs
;
1629 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1630 free_contents
= NULL
;
1632 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1633 free_extsyms
= NULL
;
1635 /* Fix the opcode. */
1636 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
1637 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1639 /* Fix irel->r_offset and irel->r_addend. */
1640 irel
->r_offset
+= 1;
1641 irel
->r_addend
+= 1;
1643 /* Delete one byte of data. */
1644 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1645 irel
->r_offset
+ 1, 1))
1648 /* That will change things, so, we should relax again.
1649 Note that this is not required, and it may be slow. */
1657 /* Get the opcode. */
1658 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1660 /* Insert data from the target function into the "call"
1661 instruction if needed. */
1664 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
1665 bfd_put_8 (abfd
, h
->stack_size
,
1666 contents
+ irel
->r_offset
+ 3);
1670 /* Deal with pc-relative gunk. */
1671 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1672 value
-= irel
->r_offset
;
1673 value
+= irel
->r_addend
;
1675 /* See if the value will fit in 8 bits, note the high value is
1676 0x7f + 1 as the target will be one bytes closer if we are
1678 if ((long)value
< 0x80 && (long)value
> -0x80)
1682 /* Get the opcode. */
1683 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1688 /* Note that we've changed the relocs, section contents, etc. */
1689 elf_section_data (sec
)->relocs
= internal_relocs
;
1692 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1693 free_contents
= NULL
;
1695 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1696 free_extsyms
= NULL
;
1698 /* Fix the opcode. */
1699 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
1701 /* Fix the relocation's type. */
1702 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1705 /* Delete one byte of data. */
1706 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1707 irel
->r_offset
+ 1, 1))
1710 /* That will change things, so, we should relax again.
1711 Note that this is not required, and it may be slow. */
1716 /* Try to eliminate an unconditional 8 bit pc-relative branch
1717 which immediately follows a conditional 8 bit pc-relative
1718 branch around the unconditional branch.
1726 This happens when the bCC can't reach lab2 at assembly time,
1727 but due to other relaxations it can reach at link time. */
1728 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
1730 Elf_Internal_Rela
*nrel
;
1731 bfd_vma value
= symval
;
1734 /* Deal with pc-relative gunk. */
1735 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1736 value
-= irel
->r_offset
;
1737 value
+= irel
->r_addend
;
1739 /* Do nothing if this reloc is the last byte in the section. */
1740 if (irel
->r_offset
== sec
->_cooked_size
)
1743 /* See if the next instruction is an unconditional pc-relative
1744 branch, more often than not this test will fail, so we
1745 test it first to speed things up. */
1746 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1750 /* Also make sure the next relocation applies to the next
1751 instruction and that it's a pc-relative 8 bit branch. */
1754 || irel
->r_offset
+ 2 != nrel
->r_offset
1755 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
1758 /* Make sure our destination immediately follows the
1759 unconditional branch. */
1760 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
1761 + irel
->r_offset
+ 3))
1764 /* Now make sure we are a conditional branch. This may not
1765 be necessary, but why take the chance.
1767 Note these checks assume that R_MN10300_PCREL8 relocs
1768 only occur on bCC and bCCx insns. If they occured
1769 elsewhere, we'd need to know the start of this insn
1770 for this check to be accurate. */
1771 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1772 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
1773 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
1774 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
1775 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
1776 && code
!= 0xea && code
!= 0xeb)
1779 /* We also have to be sure there is no symbol/label
1780 at the unconditional branch. */
1781 if (mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
,
1782 irel
->r_offset
+ 1))
1785 /* Note that we've changed the relocs, section contents, etc. */
1786 elf_section_data (sec
)->relocs
= internal_relocs
;
1789 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1790 free_contents
= NULL
;
1792 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1793 free_extsyms
= NULL
;
1795 /* Reverse the condition of the first branch. */
1841 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1843 /* Set the reloc type and symbol for the first branch
1844 from the second branch. */
1845 irel
->r_info
= nrel
->r_info
;
1847 /* Make the reloc for the second branch a null reloc. */
1848 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
1851 /* Delete two bytes of data. */
1852 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1853 irel
->r_offset
+ 1, 2))
1856 /* That will change things, so, we should relax again.
1857 Note that this is not required, and it may be slow. */
1861 /* Try to turn a 32bit immediate, displacement or absolute address
1862 into a 16bit immediate, displacement or absolute address. */
1863 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
)
1865 bfd_vma value
= symval
;
1866 value
+= irel
->r_addend
;
1868 /* See if the value will fit in 16 bits.
1869 We allow any 16bit match here. We prune those we can't
1871 if ((long)value
< 0x7fff && (long)value
> -0x8000)
1875 /* Most insns which have 32bit operands are 6 bytes long;
1876 exceptions are pcrel insns and bit insns.
1878 We handle pcrel insns above. We don't bother trying
1879 to handle the bit insns here.
1881 The first byte of the remaining insns will be 0xfc. */
1883 /* Get the first opcode. */
1884 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1889 /* Get the second opcode. */
1890 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1892 if ((code
& 0xf0) < 0x80)
1893 switch (code
& 0xf0)
1895 /* mov (d32,am),dn -> mov (d32,am),dn
1896 mov dm,(d32,am) -> mov dn,(d32,am)
1897 mov (d32,am),an -> mov (d32,am),an
1898 mov dm,(d32,am) -> mov dn,(d32,am)
1899 movbu (d32,am),dn -> movbu (d32,am),dn
1900 movbu dm,(d32,am) -> movbu dn,(d32,am)
1901 movhu (d32,am),dn -> movhu (d32,am),dn
1902 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1911 /* Not safe if the high bit is on as relaxing may
1912 move the value out of high mem and thus not fit
1913 in a signed 16bit value. */
1915 && (value
& 0x8000))
1918 /* Note that we've changed the relocation contents, etc. */
1919 elf_section_data (sec
)->relocs
= internal_relocs
;
1922 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1923 free_contents
= NULL
;
1925 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1926 free_extsyms
= NULL
;
1928 /* Fix the opcode. */
1929 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
1930 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1932 /* Fix the relocation's type. */
1933 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1936 /* Delete two bytes of data. */
1937 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1938 irel
->r_offset
+ 2, 2))
1941 /* That will change things, so, we should relax again.
1942 Note that this is not required, and it may be slow. */
1946 else if ((code
& 0xf0) == 0x80
1947 || (code
& 0xf0) == 0x90)
1948 switch (code
& 0xf3)
1950 /* mov dn,(abs32) -> mov dn,(abs16)
1951 movbu dn,(abs32) -> movbu dn,(abs16)
1952 movhu dn,(abs32) -> movhu dn,(abs16) */
1956 /* Note that we've changed the relocation contents, etc. */
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 if ((code
& 0xf3) == 0x81)
1967 code
= 0x01 + (code
& 0x0c);
1968 else if ((code
& 0xf3) == 0x82)
1969 code
= 0x02 + (code
& 0x0c);
1970 else if ((code
& 0xf3) == 0x83)
1971 code
= 0x03 + (code
& 0x0c);
1975 /* Fix the opcode. */
1976 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1978 /* Fix the relocation's type. */
1979 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1982 /* The opcode got shorter too, so we have to fix the
1983 addend and offset too! */
1984 irel
->r_offset
-= 1;
1986 /* Delete three bytes of data. */
1987 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1988 irel
->r_offset
+ 1, 3))
1991 /* That will change things, so, we should relax again.
1992 Note that this is not required, and it may be slow. */
1996 /* mov am,(abs32) -> mov am,(abs16)
1997 mov am,(d32,sp) -> mov am,(d16,sp)
1998 mov dm,(d32,sp) -> mov dm,(d32,sp)
1999 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2000 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2006 /* Note that we've changed the relocation contents, etc. */
2007 elf_section_data (sec
)->relocs
= internal_relocs
;
2010 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2011 free_contents
= NULL
;
2013 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2014 free_extsyms
= NULL
;
2016 /* Fix the opcode. */
2017 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2018 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2020 /* Fix the relocation's type. */
2021 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2024 /* Delete two bytes of data. */
2025 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2026 irel
->r_offset
+ 2, 2))
2029 /* That will change things, so, we should relax again.
2030 Note that this is not required, and it may be slow. */
2034 else if ((code
& 0xf0) < 0xf0)
2035 switch (code
& 0xfc)
2037 /* mov imm32,dn -> mov imm16,dn
2038 mov imm32,an -> mov imm16,an
2039 mov (abs32),dn -> mov (abs16),dn
2040 movbu (abs32),dn -> movbu (abs16),dn
2041 movhu (abs32),dn -> movhu (abs16),dn */
2047 /* Not safe if the high bit is on as relaxing may
2048 move the value out of high mem and thus not fit
2049 in a signed 16bit value. */
2051 && (value
& 0x8000))
2054 /* Note that we've changed the relocation contents, etc. */
2055 elf_section_data (sec
)->relocs
= internal_relocs
;
2058 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2059 free_contents
= NULL
;
2061 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2062 free_extsyms
= NULL
;
2064 if ((code
& 0xfc) == 0xcc)
2065 code
= 0x2c + (code
& 0x03);
2066 else if ((code
& 0xfc) == 0xdc)
2067 code
= 0x24 + (code
& 0x03);
2068 else if ((code
& 0xfc) == 0xa4)
2069 code
= 0x30 + (code
& 0x03);
2070 else if ((code
& 0xfc) == 0xa8)
2071 code
= 0x34 + (code
& 0x03);
2072 else if ((code
& 0xfc) == 0xac)
2073 code
= 0x38 + (code
& 0x03);
2077 /* Fix the opcode. */
2078 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2080 /* Fix the relocation's type. */
2081 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2084 /* The opcode got shorter too, so we have to fix the
2085 addend and offset too! */
2086 irel
->r_offset
-= 1;
2088 /* Delete three bytes of data. */
2089 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2090 irel
->r_offset
+ 1, 3))
2093 /* That will change things, so, we should relax again.
2094 Note that this is not required, and it may be slow. */
2098 /* mov (abs32),an -> mov (abs16),an
2099 mov (d32,sp),an -> mov (d32,sp),an
2100 mov (d32,sp),dn -> mov (d32,sp),dn
2101 movbu (d32,sp),dn -> movbu (d32,sp),dn
2102 movhu (d32,sp),dn -> movhu (d32,sp),dn
2103 add imm32,dn -> add imm16,dn
2104 cmp imm32,dn -> cmp imm16,dn
2105 add imm32,an -> add imm16,an
2106 cmp imm32,an -> cmp imm16,an
2107 and imm32,dn -> and imm32,dn
2108 or imm32,dn -> or imm32,dn
2109 xor imm32,dn -> xor imm32,dn
2110 btst imm32,dn -> btst imm32,dn */
2126 /* Note that we've changed the relocation contents, etc. */
2127 elf_section_data (sec
)->relocs
= internal_relocs
;
2130 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2131 free_contents
= NULL
;
2133 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2134 free_extsyms
= NULL
;
2136 /* Fix the opcode. */
2137 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2138 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2140 /* Fix the relocation's type. */
2141 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2144 /* Delete two bytes of data. */
2145 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2146 irel
->r_offset
+ 2, 2))
2149 /* That will change things, so, we should relax again.
2150 Note that this is not required, and it may be slow. */
2154 else if (code
== 0xfe)
2156 /* add imm32,sp -> add imm16,sp */
2158 /* Note that we've changed the relocation contents, etc. */
2159 elf_section_data (sec
)->relocs
= internal_relocs
;
2162 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2163 free_contents
= NULL
;
2165 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2166 free_extsyms
= NULL
;
2168 /* Fix the opcode. */
2169 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2170 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
2172 /* Fix the relocation's type. */
2173 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2176 /* Delete two bytes of data. */
2177 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2178 irel
->r_offset
+ 2, 2))
2181 /* That will change things, so, we should relax again.
2182 Note that this is not required, and it may be slow. */
2190 if (free_relocs
!= NULL
)
2196 if (free_contents
!= NULL
)
2198 if (! link_info
->keep_memory
)
2199 free (free_contents
);
2202 /* Cache the section contents for elf_link_input_bfd. */
2203 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2205 free_contents
= NULL
;
2208 if (free_extsyms
!= NULL
)
2210 if (! link_info
->keep_memory
)
2211 free (free_extsyms
);
2214 /* Cache the symbols for elf_link_input_bfd. */
2215 symtab_hdr
->contents
= extsyms
;
2217 free_extsyms
= NULL
;
2223 if (free_relocs
!= NULL
)
2225 if (free_contents
!= NULL
)
2226 free (free_contents
);
2227 if (free_extsyms
!= NULL
)
2228 free (free_extsyms
);
2232 /* Compute the stack size and movm arguments for the function
2233 referred to by HASH at address ADDR in section with
2234 contents CONTENTS, store the information in the hash table. */
2236 compute_function_info (abfd
, hash
, addr
, contents
)
2238 struct elf32_mn10300_link_hash_entry
*hash
;
2240 unsigned char *contents
;
2242 unsigned char byte1
, byte2
;
2243 /* We only care about a very small subset of the possible prologue
2244 sequences here. Basically we look for:
2246 movm [d2,d3,a2,a3],sp (optional)
2247 add <size>,sp (optional, and only for sizes which fit in an unsigned
2250 If we find anything else, we quit. */
2252 /* Look for movm [regs],sp */
2253 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2254 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2258 hash
->movm_args
= byte2
;
2260 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2261 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2264 /* Now look for the two stack adjustment variants. */
2265 if (byte1
== 0xf8 && byte2
== 0xfe)
2267 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
2268 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
2270 hash
->stack_size
= -temp
;
2272 else if (byte1
== 0xfa && byte2
== 0xfe)
2274 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
2275 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
2279 hash
->stack_size
= temp
;
2284 /* Delete some bytes from a section while relaxing. */
2287 mn10300_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
2293 Elf_Internal_Shdr
*symtab_hdr
;
2294 Elf32_External_Sym
*extsyms
;
2297 Elf_Internal_Rela
*irel
, *irelend
;
2298 Elf_Internal_Rela
*irelalign
;
2300 Elf32_External_Sym
*esym
, *esymend
;
2301 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2303 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2304 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2306 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2308 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2310 /* The deletion must stop at the next ALIGN reloc for an aligment
2311 power larger than the number of bytes we are deleting. */
2314 toaddr
= sec
->_cooked_size
;
2316 irel
= elf_section_data (sec
)->relocs
;
2317 irelend
= irel
+ sec
->reloc_count
;
2319 /* Actually delete the bytes. */
2320 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
2321 sec
->_cooked_size
-= count
;
2323 /* Adjust all the relocs. */
2324 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
2326 /* Get the new reloc address. */
2327 if ((irel
->r_offset
> addr
2328 && irel
->r_offset
< toaddr
))
2329 irel
->r_offset
-= count
;
2332 /* Adjust the local symbols defined in this section. */
2334 esymend
= esym
+ symtab_hdr
->sh_info
;
2335 for (; esym
< esymend
; esym
++)
2337 Elf_Internal_Sym isym
;
2339 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2341 if (isym
.st_shndx
== shndx
2342 && isym
.st_value
> addr
2343 && isym
.st_value
< toaddr
)
2345 isym
.st_value
-= count
;
2346 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
2350 /* Now adjust the global symbols defined in this section. */
2351 esym
= extsyms
+ symtab_hdr
->sh_info
;
2352 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
2353 for (index
= 0; esym
< esymend
; esym
++, index
++)
2355 Elf_Internal_Sym isym
;
2357 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2358 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
2359 (elf_sym_hashes (abfd
)[index
]);
2360 if (isym
.st_shndx
== shndx
2361 && ((sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2362 || (sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2363 && (sym_hash
)->root
.root
.u
.def
.section
== sec
2364 && (sym_hash
)->root
.root
.u
.def
.value
> addr
2365 && (sym_hash
)->root
.root
.u
.def
.value
< toaddr
)
2367 (sym_hash
)->root
.root
.u
.def
.value
-= count
;
2374 /* Return true if a symbol exists at the given address, else return
2377 mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
2380 Elf32_External_Sym
*extsyms
;
2383 Elf_Internal_Shdr
*symtab_hdr
;
2385 Elf32_External_Sym
*esym
, *esymend
;
2386 struct elf32_mn10300_link_hash_entry
**sym_hash
, **sym_hash_end
;
2388 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2389 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2391 /* Examine all the symbols. */
2393 esymend
= esym
+ symtab_hdr
->sh_info
;
2394 for (; esym
< esymend
; esym
++)
2396 Elf_Internal_Sym isym
;
2398 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2400 if (isym
.st_shndx
== shndx
2401 && isym
.st_value
== addr
)
2405 sym_hash
= (struct elf32_mn10300_link_hash_entry
**)(elf_sym_hashes (abfd
));
2406 sym_hash_end
= (sym_hash
2407 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2408 - symtab_hdr
->sh_info
));
2409 for (; sym_hash
< sym_hash_end
; sym_hash
++)
2411 if (((*sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2412 || (*sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2413 && (*sym_hash
)->root
.root
.u
.def
.section
== sec
2414 && (*sym_hash
)->root
.root
.u
.def
.value
== addr
)
2420 /* This is a version of bfd_generic_get_relocated_section_contents
2421 which uses mn10300_elf_relocate_section. */
2424 mn10300_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
2425 data
, relocateable
, symbols
)
2427 struct bfd_link_info
*link_info
;
2428 struct bfd_link_order
*link_order
;
2430 boolean relocateable
;
2433 Elf_Internal_Shdr
*symtab_hdr
;
2434 asection
*input_section
= link_order
->u
.indirect
.section
;
2435 bfd
*input_bfd
= input_section
->owner
;
2436 asection
**sections
= NULL
;
2437 Elf_Internal_Rela
*internal_relocs
= NULL
;
2438 Elf32_External_Sym
*external_syms
= NULL
;
2439 Elf_Internal_Sym
*internal_syms
= NULL
;
2441 /* We only need to handle the case of relaxing, or of having a
2442 particular set of section contents, specially. */
2444 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
2445 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
2450 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2452 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
2453 input_section
->_raw_size
);
2455 if ((input_section
->flags
& SEC_RELOC
) != 0
2456 && input_section
->reloc_count
> 0)
2458 Elf_Internal_Sym
*isymp
;
2460 Elf32_External_Sym
*esym
, *esymend
;
2462 if (symtab_hdr
->contents
!= NULL
)
2463 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2466 external_syms
= ((Elf32_External_Sym
*)
2467 bfd_malloc (symtab_hdr
->sh_info
2468 * sizeof (Elf32_External_Sym
)));
2469 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2471 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
2472 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
2473 symtab_hdr
->sh_info
, input_bfd
)
2474 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
2478 internal_relocs
= (_bfd_elf32_link_read_relocs
2479 (input_bfd
, input_section
, (PTR
) NULL
,
2480 (Elf_Internal_Rela
*) NULL
, false));
2481 if (internal_relocs
== NULL
)
2484 internal_syms
= ((Elf_Internal_Sym
*)
2485 bfd_malloc (symtab_hdr
->sh_info
2486 * sizeof (Elf_Internal_Sym
)));
2487 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2490 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
2491 * sizeof (asection
*));
2492 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
2495 isymp
= internal_syms
;
2497 esym
= external_syms
;
2498 esymend
= esym
+ symtab_hdr
->sh_info
;
2499 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
2503 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
2505 if (isymp
->st_shndx
== SHN_UNDEF
)
2506 isec
= bfd_und_section_ptr
;
2507 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
2508 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
2509 else if (isymp
->st_shndx
== SHN_ABS
)
2510 isec
= bfd_abs_section_ptr
;
2511 else if (isymp
->st_shndx
== SHN_COMMON
)
2512 isec
= bfd_com_section_ptr
;
2522 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
2523 input_section
, data
, internal_relocs
,
2524 internal_syms
, sections
))
2527 if (sections
!= NULL
)
2530 if (internal_syms
!= NULL
)
2531 free (internal_syms
);
2532 internal_syms
= NULL
;
2533 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2534 free (external_syms
);
2535 external_syms
= NULL
;
2536 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
2537 free (internal_relocs
);
2538 internal_relocs
= NULL
;
2544 if (internal_relocs
!= NULL
2545 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
2546 free (internal_relocs
);
2547 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2548 free (external_syms
);
2549 if (internal_syms
!= NULL
)
2550 free (internal_syms
);
2551 if (sections
!= NULL
)
2556 /* Assorted hash table functions. */
2558 /* Initialize an entry in the link hash table. */
2560 /* Create an entry in an MN10300 ELF linker hash table. */
2562 static struct bfd_hash_entry
*
2563 elf32_mn10300_link_hash_newfunc (entry
, table
, string
)
2564 struct bfd_hash_entry
*entry
;
2565 struct bfd_hash_table
*table
;
2568 struct elf32_mn10300_link_hash_entry
*ret
=
2569 (struct elf32_mn10300_link_hash_entry
*) entry
;
2571 /* Allocate the structure if it has not already been allocated by a
2573 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2574 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2575 bfd_hash_allocate (table
,
2576 sizeof (struct elf32_mn10300_link_hash_entry
)));
2577 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2578 return (struct bfd_hash_entry
*) ret
;
2580 /* Call the allocation method of the superclass. */
2581 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2582 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2584 if (ret
!= (struct elf32_mn10300_link_hash_entry
*) NULL
)
2586 ret
->direct_calls
= 0;
2587 ret
->stack_size
= 0;
2592 return (struct bfd_hash_entry
*) ret
;
2595 /* Create an mn10300 ELF linker hash table. */
2597 static struct bfd_link_hash_table
*
2598 elf32_mn10300_link_hash_table_create (abfd
)
2601 struct elf32_mn10300_link_hash_table
*ret
;
2603 ret
= ((struct elf32_mn10300_link_hash_table
*)
2604 bfd_alloc (abfd
, sizeof (struct elf32_mn10300_link_hash_table
)));
2605 if (ret
== (struct elf32_mn10300_link_hash_table
*) NULL
)
2608 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2609 elf32_mn10300_link_hash_newfunc
))
2611 bfd_release (abfd
, ret
);
2616 ret
->static_hash_table
2617 = ((struct elf32_mn10300_link_hash_table
*)
2618 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
2619 if (ret
->static_hash_table
== NULL
)
2621 bfd_release (abfd
, ret
);
2625 if (! _bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
2626 elf32_mn10300_link_hash_newfunc
))
2628 bfd_release (abfd
, ret
->static_hash_table
);
2629 bfd_release (abfd
, ret
);
2632 return &ret
->root
.root
;
2636 elf_mn10300_mach (flags
)
2639 switch (flags
& EF_MN10300_MACH
)
2641 case E_MN10300_MACH_MN10300
:
2643 return bfd_mach_mn10300
;
2645 /* start-sanitize-am33 */
2646 case E_MN10300_MACH_AM33
:
2647 return bfd_mach_am33
;
2648 /* end-sanitize-am33 */
2652 /* The final processing done just before writing out a MN10300 ELF object
2653 file. This gets the MN10300 architecture right based on the machine
2658 _bfd_mn10300_elf_final_write_processing (abfd
, linker
)
2664 Elf_Internal_Shdr
**hdrpp
;
2668 switch (bfd_get_mach (abfd
))
2671 case bfd_mach_mn10300
:
2672 val
= E_MN10300_MACH_MN10300
;
2675 /* start-sanitize-am33 */
2677 val
= E_MN10300_MACH_AM33
;
2679 /* end-sanitize-am33 */
2682 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
2683 elf_elfheader (abfd
)->e_flags
|= val
;
2687 _bfd_mn10300_elf_object_p (abfd
)
2690 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
2691 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
2695 /* Merge backend specific data from an object file to the output
2696 object file when linking. */
2699 _bfd_mn10300_elf_merge_private_bfd_data (ibfd
, obfd
)
2703 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
2704 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
2707 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
2708 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
2710 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
2711 bfd_get_mach (ibfd
)))
2719 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2720 #define TARGET_LITTLE_NAME "elf32-mn10300"
2721 #define ELF_ARCH bfd_arch_mn10300
2722 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2723 #define ELF_MAXPAGESIZE 0x1000
2725 #define elf_info_to_howto mn10300_info_to_howto
2726 #define elf_info_to_howto_rel 0
2727 #define elf_backend_can_gc_sections 1
2728 #define elf_backend_check_relocs mn10300_elf_check_relocs
2729 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2730 #define elf_backend_relocate_section mn10300_elf_relocate_section
2731 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2732 #define bfd_elf32_bfd_get_relocated_section_contents \
2733 mn10300_elf_get_relocated_section_contents
2734 #define bfd_elf32_bfd_link_hash_table_create \
2735 elf32_mn10300_link_hash_table_create
2737 #define elf_symbol_leading_char '_'
2739 /* So we can set bits in e_flags. */
2740 #define elf_backend_final_write_processing \
2741 _bfd_mn10300_elf_final_write_processing
2742 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2744 #define bfd_elf32_bfd_merge_private_bfd_data \
2745 _bfd_mn10300_elf_merge_private_bfd_data
2748 #include "elf32-target.h"