1 /* Generic support for 32-bit ELF
2 Copyright 1993, 1995, 1998, 1999, 2001, 2002
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 *, Elf32_Internal_Rel *));
33 static unsigned long elf32_h8_mach
35 static void elf32_h8_final_write_processing
36 PARAMS ((bfd *, boolean));
37 static boolean elf32_h8_object_p
39 static boolean elf32_h8_merge_private_bfd_data
40 PARAMS ((bfd *, bfd *));
41 static boolean elf32_h8_relax_section
42 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
43 static boolean elf32_h8_relax_delete_bytes
44 PARAMS ((bfd *, asection *, bfd_vma, int));
45 static 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 *, 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 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[] =
67 HOWTO (R_H8_NONE, /* type */
69 0, /* size (0 = byte, 1 = short, 2 = long) */
71 false, /* pc_relative */
73 complain_overflow_dont, /* complain_on_overflow */
74 special, /* special_function */
75 "R_H8_NONE", /* name */
76 false, /* partial_inplace */
79 false), /* pcrel_offset */
80 #define R_H8_DIR32_X (R_H8_NONE_X + 1)
81 HOWTO (R_H8_DIR32, /* type */
83 2, /* size (0 = byte, 1 = short, 2 = long) */
85 false, /* pc_relative */
87 complain_overflow_dont, /* complain_on_overflow */
88 special, /* special_function */
89 "R_H8_DIR32", /* name */
90 false, /* partial_inplace */
92 0xffffffff, /* dst_mask */
93 false), /* pcrel_offset */
94 #define R_H8_DIR16_X (R_H8_DIR32_X + 1)
95 HOWTO (R_H8_DIR16, /* type */
97 1, /* size (0 = byte, 1 = short, 2 = long) */
99 false, /* pc_relative */
101 complain_overflow_dont, /* complain_on_overflow */
102 special, /* special_function */
103 "R_H8_DIR16", /* name */
104 false, /* partial_inplace */
106 0x0000ffff, /* dst_mask */
107 false), /* pcrel_offset */
108 #define R_H8_DIR8_X (R_H8_DIR16_X + 1)
109 HOWTO (R_H8_DIR8, /* type */
111 0, /* size (0 = byte, 1 = short, 2 = long) */
113 false, /* pc_relative */
115 complain_overflow_dont, /* complain_on_overflow */
116 special, /* special_function */
117 "R_H8_DIR16", /* name */
118 false, /* partial_inplace */
120 0x000000ff, /* dst_mask */
121 false), /* pcrel_offset */
122 #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
123 HOWTO (R_H8_DIR16A8, /* type */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
127 false, /* pc_relative */
129 complain_overflow_bitfield, /* complain_on_overflow */
130 special, /* special_function */
131 "R_H8_DIR16A8", /* name */
132 false, /* partial_inplace */
134 0x0000ffff, /* dst_mask */
135 false), /* pcrel_offset */
136 #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
137 HOWTO (R_H8_DIR16R8, /* type */
139 1, /* size (0 = byte, 1 = short, 2 = long) */
141 false, /* pc_relative */
143 complain_overflow_bitfield, /* complain_on_overflow */
144 special, /* special_function */
145 "R_H8_DIR16R8", /* name */
146 false, /* partial_inplace */
148 0x0000ffff, /* dst_mask */
149 false), /* pcrel_offset */
150 #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
151 HOWTO (R_H8_DIR24A8, /* type */
153 2, /* size (0 = byte, 1 = short, 2 = long) */
155 false, /* pc_relative */
157 complain_overflow_bitfield, /* complain_on_overflow */
158 special, /* special_function */
159 "R_H8_DIR24A8", /* name */
160 true, /* partial_inplace */
161 0xff000000, /* src_mask */
162 0x00ffffff, /* dst_mask */
163 false), /* pcrel_offset */
164 #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
165 HOWTO (R_H8_DIR24R8, /* type */
167 2, /* size (0 = byte, 1 = short, 2 = long) */
169 false, /* pc_relative */
171 complain_overflow_bitfield, /* complain_on_overflow */
172 special, /* special_function */
173 "R_H8_DIR24R8", /* name */
174 true, /* partial_inplace */
175 0xff000000, /* src_mask */
176 0x00ffffff, /* dst_mask */
177 false), /* pcrel_offset */
178 #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
179 HOWTO (R_H8_DIR32A16, /* type */
181 2, /* size (0 = byte, 1 = short, 2 = long) */
183 false, /* pc_relative */
185 complain_overflow_dont, /* complain_on_overflow */
186 special, /* special_function */
187 "R_H8_DIR32", /* name */
188 false, /* partial_inplace */
190 0xffffffff, /* dst_mask */
191 false), /* pcrel_offset */
192 #define R_H8_PCREL16_X (R_H8_DIR32A16_X + 1)
193 HOWTO (R_H8_PCREL16, /* type */
195 1, /* size (0 = byte, 1 = short, 2 = long) */
197 true, /* pc_relative */
199 complain_overflow_signed, /* complain_on_overflow */
200 special, /* special_function */
201 "R_H8_PCREL16", /* name */
202 false, /* partial_inplace */
203 0xffff, /* src_mask */
204 0xffff, /* dst_mask */
205 true), /* pcrel_offset */
206 #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
207 HOWTO (R_H8_PCREL8, /* type */
209 0, /* size (0 = byte, 1 = short, 2 = long) */
211 true, /* pc_relative */
213 complain_overflow_signed, /* complain_on_overflow */
214 special, /* special_function */
215 "R_H8_PCREL8", /* name */
216 false, /* partial_inplace */
219 true), /* pcrel_offset */
222 /* This structure is used to map BFD reloc codes to H8 ELF relocs. */
226 bfd_reloc_code_real_type bfd_reloc_val;
227 unsigned char howto_index;
230 /* An array mapping BFD reloc codes to SH ELF relocs. */
232 static const struct elf_reloc_map h8_reloc_map[] =
234 { BFD_RELOC_NONE, R_H8_NONE_X },
235 { BFD_RELOC_32, R_H8_DIR32_X },
236 { BFD_RELOC_16, R_H8_DIR16_X },
237 { BFD_RELOC_8, R_H8_DIR8_X },
238 { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X },
239 { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X },
240 { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X },
241 { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X },
242 { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X },
243 { BFD_RELOC_16_PCREL, R_H8_PCREL16_X },
244 { BFD_RELOC_8_PCREL, R_H8_PCREL8_X },
248 static reloc_howto_type *
249 elf32_h8_reloc_type_lookup (abfd, code)
250 bfd *abfd ATTRIBUTE_UNUSED;
251 bfd_reloc_code_real_type code;
255 for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++)
257 if (h8_reloc_map[i].bfd_reloc_val == code)
258 return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index];
264 elf32_h8_info_to_howto (abfd, bfd_reloc, elf_reloc)
265 bfd *abfd ATTRIBUTE_UNUSED;
267 Elf32_Internal_Rela *elf_reloc;
272 r = ELF32_R_TYPE (elf_reloc->r_info);
273 for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++)
274 if (h8_elf_howto_table[i].type== r)
276 bfd_reloc->howto = &h8_elf_howto_table[i];
283 elf32_h8_info_to_howto_rel (abfd, bfd_reloc, elf_reloc)
284 bfd *abfd ATTRIBUTE_UNUSED;
286 Elf32_Internal_Rel *elf_reloc ATTRIBUTE_UNUSED;
291 r = ELF32_R_TYPE (elf_reloc->r_info);
292 bfd_reloc->howto = &h8_elf_howto_table[r];
295 /* Special handling for H8/300 relocs.
296 We only come here for pcrel stuff and return normally if not an -r link.
297 When doing -r, we can't do any arithmetic for the pcrel stuff, because
298 we support relaxing on the H8/300 series chips. */
299 static bfd_reloc_status_type
300 special (abfd, reloc_entry, symbol, data, input_section, output_bfd,
302 bfd *abfd ATTRIBUTE_UNUSED;
303 arelent *reloc_entry ATTRIBUTE_UNUSED;
304 asymbol *symbol ATTRIBUTE_UNUSED;
305 PTR data ATTRIBUTE_UNUSED;
306 asection *input_section ATTRIBUTE_UNUSED;
308 char **error_message ATTRIBUTE_UNUSED;
310 if (output_bfd == (bfd *) NULL)
311 return bfd_reloc_continue;
313 /* Adjust the reloc address to that in the output section. */
314 reloc_entry->address += input_section->output_offset;
318 /* Perform a relocation as part of a final link. */
319 static bfd_reloc_status_type
320 elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
321 input_section, contents, offset, value,
322 addend, info, sym_sec, is_local)
323 unsigned long r_type;
325 bfd *output_bfd ATTRIBUTE_UNUSED;
326 asection *input_section ATTRIBUTE_UNUSED;
331 struct bfd_link_info *info ATTRIBUTE_UNUSED;
332 asection *sym_sec ATTRIBUTE_UNUSED;
333 int is_local ATTRIBUTE_UNUSED;
335 bfd_byte *hit_data = contents + offset;
347 bfd_put_32 (input_bfd, value, hit_data);
354 bfd_put_16 (input_bfd, value, hit_data);
361 bfd_put_8 (input_bfd, value, hit_data);
367 /* HIT_DATA is the address for the first byte for the relocated
368 value. Subtract 1 so that we can manipulate the data in 32bit
372 /* Clear out the top byte in value. */
375 /* Retrieve the type byte for value from the section contents. */
376 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
378 /* Now scribble it out in one 32bit hunk. */
379 bfd_put_32 (input_bfd, value, hit_data);
383 value -= (input_section->output_section->vma
384 + input_section->output_offset);
388 /* The value is relative to the start of the instruction,
389 not the relocation offset. Subtract 2 to account for
393 bfd_put_16 (input_bfd, value, hit_data);
397 value -= (input_section->output_section->vma
398 + input_section->output_offset);
402 /* The value is relative to the start of the instruction,
403 not the relocation offset. Subtract 1 to account for
407 bfd_put_8 (input_bfd, value, hit_data);
411 return bfd_reloc_notsupported;
415 /* Relocate an H8 ELF section. */
417 elf32_h8_relocate_section (output_bfd, info, input_bfd, input_section,
418 contents, relocs, local_syms, local_sections)
420 struct bfd_link_info *info;
422 asection *input_section;
424 Elf_Internal_Rela *relocs;
425 Elf_Internal_Sym *local_syms;
426 asection **local_sections;
428 Elf_Internal_Shdr *symtab_hdr;
429 struct elf_link_hash_entry **sym_hashes;
430 Elf_Internal_Rela *rel, *relend;
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 r_symndx = ELF32_R_SYM (rel->r_info);
448 r_type = ELF32_R_TYPE (rel->r_info);
450 if (info->relocateable)
452 /* This is a relocateable link. We don't have to change
453 anything, unless the reloc is against a section symbol,
454 in which case we have to adjust according to where the
455 section symbol winds up in the output section. */
456 if (r_symndx < symtab_hdr->sh_info)
458 sym = local_syms + r_symndx;
459 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
461 sec = local_sections[r_symndx];
462 rel->r_addend += sec->output_offset + sym->st_value;
469 /* This is a final link. */
473 if (r_symndx < symtab_hdr->sh_info)
475 sym = local_syms + r_symndx;
476 sec = local_sections[r_symndx];
477 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
481 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
482 while (h->root.type == bfd_link_hash_indirect
483 || h->root.type == bfd_link_hash_warning)
484 h = (struct elf_link_hash_entry *) h->root.u.i.link;
485 if (h->root.type == bfd_link_hash_defined
486 || h->root.type == bfd_link_hash_defweak)
488 sec = h->root.u.def.section;
489 relocation = (h->root.u.def.value
490 + sec->output_section->vma
491 + sec->output_offset);
493 else if (h->root.type == bfd_link_hash_undefweak)
497 if (! ((*info->callbacks->undefined_symbol)
498 (info, h->root.root.string, input_bfd,
499 input_section, rel->r_offset, true)))
505 r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
507 contents, rel->r_offset,
508 relocation, rel->r_addend,
509 info, sec, h == NULL);
511 if (r != bfd_reloc_ok)
514 const char *msg = (const char *) 0;
516 reloc_howto_type *howto;
518 elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel);
519 howto = bfd_reloc.howto;
522 name = h->root.root.string;
525 name = (bfd_elf_string_from_elf_section
526 (input_bfd, symtab_hdr->sh_link, sym->st_name));
527 if (name == NULL || *name == '\0')
528 name = bfd_section_name (input_bfd, sec);
533 case bfd_reloc_overflow:
534 if (! ((*info->callbacks->reloc_overflow)
535 (info, name, howto->name, (bfd_vma) 0,
536 input_bfd, input_section, rel->r_offset)))
540 case bfd_reloc_undefined:
541 if (! ((*info->callbacks->undefined_symbol)
542 (info, name, input_bfd, input_section,
543 rel->r_offset, true)))
547 case bfd_reloc_outofrange:
548 msg = _("internal error: out of range error");
551 case bfd_reloc_notsupported:
552 msg = _("internal error: unsupported relocation error");
555 case bfd_reloc_dangerous:
556 msg = _("internal error: dangerous error");
560 msg = _("internal error: unknown error");
564 if (!((*info->callbacks->warning)
565 (info, msg, name, input_bfd, input_section,
576 /* Object files encode the specific H8 model they were compiled
577 for in the ELF flags field.
579 Examine that field and return the proper BFD machine type for
582 elf32_h8_mach (flags)
585 switch (flags & EF_H8_MACH)
587 case E_H8_MACH_H8300:
589 return bfd_mach_h8300;
591 case E_H8_MACH_H8300H:
592 return bfd_mach_h8300h;
594 case E_H8_MACH_H8300S:
595 return bfd_mach_h8300s;
599 /* The final processing done just before writing out a H8 ELF object
600 file. We use this opportunity to encode the BFD machine type
601 into the flags field in the object file. */
604 elf32_h8_final_write_processing (abfd, linker)
606 boolean linker ATTRIBUTE_UNUSED;
610 switch (bfd_get_mach (abfd))
614 val = E_H8_MACH_H8300;
617 case bfd_mach_h8300h:
618 val = E_H8_MACH_H8300H;
621 case bfd_mach_h8300s:
622 val = E_H8_MACH_H8300S;
626 elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
627 elf_elfheader (abfd)->e_flags |= val;
630 /* Return nonzero if ABFD represents a valid H8 ELF object file; also
631 record the encoded machine type found in the ELF flags. */
634 elf32_h8_object_p (abfd)
637 bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
638 elf32_h8_mach (elf_elfheader (abfd)->e_flags));
642 /* Merge backend specific data from an object file to the output
643 object file when linking. The only data we need to copy at this
644 time is the architecture/machine information. */
647 elf32_h8_merge_private_bfd_data (ibfd, obfd)
651 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
652 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
655 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
656 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
658 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
659 bfd_get_mach (ibfd)))
666 /* This function handles relaxing for the H8..
668 There's a few relaxing opportunites available on the H8:
670 jmp/jsr:24 -> bra/bsr:8 2 bytes
671 The jmp may be completely eliminated if the previous insn is a
672 conditional branch to the insn after the jump. In that case
673 we invert the branch and delete the jump and save 4 bytes.
675 bCC:16 -> bCC:8 2 bytes
676 bsr:16 -> bsr:8 2 bytes
678 mov.b:16 -> mov.b:8 2 bytes
679 mov.b:24/32 -> mov.b:8 4 bytes
681 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes
687 elf32_h8_relax_section (abfd, sec, link_info, again)
690 struct bfd_link_info *link_info;
693 Elf_Internal_Shdr *symtab_hdr;
694 Elf_Internal_Shdr *shndx_hdr;
695 Elf_Internal_Rela *internal_relocs;
696 Elf_Internal_Rela *free_relocs = NULL;
697 Elf_Internal_Rela *irel, *irelend;
698 bfd_byte *contents = NULL;
699 bfd_byte *free_contents = NULL;
700 Elf32_External_Sym *extsyms = NULL;
701 Elf32_External_Sym *free_extsyms = NULL;
702 Elf_External_Sym_Shndx *shndx_buf = 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;
724 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
726 /* Get a copy of the native relocations. */
727 internal_relocs = (_bfd_elf32_link_read_relocs
728 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
729 link_info->keep_memory));
730 if (internal_relocs == NULL)
732 if (! link_info->keep_memory)
733 free_relocs = internal_relocs;
735 if (sec != last_input_section)
738 last_input_section = sec;
740 /* Walk through the relocs looking for relaxing opportunities. */
741 irelend = internal_relocs + sec->reloc_count;
742 for (irel = internal_relocs; irel < irelend; irel++)
746 /* Keep track of the previous reloc so that we can delete
747 some long jumps created by the compiler. */
748 if (irel != internal_relocs)
749 last_reloc = irel - 1;
751 /* Get the section contents if we haven't done so already. */
752 if (contents == NULL)
754 /* Get cached copy if it exists. */
755 if (elf_section_data (sec)->this_hdr.contents != NULL)
756 contents = elf_section_data (sec)->this_hdr.contents;
759 /* Go get them off disk. */
760 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
761 if (contents == NULL)
763 free_contents = contents;
765 if (! bfd_get_section_contents (abfd, sec, contents,
766 (file_ptr) 0, sec->_raw_size))
771 /* Read this BFD's local symbols if we haven't done so already. */
774 /* Get cached copy if it exists. */
775 if (symtab_hdr->contents != NULL)
776 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
779 /* Go get them off disk. */
782 amt = symtab_hdr->sh_info * sizeof (Elf32_External_Sym);
783 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
786 free_extsyms = extsyms;
787 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
788 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
790 symtab_hdr->contents = (PTR) extsyms;
793 if (shndx_hdr->sh_size != 0)
797 amt = symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx);
798 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
799 if (shndx_buf == NULL)
801 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
802 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
804 shndx_hdr->contents = (PTR) shndx_buf;
808 /* Get the value of the symbol referred to by the reloc. */
809 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
811 Elf32_External_Sym *esym;
812 Elf_External_Sym_Shndx *shndx;
813 Elf_Internal_Sym isym;
816 /* A local symbol. */
817 esym = extsyms + ELF32_R_SYM (irel->r_info);
818 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
819 bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx,
822 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
823 symval = (isym.st_value
824 + sym_sec->output_section->vma
825 + sym_sec->output_offset);
830 struct elf_link_hash_entry *h;
832 /* An external symbol. */
833 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
834 h = elf_sym_hashes (abfd)[indx];
835 BFD_ASSERT (h != NULL);
836 if (h->root.type != bfd_link_hash_defined
837 && h->root.type != bfd_link_hash_defweak)
839 /* This appears to be a reference to an undefined
840 symbol. Just ignore it--it will be caught by the
841 regular reloc processing. */
845 symval = (h->root.u.def.value
846 + h->root.u.def.section->output_section->vma
847 + h->root.u.def.section->output_offset);
850 /* For simplicity of coding, we are going to modify the section
851 contents, the section relocs, and the BFD symbol table. We
852 must tell the rest of the code not to free up this
853 information. It would be possible to instead create a table
854 of changes which have to be made, as is done in coff-mips.c;
855 that would be more work, but would require less memory when
856 the linker is run. */
857 switch (ELF32_R_TYPE (irel->r_info))
859 /* Try to turn a 24 bit absolute branch/call into an 8 bit
860 pc-relative branch/call. */
863 bfd_vma value = symval + irel->r_addend;
866 /* Get the address of this instruction. */
867 dot = (sec->output_section->vma
868 + sec->output_offset + irel->r_offset - 1);
870 /* Compute the distance from this insn to the branch target. */
873 /* If the distance is within -126..+130 inclusive, then we can
874 relax this jump. +130 is valid since the target will move
875 two bytes closer if we do relax this branch. */
876 if ((int) gap >= -126 && (int) gap <= 130)
880 /* Note that we've changed the relocs, section contents,
882 elf_section_data (sec)->relocs = internal_relocs;
885 elf_section_data (sec)->this_hdr.contents = contents;
886 free_contents = NULL;
890 /* If the previous instruction conditionally jumped around
891 this instruction, we may be able to reverse the condition
892 and redirect the previous instruction to the target of
895 Such sequences are used by the compiler to deal with
896 long conditional branches. */
900 && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
901 && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
903 Elf32_External_Sym *esym;
904 Elf_External_Sym_Shndx *shndx;
906 asection *last_sym_sec;
907 Elf_Internal_Sym last_symbol;
909 /* We will need to examine the symbol used by the
910 previous relocation. */
912 esym = extsyms + ELF32_R_SYM (last_reloc->r_info);
915 shndx += ELF32_R_SYM (last_reloc->r_info);
916 bfd_elf32_swap_symbol_in (abfd, (const PTR) esym,
921 = bfd_section_from_elf_index (abfd, last_symbol.st_shndx);
922 last_value = (last_symbol.st_value
923 + last_sym_sec->output_section->vma
924 + last_sym_sec->output_offset);
926 /* Verify that the previous relocation was for a
927 branch around this instruction and that no symbol
928 exists at the current location. */
929 if (last_value == dot + 4
930 && last_reloc->r_offset + 2 == irel->r_offset
931 && ! elf32_h8_symbol_address_p (abfd, sec, dot))
933 /* We can eliminate this jump. Twiddle the
934 previous relocation as necessary. */
936 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
937 ELF32_R_TYPE (R_H8_NONE));
940 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
941 ELF32_R_TYPE (R_H8_PCREL8));
942 last_reloc->r_addend = irel->r_addend;
945 code = bfd_get_8 (abfd,
946 contents + last_reloc->r_offset - 1);
950 contents + last_reloc->r_offset - 1);
952 /* Delete four bytes of data. */
953 if (!elf32_h8_relax_delete_bytes (abfd, sec,
963 /* We could not eliminate this jump, so just shorten it. */
964 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
967 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1);
968 else if (code == 0x5a)
969 bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1);
973 /* Fix the relocation's type. */
974 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
977 /* Delete two bytes of data. */
978 if (!elf32_h8_relax_delete_bytes (abfd, sec,
979 irel->r_offset + 1, 2))
982 /* That will change things, so, we should relax again.
983 Note that this is not required, and it may be slow. */
989 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
993 bfd_vma value = symval + irel->r_addend;
997 /* Get the address of this instruction. */
998 dot = (sec->output_section->vma
1000 + irel->r_offset - 2);
1004 /* If the distance is within -126..+130 inclusive, then we can
1005 relax this jump. +130 is valid since the target will move
1006 two bytes closer if we do relax this branch. */
1007 if ((int)gap >= -126 && (int)gap <= 130)
1011 /* Note that we've changed the relocs, section contents,
1013 elf_section_data (sec)->relocs = internal_relocs;
1016 elf_section_data (sec)->this_hdr.contents = contents;
1017 free_contents = NULL;
1019 free_extsyms = NULL;
1021 /* Get the opcode. */
1022 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1026 /* bCC:16 -> bCC:8 */
1027 /* Get the condition code from the original insn. */
1028 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1032 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1034 else if (code == 0x5c)
1035 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2);
1039 /* Fix the relocation's type. */
1040 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1044 /* Delete two bytes of data. */
1045 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1046 irel->r_offset + 1, 2))
1049 /* That will change things, so, we should relax again.
1050 Note that this is not required, and it may be slow. */
1056 /* This is a 16 bit absolute address in a "mov.b" insn, which may
1057 become an 8 bit absolute address if its in the right range. */
1060 bfd_vma value = symval + irel->r_addend;
1062 if ((bfd_get_mach (abfd) == bfd_mach_h8300
1065 || ((bfd_get_mach (abfd) == bfd_mach_h8300h
1066 || bfd_get_mach (abfd) == bfd_mach_h8300s)
1067 && value >= 0xffff00
1068 && value <= 0xffffff))
1072 /* Note that we've changed the relocs, section contents,
1074 elf_section_data (sec)->relocs = internal_relocs;
1077 elf_section_data (sec)->this_hdr.contents = contents;
1078 free_contents = NULL;
1080 free_extsyms = NULL;
1082 /* Get the opcode. */
1083 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1089 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1091 if ((code & 0xf0) == 0x00)
1093 (code & 0xf) | 0x20,
1094 contents + irel->r_offset - 2);
1095 else if ((code & 0xf0) == 0x80)
1097 (code & 0xf) | 0x30,
1098 contents + irel->r_offset - 2);
1102 /* Fix the relocation's type. */
1103 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1106 /* Delete two bytes of data. */
1107 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1108 irel->r_offset + 1, 2))
1111 /* That will change things, so, we should relax again.
1112 Note that this is not required, and it may be slow. */
1118 /* This is a 24 bit absolute address in a "mov.b" insn, which may
1119 become an 8 bit absolute address if its in the right range. */
1122 bfd_vma value = symval + irel->r_addend;
1124 if ((bfd_get_mach (abfd) == bfd_mach_h8300
1127 || ((bfd_get_mach (abfd) == bfd_mach_h8300h
1128 || bfd_get_mach (abfd) == bfd_mach_h8300s)
1129 && value >= 0xffff00
1130 && value <= 0xffffff))
1134 /* Note that we've changed the relocs, section contents,
1136 elf_section_data (sec)->relocs = internal_relocs;
1139 elf_section_data (sec)->this_hdr.contents = contents;
1140 free_contents = NULL;
1142 free_extsyms = NULL;
1144 /* Get the opcode. */
1145 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1151 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1153 if ((code & 0xf0) == 0x00)
1155 (code & 0xf) | 0x20,
1156 contents + irel->r_offset - 2);
1157 else if ((code & 0xf0) == 0x80)
1159 (code & 0xf) | 0x30,
1160 contents + irel->r_offset - 2);
1164 /* Fix the relocation's type. */
1165 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1168 /* Delete two bytes of data. */
1169 if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
1172 /* That will change things, so, we should relax again.
1173 Note that this is not required, and it may be slow. */
1180 /* This is a 24/32bit absolute address in a "mov" insn, which may
1181 become a 16bit absoulte address if it is in the right range. */
1184 bfd_vma value = symval + irel->r_addend;
1186 if (value <= 0x7fff || value >= 0xff8000)
1190 /* Note that we've changed the relocs, section contents,
1192 elf_section_data (sec)->relocs = internal_relocs;
1195 elf_section_data (sec)->this_hdr.contents = contents;
1196 free_contents = NULL;
1198 free_extsyms = NULL;
1200 /* Get the opcode. */
1201 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1203 /* We just need to turn off bit 0x20. */
1206 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1208 /* Fix the relocation's type. */
1209 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1212 /* Delete two bytes of data. */
1213 if (!elf32_h8_relax_delete_bytes (abfd, sec,
1214 irel->r_offset + 1, 2))
1217 /* That will change things, so, we should relax again.
1218 Note that this is not required, and it may be slow. */
1229 if (free_relocs != NULL)
1235 if (free_contents != NULL)
1237 if (! link_info->keep_memory)
1238 free (free_contents);
1241 /* Cache the section contents for elf_link_input_bfd. */
1242 elf_section_data (sec)->this_hdr.contents = contents;
1244 free_contents = NULL;
1247 if (shndx_buf != NULL)
1249 shndx_hdr->contents = NULL;
1253 if (free_extsyms != NULL)
1255 if (! link_info->keep_memory)
1257 symtab_hdr->contents = NULL;
1258 free (free_extsyms);
1265 if (free_relocs != NULL)
1267 if (free_contents != NULL)
1268 free (free_contents);
1269 if (shndx_buf != NULL)
1271 shndx_hdr->contents = NULL;
1274 if (free_extsyms != NULL)
1276 symtab_hdr->contents = NULL;
1277 free (free_extsyms);
1282 /* Delete some bytes from a section while relaxing. */
1285 elf32_h8_relax_delete_bytes (abfd, sec, addr, count)
1291 Elf_Internal_Shdr *symtab_hdr;
1292 Elf_Internal_Shdr *shndx_hdr;
1293 Elf32_External_Sym *extsyms;
1294 unsigned int sec_shndx;
1296 Elf_Internal_Rela *irel, *irelend;
1297 Elf_Internal_Rela *irelalign;
1299 Elf32_External_Sym *esym, *esymend;
1300 Elf_External_Sym_Shndx *shndx;
1301 struct elf_link_hash_entry **sym_hashes;
1302 struct elf_link_hash_entry **end_hashes;
1303 unsigned int symcount;
1305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1306 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
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 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1337 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1339 esymend = esym + symtab_hdr->sh_info;
1340 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1342 Elf_Internal_Sym isym;
1343 Elf_External_Sym_Shndx dummy;
1345 bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx,
1348 if (isym.st_shndx == sec_shndx
1349 && isym.st_value > addr
1350 && isym.st_value < toaddr)
1352 isym.st_value -= count;
1353 bfd_elf32_swap_symbol_out (abfd, &isym, esym, &dummy);
1357 /* Now adjust the global symbols defined in this section. */
1358 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1359 - symtab_hdr->sh_info);
1360 sym_hashes = elf_sym_hashes (abfd);
1361 end_hashes = sym_hashes + symcount;
1362 for (; sym_hashes < end_hashes; sym_hashes++)
1364 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1365 if ((sym_hash->root.type == bfd_link_hash_defined
1366 || sym_hash->root.type == bfd_link_hash_defweak)
1367 && sym_hash->root.u.def.section == sec
1368 && sym_hash->root.u.def.value > addr
1369 && sym_hash->root.u.def.value < toaddr)
1371 sym_hash->root.u.def.value -= count;
1378 /* Return true if a symbol exists at the given address, else return
1381 elf32_h8_symbol_address_p (abfd, sec, addr)
1386 Elf_Internal_Shdr *symtab_hdr;
1387 Elf_Internal_Shdr *shndx_hdr;
1388 unsigned int sec_shndx;
1389 Elf32_External_Sym *esym, *esymend;
1390 Elf_External_Sym_Shndx *shndx;
1391 struct elf_link_hash_entry **sym_hashes;
1392 struct elf_link_hash_entry **end_hashes;
1393 unsigned int symcount;
1395 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1397 /* Examine all the symbols. */
1398 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1399 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1400 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1401 esym = (Elf32_External_Sym *) symtab_hdr->contents;
1402 esymend = esym + symtab_hdr->sh_info;
1403 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1405 Elf_Internal_Sym isym;
1407 bfd_elf32_swap_symbol_in (abfd, (const PTR) esym, (const PTR) shndx,
1410 if (isym.st_shndx == sec_shndx
1411 && isym.st_value == addr)
1415 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1416 - symtab_hdr->sh_info);
1417 sym_hashes = elf_sym_hashes (abfd);
1418 end_hashes = sym_hashes + symcount;
1419 for (; sym_hashes < end_hashes; sym_hashes++)
1421 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1422 if ((sym_hash->root.type == bfd_link_hash_defined
1423 || sym_hash->root.type == bfd_link_hash_defweak)
1424 && sym_hash->root.u.def.section == sec
1425 && sym_hash->root.u.def.value == addr)
1432 /* This is a version of bfd_generic_get_relocated_section_contents
1433 which uses elf32_h8_relocate_section. */
1436 elf32_h8_get_relocated_section_contents (output_bfd, link_info, link_order,
1437 data, relocateable, symbols)
1439 struct bfd_link_info *link_info;
1440 struct bfd_link_order *link_order;
1442 boolean relocateable;
1445 Elf_Internal_Shdr *symtab_hdr;
1446 Elf_Internal_Shdr *shndx_hdr;
1447 asection *input_section = link_order->u.indirect.section;
1448 bfd *input_bfd = input_section->owner;
1449 asection **sections = NULL;
1450 Elf_Internal_Rela *internal_relocs = NULL;
1451 Elf32_External_Sym *external_syms = NULL;
1452 Elf_External_Sym_Shndx *shndx_buf = NULL;
1453 Elf_External_Sym_Shndx *shndx;
1454 Elf_Internal_Sym *internal_syms = NULL;
1456 /* We only need to handle the case of relaxing, or of having a
1457 particular set of section contents, specially. */
1459 || elf_section_data (input_section)->this_hdr.contents == NULL)
1460 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1465 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1466 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1468 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1469 (size_t) input_section->_raw_size);
1471 if ((input_section->flags & SEC_RELOC) != 0
1472 && input_section->reloc_count > 0)
1474 Elf_Internal_Sym *isymp;
1476 Elf32_External_Sym *esym, *esymend;
1479 if (symtab_hdr->contents != NULL)
1480 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1481 else if (symtab_hdr->sh_info != 0)
1483 amt = symtab_hdr->sh_info;
1484 amt *= sizeof (Elf32_External_Sym);
1485 external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
1486 if (external_syms == NULL)
1488 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1489 || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
1493 if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
1495 amt = symtab_hdr->sh_info;
1496 amt *= sizeof (Elf_External_Sym_Shndx);
1497 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1498 if (shndx_buf == NULL)
1500 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1501 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1505 internal_relocs = (_bfd_elf32_link_read_relocs
1506 (input_bfd, input_section, (PTR) NULL,
1507 (Elf_Internal_Rela *) NULL, false));
1508 if (internal_relocs == NULL)
1511 amt = symtab_hdr->sh_info;
1512 amt *= sizeof (Elf_Internal_Sym);
1513 internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
1514 if (internal_syms == NULL && amt != 0)
1517 amt = symtab_hdr->sh_info;
1518 amt *= sizeof (asection *);
1519 sections = (asection **) bfd_malloc (amt);
1520 if (sections == NULL && amt != 0)
1523 for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
1524 esym = external_syms, esymend = esym + symtab_hdr->sh_info;
1526 ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
1530 bfd_elf32_swap_symbol_in (input_bfd, (const PTR) esym,
1531 (const PTR) shndx, isymp);
1533 if (isymp->st_shndx == SHN_UNDEF)
1534 isec = bfd_und_section_ptr;
1535 else if (isymp->st_shndx == SHN_ABS)
1536 isec = bfd_abs_section_ptr;
1537 else if (isymp->st_shndx == SHN_COMMON)
1538 isec = bfd_com_section_ptr;
1540 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1545 if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd,
1546 input_section, data, internal_relocs,
1547 internal_syms, sections))
1550 if (sections != NULL)
1552 if (internal_syms != NULL)
1553 free (internal_syms);
1554 if (shndx_buf != NULL)
1556 if (external_syms != NULL && symtab_hdr->contents == NULL)
1557 free (external_syms);
1558 if (internal_relocs != elf_section_data (input_section)->relocs)
1559 free (internal_relocs);
1565 if (internal_relocs != NULL
1566 && internal_relocs != elf_section_data (input_section)->relocs)
1567 free (internal_relocs);
1568 if (shndx_buf != NULL)
1570 if (external_syms != NULL && symtab_hdr->contents == NULL)
1571 free (external_syms);
1572 if (internal_syms != NULL)
1573 free (internal_syms);
1574 if (sections != NULL)
1580 #define TARGET_BIG_SYM bfd_elf32_h8300_vec
1581 #define TARGET_BIG_NAME "elf32-h8300"
1582 #define ELF_ARCH bfd_arch_h8300
1583 #define ELF_MACHINE_CODE EM_H8_300
1584 #define ELF_MAXPAGESIZE 0x1
1585 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1586 #define elf_info_to_howto elf32_h8_info_to_howto
1587 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel
1589 /* So we can set/examine bits in e_flags to get the specific
1590 H8 architecture in use. */
1591 #define elf_backend_final_write_processing \
1592 elf32_h8_final_write_processing
1593 #define elf_backend_object_p \
1595 #define bfd_elf32_bfd_merge_private_bfd_data \
1596 elf32_h8_merge_private_bfd_data
1598 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1599 defaults to using _bfd_generic_link_hash_table_create, but
1600 elflink.h:bfd_elf32_size_dynamic_sections uses
1601 dynobj = elf_hash_table (info)->dynobj;
1602 and thus requires an elf hash table. */
1603 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1605 /* Use an H8 specific linker, not the ELF generic linker. */
1606 #define elf_backend_relocate_section elf32_h8_relocate_section
1608 /* And relaxing stuff. */
1609 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section
1610 #define bfd_elf32_bfd_get_relocated_section_contents \
1611 elf32_h8_get_relocated_section_contents
1614 #include "elf32-target.h"