1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #include "elf/mn10300.h"
26 struct elf32_mn10300_link_hash_entry
28 /* The basic elf link hash table entry. */
29 struct elf_link_hash_entry root
;
31 /* For function symbols, the number of times this function is
32 called directly (ie by name). */
33 unsigned int direct_calls
;
35 /* For function symbols, the size of this function's stack
36 (if <= 255 bytes). We stuff this into "call" instructions
37 to this target when it's valid and profitable to do so. */
38 unsigned char stack_size
;
40 /* For function symbols, arguments (if any) for movm instruction
41 in the prologue. We stuff this value into "call" instructions
42 to the target when it's valid and profitable to do so. */
43 unsigned char movm_args
;
45 /* When set, convert all "call" instructions to this target into "calls"
47 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
49 /* Used to mark functions which have had redundant parts of their
51 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
55 /* We derive a hash table from the main elf linker hash table so
56 we can store state variables and a secondary hash table without
57 resorting to global variables. */
58 struct elf32_mn10300_link_hash_table
60 /* The main hash table. */
61 struct elf_link_hash_table root
;
63 /* A hash table for static functions. We could derive a new hash table
64 instead of using the full elf32_mn10300_link_hash_table if we wanted
65 to save some memory. */
66 struct elf32_mn10300_link_hash_table
*static_hash_table
;
68 /* Random linker state flags. */
69 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
73 /* For MN10300 linker hash table. */
75 /* Get the MN10300 ELF linker hash table from a link_info structure. */
77 #define elf32_mn10300_hash_table(p) \
78 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
80 #define elf32_mn10300_link_hash_traverse(table, func, info) \
81 (elf_link_hash_traverse \
83 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
86 static struct bfd_hash_entry
*elf32_mn10300_link_hash_newfunc
87 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
88 static struct bfd_link_hash_table
*elf32_mn10300_link_hash_table_create
91 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
92 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
93 static void mn10300_info_to_howto
94 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rela
*));
95 static boolean mn10300_elf_check_relocs
96 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
97 const Elf_Internal_Rela
*));
98 static asection
*mn10300_elf_gc_mark_hook
99 PARAMS ((bfd
*, struct bfd_link_info
*info
, Elf_Internal_Rela
*,
100 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
101 static boolean mn10300_elf_relax_delete_bytes
102 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
103 static boolean mn10300_elf_symbol_address_p
104 PARAMS ((bfd
*, asection
*, Elf32_External_Sym
*, bfd_vma
));
105 static boolean elf32_mn10300_finish_hash_table_entry
106 PARAMS ((struct bfd_hash_entry
*, PTR
));
107 static void compute_function_info
108 PARAMS ((bfd
*, struct elf32_mn10300_link_hash_entry
*,
109 bfd_vma
, unsigned char *));
111 /* We have to use RELA instructions since md_apply_fix3 in the assembler
112 does absolutely nothing. */
125 /* These are GNU extensions to enable C++ vtable garbage collection. */
126 R_MN10300_GNU_VTINHERIT
,
127 R_MN10300_GNU_VTENTRY
,
132 static reloc_howto_type elf_mn10300_howto_table
[] =
134 /* Dummy relocation. Does nothing. */
135 HOWTO (R_MN10300_NONE
,
141 complain_overflow_bitfield
,
142 bfd_elf_generic_reloc
,
148 /* Standard 32 bit reloc. */
155 complain_overflow_bitfield
,
156 bfd_elf_generic_reloc
,
162 /* Standard 16 bit reloc. */
169 complain_overflow_bitfield
,
170 bfd_elf_generic_reloc
,
176 /* Standard 8 bit reloc. */
183 complain_overflow_bitfield
,
184 bfd_elf_generic_reloc
,
190 /* Standard 32bit pc-relative reloc. */
191 HOWTO (R_MN10300_PCREL32
,
197 complain_overflow_bitfield
,
198 bfd_elf_generic_reloc
,
204 /* Standard 16bit pc-relative reloc. */
205 HOWTO (R_MN10300_PCREL16
,
211 complain_overflow_bitfield
,
212 bfd_elf_generic_reloc
,
218 /* Standard 8 pc-relative reloc. */
219 HOWTO (R_MN10300_PCREL8
,
225 complain_overflow_bitfield
,
226 bfd_elf_generic_reloc
,
233 /* GNU extension to record C++ vtable hierarchy */
234 HOWTO (R_MN10300_GNU_VTINHERIT
, /* type */
236 0, /* size (0 = byte, 1 = short, 2 = long) */
238 false, /* pc_relative */
240 complain_overflow_dont
, /* complain_on_overflow */
241 NULL
, /* special_function */
242 "R_MN10300_GNU_VTINHERIT", /* name */
243 false, /* partial_inplace */
246 false), /* pcrel_offset */
248 /* GNU extension to record C++ vtable member usage */
249 HOWTO (R_MN10300_GNU_VTENTRY
, /* type */
251 0, /* size (0 = byte, 1 = short, 2 = long) */
253 false, /* pc_relative */
255 complain_overflow_dont
, /* complain_on_overflow */
256 NULL
, /* special_function */
257 "R_MN10300_GNU_VTENTRY", /* name */
258 false, /* partial_inplace */
261 false), /* pcrel_offset */
264 struct mn10300_reloc_map
266 unsigned char bfd_reloc_val
;
267 unsigned char elf_reloc_val
;
270 static const struct mn10300_reloc_map mn10300_reloc_map
[] =
272 { BFD_RELOC_NONE
, R_MN10300_NONE
, },
273 { BFD_RELOC_32
, R_MN10300_32
, },
274 { BFD_RELOC_16
, R_MN10300_16
, },
275 { BFD_RELOC_8
, R_MN10300_8
, },
276 { BFD_RELOC_32_PCREL
, R_MN10300_PCREL32
, },
277 { BFD_RELOC_16_PCREL
, R_MN10300_PCREL16
, },
278 { BFD_RELOC_8_PCREL
, R_MN10300_PCREL8
, },
279 { BFD_RELOC_VTABLE_INHERIT
, R_MN10300_GNU_VTINHERIT
},
280 { BFD_RELOC_VTABLE_ENTRY
, R_MN10300_GNU_VTENTRY
},
283 static reloc_howto_type
*
284 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
286 bfd_reloc_code_real_type code
;
291 i
< sizeof (mn10300_reloc_map
) / sizeof (struct mn10300_reloc_map
);
294 if (mn10300_reloc_map
[i
].bfd_reloc_val
== code
)
295 return &elf_mn10300_howto_table
[mn10300_reloc_map
[i
].elf_reloc_val
];
301 /* Set the howto pointer for an MN10300 ELF reloc. */
304 mn10300_info_to_howto (abfd
, cache_ptr
, dst
)
307 Elf32_Internal_Rela
*dst
;
311 r_type
= ELF32_R_TYPE (dst
->r_info
);
312 BFD_ASSERT (r_type
< (unsigned int) R_MN10300_MAX
);
313 cache_ptr
->howto
= &elf_mn10300_howto_table
[r_type
];
316 /* Look through the relocs for a section during the first phase.
317 Since we don't do .gots or .plts, we just need to consider the
318 virtual table relocs for gc. */
321 mn10300_elf_check_relocs (abfd
, info
, sec
, relocs
)
323 struct bfd_link_info
*info
;
325 const Elf_Internal_Rela
*relocs
;
327 Elf_Internal_Shdr
*symtab_hdr
;
328 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
329 const Elf_Internal_Rela
*rel
;
330 const Elf_Internal_Rela
*rel_end
;
332 if (info
->relocateable
)
335 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
336 sym_hashes
= elf_sym_hashes (abfd
);
337 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof(Elf32_External_Sym
);
338 if (!elf_bad_symtab (abfd
))
339 sym_hashes_end
-= symtab_hdr
->sh_info
;
341 rel_end
= relocs
+ sec
->reloc_count
;
342 for (rel
= relocs
; rel
< rel_end
; rel
++)
344 struct elf_link_hash_entry
*h
;
345 unsigned long r_symndx
;
347 r_symndx
= ELF32_R_SYM (rel
->r_info
);
348 if (r_symndx
< symtab_hdr
->sh_info
)
351 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
353 switch (ELF32_R_TYPE (rel
->r_info
))
355 /* This relocation describes the C++ object vtable hierarchy.
356 Reconstruct it for later use during GC. */
357 case R_MN10300_GNU_VTINHERIT
:
358 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
362 /* This relocation describes which C++ vtable entries are actually
363 used. Record for later use during GC. */
364 case R_MN10300_GNU_VTENTRY
:
365 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
374 /* Return the section that should be marked against GC for a given
378 mn10300_elf_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
380 struct bfd_link_info
*info
;
381 Elf_Internal_Rela
*rel
;
382 struct elf_link_hash_entry
*h
;
383 Elf_Internal_Sym
*sym
;
387 switch (ELF32_R_TYPE (rel
->r_info
))
389 case R_MN10300_GNU_VTINHERIT
:
390 case R_MN10300_GNU_VTENTRY
:
394 switch (h
->root
.type
)
396 case bfd_link_hash_defined
:
397 case bfd_link_hash_defweak
:
398 return h
->root
.u
.def
.section
;
400 case bfd_link_hash_common
:
401 return h
->root
.u
.c
.p
->section
;
407 if (!(elf_bad_symtab (abfd
)
408 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
409 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
410 && sym
->st_shndx
!= SHN_COMMON
))
412 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
419 /* Perform a relocation as part of a final link. */
420 static bfd_reloc_status_type
421 mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
422 input_section
, contents
, offset
, value
,
423 addend
, info
, sym_sec
, is_local
)
424 reloc_howto_type
*howto
;
427 asection
*input_section
;
432 struct bfd_link_info
*info
;
436 unsigned long r_type
= howto
->type
;
437 bfd_byte
*hit_data
= contents
+ offset
;
446 bfd_put_32 (input_bfd
, value
, hit_data
);
452 if ((long)value
> 0x7fff || (long)value
< -0x8000)
453 return bfd_reloc_overflow
;
455 bfd_put_16 (input_bfd
, value
, hit_data
);
461 if ((long)value
> 0x7fff || (long)value
< -0x8000)
462 return bfd_reloc_overflow
;
464 bfd_put_8 (input_bfd
, value
, hit_data
);
467 case R_MN10300_PCREL8
:
468 value
-= (input_section
->output_section
->vma
469 + input_section
->output_offset
);
473 if ((long)value
> 0xff || (long)value
< -0x100)
474 return bfd_reloc_overflow
;
476 bfd_put_8 (input_bfd
, value
, hit_data
);
479 case R_MN10300_PCREL16
:
480 value
-= (input_section
->output_section
->vma
481 + input_section
->output_offset
);
485 if ((long)value
> 0xffff || (long)value
< -0x10000)
486 return bfd_reloc_overflow
;
488 bfd_put_16 (input_bfd
, value
, hit_data
);
491 case R_MN10300_PCREL32
:
492 value
-= (input_section
->output_section
->vma
493 + input_section
->output_offset
);
497 bfd_put_32 (input_bfd
, value
, hit_data
);
500 case R_MN10300_GNU_VTINHERIT
:
501 case R_MN10300_GNU_VTENTRY
:
505 return bfd_reloc_notsupported
;
510 /* Relocate an MN10300 ELF section. */
512 mn10300_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
513 contents
, relocs
, local_syms
, local_sections
)
515 struct bfd_link_info
*info
;
517 asection
*input_section
;
519 Elf_Internal_Rela
*relocs
;
520 Elf_Internal_Sym
*local_syms
;
521 asection
**local_sections
;
523 Elf_Internal_Shdr
*symtab_hdr
;
524 struct elf32_mn10300_link_hash_entry
**sym_hashes
;
525 Elf_Internal_Rela
*rel
, *relend
;
527 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
528 sym_hashes
= (struct elf32_mn10300_link_hash_entry
**)
529 (elf_sym_hashes (input_bfd
));
532 relend
= relocs
+ input_section
->reloc_count
;
533 for (; rel
< relend
; rel
++)
536 reloc_howto_type
*howto
;
537 unsigned long r_symndx
;
538 Elf_Internal_Sym
*sym
;
540 struct elf32_mn10300_link_hash_entry
*h
;
542 bfd_reloc_status_type r
;
544 r_symndx
= ELF32_R_SYM (rel
->r_info
);
545 r_type
= ELF32_R_TYPE (rel
->r_info
);
546 howto
= elf_mn10300_howto_table
+ r_type
;
548 /* Just skip the vtable gc relocs. */
549 if (r_type
== R_MN10300_GNU_VTINHERIT
550 || r_type
== R_MN10300_GNU_VTENTRY
)
553 if (info
->relocateable
)
555 /* This is a relocateable link. We don't have to change
556 anything, unless the reloc is against a section symbol,
557 in which case we have to adjust according to where the
558 section symbol winds up in the output section. */
559 if (r_symndx
< symtab_hdr
->sh_info
)
561 sym
= local_syms
+ r_symndx
;
562 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
564 sec
= local_sections
[r_symndx
];
565 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
572 /* This is a final link. */
576 if (r_symndx
< symtab_hdr
->sh_info
)
578 sym
= local_syms
+ r_symndx
;
579 sec
= local_sections
[r_symndx
];
580 relocation
= (sec
->output_section
->vma
586 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
587 while (h
->root
.type
== bfd_link_hash_indirect
588 || h
->root
.type
== bfd_link_hash_warning
)
589 h
= (struct elf32_mn10300_link_hash_entry
*) h
->root
.root
.u
.i
.link
;
590 if (h
->root
.root
.type
== bfd_link_hash_defined
591 || h
->root
.root
.type
== bfd_link_hash_defweak
)
593 sec
= h
->root
.root
.u
.def
.section
;
594 relocation
= (h
->root
.root
.u
.def
.value
595 + sec
->output_section
->vma
596 + sec
->output_offset
);
598 else if (h
->root
.root
.type
== bfd_link_hash_undefweak
)
602 if (! ((*info
->callbacks
->undefined_symbol
)
603 (info
, h
->root
.root
.root
.string
, input_bfd
,
604 input_section
, rel
->r_offset
)))
610 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
612 contents
, rel
->r_offset
,
613 relocation
, rel
->r_addend
,
614 info
, sec
, h
== NULL
);
616 if (r
!= bfd_reloc_ok
)
619 const char *msg
= (const char *)0;
622 name
= h
->root
.root
.root
.string
;
625 name
= (bfd_elf_string_from_elf_section
626 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
627 if (name
== NULL
|| *name
== '\0')
628 name
= bfd_section_name (input_bfd
, sec
);
633 case bfd_reloc_overflow
:
634 if (! ((*info
->callbacks
->reloc_overflow
)
635 (info
, name
, howto
->name
, (bfd_vma
) 0,
636 input_bfd
, input_section
, rel
->r_offset
)))
640 case bfd_reloc_undefined
:
641 if (! ((*info
->callbacks
->undefined_symbol
)
642 (info
, name
, input_bfd
, input_section
,
647 case bfd_reloc_outofrange
:
648 msg
= _("internal error: out of range error");
651 case bfd_reloc_notsupported
:
652 msg
= _("internal error: unsupported relocation error");
655 case bfd_reloc_dangerous
:
656 msg
= _("internal error: dangerous error");
660 msg
= _("internal error: unknown error");
664 if (!((*info
->callbacks
->warning
)
665 (info
, msg
, name
, input_bfd
, input_section
,
676 /* Finish initializing one hash table entry. */
678 elf32_mn10300_finish_hash_table_entry (gen_entry
, in_args
)
679 struct bfd_hash_entry
*gen_entry
;
682 struct elf32_mn10300_link_hash_entry
*entry
;
683 unsigned int byte_count
= 0;
685 entry
= (struct elf32_mn10300_link_hash_entry
*)gen_entry
;
687 /* If we already know we want to convert "call" to "calls" for calls
688 to this symbol, then return now. */
689 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
692 /* If there are no named calls to this symbol, or there's nothing we
693 can move from the function itself into the "call" instruction, then
694 note that all "call" instructions should be converted into "calls"
695 instructions and return. */
696 if (entry
->direct_calls
== 0
697 || (entry
->stack_size
== 0 && entry
->movm_args
== 0))
699 /* Make a note that we should convert "call" instructions to "calls"
700 instructions for calls to this symbol. */
701 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
705 /* We may be able to move some instructions from the function itself into
706 the "call" instruction. Count how many bytes we might be able to
707 eliminate in the function itself. */
709 /* A movm instruction is two bytes. */
710 if (entry
->movm_args
)
713 /* Count the insn to allocate stack space too. */
714 if (entry
->stack_size
> 0 && entry
->stack_size
<= 128)
716 else if (entry
->stack_size
> 0 && entry
->stack_size
< 256)
719 /* If using "call" will result in larger code, then turn all
720 the associated "call" instructions into "calls" instrutions. */
721 if (byte_count
< entry
->direct_calls
)
722 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
724 /* This routine never fails. */
728 /* This function handles relaxing for the mn10300.
730 There's quite a few relaxing opportunites available on the mn10300:
732 * calls:32 -> calls:16 2 bytes
733 * call:32 -> call:16 2 bytes
735 * call:32 -> calls:32 1 byte
736 * call:16 -> calls:16 1 byte
737 * These are done anytime using "calls" would result
738 in smaller code, or when necessary to preserve the
739 meaning of the program.
743 * In some circumstances we can move instructions
744 from a function prologue into a "call" instruction.
745 This is only done if the resulting code is no larger
746 than the original code.
749 * jmp:32 -> jmp:16 2 bytes
750 * jmp:16 -> bra:8 1 byte
752 * If the previous instruction is a conditional branch
753 around the jump/bra, we may be able to reverse its condition
754 and change its target to the jump's target. The jump/bra
755 can then be deleted. 2 bytes
757 * mov abs32 -> mov abs16 1 or 2 bytes
759 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
760 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
762 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
763 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
765 We don't handle imm16->imm8 or d16->d8 as they're very rare
766 and somewhat more difficult to support. */
769 mn10300_elf_relax_section (abfd
, sec
, link_info
, again
)
772 struct bfd_link_info
*link_info
;
775 Elf_Internal_Shdr
*symtab_hdr
;
776 Elf_Internal_Rela
*internal_relocs
= NULL
;
777 Elf_Internal_Rela
*free_relocs
= NULL
;
778 Elf_Internal_Rela
*irel
, *irelend
;
779 bfd_byte
*contents
= NULL
;
780 bfd_byte
*free_contents
= NULL
;
781 Elf32_External_Sym
*extsyms
= NULL
;
782 Elf32_External_Sym
*free_extsyms
= NULL
;
783 struct elf32_mn10300_link_hash_table
*hash_table
;
785 /* Assume nothing changes. */
788 /* We need a pointer to the mn10300 specific hash table. */
789 hash_table
= elf32_mn10300_hash_table (link_info
);
791 /* Initialize fields in each hash table entry the first time through. */
792 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
796 /* Iterate over all the input bfds. */
797 for (input_bfd
= link_info
->input_bfds
;
799 input_bfd
= input_bfd
->link_next
)
803 /* We're going to need all the symbols for each bfd. */
804 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
806 /* Get cached copy if it exists. */
807 if (symtab_hdr
->contents
!= NULL
)
808 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
811 /* Go get them off disk. */
812 extsyms
= ((Elf32_External_Sym
*)
813 bfd_malloc (symtab_hdr
->sh_size
));
816 free_extsyms
= extsyms
;
817 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
818 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
819 != symtab_hdr
->sh_size
))
823 /* Iterate over each section in this bfd. */
824 for (section
= input_bfd
->sections
;
826 section
= section
->next
)
828 struct elf32_mn10300_link_hash_entry
*hash
;
829 Elf_Internal_Sym
*sym
;
831 const char *sym_name
;
833 Elf_Internal_Shdr
*hdr
;
835 /* Get cached copy of section contents if it exists. */
836 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
837 contents
= elf_section_data (section
)->this_hdr
.contents
;
838 else if (section
->_raw_size
!= 0)
840 /* Go get them off disk. */
841 contents
= (bfd_byte
*)bfd_malloc (section
->_raw_size
);
842 if (contents
== NULL
)
844 free_contents
= contents
;
846 if (!bfd_get_section_contents (input_bfd
, section
,
847 contents
, (file_ptr
) 0,
854 free_contents
= NULL
;
857 /* If there aren't any relocs, then there's nothing to do. */
858 if ((section
->flags
& SEC_RELOC
) != 0
859 && section
->reloc_count
!= 0)
862 /* Get a copy of the native relocations. */
863 internal_relocs
= (_bfd_elf32_link_read_relocs
864 (input_bfd
, section
, (PTR
) NULL
,
865 (Elf_Internal_Rela
*) NULL
,
866 link_info
->keep_memory
));
867 if (internal_relocs
== NULL
)
869 if (! link_info
->keep_memory
)
870 free_relocs
= internal_relocs
;
872 /* Now examine each relocation. */
873 irel
= internal_relocs
;
874 irelend
= irel
+ section
->reloc_count
;
875 for (; irel
< irelend
; irel
++)
878 unsigned long r_index
;
881 r_type
= ELF32_R_TYPE (irel
->r_info
);
882 r_index
= ELF32_R_SYM (irel
->r_info
);
884 if (r_type
< 0 || r_type
>= (int)R_MN10300_MAX
)
887 /* We need the name and hash table entry of the target
893 if (r_index
< symtab_hdr
->sh_info
)
895 /* A local symbol. */
896 Elf_Internal_Sym isym
;
898 bfd_elf32_swap_symbol_in (input_bfd
,
899 extsyms
+ r_index
, &isym
);
901 if (isym
.st_shndx
== SHN_UNDEF
)
902 sym_sec
= bfd_und_section_ptr
;
903 else if (isym
.st_shndx
> 0
904 && isym
.st_shndx
< SHN_LORESERVE
)
906 = bfd_section_from_elf_index (input_bfd
,
908 else if (isym
.st_shndx
== SHN_ABS
)
909 sym_sec
= bfd_abs_section_ptr
;
910 else if (isym
.st_shndx
== SHN_COMMON
)
911 sym_sec
= bfd_com_section_ptr
;
913 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
917 /* If it isn't a function, then we don't care
919 if (r_index
< symtab_hdr
->sh_info
920 && ELF_ST_TYPE (isym
.st_info
) != STT_FUNC
)
923 /* Tack on an ID so we can uniquely identify this
924 local symbol in the global hash table. */
925 new_name
= alloca (strlen (sym_name
) + 10);
926 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
929 hash
= (struct elf32_mn10300_link_hash_entry
*)
930 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
936 r_index
-= symtab_hdr
->sh_info
;
937 hash
= (struct elf32_mn10300_link_hash_entry
*)
938 elf_sym_hashes (input_bfd
)[r_index
];
941 /* If this is not a "call" instruction, then we
942 should convert "call" instructions to "calls"
944 code
= bfd_get_8 (input_bfd
,
945 contents
+ irel
->r_offset
- 1);
946 if (code
!= 0xdd && code
!= 0xcd)
947 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
949 /* If this is a jump/call, then bump the direct_calls
950 counter. Else force "call" to "calls" conversions. */
951 if (r_type
== R_MN10300_PCREL32
952 || r_type
== R_MN10300_PCREL16
)
953 hash
->direct_calls
++;
955 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
959 /* Now look at the actual contents to get the stack size,
960 and a list of what registers were saved in the prologue
962 if ((section
->flags
& SEC_CODE
) != 0)
965 Elf32_External_Sym
*esym
, *esymend
;
968 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
972 /* Look at each function defined in this section and
973 update info for that function. */
975 esymend
= esym
+ symtab_hdr
->sh_info
;
976 for (; esym
< esymend
; esym
++)
978 Elf_Internal_Sym isym
;
980 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
981 if (isym
.st_shndx
== shndx
982 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
)
984 if (isym
.st_shndx
== SHN_UNDEF
)
985 sym_sec
= bfd_und_section_ptr
;
986 else if (isym
.st_shndx
> 0
987 && isym
.st_shndx
< SHN_LORESERVE
)
989 = bfd_section_from_elf_index (input_bfd
,
991 else if (isym
.st_shndx
== SHN_ABS
)
992 sym_sec
= bfd_abs_section_ptr
;
993 else if (isym
.st_shndx
== SHN_COMMON
)
994 sym_sec
= bfd_com_section_ptr
;
996 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1000 /* Tack on an ID so we can uniquely identify this
1001 local symbol in the global hash table. */
1002 new_name
= alloca (strlen (sym_name
) + 10);
1003 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1004 sym_name
= new_name
;
1006 hash
= (struct elf32_mn10300_link_hash_entry
*)
1007 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1010 compute_function_info (input_bfd
, hash
,
1011 isym
.st_value
, contents
);
1015 esym
= extsyms
+ symtab_hdr
->sh_info
;
1016 esymend
= extsyms
+ (symtab_hdr
->sh_size
1017 / sizeof (Elf32_External_Sym
));
1018 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1020 Elf_Internal_Sym isym
;
1022 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1023 hash
= (struct elf32_mn10300_link_hash_entry
*)
1024 elf_sym_hashes (input_bfd
)[idx
];
1025 if (isym
.st_shndx
== shndx
1026 && ELF_ST_TYPE (isym
.st_info
) == STT_FUNC
1027 && (hash
)->root
.root
.u
.def
.section
== section
1028 && ((hash
)->root
.root
.type
== bfd_link_hash_defined
1029 || (hash
)->root
.root
.type
== bfd_link_hash_defweak
))
1030 compute_function_info (input_bfd
, hash
,
1031 (hash
)->root
.root
.u
.def
.value
,
1036 /* Cache or free any memory we allocated for the relocs. */
1037 if (free_relocs
!= NULL
)
1043 /* Cache or free any memory we allocated for the contents. */
1044 if (free_contents
!= NULL
)
1046 if (! link_info
->keep_memory
)
1047 free (free_contents
);
1050 /* Cache the section contents for elf_link_input_bfd. */
1051 elf_section_data (section
)->this_hdr
.contents
= contents
;
1053 free_contents
= NULL
;
1057 /* Cache or free any memory we allocated for the symbols. */
1058 if (free_extsyms
!= NULL
)
1060 if (! link_info
->keep_memory
)
1061 free (free_extsyms
);
1064 /* Cache the symbols for elf_link_input_bfd. */
1065 symtab_hdr
->contents
= extsyms
;
1067 free_extsyms
= NULL
;
1071 /* Now iterate on each symbol in the hash table and perform
1072 the final initialization steps on each. */
1073 elf32_mn10300_link_hash_traverse (hash_table
,
1074 elf32_mn10300_finish_hash_table_entry
,
1076 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
1077 elf32_mn10300_finish_hash_table_entry
,
1080 /* All entries in the hash table are fully initialized. */
1081 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
1083 /* Now that everything has been initialized, go through each
1084 code section and delete any prologue insns which will be
1085 redundant because their operations will be performed by
1086 a "call" instruction. */
1087 for (input_bfd
= link_info
->input_bfds
;
1089 input_bfd
= input_bfd
->link_next
)
1093 /* We're going to need all the symbols for each bfd. */
1094 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1096 /* Get cached copy if it exists. */
1097 if (symtab_hdr
->contents
!= NULL
)
1098 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1101 /* Go get them off disk. */
1102 extsyms
= ((Elf32_External_Sym
*)
1103 bfd_malloc (symtab_hdr
->sh_size
));
1104 if (extsyms
== NULL
)
1106 free_extsyms
= extsyms
;
1107 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1108 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, input_bfd
)
1109 != symtab_hdr
->sh_size
))
1113 /* Walk over each section in this bfd. */
1114 for (section
= input_bfd
->sections
;
1116 section
= section
->next
)
1119 Elf32_External_Sym
*esym
, *esymend
;
1122 /* Skip non-code sections and empty sections. */
1123 if ((section
->flags
& SEC_CODE
) == 0 || section
->_raw_size
== 0)
1126 if (section
->reloc_count
!= 0)
1128 /* Get a copy of the native relocations. */
1129 internal_relocs
= (_bfd_elf32_link_read_relocs
1130 (input_bfd
, section
, (PTR
) NULL
,
1131 (Elf_Internal_Rela
*) NULL
,
1132 link_info
->keep_memory
));
1133 if (internal_relocs
== NULL
)
1135 if (! link_info
->keep_memory
)
1136 free_relocs
= internal_relocs
;
1139 /* Get cached copy of section contents if it exists. */
1140 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
1141 contents
= elf_section_data (section
)->this_hdr
.contents
;
1144 /* Go get them off disk. */
1145 contents
= (bfd_byte
*)bfd_malloc (section
->_raw_size
);
1146 if (contents
== NULL
)
1148 free_contents
= contents
;
1150 if (!bfd_get_section_contents (input_bfd
, section
,
1151 contents
, (file_ptr
) 0,
1152 section
->_raw_size
))
1157 shndx
= _bfd_elf_section_from_bfd_section (input_bfd
, section
);
1159 /* Now look for any function in this section which needs
1160 insns deleted from its prologue. */
1162 esymend
= esym
+ symtab_hdr
->sh_info
;
1163 for (; esym
< esymend
; esym
++)
1165 Elf_Internal_Sym isym
;
1166 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1168 const char *sym_name
;
1169 Elf_Internal_Shdr
*hdr
;
1172 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1174 if (isym
.st_shndx
!= shndx
)
1177 if (isym
.st_shndx
== SHN_UNDEF
)
1178 sym_sec
= bfd_und_section_ptr
;
1179 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1181 = bfd_section_from_elf_index (input_bfd
, isym
.st_shndx
);
1182 else if (isym
.st_shndx
== SHN_ABS
)
1183 sym_sec
= bfd_abs_section_ptr
;
1184 else if (isym
.st_shndx
== SHN_COMMON
)
1185 sym_sec
= bfd_com_section_ptr
;
1187 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1188 symtab_hdr
->sh_link
,
1191 /* Tack on an ID so we can uniquely identify this
1192 local symbol in the global hash table. */
1193 new_name
= alloca (strlen (sym_name
) + 10);
1194 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1195 sym_name
= new_name
;
1197 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1198 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1202 if (sym_hash
== NULL
)
1205 if (! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1206 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1210 /* Note that we've changed things. */
1211 elf_section_data (section
)->relocs
= internal_relocs
;
1214 elf_section_data (section
)->this_hdr
.contents
= contents
;
1215 free_contents
= NULL
;
1217 symtab_hdr
->contents
= (bfd_byte
*)extsyms
;
1218 free_extsyms
= NULL
;
1220 /* Count how many bytes we're going to delete. */
1221 if (sym_hash
->movm_args
)
1224 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1226 else if (sym_hash
->stack_size
1227 && sym_hash
->stack_size
< 256)
1230 /* Note that we've deleted prologue bytes for this
1232 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1234 /* Actually delete the bytes. */
1235 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1241 /* Something changed. Not strictly necessary, but
1242 may lead to more relaxing opportunities. */
1247 /* Look for any global functions in this section which
1248 need insns deleted from their prologues. */
1249 esym
= extsyms
+ symtab_hdr
->sh_info
;
1250 esymend
= extsyms
+ (symtab_hdr
->sh_size
1251 / sizeof (Elf32_External_Sym
));
1252 for (idx
= 0; esym
< esymend
; esym
++, idx
++)
1254 Elf_Internal_Sym isym
;
1255 struct elf32_mn10300_link_hash_entry
*sym_hash
;
1257 bfd_elf32_swap_symbol_in (input_bfd
, esym
, &isym
);
1258 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
1259 (elf_sym_hashes (input_bfd
)[idx
]);
1260 if (isym
.st_shndx
== shndx
1261 && (sym_hash
)->root
.root
.u
.def
.section
== section
1262 && ! ((sym_hash
)->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
1263 && ! ((sym_hash
)->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
1267 /* Note that we've changed things. */
1268 elf_section_data (section
)->relocs
= internal_relocs
;
1271 elf_section_data (section
)->this_hdr
.contents
= contents
;
1272 free_contents
= NULL
;
1274 symtab_hdr
->contents
= (bfd_byte
*)extsyms
;
1275 free_extsyms
= NULL
;
1277 /* Count how many bytes we're going to delete. */
1278 if (sym_hash
->movm_args
)
1281 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
1283 else if (sym_hash
->stack_size
1284 && sym_hash
->stack_size
< 256)
1287 /* Note that we've deleted prologue bytes for this
1289 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
1291 /* Actually delete the bytes. */
1292 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
1294 (sym_hash
)->root
.root
.u
.def
.value
,
1298 /* Something changed. Not strictly necessary, but
1299 may lead to more relaxing opportunities. */
1304 /* Cache or free any memory we allocated for the relocs. */
1305 if (free_relocs
!= NULL
)
1311 /* Cache or free any memory we allocated for the contents. */
1312 if (free_contents
!= NULL
)
1314 if (! link_info
->keep_memory
)
1315 free (free_contents
);
1318 /* Cache the section contents for elf_link_input_bfd. */
1319 elf_section_data (section
)->this_hdr
.contents
= contents
;
1321 free_contents
= NULL
;
1325 /* Cache or free any memory we allocated for the symbols. */
1326 if (free_extsyms
!= NULL
)
1328 if (! link_info
->keep_memory
)
1329 free (free_extsyms
);
1332 /* Cache the symbols for elf_link_input_bfd. */
1333 symtab_hdr
->contents
= extsyms
;
1335 free_extsyms
= NULL
;
1341 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1344 internal_relocs
= NULL
;
1346 free_contents
= NULL
;
1347 free_extsyms
= NULL
;
1349 /* We don't have to do anything for a relocateable link, if
1350 this section does not have relocs, or if this is not a
1352 if (link_info
->relocateable
1353 || (sec
->flags
& SEC_RELOC
) == 0
1354 || sec
->reloc_count
== 0
1355 || (sec
->flags
& SEC_CODE
) == 0)
1358 /* If this is the first time we have been called for this section,
1359 initialize the cooked size. */
1360 if (sec
->_cooked_size
== 0)
1361 sec
->_cooked_size
= sec
->_raw_size
;
1363 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1365 /* Get a copy of the native relocations. */
1366 internal_relocs
= (_bfd_elf32_link_read_relocs
1367 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
1368 link_info
->keep_memory
));
1369 if (internal_relocs
== NULL
)
1371 if (! link_info
->keep_memory
)
1372 free_relocs
= internal_relocs
;
1374 /* Walk through them looking for relaxing opportunities. */
1375 irelend
= internal_relocs
+ sec
->reloc_count
;
1376 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1379 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
1381 /* If this isn't something that can be relaxed, then ignore
1383 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
1384 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
1385 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
1388 /* Get the section contents if we haven't done so already. */
1389 if (contents
== NULL
)
1391 /* Get cached copy if it exists. */
1392 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1393 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1396 /* Go get them off disk. */
1397 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
1398 if (contents
== NULL
)
1400 free_contents
= contents
;
1402 if (! bfd_get_section_contents (abfd
, sec
, contents
,
1403 (file_ptr
) 0, sec
->_raw_size
))
1408 /* Read this BFD's symbols if we haven't done so already. */
1409 if (extsyms
== NULL
)
1411 /* Get cached copy if it exists. */
1412 if (symtab_hdr
->contents
!= NULL
)
1413 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1416 /* Go get them off disk. */
1417 extsyms
= ((Elf32_External_Sym
*)
1418 bfd_malloc (symtab_hdr
->sh_size
));
1419 if (extsyms
== NULL
)
1421 free_extsyms
= extsyms
;
1422 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1423 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
1424 != symtab_hdr
->sh_size
))
1429 /* Get the value of the symbol referred to by the reloc. */
1430 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1432 Elf_Internal_Sym isym
;
1434 Elf_Internal_Shdr
*hdr
;
1435 const char *sym_name
;
1438 /* A local symbol. */
1439 bfd_elf32_swap_symbol_in (abfd
,
1440 extsyms
+ ELF32_R_SYM (irel
->r_info
),
1443 if (isym
.st_shndx
== SHN_UNDEF
)
1444 sym_sec
= bfd_und_section_ptr
;
1445 else if (isym
.st_shndx
> 0 && isym
.st_shndx
< SHN_LORESERVE
)
1446 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
1447 else if (isym
.st_shndx
== SHN_ABS
)
1448 sym_sec
= bfd_abs_section_ptr
;
1449 else if (isym
.st_shndx
== SHN_COMMON
)
1450 sym_sec
= bfd_com_section_ptr
;
1452 symval
= (isym
.st_value
1453 + sym_sec
->output_section
->vma
1454 + sym_sec
->output_offset
);
1455 sym_name
= bfd_elf_string_from_elf_section (abfd
,
1456 symtab_hdr
->sh_link
,
1459 /* Tack on an ID so we can uniquely identify this
1460 local symbol in the global hash table. */
1461 new_name
= alloca (strlen (sym_name
) + 10);
1462 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1463 sym_name
= new_name
;
1465 h
= (struct elf32_mn10300_link_hash_entry
*)
1466 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
1467 sym_name
, false, false, false);
1473 /* An external symbol. */
1474 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1475 h
= (struct elf32_mn10300_link_hash_entry
*)
1476 (elf_sym_hashes (abfd
)[indx
]);
1477 BFD_ASSERT (h
!= NULL
);
1478 if (h
->root
.root
.type
!= bfd_link_hash_defined
1479 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
1481 /* This appears to be a reference to an undefined
1482 symbol. Just ignore it--it will be caught by the
1483 regular reloc processing. */
1487 symval
= (h
->root
.root
.u
.def
.value
1488 + h
->root
.root
.u
.def
.section
->output_section
->vma
1489 + h
->root
.root
.u
.def
.section
->output_offset
);
1492 /* For simplicity of coding, we are going to modify the section
1493 contents, the section relocs, and the BFD symbol table. We
1494 must tell the rest of the code not to free up this
1495 information. It would be possible to instead create a table
1496 of changes which have to be made, as is done in coff-mips.c;
1497 that would be more work, but would require less memory when
1498 the linker is run. */
1500 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1501 branch/call, also deal with "call" -> "calls" conversions and
1502 insertion of prologue data into "call" instructions. */
1503 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
)
1505 bfd_vma value
= symval
;
1507 /* If we've got a "call" instruction that needs to be turned
1508 into a "calls" instruction, do so now. It saves a byte. */
1509 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1513 /* Get the opcode. */
1514 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1516 /* Make sure we're working with a "call" instruction! */
1519 /* Note that we've changed the relocs, section contents,
1521 elf_section_data (sec
)->relocs
= internal_relocs
;
1524 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1525 free_contents
= NULL
;
1527 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1528 free_extsyms
= NULL
;
1530 /* Fix the opcode. */
1531 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
1532 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1534 /* Fix irel->r_offset and irel->r_addend. */
1535 irel
->r_offset
+= 1;
1536 irel
->r_addend
+= 1;
1538 /* Delete one byte of data. */
1539 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1540 irel
->r_offset
+ 3, 1))
1543 /* That will change things, so, we should relax again.
1544 Note that this is not required, and it may be slow. */
1550 /* We've got a "call" instruction which needs some data
1551 from target function filled in. */
1554 /* Get the opcode. */
1555 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1557 /* Insert data from the target function into the "call"
1558 instruction if needed. */
1561 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
1562 bfd_put_8 (abfd
, h
->stack_size
,
1563 contents
+ irel
->r_offset
+ 5);
1567 /* Deal with pc-relative gunk. */
1568 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1569 value
-= irel
->r_offset
;
1570 value
+= irel
->r_addend
;
1572 /* See if the value will fit in 16 bits, note the high value is
1573 0x7fff + 2 as the target will be two bytes closer if we are
1575 if ((long)value
< 0x8001 && (long)value
> -0x8000)
1579 /* Get the opcode. */
1580 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1582 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
1585 /* Note that we've changed the relocs, section contents, etc. */
1586 elf_section_data (sec
)->relocs
= internal_relocs
;
1589 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1590 free_contents
= NULL
;
1592 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1593 free_extsyms
= NULL
;
1595 /* Fix the opcode. */
1597 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
1598 else if (code
== 0xdd)
1599 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
1600 else if (code
== 0xff)
1601 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
1603 /* Fix the relocation's type. */
1604 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1607 /* Delete two bytes of data. */
1608 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1609 irel
->r_offset
+ 1, 2))
1612 /* That will change things, so, we should relax again.
1613 Note that this is not required, and it may be slow. */
1618 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1620 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
1622 bfd_vma value
= symval
;
1624 /* If we've got a "call" instruction that needs to be turned
1625 into a "calls" instruction, do so now. It saves a byte. */
1626 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
1630 /* Get the opcode. */
1631 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1633 /* Make sure we're working with a "call" instruction! */
1636 /* Note that we've changed the relocs, section contents,
1638 elf_section_data (sec
)->relocs
= internal_relocs
;
1641 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1642 free_contents
= NULL
;
1644 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1645 free_extsyms
= NULL
;
1647 /* Fix the opcode. */
1648 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
1649 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
1651 /* Fix irel->r_offset and irel->r_addend. */
1652 irel
->r_offset
+= 1;
1653 irel
->r_addend
+= 1;
1655 /* Delete one byte of data. */
1656 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1657 irel
->r_offset
+ 1, 1))
1660 /* That will change things, so, we should relax again.
1661 Note that this is not required, and it may be slow. */
1669 /* Get the opcode. */
1670 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1672 /* Insert data from the target function into the "call"
1673 instruction if needed. */
1676 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
1677 bfd_put_8 (abfd
, h
->stack_size
,
1678 contents
+ irel
->r_offset
+ 3);
1682 /* Deal with pc-relative gunk. */
1683 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1684 value
-= irel
->r_offset
;
1685 value
+= irel
->r_addend
;
1687 /* See if the value will fit in 8 bits, note the high value is
1688 0x7f + 1 as the target will be one bytes closer if we are
1690 if ((long)value
< 0x80 && (long)value
> -0x80)
1694 /* Get the opcode. */
1695 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1700 /* Note that we've changed the relocs, section contents, etc. */
1701 elf_section_data (sec
)->relocs
= internal_relocs
;
1704 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1705 free_contents
= NULL
;
1707 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1708 free_extsyms
= NULL
;
1710 /* Fix the opcode. */
1711 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
1713 /* Fix the relocation's type. */
1714 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1717 /* Delete one byte of data. */
1718 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1719 irel
->r_offset
+ 1, 1))
1722 /* That will change things, so, we should relax again.
1723 Note that this is not required, and it may be slow. */
1728 /* Try to eliminate an unconditional 8 bit pc-relative branch
1729 which immediately follows a conditional 8 bit pc-relative
1730 branch around the unconditional branch.
1738 This happens when the bCC can't reach lab2 at assembly time,
1739 but due to other relaxations it can reach at link time. */
1740 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
1742 Elf_Internal_Rela
*nrel
;
1743 bfd_vma value
= symval
;
1746 /* Deal with pc-relative gunk. */
1747 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1748 value
-= irel
->r_offset
;
1749 value
+= irel
->r_addend
;
1751 /* Do nothing if this reloc is the last byte in the section. */
1752 if (irel
->r_offset
== sec
->_cooked_size
)
1755 /* See if the next instruction is an unconditional pc-relative
1756 branch, more often than not this test will fail, so we
1757 test it first to speed things up. */
1758 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1762 /* Also make sure the next relocation applies to the next
1763 instruction and that it's a pc-relative 8 bit branch. */
1766 || irel
->r_offset
+ 2 != nrel
->r_offset
1767 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
1770 /* Make sure our destination immediately follows the
1771 unconditional branch. */
1772 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
1773 + irel
->r_offset
+ 3))
1776 /* Now make sure we are a conditional branch. This may not
1777 be necessary, but why take the chance.
1779 Note these checks assume that R_MN10300_PCREL8 relocs
1780 only occur on bCC and bCCx insns. If they occured
1781 elsewhere, we'd need to know the start of this insn
1782 for this check to be accurate. */
1783 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1784 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
1785 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
1786 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
1787 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
1788 && code
!= 0xea && code
!= 0xeb)
1791 /* We also have to be sure there is no symbol/label
1792 at the unconditional branch. */
1793 if (mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
,
1794 irel
->r_offset
+ 1))
1797 /* Note that we've changed the relocs, section contents, etc. */
1798 elf_section_data (sec
)->relocs
= internal_relocs
;
1801 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1802 free_contents
= NULL
;
1804 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1805 free_extsyms
= NULL
;
1807 /* Reverse the condition of the first branch. */
1853 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1855 /* Set the reloc type and symbol for the first branch
1856 from the second branch. */
1857 irel
->r_info
= nrel
->r_info
;
1859 /* Make the reloc for the second branch a null reloc. */
1860 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
1863 /* Delete two bytes of data. */
1864 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1865 irel
->r_offset
+ 1, 2))
1868 /* That will change things, so, we should relax again.
1869 Note that this is not required, and it may be slow. */
1873 /* Try to turn a 32bit immediate, displacement or absolute address
1874 into a 16bit immediate, displacement or absolute address. */
1875 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
)
1877 bfd_vma value
= symval
;
1878 value
+= irel
->r_addend
;
1880 /* See if the value will fit in 16 bits.
1881 We allow any 16bit match here. We prune those we can't
1883 if ((long)value
< 0x7fff && (long)value
> -0x8000)
1887 /* Most insns which have 32bit operands are 6 bytes long;
1888 exceptions are pcrel insns and bit insns.
1890 We handle pcrel insns above. We don't bother trying
1891 to handle the bit insns here.
1893 The first byte of the remaining insns will be 0xfc. */
1895 /* Get the first opcode. */
1896 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1901 /* Get the second opcode. */
1902 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1904 if ((code
& 0xf0) < 0x80)
1905 switch (code
& 0xf0)
1907 /* mov (d32,am),dn -> mov (d32,am),dn
1908 mov dm,(d32,am) -> mov dn,(d32,am)
1909 mov (d32,am),an -> mov (d32,am),an
1910 mov dm,(d32,am) -> mov dn,(d32,am)
1911 movbu (d32,am),dn -> movbu (d32,am),dn
1912 movbu dm,(d32,am) -> movbu dn,(d32,am)
1913 movhu (d32,am),dn -> movhu (d32,am),dn
1914 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1923 /* Not safe if the high bit is on as relaxing may
1924 move the value out of high mem and thus not fit
1925 in a signed 16bit value. */
1927 && (value
& 0x8000))
1930 /* Note that we've changed the relocation contents, etc. */
1931 elf_section_data (sec
)->relocs
= internal_relocs
;
1934 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1935 free_contents
= NULL
;
1937 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1938 free_extsyms
= NULL
;
1940 /* Fix the opcode. */
1941 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
1942 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1944 /* Fix the relocation's type. */
1945 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1948 /* Delete two bytes of data. */
1949 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
1950 irel
->r_offset
+ 2, 2))
1953 /* That will change things, so, we should relax again.
1954 Note that this is not required, and it may be slow. */
1958 else if ((code
& 0xf0) == 0x80
1959 || (code
& 0xf0) == 0x90)
1960 switch (code
& 0xf3)
1962 /* mov dn,(abs32) -> mov dn,(abs16)
1963 movbu dn,(abs32) -> movbu dn,(abs16)
1964 movhu dn,(abs32) -> movhu dn,(abs16) */
1968 /* Note that we've changed the relocation contents, etc. */
1969 elf_section_data (sec
)->relocs
= internal_relocs
;
1972 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1973 free_contents
= NULL
;
1975 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1976 free_extsyms
= NULL
;
1978 if ((code
& 0xf3) == 0x81)
1979 code
= 0x01 + (code
& 0x0c);
1980 else if ((code
& 0xf3) == 0x82)
1981 code
= 0x02 + (code
& 0x0c);
1982 else if ((code
& 0xf3) == 0x83)
1983 code
= 0x03 + (code
& 0x0c);
1987 /* Fix the opcode. */
1988 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1990 /* Fix the relocation's type. */
1991 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1994 /* The opcode got shorter too, so we have to fix the
1995 addend and offset too! */
1996 irel
->r_offset
-= 1;
1998 /* Delete three bytes of data. */
1999 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2000 irel
->r_offset
+ 1, 3))
2003 /* That will change things, so, we should relax again.
2004 Note that this is not required, and it may be slow. */
2008 /* mov am,(abs32) -> mov am,(abs16)
2009 mov am,(d32,sp) -> mov am,(d16,sp)
2010 mov dm,(d32,sp) -> mov dm,(d32,sp)
2011 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2012 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2018 /* Note that we've changed the relocation contents, etc. */
2019 elf_section_data (sec
)->relocs
= internal_relocs
;
2022 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2023 free_contents
= NULL
;
2025 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2026 free_extsyms
= NULL
;
2028 /* Fix the opcode. */
2029 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2030 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2032 /* Fix the relocation's type. */
2033 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2036 /* Delete two bytes of data. */
2037 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2038 irel
->r_offset
+ 2, 2))
2041 /* That will change things, so, we should relax again.
2042 Note that this is not required, and it may be slow. */
2046 else if ((code
& 0xf0) < 0xf0)
2047 switch (code
& 0xfc)
2049 /* mov imm32,dn -> mov imm16,dn
2050 mov imm32,an -> mov imm16,an
2051 mov (abs32),dn -> mov (abs16),dn
2052 movbu (abs32),dn -> movbu (abs16),dn
2053 movhu (abs32),dn -> movhu (abs16),dn */
2059 /* Not safe if the high bit is on as relaxing may
2060 move the value out of high mem and thus not fit
2061 in a signed 16bit value. */
2063 && (value
& 0x8000))
2066 /* Note that we've changed the relocation contents, etc. */
2067 elf_section_data (sec
)->relocs
= internal_relocs
;
2070 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2071 free_contents
= NULL
;
2073 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2074 free_extsyms
= NULL
;
2076 if ((code
& 0xfc) == 0xcc)
2077 code
= 0x2c + (code
& 0x03);
2078 else if ((code
& 0xfc) == 0xdc)
2079 code
= 0x24 + (code
& 0x03);
2080 else if ((code
& 0xfc) == 0xa4)
2081 code
= 0x30 + (code
& 0x03);
2082 else if ((code
& 0xfc) == 0xa8)
2083 code
= 0x34 + (code
& 0x03);
2084 else if ((code
& 0xfc) == 0xac)
2085 code
= 0x38 + (code
& 0x03);
2089 /* Fix the opcode. */
2090 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2092 /* Fix the relocation's type. */
2093 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2096 /* The opcode got shorter too, so we have to fix the
2097 addend and offset too! */
2098 irel
->r_offset
-= 1;
2100 /* Delete three bytes of data. */
2101 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2102 irel
->r_offset
+ 1, 3))
2105 /* That will change things, so, we should relax again.
2106 Note that this is not required, and it may be slow. */
2110 /* mov (abs32),an -> mov (abs16),an
2111 mov (d32,sp),an -> mov (d32,sp),an
2112 mov (d32,sp),dn -> mov (d32,sp),dn
2113 movbu (d32,sp),dn -> movbu (d32,sp),dn
2114 movhu (d32,sp),dn -> movhu (d32,sp),dn
2115 add imm32,dn -> add imm16,dn
2116 cmp imm32,dn -> cmp imm16,dn
2117 add imm32,an -> add imm16,an
2118 cmp imm32,an -> cmp imm16,an
2119 and imm32,dn -> and imm32,dn
2120 or imm32,dn -> or imm32,dn
2121 xor imm32,dn -> xor imm32,dn
2122 btst imm32,dn -> btst imm32,dn */
2138 /* Note that we've changed the relocation contents, etc. */
2139 elf_section_data (sec
)->relocs
= internal_relocs
;
2142 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2143 free_contents
= NULL
;
2145 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2146 free_extsyms
= NULL
;
2148 /* Fix the opcode. */
2149 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2150 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2152 /* Fix the relocation's type. */
2153 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2156 /* Delete two bytes of data. */
2157 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2158 irel
->r_offset
+ 2, 2))
2161 /* That will change things, so, we should relax again.
2162 Note that this is not required, and it may be slow. */
2166 else if (code
== 0xfe)
2168 /* add imm32,sp -> add imm16,sp */
2170 /* Note that we've changed the relocation contents, etc. */
2171 elf_section_data (sec
)->relocs
= internal_relocs
;
2174 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2175 free_contents
= NULL
;
2177 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
2178 free_extsyms
= NULL
;
2180 /* Fix the opcode. */
2181 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2182 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
2184 /* Fix the relocation's type. */
2185 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2188 /* Delete two bytes of data. */
2189 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2190 irel
->r_offset
+ 2, 2))
2193 /* That will change things, so, we should relax again.
2194 Note that this is not required, and it may be slow. */
2202 if (free_relocs
!= NULL
)
2208 if (free_contents
!= NULL
)
2210 if (! link_info
->keep_memory
)
2211 free (free_contents
);
2214 /* Cache the section contents for elf_link_input_bfd. */
2215 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2217 free_contents
= NULL
;
2220 if (free_extsyms
!= NULL
)
2222 if (! link_info
->keep_memory
)
2223 free (free_extsyms
);
2226 /* Cache the symbols for elf_link_input_bfd. */
2227 symtab_hdr
->contents
= extsyms
;
2229 free_extsyms
= NULL
;
2235 if (free_relocs
!= NULL
)
2237 if (free_contents
!= NULL
)
2238 free (free_contents
);
2239 if (free_extsyms
!= NULL
)
2240 free (free_extsyms
);
2244 /* Compute the stack size and movm arguments for the function
2245 referred to by HASH at address ADDR in section with
2246 contents CONTENTS, store the information in the hash table. */
2248 compute_function_info (abfd
, hash
, addr
, contents
)
2250 struct elf32_mn10300_link_hash_entry
*hash
;
2252 unsigned char *contents
;
2254 unsigned char byte1
, byte2
;
2255 /* We only care about a very small subset of the possible prologue
2256 sequences here. Basically we look for:
2258 movm [d2,d3,a2,a3],sp (optional)
2259 add <size>,sp (optional, and only for sizes which fit in an unsigned
2262 If we find anything else, we quit. */
2264 /* Look for movm [regs],sp */
2265 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2266 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2270 hash
->movm_args
= byte2
;
2272 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
2273 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
2276 /* Now look for the two stack adjustment variants. */
2277 if (byte1
== 0xf8 && byte2
== 0xfe)
2279 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
2280 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
2282 hash
->stack_size
= -temp
;
2284 else if (byte1
== 0xfa && byte2
== 0xfe)
2286 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
2287 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
2291 hash
->stack_size
= temp
;
2296 /* Delete some bytes from a section while relaxing. */
2299 mn10300_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
2305 Elf_Internal_Shdr
*symtab_hdr
;
2306 Elf32_External_Sym
*extsyms
;
2309 Elf_Internal_Rela
*irel
, *irelend
;
2310 Elf_Internal_Rela
*irelalign
;
2312 Elf32_External_Sym
*esym
, *esymend
;
2313 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2315 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2316 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2318 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2320 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2322 /* The deletion must stop at the next ALIGN reloc for an aligment
2323 power larger than the number of bytes we are deleting. */
2326 toaddr
= sec
->_cooked_size
;
2328 irel
= elf_section_data (sec
)->relocs
;
2329 irelend
= irel
+ sec
->reloc_count
;
2331 /* Actually delete the bytes. */
2332 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
2333 sec
->_cooked_size
-= count
;
2335 /* Adjust all the relocs. */
2336 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
2338 /* Get the new reloc address. */
2339 if ((irel
->r_offset
> addr
2340 && irel
->r_offset
< toaddr
))
2341 irel
->r_offset
-= count
;
2344 /* Adjust the local symbols defined in this section. */
2346 esymend
= esym
+ symtab_hdr
->sh_info
;
2347 for (; esym
< esymend
; esym
++)
2349 Elf_Internal_Sym isym
;
2351 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2353 if (isym
.st_shndx
== shndx
2354 && isym
.st_value
> addr
2355 && isym
.st_value
< toaddr
)
2357 isym
.st_value
-= count
;
2358 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
2362 /* Now adjust the global symbols defined in this section. */
2363 esym
= extsyms
+ symtab_hdr
->sh_info
;
2364 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
2365 for (index
= 0; esym
< esymend
; esym
++, index
++)
2367 Elf_Internal_Sym isym
;
2369 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2370 sym_hash
= (struct elf32_mn10300_link_hash_entry
*)
2371 (elf_sym_hashes (abfd
)[index
]);
2372 if (isym
.st_shndx
== shndx
2373 && ((sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2374 || (sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2375 && (sym_hash
)->root
.root
.u
.def
.section
== sec
2376 && (sym_hash
)->root
.root
.u
.def
.value
> addr
2377 && (sym_hash
)->root
.root
.u
.def
.value
< toaddr
)
2379 (sym_hash
)->root
.root
.u
.def
.value
-= count
;
2386 /* Return true if a symbol exists at the given address, else return
2389 mn10300_elf_symbol_address_p (abfd
, sec
, extsyms
, addr
)
2392 Elf32_External_Sym
*extsyms
;
2395 Elf_Internal_Shdr
*symtab_hdr
;
2397 Elf32_External_Sym
*esym
, *esymend
;
2398 struct elf32_mn10300_link_hash_entry
**sym_hash
, **sym_hash_end
;
2400 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2401 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
2403 /* Examine all the symbols. */
2405 esymend
= esym
+ symtab_hdr
->sh_info
;
2406 for (; esym
< esymend
; esym
++)
2408 Elf_Internal_Sym isym
;
2410 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
2412 if (isym
.st_shndx
== shndx
2413 && isym
.st_value
== addr
)
2417 sym_hash
= (struct elf32_mn10300_link_hash_entry
**)(elf_sym_hashes (abfd
));
2418 sym_hash_end
= (sym_hash
2419 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2420 - symtab_hdr
->sh_info
));
2421 for (; sym_hash
< sym_hash_end
; sym_hash
++)
2423 if (((*sym_hash
)->root
.root
.type
== bfd_link_hash_defined
2424 || (*sym_hash
)->root
.root
.type
== bfd_link_hash_defweak
)
2425 && (*sym_hash
)->root
.root
.u
.def
.section
== sec
2426 && (*sym_hash
)->root
.root
.u
.def
.value
== addr
)
2432 /* This is a version of bfd_generic_get_relocated_section_contents
2433 which uses mn10300_elf_relocate_section. */
2436 mn10300_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
2437 data
, relocateable
, symbols
)
2439 struct bfd_link_info
*link_info
;
2440 struct bfd_link_order
*link_order
;
2442 boolean relocateable
;
2445 Elf_Internal_Shdr
*symtab_hdr
;
2446 asection
*input_section
= link_order
->u
.indirect
.section
;
2447 bfd
*input_bfd
= input_section
->owner
;
2448 asection
**sections
= NULL
;
2449 Elf_Internal_Rela
*internal_relocs
= NULL
;
2450 Elf32_External_Sym
*external_syms
= NULL
;
2451 Elf_Internal_Sym
*internal_syms
= NULL
;
2453 /* We only need to handle the case of relaxing, or of having a
2454 particular set of section contents, specially. */
2456 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
2457 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
2462 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2464 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
2465 input_section
->_raw_size
);
2467 if ((input_section
->flags
& SEC_RELOC
) != 0
2468 && input_section
->reloc_count
> 0)
2470 Elf_Internal_Sym
*isymp
;
2472 Elf32_External_Sym
*esym
, *esymend
;
2474 if (symtab_hdr
->contents
!= NULL
)
2475 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
2478 external_syms
= ((Elf32_External_Sym
*)
2479 bfd_malloc (symtab_hdr
->sh_info
2480 * sizeof (Elf32_External_Sym
)));
2481 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2483 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
2484 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
2485 symtab_hdr
->sh_info
, input_bfd
)
2486 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
2490 internal_relocs
= (_bfd_elf32_link_read_relocs
2491 (input_bfd
, input_section
, (PTR
) NULL
,
2492 (Elf_Internal_Rela
*) NULL
, false));
2493 if (internal_relocs
== NULL
)
2496 internal_syms
= ((Elf_Internal_Sym
*)
2497 bfd_malloc (symtab_hdr
->sh_info
2498 * sizeof (Elf_Internal_Sym
)));
2499 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
2502 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
2503 * sizeof (asection
*));
2504 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
2507 isymp
= internal_syms
;
2509 esym
= external_syms
;
2510 esymend
= esym
+ symtab_hdr
->sh_info
;
2511 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
2515 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
2517 if (isymp
->st_shndx
== SHN_UNDEF
)
2518 isec
= bfd_und_section_ptr
;
2519 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
2520 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
2521 else if (isymp
->st_shndx
== SHN_ABS
)
2522 isec
= bfd_abs_section_ptr
;
2523 else if (isymp
->st_shndx
== SHN_COMMON
)
2524 isec
= bfd_com_section_ptr
;
2534 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
2535 input_section
, data
, internal_relocs
,
2536 internal_syms
, sections
))
2539 if (sections
!= NULL
)
2542 if (internal_syms
!= NULL
)
2543 free (internal_syms
);
2544 internal_syms
= NULL
;
2545 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2546 free (external_syms
);
2547 external_syms
= NULL
;
2548 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
2549 free (internal_relocs
);
2550 internal_relocs
= NULL
;
2556 if (internal_relocs
!= NULL
2557 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
2558 free (internal_relocs
);
2559 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
2560 free (external_syms
);
2561 if (internal_syms
!= NULL
)
2562 free (internal_syms
);
2563 if (sections
!= NULL
)
2568 /* Assorted hash table functions. */
2570 /* Initialize an entry in the link hash table. */
2572 /* Create an entry in an MN10300 ELF linker hash table. */
2574 static struct bfd_hash_entry
*
2575 elf32_mn10300_link_hash_newfunc (entry
, table
, string
)
2576 struct bfd_hash_entry
*entry
;
2577 struct bfd_hash_table
*table
;
2580 struct elf32_mn10300_link_hash_entry
*ret
=
2581 (struct elf32_mn10300_link_hash_entry
*) entry
;
2583 /* Allocate the structure if it has not already been allocated by a
2585 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2586 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2587 bfd_hash_allocate (table
,
2588 sizeof (struct elf32_mn10300_link_hash_entry
)));
2589 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
2590 return (struct bfd_hash_entry
*) ret
;
2592 /* Call the allocation method of the superclass. */
2593 ret
= ((struct elf32_mn10300_link_hash_entry
*)
2594 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
2596 if (ret
!= (struct elf32_mn10300_link_hash_entry
*) NULL
)
2598 ret
->direct_calls
= 0;
2599 ret
->stack_size
= 0;
2604 return (struct bfd_hash_entry
*) ret
;
2607 /* Create an mn10300 ELF linker hash table. */
2609 static struct bfd_link_hash_table
*
2610 elf32_mn10300_link_hash_table_create (abfd
)
2613 struct elf32_mn10300_link_hash_table
*ret
;
2615 ret
= ((struct elf32_mn10300_link_hash_table
*)
2616 bfd_alloc (abfd
, sizeof (struct elf32_mn10300_link_hash_table
)));
2617 if (ret
== (struct elf32_mn10300_link_hash_table
*) NULL
)
2620 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
2621 elf32_mn10300_link_hash_newfunc
))
2623 bfd_release (abfd
, ret
);
2628 ret
->static_hash_table
2629 = ((struct elf32_mn10300_link_hash_table
*)
2630 bfd_alloc (abfd
, sizeof (struct elf_link_hash_table
)));
2631 if (ret
->static_hash_table
== NULL
)
2633 bfd_release (abfd
, ret
);
2637 if (! _bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
2638 elf32_mn10300_link_hash_newfunc
))
2640 bfd_release (abfd
, ret
->static_hash_table
);
2641 bfd_release (abfd
, ret
);
2644 return &ret
->root
.root
;
2648 elf_mn10300_mach (flags
)
2651 switch (flags
& EF_MN10300_MACH
)
2653 case E_MN10300_MACH_MN10300
:
2655 return bfd_mach_mn10300
;
2657 /* start-sanitize-am33 */
2658 case E_MN10300_MACH_AM33
:
2659 return bfd_mach_am33
;
2660 /* end-sanitize-am33 */
2664 /* The final processing done just before writing out a MN10300 ELF object
2665 file. This gets the MN10300 architecture right based on the machine
2670 _bfd_mn10300_elf_final_write_processing (abfd
, linker
)
2676 Elf_Internal_Shdr
**hdrpp
;
2680 switch (bfd_get_mach (abfd
))
2683 case bfd_mach_mn10300
:
2684 val
= E_MN10300_MACH_MN10300
;
2687 /* start-sanitize-am33 */
2689 val
= E_MN10300_MACH_AM33
;
2691 /* end-sanitize-am33 */
2694 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
2695 elf_elfheader (abfd
)->e_flags
|= val
;
2699 _bfd_mn10300_elf_object_p (abfd
)
2702 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
2703 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
2707 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2708 #define TARGET_LITTLE_NAME "elf32-mn10300"
2709 #define ELF_ARCH bfd_arch_mn10300
2710 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2711 #define ELF_MAXPAGESIZE 0x1000
2713 #define elf_info_to_howto mn10300_info_to_howto
2714 #define elf_info_to_howto_rel 0
2715 #define elf_backend_can_gc_sections 1
2716 #define elf_backend_check_relocs mn10300_elf_check_relocs
2717 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2718 #define elf_backend_relocate_section mn10300_elf_relocate_section
2719 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2720 #define bfd_elf32_bfd_get_relocated_section_contents \
2721 mn10300_elf_get_relocated_section_contents
2722 #define bfd_elf32_bfd_link_hash_table_create \
2723 elf32_mn10300_link_hash_table_create
2725 #define elf_symbol_leading_char '_'
2727 /* So we can set bits in e_flags. */
2728 #define elf_backend_final_write_processing \
2729 _bfd_mn10300_elf_final_write_processing
2730 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2733 #include "elf32-target.h"