2004-10-21 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / bfd / elf32-v850.c
index 26201f5e8ec6fe84a509d78f6676fdc954679e9a..763220acce08ffeb898e3ff0d1d6b10216f92929 100644 (file)
@@ -1,5 +1,5 @@
 /* V850-specific support for 32-bit ELF
-   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
+   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
    Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -73,11 +73,11 @@ static bfd_boolean v850_elf_section_from_bfd_section
 static void v850_elf_symbol_processing
   PARAMS ((bfd *, asymbol *));
 static bfd_boolean v850_elf_add_symbol_hook
-  PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
+  PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Sym *,
           const char **, flagword *, asection **, bfd_vma *));
 static bfd_boolean v850_elf_link_output_symbol_hook
-  PARAMS ((bfd *, struct bfd_link_info *, const char *,
-          Elf_Internal_Sym *, asection *));
+  PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
+          asection *, struct elf_link_hash_entry *));
 static bfd_boolean v850_elf_section_from_shdr
   PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
 static bfd_boolean v850_elf_gc_sweep_hook
@@ -189,7 +189,7 @@ static reloc_howto_type v850_elf_howto_table[] =
         FALSE),                        /* pcrel_offset */
 
   /* Simple 32bit reloc.  */
-  HOWTO (R_V850_32,                    /* type */
+  HOWTO (R_V850_ABS32,                 /* type */
         0,                             /* rightshift */
         2,                             /* size (0 = byte, 1 = short, 2 = long) */
         32,                            /* bitsize */
@@ -197,7 +197,7 @@ static reloc_howto_type v850_elf_howto_table[] =
         0,                             /* bitpos */
         complain_overflow_dont,        /* complain_on_overflow */
         v850_elf_reloc,                /* special_function */
-        "R_V850_32",                   /* name */
+        "R_V850_ABS32",                /* name */
         FALSE,                         /* partial_inplace */
         0xffffffff,                    /* src_mask */
         0xffffffff,                    /* dst_mask */
@@ -518,6 +518,21 @@ static reloc_howto_type v850_elf_howto_table[] =
        0,                     /* src_mask */
        0,                     /* dst_mask */
        TRUE),                 /* pcrel_offset */
+  
+  /* Simple pc-relative 32bit reloc.  */
+  HOWTO (R_V850_REL32,                 /* type */
+        0,                             /* rightshift */
+        2,                             /* size (0 = byte, 1 = short, 2 = long) */
+        32,                            /* bitsize */
+        TRUE,                          /* pc_relative */
+        0,                             /* bitpos */
+        complain_overflow_dont,        /* complain_on_overflow */
+        v850_elf_reloc,                /* special_function */
+        "R_V850_REL32",                /* name */
+        FALSE,                         /* partial_inplace */
+        0xffffffff,                    /* src_mask */
+        0xffffffff,                    /* dst_mask */
+        FALSE),                        /* pcrel_offset */
 };
 
 /* Map BFD reloc types to V850 ELF reloc types.  */
@@ -538,7 +553,8 @@ static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
   { BFD_RELOC_HI16_S,                     R_V850_HI16_S                 },
   { BFD_RELOC_HI16,                       R_V850_HI16                   },
   { BFD_RELOC_LO16,                       R_V850_LO16                   },
-  { BFD_RELOC_32,                         R_V850_32                     },
+  { BFD_RELOC_32,                         R_V850_ABS32                  },
+  { BFD_RELOC_32_PCREL,                           R_V850_REL32                  },
   { BFD_RELOC_16,                         R_V850_16                     },
   { BFD_RELOC_8,                          R_V850_8                      },
   { BFD_RELOC_V850_SDA_16_16_OFFSET,       R_V850_SDA_16_16_OFFSET       },
@@ -636,13 +652,12 @@ v850_elf_check_relocs (abfd, info, sec, relocs)
   int other = 0;
   const char *common = (const char *)0;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
 #ifdef DEBUG
-  fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
-          bfd_get_section_name (abfd, sec),
-          bfd_archive_filename (abfd));
+  _bfd_error_handler ("v850_elf_check_relocs called for section %A in %B",
+                     sec, abfd);
 #endif
 
   dynobj = elf_hash_table (info)->dynobj;
@@ -672,7 +687,8 @@ v850_elf_check_relocs (abfd, info, sec, relocs)
        case R_V850_HI16_S:
        case R_V850_HI16:
        case R_V850_LO16:
-       case R_V850_32:
+       case R_V850_ABS32:
+       case R_V850_REL32:
        case R_V850_16:
        case R_V850_8:
        case R_V850_CALLT_6_7_OFFSET:
@@ -682,14 +698,14 @@ v850_elf_check_relocs (abfd, info, sec, relocs)
         /* This relocation describes the C++ object vtable hierarchy.
            Reconstruct it for later use during GC.  */
         case R_V850_GNU_VTINHERIT:
-          if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
+          if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
             return FALSE;
           break;
 
         /* This relocation describes which C++ vtable entries
           are actually used.  Record for later use during GC.  */
         case R_V850_GNU_VTENTRY:
-          if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
+          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
             return FALSE;
           break;
 
@@ -876,7 +892,7 @@ find_remembered_hi16s_reloc (addend, already_found)
   /* Extract the address.  */
   addr = match->address;
 
-  /* Remeber if this entry has already been used before.  */
+  /* Remember if this entry has already been used before.  */
   if (already_found)
     * already_found = match->found;
 
@@ -887,7 +903,7 @@ find_remembered_hi16s_reloc (addend, already_found)
 }
 
 /* FIXME:  The code here probably ought to be removed and the code in reloc.c
-   allowed to do its  stuff instead.  At least for most of the relocs, anwyay.  */
+   allowed to do its stuff instead.  At least for most of the relocs, anyway.  */
 
 static bfd_reloc_status_type
 v850_elf_perform_relocation (abfd, r_type, addend, address)
@@ -905,7 +921,8 @@ v850_elf_perform_relocation (abfd, r_type, addend, address)
       /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
       return bfd_reloc_notsupported;
 
-    case R_V850_32:
+    case R_V850_REL32:
+    case R_V850_ABS32:
       bfd_put_32 (abfd, addend, address);
       return bfd_reloc_ok;
 
@@ -1341,10 +1358,10 @@ v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
   /* We handle final linking of some relocs ourselves.  */
 
   /* Is the address of the relocation really within the section?  */
-  if (reloc->address > isection->_cooked_size)
+  if (reloc->address > bfd_get_section_limit (abfd, isection))
     return bfd_reloc_outofrange;
 
-  /* Work out which section the relocation is targetted at and the
+  /* Work out which section the relocation is targeted at and the
      initial relocation command value.  */
 
   if (reloc->howto->pc_relative)
@@ -1466,11 +1483,17 @@ v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
       value = SEXT24 (value);
       break;
 
+    case R_V850_REL32:
+      value -= (input_section->output_section->vma
+               + input_section->output_offset
+               + offset);
+      break;
+
     case R_V850_HI16_S:
     case R_V850_HI16:
     case R_V850_LO16:
     case R_V850_16:
-    case R_V850_32:
+    case R_V850_ABS32:
     case R_V850_8:
       break;
 
@@ -1608,7 +1631,7 @@ v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
@@ -1657,7 +1680,7 @@ v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        {
          sym = local_syms + r_symndx;
          sec = local_sections[r_symndx];
-         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
+         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
 #if 0
          {
            char * name;
@@ -1672,43 +1695,12 @@ v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        }
       else
        {
-         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-
-         while (h->root.type == bfd_link_hash_indirect
-                || h->root.type == bfd_link_hash_warning)
-           h = (struct elf_link_hash_entry *) h->root.u.i.link;
+         bfd_boolean unresolved_reloc, warned;
 
-         if (h->root.type == bfd_link_hash_defined
-             || h->root.type == bfd_link_hash_defweak)
-           {
-             sec = h->root.u.def.section;
-             relocation = (h->root.u.def.value
-                           + sec->output_section->vma
-                           + sec->output_offset);
-#if 0
-             fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
-                      sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
-#endif
-           }
-         else if (h->root.type == bfd_link_hash_undefweak)
-           {
-#if 0
-             fprintf (stderr, "undefined: sec: %s, name: %s\n",
-                      sec->name, h->root.root.string);
-#endif
-             relocation = 0;
-           }
-         else
-           {
-             if (! ((*info->callbacks->undefined_symbol)
-                    (info, h->root.root.string, input_bfd,
-                     input_section, rel->r_offset, TRUE)))
-               return FALSE;
-#if 0
-             fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
-#endif
-             relocation = 0;
-           }
+         RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
+                                  r_symndx, symtab_hdr, sym_hashes,
+                                  h, sec, relocation,
+                                  unresolved_reloc, warned);
        }
 
       /* FIXME: We should use the addend, but the COFF relocations don't.  */
@@ -1853,6 +1845,9 @@ v850_elf_object_p (abfd)
     case E_V850E_ARCH:
       bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e);
       break;
+    case E_V850E1_ARCH:
+      bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e1);
+      break;
     }
   return TRUE;
 }
@@ -1871,6 +1866,7 @@ v850_elf_final_write_processing (abfd, linker)
     default:
     case bfd_mach_v850:  val = E_V850_ARCH; break;
     case bfd_mach_v850e: val = E_V850E_ARCH; break;
+    case bfd_mach_v850e1: val = E_V850E1_ARCH;  break;
     }
 
   elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
@@ -1936,8 +1932,24 @@ v850_elf_merge_private_bfd_data (ibfd, obfd)
 
   if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
       && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
-    _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
-                       bfd_archive_filename (ibfd));
+    {
+      /* Allow v850e1 binaries to be linked with v850e binaries.
+        Set the output binary to v850e.  */
+      if ((in_flags & EF_V850_ARCH) == E_V850E1_ARCH
+         && (out_flags & EF_V850_ARCH) == E_V850E_ARCH)
+       return TRUE;
+
+      if ((in_flags & EF_V850_ARCH) == E_V850E_ARCH
+         && (out_flags & EF_V850_ARCH) == E_V850E1_ARCH)
+       {
+         elf_elfheader (obfd)->e_flags =
+           ((out_flags & ~ EF_V850_ARCH) | E_V850E_ARCH);
+         return TRUE;
+       }
+
+      _bfd_error_handler (_("%B: Architecture mismatch with previous modules"),
+                         ibfd);
+    }
 
   return TRUE;
 }
@@ -1963,6 +1975,7 @@ v850_elf_print_private_bfd_data (abfd, ptr)
     default:
     case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
     case E_V850E_ARCH:  fprintf (file, _("v850e architecture")); break;
+    case E_V850E1_ARCH: fprintf (file, _("v850e1 architecture")); break;
     }
 
   fputc ('\n', file);
@@ -2110,7 +2123,7 @@ static bfd_boolean
 v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
      bfd *abfd;
      struct bfd_link_info *info ATTRIBUTE_UNUSED;
-     const Elf_Internal_Sym *sym;
+     Elf_Internal_Sym *sym;
      const char **namep ATTRIBUTE_UNUSED;
      flagword *flagsp ATTRIBUTE_UNUSED;
      asection **secp;
@@ -2168,12 +2181,12 @@ v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
 }
 
 static bfd_boolean
-v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
-     bfd *abfd ATTRIBUTE_UNUSED;
+v850_elf_link_output_symbol_hook (info, name, sym, input_sec, h)
      struct bfd_link_info *info ATTRIBUTE_UNUSED;
      const char *name ATTRIBUTE_UNUSED;
      Elf_Internal_Sym *sym;
      asection *input_sec;
+     struct elf_link_hash_entry *h ATTRIBUTE_UNUSED;
 {
   /* If we see a common symbol, which implies a relocatable link, then
      if a symbol was in a special common section in an input file, mark
@@ -2496,19 +2509,14 @@ v850_elf_relax_section (abfd, sec, link_info, again)
 
   *again = FALSE;
 
-  if (link_info->relocateable
+  if (link_info->relocatable
       || (sec->flags & SEC_RELOC) == 0
       || sec->reloc_count == 0)
     return TRUE;
 
-  /* If this is the first time we have been called
-     for this section, initialize the cooked size.  */
-  if (sec->_cooked_size == 0)
-    sec->_cooked_size = sec->_raw_size;
-
   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
 
-  internal_relocs = (_bfd_elf32_link_read_relocs
+  internal_relocs = (_bfd_elf_link_read_relocs
                     (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
                      link_info->keep_memory));
   if (internal_relocs == NULL)
@@ -2516,9 +2524,9 @@ v850_elf_relax_section (abfd, sec, link_info, again)
 
   irelend = internal_relocs + sec->reloc_count;
 
-  while (addr < sec->_cooked_size)
+  while (addr < sec->size)
     {
-      toaddr = sec->_cooked_size;
+      toaddr = sec->size;
 
       for (irel = internal_relocs; irel < irelend; irel ++)
        if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN
@@ -2592,12 +2600,7 @@ v850_elf_relax_section (abfd, sec, link_info, again)
                contents = elf_section_data (sec)->this_hdr.contents;
              else
                {
-                 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
-                 if (contents == NULL)
-                   goto error_return;
-
-                 if (! bfd_get_section_contents (abfd, sec, contents,
-                                                 (file_ptr) 0, sec->_raw_size))
+                 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
                    goto error_return;
                }
            }
@@ -2619,7 +2622,7 @@ v850_elf_relax_section (abfd, sec, link_info, again)
          if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL)
            {
              /* Check code for -mlong-calls output. */
-             if (laddr + 16 <= (bfd_vma) sec->_raw_size)
+             if (laddr + 16 <= (bfd_vma) sec->size)
                {
                  insn[0] = bfd_get_16 (abfd, contents + laddr);
                  insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
@@ -2848,7 +2851,7 @@ v850_elf_relax_section (abfd, sec, link_info, again)
          else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP)
            {
              /* Check code for -mlong-jumps output.  */
-             if (laddr + 10 <= (bfd_vma) sec->_raw_size)
+             if (laddr + 10 <= (bfd_vma) sec->size)
                {
                  insn[0] = bfd_get_16 (abfd, contents + laddr);
                  insn[1] = bfd_get_16 (abfd, contents + laddr + 4);
@@ -3090,10 +3093,10 @@ v850_elf_relax_section (abfd, sec, link_info, again)
 #ifdef DEBUG_RELAX
       fprintf (stderr, "relax pad %d shorten %d -> %d\n",
               align_pad_size,
-              sec->_cooked_size,
-              sec->_cooked_size - align_pad_size);
+              sec->size,
+              sec->size - align_pad_size);
 #endif
-      sec->_cooked_size -= align_pad_size;
+      sec->size -= align_pad_size;
     }
 
  finish:
@@ -3115,6 +3118,37 @@ v850_elf_relax_section (abfd, sec, link_info, again)
   result = FALSE;
   goto finish;
 }
+
+static struct bfd_elf_special_section const v850_elf_special_sections[]=
+{
+  { ".sdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_GPREL) },
+  { ".rosdata",          8, -2, SHT_PROGBITS,     (SHF_ALLOC
+                                                   + SHF_V850_GPREL) },
+  { ".sbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_GPREL) },
+  { ".scommon",          8, -2, SHT_V850_SCOMMON, (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_GPREL) },
+  { ".tdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_EPREL) },
+  { ".tbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_EPREL) },
+  { ".tcommon",          8, -2, SHT_V850_TCOMMON, (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_R0REL) },
+  { ".zdata",            6, -2, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_R0REL) },
+  { ".rozdata",          8, -2, SHT_PROGBITS,     (SHF_ALLOC
+                                                   + SHF_V850_R0REL) },
+  { ".zbss",             5, -2, SHT_NOBITS,       (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_R0REL) },
+  { ".zcommon",          8, -2, SHT_V850_ZCOMMON, (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_V850_R0REL) },
+  { ".call_table_data", 16,  0, SHT_PROGBITS,     (SHF_ALLOC
+                                                   + SHF_WRITE) },
+  { ".call_table_text", 16,  0, SHT_PROGBITS,     (SHF_ALLOC + SHF_WRITE
+                                                   + SHF_EXECINSTR) },
+  { NULL,               0,   0, 0,                0 }
+};
 \f
 #define TARGET_LITTLE_SYM                      bfd_elf32_v850_vec
 #define TARGET_LITTLE_NAME                     "elf32-v850"
@@ -3138,6 +3172,7 @@ v850_elf_relax_section (abfd, sec, link_info, again)
 #define elf_backend_fake_sections              v850_elf_fake_sections
 #define elf_backend_gc_mark_hook                v850_elf_gc_mark_hook
 #define elf_backend_gc_sweep_hook               v850_elf_gc_sweep_hook
+#define elf_backend_special_sections           v850_elf_special_sections
 
 #define elf_backend_can_gc_sections 1
 #define elf_backend_rela_normal 1
This page took 0.049726 seconds and 4 git commands to generate.