1 /* Generic support for 32-bit ELF
2 Copyright 1993, 1995, 1998, 1999 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. */
26 static reloc_howto_type
*elf32_h8_reloc_type_lookup
27 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
28 static void elf32_h8_info_to_howto
29 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
30 static void elf32_h8_info_to_howto_rel
31 PARAMS ((bfd
*, arelent
*, Elf32_Internal_Rel
*));
32 static int elf32_h8_mach
34 static bfd_reloc_status_type elf32_h8_final_link_relocate
35 PARAMS ((unsigned long, bfd
*, bfd
*, asection
*,
36 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
37 struct bfd_link_info
*, asection
*, int));
38 static boolean elf32_h8_relocate_section
39 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
40 bfd_byte
*, Elf_Internal_Rela
*,
41 Elf_Internal_Sym
*, asection
**));
42 static bfd_reloc_status_type special
43 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
45 /* This does not include any relocation information, but should be
46 good enough for GDB or objdump to read the file. */
48 static reloc_howto_type h8_elf_howto_table
[] =
51 HOWTO (R_H8_NONE
, /* type */
53 0, /* size (0 = byte, 1 = short, 2 = long) */
55 false, /* pc_relative */
57 complain_overflow_dont
, /* complain_on_overflow */
58 special
, /* special_function */
59 "R_H8_NONE", /* name */
60 false, /* partial_inplace */
63 false), /* pcrel_offset */
64 #define R_H8_DIR32_X (R_H8_NONE_X + 1)
65 HOWTO (R_H8_DIR32
, /* type */
67 2, /* size (0 = byte, 1 = short, 2 = long) */
69 false, /* pc_relative */
71 complain_overflow_dont
, /* complain_on_overflow */
72 special
, /* special_function */
73 "R_H8_DIR32", /* name */
74 false, /* partial_inplace */
76 0xffffffff, /* dst_mask */
77 false), /* pcrel_offset */
78 #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
79 HOWTO (R_H8_DIR16
, /* type */
81 1, /* size (0 = byte, 1 = short, 2 = long) */
83 false, /* pc_relative */
85 complain_overflow_dont
, /* complain_on_overflow */
86 special
, /* special_function */
87 "R_H8_DIR16", /* name */
88 false, /* partial_inplace */
90 0x0000ffff, /* dst_mask */
91 false), /* pcrel_offset */
92 #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
93 HOWTO (R_H8_DIR8
, /* type */
95 0, /* size (0 = byte, 1 = short, 2 = long) */
97 false, /* pc_relative */
99 complain_overflow_dont
, /* complain_on_overflow */
100 special
, /* special_function */
101 "R_H8_DIR16", /* name */
102 false, /* partial_inplace */
104 0x000000ff, /* dst_mask */
105 false), /* pcrel_offset */
106 #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
107 HOWTO (R_H8_DIR16A8
, /* type */
109 1, /* size (0 = byte, 1 = short, 2 = long) */
111 false, /* pc_relative */
113 complain_overflow_bitfield
, /* complain_on_overflow */
114 special
, /* special_function */
115 "R_H8_DIR16A8", /* name */
116 false, /* partial_inplace */
118 0x0000ffff, /* dst_mask */
119 false), /* pcrel_offset */
120 #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
121 HOWTO (R_H8_DIR16R8
, /* type */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
125 false, /* pc_relative */
127 complain_overflow_bitfield
, /* complain_on_overflow */
128 special
, /* special_function */
129 "R_H8_DIR16R8", /* name */
130 false, /* partial_inplace */
132 0x0000ffff, /* dst_mask */
133 false), /* pcrel_offset */
134 #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
135 HOWTO (R_H8_DIR24A8
, /* type */
137 2, /* size (0 = byte, 1 = short, 2 = long) */
139 false, /* pc_relative */
141 complain_overflow_bitfield
, /* complain_on_overflow */
142 special
, /* special_function */
143 "R_H8_DIR24A8", /* name */
144 true, /* partial_inplace */
145 0xff000000, /* src_mask */
146 0x00ffffff, /* dst_mask */
147 false), /* pcrel_offset */
148 #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
149 HOWTO (R_H8_DIR24R8
, /* type */
151 2, /* size (0 = byte, 1 = short, 2 = long) */
153 false, /* pc_relative */
155 complain_overflow_bitfield
, /* complain_on_overflow */
156 special
, /* special_function */
157 "R_H8_DIR24R8", /* name */
158 true, /* partial_inplace */
159 0xff000000, /* src_mask */
160 0x00ffffff, /* dst_mask */
161 false), /* pcrel_offset */
162 #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
163 HOWTO (R_H8_DIR32A16
, /* type */
165 2, /* size (0 = byte, 1 = short, 2 = long) */
167 false, /* pc_relative */
169 complain_overflow_dont
, /* complain_on_overflow */
170 special
, /* special_function */
171 "R_H8_DIR32", /* name */
172 false, /* partial_inplace */
174 0xffffffff, /* dst_mask */
175 false), /* pcrel_offset */
176 #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1)
177 HOWTO (R_H8_PCREL16
, /* type */
179 1, /* size (0 = byte, 1 = short, 2 = long) */
181 true, /* pc_relative */
183 complain_overflow_signed
, /* complain_on_overflow */
184 special
, /* special_function */
185 "R_H8_PCREL16", /* name */
186 false, /* partial_inplace */
187 0xffff, /* src_mask */
188 0xffff, /* dst_mask */
189 true), /* pcrel_offset */
190 #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
191 HOWTO (R_H8_PCREL8
, /* type */
193 0, /* size (0 = byte, 1 = short, 2 = long) */
195 true, /* pc_relative */
197 complain_overflow_signed
, /* complain_on_overflow */
198 special
, /* special_function */
199 "R_H8_PCREL8", /* name */
200 false, /* partial_inplace */
203 true), /* pcrel_offset */
206 /* This structure is used to map BFD reloc codes to H8 ELF relocs. */
210 bfd_reloc_code_real_type bfd_reloc_val
;
211 unsigned char howto_index
;
214 /* An array mapping BFD reloc codes to SH ELF relocs. */
216 static const struct elf_reloc_map h8_reloc_map
[] =
218 { BFD_RELOC_NONE
, R_H8_NONE_X
},
219 { BFD_RELOC_32
, R_H8_DIR32_X
},
220 { BFD_RELOC_16
, R_H8_DIR16_X
},
221 { BFD_RELOC_8
, R_H8_DIR8_X
},
222 { BFD_RELOC_H8_DIR16A8
, R_H8_DIR16A8_X
},
223 { BFD_RELOC_H8_DIR16R8
, R_H8_DIR16R8_X
},
224 { BFD_RELOC_H8_DIR24A8
, R_H8_DIR24A8_X
},
225 { BFD_RELOC_H8_DIR24R8
, R_H8_DIR24R8_X
},
226 { BFD_RELOC_H8_DIR32A16
, R_H8_DIR32A16_X
},
227 { BFD_RELOC_16_PCREL
, R_H8_PCREL16_X
},
228 { BFD_RELOC_8_PCREL
, R_H8_PCREL8_X
},
232 static reloc_howto_type
*
233 elf32_h8_reloc_type_lookup (abfd
, code
)
234 bfd
*abfd ATTRIBUTE_UNUSED
;
235 bfd_reloc_code_real_type code
;
239 for (i
= 0; i
< sizeof (h8_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
241 if (h8_reloc_map
[i
].bfd_reloc_val
== code
)
242 return &h8_elf_howto_table
[(int) h8_reloc_map
[i
].howto_index
];
248 elf32_h8_info_to_howto (abfd
, bfd_reloc
, elf_reloc
)
249 bfd
*abfd ATTRIBUTE_UNUSED
;
251 Elf32_Internal_Rela
*elf_reloc
;
256 r
= ELF32_R_TYPE (elf_reloc
->r_info
);
257 for (i
= 0; i
< sizeof (h8_elf_howto_table
) / sizeof (reloc_howto_type
); i
++)
258 if (h8_elf_howto_table
[i
].type
== r
)
260 bfd_reloc
->howto
= &h8_elf_howto_table
[i
];
267 elf32_h8_info_to_howto_rel (abfd
, bfd_reloc
, elf_reloc
)
268 bfd
*abfd ATTRIBUTE_UNUSED
;
270 Elf32_Internal_Rel
*elf_reloc ATTRIBUTE_UNUSED
;
275 r
= ELF32_R_TYPE (elf_reloc
->r_info
);
276 bfd_reloc
->howto
= &h8_elf_howto_table
[r
];
279 /* Special handling for H8/300 relocs.
280 We only come here for pcrel stuff and return normally if not an -r link.
281 When doing -r, we can't do any arithmetic for the pcrel stuff, because
282 we support relaxing on the H8/300 series chips. */
283 static bfd_reloc_status_type
284 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
286 bfd
*abfd ATTRIBUTE_UNUSED
;
287 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
288 asymbol
*symbol ATTRIBUTE_UNUSED
;
289 PTR data ATTRIBUTE_UNUSED
;
290 asection
*input_section ATTRIBUTE_UNUSED
;
292 char **error_message ATTRIBUTE_UNUSED
;
294 if (output_bfd
== (bfd
*) NULL
)
295 return bfd_reloc_continue
;
297 /* Adjust the reloc address to that in the output section. */
298 reloc_entry
->address
+= input_section
->output_offset
;
302 /* Perform a relocation as part of a final link. */
303 static bfd_reloc_status_type
304 elf32_h8_final_link_relocate (r_type
, input_bfd
, output_bfd
,
305 input_section
, contents
, offset
, value
,
306 addend
, info
, sym_sec
, is_local
)
307 unsigned long r_type
;
309 bfd
*output_bfd ATTRIBUTE_UNUSED
;
310 asection
*input_section ATTRIBUTE_UNUSED
;
315 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
316 asection
*sym_sec ATTRIBUTE_UNUSED
;
317 int is_local ATTRIBUTE_UNUSED
;
319 bfd_byte
*hit_data
= contents
+ offset
;
331 bfd_put_32 (input_bfd
, value
, hit_data
);
338 bfd_put_16 (input_bfd
, value
, hit_data
);
345 bfd_put_8 (input_bfd
, value
, hit_data
);
351 /* HIT_DATA is the address for the first byte for the relocated
352 value. Subtract 1 so that we can manipulate the data in 32bit
356 /* Clear out the top byte in value. */
359 /* Retrieve the type byte for value from the section contents. */
360 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
362 /* Now scribble it out in one 32bit hunk. */
363 bfd_put_32 (input_bfd
, value
, hit_data
);
367 value
-= (input_section
->output_section
->vma
368 + input_section
->output_offset
);
372 /* The value is relative to the start of the instruction,
373 not the relocation offset. Subtract 2 to account for
377 bfd_put_16 (input_bfd
, value
, hit_data
);
381 value
-= (input_section
->output_section
->vma
382 + input_section
->output_offset
);
386 /* The value is relative to the start of the instruction,
387 not the relocation offset. Subtract 1 to account for
391 bfd_put_8 (input_bfd
, value
, hit_data
);
395 return bfd_reloc_notsupported
;
399 /* Relocate an H8 ELF section. */
401 elf32_h8_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
402 contents
, relocs
, local_syms
, local_sections
)
404 struct bfd_link_info
*info
;
406 asection
*input_section
;
408 Elf_Internal_Rela
*relocs
;
409 Elf_Internal_Sym
*local_syms
;
410 asection
**local_sections
;
412 Elf_Internal_Shdr
*symtab_hdr
;
413 struct elf_link_hash_entry
**sym_hashes
;
414 Elf_Internal_Rela
*rel
, *relend
;
416 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
417 sym_hashes
= elf_sym_hashes (input_bfd
);
420 relend
= relocs
+ input_section
->reloc_count
;
421 for (; rel
< relend
; rel
++)
424 unsigned long r_symndx
;
425 Elf_Internal_Sym
*sym
;
427 struct elf_link_hash_entry
*h
;
429 bfd_reloc_status_type r
;
431 r_symndx
= ELF32_R_SYM (rel
->r_info
);
432 r_type
= ELF32_R_TYPE (rel
->r_info
);
434 if (info
->relocateable
)
436 /* This is a relocateable link. We don't have to change
437 anything, unless the reloc is against a section symbol,
438 in which case we have to adjust according to where the
439 section symbol winds up in the output section. */
440 if (r_symndx
< symtab_hdr
->sh_info
)
442 sym
= local_syms
+ r_symndx
;
443 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
445 sec
= local_sections
[r_symndx
];
446 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
453 /* This is a final link. */
457 if (r_symndx
< symtab_hdr
->sh_info
)
459 sym
= local_syms
+ r_symndx
;
460 sec
= local_sections
[r_symndx
];
461 relocation
= (sec
->output_section
->vma
467 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
468 while (h
->root
.type
== bfd_link_hash_indirect
469 || h
->root
.type
== bfd_link_hash_warning
)
470 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
471 if (h
->root
.type
== bfd_link_hash_defined
472 || h
->root
.type
== bfd_link_hash_defweak
)
474 sec
= h
->root
.u
.def
.section
;
475 relocation
= (h
->root
.u
.def
.value
476 + sec
->output_section
->vma
477 + sec
->output_offset
);
479 else if (h
->root
.type
== bfd_link_hash_undefweak
)
483 if (! ((*info
->callbacks
->undefined_symbol
)
484 (info
, h
->root
.root
.string
, input_bfd
,
485 input_section
, rel
->r_offset
, true)))
491 r
= elf32_h8_final_link_relocate (r_type
, input_bfd
, output_bfd
,
493 contents
, rel
->r_offset
,
494 relocation
, rel
->r_addend
,
495 info
, sec
, h
== NULL
);
497 if (r
!= bfd_reloc_ok
)
500 const char *msg
= (const char *) 0;
502 reloc_howto_type
*howto
;
504 elf32_h8_info_to_howto (input_bfd
, &bfd_reloc
, rel
);
505 howto
= bfd_reloc
.howto
;
508 name
= h
->root
.root
.string
;
511 name
= (bfd_elf_string_from_elf_section
512 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
513 if (name
== NULL
|| *name
== '\0')
514 name
= bfd_section_name (input_bfd
, sec
);
519 case bfd_reloc_overflow
:
520 if (! ((*info
->callbacks
->reloc_overflow
)
521 (info
, name
, howto
->name
, (bfd_vma
) 0,
522 input_bfd
, input_section
, rel
->r_offset
)))
526 case bfd_reloc_undefined
:
527 if (! ((*info
->callbacks
->undefined_symbol
)
528 (info
, name
, input_bfd
, input_section
,
529 rel
->r_offset
, true)))
533 case bfd_reloc_outofrange
:
534 msg
= _("internal error: out of range error");
537 case bfd_reloc_notsupported
:
538 msg
= _("internal error: unsupported relocation error");
541 case bfd_reloc_dangerous
:
542 msg
= _("internal error: dangerous error");
546 msg
= _("internal error: unknown error");
550 if (!((*info
->callbacks
->warning
)
551 (info
, msg
, name
, input_bfd
, input_section
,
562 /* Object files encode the specific H8 model they were compiled
563 for in the ELF flags field.
565 Examine that field and return the proper BFD machine type for
568 elf32_h8_mach (flags
)
571 switch (flags
& EF_H8_MACH
)
573 case E_H8_MACH_H8300
:
575 return bfd_mach_h8300
;
577 case E_H8_MACH_H8300H
:
578 return bfd_mach_h8300h
;
580 case E_H8_MACH_H8300S
:
581 return bfd_mach_h8300s
;
585 /* The final processing done just before writing out a H8 ELF object
586 file. We use this opportunity to encode the BFD machine type
587 into the flags field in the object file. */
590 elf32_h8_final_write_processing (abfd
, linker
)
592 boolean linker ATTRIBUTE_UNUSED
;
596 switch (bfd_get_mach (abfd
))
600 val
= E_H8_MACH_H8300
;
603 case bfd_mach_h8300h
:
604 val
= E_H8_MACH_H8300H
;
607 case bfd_mach_h8300s
:
608 val
= E_H8_MACH_H8300S
;
612 elf_elfheader (abfd
)->e_flags
&= ~ (EF_H8_MACH
);
613 elf_elfheader (abfd
)->e_flags
|= val
;
616 /* Return nonzero if ABFD represents a valid H8 ELF object file; also
617 record the encoded machine type found in the ELF flags. */
620 elf32_h8_object_p (abfd
)
623 bfd_default_set_arch_mach (abfd
, bfd_arch_h8300
,
624 elf32_h8_mach (elf_elfheader (abfd
)->e_flags
));
628 /* Merge backend specific data from an object file to the output
629 object file when linking. The only data we need to copy at this
630 time is the architecture/machine information. */
633 elf32_h8_merge_private_bfd_data (ibfd
, obfd
)
637 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
638 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
641 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
642 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
644 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
645 bfd_get_mach (ibfd
)))
652 /* This function handles relaxing for the H8..
654 There's a few relaxing opportunites available on the H8:
656 jmp/jsr:24 -> bra/bsr:8 2 bytes
657 The jmp may be completely eliminated if the previous insn is a
658 conditional branch to the insn after the jump. In that case
659 we invert the branch and delete the jump and save 4 bytes.
661 bCC:16 -> bCC:8 2 bytes
662 bsr:16 -> bsr:8 2 bytes
664 mov.b:16 -> mov.b:8 2 bytes
665 mov.b:24/32 -> mov.b:8 4 bytes
667 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes
673 elf32_h8_relax_section (abfd
, sec
, link_info
, again
)
676 struct bfd_link_info
*link_info
;
679 Elf_Internal_Shdr
*symtab_hdr
;
680 Elf_Internal_Rela
*internal_relocs
;
681 Elf_Internal_Rela
*free_relocs
= NULL
;
682 Elf_Internal_Rela
*irel
, *irelend
;
683 bfd_byte
*contents
= NULL
;
684 bfd_byte
*free_contents
= NULL
;
685 Elf32_External_Sym
*extsyms
= NULL
;
686 Elf32_External_Sym
*free_extsyms
= NULL
;
687 static asection
*last_input_section
= NULL
;
688 static Elf_Internal_Rela
*last_reloc
= NULL
;
690 /* Assume nothing changes. */
693 /* We don't have to do anything for a relocateable link, if
694 this section does not have relocs, or if this is not a
696 if (link_info
->relocateable
697 || (sec
->flags
& SEC_RELOC
) == 0
698 || sec
->reloc_count
== 0
699 || (sec
->flags
& SEC_CODE
) == 0)
702 /* If this is the first time we have been called for this section,
703 initialize the cooked size. */
704 if (sec
->_cooked_size
== 0)
705 sec
->_cooked_size
= sec
->_raw_size
;
707 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
709 /* Get a copy of the native relocations. */
710 internal_relocs
= (_bfd_elf32_link_read_relocs
711 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
712 link_info
->keep_memory
));
713 if (internal_relocs
== NULL
)
715 if (! link_info
->keep_memory
)
716 free_relocs
= internal_relocs
;
718 if (sec
!= last_input_section
)
721 last_input_section
= sec
;
723 /* Walk through the relocs looking for relaxing opportunities. */
724 irelend
= internal_relocs
+ sec
->reloc_count
;
725 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
729 /* Keep track of the previous reloc so that we can delete
730 some long jumps created by the compiler. */
731 if (irel
!= internal_relocs
)
732 last_reloc
= irel
- 1;
734 /* Get the section contents if we haven't done so already. */
735 if (contents
== NULL
)
737 /* Get cached copy if it exists. */
738 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
739 contents
= elf_section_data (sec
)->this_hdr
.contents
;
742 /* Go get them off disk. */
743 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
744 if (contents
== NULL
)
746 free_contents
= contents
;
748 if (! bfd_get_section_contents (abfd
, sec
, contents
,
749 (file_ptr
) 0, sec
->_raw_size
))
754 /* Read this BFD's symbols if we haven't done so already. */
757 /* Get cached copy if it exists. */
758 if (symtab_hdr
->contents
!= NULL
)
759 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
762 /* Go get them off disk. */
763 extsyms
= ((Elf32_External_Sym
*)
764 bfd_malloc (symtab_hdr
->sh_size
));
767 free_extsyms
= extsyms
;
768 if (bfd_seek (abfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
769 || (bfd_read (extsyms
, 1, symtab_hdr
->sh_size
, abfd
)
770 != symtab_hdr
->sh_size
))
775 /* Get the value of the symbol referred to by the reloc. */
776 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
778 Elf_Internal_Sym isym
;
781 /* A local symbol. */
782 bfd_elf32_swap_symbol_in (abfd
,
783 extsyms
+ ELF32_R_SYM (irel
->r_info
),
786 sym_sec
= bfd_section_from_elf_index (abfd
, isym
.st_shndx
);
787 symval
= (isym
.st_value
788 + sym_sec
->output_section
->vma
789 + sym_sec
->output_offset
);
794 struct elf_link_hash_entry
*h
;
796 /* An external symbol. */
797 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
798 h
= elf_sym_hashes (abfd
)[indx
];
799 BFD_ASSERT (h
!= NULL
);
800 if (h
->root
.type
!= bfd_link_hash_defined
801 && h
->root
.type
!= bfd_link_hash_defweak
)
803 /* This appears to be a reference to an undefined
804 symbol. Just ignore it--it will be caught by the
805 regular reloc processing. */
809 symval
= (h
->root
.u
.def
.value
810 + h
->root
.u
.def
.section
->output_section
->vma
811 + h
->root
.u
.def
.section
->output_offset
);
814 /* For simplicity of coding, we are going to modify the section
815 contents, the section relocs, and the BFD symbol table. We
816 must tell the rest of the code not to free up this
817 information. It would be possible to instead create a table
818 of changes which have to be made, as is done in coff-mips.c;
819 that would be more work, but would require less memory when
820 the linker is run. */
821 switch (ELF32_R_TYPE (irel
->r_info
))
823 /* Try to turn a 24 bit absolute branch/call into an 8 bit
824 pc-relative branch/call. */
827 bfd_vma value
= symval
+ irel
->r_addend
;
830 /* Get the address of this instruction. */
831 dot
= (sec
->output_section
->vma
832 + sec
->output_offset
+ irel
->r_offset
- 1);
834 /* Compute the distance from this insn to the branch target. */
837 /* If the distance is within -126..+130 inclusive, then we can
838 relax this jump. +130 is valid since the target will move
839 two bytes closer if we do relax this branch. */
840 if ((int)gap
>= -126 && (int)gap
<= 130)
844 /* Note that we've changed the relocs, section contents,
846 elf_section_data (sec
)->relocs
= internal_relocs
;
849 elf_section_data (sec
)->this_hdr
.contents
= contents
;
850 free_contents
= NULL
;
852 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
855 /* If the previous instruction conditionally jumped around
856 this instruction, we may be able to reverse the condition
857 and redirect the previous instruction to the target of
860 Such sequences are used by the compiler to deal with
861 long conditional branches. */
865 && ELF32_R_TYPE (last_reloc
->r_info
) == R_H8_PCREL8
866 && ELF32_R_SYM (last_reloc
->r_info
) < symtab_hdr
->sh_info
)
869 asection
*last_sym_sec
;
870 Elf_Internal_Sym last_symbol
;
872 /* We will need to examine the symbol used by the
873 previous relocation. */
875 bfd_elf32_swap_symbol_in (abfd
,
876 (extsyms
+ ELF32_R_SYM (last_reloc
->r_info
)),
880 = bfd_section_from_elf_index (abfd
, last_symbol
.st_shndx
);
881 last_value
= (last_symbol
.st_value
882 + last_sym_sec
->output_section
->vma
883 + last_sym_sec
->output_offset
);
885 /* Verify that the previous relocation was for a
886 branch around this instruction and that no symbol
887 exists at the current location. */
888 if (last_value
== dot
+ 4
889 && last_reloc
->r_offset
+ 2 == irel
->r_offset
890 && ! elf32_h8_symbol_address_p (abfd
, sec
,
893 /* We can eliminate this jump. Twiddle the
894 previous relocation as necessary. */
896 = ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
897 ELF32_R_TYPE (R_H8_NONE
));
900 = ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
901 ELF32_R_TYPE (R_H8_PCREL8
));
902 last_reloc
->r_addend
= irel
->r_addend
;
905 code
= bfd_get_8 (abfd
,
906 contents
+ last_reloc
->r_offset
- 1);
910 contents
+ last_reloc
->r_offset
- 1);
912 /* Delete four bytes of data. */
913 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
923 /* We could not eliminate this jump, so just shorten it. */
924 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
927 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 1);
928 else if (code
== 0x5a)
929 bfd_put_8 (abfd
, 0x40, contents
+ irel
->r_offset
- 1);
933 /* Fix the relocation's type. */
934 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
937 /* Delete two bytes of data. */
938 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
939 irel
->r_offset
+ 1, 2))
942 /* That will change things, so, we should relax again.
943 Note that this is not required, and it may be slow. */
949 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
953 bfd_vma value
= symval
+ irel
->r_addend
;
957 /* Get the address of this instruction. */
958 dot
= (sec
->output_section
->vma
960 + irel
->r_offset
- 2);
964 /* If the distance is within -126..+130 inclusive, then we can
965 relax this jump. +130 is valid since the target will move
966 two bytes closer if we do relax this branch. */
967 if ((int)gap
>= -126 && (int)gap
<= 130)
971 /* Note that we've changed the relocs, section contents,
973 elf_section_data (sec
)->relocs
= internal_relocs
;
976 elf_section_data (sec
)->this_hdr
.contents
= contents
;
977 free_contents
= NULL
;
979 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
982 /* Get the opcode. */
983 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
987 /* bCC:16 -> bCC:8 */
988 /* Get the condition code from the original insn. */
989 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
993 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
995 else if (code
== 0x5c)
996 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 2);
1000 /* Fix the relocation's type. */
1001 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1005 /* Delete two bytes of data. */
1006 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1007 irel
->r_offset
+ 1, 2))
1010 /* That will change things, so, we should relax again.
1011 Note that this is not required, and it may be slow. */
1017 /* This is a 16 bit absolute address in a "mov.b" insn, which may
1018 become an 8 bit absolute address if its in the right range. */
1021 bfd_vma value
= symval
+ irel
->r_addend
;
1023 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
1026 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
1027 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
1028 && value
>= 0xffff00
1029 && value
<= 0xffffff))
1033 /* Note that we've changed the relocs, section contents,
1035 elf_section_data (sec
)->relocs
= internal_relocs
;
1038 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1039 free_contents
= NULL
;
1041 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1042 free_extsyms
= NULL
;
1044 /* Get the opcode. */
1045 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1051 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1053 if ((code
& 0xf0) == 0x00)
1055 (code
& 0xf) | 0x20,
1056 contents
+ irel
->r_offset
- 2);
1057 else if ((code
& 0xf0) == 0x80)
1059 (code
& 0xf) | 0x30,
1060 contents
+ irel
->r_offset
- 2);
1064 /* Fix the relocation's type. */
1065 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1068 /* Delete two bytes of data. */
1069 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1070 irel
->r_offset
+ 1, 2))
1073 /* That will change things, so, we should relax again.
1074 Note that this is not required, and it may be slow. */
1080 /* This is a 24 bit absolute address in a "mov.b" insn, which may
1081 become an 8 bit absolute address if its in the right range. */
1084 bfd_vma value
= symval
+ irel
->r_addend
;
1086 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
1089 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
1090 || bfd_get_mach (abfd
) == bfd_mach_h8300s
)
1091 && value
>= 0xffff00
1092 && value
<= 0xffffff))
1096 /* Note that we've changed the relocs, section contents,
1098 elf_section_data (sec
)->relocs
= internal_relocs
;
1101 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1102 free_contents
= NULL
;
1104 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1105 free_extsyms
= NULL
;
1107 /* Get the opcode. */
1108 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1114 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1116 if ((code
& 0xf0) == 0x00)
1118 (code
& 0xf) | 0x20,
1119 contents
+ irel
->r_offset
- 2);
1120 else if ((code
& 0xf0) == 0x80)
1122 (code
& 0xf) | 0x30,
1123 contents
+ irel
->r_offset
- 2);
1127 /* Fix the relocation's type. */
1128 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1131 /* Delete two bytes of data. */
1132 if (!elf32_h8_relax_delete_bytes (abfd
, sec
, irel
->r_offset
, 2))
1135 /* That will change things, so, we should relax again.
1136 Note that this is not required, and it may be slow. */
1143 /* This is a 24/32bit absolute address in a "mov" insn, which may
1144 become a 16bit absoulte address if it is in the right range. */
1147 bfd_vma value
= symval
+ irel
->r_addend
;
1149 if (value
<= 0x7fff || value
>= 0xff8000)
1153 /* Note that we've changed the relocs, section contents,
1155 elf_section_data (sec
)->relocs
= internal_relocs
;
1158 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1159 free_contents
= NULL
;
1161 symtab_hdr
->contents
= (bfd_byte
*) extsyms
;
1162 free_extsyms
= NULL
;
1164 /* Get the opcode. */
1165 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1167 /* We just need to turn off bit 0x20. */
1170 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1172 /* Fix the relocation's type. */
1173 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1176 /* Delete two bytes of data. */
1177 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1178 irel
->r_offset
+ 1, 2))
1181 /* That will change things, so, we should relax again.
1182 Note that this is not required, and it may be slow. */
1193 if (free_relocs
!= NULL
)
1199 if (free_contents
!= NULL
)
1201 if (! link_info
->keep_memory
)
1202 free (free_contents
);
1205 /* Cache the section contents for elf_link_input_bfd. */
1206 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1208 free_contents
= NULL
;
1211 if (free_extsyms
!= NULL
)
1213 if (! link_info
->keep_memory
)
1214 free (free_extsyms
);
1217 /* Cache the symbols for elf_link_input_bfd. */
1218 symtab_hdr
->contents
= extsyms
;
1220 free_extsyms
= NULL
;
1226 if (free_relocs
!= NULL
)
1228 if (free_contents
!= NULL
)
1229 free (free_contents
);
1230 if (free_extsyms
!= NULL
)
1231 free (free_extsyms
);
1235 /* Delete some bytes from a section while relaxing. */
1238 elf32_h8_relax_delete_bytes (abfd
, sec
, addr
, count
)
1244 Elf_Internal_Shdr
*symtab_hdr
;
1245 Elf32_External_Sym
*extsyms
;
1248 Elf_Internal_Rela
*irel
, *irelend
;
1249 Elf_Internal_Rela
*irelalign
;
1251 Elf32_External_Sym
*esym
, *esymend
;
1252 struct elf_link_hash_entry
*sym_hash
;
1254 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1255 extsyms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1257 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1259 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1261 /* The deletion must stop at the next ALIGN reloc for an aligment
1262 power larger than the number of bytes we are deleting. */
1265 toaddr
= sec
->_cooked_size
;
1267 irel
= elf_section_data (sec
)->relocs
;
1268 irelend
= irel
+ sec
->reloc_count
;
1270 /* Actually delete the bytes. */
1271 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
1272 sec
->_cooked_size
-= count
;
1274 /* Adjust all the relocs. */
1275 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1277 /* Get the new reloc address. */
1278 if ((irel
->r_offset
> addr
1279 && irel
->r_offset
< toaddr
))
1280 irel
->r_offset
-= count
;
1283 /* Adjust the local symbols defined in this section. */
1285 esymend
= esym
+ symtab_hdr
->sh_info
;
1286 for (; esym
< esymend
; esym
++)
1288 Elf_Internal_Sym isym
;
1290 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1292 if (isym
.st_shndx
== shndx
1293 && isym
.st_value
> addr
1294 && isym
.st_value
< toaddr
)
1296 isym
.st_value
-= count
;
1297 bfd_elf32_swap_symbol_out (abfd
, &isym
, esym
);
1301 /* Now adjust the global symbols defined in this section. */
1302 esym
= extsyms
+ symtab_hdr
->sh_info
;
1303 esymend
= extsyms
+ (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
));
1304 for (index
= 0; esym
< esymend
; esym
++, index
++)
1306 Elf_Internal_Sym isym
;
1308 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1309 sym_hash
= elf_sym_hashes (abfd
)[index
];
1310 if (isym
.st_shndx
== shndx
1311 && ((sym_hash
)->root
.type
== bfd_link_hash_defined
1312 || (sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1313 && (sym_hash
)->root
.u
.def
.section
== sec
1314 && (sym_hash
)->root
.u
.def
.value
> addr
1315 && (sym_hash
)->root
.u
.def
.value
< toaddr
)
1317 (sym_hash
)->root
.u
.def
.value
-= count
;
1324 /* Return true if a symbol exists at the given address, else return
1327 elf32_h8_symbol_address_p (abfd
, sec
, extsyms
, addr
)
1330 Elf32_External_Sym
*extsyms
;
1333 Elf_Internal_Shdr
*symtab_hdr
;
1335 Elf32_External_Sym
*esym
, *esymend
;
1336 struct elf_link_hash_entry
**sym_hash
, **sym_hash_end
;
1338 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1339 shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1341 /* Examine all the symbols. */
1343 esymend
= esym
+ symtab_hdr
->sh_info
;
1344 for (; esym
< esymend
; esym
++)
1346 Elf_Internal_Sym isym
;
1348 bfd_elf32_swap_symbol_in (abfd
, esym
, &isym
);
1350 if (isym
.st_shndx
== shndx
1351 && isym
.st_value
== addr
)
1355 sym_hash
= elf_sym_hashes (abfd
);
1356 sym_hash_end
= (sym_hash
1357 + (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1358 - symtab_hdr
->sh_info
));
1359 for (; sym_hash
< sym_hash_end
; sym_hash
++)
1361 if (((*sym_hash
)->root
.type
== bfd_link_hash_defined
1362 || (*sym_hash
)->root
.type
== bfd_link_hash_defweak
)
1363 && (*sym_hash
)->root
.u
.def
.section
== sec
1364 && (*sym_hash
)->root
.u
.def
.value
== addr
)
1370 /* This is a version of bfd_generic_get_relocated_section_contents
1371 which uses elf32_h8_relocate_section. */
1374 elf32_h8_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1375 data
, relocateable
, symbols
)
1377 struct bfd_link_info
*link_info
;
1378 struct bfd_link_order
*link_order
;
1380 boolean relocateable
;
1383 Elf_Internal_Shdr
*symtab_hdr
;
1384 asection
*input_section
= link_order
->u
.indirect
.section
;
1385 bfd
*input_bfd
= input_section
->owner
;
1386 asection
**sections
= NULL
;
1387 Elf_Internal_Rela
*internal_relocs
= NULL
;
1388 Elf32_External_Sym
*external_syms
= NULL
;
1389 Elf_Internal_Sym
*internal_syms
= NULL
;
1391 /* We only need to handle the case of relaxing, or of having a
1392 particular set of section contents, specially. */
1394 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1395 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1400 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1402 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1403 input_section
->_raw_size
);
1405 if ((input_section
->flags
& SEC_RELOC
) != 0
1406 && input_section
->reloc_count
> 0)
1408 Elf_Internal_Sym
*isymp
;
1410 Elf32_External_Sym
*esym
, *esymend
;
1412 if (symtab_hdr
->contents
!= NULL
)
1413 external_syms
= (Elf32_External_Sym
*) symtab_hdr
->contents
;
1416 external_syms
= ((Elf32_External_Sym
*)
1417 bfd_malloc (symtab_hdr
->sh_info
1418 * sizeof (Elf32_External_Sym
)));
1419 if (external_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1421 if (bfd_seek (input_bfd
, symtab_hdr
->sh_offset
, SEEK_SET
) != 0
1422 || (bfd_read (external_syms
, sizeof (Elf32_External_Sym
),
1423 symtab_hdr
->sh_info
, input_bfd
)
1424 != (symtab_hdr
->sh_info
* sizeof (Elf32_External_Sym
))))
1428 internal_relocs
= (_bfd_elf32_link_read_relocs
1429 (input_bfd
, input_section
, (PTR
) NULL
,
1430 (Elf_Internal_Rela
*) NULL
, false));
1431 if (internal_relocs
== NULL
)
1434 internal_syms
= ((Elf_Internal_Sym
*)
1435 bfd_malloc (symtab_hdr
->sh_info
1436 * sizeof (Elf_Internal_Sym
)));
1437 if (internal_syms
== NULL
&& symtab_hdr
->sh_info
> 0)
1440 sections
= (asection
**) bfd_malloc (symtab_hdr
->sh_info
1441 * sizeof (asection
*));
1442 if (sections
== NULL
&& symtab_hdr
->sh_info
> 0)
1445 isymp
= internal_syms
;
1447 esym
= external_syms
;
1448 esymend
= esym
+ symtab_hdr
->sh_info
;
1449 for (; esym
< esymend
; ++esym
, ++isymp
, ++secpp
)
1453 bfd_elf32_swap_symbol_in (input_bfd
, esym
, isymp
);
1455 if (isymp
->st_shndx
== SHN_UNDEF
)
1456 isec
= bfd_und_section_ptr
;
1457 else if (isymp
->st_shndx
> 0 && isymp
->st_shndx
< SHN_LORESERVE
)
1458 isec
= bfd_section_from_elf_index (input_bfd
, isymp
->st_shndx
);
1459 else if (isymp
->st_shndx
== SHN_ABS
)
1460 isec
= bfd_abs_section_ptr
;
1461 else if (isymp
->st_shndx
== SHN_COMMON
)
1462 isec
= bfd_com_section_ptr
;
1472 if (! elf32_h8_relocate_section (output_bfd
, link_info
, input_bfd
,
1473 input_section
, data
, internal_relocs
,
1474 internal_syms
, sections
))
1477 if (sections
!= NULL
)
1480 if (internal_syms
!= NULL
)
1481 free (internal_syms
);
1482 internal_syms
= NULL
;
1483 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1484 free (external_syms
);
1485 external_syms
= NULL
;
1486 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
1487 free (internal_relocs
);
1488 internal_relocs
= NULL
;
1494 if (internal_relocs
!= NULL
1495 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
1496 free (internal_relocs
);
1497 if (external_syms
!= NULL
&& symtab_hdr
->contents
== NULL
)
1498 free (external_syms
);
1499 if (internal_syms
!= NULL
)
1500 free (internal_syms
);
1501 if (sections
!= NULL
)
1507 #define TARGET_BIG_SYM bfd_elf32_h8300_vec
1508 #define TARGET_BIG_NAME "elf32-h8300"
1509 #define ELF_ARCH bfd_arch_h8300
1510 #define ELF_MACHINE_CODE EM_H8_300
1511 #define ELF_MAXPAGESIZE 0x1
1512 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1513 #define elf_info_to_howto elf32_h8_info_to_howto
1514 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1516 /* So we can set/examine bits in e_flags to get the specific
1517 H8 architecture in use. */
1518 #define elf_backend_final_write_processing \
1519 elf32_h8_final_write_processing
1520 #define elf_backend_object_p \
1522 #define bfd_elf32_bfd_merge_private_bfd_data \
1523 elf32_h8_merge_private_bfd_data
1525 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1526 defaults to using _bfd_generic_link_hash_table_create, but
1527 elflink.h:bfd_elf32_size_dynamic_sections uses
1528 dynobj = elf_hash_table (info)->dynobj;
1529 and thus requires an elf hash table. */
1530 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1532 /* Use an H8 specific linker, not the ELF generic linker. */
1533 #define elf_backend_relocate_section elf32_h8_relocate_section
1535 /* And relaxing stuff. */
1536 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1537 #define bfd_elf32_bfd_get_relocated_section_contents \
1538 elf32_h8_get_relocated_section_contents
1541 #include "elf32-target.h"