bfd/
[deliverable/binutils-gdb.git] / bfd / elf32-vax.c
index bf481e459e75d496cb1902f2e607ec991bffbe64..80c5046d289040efbeeb300712c76e1c753fbcbf 100644 (file)
@@ -1,13 +1,13 @@
 /* VAX series support for 32-bit ELF
    Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004, 2005, 2006 Free Software Foundation, Inc.
+   2004, 2005, 2006, 2007, 2008  Free Software Foundation, Inc.
    Contributed by Matt Thomas <matt@3am-software.com>.
 
    This file is part of BFD, the Binary File Descriptor library.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
-#include "bfd.h"
 #include "sysdep.h"
+#include "bfd.h"
 #include "bfdlink.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
@@ -34,13 +35,6 @@ static struct bfd_hash_entry *elf_vax_link_hash_newfunc (struct bfd_hash_entry *
 static struct bfd_link_hash_table *elf_vax_link_hash_table_create (bfd *);
 static bfd_boolean elf_vax_check_relocs (bfd *, struct bfd_link_info *,
                                         asection *, const Elf_Internal_Rela *);
-static asection *elf_vax_gc_mark_hook (asection *, struct bfd_link_info *,
-                                      Elf_Internal_Rela *,
-                                      struct elf_link_hash_entry *,
-                                      Elf_Internal_Sym *);
-static bfd_boolean elf_vax_gc_sweep_hook (bfd *, struct bfd_link_info *,
-                                         asection *,
-                                         const Elf_Internal_Rela *);
 static bfd_boolean elf_vax_adjust_dynamic_symbol (struct bfd_link_info *,
                                                  struct elf_link_hash_entry *);
 static bfd_boolean elf_vax_size_dynamic_sections (bfd *, struct bfd_link_info *);
@@ -329,7 +323,22 @@ reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
   return 0;
 }
 
+static reloc_howto_type *
+reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
+                  const char *r_name)
+{
+  unsigned int i;
+
+  for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
+    if (howto_table[i].name != NULL
+       && strcasecmp (howto_table[i].name, r_name) == 0)
+      return &howto_table[i];
+
+  return NULL;
+}
+
 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
+#define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
 #define ELF_ARCH bfd_arch_vax
 /* end code generated by elf.el */
 \f
@@ -359,7 +368,7 @@ static const bfd_byte elf_vax_plt0_entry[PLT_ENTRY_SIZE] =
 
 static const bfd_byte elf_vax_plt_entry[PLT_ENTRY_SIZE] =
 {
-  0x40, 0x00,          /* .word ^M<r6> */
+  0xfc, 0x0f,          /* .word ^M<r11:r2> */
   0x16,        0xef,           /* jsb L^(pc) */
   0, 0, 0, 0,          /* replaced with offset to start of .plt  */
   0, 0, 0, 0,          /* index into .rela.plt */
@@ -590,8 +599,15 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
       switch (ELF32_R_TYPE (rel->r_info))
        {
        case R_VAX_GOT32:
-         if (h != NULL
-             && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+         BFD_ASSERT (h != NULL);
+         if (h->forced_local
+             || h == elf_hash_table (info)->hgot
+             || h == elf_hash_table (info)->hplt)
+           break;
+
+         /* If this is a local symbol, we resolve it directly without
+            creating a global offset table entry.  */
+         if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
            break;
 
          /* This symbol requires a global offset table entry.  */
@@ -645,10 +661,11 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
                  h->got.refcount++;
                  if (eh->got_addend != (bfd_vma) rel->r_addend)
                    (*_bfd_error_handler)
-                     (_("%s: warning: GOT addend of %ld to `%s' does not match previous GOT addend of %ld"),
-                             bfd_get_filename (abfd), rel->r_addend,
-                             h->root.root.string,
-                             eh->got_addend);
+                     (_("%s: warning: GOT addend of %ld to `%s' does"
+                        " not match previous GOT addend of %ld"),
+                        bfd_get_filename (abfd), rel->r_addend,
+                        h->root.root.string,
+                        eh->got_addend);
 
                }
            }
@@ -664,8 +681,9 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
 
          /* If this is a local symbol, we resolve it directly without
             creating a procedure linkage table entry.  */
-         if (h == NULL)
-           continue;
+         BFD_ASSERT (h != NULL);
+         if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT || h->forced_local)
+           break;
 
          h->needs_plt = 1;
          if (h->plt.refcount == -1)
@@ -693,7 +711,9 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
                && (!info->symbolic
                    || !h->def_regular)))
            {
-             if (h != NULL)
+             if (h != NULL
+                 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+                 && !h->forced_local)
                {
                  /* Make sure a plt entry is created for this symbol if
                     it turns out to be a function defined by a dynamic
@@ -705,11 +725,17 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
                }
              break;
            }
+         /* If this is a local symbol, we can resolve it directly.  */
+         if (h != NULL
+             && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+                 || h->forced_local))
+           break;
+
          /* Fall through.  */
        case R_VAX_8:
        case R_VAX_16:
        case R_VAX_32:
-         if (h != NULL)
+         if (h != NULL && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
            {
              /* Make sure a plt entry is created for this symbol if it
                 turns out to be a function defined by a dynamic object.  */
@@ -729,34 +755,12 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
                 section in dynobj and make room for this reloc.  */
              if (sreloc == NULL)
                {
-                 const char *name;
+                 sreloc = _bfd_elf_make_dynamic_reloc_section
+                   (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
 
-                 name = (bfd_elf_string_from_elf_section
-                         (abfd,
-                          elf_elfheader (abfd)->e_shstrndx,
-                          elf_section_data (sec)->rel_hdr.sh_name));
-                 if (name == NULL)
+                 if (sreloc == NULL)
                    return FALSE;
 
-                 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
-                             && strcmp (bfd_get_section_name (abfd, sec),
-                                        name + 5) == 0);
-
-                 sreloc = bfd_get_section_by_name (dynobj, name);
-                 if (sreloc == NULL)
-                   {
-                     sreloc = bfd_make_section_with_flags (dynobj,
-                                                           name,
-                                                           (SEC_ALLOC
-                                                            | SEC_LOAD
-                                                            | SEC_HAS_CONTENTS
-                                                            | SEC_IN_MEMORY
-                                                            | SEC_LINKER_CREATED
-                                                            | SEC_READONLY));
-                     if (sreloc == NULL
-                         || !bfd_set_section_alignment (dynobj, sreloc, 2))
-                       return FALSE;
-                   }
                  if (sec->flags & SEC_READONLY)
                    info->flags |= DF_TEXTREL;
                }
@@ -812,7 +816,9 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
          /* This relocation describes which C++ vtable entries are actually
             used.  Record for later use during GC.  */
        case R_VAX_GNU_VTENTRY:
-         if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
+         BFD_ASSERT (h != NULL);
+         if (h != NULL
+             && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
            return FALSE;
          break;
 
@@ -829,38 +835,20 @@ elf_vax_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
 
 static asection *
 elf_vax_gc_mark_hook (asection *sec,
-                     struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                     struct bfd_link_info *info,
                      Elf_Internal_Rela *rel,
                      struct elf_link_hash_entry *h,
                      Elf_Internal_Sym *sym)
 {
   if (h != NULL)
-    {
-      switch (ELF32_R_TYPE (rel->r_info))
-       {
-       case R_VAX_GNU_VTINHERIT:
-       case R_VAX_GNU_VTENTRY:
-         break;
-
-       default:
-         switch (h->root.type)
-           {
-           default:
-             break;
-
-           case bfd_link_hash_defined:
-           case bfd_link_hash_defweak:
-             return h->root.u.def.section;
-
-           case bfd_link_hash_common:
-             return h->root.u.c.p->section;
-           }
-       }
-    }
-  else
-    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
-
-  return NULL;
+    switch (ELF32_R_TYPE (rel->r_info))
+      {
+      case R_VAX_GNU_VTINHERIT:
+      case R_VAX_GNU_VTENTRY:
+       return NULL;
+      }
+
+  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
 }
 
 /* Update the got entry reference counts for the section being removed.  */
@@ -874,6 +862,9 @@ elf_vax_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
   const Elf_Internal_Rela *rel, *relend;
   bfd *dynobj;
 
+  if (info->relocatable)
+    return TRUE;
+
   dynobj = elf_hash_table (info)->dynobj;
   if (dynobj == NULL)
     return TRUE;
@@ -935,7 +926,6 @@ elf_vax_adjust_dynamic_symbol (info, h)
 {
   bfd *dynobj;
   asection *s;
-  unsigned int power_of_two;
 
   dynobj = elf_hash_table (info)->dynobj;
 
@@ -1089,28 +1079,7 @@ elf_vax_adjust_dynamic_symbol (info, h)
       h->needs_copy = 1;
     }
 
-  /* We need to figure out the alignment required for this symbol.  I
-     have no idea how ELF linkers handle this.  */
-  power_of_two = bfd_log2 (h->size);
-  if (power_of_two > 3)
-    power_of_two = 3;
-
-  /* Apply the required alignment.  */
-  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
-  if (power_of_two > bfd_get_section_alignment (dynobj, s))
-    {
-      if (!bfd_set_section_alignment (dynobj, s, power_of_two))
-       return FALSE;
-    }
-
-  /* Define the symbol as being at this point in the section.  */
-  h->root.u.def.section = s;
-  h->root.u.def.value = s->size;
-
-  /* Increment the section size to make room for the symbol.  */
-  s->size += h->size;
-
-  return TRUE;
+  return _bfd_elf_adjust_dynamic_copy (h, s);
 }
 
 /* Set the sizes of the dynamic sections.  */
@@ -1194,7 +1163,7 @@ elf_vax_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
          /* Remember whether there is a PLT.  */
          plt = s->size != 0;
        }
-      else if (strncmp (name, ".rela", 5) == 0)
+      else if (CONST_STRNEQ (name, ".rela"))
        {
          if (s->size != 0)
            {
@@ -1226,7 +1195,7 @@ elf_vax_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
              s->reloc_count = 0;
            }
        }
-      else if (strncmp (name, ".got", 4) != 0
+      else if (! CONST_STRNEQ (name, ".got")
               && strcmp (name, ".dynbss") != 0)
        {
          /* It's not one of our sections, so don't allocate space.  */
@@ -1353,7 +1322,8 @@ elf_vax_instantiate_got_entries (struct elf_link_hash_entry *h, PTR infoptr)
   srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
 
   if (!elf_hash_table (info)->dynamic_sections_created
-      || (info->shared && info->symbolic))
+      || (info->shared && info->symbolic)
+      || h->forced_local)
     {
       h->got.refcount = 0;
       h->got.offset = (bfd_vma) -1;
@@ -1362,6 +1332,8 @@ elf_vax_instantiate_got_entries (struct elf_link_hash_entry *h, PTR infoptr)
     }
   else if (h->got.refcount > 0)
     {
+      bfd_boolean dyn;
+
       /* Make sure this symbol is output as a dynamic symbol.  */
       if (h->dynindx == -1)
        {
@@ -1369,9 +1341,15 @@ elf_vax_instantiate_got_entries (struct elf_link_hash_entry *h, PTR infoptr)
            return FALSE;
        }
 
+      dyn = elf_hash_table (info)->dynamic_sections_created;
       /* Allocate space in the .got and .rela.got sections.  */
-      sgot->size += 4;
-      srelgot->size += sizeof (Elf32_External_Rela);
+      if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+         && (info->shared
+             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
+       {
+         sgot->size += 4;
+         srelgot->size += sizeof (Elf32_External_Rela);
+       }
     }
 
   return TRUE;
@@ -1402,9 +1380,6 @@ elf_vax_relocate_section (bfd *output_bfd,
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
 
-  if (info->relocatable)
-    return TRUE;
-
   dynobj = elf_hash_table (info)->dynobj;
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (input_bfd);
@@ -1436,7 +1411,6 @@ elf_vax_relocate_section (bfd *output_bfd,
        }
       howto = howto_table + r_type;
 
-      /* This is a final link.  */
       r_symndx = ELF32_R_SYM (rel->r_info);
       h = NULL;
       sym = NULL;
@@ -1461,10 +1435,11 @@ elf_vax_relocate_section (bfd *output_bfd,
              || h->root.type == bfd_link_hash_defweak)
              && ((r_type == R_VAX_PLT32
                   && h->plt.offset != (bfd_vma) -1
+                  && !h->forced_local
                   && elf_hash_table (info)->dynamic_sections_created)
                  || (r_type == R_VAX_GOT32
-                     && strcmp (h->root.root.string,
-                                "_GLOBAL_OFFSET_TABLE_") != 0
+                     && h->got.offset != (bfd_vma) -1
+                     && !h->forced_local
                      && elf_hash_table (info)->dynamic_sections_created
                      && (! info->shared
                          || (! info->symbolic && h->dynindx != -1)
@@ -1482,28 +1457,43 @@ elf_vax_relocate_section (bfd *output_bfd,
                              && h->def_dynamic))
                      && (r_type == R_VAX_8
                          || r_type == R_VAX_16
-                         || r_type == R_VAX_32
-                         || r_type == R_VAX_PC8
-                         || r_type == R_VAX_PC16
-                         || r_type == R_VAX_PC32))))
+                         || r_type == R_VAX_32))))
            /* In these cases, we don't need the relocation
               value.  We check specially because in some
               obscure cases sec->output_section will be NULL.  */
            relocation = 0;
        }
 
+      if (sec != NULL && elf_discarded_section (sec))
+       {
+         /* For relocs against symbols from removed linkonce sections,
+            or sections discarded by a linker script, we just want the
+            section contents zeroed.  Avoid any special processing.  */
+         _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
+         rel->r_info = 0;
+         rel->r_addend = 0;
+         continue;
+       }
+
+      if (info->relocatable)
+       continue;
+
       switch (r_type)
        {
        case R_VAX_GOT32:
          /* Relocation is to the address of the entry for this symbol
             in the global offset table.  */
-         if (h == NULL || h->got.offset == (bfd_vma) -1)
+         if (h == NULL
+             || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+             || h->got.offset == (bfd_vma) -1
+             || h->forced_local)
            break;
 
          /* Relocation is the offset of the entry for this symbol in
             the global offset table.  */
 
          {
+           bfd_boolean dyn;
            bfd_vma off;
 
            if (sgot == NULL)
@@ -1517,9 +1507,10 @@ elf_vax_relocate_section (bfd *output_bfd,
            BFD_ASSERT (off != (bfd_vma) -1);
            BFD_ASSERT (off < sgot->size);
 
-           if (info->shared
-               && h->dynindx == -1
-               && h->def_regular)
+           dyn = elf_hash_table (info)->dynamic_sections_created;
+           if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
+               || (info->shared
+                   && SYMBOL_REFERENCES_LOCAL (info, h)))
              {
                /* The symbol was forced to be local
                   because of a version file..  We must initialize
@@ -1553,13 +1544,23 @@ elf_vax_relocate_section (bfd *output_bfd,
          }
          break;
 
+       case R_VAX_PC32:
+         /* If we are creating an executable and the function this
+            reloc refers to is in a shared lib, then we made a PLT
+            entry for this symbol and need to handle the reloc like
+            a PLT reloc.  */
+         if (info->shared)
+            goto r_vax_pc32_shared;
+         /* Fall through.  */
        case R_VAX_PLT32:
          /* Relocation is to the entry for this symbol in the
             procedure linkage table.  */
 
          /* Resolve a PLTxx reloc against a local symbol directly,
             without using the procedure linkage table.  */
-         if (h == NULL)
+         if (h == NULL
+             || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+             || h->forced_local)
            break;
 
          if (h->plt.offset == (bfd_vma) -1
@@ -1612,8 +1613,10 @@ elf_vax_relocate_section (bfd *output_bfd,
 
        case R_VAX_PC8:
        case R_VAX_PC16:
-       case R_VAX_PC32:
-         if (h == NULL)
+       r_vax_pc32_shared:
+         if (h == NULL
+             || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+             || h->forced_local)
            break;
          /* Fall through.  */
        case R_VAX_8:
@@ -1625,8 +1628,9 @@ elf_vax_relocate_section (bfd *output_bfd,
              && ((r_type != R_VAX_PC8
                   && r_type != R_VAX_PC16
                   && r_type != R_VAX_PC32)
-                 || (!info->symbolic
-                     || !h->def_regular)))
+                 || ((input_section->flags & SEC_CODE)
+                     && (!info->symbolic
+                         || (!h->def_regular && h->type != STT_SECTION)))))
            {
              Elf_Internal_Rela outrel;
              bfd_byte *loc;
@@ -1637,22 +1641,10 @@ elf_vax_relocate_section (bfd *output_bfd,
                 time.  */
              if (sreloc == NULL)
                {
-                 const char *name;
-
-                 name = (bfd_elf_string_from_elf_section
-                         (input_bfd,
-                          elf_elfheader (input_bfd)->e_shstrndx,
-                          elf_section_data (input_section)->rel_hdr.sh_name));
-                 if (name == NULL)
+                 sreloc = _bfd_elf_get_dynamic_reloc_section
+                   (input_bfd, input_section, /*rela?*/ TRUE);
+                 if (sreloc == NULL)
                    return FALSE;
-
-                 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
-                             && strcmp (bfd_get_section_name (input_bfd,
-                                                              input_section),
-                                        name + 5) == 0);
-
-                 sreloc = bfd_get_section_by_name (dynobj, name);
-                 BFD_ASSERT (sreloc != NULL);
                }
 
              skip = FALSE;
@@ -1705,9 +1697,21 @@ elf_vax_relocate_section (bfd *output_bfd,
                        {
                          asection *osec;
 
+                         /* We are turning this relocation into one
+                            against a section symbol.  It would be
+                            proper to subtract the symbol's value,
+                            osec->vma, from the emitted reloc addend,
+                            but ld.so expects buggy relocs.  */
                          osec = sec->output_section;
                          indx = elf_section_data (osec)->dynindx;
-                         BFD_ASSERT (indx > 0);
+                         if (indx == 0)
+                           {
+                             struct elf_link_hash_table *htab;
+                             htab = elf_hash_table (info);
+                             osec = htab->text_index_section;
+                             indx = elf_section_data (osec)->dynindx;
+                           }
+                         BFD_ASSERT (indx != 0);
                        }
 
                      outrel.r_info = ELF32_R_INFO (indx, r_type);
@@ -2087,6 +2091,7 @@ elf_vax_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
                                        elf_vax_adjust_dynamic_symbol
 #define elf_backend_size_dynamic_sections \
                                        elf_vax_size_dynamic_sections
+#define elf_backend_init_index_section _bfd_elf_init_1_index_section
 #define elf_backend_relocate_section   elf_vax_relocate_section
 #define elf_backend_finish_dynamic_symbol \
                                        elf_vax_finish_dynamic_symbol
This page took 0.030868 seconds and 4 git commands to generate.