bfd/
[binutils-gdb.git] / bfd / elf32-h8300.c
index f272dc8e084a5f8c3020a5e992d1d736db9d8176..efd14bb582fb0f763548979dec3d32e94640f19f 100644 (file)
 #include "elf/h8.h"
 
 static reloc_howto_type *elf32_h8_reloc_type_lookup
-  PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
+  (bfd *abfd, bfd_reloc_code_real_type code);
 static void elf32_h8_info_to_howto
-  PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
+  (bfd *, arelent *, Elf_Internal_Rela *);
 static void elf32_h8_info_to_howto_rel
-  PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
+  (bfd *, arelent *, Elf_Internal_Rela *);
 static unsigned long elf32_h8_mach
-  PARAMS ((flagword));
+  (flagword);
 static void elf32_h8_final_write_processing
-  PARAMS ((bfd *, bfd_boolean));
+  (bfd *, bfd_boolean);
 static bfd_boolean elf32_h8_object_p
-  PARAMS ((bfd *));
+  (bfd *);
 static bfd_boolean elf32_h8_merge_private_bfd_data
-  PARAMS ((bfd *, bfd *));
+  (bfd *, bfd *);
 static bfd_boolean elf32_h8_relax_section
-  PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
+  (bfd *, asection *, struct bfd_link_info *, bfd_boolean *);
 static bfd_boolean elf32_h8_relax_delete_bytes
-  PARAMS ((bfd *, asection *, bfd_vma, int));
+  (bfd *, asection *, bfd_vma, int);
 static bfd_boolean elf32_h8_symbol_address_p
-  PARAMS ((bfd *, asection *, bfd_vma));
+  (bfd *, asection *, bfd_vma);
 static bfd_byte *elf32_h8_get_relocated_section_contents
-  PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
-          bfd_byte *, bfd_boolean, asymbol **));
+  (bfd *, struct bfd_link_info *, struct bfd_link_order *,
+   bfd_byte *, bfd_boolean, asymbol **);
 static bfd_reloc_status_type elf32_h8_final_link_relocate
-  PARAMS ((unsigned long, bfd *, bfd *, asection *,
-          bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
-          struct bfd_link_info *, asection *, int));
+  (unsigned long, bfd *, bfd *, asection *,
+   bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
+   struct bfd_link_info *, asection *, int);
 static bfd_boolean elf32_h8_relocate_section
-  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
-          bfd_byte *, Elf_Internal_Rela *,
-          Elf_Internal_Sym *, asection **));
+  (bfd *, struct bfd_link_info *, bfd *, asection *,
+   bfd_byte *, Elf_Internal_Rela *,
+   Elf_Internal_Sym *, asection **);
 static bfd_reloc_status_type special
-  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
+  (bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **);
 
 /* This does not include any relocation information, but should be
    good enough for GDB or objdump to read the file.  */
@@ -183,7 +183,7 @@ static reloc_howto_type h8_elf_howto_table[] = {
         0,                     /* bitpos */
         complain_overflow_dont,/* complain_on_overflow */
         special,               /* special_function */
-        "R_H8_DIR32",          /* name */
+        "R_H8_DIR32A16",       /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
         0xffffffff,            /* dst_mask */
@@ -243,9 +243,8 @@ static const struct elf_reloc_map h8_reloc_map[] = {
 
 
 static reloc_howto_type *
-elf32_h8_reloc_type_lookup (abfd, code)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     bfd_reloc_code_real_type code;
+elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
+                           bfd_reloc_code_real_type code)
 {
   unsigned int i;
 
@@ -258,10 +257,8 @@ elf32_h8_reloc_type_lookup (abfd, code)
 }
 
 static void
-elf32_h8_info_to_howto (abfd, bfd_reloc, elf_reloc)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *bfd_reloc;
-     Elf_Internal_Rela *elf_reloc;
+elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
+                       Elf_Internal_Rela *elf_reloc)
 {
   unsigned int r;
   unsigned int i;
@@ -277,10 +274,8 @@ elf32_h8_info_to_howto (abfd, bfd_reloc, elf_reloc)
 }
 
 static void
-elf32_h8_info_to_howto_rel (abfd, bfd_reloc, elf_reloc)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *bfd_reloc;
-     Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED;
+elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
+                           Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED)
 {
   unsigned int r;
 
@@ -294,15 +289,13 @@ elf32_h8_info_to_howto_rel (abfd, bfd_reloc, elf_reloc)
    When doing -r, we can't do any arithmetic for the pcrel stuff, because
    we support relaxing on the H8/300 series chips.  */
 static bfd_reloc_status_type
-special (abfd, reloc_entry, symbol, data, input_section, output_bfd,
-        error_message)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *reloc_entry ATTRIBUTE_UNUSED;
-     asymbol *symbol ATTRIBUTE_UNUSED;
-     PTR data ATTRIBUTE_UNUSED;
-     asection *input_section ATTRIBUTE_UNUSED;
-     bfd *output_bfd;
-     char **error_message ATTRIBUTE_UNUSED;
+special (bfd *abfd ATTRIBUTE_UNUSED,
+        arelent *reloc_entry ATTRIBUTE_UNUSED,
+        asymbol *symbol ATTRIBUTE_UNUSED,
+        PTR data ATTRIBUTE_UNUSED,
+        asection *input_section ATTRIBUTE_UNUSED,
+        bfd *output_bfd,
+        char **error_message ATTRIBUTE_UNUSED)
 {
   if (output_bfd == (bfd *) NULL)
     return bfd_reloc_continue;
@@ -314,26 +307,19 @@ special (abfd, reloc_entry, symbol, data, input_section, output_bfd,
 
 /* Perform a relocation as part of a final link.  */
 static bfd_reloc_status_type
-elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
-                             input_section, contents, offset, value,
-                             addend, info, sym_sec, is_local)
-     unsigned long r_type;
-     bfd *input_bfd;
-     bfd *output_bfd ATTRIBUTE_UNUSED;
-     asection *input_section ATTRIBUTE_UNUSED;
-     bfd_byte *contents;
-     bfd_vma offset;
-     bfd_vma value;
-     bfd_vma addend;
-     struct bfd_link_info *info ATTRIBUTE_UNUSED;
-     asection *sym_sec ATTRIBUTE_UNUSED;
-     int is_local ATTRIBUTE_UNUSED;
+elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd,
+                             bfd *output_bfd ATTRIBUTE_UNUSED,
+                             asection *input_section ATTRIBUTE_UNUSED,
+                             bfd_byte *contents, bfd_vma offset,
+                             bfd_vma value, bfd_vma addend,
+                             struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                             asection *sym_sec ATTRIBUTE_UNUSED,
+                             int is_local ATTRIBUTE_UNUSED)
 {
   bfd_byte *hit_data = contents + offset;
 
   switch (r_type)
     {
-
     case R_H8_NONE:
       return bfd_reloc_ok;
 
@@ -411,22 +397,17 @@ elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd,
 \f
 /* Relocate an H8 ELF section.  */
 static bfd_boolean
-elf32_h8_relocate_section (output_bfd, info, input_bfd, input_section,
-                          contents, relocs, local_syms, local_sections)
-     bfd *output_bfd;
-     struct bfd_link_info *info;
-     bfd *input_bfd;
-     asection *input_section;
-     bfd_byte *contents;
-     Elf_Internal_Rela *relocs;
-     Elf_Internal_Sym *local_syms;
-     asection **local_sections;
+elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
+                          bfd *input_bfd, asection *input_section,
+                          bfd_byte *contents, Elf_Internal_Rela *relocs,
+                          Elf_Internal_Sym *local_syms,
+                          asection **local_sections)
 {
   Elf_Internal_Shdr *symtab_hdr;
   struct elf_link_hash_entry **sym_hashes;
   Elf_Internal_Rela *rel, *relend;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
@@ -559,8 +540,7 @@ elf32_h8_relocate_section (output_bfd, info, input_bfd, input_section,
    Examine that field and return the proper BFD machine type for
    the object file.  */
 static unsigned long
-elf32_h8_mach (flags)
-     flagword flags;
+elf32_h8_mach (flagword flags)
 {
   switch (flags & EF_H8_MACH)
     {
@@ -582,6 +562,9 @@ elf32_h8_mach (flags)
 
     case E_H8_MACH_H8300SX:
       return bfd_mach_h8300sx;
+
+    case E_H8_MACH_H8300SXN:
+      return bfd_mach_h8300sxn;
     }
 }
 
@@ -590,9 +573,8 @@ elf32_h8_mach (flags)
    into the flags field in the object file.  */
 
 static void
-elf32_h8_final_write_processing (abfd, linker)
-     bfd *abfd;
-     bfd_boolean linker ATTRIBUTE_UNUSED;
+elf32_h8_final_write_processing (bfd *abfd,
+                                bfd_boolean linker ATTRIBUTE_UNUSED)
 {
   unsigned long val;
 
@@ -622,6 +604,10 @@ elf32_h8_final_write_processing (abfd, linker)
     case bfd_mach_h8300sx:
       val = E_H8_MACH_H8300SX;
       break;
+
+    case bfd_mach_h8300sxn:
+      val = E_H8_MACH_H8300SXN;
+      break;
     }
 
   elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
@@ -632,8 +618,7 @@ elf32_h8_final_write_processing (abfd, linker)
    record the encoded machine type found in the ELF flags.  */
 
 static bfd_boolean
-elf32_h8_object_p (abfd)
-     bfd *abfd;
+elf32_h8_object_p (bfd *abfd)
 {
   bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
                             elf32_h8_mach (elf_elfheader (abfd)->e_flags));
@@ -645,9 +630,7 @@ elf32_h8_object_p (abfd)
    time is the architecture/machine information.  */
 
 static bfd_boolean
-elf32_h8_merge_private_bfd_data (ibfd, obfd)
-     bfd *ibfd;
-     bfd *obfd;
+elf32_h8_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
@@ -682,11 +665,8 @@ elf32_h8_merge_private_bfd_data (ibfd, obfd)
      mov.[bwl]:24/32 ->    mov.[bwl]:16           2 bytes */
 
 static bfd_boolean
-elf32_h8_relax_section (abfd, sec, link_info, again)
-     bfd *abfd;
-     asection *sec;
-     struct bfd_link_info *link_info;
-     bfd_boolean *again;
+elf32_h8_relax_section (bfd *abfd, asection *sec,
+                       struct bfd_link_info *link_info, bfd_boolean *again)
 {
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Rela *internal_relocs;
@@ -699,10 +679,10 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
   /* Assume nothing changes.  */
   *again = FALSE;
 
-  /* We don't have to do anything for a relocateable link, if
+  /* We don't have to do anything for a relocatable link, if
      this section does not have relocs, or if this is not a
      code section.  */
-  if (link_info->relocateable
+  if (link_info->relocatable
       || (sec->flags & SEC_RELOC) == 0
       || sec->reloc_count == 0
       || (sec->flags & SEC_CODE) == 0)
@@ -1024,18 +1004,10 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
           become an 8 bit absolute address if its in the right range.  */
        case R_H8_DIR16A8:
          {
-           bfd_vma value = symval + irel->r_addend;
+           bfd_vma value;
 
-           if ((bfd_get_mach (abfd) == bfd_mach_h8300
-                && value >= 0xff00
-                && value <= 0xffff)
-               || ((bfd_get_mach (abfd) == bfd_mach_h8300h
-                    /* FIXME: h8300hn? */
-                    || bfd_get_mach (abfd) == bfd_mach_h8300s
-                    /* FIXME: h8300sn? */
-                    || bfd_get_mach (abfd) == bfd_mach_h8300sx)
-                   && value >= 0xffff00
-                   && value <= 0xffffff))
+           value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
+           if (value >= 0xffffff00u)
              {
                unsigned char code;
 
@@ -1069,6 +1041,9 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
                irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
                                             R_H8_DIR8);
 
+               /* Move the relocation.  */
+               irel->r_offset--;
+
                /* Delete two bytes of data.  */
                if (!elf32_h8_relax_delete_bytes (abfd, sec,
                                                  irel->r_offset + 1, 2))
@@ -1085,20 +1060,11 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
           become an 8 bit absolute address if its in the right range.  */
        case R_H8_DIR24A8:
          {
-           bfd_vma value = symval + irel->r_addend;
+           bfd_vma value;
 
-           if ((bfd_get_mach (abfd) == bfd_mach_h8300
-                && value >= 0xff00
-                && value <= 0xffff)
-               || ((bfd_get_mach (abfd) == bfd_mach_h8300h
-                    /* FIXME: h8300hn? */
-                    || bfd_get_mach (abfd) == bfd_mach_h8300s
-                    /* FIXME: h8300sn? */
-                    || bfd_get_mach (abfd) == bfd_mach_h8300sx)
-                   && value >= 0xffff00
-                   && value <= 0xffffff))
+           value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
+           if (value >= 0xffffff00u)
              {
-               bfd_boolean skip = FALSE;
                unsigned char code;
 
                /* Note that we've changed the relocs, section contents,
@@ -1118,37 +1084,32 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
 
                switch (code & 0xf0)
                  {
-                 case 0x00:
+                 case 0x20:
                    bfd_put_8 (abfd, (code & 0xf) | 0x20,
                               contents + irel->r_offset - 2);
                    break;
-                 case 0x80:
+                 case 0xa0:
                    bfd_put_8 (abfd, (code & 0xf) | 0x30,
                               contents + irel->r_offset - 2);
                    break;
-                 case 0x20:
-                 case 0xa0:
-                   /* Skip 32bit versions.  */
-                   skip = TRUE;
-                   break;
                  default:
                    abort ();
                  }
 
-               if (skip)
-                 break;
-
                /* Fix the relocation's type.  */
                irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
                                             R_H8_DIR8);
+               irel->r_offset--;
 
                /* Delete two bytes of data.  */
-               if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
+               if (!elf32_h8_relax_delete_bytes (abfd, sec,
+                                                 irel->r_offset + 1, 4))
                  goto error_return;
 
                /* That will change things, so, we should relax again.
                   Note that this is not required, and it may be slow.  */
                *again = TRUE;
+               break;
              }
          }
 
@@ -1158,9 +1119,10 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
           become a 16bit absoulte address if it is in the right range.  */
        case R_H8_DIR32A16:
          {
-           bfd_vma value = symval + irel->r_addend;
+           bfd_vma value;
 
-           if (value <= 0x7fff || value >= 0xff8000)
+           value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
+           if (value <= 0x7fff || value >= 0xffff8000u)
              {
                unsigned char code;
 
@@ -1180,7 +1142,7 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
 
                /* Fix the relocation's type.  */
                irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
-                                            R_H8_DIR16A8);
+                                            R_H8_DIR16);
 
                /* Delete two bytes of data.  */
                if (!elf32_h8_relax_delete_bytes (abfd, sec,
@@ -1242,11 +1204,7 @@ elf32_h8_relax_section (abfd, sec, link_info, again)
 /* Delete some bytes from a section while relaxing.  */
 
 static bfd_boolean
-elf32_h8_relax_delete_bytes (abfd, sec, addr, count)
-     bfd *abfd;
-     asection *sec;
-     bfd_vma addr;
-     int count;
+elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
 {
   Elf_Internal_Shdr *symtab_hdr;
   unsigned int sec_shndx;
@@ -1323,10 +1281,7 @@ elf32_h8_relax_delete_bytes (abfd, sec, addr, count)
 /* Return TRUE if a symbol exists at the given address, else return
    FALSE.  */
 static bfd_boolean
-elf32_h8_symbol_address_p (abfd, sec, addr)
-     bfd *abfd;
-     asection *sec;
-     bfd_vma addr;
+elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr)
 {
   Elf_Internal_Shdr *symtab_hdr;
   unsigned int sec_shndx;
@@ -1370,14 +1325,12 @@ elf32_h8_symbol_address_p (abfd, sec, addr)
    which uses elf32_h8_relocate_section.  */
 
 static bfd_byte *
-elf32_h8_get_relocated_section_contents (output_bfd, link_info, link_order,
-                                        data, relocateable, symbols)
-     bfd *output_bfd;
-     struct bfd_link_info *link_info;
-     struct bfd_link_order *link_order;
-     bfd_byte *data;
-     bfd_boolean relocateable;
-     asymbol **symbols;
+elf32_h8_get_relocated_section_contents (bfd *output_bfd,
+                                        struct bfd_link_info *link_info,
+                                        struct bfd_link_order *link_order,
+                                        bfd_byte *data,
+                                        bfd_boolean relocatable,
+                                        asymbol **symbols)
 {
   Elf_Internal_Shdr *symtab_hdr;
   asection *input_section = link_order->u.indirect.section;
@@ -1388,11 +1341,11 @@ elf32_h8_get_relocated_section_contents (output_bfd, link_info, link_order,
 
   /* We only need to handle the case of relaxing, or of having a
      particular set of section contents, specially.  */
-  if (relocateable
+  if (relocatable
       || elf_section_data (input_section)->this_hdr.contents == NULL)
     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
                                                       link_order, data,
-                                                      relocateable,
+                                                      relocatable,
                                                       symbols);
 
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;