1 /* BFD back-end for Renesas H8/300 ELF binaries.
2 Copyright 1993, 1995, 1998, 1999, 2001, 2002, 2003, 2004
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 (bfd
*abfd
, bfd_reloc_code_real_type code
);
29 static void elf32_h8_info_to_howto
30 (bfd
*, arelent
*, Elf_Internal_Rela
*);
31 static void elf32_h8_info_to_howto_rel
32 (bfd
*, arelent
*, Elf_Internal_Rela
*);
33 static unsigned long elf32_h8_mach
35 static void elf32_h8_final_write_processing
37 static bfd_boolean elf32_h8_object_p
39 static bfd_boolean elf32_h8_merge_private_bfd_data
41 static bfd_boolean elf32_h8_relax_section
42 (bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*);
43 static bfd_boolean elf32_h8_relax_delete_bytes
44 (bfd
*, asection
*, bfd_vma
, int);
45 static bfd_boolean elf32_h8_symbol_address_p
46 (bfd
*, asection
*, bfd_vma
);
47 static bfd_byte
*elf32_h8_get_relocated_section_contents
48 (bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
49 bfd_byte
*, bfd_boolean
, asymbol
**);
50 static asection
* elf32_h8_gc_mark_hook
51 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
52 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
53 static bfd_boolean elf32_h8_gc_sweep_hook
54 (bfd
*, struct bfd_link_info
*, asection
*, const Elf_Internal_Rela
*);
55 static bfd_reloc_status_type elf32_h8_final_link_relocate
56 (unsigned long, bfd
*, bfd
*, asection
*,
57 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
58 struct bfd_link_info
*, asection
*, int);
59 static bfd_boolean elf32_h8_relocate_section
60 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
61 bfd_byte
*, Elf_Internal_Rela
*,
62 Elf_Internal_Sym
*, asection
**);
63 static bfd_reloc_status_type special
64 (bfd
*, arelent
*, asymbol
*, PTR
, asection
*, bfd
*, char **);
66 /* This does not include any relocation information, but should be
67 good enough for GDB or objdump to read the file. */
69 static reloc_howto_type h8_elf_howto_table
[] = {
71 HOWTO (R_H8_NONE
, /* type */
73 0, /* size (0 = byte, 1 = short, 2 = long) */
75 FALSE
, /* pc_relative */
77 complain_overflow_dont
,/* complain_on_overflow */
78 special
, /* special_function */
79 "R_H8_NONE", /* name */
80 FALSE
, /* partial_inplace */
83 FALSE
), /* pcrel_offset */
84 #define R_H8_DIR32_X (R_H8_NONE_X + 1)
85 HOWTO (R_H8_DIR32
, /* type */
87 2, /* size (0 = byte, 1 = short, 2 = long) */
89 FALSE
, /* pc_relative */
91 complain_overflow_dont
,/* complain_on_overflow */
92 special
, /* special_function */
93 "R_H8_DIR32", /* name */
94 FALSE
, /* partial_inplace */
96 0xffffffff, /* dst_mask */
97 FALSE
), /* pcrel_offset */
98 #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
99 HOWTO (R_H8_DIR16
, /* type */
101 1, /* size (0 = byte, 1 = short, 2 = long) */
103 FALSE
, /* pc_relative */
105 complain_overflow_dont
,/* complain_on_overflow */
106 special
, /* special_function */
107 "R_H8_DIR16", /* name */
108 FALSE
, /* partial_inplace */
110 0x0000ffff, /* dst_mask */
111 FALSE
), /* pcrel_offset */
112 #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
113 HOWTO (R_H8_DIR8
, /* type */
115 0, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE
, /* pc_relative */
119 complain_overflow_dont
,/* complain_on_overflow */
120 special
, /* special_function */
121 "R_H8_DIR8", /* name */
122 FALSE
, /* partial_inplace */
124 0x000000ff, /* dst_mask */
125 FALSE
), /* pcrel_offset */
126 #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
127 HOWTO (R_H8_DIR16A8
, /* type */
129 1, /* size (0 = byte, 1 = short, 2 = long) */
131 FALSE
, /* pc_relative */
133 complain_overflow_bitfield
, /* complain_on_overflow */
134 special
, /* special_function */
135 "R_H8_DIR16A8", /* name */
136 FALSE
, /* partial_inplace */
138 0x0000ffff, /* dst_mask */
139 FALSE
), /* pcrel_offset */
140 #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
141 HOWTO (R_H8_DIR16R8
, /* type */
143 1, /* size (0 = byte, 1 = short, 2 = long) */
145 FALSE
, /* pc_relative */
147 complain_overflow_bitfield
, /* complain_on_overflow */
148 special
, /* special_function */
149 "R_H8_DIR16R8", /* name */
150 FALSE
, /* partial_inplace */
152 0x0000ffff, /* dst_mask */
153 FALSE
), /* pcrel_offset */
154 #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
155 HOWTO (R_H8_DIR24A8
, /* type */
157 2, /* size (0 = byte, 1 = short, 2 = long) */
159 FALSE
, /* pc_relative */
161 complain_overflow_bitfield
, /* complain_on_overflow */
162 special
, /* special_function */
163 "R_H8_DIR24A8", /* name */
164 TRUE
, /* partial_inplace */
165 0xff000000, /* src_mask */
166 0x00ffffff, /* dst_mask */
167 FALSE
), /* pcrel_offset */
168 #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
169 HOWTO (R_H8_DIR24R8
, /* type */
171 2, /* size (0 = byte, 1 = short, 2 = long) */
173 FALSE
, /* pc_relative */
175 complain_overflow_bitfield
, /* complain_on_overflow */
176 special
, /* special_function */
177 "R_H8_DIR24R8", /* name */
178 TRUE
, /* partial_inplace */
179 0xff000000, /* src_mask */
180 0x00ffffff, /* dst_mask */
181 FALSE
), /* pcrel_offset */
182 #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
183 HOWTO (R_H8_DIR32A16
, /* type */
185 2, /* size (0 = byte, 1 = short, 2 = long) */
187 FALSE
, /* pc_relative */
189 complain_overflow_dont
,/* complain_on_overflow */
190 special
, /* special_function */
191 "R_H8_DIR32A16", /* name */
192 FALSE
, /* partial_inplace */
194 0xffffffff, /* dst_mask */
195 FALSE
), /* pcrel_offset */
196 #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1)
197 HOWTO (R_H8_PCREL16
, /* type */
199 1, /* size (0 = byte, 1 = short, 2 = long) */
201 TRUE
, /* pc_relative */
203 complain_overflow_signed
,/* complain_on_overflow */
204 special
, /* special_function */
205 "R_H8_PCREL16", /* name */
206 FALSE
, /* partial_inplace */
207 0xffff, /* src_mask */
208 0xffff, /* dst_mask */
209 TRUE
), /* pcrel_offset */
210 #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
211 HOWTO (R_H8_PCREL8
, /* type */
213 0, /* size (0 = byte, 1 = short, 2 = long) */
215 TRUE
, /* pc_relative */
217 complain_overflow_signed
,/* complain_on_overflow */
218 special
, /* special_function */
219 "R_H8_PCREL8", /* name */
220 FALSE
, /* partial_inplace */
223 TRUE
), /* pcrel_offset */
226 /* This structure is used to map BFD reloc codes to H8 ELF relocs. */
228 struct elf_reloc_map
{
229 bfd_reloc_code_real_type bfd_reloc_val
;
230 unsigned char howto_index
;
233 /* An array mapping BFD reloc codes to H8 ELF relocs. */
235 static const struct elf_reloc_map h8_reloc_map
[] = {
236 { BFD_RELOC_NONE
, R_H8_NONE_X
},
237 { BFD_RELOC_32
, R_H8_DIR32_X
},
238 { BFD_RELOC_16
, R_H8_DIR16_X
},
239 { BFD_RELOC_8
, R_H8_DIR8_X
},
240 { BFD_RELOC_H8_DIR16A8
, R_H8_DIR16A8_X
},
241 { BFD_RELOC_H8_DIR16R8
, R_H8_DIR16R8_X
},
242 { BFD_RELOC_H8_DIR24A8
, R_H8_DIR24A8_X
},
243 { BFD_RELOC_H8_DIR24R8
, R_H8_DIR24R8_X
},
244 { BFD_RELOC_H8_DIR32A16
, R_H8_DIR32A16_X
},
245 { BFD_RELOC_16_PCREL
, R_H8_PCREL16_X
},
246 { BFD_RELOC_8_PCREL
, R_H8_PCREL8_X
},
250 static reloc_howto_type
*
251 elf32_h8_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
252 bfd_reloc_code_real_type code
)
256 for (i
= 0; i
< sizeof (h8_reloc_map
) / sizeof (struct elf_reloc_map
); i
++)
258 if (h8_reloc_map
[i
].bfd_reloc_val
== code
)
259 return &h8_elf_howto_table
[(int) h8_reloc_map
[i
].howto_index
];
265 elf32_h8_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*bfd_reloc
,
266 Elf_Internal_Rela
*elf_reloc
)
271 r
= ELF32_R_TYPE (elf_reloc
->r_info
);
272 for (i
= 0; i
< sizeof (h8_elf_howto_table
) / sizeof (reloc_howto_type
); i
++)
273 if (h8_elf_howto_table
[i
].type
== r
)
275 bfd_reloc
->howto
= &h8_elf_howto_table
[i
];
282 elf32_h8_info_to_howto_rel (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*bfd_reloc
,
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 (bfd
*abfd ATTRIBUTE_UNUSED
,
298 arelent
*reloc_entry ATTRIBUTE_UNUSED
,
299 asymbol
*symbol ATTRIBUTE_UNUSED
,
300 PTR data ATTRIBUTE_UNUSED
,
301 asection
*input_section ATTRIBUTE_UNUSED
,
303 char **error_message ATTRIBUTE_UNUSED
)
305 if (output_bfd
== (bfd
*) NULL
)
306 return bfd_reloc_continue
;
308 /* Adjust the reloc address to that in the output section. */
309 reloc_entry
->address
+= input_section
->output_offset
;
313 /* Perform a relocation as part of a final link. */
314 static bfd_reloc_status_type
315 elf32_h8_final_link_relocate (unsigned long r_type
, bfd
*input_bfd
,
316 bfd
*output_bfd ATTRIBUTE_UNUSED
,
317 asection
*input_section ATTRIBUTE_UNUSED
,
318 bfd_byte
*contents
, bfd_vma offset
,
319 bfd_vma value
, bfd_vma addend
,
320 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
321 asection
*sym_sec ATTRIBUTE_UNUSED
,
322 int is_local ATTRIBUTE_UNUSED
)
324 bfd_byte
*hit_data
= contents
+ offset
;
335 bfd_put_32 (input_bfd
, value
, hit_data
);
342 bfd_put_16 (input_bfd
, value
, hit_data
);
349 bfd_put_8 (input_bfd
, value
, hit_data
);
355 /* HIT_DATA is the address for the first byte for the relocated
356 value. Subtract 1 so that we can manipulate the data in 32bit
360 /* Clear out the top byte in value. */
363 /* Retrieve the type byte for value from the section contents. */
364 value
|= (bfd_get_32 (input_bfd
, hit_data
) & 0xff000000);
366 /* Now scribble it out in one 32bit hunk. */
367 bfd_put_32 (input_bfd
, value
, hit_data
);
371 value
-= (input_section
->output_section
->vma
372 + input_section
->output_offset
);
376 /* The value is relative to the start of the instruction,
377 not the relocation offset. Subtract 2 to account for
381 bfd_put_16 (input_bfd
, value
, hit_data
);
385 value
-= (input_section
->output_section
->vma
386 + input_section
->output_offset
);
390 /* The value is relative to the start of the instruction,
391 not the relocation offset. Subtract 1 to account for
395 bfd_put_8 (input_bfd
, value
, hit_data
);
399 return bfd_reloc_notsupported
;
403 /* Relocate an H8 ELF section. */
405 elf32_h8_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
406 bfd
*input_bfd
, asection
*input_section
,
407 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
408 Elf_Internal_Sym
*local_syms
,
409 asection
**local_sections
)
411 Elf_Internal_Shdr
*symtab_hdr
;
412 struct elf_link_hash_entry
**sym_hashes
;
413 Elf_Internal_Rela
*rel
, *relend
;
415 if (info
->relocatable
)
418 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
419 sym_hashes
= elf_sym_hashes (input_bfd
);
422 relend
= relocs
+ input_section
->reloc_count
;
423 for (; rel
< relend
; rel
++)
426 unsigned long r_symndx
;
427 Elf_Internal_Sym
*sym
;
429 struct elf_link_hash_entry
*h
;
431 bfd_reloc_status_type r
;
433 /* This is a final link. */
434 r_symndx
= ELF32_R_SYM (rel
->r_info
);
435 r_type
= ELF32_R_TYPE (rel
->r_info
);
439 if (r_symndx
< symtab_hdr
->sh_info
)
441 sym
= local_syms
+ r_symndx
;
442 sec
= local_sections
[r_symndx
];
443 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
447 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
448 while (h
->root
.type
== bfd_link_hash_indirect
449 || h
->root
.type
== bfd_link_hash_warning
)
450 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
451 if (h
->root
.type
== bfd_link_hash_defined
452 || h
->root
.type
== bfd_link_hash_defweak
)
454 sec
= h
->root
.u
.def
.section
;
455 relocation
= (h
->root
.u
.def
.value
456 + sec
->output_section
->vma
457 + sec
->output_offset
);
459 else if (h
->root
.type
== bfd_link_hash_undefweak
)
463 if (! ((*info
->callbacks
->undefined_symbol
)
464 (info
, h
->root
.root
.string
, input_bfd
,
465 input_section
, rel
->r_offset
, TRUE
)))
471 r
= elf32_h8_final_link_relocate (r_type
, input_bfd
, output_bfd
,
473 contents
, rel
->r_offset
,
474 relocation
, rel
->r_addend
,
475 info
, sec
, h
== NULL
);
477 if (r
!= bfd_reloc_ok
)
480 const char *msg
= (const char *) 0;
482 reloc_howto_type
*howto
;
484 elf32_h8_info_to_howto (input_bfd
, &bfd_reloc
, rel
);
485 howto
= bfd_reloc
.howto
;
488 name
= h
->root
.root
.string
;
491 name
= (bfd_elf_string_from_elf_section
492 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
493 if (name
== NULL
|| *name
== '\0')
494 name
= bfd_section_name (input_bfd
, sec
);
499 case bfd_reloc_overflow
:
500 if (! ((*info
->callbacks
->reloc_overflow
)
501 (info
, name
, howto
->name
, (bfd_vma
) 0,
502 input_bfd
, input_section
, rel
->r_offset
)))
506 case bfd_reloc_undefined
:
507 if (! ((*info
->callbacks
->undefined_symbol
)
508 (info
, name
, input_bfd
, input_section
,
509 rel
->r_offset
, TRUE
)))
513 case bfd_reloc_outofrange
:
514 msg
= _("internal error: out of range error");
517 case bfd_reloc_notsupported
:
518 msg
= _("internal error: unsupported relocation error");
521 case bfd_reloc_dangerous
:
522 msg
= _("internal error: dangerous error");
526 msg
= _("internal error: unknown error");
530 if (!((*info
->callbacks
->warning
)
531 (info
, msg
, name
, input_bfd
, input_section
,
542 /* Object files encode the specific H8 model they were compiled
543 for in the ELF flags field.
545 Examine that field and return the proper BFD machine type for
548 elf32_h8_mach (flagword flags
)
550 switch (flags
& EF_H8_MACH
)
552 case E_H8_MACH_H8300
:
554 return bfd_mach_h8300
;
556 case E_H8_MACH_H8300H
:
557 return bfd_mach_h8300h
;
559 case E_H8_MACH_H8300S
:
560 return bfd_mach_h8300s
;
562 case E_H8_MACH_H8300HN
:
563 return bfd_mach_h8300hn
;
565 case E_H8_MACH_H8300SN
:
566 return bfd_mach_h8300sn
;
568 case E_H8_MACH_H8300SX
:
569 return bfd_mach_h8300sx
;
571 case E_H8_MACH_H8300SXN
:
572 return bfd_mach_h8300sxn
;
576 /* The final processing done just before writing out a H8 ELF object
577 file. We use this opportunity to encode the BFD machine type
578 into the flags field in the object file. */
581 elf32_h8_final_write_processing (bfd
*abfd
,
582 bfd_boolean linker ATTRIBUTE_UNUSED
)
586 switch (bfd_get_mach (abfd
))
590 val
= E_H8_MACH_H8300
;
593 case bfd_mach_h8300h
:
594 val
= E_H8_MACH_H8300H
;
597 case bfd_mach_h8300s
:
598 val
= E_H8_MACH_H8300S
;
601 case bfd_mach_h8300hn
:
602 val
= E_H8_MACH_H8300HN
;
605 case bfd_mach_h8300sn
:
606 val
= E_H8_MACH_H8300SN
;
609 case bfd_mach_h8300sx
:
610 val
= E_H8_MACH_H8300SX
;
613 case bfd_mach_h8300sxn
:
614 val
= E_H8_MACH_H8300SXN
;
618 elf_elfheader (abfd
)->e_flags
&= ~ (EF_H8_MACH
);
619 elf_elfheader (abfd
)->e_flags
|= val
;
622 /* Return nonzero if ABFD represents a valid H8 ELF object file; also
623 record the encoded machine type found in the ELF flags. */
626 elf32_h8_object_p (bfd
*abfd
)
628 bfd_default_set_arch_mach (abfd
, bfd_arch_h8300
,
629 elf32_h8_mach (elf_elfheader (abfd
)->e_flags
));
633 /* Merge backend specific data from an object file to the output
634 object file when linking. The only data we need to copy at this
635 time is the architecture/machine information. */
638 elf32_h8_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
640 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
641 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
644 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
645 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
647 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
648 bfd_get_mach (ibfd
)))
655 /* This function handles relaxing for the H8..
657 There are a few relaxing opportunities available on the H8:
659 jmp/jsr:24 -> bra/bsr:8 2 bytes
660 The jmp may be completely eliminated if the previous insn is a
661 conditional branch to the insn after the jump. In that case
662 we invert the branch and delete the jump and save 4 bytes.
664 bCC:16 -> bCC:8 2 bytes
665 bsr:16 -> bsr:8 2 bytes
667 mov.b:16 -> mov.b:8 2 bytes
668 mov.b:24/32 -> mov.b:8 4 bytes
670 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */
673 elf32_h8_relax_section (bfd
*abfd
, asection
*sec
,
674 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
676 Elf_Internal_Shdr
*symtab_hdr
;
677 Elf_Internal_Rela
*internal_relocs
;
678 Elf_Internal_Rela
*irel
, *irelend
;
679 bfd_byte
*contents
= NULL
;
680 Elf_Internal_Sym
*isymbuf
= NULL
;
681 static asection
*last_input_section
= NULL
;
682 static Elf_Internal_Rela
*last_reloc
= NULL
;
684 /* Assume nothing changes. */
687 /* We don't have to do anything for a relocatable link, if
688 this section does not have relocs, or if this is not a
690 if (link_info
->relocatable
691 || (sec
->flags
& SEC_RELOC
) == 0
692 || sec
->reloc_count
== 0
693 || (sec
->flags
& SEC_CODE
) == 0)
696 /* If this is the first time we have been called for this section,
697 initialize the cooked size. */
698 if (sec
->_cooked_size
== 0)
699 sec
->_cooked_size
= sec
->_raw_size
;
701 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
703 /* Get a copy of the native relocations. */
704 internal_relocs
= (_bfd_elf_link_read_relocs
705 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
706 link_info
->keep_memory
));
707 if (internal_relocs
== NULL
)
710 if (sec
!= last_input_section
)
713 last_input_section
= sec
;
715 /* Walk through the relocs looking for relaxing opportunities. */
716 irelend
= internal_relocs
+ sec
->reloc_count
;
717 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
721 /* Keep track of the previous reloc so that we can delete
722 some long jumps created by the compiler. */
723 if (irel
!= internal_relocs
)
724 last_reloc
= irel
- 1;
726 if (ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR24R8
727 && ELF32_R_TYPE (irel
->r_info
) != R_H8_PCREL16
728 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR16A8
729 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR24A8
730 && ELF32_R_TYPE (irel
->r_info
) != R_H8_DIR32A16
)
733 /* Get the section contents if we haven't done so already. */
734 if (contents
== NULL
)
736 /* Get cached copy if it exists. */
737 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
738 contents
= elf_section_data (sec
)->this_hdr
.contents
;
741 /* Go get them off disk. */
742 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
743 if (contents
== NULL
)
746 if (! bfd_get_section_contents (abfd
, sec
, contents
,
747 (file_ptr
) 0, sec
->_raw_size
))
752 /* Read this BFD's local symbols if we haven't done so already. */
753 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
755 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
757 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
758 symtab_hdr
->sh_info
, 0,
764 /* Get the value of the symbol referred to by the reloc. */
765 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
767 /* A local symbol. */
768 Elf_Internal_Sym
*isym
;
771 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
772 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
773 symval
= isym
->st_value
;
774 /* If the reloc is absolute, it will not have
775 a symbol or section associated with it. */
777 symval
+= sym_sec
->output_section
->vma
778 + sym_sec
->output_offset
;
783 struct elf_link_hash_entry
*h
;
785 /* An external symbol. */
786 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
787 h
= elf_sym_hashes (abfd
)[indx
];
788 BFD_ASSERT (h
!= NULL
);
789 if (h
->root
.type
!= bfd_link_hash_defined
790 && h
->root
.type
!= bfd_link_hash_defweak
)
792 /* This appears to be a reference to an undefined
793 symbol. Just ignore it--it will be caught by the
794 regular reloc processing. */
798 symval
= (h
->root
.u
.def
.value
799 + h
->root
.u
.def
.section
->output_section
->vma
800 + h
->root
.u
.def
.section
->output_offset
);
803 /* For simplicity of coding, we are going to modify the section
804 contents, the section relocs, and the BFD symbol table. We
805 must tell the rest of the code not to free up this
806 information. It would be possible to instead create a table
807 of changes which have to be made, as is done in coff-mips.c;
808 that would be more work, but would require less memory when
809 the linker is run. */
810 switch (ELF32_R_TYPE (irel
->r_info
))
812 /* Try to turn a 24 bit absolute branch/call into an 8 bit
813 pc-relative branch/call. */
816 bfd_vma value
= symval
+ irel
->r_addend
;
819 /* Get the address of this instruction. */
820 dot
= (sec
->output_section
->vma
821 + sec
->output_offset
+ irel
->r_offset
- 1);
823 /* Compute the distance from this insn to the branch target. */
826 /* If the distance is within -126..+130 inclusive, then we can
827 relax this jump. +130 is valid since the target will move
828 two bytes closer if we do relax this branch. */
829 if ((int) gap
>= -126 && (int) gap
<= 130)
833 /* Note that we've changed the relocs, section contents,
835 elf_section_data (sec
)->relocs
= internal_relocs
;
836 elf_section_data (sec
)->this_hdr
.contents
= contents
;
837 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
839 /* Get the instruction code being relaxed. */
840 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
842 /* If the previous instruction conditionally jumped around
843 this instruction, we may be able to reverse the condition
844 and redirect the previous instruction to the target of
847 Such sequences are used by the compiler to deal with
848 long conditional branches.
850 Only perform this optimisation for jumps (code 0x5a) not
851 subroutine calls, as otherwise it could transform:
864 which changes the call (jsr) into a branch (bne). */
869 && ELF32_R_TYPE (last_reloc
->r_info
) == R_H8_PCREL8
870 && ELF32_R_SYM (last_reloc
->r_info
) < symtab_hdr
->sh_info
)
873 asection
*last_sym_sec
;
874 Elf_Internal_Sym
*last_sym
;
876 /* We will need to examine the symbol used by the
877 previous relocation. */
879 last_sym
= isymbuf
+ ELF32_R_SYM (last_reloc
->r_info
);
881 = bfd_section_from_elf_index (abfd
, last_sym
->st_shndx
);
882 last_value
= (last_sym
->st_value
883 + last_sym_sec
->output_section
->vma
884 + last_sym_sec
->output_offset
);
886 /* Verify that the previous relocation was for a
887 branch around this instruction and that no symbol
888 exists at the current location. */
889 if (last_value
== dot
+ 4
890 && last_reloc
->r_offset
+ 2 == irel
->r_offset
891 && ! elf32_h8_symbol_address_p (abfd
, sec
, dot
))
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
;
904 code
= bfd_get_8 (abfd
,
905 contents
+ last_reloc
->r_offset
- 1);
909 contents
+ last_reloc
->r_offset
- 1);
911 /* Delete four bytes of data. */
912 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
923 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 1);
924 else if (code
== 0x5a)
925 bfd_put_8 (abfd
, 0x40, contents
+ irel
->r_offset
- 1);
929 /* Fix the relocation's type. */
930 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
933 /* Delete two bytes of data. */
934 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
935 irel
->r_offset
+ 1, 2))
938 /* That will change things, so, we should relax again.
939 Note that this is not required, and it may be slow. */
945 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
949 bfd_vma value
= symval
+ irel
->r_addend
;
953 /* Get the address of this instruction. */
954 dot
= (sec
->output_section
->vma
956 + irel
->r_offset
- 2);
960 /* If the distance is within -126..+130 inclusive, then we can
961 relax this jump. +130 is valid since the target will move
962 two bytes closer if we do relax this branch. */
963 if ((int) gap
>= -126 && (int) gap
<= 130)
967 /* Note that we've changed the relocs, section contents,
969 elf_section_data (sec
)->relocs
= internal_relocs
;
970 elf_section_data (sec
)->this_hdr
.contents
= contents
;
971 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
973 /* Get the opcode. */
974 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
978 /* bCC:16 -> bCC:8 */
979 /* Get the condition code from the original insn. */
980 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
984 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
986 else if (code
== 0x5c)
987 bfd_put_8 (abfd
, 0x55, contents
+ irel
->r_offset
- 2);
991 /* Fix the relocation's type. */
992 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
996 /* Delete two bytes of data. */
997 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
998 irel
->r_offset
+ 1, 2))
1001 /* That will change things, so, we should relax again.
1002 Note that this is not required, and it may be slow. */
1008 /* This is a 16 bit absolute address in a "mov.b" insn, which may
1009 become an 8 bit absolute address if its in the right range. */
1014 value
= bfd_h8300_pad_address (abfd
, symval
+ irel
->r_addend
);
1015 if (value
>= 0xffffff00u
)
1019 /* Note that we've changed the relocs, section contents,
1021 elf_section_data (sec
)->relocs
= internal_relocs
;
1022 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1023 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1025 /* Get the opcode. */
1026 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1032 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1034 if ((code
& 0xf0) == 0x00)
1036 (code
& 0xf) | 0x20,
1037 contents
+ irel
->r_offset
- 2);
1038 else if ((code
& 0xf0) == 0x80)
1040 (code
& 0xf) | 0x30,
1041 contents
+ irel
->r_offset
- 2);
1045 /* Fix the relocation's type. */
1046 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1049 /* Move the relocation. */
1052 /* Delete two bytes of data. */
1053 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1054 irel
->r_offset
+ 1, 2))
1057 /* That will change things, so, we should relax again.
1058 Note that this is not required, and it may be slow. */
1064 /* This is a 24 bit absolute address in a "mov.b" insn, which may
1065 become an 8 bit absolute address if its in the right range. */
1070 value
= bfd_h8300_pad_address (abfd
, symval
+ irel
->r_addend
);
1071 if (value
>= 0xffffff00u
)
1075 /* Note that we've changed the relocs, section contents,
1077 elf_section_data (sec
)->relocs
= internal_relocs
;
1078 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1079 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1081 /* Get the opcode. */
1082 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1088 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1090 switch (code
& 0xf0)
1093 bfd_put_8 (abfd
, (code
& 0xf) | 0x20,
1094 contents
+ irel
->r_offset
- 2);
1097 bfd_put_8 (abfd
, (code
& 0xf) | 0x30,
1098 contents
+ irel
->r_offset
- 2);
1104 /* Fix the relocation's type. */
1105 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1109 /* Delete two bytes of data. */
1110 if (!elf32_h8_relax_delete_bytes (abfd
, sec
,
1111 irel
->r_offset
+ 1, 4))
1114 /* That will change things, so, we should relax again.
1115 Note that this is not required, and it may be slow. */
1123 /* This is a 24/32bit absolute address in a "mov" insn, which may
1124 become a 16-bit absolute address if it is in the right range. */
1129 value
= bfd_h8300_pad_address (abfd
, symval
+ irel
->r_addend
);
1130 if (value
<= 0x7fff || value
>= 0xffff8000u
)
1134 /* Note that we've changed the relocs, section contents,
1136 elf_section_data (sec
)->relocs
= internal_relocs
;
1137 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1138 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1140 /* Get the opcode. */
1141 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1143 /* We just need to turn off bit 0x20. */
1146 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
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
,
1154 irel
->r_offset
+ 1, 2))
1157 /* That will change things, so, we should relax again.
1158 Note that this is not required, and it may be slow. */
1170 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1172 if (! link_info
->keep_memory
)
1175 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1178 if (contents
!= NULL
1179 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1181 if (! link_info
->keep_memory
)
1185 /* Cache the section contents for elf_link_input_bfd. */
1186 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1190 if (internal_relocs
!= NULL
1191 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1192 free (internal_relocs
);
1198 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1200 if (contents
!= NULL
1201 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1203 if (internal_relocs
!= NULL
1204 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1205 free (internal_relocs
);
1209 /* Delete some bytes from a section while relaxing. */
1212 elf32_h8_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, int count
)
1214 Elf_Internal_Shdr
*symtab_hdr
;
1215 unsigned int sec_shndx
;
1217 Elf_Internal_Rela
*irel
, *irelend
;
1218 Elf_Internal_Rela
*irelalign
;
1219 Elf_Internal_Sym
*isym
;
1220 Elf_Internal_Sym
*isymend
;
1222 struct elf_link_hash_entry
**sym_hashes
;
1223 struct elf_link_hash_entry
**end_hashes
;
1224 unsigned int symcount
;
1226 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1228 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1230 /* The deletion must stop at the next ALIGN reloc for an aligment
1231 power larger than the number of bytes we are deleting. */
1234 toaddr
= sec
->_cooked_size
;
1236 irel
= elf_section_data (sec
)->relocs
;
1237 irelend
= irel
+ sec
->reloc_count
;
1239 /* Actually delete the bytes. */
1240 memmove (contents
+ addr
, contents
+ addr
+ count
,
1241 (size_t) (toaddr
- addr
- count
));
1242 sec
->_cooked_size
-= count
;
1244 /* Adjust all the relocs. */
1245 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1247 /* Get the new reloc address. */
1248 if ((irel
->r_offset
> addr
1249 && irel
->r_offset
< toaddr
))
1250 irel
->r_offset
-= count
;
1253 /* Adjust the local symbols defined in this section. */
1254 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1255 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1256 isymend
= isym
+ symtab_hdr
->sh_info
;
1257 for (; isym
< isymend
; isym
++)
1259 if (isym
->st_shndx
== sec_shndx
1260 && isym
->st_value
> addr
1261 && isym
->st_value
< toaddr
)
1262 isym
->st_value
-= count
;
1265 /* Now adjust the global symbols defined in this section. */
1266 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1267 - symtab_hdr
->sh_info
);
1268 sym_hashes
= elf_sym_hashes (abfd
);
1269 end_hashes
= sym_hashes
+ symcount
;
1270 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1272 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1273 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1274 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1275 && sym_hash
->root
.u
.def
.section
== sec
1276 && sym_hash
->root
.u
.def
.value
> addr
1277 && sym_hash
->root
.u
.def
.value
< toaddr
)
1279 sym_hash
->root
.u
.def
.value
-= count
;
1286 /* Return TRUE if a symbol exists at the given address, else return
1289 elf32_h8_symbol_address_p (bfd
*abfd
, asection
*sec
, bfd_vma addr
)
1291 Elf_Internal_Shdr
*symtab_hdr
;
1292 unsigned int sec_shndx
;
1293 Elf_Internal_Sym
*isym
;
1294 Elf_Internal_Sym
*isymend
;
1295 struct elf_link_hash_entry
**sym_hashes
;
1296 struct elf_link_hash_entry
**end_hashes
;
1297 unsigned int symcount
;
1299 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1301 /* Examine all the symbols. */
1302 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1303 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1304 isymend
= isym
+ symtab_hdr
->sh_info
;
1305 for (; isym
< isymend
; isym
++)
1307 if (isym
->st_shndx
== sec_shndx
1308 && isym
->st_value
== addr
)
1312 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1313 - symtab_hdr
->sh_info
);
1314 sym_hashes
= elf_sym_hashes (abfd
);
1315 end_hashes
= sym_hashes
+ symcount
;
1316 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1318 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1319 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1320 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1321 && sym_hash
->root
.u
.def
.section
== sec
1322 && sym_hash
->root
.u
.def
.value
== addr
)
1329 /* This is a version of bfd_generic_get_relocated_section_contents
1330 which uses elf32_h8_relocate_section. */
1333 elf32_h8_get_relocated_section_contents (bfd
*output_bfd
,
1334 struct bfd_link_info
*link_info
,
1335 struct bfd_link_order
*link_order
,
1337 bfd_boolean relocatable
,
1340 Elf_Internal_Shdr
*symtab_hdr
;
1341 asection
*input_section
= link_order
->u
.indirect
.section
;
1342 bfd
*input_bfd
= input_section
->owner
;
1343 asection
**sections
= NULL
;
1344 Elf_Internal_Rela
*internal_relocs
= NULL
;
1345 Elf_Internal_Sym
*isymbuf
= NULL
;
1347 /* We only need to handle the case of relaxing, or of having a
1348 particular set of section contents, specially. */
1350 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1351 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1356 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1358 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1359 (size_t) input_section
->_raw_size
);
1361 if ((input_section
->flags
& SEC_RELOC
) != 0
1362 && input_section
->reloc_count
> 0)
1365 Elf_Internal_Sym
*isym
, *isymend
;
1368 internal_relocs
= (_bfd_elf_link_read_relocs
1369 (input_bfd
, input_section
, (PTR
) NULL
,
1370 (Elf_Internal_Rela
*) NULL
, FALSE
));
1371 if (internal_relocs
== NULL
)
1374 if (symtab_hdr
->sh_info
!= 0)
1376 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1377 if (isymbuf
== NULL
)
1378 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1379 symtab_hdr
->sh_info
, 0,
1381 if (isymbuf
== NULL
)
1385 amt
= symtab_hdr
->sh_info
;
1386 amt
*= sizeof (asection
*);
1387 sections
= (asection
**) bfd_malloc (amt
);
1388 if (sections
== NULL
&& amt
!= 0)
1391 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1392 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1396 if (isym
->st_shndx
== SHN_UNDEF
)
1397 isec
= bfd_und_section_ptr
;
1398 else if (isym
->st_shndx
== SHN_ABS
)
1399 isec
= bfd_abs_section_ptr
;
1400 else if (isym
->st_shndx
== SHN_COMMON
)
1401 isec
= bfd_com_section_ptr
;
1403 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1408 if (! elf32_h8_relocate_section (output_bfd
, link_info
, input_bfd
,
1409 input_section
, data
, internal_relocs
,
1413 if (sections
!= NULL
)
1416 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1418 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1419 free (internal_relocs
);
1425 if (sections
!= NULL
)
1428 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1430 if (internal_relocs
!= NULL
1431 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1432 free (internal_relocs
);
1437 elf32_h8_gc_mark_hook (asection
*sec
,
1438 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1439 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
1440 struct elf_link_hash_entry
*h
,
1441 Elf_Internal_Sym
*sym
)
1445 switch (h
->root
.type
)
1447 case bfd_link_hash_defined
:
1448 case bfd_link_hash_defweak
:
1449 return h
->root
.u
.def
.section
;
1451 case bfd_link_hash_common
:
1452 return h
->root
.u
.c
.p
->section
;
1459 return bfd_section_from_elf_index(sec
->owner
, sym
->st_shndx
);
1464 elf32_h8_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
1465 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1466 asection
*sec ATTRIBUTE_UNUSED
,
1467 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
1473 #define TARGET_BIG_SYM bfd_elf32_h8300_vec
1474 #define TARGET_BIG_NAME "elf32-h8300"
1475 #define ELF_ARCH bfd_arch_h8300
1476 #define ELF_MACHINE_CODE EM_H8_300
1477 #define ELF_MAXPAGESIZE 0x1
1478 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1479 #define elf_info_to_howto elf32_h8_info_to_howto
1480 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1482 /* So we can set/examine bits in e_flags to get the specific
1483 H8 architecture in use. */
1484 #define elf_backend_final_write_processing \
1485 elf32_h8_final_write_processing
1486 #define elf_backend_object_p \
1488 #define bfd_elf32_bfd_merge_private_bfd_data \
1489 elf32_h8_merge_private_bfd_data
1490 #define elf_backend_gc_mark_hook elf32_h8_gc_mark_hook
1491 #define elf_backend_gc_sweep_hook elf32_h8_gc_sweep_hook
1493 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1494 defaults to using _bfd_generic_link_hash_table_create, but
1495 elflink.h:bfd_elf32_size_dynamic_sections uses
1496 dynobj = elf_hash_table (info)->dynobj;
1497 and thus requires an elf hash table. */
1498 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1500 /* Use an H8 specific linker, not the ELF generic linker. */
1501 #define elf_backend_relocate_section elf32_h8_relocate_section
1502 #define elf_backend_rela_normal 1
1503 #define elf_backend_can_gc_sections 1
1505 /* And relaxing stuff. */
1506 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1507 #define bfd_elf32_bfd_get_relocated_section_contents \
1508 elf32_h8_get_relocated_section_contents
1511 #include "elf32-target.h"