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 (flagword);
34 static void elf32_h8_final_write_processing (bfd *, bfd_boolean);
35 static bfd_boolean elf32_h8_object_p (bfd *);
36 static bfd_boolean elf32_h8_merge_private_bfd_data (bfd *, bfd *);
37 static bfd_boolean elf32_h8_relax_section
38 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
39 static bfd_boolean elf32_h8_relax_delete_bytes
40 (bfd *, asection *, bfd_vma, int);
41 static bfd_boolean elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma);
42 static bfd_byte *elf32_h8_get_relocated_section_contents
43 (bfd *, struct bfd_link_info *, struct bfd_link_order *,
44 bfd_byte *, bfd_boolean, asymbol **);
45 static asection *elf32_h8_gc_mark_hook
46 (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
47 struct elf_link_hash_entry *, Elf_Internal_Sym *);
48 static bfd_boolean elf32_h8_gc_sweep_hook
49 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
50 static bfd_reloc_status_type elf32_h8_final_link_relocate
51 (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 (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 (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_DIR32A16", /* 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 H8 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 (bfd *abfd ATTRIBUTE_UNUSED,
247 bfd_reloc_code_real_type code)
251 for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++)
253 if (h8_reloc_map[i].bfd_reloc_val == code)
254 return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index];
260 elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
261 Elf_Internal_Rela *elf_reloc)
266 r = ELF32_R_TYPE (elf_reloc->r_info);
267 for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++)
268 if (h8_elf_howto_table[i].type == r)
270 bfd_reloc->howto = &h8_elf_howto_table[i];
277 elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
278 Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED)
283 r = ELF32_R_TYPE (elf_reloc->r_info);
284 bfd_reloc->howto = &h8_elf_howto_table[r];
287 /* Special handling for H8/300 relocs.
288 We only come here for pcrel stuff and return normally if not an -r link.
289 When doing -r, we can't do any arithmetic for the pcrel stuff, because
290 we support relaxing on the H8/300 series chips. */
291 static bfd_reloc_status_type
292 special (bfd *abfd ATTRIBUTE_UNUSED,
293 arelent *reloc_entry ATTRIBUTE_UNUSED,
294 asymbol *symbol ATTRIBUTE_UNUSED,
295 PTR data ATTRIBUTE_UNUSED,
296 asection *input_section ATTRIBUTE_UNUSED,
298 char **error_message ATTRIBUTE_UNUSED)
300 if (output_bfd == (bfd *) NULL)
301 return bfd_reloc_continue;
303 /* Adjust the reloc address to that in the output section. */
304 reloc_entry->address += input_section->output_offset;
308 /* Perform a relocation as part of a final link. */
309 static bfd_reloc_status_type
310 elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd,
311 bfd *output_bfd ATTRIBUTE_UNUSED,
312 asection *input_section ATTRIBUTE_UNUSED,
313 bfd_byte *contents, bfd_vma offset,
314 bfd_vma value, bfd_vma addend,
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;
330 bfd_put_32 (input_bfd, value, hit_data);
337 bfd_put_16 (input_bfd, value, hit_data);
344 bfd_put_8 (input_bfd, value, hit_data);
350 /* HIT_DATA is the address for the first byte for the relocated
351 value. Subtract 1 so that we can manipulate the data in 32-bit
355 /* Clear out the top byte in value. */
358 /* Retrieve the type byte for value from the section contents. */
359 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
361 /* Now scribble it out in one 32-bit hunk. */
362 bfd_put_32 (input_bfd, value, hit_data);
366 value -= (input_section->output_section->vma
367 + input_section->output_offset);
371 /* The value is relative to the start of the instruction,
372 not the relocation offset. Subtract 2 to account for
376 bfd_put_16 (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 1 to account for
390 bfd_put_8 (input_bfd, value, hit_data);
394 return bfd_reloc_notsupported;
398 /* Relocate an H8 ELF section. */
400 elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
401 bfd *input_bfd, asection *input_section,
402 bfd_byte *contents, Elf_Internal_Rela *relocs,
403 Elf_Internal_Sym *local_syms,
404 asection **local_sections)
406 Elf_Internal_Shdr *symtab_hdr;
407 struct elf_link_hash_entry **sym_hashes;
408 Elf_Internal_Rela *rel, *relend;
410 if (info->relocatable)
413 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
414 sym_hashes = elf_sym_hashes (input_bfd);
417 relend = relocs + input_section->reloc_count;
418 for (; rel < relend; rel++)
421 unsigned long r_symndx;
422 Elf_Internal_Sym *sym;
424 struct elf_link_hash_entry *h;
426 bfd_reloc_status_type r;
428 /* This is a final link. */
429 r_symndx = ELF32_R_SYM (rel->r_info);
430 r_type = ELF32_R_TYPE (rel->r_info);
434 if (r_symndx < symtab_hdr->sh_info)
436 sym = local_syms + r_symndx;
437 sec = local_sections[r_symndx];
438 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
442 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
443 while (h->root.type == bfd_link_hash_indirect
444 || h->root.type == bfd_link_hash_warning)
445 h = (struct elf_link_hash_entry *) h->root.u.i.link;
446 if (h->root.type == bfd_link_hash_defined
447 || h->root.type == bfd_link_hash_defweak)
449 sec = h->root.u.def.section;
450 relocation = (h->root.u.def.value
451 + sec->output_section->vma
452 + sec->output_offset);
454 else if (h->root.type == bfd_link_hash_undefweak)
458 if (! ((*info->callbacks->undefined_symbol)
459 (info, h->root.root.string, input_bfd,
460 input_section, rel->r_offset, TRUE)))
466 r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
468 contents, rel->r_offset,
469 relocation, rel->r_addend,
470 info, sec, h == NULL);
472 if (r != bfd_reloc_ok)
475 const char *msg = (const char *) 0;
477 reloc_howto_type *howto;
479 elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel);
480 howto = bfd_reloc.howto;
483 name = h->root.root.string;
486 name = (bfd_elf_string_from_elf_section
487 (input_bfd, symtab_hdr->sh_link, sym->st_name));
488 if (name == NULL || *name == '\0')
489 name = bfd_section_name (input_bfd, sec);
494 case bfd_reloc_overflow:
495 if (! ((*info->callbacks->reloc_overflow)
496 (info, name, howto->name, (bfd_vma) 0,
497 input_bfd, input_section, rel->r_offset)))
501 case bfd_reloc_undefined:
502 if (! ((*info->callbacks->undefined_symbol)
503 (info, name, input_bfd, input_section,
504 rel->r_offset, TRUE)))
508 case bfd_reloc_outofrange:
509 msg = _("internal error: out of range error");
512 case bfd_reloc_notsupported:
513 msg = _("internal error: unsupported relocation error");
516 case bfd_reloc_dangerous:
517 msg = _("internal error: dangerous error");
521 msg = _("internal error: unknown error");
525 if (!((*info->callbacks->warning)
526 (info, msg, name, input_bfd, input_section,
537 /* Object files encode the specific H8 model they were compiled
538 for in the ELF flags field.
540 Examine that field and return the proper BFD machine type for
543 elf32_h8_mach (flagword flags)
545 switch (flags & EF_H8_MACH)
547 case E_H8_MACH_H8300:
549 return bfd_mach_h8300;
551 case E_H8_MACH_H8300H:
552 return bfd_mach_h8300h;
554 case E_H8_MACH_H8300S:
555 return bfd_mach_h8300s;
557 case E_H8_MACH_H8300HN:
558 return bfd_mach_h8300hn;
560 case E_H8_MACH_H8300SN:
561 return bfd_mach_h8300sn;
563 case E_H8_MACH_H8300SX:
564 return bfd_mach_h8300sx;
566 case E_H8_MACH_H8300SXN:
567 return bfd_mach_h8300sxn;
571 /* The final processing done just before writing out a H8 ELF object
572 file. We use this opportunity to encode the BFD machine type
573 into the flags field in the object file. */
576 elf32_h8_final_write_processing (bfd *abfd,
577 bfd_boolean linker ATTRIBUTE_UNUSED)
581 switch (bfd_get_mach (abfd))
585 val = E_H8_MACH_H8300;
588 case bfd_mach_h8300h:
589 val = E_H8_MACH_H8300H;
592 case bfd_mach_h8300s:
593 val = E_H8_MACH_H8300S;
596 case bfd_mach_h8300hn:
597 val = E_H8_MACH_H8300HN;
600 case bfd_mach_h8300sn:
601 val = E_H8_MACH_H8300SN;
604 case bfd_mach_h8300sx:
605 val = E_H8_MACH_H8300SX;
608 case bfd_mach_h8300sxn:
609 val = E_H8_MACH_H8300SXN;
613 elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
614 elf_elfheader (abfd)->e_flags |= val;
617 /* Return nonzero if ABFD represents a valid H8 ELF object file; also
618 record the encoded machine type found in the ELF flags. */
621 elf32_h8_object_p (bfd *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 (bfd *ibfd, bfd *obfd)
635 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
636 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
639 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
640 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
642 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
643 bfd_get_mach (ibfd)))
650 /* This function handles relaxing for the H8..
652 There are a few relaxing opportunities available on the H8:
654 jmp/jsr:24 -> bra/bsr:8 2 bytes
655 The jmp may be completely eliminated if the previous insn is a
656 conditional branch to the insn after the jump. In that case
657 we invert the branch and delete the jump and save 4 bytes.
659 bCC:16 -> bCC:8 2 bytes
660 bsr:16 -> bsr:8 2 bytes
662 bset:16 -> bset:8 2 bytes
663 bset:24/32 -> bset:8 4 bytes
664 (also applicable to other bit manipulation instructions)
666 mov.b:16 -> mov.b:8 2 bytes
667 mov.b:24/32 -> mov.b:8 4 bytes
669 bset:24/32 -> bset:16 2 bytes
670 (also applicable to other bit manipulation instructions)
672 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes */
675 elf32_h8_relax_section (bfd *abfd, asection *sec,
676 struct bfd_link_info *link_info, bfd_boolean *again)
678 Elf_Internal_Shdr *symtab_hdr;
679 Elf_Internal_Rela *internal_relocs;
680 Elf_Internal_Rela *irel, *irelend;
681 bfd_byte *contents = NULL;
682 Elf_Internal_Sym *isymbuf = NULL;
683 static asection *last_input_section = NULL;
684 static Elf_Internal_Rela *last_reloc = NULL;
686 /* Assume nothing changes. */
689 /* We don't have to do anything for a relocatable link, if
690 this section does not have relocs, or if this is not a
692 if (link_info->relocatable
693 || (sec->flags & SEC_RELOC) == 0
694 || sec->reloc_count == 0
695 || (sec->flags & SEC_CODE) == 0)
698 /* If this is the first time we have been called for this section,
699 initialize the cooked size. */
700 if (sec->_cooked_size == 0)
701 sec->_cooked_size = sec->_raw_size;
703 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
705 /* Get a copy of the native relocations. */
706 internal_relocs = (_bfd_elf_link_read_relocs
707 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
708 link_info->keep_memory));
709 if (internal_relocs == NULL)
712 if (sec != last_input_section)
715 last_input_section = sec;
717 /* Walk through the relocs looking for relaxing opportunities. */
718 irelend = internal_relocs + sec->reloc_count;
719 for (irel = internal_relocs; irel < irelend; irel++)
723 /* Keep track of the previous reloc so that we can delete
724 some long jumps created by the compiler. */
725 if (irel != internal_relocs)
726 last_reloc = irel - 1;
728 if (ELF32_R_TYPE (irel->r_info) != R_H8_DIR24R8
729 && ELF32_R_TYPE (irel->r_info) != R_H8_PCREL16
730 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR16A8
731 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR24A8
732 && ELF32_R_TYPE (irel->r_info) != R_H8_DIR32A16)
735 /* Get the section contents if we haven't done so already. */
736 if (contents == NULL)
738 /* Get cached copy if it exists. */
739 if (elf_section_data (sec)->this_hdr.contents != NULL)
740 contents = elf_section_data (sec)->this_hdr.contents;
743 /* Go get them off disk. */
744 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
745 if (contents == NULL)
748 if (! bfd_get_section_contents (abfd, sec, contents,
749 (file_ptr) 0, sec->_raw_size))
754 /* Read this BFD's local symbols if we haven't done so already. */
755 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
757 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
759 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
760 symtab_hdr->sh_info, 0,
766 /* Get the value of the symbol referred to by the reloc. */
767 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
769 /* A local symbol. */
770 Elf_Internal_Sym *isym;
773 isym = isymbuf + ELF32_R_SYM (irel->r_info);
774 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
775 symval = isym->st_value;
776 /* If the reloc is absolute, it will not have
777 a symbol or section associated with it. */
779 symval += sym_sec->output_section->vma
780 + sym_sec->output_offset;
785 struct elf_link_hash_entry *h;
787 /* An external symbol. */
788 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
789 h = elf_sym_hashes (abfd)[indx];
790 BFD_ASSERT (h != NULL);
791 if (h->root.type != bfd_link_hash_defined
792 && h->root.type != bfd_link_hash_defweak)
794 /* This appears to be a reference to an undefined
795 symbol. Just ignore it--it will be caught by the
796 regular reloc processing. */
800 symval = (h->root.u.def.value
801 + h->root.u.def.section->output_section->vma
802 + h->root.u.def.section->output_offset);
805 /* For simplicity of coding, we are going to modify the section
806 contents, the section relocs, and the BFD symbol table. We
807 must tell the rest of the code not to free up this
808 information. It would be possible to instead create a table
809 of changes which have to be made, as is done in coff-mips.c;
810 that would be more work, but would require less memory when
811 the linker is run. */
812 switch (ELF32_R_TYPE (irel->r_info))
814 /* Try to turn a 24-bit absolute branch/call into an 8-bit
815 pc-relative branch/call. */
818 bfd_vma value = symval + irel->r_addend;
821 /* Get the address of this instruction. */
822 dot = (sec->output_section->vma
823 + sec->output_offset + irel->r_offset - 1);
825 /* Compute the distance from this insn to the branch target. */
828 /* If the distance is within -126..+130 inclusive, then we can
829 relax this jump. +130 is valid since the target will move
830 two bytes closer if we do relax this branch. */
831 if ((int) gap >= -126 && (int) gap <= 130)
835 /* Note that we've changed the relocs, section contents,
837 elf_section_data (sec)->relocs = internal_relocs;
838 elf_section_data (sec)->this_hdr.contents = contents;
839 symtab_hdr->contents = (unsigned char *) isymbuf;
841 /* Get the instruction code being relaxed. */
842 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
844 /* If the previous instruction conditionally jumped around
845 this instruction, we may be able to reverse the condition
846 and redirect the previous instruction to the target of
849 Such sequences are used by the compiler to deal with
850 long conditional branches.
852 Only perform this optimisation for jumps (code 0x5a) not
853 subroutine calls, as otherwise it could transform:
866 which changes the call (jsr) into a branch (bne). */
871 && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
872 && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
875 asection *last_sym_sec;
876 Elf_Internal_Sym *last_sym;
878 /* We will need to examine the symbol used by the
879 previous relocation. */
881 last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info);
883 = bfd_section_from_elf_index (abfd, last_sym->st_shndx);
884 last_value = (last_sym->st_value
885 + last_sym_sec->output_section->vma
886 + last_sym_sec->output_offset);
888 /* Verify that the previous relocation was for a
889 branch around this instruction and that no symbol
890 exists at the current location. */
891 if (last_value == dot + 4
892 && last_reloc->r_offset + 2 == irel->r_offset
893 && ! elf32_h8_symbol_address_p (abfd, sec, dot))
895 /* We can eliminate this jump. Twiddle the
896 previous relocation as necessary. */
898 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
899 ELF32_R_TYPE (R_H8_NONE));
902 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
903 ELF32_R_TYPE (R_H8_PCREL8));
904 last_reloc->r_addend = irel->r_addend;
906 code = bfd_get_8 (abfd,
907 contents + last_reloc->r_offset - 1);
911 contents + last_reloc->r_offset - 1);
913 /* Delete four bytes of data. */
914 if (!elf32_h8_relax_delete_bytes (abfd, sec,
926 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1);
927 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 16-bit pc-relative branch into a 8-bit 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;
974 elf_section_data (sec)->this_hdr.contents = contents;
975 symtab_hdr->contents = (unsigned char *) isymbuf;
977 /* Get the opcode. */
978 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
982 /* bCC:16 -> bCC:8 */
983 /* Get the second byte of the original insn, which
984 contains the condition code. */
985 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
987 /* Compute the fisrt byte of the relaxed
988 instruction. The original sequence 0x58 0xX0
989 is relaxed to 0x4X, where X represents the
994 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
996 else if (code == 0x5c)
998 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2);
1002 /* Fix the relocation's type. */
1003 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1007 /* Delete two bytes of data. */
1008 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1009 irel->r_offset + 1, 2))
1012 /* That will change things, so, we should relax again.
1013 Note that this is not required, and it may be slow. */
1019 /* This is a 16-bit absolute address in one of the following
1022 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
1023 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
1026 We may relax this into an 8-bit absolute address if it's in
1032 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1033 if (value >= 0xffffff00u)
1036 unsigned char temp_code;
1038 /* Note that we've changed the relocs, section contents,
1040 elf_section_data (sec)->relocs = internal_relocs;
1041 elf_section_data (sec)->this_hdr.contents = contents;
1042 symtab_hdr->contents = (unsigned char *) isymbuf;
1044 /* Get the opcode. */
1045 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1047 /* All instructions with R_H8_DIR16A8 start with
1052 temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1053 /* If this is a mov.b instruction, clear the lower
1054 nibble, which contains the source/destination
1056 if ((temp_code & 0x10) != 0x10)
1062 /* This is mov.b @aa:16,Rd. */
1063 bfd_put_8 (abfd, (code & 0xf) | 0x20,
1064 contents + irel->r_offset - 2);
1067 /* This is mov.b Rs,@aa:16. */
1068 bfd_put_8 (abfd, (code & 0xf) | 0x30,
1069 contents + irel->r_offset - 2);
1072 /* This is a bit-maniputation instruction that
1073 stores one bit into memory, one of "bclr",
1074 "bist", "bnot", "bset", and "bst". */
1075 bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
1078 /* This is a bit-maniputation instruction that
1079 loads one bit from memory, one of "band",
1080 "biand", "bild", "bior", "bixor", "bld", "bor",
1081 "btst", and "bxor". */
1082 bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
1088 /* Fix the relocation's type. */
1089 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1092 /* Move the relocation. */
1095 /* Delete two bytes of data. */
1096 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1097 irel->r_offset + 1, 2))
1100 /* That will change things, so, we should relax again.
1101 Note that this is not required, and it may be slow. */
1107 /* This is a 24-bit absolute address in one of the following
1110 "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
1111 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
1114 We may relax this into an 8-bit absolute address if it's in
1120 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1121 if (value >= 0xffffff00u)
1124 unsigned char temp_code;
1126 /* Note that we've changed the relocs, section contents,
1128 elf_section_data (sec)->relocs = internal_relocs;
1129 elf_section_data (sec)->this_hdr.contents = contents;
1130 symtab_hdr->contents = (unsigned char *) isymbuf;
1132 /* Get the opcode. */
1133 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1135 /* All instructions with R_H8_DIR24A8 start with
1140 temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1142 /* If this is a mov.b instruction, clear the lower
1143 nibble, which contains the source/destination
1145 if ((temp_code & 0x30) != 0x30)
1151 /* This is mov.b @aa:24/32,Rd. */
1152 bfd_put_8 (abfd, (code & 0xf) | 0x20,
1153 contents + irel->r_offset - 2);
1156 /* This is mov.b Rs,@aa:24/32. */
1157 bfd_put_8 (abfd, (code & 0xf) | 0x30,
1158 contents + irel->r_offset - 2);
1161 /* This is a bit-maniputation instruction that
1162 stores one bit into memory, one of "bclr",
1163 "bist", "bnot", "bset", and "bst". */
1164 bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
1167 /* This is a bit-maniputation instruction that
1168 loads one bit from memory, one of "band",
1169 "biand", "bild", "bior", "bixor", "bld", "bor",
1170 "btst", and "bxor". */
1171 bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
1177 /* Fix the relocation's type. */
1178 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1182 /* Delete two bytes of data. */
1183 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1184 irel->r_offset + 1, 4))
1187 /* That will change things, so, we should relax again.
1188 Note that this is not required, and it may be slow. */
1196 /* This is a 24-/32-bit absolute address in one of the
1197 following instructions:
1199 "band", "bclr", "biand", "bild", "bior", "bist",
1200 "bixor", "bld", "bnot", "bor", "bset", "bst", "btst",
1201 "bxor", and "mov.[bwl]"
1203 We may relax this into an 16-bit absolute address if it's
1204 in the right range. */
1209 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1210 if (value <= 0x7fff || value >= 0xffff8000u)
1214 /* Note that we've changed the relocs, section contents,
1216 elf_section_data (sec)->relocs = internal_relocs;
1217 elf_section_data (sec)->this_hdr.contents = contents;
1218 symtab_hdr->contents = (unsigned char *) isymbuf;
1220 /* Get the opcode. */
1221 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1223 /* Fix the opcode. For all the instructions that
1224 belong to this relaxation, we simply need to turn
1225 off bit 0x20 in the previous byte. */
1228 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1230 /* Fix the relocation's type. */
1231 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1234 /* Delete two bytes of data. */
1235 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1236 irel->r_offset + 1, 2))
1239 /* That will change things, so, we should relax again.
1240 Note that this is not required, and it may be slow. */
1252 && symtab_hdr->contents != (unsigned char *) isymbuf)
1254 if (! link_info->keep_memory)
1257 symtab_hdr->contents = (unsigned char *) isymbuf;
1260 if (contents != NULL
1261 && elf_section_data (sec)->this_hdr.contents != contents)
1263 if (! link_info->keep_memory)
1267 /* Cache the section contents for elf_link_input_bfd. */
1268 elf_section_data (sec)->this_hdr.contents = contents;
1272 if (internal_relocs != NULL
1273 && elf_section_data (sec)->relocs != internal_relocs)
1274 free (internal_relocs);
1280 && symtab_hdr->contents != (unsigned char *) isymbuf)
1282 if (contents != NULL
1283 && elf_section_data (sec)->this_hdr.contents != contents)
1285 if (internal_relocs != NULL
1286 && elf_section_data (sec)->relocs != internal_relocs)
1287 free (internal_relocs);
1291 /* Delete some bytes from a section while relaxing. */
1294 elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
1296 Elf_Internal_Shdr *symtab_hdr;
1297 unsigned int sec_shndx;
1299 Elf_Internal_Rela *irel, *irelend;
1300 Elf_Internal_Rela *irelalign;
1301 Elf_Internal_Sym *isym;
1302 Elf_Internal_Sym *isymend;
1304 struct elf_link_hash_entry **sym_hashes;
1305 struct elf_link_hash_entry **end_hashes;
1306 unsigned int symcount;
1308 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1310 contents = elf_section_data (sec)->this_hdr.contents;
1312 /* The deletion must stop at the next ALIGN reloc for an aligment
1313 power larger than the number of bytes we are deleting. */
1316 toaddr = sec->_cooked_size;
1318 irel = elf_section_data (sec)->relocs;
1319 irelend = irel + sec->reloc_count;
1321 /* Actually delete the bytes. */
1322 memmove (contents + addr, contents + addr + count,
1323 (size_t) (toaddr - addr - count));
1324 sec->_cooked_size -= count;
1326 /* Adjust all the relocs. */
1327 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1329 /* Get the new reloc address. */
1330 if ((irel->r_offset > addr
1331 && irel->r_offset < toaddr))
1332 irel->r_offset -= count;
1335 /* Adjust the local symbols defined in this section. */
1336 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1337 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1338 isymend = isym + symtab_hdr->sh_info;
1339 for (; isym < isymend; isym++)
1341 if (isym->st_shndx == sec_shndx
1342 && isym->st_value > addr
1343 && isym->st_value < toaddr)
1344 isym->st_value -= count;
1347 /* Now adjust the global symbols defined in this section. */
1348 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1349 - symtab_hdr->sh_info);
1350 sym_hashes = elf_sym_hashes (abfd);
1351 end_hashes = sym_hashes + symcount;
1352 for (; sym_hashes < end_hashes; sym_hashes++)
1354 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1355 if ((sym_hash->root.type == bfd_link_hash_defined
1356 || sym_hash->root.type == bfd_link_hash_defweak)
1357 && sym_hash->root.u.def.section == sec
1358 && sym_hash->root.u.def.value > addr
1359 && sym_hash->root.u.def.value < toaddr)
1361 sym_hash->root.u.def.value -= count;
1368 /* Return TRUE if a symbol exists at the given address, else return
1371 elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr)
1373 Elf_Internal_Shdr *symtab_hdr;
1374 unsigned int sec_shndx;
1375 Elf_Internal_Sym *isym;
1376 Elf_Internal_Sym *isymend;
1377 struct elf_link_hash_entry **sym_hashes;
1378 struct elf_link_hash_entry **end_hashes;
1379 unsigned int symcount;
1381 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1383 /* Examine all the symbols. */
1384 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1385 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1386 isymend = isym + symtab_hdr->sh_info;
1387 for (; isym < isymend; isym++)
1389 if (isym->st_shndx == sec_shndx
1390 && isym->st_value == addr)
1394 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1395 - symtab_hdr->sh_info);
1396 sym_hashes = elf_sym_hashes (abfd);
1397 end_hashes = sym_hashes + symcount;
1398 for (; sym_hashes < end_hashes; sym_hashes++)
1400 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1401 if ((sym_hash->root.type == bfd_link_hash_defined
1402 || sym_hash->root.type == bfd_link_hash_defweak)
1403 && sym_hash->root.u.def.section == sec
1404 && sym_hash->root.u.def.value == addr)
1411 /* This is a version of bfd_generic_get_relocated_section_contents
1412 which uses elf32_h8_relocate_section. */
1415 elf32_h8_get_relocated_section_contents (bfd *output_bfd,
1416 struct bfd_link_info *link_info,
1417 struct bfd_link_order *link_order,
1419 bfd_boolean relocatable,
1422 Elf_Internal_Shdr *symtab_hdr;
1423 asection *input_section = link_order->u.indirect.section;
1424 bfd *input_bfd = input_section->owner;
1425 asection **sections = NULL;
1426 Elf_Internal_Rela *internal_relocs = NULL;
1427 Elf_Internal_Sym *isymbuf = NULL;
1429 /* We only need to handle the case of relaxing, or of having a
1430 particular set of section contents, specially. */
1432 || elf_section_data (input_section)->this_hdr.contents == NULL)
1433 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1438 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1440 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1441 (size_t) input_section->_raw_size);
1443 if ((input_section->flags & SEC_RELOC) != 0
1444 && input_section->reloc_count > 0)
1447 Elf_Internal_Sym *isym, *isymend;
1450 internal_relocs = (_bfd_elf_link_read_relocs
1451 (input_bfd, input_section, (PTR) NULL,
1452 (Elf_Internal_Rela *) NULL, FALSE));
1453 if (internal_relocs == NULL)
1456 if (symtab_hdr->sh_info != 0)
1458 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1459 if (isymbuf == NULL)
1460 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1461 symtab_hdr->sh_info, 0,
1463 if (isymbuf == NULL)
1467 amt = symtab_hdr->sh_info;
1468 amt *= sizeof (asection *);
1469 sections = (asection **) bfd_malloc (amt);
1470 if (sections == NULL && amt != 0)
1473 isymend = isymbuf + symtab_hdr->sh_info;
1474 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1478 if (isym->st_shndx == SHN_UNDEF)
1479 isec = bfd_und_section_ptr;
1480 else if (isym->st_shndx == SHN_ABS)
1481 isec = bfd_abs_section_ptr;
1482 else if (isym->st_shndx == SHN_COMMON)
1483 isec = bfd_com_section_ptr;
1485 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1490 if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd,
1491 input_section, data, internal_relocs,
1495 if (sections != NULL)
1498 && symtab_hdr->contents != (unsigned char *) isymbuf)
1500 if (elf_section_data (input_section)->relocs != internal_relocs)
1501 free (internal_relocs);
1507 if (sections != NULL)
1510 && symtab_hdr->contents != (unsigned char *) isymbuf)
1512 if (internal_relocs != NULL
1513 && elf_section_data (input_section)->relocs != internal_relocs)
1514 free (internal_relocs);
1519 elf32_h8_gc_mark_hook (asection *sec,
1520 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1521 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED,
1522 struct elf_link_hash_entry *h,
1523 Elf_Internal_Sym *sym)
1527 switch (h->root.type)
1529 case bfd_link_hash_defined:
1530 case bfd_link_hash_defweak:
1531 return h->root.u.def.section;
1533 case bfd_link_hash_common:
1534 return h->root.u.c.p->section;
1541 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1546 elf32_h8_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
1547 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1548 asection *sec ATTRIBUTE_UNUSED,
1549 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
1555 #define TARGET_BIG_SYM bfd_elf32_h8300_vec
1556 #define TARGET_BIG_NAME "elf32-h8300"
1557 #define ELF_ARCH bfd_arch_h8300
1558 #define ELF_MACHINE_CODE EM_H8_300
1559 #define ELF_MAXPAGESIZE 0x1
1560 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1561 #define elf_info_to_howto elf32_h8_info_to_howto
1562 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1564 /* So we can set/examine bits in e_flags to get the specific
1565 H8 architecture in use. */
1566 #define elf_backend_final_write_processing \
1567 elf32_h8_final_write_processing
1568 #define elf_backend_object_p \
1570 #define bfd_elf32_bfd_merge_private_bfd_data \
1571 elf32_h8_merge_private_bfd_data
1572 #define elf_backend_gc_mark_hook elf32_h8_gc_mark_hook
1573 #define elf_backend_gc_sweep_hook elf32_h8_gc_sweep_hook
1575 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1576 defaults to using _bfd_generic_link_hash_table_create, but
1577 elflink.h:bfd_elf32_size_dynamic_sections uses
1578 dynobj = elf_hash_table (info)->dynobj;
1579 and thus requires an elf hash table. */
1580 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1582 /* Use an H8 specific linker, not the ELF generic linker. */
1583 #define elf_backend_relocate_section elf32_h8_relocate_section
1584 #define elf_backend_rela_normal 1
1585 #define elf_backend_can_gc_sections 1
1587 /* And relaxing stuff. */
1588 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1589 #define bfd_elf32_bfd_get_relocated_section_contents \
1590 elf32_h8_get_relocated_section_contents
1593 #include "elf32-target.h"