1 /* BFD back-end for Renesas H8/300 ELF binaries.
2 Copyright 1993, 1995, 1998, 1999, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 static reloc_howto_type
*elf32_h8_reloc_type_lookup
28 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
29 static void elf32_h8_info_to_howto
30 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
31 static void elf32_h8_info_to_howto_rel
32 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
33 static unsigned long elf32_h8_mach
35 static void elf32_h8_final_write_processing
36 PARAMS ((bfd
*, bfd_boolean
));
37 static bfd_boolean elf32_h8_object_p
39 static bfd_boolean elf32_h8_merge_private_bfd_data
40 PARAMS ((bfd
*, bfd
*));
41 static bfd_boolean elf32_h8_relax_section
42 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
43 static bfd_boolean elf32_h8_relax_delete_bytes
44 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
45 static bfd_boolean elf32_h8_symbol_address_p
46 PARAMS ((bfd
*, asection
*, bfd_vma
));
47 static bfd_byte
*elf32_h8_get_relocated_section_contents
48 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
49 bfd_byte
*, bfd_boolean
, asymbol
**));
50 static bfd_reloc_status_type elf32_h8_final_link_relocate
51 PARAMS ((unsigned long, bfd
*, bfd
*, asection
*,
52 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
53 struct bfd_link_info
*, asection
*, int));
54 static bfd_boolean elf32_h8_relocate_section
55 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
56 bfd_byte
*, Elf_Internal_Rela
*,
57 Elf_Internal_Sym
*, asection
**));
58 static bfd_reloc_status_type special
59 PARAMS ((bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **));
61 /* This does not include any relocation information, but should be
62 good enough for GDB or objdump to read the file. */
64 static reloc_howto_type h8_elf_howto_table
[] = {
66 HOWTO (R_H8_NONE
, /* type */
68 0, /* size (0 = byte, 1 = short, 2 = long) */
70 FALSE
, /* pc_relative */
72 complain_overflow_dont
,/* complain_on_overflow */
73 special
, /* special_function */
74 "R_H8_NONE", /* name */
75 FALSE
, /* partial_inplace */
78 FALSE
), /* pcrel_offset */
79 #define R_H8_DIR32_X (R_H8_NONE_X + 1)
80 HOWTO (R_H8_DIR32
, /* type */
82 2, /* size (0 = byte, 1 = short, 2 = long) */
84 FALSE
, /* pc_relative */
86 complain_overflow_dont
,/* complain_on_overflow */
87 special
, /* special_function */
88 "R_H8_DIR32", /* name */
89 FALSE
, /* partial_inplace */
91 0xffffffff, /* dst_mask */
92 FALSE
), /* pcrel_offset */
93 #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
94 HOWTO (R_H8_DIR16
, /* type */
96 1, /* size (0 = byte, 1 = short, 2 = long) */
98 FALSE
, /* pc_relative */
100 complain_overflow_dont
,/* complain_on_overflow */
101 special
, /* special_function */
102 "R_H8_DIR16", /* name */
103 FALSE
, /* partial_inplace */
105 0x0000ffff, /* dst_mask */
106 FALSE
), /* pcrel_offset */
107 #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
108 HOWTO (R_H8_DIR8
, /* type */
110 0, /* size (0 = byte, 1 = short, 2 = long) */
112 FALSE
, /* pc_relative */
114 complain_overflow_dont
,/* complain_on_overflow */
115 special
, /* special_function */
116 "R_H8_DIR8", /* name */
117 FALSE
, /* partial_inplace */
119 0x000000ff, /* dst_mask */
120 FALSE
), /* pcrel_offset */
121 #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
122 HOWTO (R_H8_DIR16A8
, /* type */
124 1, /* size (0 = byte, 1 = short, 2 = long) */
126 FALSE
, /* pc_relative */
128 complain_overflow_bitfield
, /* complain_on_overflow */
129 special
, /* special_function */
130 "R_H8_DIR16A8", /* name */
131 FALSE
, /* partial_inplace */
133 0x0000ffff, /* dst_mask */
134 FALSE
), /* pcrel_offset */
135 #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
136 HOWTO (R_H8_DIR16R8
, /* type */
138 1, /* size (0 = byte, 1 = short, 2 = long) */
140 FALSE
, /* pc_relative */
142 complain_overflow_bitfield
, /* complain_on_overflow */
143 special
, /* special_function */
144 "R_H8_DIR16R8", /* name */
145 FALSE
, /* partial_inplace */
147 0x0000ffff, /* dst_mask */
148 FALSE
), /* pcrel_offset */
149 #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
150 HOWTO (R_H8_DIR24A8
, /* type */
152 2, /* size (0 = byte, 1 = short, 2 = long) */
154 FALSE
, /* pc_relative */
156 complain_overflow_bitfield
, /* complain_on_overflow */
157 special
, /* special_function */
158 "R_H8_DIR24A8", /* name */
159 TRUE
, /* partial_inplace */
160 0xff000000, /* src_mask */
161 0x00ffffff, /* dst_mask */
162 FALSE
), /* pcrel_offset */
163 #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
164 HOWTO (R_H8_DIR24R8
, /* type */
166 2, /* size (0 = byte, 1 = short, 2 = long) */
168 FALSE
, /* pc_relative */
170 complain_overflow_bitfield
, /* complain_on_overflow */
171 special
, /* special_function */
172 "R_H8_DIR24R8", /* name */
173 TRUE
, /* partial_inplace */
174 0xff000000, /* src_mask */
175 0x00ffffff, /* dst_mask */
176 FALSE
), /* pcrel_offset */
177 #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
178 HOWTO (R_H8_DIR32A16
, /* type */
180 2, /* size (0 = byte, 1 = short, 2 = long) */
182 FALSE
, /* pc_relative */
184 complain_overflow_dont
,/* complain_on_overflow */
185 special
, /* special_function */
186 "R_H8_DIR32", /* name */
187 FALSE
, /* partial_inplace */
189 0xffffffff, /* dst_mask */
190 FALSE
), /* pcrel_offset */
191 #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1)
192 HOWTO (R_H8_PCREL16
, /* type */
194 1, /* size (0 = byte, 1 = short, 2 = long) */
196 TRUE
, /* pc_relative */
198 complain_overflow_signed
,/* complain_on_overflow */
199 special
, /* special_function */
200 "R_H8_PCREL16", /* name */
201 FALSE
, /* partial_inplace */
202 0xffff, /* src_mask */
203 0xffff, /* dst_mask */
204 TRUE
), /* pcrel_offset */
205 #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
206 HOWTO (R_H8_PCREL8
, /* type */
208 0, /* size (0 = byte, 1 = short, 2 = long) */
210 TRUE
, /* pc_relative */
212 complain_overflow_signed
,/* complain_on_overflow */
213 special
, /* special_function */
214 "R_H8_PCREL8", /* name */
215 FALSE
, /* partial_inplace */
218 TRUE
), /* pcrel_offset */
221 /* This structure is used to map BFD reloc codes to H8 ELF relocs. */
223 struct elf_reloc_map
{
224 bfd_reloc_code_real_type bfd_reloc_val
;
225 unsigned char howto_index
;
228 /* An array mapping BFD reloc codes to SH ELF relocs. */
230 static const struct elf_reloc_map h8_reloc_map
[] = {
231 { BFD_RELOC_NONE
, R_H8_NONE_X
},
232 { BFD_RELOC_32
, R_H8_DIR32_X
},
233 { BFD_RELOC_16
, R_H8_DIR16_X
},
234 { BFD_RELOC_8
, R_H8_DIR8_X
},
235 { BFD_RELOC_H8_DIR16A8
, R_H8_DIR16A8_X
},
236 { BFD_RELOC_H8_DIR16R8
, R_H8_DIR16R8_X
},
237 { BFD_RELOC_H8_DIR24A8
, R_H8_DIR24A8_X
},
238 { BFD_RELOC_H8_DIR24R8
, R_H8_DIR24R8_X
},
239 { BFD_RELOC_H8_DIR32A16
, R_H8_DIR32A16_X
},
240 { BFD_RELOC_16_PCREL
, R_H8_PCREL16_X
},
241 { BFD_RELOC_8_PCREL
, R_H8_PCREL8_X
},
245 static reloc_howto_type
*
246 elf32_h8_reloc_type_lookup (abfd
, code
)
247 bfd
*abfd ATTRIBUTE_UNUSED
;
248 bfd_reloc_code_real_type code
;
252 for (i
= 0; i
< sizeof (h8_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
254 if (h8_reloc_map
[i
].bfd_reloc_val
== code
)
255 return &h8_elf_howto_table
[(int) h8_reloc_map
[i
].howto_index
];
261 elf32_h8_info_to_howto (abfd
, bfd_reloc
, elf_reloc
)
262 bfd
*abfd ATTRIBUTE_UNUSED
;
264 Elf_Internal_Rela
*elf_reloc
;
269 r
= ELF32_R_TYPE (elf_reloc
->r_info
);
270 for (i
= 0; i
< sizeof (h8_elf_howto_table
) / sizeof (reloc_howto_type
); i
++)
271 if (h8_elf_howto_table
[i
].type
== r
)
273 bfd_reloc
->howto
= &h8_elf_howto_table
[i
];
280 elf32_h8_info_to_howto_rel (abfd
, bfd_reloc
, elf_reloc
)
281 bfd
*abfd ATTRIBUTE_UNUSED
;
283 Elf_Internal_Rela
*elf_reloc ATTRIBUTE_UNUSED
;
288 r
= ELF32_R_TYPE (elf_reloc
->r_info
);
289 bfd_reloc
->howto
= &h8_elf_howto_table
[r
];
292 /* Special handling for H8/300 relocs.
293 We only come here for pcrel stuff and return normally if not an -r link.
294 When doing -r, we can't do any arithmetic for the pcrel stuff, because
295 we support relaxing on the H8/300 series chips. */
296 static bfd_reloc_status_type
297 special (abfd
, reloc_entry
, symbol
, data
, input_section
, output_bfd
,
299 bfd
*abfd ATTRIBUTE_UNUSED
;
300 arelent
*reloc_entry ATTRIBUTE_UNUSED
;
301 asymbol
*symbol ATTRIBUTE_UNUSED
;
302 PTR data ATTRIBUTE_UNUSED
;
303 asection
*input_section ATTRIBUTE_UNUSED
;
305 char **error_message ATTRIBUTE_UNUSED
;
307 if (output_bfd
== (bfd
*) NULL
)
308 return bfd_reloc_continue
;
310 /* Adjust the reloc address to that in the output section. */
311 reloc_entry
->address
+= input_section
->output_offset
;
315 /* Perform a relocation as part of a final link. */
316 static bfd_reloc_status_type
317 elf32_h8_final_link_relocate (r_type
, input_bfd
, output_bfd
,
318 input_section
, contents
, offset
, value
,
319 addend
, info
, sym_sec
, is_local
)
320 unsigned long r_type
;
322 bfd
*output_bfd ATTRIBUTE_UNUSED
;
323 asection
*input_section ATTRIBUTE_UNUSED
;
328 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
329 asection
*sym_sec ATTRIBUTE_UNUSED
;
330 int is_local ATTRIBUTE_UNUSED
;
332 bfd_byte
*hit_data
= contents
+ offset
;
344 bfd_put_32 (input_bfd
, value
, hit_data
);
351 bfd_put_16 (input_bfd
, value
, hit_data
);
358 bfd_put_8 (input_bfd
, value
, hit_data
);
364 /* HIT_DATA is the address for the first byte for the relocated
365 value. Subtract 1 so that we can manipulate the data in 32bit
369 /* Clear out the top byte in value. */
372 /* Retrieve the type byte for value from the section contents. */
373 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
375 /* Now scribble it out in one 32bit hunk. */
376 bfd_put_32 (input_bfd
, value
, hit_data
);
380 value
-= (input_section
->output_section
->vma
381 + input_section
->output_offset
);
385 /* The value is relative to the start of the instruction,
386 not the relocation offset. Subtract 2 to account for
390 bfd_put_16 (input_bfd
, value
, hit_data
);
394 value
-= (input_section
->output_section
->vma
395 + input_section
->output_offset
);
399 /* The value is relative to the start of the instruction,
400 not the relocation offset. Subtract 1 to account for
404 bfd_put_8 (input_bfd
, value
, hit_data
);
408 return bfd_reloc_notsupported
;
412 /* Relocate an H8 ELF section. */
414 elf32_h8_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
415 contents
, relocs
, local_syms
, local_sections
)
417 struct bfd_link_info
*info
;
419 asection
*input_section
;
421 Elf_Internal_Rela
*relocs
;
422 Elf_Internal_Sym
*local_syms
;
423 asection
**local_sections
;
425 Elf_Internal_Shdr
*symtab_hdr
;
426 struct elf_link_hash_entry
**sym_hashes
;
427 Elf_Internal_Rela
*rel
, *relend
;
429 if (info
->relocateable
)
432 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
433 sym_hashes
= elf_sym_hashes (input_bfd
);
436 relend
= relocs
+ input_section
->reloc_count
;
437 for (; rel
< relend
; rel
++)
440 unsigned long r_symndx
;
441 Elf_Internal_Sym
*sym
;
443 struct elf_link_hash_entry
*h
;
445 bfd_reloc_status_type r
;
447 /* This is a final link. */
448 r_symndx
= ELF32_R_SYM (rel
->r_info
);
449 r_type
= ELF32_R_TYPE (rel
->r_info
);
453 if (r_symndx
< symtab_hdr
->sh_info
)
455 sym
= local_syms
+ r_symndx
;
456 sec
= local_sections
[r_symndx
];
457 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
461 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
462 while (h
->root
.type
== bfd_link_hash_indirect
463 || h
->root
.type
== bfd_link_hash_warning
)
464 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
465 if (h
->root
.type
== bfd_link_hash_defined
466 || h
->root
.type
== bfd_link_hash_defweak
)
468 sec
= h
->root
.u
.def
.section
;
469 relocation
= (h
->root
.u
.def
.value
470 + sec
->output_section
->vma
471 + sec
->output_offset
);
473 else if (h
->root
.type
== bfd_link_hash_undefweak
)
477 if (! ((*info
->callbacks
->undefined_symbol
)
478 (info
, h
->root
.root
.string
, input_bfd
,
479 input_section
, rel
->r_offset
, TRUE
)))
485 r
= elf32_h8_final_link_relocate (r_type
, input_bfd
, output_bfd
,
487 contents
, rel
->r_offset
,
488 relocation
, rel
->r_addend
,
489 info
, sec
, h
== NULL
);
491 if (r
!= bfd_reloc_ok
)
494 const char *msg
= (const char *) 0;
496 reloc_howto_type
*howto
;
498 elf32_h8_info_to_howto (input_bfd
, &bfd_reloc
, rel
);
499 howto
= bfd_reloc
.howto
;
502 name
= h
->root
.root
.string
;
505 name
= (bfd_elf_string_from_elf_section
506 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
507 if (name
== NULL
|| *name
== '\0')
508 name
= bfd_section_name (input_bfd
, sec
);
513 case bfd_reloc_overflow
:
514 if (! ((*info
->callbacks
->reloc_overflow
)
515 (info
, name
, howto
->name
, (bfd_vma
) 0,
516 input_bfd
, input_section
, rel
->r_offset
)))
520 case bfd_reloc_undefined
:
521 if (! ((*info
->callbacks
->undefined_symbol
)
522 (info
, name
, input_bfd
, input_section
,
523 rel
->r_offset
, TRUE
)))
527 case bfd_reloc_outofrange
:
528 msg
= _("internal error: out of range error");
531 case bfd_reloc_notsupported
:
532 msg
= _("internal error: unsupported relocation error");
535 case bfd_reloc_dangerous
:
536 msg
= _("internal error: dangerous error");
540 msg
= _("internal error: unknown error");
544 if (!((*info
->callbacks
->warning
)
545 (info
, msg
, name
, input_bfd
, input_section
,
556 /* Object files encode the specific H8 model they were compiled
557 for in the ELF flags field.
559 Examine that field and return the proper BFD machine type for
562 elf32_h8_mach (flags
)
565 switch (flags
& EF_H8_MACH
)
567 case E_H8_MACH_H8300
:
569 return bfd_mach_h8300
;
571 case E_H8_MACH_H8300H
:
572 return bfd_mach_h8300h
;
574 case E_H8_MACH_H8300S
:
575 return bfd_mach_h8300s
;
577 case E_H8_MACH_H8300HN
:
578 return bfd_mach_h8300hn
;
580 case E_H8_MACH_H8300SN
:
581 return bfd_mach_h8300sn
;
583 case E_H8_MACH_H8300SX
:
584 return bfd_mach_h8300sx
;
586 case E_H8_MACH_H8300SXN
:
587 return bfd_mach_h8300sxn
;
591 /* The final processing done just before writing out a H8 ELF object
592 file. We use this opportunity to encode the BFD machine type
593 into the flags field in the object file. */
596 elf32_h8_final_write_processing (abfd
, linker
)
598 bfd_boolean linker ATTRIBUTE_UNUSED
;
602 switch (bfd_get_mach (abfd
))
606 val
= E_H8_MACH_H8300
;
609 case bfd_mach_h8300h
:
610 val
= E_H8_MACH_H8300H
;
613 case bfd_mach_h8300s
:
614 val
= E_H8_MACH_H8300S
;
617 case bfd_mach_h8300hn
:
618 val
= E_H8_MACH_H8300HN
;
621 case bfd_mach_h8300sn
:
622 val
= E_H8_MACH_H8300SN
;
625 case bfd_mach_h8300sx
:
626 val
= E_H8_MACH_H8300SX
;
629 case bfd_mach_h8300sxn
:
630 val
= E_H8_MACH_H8300SXN
;
634 elf_elfheader (abfd
)->e_flags
&= ~ (EF_H8_MACH
);
635 elf_elfheader (abfd
)->e_flags
|= val
;
638 /* Return nonzero if ABFD represents a valid H8 ELF object file; also
639 record the encoded machine type found in the ELF flags. */
642 elf32_h8_object_p (abfd
)
645 bfd_default_set_arch_mach (abfd
, bfd_arch_h8300
,
646 elf32_h8_mach (elf_elfheader (abfd
)->e_flags
));
650 /* Merge backend specific data from an object file to the output
651 object file when linking. The only data we need to copy at this
652 time is the architecture/machine information. */
655 elf32_h8_merge_private_bfd_data (ibfd
, obfd
)
659 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
660 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
663 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
664 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
666 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
667 bfd_get_mach (ibfd
)))
674 /* This function handles relaxing for the H8..
676 There's a few relaxing opportunites available on the H8:
678 jmp/jsr:24 -> bra/bsr:8 2 bytes
679 The jmp may be completely eliminated if the previous insn is a
680 conditional branch to the insn after the jump. In that case
681 we invert the branch and delete the jump and save 4 bytes.
683 bCC:16 -> bCC:8 2 bytes
684 bsr:16 -> bsr:8 2 bytes
686 mov.b:16 -> mov.b:8 2 bytes
687 mov.b:24/32 -> mov.b:8 4 bytes
689 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */
692 elf32_h8_relax_section (abfd
, sec
, link_info
, again
)
695 struct bfd_link_info
*link_info
;
698 Elf_Internal_Shdr
*symtab_hdr
;
699 Elf_Internal_Rela
*internal_relocs
;
700 Elf_Internal_Rela
*irel
, *irelend
;
701 bfd_byte
*contents
= NULL
;
702 Elf_Internal_Sym
*isymbuf
= NULL
;
703 static asection
*last_input_section
= NULL
;
704 static Elf_Internal_Rela
*last_reloc
= NULL
;
706 /* Assume nothing changes. */
709 /* We don't have to do anything for a relocateable link, if
710 this section does not have relocs, or if this is not a
712 if (link_info
->relocateable
713 || (sec
->flags
& SEC_RELOC
) == 0
714 || sec
->reloc_count
== 0
715 || (sec
->flags
& SEC_CODE
) == 0)
718 /* If this is the first time we have been called for this section,
719 initialize the cooked size. */
720 if (sec
->_cooked_size
== 0)
721 sec
->_cooked_size
= sec
->_raw_size
;
723 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
725 /* Get a copy of the native relocations. */
726 internal_relocs
= (_bfd_elf_link_read_relocs
727 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
728 link_info
->keep_memory
));
729 if (internal_relocs
== NULL
)
732 if (sec
!= last_input_section
)
735 last_input_section
= sec
;
737 /* Walk through the relocs looking for relaxing opportunities. */
738 irelend
= internal_relocs
+ sec
->reloc_count
;
739 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
743 /* Keep track of the previous reloc so that we can delete
744 some long jumps created by the compiler. */
745 if (irel
!= internal_relocs
)
746 last_reloc
= irel
- 1;
748 if (ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR24R8
749 && ELF32_R_TYPE (irel
->r_info
) != R_H8_PCREL16
750 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR16A8
751 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR24A8
752 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR32A16
)
755 /* Get the section contents if we haven't done so already. */
756 if (contents
== NULL
)
758 /* Get cached copy if it exists. */
759 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
760 contents
= elf_section_data (sec
)->this_hdr
.contents
;
763 /* Go get them off disk. */
764 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
765 if (contents
== NULL
)
768 if (! bfd_get_section_contents (abfd
, sec
, contents
,
769 (file_ptr
) 0, sec
->_raw_size
))
774 /* Read this BFD's local symbols if we haven't done so already. */
775 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
777 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
779 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
780 symtab_hdr
->sh_info
, 0,
786 /* Get the value of the symbol referred to by the reloc. */
787 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
789 /* A local symbol. */
790 Elf_Internal_Sym
*isym
;
793 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
794 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
795 symval
= isym
->st_value
;
796 /* If the reloc is absolute, it will not have
797 a symbol or section associated with it. */
799 symval
+= sym_sec
->output_section
->vma
800 + sym_sec
->output_offset
;
805 struct elf_link_hash_entry
*h
;
807 /* An external symbol. */
808 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
809 h
= elf_sym_hashes (abfd
)[indx
];
810 BFD_ASSERT (h
!= NULL
);
811 if (h
->root
.type
!= bfd_link_hash_defined
812 && h
->root
.type
!= bfd_link_hash_defweak
)
814 /* This appears to be a reference to an undefined
815 symbol. Just ignore it--it will be caught by the
816 regular reloc processing. */
820 symval
= (h
->root
.u
.def
.value
821 + h
->root
.u
.def
.section
->output_section
->vma
822 + h
->root
.u
.def
.section
->output_offset
);
825 /* For simplicity of coding, we are going to modify the section
826 contents, the section relocs, and the BFD symbol table. We
827 must tell the rest of the code not to free up this
828 information. It would be possible to instead create a table
829 of changes which have to be made, as is done in coff-mips.c;
830 that would be more work, but would require less memory when
831 the linker is run. */
832 switch (ELF32_R_TYPE (irel
->r_info
))
834 /* Try to turn a 24 bit absolute branch/call into an 8 bit
835 pc-relative branch/call. */
838 bfd_vma value
= symval
+ irel
->r_addend
;
841 /* Get the address of this instruction. */
842 dot
= (sec
->output_section
->vma
843 + sec
->output_offset
+ irel
->r_offset
- 1);
845 /* Compute the distance from this insn to the branch target. */
848 /* If the distance is within -126..+130 inclusive, then we can
849 relax this jump. +130 is valid since the target will move
850 two bytes closer if we do relax this branch. */
851 if ((int) gap
>= -126 && (int) gap
<= 130)
855 /* Note that we've changed the relocs, section contents,
857 elf_section_data (sec
)->relocs
= internal_relocs
;
858 elf_section_data (sec
)->this_hdr
.contents
= contents
;
859 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
861 /* Get the instruction code being relaxed. */
862 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
864 /* If the previous instruction conditionally jumped around
865 this instruction, we may be able to reverse the condition
866 and redirect the previous instruction to the target of
869 Such sequences are used by the compiler to deal with
870 long conditional branches.
872 Only perform this optimisation for jumps (code 0x5a) not
873 subroutine calls, as otherwise it could transform:
886 which changes the call (jsr) into a branch (bne). */
891 && ELF32_R_TYPE (last_reloc
->r_info
) == R_H8_PCREL8
892 && ELF32_R_SYM (last_reloc
->r_info
) < symtab_hdr
->sh_info
)
895 asection
*last_sym_sec
;
896 Elf_Internal_Sym
*last_sym
;
898 /* We will need to examine the symbol used by the
899 previous relocation. */
901 last_sym
= isymbuf
+ ELF32_R_SYM (last_reloc
->r_info
);
903 = bfd_section_from_elf_index (abfd
, last_sym
->st_shndx
);
904 last_value
= (last_sym
->st_value
905 + last_sym_sec
->output_section
->vma
906 + last_sym_sec
->output_offset
);
908 /* Verify that the previous relocation was for a
909 branch around this instruction and that no symbol
910 exists at the current location. */
911 if (last_value
== dot
+ 4
912 && last_reloc
->r_offset
+ 2 == irel
->r_offset
913 && ! elf32_h8_symbol_address_p (abfd
, sec
, dot
))
915 /* We can eliminate this jump. Twiddle the
916 previous relocation as necessary. */
918 = ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
919 ELF32_R_TYPE (R_H8_NONE
));
922 = ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
923 ELF32_R_TYPE (R_H8_PCREL8
));
924 last_reloc
->r_addend
= irel
->r_addend
;
926 code
= bfd_get_8 (abfd
,
927 contents
+ last_reloc
->r_offset
- 1);
931 contents
+ last_reloc
->r_offset
- 1);
933 /* Delete four bytes of data. */
934 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
945 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 1);
946 else if (code
== 0x5a)
947 bfd_put_8 (abfd
, 0x40, contents
+ irel
->r_offset
- 1);
951 /* Fix the relocation's type. */
952 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
955 /* Delete two bytes of data. */
956 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
957 irel
->r_offset
+ 1, 2))
960 /* That will change things, so, we should relax again.
961 Note that this is not required, and it may be slow. */
967 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
971 bfd_vma value
= symval
+ irel
->r_addend
;
975 /* Get the address of this instruction. */
976 dot
= (sec
->output_section
->vma
978 + irel
->r_offset
- 2);
982 /* If the distance is within -126..+130 inclusive, then we can
983 relax this jump. +130 is valid since the target will move
984 two bytes closer if we do relax this branch. */
985 if ((int) gap
>= -126 && (int) gap
<= 130)
989 /* Note that we've changed the relocs, section contents,
991 elf_section_data (sec
)->relocs
= internal_relocs
;
992 elf_section_data (sec
)->this_hdr
.contents
= contents
;
993 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
995 /* Get the opcode. */
996 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1000 /* bCC:16 -> bCC:8 */
1001 /* Get the condition code from the original insn. */
1002 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1006 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
1008 else if (code
== 0x5c)
1009 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 2);
1013 /* Fix the relocation's type. */
1014 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1018 /* Delete two bytes of data. */
1019 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1020 irel
->r_offset
+ 1, 2))
1023 /* That will change things, so, we should relax again.
1024 Note that this is not required, and it may be slow. */
1030 /* This is a 16 bit absolute address in a "mov.b" insn, which may
1031 become an 8 bit absolute address if its in the right range. */
1034 bfd_vma value
= symval
+ irel
->r_addend
;
1036 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
1039 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
1040 /* FIXME: h8300hn? */
1041 || bfd_get_mach (abfd
) == bfd_mach_h8300s
1042 /* FIXME: h8300sn? */
1043 || bfd_get_mach (abfd
) == bfd_mach_h8300sx
)
1044 && value
>= 0xffff00
1045 && value
<= 0xffffff))
1049 /* Note that we've changed the relocs, section contents,
1051 elf_section_data (sec
)->relocs
= internal_relocs
;
1052 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1053 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1055 /* Get the opcode. */
1056 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1062 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1064 if ((code
& 0xf0) == 0x00)
1066 (code
& 0xf) | 0x20,
1067 contents
+ irel
->r_offset
- 2);
1068 else if ((code
& 0xf0) == 0x80)
1070 (code
& 0xf) | 0x30,
1071 contents
+ irel
->r_offset
- 2);
1075 /* Fix the relocation's type. */
1076 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1079 /* Delete two bytes of data. */
1080 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1081 irel
->r_offset
+ 1, 2))
1084 /* That will change things, so, we should relax again.
1085 Note that this is not required, and it may be slow. */
1091 /* This is a 24 bit absolute address in a "mov.b" insn, which may
1092 become an 8 bit absolute address if its in the right range. */
1095 bfd_vma value
= symval
+ irel
->r_addend
;
1097 if ((bfd_get_mach (abfd
) == bfd_mach_h8300
1100 || ((bfd_get_mach (abfd
) == bfd_mach_h8300h
1101 /* FIXME: h8300hn? */
1102 || bfd_get_mach (abfd
) == bfd_mach_h8300s
1103 /* FIXME: h8300sn? */
1104 || bfd_get_mach (abfd
) == bfd_mach_h8300sx
)
1105 && value
>= 0xffff00
1106 && value
<= 0xffffff))
1108 bfd_boolean skip
= FALSE
;
1111 /* Note that we've changed the relocs, section contents,
1113 elf_section_data (sec
)->relocs
= internal_relocs
;
1114 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1115 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1117 /* Get the opcode. */
1118 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1124 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1126 switch (code
& 0xf0)
1129 bfd_put_8 (abfd
, (code
& 0xf) | 0x20,
1130 contents
+ irel
->r_offset
- 2);
1133 bfd_put_8 (abfd
, (code
& 0xf) | 0x30,
1134 contents
+ irel
->r_offset
- 2);
1138 /* Skip 32bit versions. */
1148 /* Fix the relocation's type. */
1149 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1152 /* Delete two bytes of data. */
1153 if (!elf32_h8_relax_delete_bytes (abfd
, sec
, irel
->r_offset
, 2))
1156 /* That will change things, so, we should relax again.
1157 Note that this is not required, and it may be slow. */
1164 /* This is a 24/32bit absolute address in a "mov" insn, which may
1165 become a 16bit absoulte address if it is in the right range. */
1168 bfd_vma value
= symval
+ irel
->r_addend
;
1170 if (value
<= 0x7fff || value
>= 0xff8000)
1174 /* Note that we've changed the relocs, section contents,
1176 elf_section_data (sec
)->relocs
= internal_relocs
;
1177 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1178 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1180 /* Get the opcode. */
1181 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1183 /* We just need to turn off bit 0x20. */
1186 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
1188 /* Fix the relocation's type. */
1189 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1192 /* Delete two bytes of data. */
1193 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1194 irel
->r_offset
+ 1, 2))
1197 /* That will change things, so, we should relax again.
1198 Note that this is not required, and it may be slow. */
1210 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1212 if (! link_info
->keep_memory
)
1215 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1218 if (contents
!= NULL
1219 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1221 if (! link_info
->keep_memory
)
1225 /* Cache the section contents for elf_link_input_bfd. */
1226 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1230 if (internal_relocs
!= NULL
1231 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1232 free (internal_relocs
);
1238 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1240 if (contents
!= NULL
1241 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1243 if (internal_relocs
!= NULL
1244 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1245 free (internal_relocs
);
1249 /* Delete some bytes from a section while relaxing. */
1252 elf32_h8_relax_delete_bytes (abfd
, sec
, addr
, count
)
1258 Elf_Internal_Shdr
*symtab_hdr
;
1259 unsigned int sec_shndx
;
1261 Elf_Internal_Rela
*irel
, *irelend
;
1262 Elf_Internal_Rela
*irelalign
;
1263 Elf_Internal_Sym
*isym
;
1264 Elf_Internal_Sym
*isymend
;
1266 struct elf_link_hash_entry
**sym_hashes
;
1267 struct elf_link_hash_entry
**end_hashes
;
1268 unsigned int symcount
;
1270 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1272 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1274 /* The deletion must stop at the next ALIGN reloc for an aligment
1275 power larger than the number of bytes we are deleting. */
1278 toaddr
= sec
->_cooked_size
;
1280 irel
= elf_section_data (sec
)->relocs
;
1281 irelend
= irel
+ sec
->reloc_count
;
1283 /* Actually delete the bytes. */
1284 memmove (contents
+ addr
, contents
+ addr
+ count
,
1285 (size_t) (toaddr
- addr
- count
));
1286 sec
->_cooked_size
-= count
;
1288 /* Adjust all the relocs. */
1289 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1291 /* Get the new reloc address. */
1292 if ((irel
->r_offset
> addr
1293 && irel
->r_offset
< toaddr
))
1294 irel
->r_offset
-= count
;
1297 /* Adjust the local symbols defined in this section. */
1298 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1299 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1300 isymend
= isym
+ symtab_hdr
->sh_info
;
1301 for (; isym
< isymend
; isym
++)
1303 if (isym
->st_shndx
== sec_shndx
1304 && isym
->st_value
> addr
1305 && isym
->st_value
< toaddr
)
1306 isym
->st_value
-= count
;
1309 /* Now adjust the global symbols defined in this section. */
1310 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1311 - symtab_hdr
->sh_info
);
1312 sym_hashes
= elf_sym_hashes (abfd
);
1313 end_hashes
= sym_hashes
+ symcount
;
1314 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1316 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1317 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1318 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1319 && sym_hash
->root
.u
.def
.section
== sec
1320 && sym_hash
->root
.u
.def
.value
> addr
1321 && sym_hash
->root
.u
.def
.value
< toaddr
)
1323 sym_hash
->root
.u
.def
.value
-= count
;
1330 /* Return TRUE if a symbol exists at the given address, else return
1333 elf32_h8_symbol_address_p (abfd
, sec
, addr
)
1338 Elf_Internal_Shdr
*symtab_hdr
;
1339 unsigned int sec_shndx
;
1340 Elf_Internal_Sym
*isym
;
1341 Elf_Internal_Sym
*isymend
;
1342 struct elf_link_hash_entry
**sym_hashes
;
1343 struct elf_link_hash_entry
**end_hashes
;
1344 unsigned int symcount
;
1346 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1348 /* Examine all the symbols. */
1349 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1350 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1351 isymend
= isym
+ symtab_hdr
->sh_info
;
1352 for (; isym
< isymend
; isym
++)
1354 if (isym
->st_shndx
== sec_shndx
1355 && isym
->st_value
== addr
)
1359 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1360 - symtab_hdr
->sh_info
);
1361 sym_hashes
= elf_sym_hashes (abfd
);
1362 end_hashes
= sym_hashes
+ symcount
;
1363 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1365 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1366 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1367 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1368 && sym_hash
->root
.u
.def
.section
== sec
1369 && sym_hash
->root
.u
.def
.value
== addr
)
1376 /* This is a version of bfd_generic_get_relocated_section_contents
1377 which uses elf32_h8_relocate_section. */
1380 elf32_h8_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
1381 data
, relocateable
, symbols
)
1383 struct bfd_link_info
*link_info
;
1384 struct bfd_link_order
*link_order
;
1386 bfd_boolean relocateable
;
1389 Elf_Internal_Shdr
*symtab_hdr
;
1390 asection
*input_section
= link_order
->u
.indirect
.section
;
1391 bfd
*input_bfd
= input_section
->owner
;
1392 asection
**sections
= NULL
;
1393 Elf_Internal_Rela
*internal_relocs
= NULL
;
1394 Elf_Internal_Sym
*isymbuf
= NULL
;
1396 /* We only need to handle the case of relaxing, or of having a
1397 particular set of section contents, specially. */
1399 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1400 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1405 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1407 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1408 (size_t) input_section
->_raw_size
);
1410 if ((input_section
->flags
& SEC_RELOC
) != 0
1411 && input_section
->reloc_count
> 0)
1414 Elf_Internal_Sym
*isym
, *isymend
;
1417 internal_relocs
= (_bfd_elf_link_read_relocs
1418 (input_bfd
, input_section
, (PTR
) NULL
,
1419 (Elf_Internal_Rela
*) NULL
, FALSE
));
1420 if (internal_relocs
== NULL
)
1423 if (symtab_hdr
->sh_info
!= 0)
1425 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1426 if (isymbuf
== NULL
)
1427 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1428 symtab_hdr
->sh_info
, 0,
1430 if (isymbuf
== NULL
)
1434 amt
= symtab_hdr
->sh_info
;
1435 amt
*= sizeof (asection
*);
1436 sections
= (asection
**) bfd_malloc (amt
);
1437 if (sections
== NULL
&& amt
!= 0)
1440 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1441 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1445 if (isym
->st_shndx
== SHN_UNDEF
)
1446 isec
= bfd_und_section_ptr
;
1447 else if (isym
->st_shndx
== SHN_ABS
)
1448 isec
= bfd_abs_section_ptr
;
1449 else if (isym
->st_shndx
== SHN_COMMON
)
1450 isec
= bfd_com_section_ptr
;
1452 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1457 if (! elf32_h8_relocate_section (output_bfd
, link_info
, input_bfd
,
1458 input_section
, data
, internal_relocs
,
1462 if (sections
!= NULL
)
1465 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1467 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1468 free (internal_relocs
);
1474 if (sections
!= NULL
)
1477 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1479 if (internal_relocs
!= NULL
1480 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1481 free (internal_relocs
);
1486 #define TARGET_BIG_SYM bfd_elf32_h8300_vec
1487 #define TARGET_BIG_NAME "elf32-h8300"
1488 #define ELF_ARCH bfd_arch_h8300
1489 #define ELF_MACHINE_CODE EM_H8_300
1490 #define ELF_MAXPAGESIZE 0x1
1491 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1492 #define elf_info_to_howto elf32_h8_info_to_howto
1493 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1495 /* So we can set/examine bits in e_flags to get the specific
1496 H8 architecture in use. */
1497 #define elf_backend_final_write_processing \
1498 elf32_h8_final_write_processing
1499 #define elf_backend_object_p \
1501 #define bfd_elf32_bfd_merge_private_bfd_data \
1502 elf32_h8_merge_private_bfd_data
1504 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1505 defaults to using _bfd_generic_link_hash_table_create, but
1506 elflink.h:bfd_elf32_size_dynamic_sections uses
1507 dynobj = elf_hash_table (info)->dynobj;
1508 and thus requires an elf hash table. */
1509 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1511 /* Use an H8 specific linker, not the ELF generic linker. */
1512 #define elf_backend_relocate_section elf32_h8_relocate_section
1513 #define elf_backend_rela_normal 1
1515 /* And relaxing stuff. */
1516 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1517 #define bfd_elf32_bfd_get_relocated_section_contents \
1518 elf32_h8_get_relocated_section_contents
1521 #include "elf32-target.h"