PR ld/1540
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
index 855b98511688750cfbee3457bd4a37983b8b5eb1..061a9cb32d88d6bf70bee1436a1f2ac0489dc67f 100644 (file)
@@ -1,6 +1,6 @@
 /* Intel 80386/80486-specific support for 32-bit ELF
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-   2003, 2004 Free Software Foundation, Inc.
+   2003, 2004, 2005 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
 
    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #include "bfd.h"
 #include "sysdep.h"
 #include "bfdlink.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
+#include "elf-vxworks.h"
 
 /* 386 uses REL relocations instead of RELA.  */
 #define USE_REL        1
@@ -471,15 +472,15 @@ elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 #define PLT_ENTRY_SIZE 16
 
 /* The first entry in an absolute procedure linkage table looks like
-   this.  See the SVR4 ABI i386 supplement to see how this works.  */
+   this.  See the SVR4 ABI i386 supplement to see how this works.
+   Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
 
-static const bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] =
+static const bfd_byte elf_i386_plt0_entry[12] =
 {
   0xff, 0x35,  /* pushl contents of address */
   0, 0, 0, 0,  /* replaced with address of .got + 4.  */
   0xff, 0x25,  /* jmp indirect */
-  0, 0, 0, 0,  /* replaced with address of .got + 8.  */
-  0, 0, 0, 0   /* pad out to 16 bytes.  */
+  0, 0, 0, 0   /* replaced with address of .got + 8.  */
 };
 
 /* Subsequent entries in an absolute procedure linkage table look like
@@ -495,13 +496,13 @@ static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] =
   0, 0, 0, 0   /* replaced with offset to start of .plt.  */
 };
 
-/* The first entry in a PIC procedure linkage table look like this.  */
+/* The first entry in a PIC procedure linkage table look like this.
+   Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte.  */
 
-static const bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] =
+static const bfd_byte elf_i386_pic_plt0_entry[12] =
 {
   0xff, 0xb3, 4, 0, 0, 0,      /* pushl 4(%ebx) */
-  0xff, 0xa3, 8, 0, 0, 0,      /* jmp *8(%ebx) */
-  0, 0, 0, 0                   /* pad out to 16 bytes.  */
+  0xff, 0xa3, 8, 0, 0, 0       /* jmp *8(%ebx) */
 };
 
 /* Subsequent entries in a PIC procedure linkage table look like this.  */
@@ -516,6 +517,12 @@ static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] =
   0, 0, 0, 0   /* replaced with offset to start of .plt.  */
 };
 
+/* On VxWorks, the .rel.plt.unloaded section has absolute relocations
+   for the PLTResolve stub and then for each PLT entry.  */
+#define PLTRESOLVE_RELOCS_SHLIB 0
+#define PLTRESOLVE_RELOCS 2
+#define PLT_NON_JUMP_SLOT_RELOCS 2
+
 /* The i386 linker needs to keep track of the number of relocs that it
    decides to copy as dynamic relocs in check_relocs for each symbol.
    This is so that it can later discard them if they are found to be
@@ -596,6 +603,18 @@ struct elf_i386_link_hash_table
   asection *sdynbss;
   asection *srelbss;
 
+  /* The (unloaded but important) .rel.plt.unloaded section on VxWorks.  */
+  asection *srelplt2;
+
+  /* Short-cuts to frequently used symbols for VxWorks targets.  */
+  struct elf_link_hash_entry *hgot, *hplt;
+
+  /* True if the target system is VxWorks.  */
+  int is_vxworks;
+
+  /* Value used to fill the last word of the first plt entry.  */
+  bfd_byte plt0_pad_byte;
+
   union {
     bfd_signed_vma refcount;
     bfd_vma offset;
@@ -668,6 +687,11 @@ elf_i386_link_hash_table_create (bfd *abfd)
   ret->srelbss = NULL;
   ret->tls_ldm_got.refcount = 0;
   ret->sym_sec.abfd = NULL;
+  ret->is_vxworks = 0;
+  ret->srelplt2 = NULL;
+  ret->hgot = NULL;
+  ret->hplt = NULL;
+  ret->plt0_pad_byte = 0;
 
   return &ret->elf.root;
 }
@@ -689,12 +713,13 @@ create_got_section (bfd *dynobj, struct bfd_link_info *info)
   if (!htab->sgot || !htab->sgotplt)
     abort ();
 
-  htab->srelgot = bfd_make_section (dynobj, ".rel.got");
+  htab->srelgot = bfd_make_section_with_flags (dynobj, ".rel.got",
+                                              (SEC_ALLOC | SEC_LOAD
+                                               | SEC_HAS_CONTENTS
+                                               | SEC_IN_MEMORY
+                                               | SEC_LINKER_CREATED
+                                               | SEC_READONLY));
   if (htab->srelgot == NULL
-      || ! bfd_set_section_flags (dynobj, htab->srelgot,
-                                 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
-                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED
-                                  | SEC_READONLY))
       || ! bfd_set_section_alignment (dynobj, htab->srelgot, 2))
     return FALSE;
   return TRUE;
@@ -708,6 +733,9 @@ static bfd_boolean
 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
 {
   struct elf_i386_link_hash_table *htab;
+  asection * s;
+  int flags;
+  const struct elf_backend_data *bed = get_elf_backend_data (dynobj);
 
   htab = elf_i386_hash_table (info);
   if (!htab->sgot && !create_got_section (dynobj, info))
@@ -726,13 +754,25 @@ elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
       || (!info->shared && !htab->srelbss))
     abort ();
 
+  if (htab->is_vxworks && !info->shared)
+    {
+      s = bfd_make_section (dynobj, ".rel.plt.unloaded");
+      flags = (SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_READONLY
+             | SEC_LINKER_CREATED);
+      if (s == NULL
+        || ! bfd_set_section_flags (dynobj, s, flags)
+        || ! bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
+       return FALSE;
+      htab->srelplt2 = s;
+    }
+
   return TRUE;
 }
 
 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
 
 static void
-elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
+elf_i386_copy_indirect_symbol (struct bfd_link_info *info,
                               struct elf_link_hash_entry *dir,
                               struct elf_link_hash_entry *ind)
 {
@@ -748,10 +788,7 @@ elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
          struct elf_i386_dyn_relocs **pp;
          struct elf_i386_dyn_relocs *p;
 
-         if (ind->root.type == bfd_link_hash_indirect)
-           abort ();
-
-         /* Add reloc counts against the weak sym to the strong sym
+         /* Add reloc counts against the indirect sym to the direct sym
             list.  Merge any entries against the same section.  */
          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
            {
@@ -784,18 +821,19 @@ elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
 
   if (ELIMINATE_COPY_RELOCS
       && ind->root.type != bfd_link_hash_indirect
-      && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
-    /* If called to transfer flags for a weakdef during processing
-       of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
-       We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
-    dir->elf_link_hash_flags |=
-      (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
-                                  | ELF_LINK_HASH_REF_REGULAR
-                                  | ELF_LINK_HASH_REF_REGULAR_NONWEAK
-                                  | ELF_LINK_HASH_NEEDS_PLT
-                                  | ELF_LINK_POINTER_EQUALITY_NEEDED));
+      && dir->dynamic_adjusted)
+    {
+      /* If called to transfer flags for a weakdef during processing
+        of elf_adjust_dynamic_symbol, don't copy non_got_ref.
+        We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
+      dir->ref_dynamic |= ind->ref_dynamic;
+      dir->ref_regular |= ind->ref_regular;
+      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
+      dir->needs_plt |= ind->needs_plt;
+      dir->pointer_equality_needed |= ind->pointer_equality_needed;
+    }
   else
-    _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
+    _bfd_elf_link_hash_copy_indirect (info, dir, ind);
 }
 
 static int
@@ -870,7 +908,12 @@ elf_i386_check_relocs (bfd *abfd,
       if (r_symndx < symtab_hdr->sh_info)
        h = NULL;
       else
-       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+       {
+         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;
+       }
 
       r_type = elf_i386_tls_transition (info, r_type, h == NULL);
 
@@ -893,7 +936,7 @@ elf_i386_check_relocs (bfd *abfd,
          if (h == NULL)
            continue;
 
-         h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
+         h->needs_plt = 1;
          h->plt.refcount += 1;
          break;
 
@@ -1018,13 +1061,13 @@ elf_i386_check_relocs (bfd *abfd,
                 sections have not yet been mapped to output sections.
                 Tentatively set the flag for now, and correct in
                 adjust_dynamic_symbol.  */
-             h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
+             h->non_got_ref = 1;
 
              /* We may need a .plt entry if the function this reloc
                 refers to is in a shared lib.  */
              h->plt.refcount += 1;
              if (r_type != R_386_PC32)
-               h->elf_link_hash_flags |= ELF_LINK_POINTER_EQUALITY_NEEDED;
+               h->pointer_equality_needed = 1;
            }
 
          /* If we are creating a shared library, and this is a reloc
@@ -1054,15 +1097,13 @@ elf_i386_check_relocs (bfd *abfd,
                   || (h != NULL
                       && (! info->symbolic
                           || h->root.type == bfd_link_hash_defweak
-                          || (h->elf_link_hash_flags
-                              & ELF_LINK_HASH_DEF_REGULAR) == 0))))
+                          || !h->def_regular))))
              || (ELIMINATE_COPY_RELOCS
                  && !info->shared
                  && (sec->flags & SEC_ALLOC) != 0
                  && h != NULL
                  && (h->root.type == bfd_link_hash_defweak
-                     || (h->elf_link_hash_flags
-                         & ELF_LINK_HASH_DEF_REGULAR) == 0)))
+                     || !h->def_regular)))
            {
              struct elf_i386_dyn_relocs *p;
              struct elf_i386_dyn_relocs **head;
@@ -1099,13 +1140,14 @@ elf_i386_check_relocs (bfd *abfd,
                    {
                      flagword flags;
 
-                     sreloc = bfd_make_section (dynobj, name);
                      flags = (SEC_HAS_CONTENTS | SEC_READONLY
                               | SEC_IN_MEMORY | SEC_LINKER_CREATED);
                      if ((sec->flags & SEC_ALLOC) != 0)
                        flags |= SEC_ALLOC | SEC_LOAD;
+                     sreloc = bfd_make_section_with_flags (dynobj,
+                                                           name,
+                                                           flags);
                      if (sreloc == NULL
-                         || ! bfd_set_section_flags (dynobj, sreloc, flags)
                          || ! bfd_set_section_alignment (dynobj, sreloc, 2))
                        return FALSE;
                    }
@@ -1120,6 +1162,7 @@ elf_i386_check_relocs (bfd *abfd,
                }
              else
                {
+                 void **vpp;
                  /* Track dynamic relocs needed for local syms too.
                     We really need local syms available to do this
                     easily.  Oh well.  */
@@ -1130,8 +1173,8 @@ elf_i386_check_relocs (bfd *abfd,
                  if (s == NULL)
                    return FALSE;
 
-                 head = ((struct elf_i386_dyn_relocs **)
-                         &elf_section_data (s)->local_dynrel);
+                 vpp = &elf_section_data (s)->local_dynrel;
+                 head = (struct elf_i386_dyn_relocs **)vpp;
                }
 
              p = *head;
@@ -1249,6 +1292,9 @@ elf_i386_gc_sweep_hook (bfd *abfd,
          struct elf_i386_dyn_relocs *p;
 
          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;
          eh = (struct elf_i386_link_hash_entry *) h;
 
          for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
@@ -1326,7 +1372,7 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
      will fill in the contents of the procedure linkage table later,
      when we know the address of the .got section.  */
   if (h->type == STT_FUNC
-      || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
+      || h->needs_plt)
     {
       if (h->plt.refcount <= 0
          || SYMBOL_CALLS_LOCAL (info, h)
@@ -1339,7 +1385,7 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
             such a case, we don't actually need to build a procedure
             linkage table, and we can just do a PC32 reloc instead.  */
          h->plt.offset = (bfd_vma) -1;
-         h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
+         h->needs_plt = 0;
        }
 
       return TRUE;
@@ -1355,16 +1401,14 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
   /* If this is a weak symbol, and there is a real definition, the
      processor independent code will have arranged for us to see the
      real definition first, and we can just use the same value.  */
-  if (h->weakdef != NULL)
+  if (h->u.weakdef != NULL)
     {
-      BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
-                 || h->weakdef->root.type == bfd_link_hash_defweak);
-      h->root.u.def.section = h->weakdef->root.u.def.section;
-      h->root.u.def.value = h->weakdef->root.u.def.value;
+      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
+                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
+      h->root.u.def.section = h->u.weakdef->root.u.def.section;
+      h->root.u.def.value = h->u.weakdef->root.u.def.value;
       if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
-       h->elf_link_hash_flags
-         = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
-            | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
+       h->non_got_ref = h->u.weakdef->non_got_ref;
       return TRUE;
     }
 
@@ -1380,17 +1424,23 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
 
   /* If there are no references to this symbol that do not use the
      GOT, we don't need to generate a copy reloc.  */
-  if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
+  if (!h->non_got_ref)
     return TRUE;
 
   /* If -z nocopyreloc was given, we won't generate them either.  */
   if (info->nocopyreloc)
     {
-      h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
+      h->non_got_ref = 0;
       return TRUE;
     }
 
-  if (ELIMINATE_COPY_RELOCS)
+  htab = elf_i386_hash_table (info);
+
+  /* If there aren't any dynamic relocs in read-only sections, then
+     we can keep the dynamic relocs and avoid the copy reloc.  This
+     doesn't work on VxWorks, where we can not have dynamic relocations
+     (other than copy and jump slot relocations) in an executable.  */
+  if (ELIMINATE_COPY_RELOCS && !htab->is_vxworks)
     {
       struct elf_i386_link_hash_entry * eh;
       struct elf_i386_dyn_relocs *p;
@@ -1403,15 +1453,20 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
            break;
        }
 
-      /* If we didn't find any dynamic relocs in read-only sections, then
-        we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
       if (p == NULL)
        {
-         h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
+         h->non_got_ref = 0;
          return TRUE;
        }
     }
 
+  if (h->size == 0)
+    {
+      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
+                            h->root.root.string);
+      return TRUE;
+    }
+
   /* We must allocate the symbol in our .dynbss section, which will
      become part of the .bss section of the executable.  There will be
      an entry for this symbol in the .dynsym section.  The dynamic
@@ -1422,15 +1477,13 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
      both the dynamic object and the regular object will refer to the
      same memory location for the variable.  */
 
-  htab = elf_i386_hash_table (info);
-
   /* We must generate a R_386_COPY reloc to tell the dynamic linker to
      copy the initial value out of the dynamic object and into the
      runtime process image.  */
   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
     {
       htab->srelbss->size += sizeof (Elf32_External_Rel);
-      h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
+      h->needs_copy = 1;
     }
 
   /* We need to figure out the alignment required for this symbol.  I
@@ -1487,7 +1540,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       /* Make sure this symbol is output as a dynamic symbol.
         Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
-         && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+         && !h->forced_local)
        {
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
@@ -1511,7 +1564,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
             pointers compare as equal between the normal executable and
             the shared library.  */
          if (! info->shared
-             && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+             && !h->def_regular)
            {
              h->root.u.def.section = s;
              h->root.u.def.value = h->plt.offset;
@@ -1526,17 +1579,37 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
          /* We also need to make an entry in the .rel.plt section.  */
          htab->srelplt->size += sizeof (Elf32_External_Rel);
+
+         if (htab->is_vxworks && !info->shared)
+           {
+             /* VxWorks has a second set of relocations for each PLT entry
+                in executables.  They go in a separate relocation section,
+                which is processed by the kernel loader.  */
+
+             /* There are two relocations for the initial PLT entry: an
+                R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
+                R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
+
+             if (h->plt.offset == PLT_ENTRY_SIZE)
+               htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
+
+             /* There are two extra relocations for each subsequent PLT entry:
+                an R_386_32 relocation for the GOT entry, and an R_386_32
+                relocation for the PLT entry.  */
+
+             htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2);
+           }
        }
       else
        {
          h->plt.offset = (bfd_vma) -1;
-         h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
+         h->needs_plt = 0;
        }
     }
   else
     {
       h->plt.offset = (bfd_vma) -1;
-      h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
+      h->needs_plt = 0;
     }
 
   /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary,
@@ -1555,7 +1628,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       /* Make sure this symbol is output as a dynamic symbol.
         Undefined weak syms won't yet be marked as dynamic.  */
       if (h->dynindx == -1
-         && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+         && !h->forced_local)
        {
          if (! bfd_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
@@ -1634,9 +1707,9 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
         symbols which turn out to need copy relocs or are not
         dynamic.  */
 
-      if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
-         && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
-              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+      if (!h->non_got_ref
+         && ((h->def_dynamic
+              && !h->def_regular)
              || (htab->elf.dynamic_sections_created
                  && (h->root.type == bfd_link_hash_undefweak
                      || h->root.type == bfd_link_hash_undefined))))
@@ -1644,7 +1717,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          /* Make sure this symbol is output as a dynamic symbol.
             Undefined weak syms won't yet be marked as dynamic.  */
          if (h->dynindx == -1
-             && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+             && !h->forced_local)
            {
              if (! bfd_elf_link_record_dynamic_symbol (info, h))
                return FALSE;
@@ -1748,8 +1821,8 @@ elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        {
          struct elf_i386_dyn_relocs *p;
 
-         for (p = *((struct elf_i386_dyn_relocs **)
-                    &elf_section_data (s)->local_dynrel);
+         for (p = ((struct elf_i386_dyn_relocs *)
+                    elf_section_data (s)->local_dynrel);
               p != NULL;
               p = p->next)
            {
@@ -1816,6 +1889,27 @@ elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   else
     htab->tls_ldm_got.offset = -1;
 
+  if (htab->is_vxworks)
+    {
+      /* Save the GOT and PLT symbols in the hash table for easy access.
+        Mark them as having relocations; they might not, but we won't
+        know for sure until we build the GOT in finish_dynamic_symbol.  */
+
+      htab->hgot = elf_link_hash_lookup (elf_hash_table (info),
+                                       "_GLOBAL_OFFSET_TABLE_",
+                                       FALSE, FALSE, FALSE);
+      if (htab->hgot)
+       htab->hgot->indx = -2;
+      htab->hplt = elf_link_hash_lookup (elf_hash_table (info),
+                                       "_PROCEDURE_LINKAGE_TABLE_",
+                                       FALSE, FALSE, FALSE);
+      if (htab->hplt)
+       htab->hplt->indx = -2;
+
+      if (htab->is_vxworks && htab->hplt && htab->splt->flags & SEC_CODE)
+       htab->hplt->type = STT_FUNC;
+    }
+
   /* Allocate global sym .plt and .got entries, and space for global
      sym dynamic relocs.  */
   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
@@ -1825,19 +1919,28 @@ elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   relocs = FALSE;
   for (s = dynobj->sections; s != NULL; s = s->next)
     {
+      bfd_boolean strip_section = TRUE;
+
       if ((s->flags & SEC_LINKER_CREATED) == 0)
        continue;
 
       if (s == htab->splt
          || s == htab->sgot
-         || s == htab->sgotplt)
+         || s == htab->sgotplt
+         || s == htab->sdynbss)
        {
          /* Strip this section if we don't need it; see the
             comment below.  */
+         /* We'd like to strip these sections if they aren't needed, but if
+            we've exported dynamic symbols from them we must leave them.
+            It's too late to tell BFD to get rid of the symbols.  */
+
+         if (htab->hplt != NULL)
+           strip_section = FALSE;
        }
       else if (strncmp (bfd_get_section_name (dynobj, s), ".rel", 4) == 0)
        {
-         if (s->size != 0 && s != htab->srelplt)
+         if (s->size != 0 && s != htab->srelplt && s != htab->srelplt2)
            relocs = TRUE;
 
          /* We use the reloc_count field as a counter if we need
@@ -1861,11 +1964,14 @@ elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
             adjust_dynamic_symbol is called, and it is that
             function which decides whether anything needs to go
             into these sections.  */
-
-         _bfd_strip_section_from_output (info, s);
+         if (strip_section)
+           s->flags |= SEC_EXCLUDE;
          continue;
        }
 
+      if ((s->flags & SEC_HAS_CONTENTS) == 0)
+       continue;
+
       /* Allocate memory for the section contents.  We use bfd_zalloc
         here in case unused entries are not reclaimed before the
         section's contents are written out.  This should not happen,
@@ -2177,6 +2283,21 @@ elf_i386_relocate_section (bfd *output_bfd,
                                   unresolved_reloc, warned);
        }
 
+      if (r_symndx == 0)
+       {
+       /* r_symndx will be zero only for relocs against symbols from
+          removed linkonce sections, or sections discarded by a linker
+          script.  For these relocs, we just want the section contents
+          zeroed.  Avoid any special processing in the switch below.  */
+         r_type = R_386_NONE;
+
+         relocation = 0;
+         if (howto->pc_relative)
+           relocation = (input_section->output_section->vma
+                         + input_section->output_offset
+                         + rel->r_offset);
+       }
+
       switch (r_type)
        {
        case R_386_GOT32:
@@ -2274,6 +2395,23 @@ elf_i386_relocate_section (bfd *output_bfd,
          /* Relocation is relative to the start of the global offset
             table.  */
 
+         /* Check to make sure it isn't a protected function symbol
+            for shared library since it may not be local when used
+            as function address.  */
+         if (info->shared
+             && !info->executable
+             && h
+             && h->def_regular
+             && h->type == STT_FUNC
+             && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
+           {
+             (*_bfd_error_handler)
+               (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"),
+                input_bfd, h->root.root.string);
+             bfd_set_error (bfd_error_bad_value);
+             return FALSE;
+           }
+
          /* Note that sgot is not involved in this
             calculation.  We always want the start of .got.plt.  If we
             defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
@@ -2316,21 +2454,6 @@ elf_i386_relocate_section (bfd *output_bfd,
 
        case R_386_32:
        case R_386_PC32:
-         /* r_symndx will be zero only for relocs against symbols
-            from removed linkonce sections, or sections discarded by
-            a linker script.  */
-         if (r_symndx == 0)
-           {
-             /* Zero the section contents.  eh_frame generated by old
-                versions of gcc isn't edited by elf-eh-frame.c, so
-                FDEs for discarded linkonce functions might remain.
-                Putting zeros here will zero such FDE's address range.
-                This is a hint to unwinders and other consumers of
-                exception handling info that the FDE is invalid.  */
-             bfd_put_32 (input_bfd, 0, contents + rel->r_offset);
-             continue;
-           }
-
          if ((input_section->flags & SEC_ALLOC) == 0)
            break;
 
@@ -2344,11 +2467,9 @@ elf_i386_relocate_section (bfd *output_bfd,
                  && !info->shared
                  && h != NULL
                  && h->dynindx != -1
-                 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
-                 && (((h->elf_link_hash_flags
-                       & ELF_LINK_HASH_DEF_DYNAMIC) != 0
-                      && (h->elf_link_hash_flags
-                          & ELF_LINK_HASH_DEF_REGULAR) == 0)
+                 && !h->non_got_ref
+                 && ((h->def_dynamic
+                      && !h->def_regular)
                      || h->root.type == bfd_link_hash_undefweak
                      || h->root.type == bfd_link_hash_undefined)))
            {
@@ -2381,8 +2502,7 @@ elf_i386_relocate_section (bfd *output_bfd,
                       && (r_type == R_386_PC32
                           || !info->shared
                           || !info->symbolic
-                          || (h->elf_link_hash_flags
-                              & ELF_LINK_HASH_DEF_REGULAR) == 0))
+                          || !h->def_regular))
                outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
              else
                {
@@ -2912,13 +3032,14 @@ elf_i386_relocate_section (bfd *output_bfd,
         not process them.  */
       if (unresolved_reloc
          && !((input_section->flags & SEC_DEBUGGING) != 0
-              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
+              && h->def_dynamic))
        {
          (*_bfd_error_handler)
-           (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
+           (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
             input_bfd,
             input_section,
             (long) rel->r_offset,
+            howto->name,
             h->root.root.string);
          return FALSE;
        }
@@ -2947,8 +3068,9 @@ elf_i386_relocate_section (bfd *output_bfd,
          if (r == bfd_reloc_overflow)
            {
              if (! ((*info->callbacks->reloc_overflow)
-                    (info, name, howto->name, 0,
-                     input_bfd, input_section, rel->r_offset)))
+                    (info, (h ? &h->root : NULL), name, howto->name,
+                     (bfd_vma) 0, input_bfd, input_section,
+                     rel->r_offset)))
                return FALSE;
            }
          else
@@ -3015,6 +3137,42 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
                       + htab->sgotplt->output_offset
                       + got_offset),
                      htab->splt->contents + h->plt.offset + 2);
+
+         if (htab->is_vxworks)
+           {
+             int s, k, reloc_index;
+
+             /* Create the R_386_32 relocation referencing the GOT
+                for this PLT entry.  */
+
+             /* S: Current slot number (zero-based).  */
+             s = (h->plt.offset - PLT_ENTRY_SIZE) / PLT_ENTRY_SIZE;
+             /* K: Number of relocations for PLTResolve. */
+             if (info->shared)
+               k = PLTRESOLVE_RELOCS_SHLIB;
+             else
+               k = PLTRESOLVE_RELOCS;
+             /* Skip the PLTresolve relocations, and the relocations for
+                the other PLT slots. */
+             reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS;
+             loc = (htab->srelplt2->contents + reloc_index
+                    * sizeof (Elf32_External_Rel));
+
+             rel.r_offset = (htab->splt->output_section->vma
+                             + htab->splt->output_offset
+                             + h->plt.offset + 2),
+             rel.r_info = ELF32_R_INFO (htab->hgot->indx, R_386_32);
+             bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
+
+             /* Create the R_386_32 relocation referencing the beginning of
+                the PLT for this GOT entry.  */
+             rel.r_offset = (htab->sgotplt->output_section->vma
+                             + htab->sgotplt->output_offset
+                             + got_offset);
+             rel.r_info = ELF32_R_INFO (htab->hplt->indx, R_386_32);
+             bfd_elf32_swap_reloc_out (output_bfd, &rel,
+             loc + sizeof (Elf32_External_Rel));
+           }
        }
       else
        {
@@ -3045,7 +3203,7 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
       loc = htab->srelplt->contents + plt_index * sizeof (Elf32_External_Rel);
       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
 
-      if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+      if (!h->def_regular)
        {
          /* Mark the symbol as undefined, rather than as defined in
             the .plt section.  Leave the value if there were any
@@ -3056,7 +3214,7 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
             called from a binary, there is no need to slow down
             shared libraries because of that.  */
          sym->st_shndx = SHN_UNDEF;
-         if ((h->elf_link_hash_flags & ELF_LINK_POINTER_EQUALITY_NEEDED) == 0)
+         if (!h->pointer_equality_needed)
            sym->st_value = 0;
        }
     }
@@ -3102,7 +3260,7 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
     }
 
-  if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
+  if (h->needs_copy)
     {
       Elf_Internal_Rela rel;
       bfd_byte *loc;
@@ -3124,9 +3282,12 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
       bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
     }
 
-  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
+  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.
+     On VxWorks, the _GLOBAL_OFFSET_TABLE_ symbol is not absolute: it
+     is relative to the ".got" section.  */
   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
-      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+      || (strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
+         && !htab->is_vxworks))
     sym->st_shndx = SHN_ABS;
 
   return TRUE;
@@ -3234,12 +3395,20 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
       if (htab->splt && htab->splt->size > 0)
        {
          if (info->shared)
-           memcpy (htab->splt->contents,
-                   elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE);
+           {
+             memcpy (htab->splt->contents, elf_i386_pic_plt0_entry,
+                     sizeof (elf_i386_pic_plt0_entry));
+             memset (htab->splt->contents + sizeof (elf_i386_pic_plt0_entry),
+                     htab->plt0_pad_byte,
+                     PLT_ENTRY_SIZE - sizeof (elf_i386_pic_plt0_entry));
+           }
          else
            {
-             memcpy (htab->splt->contents,
-                     elf_i386_plt0_entry, PLT_ENTRY_SIZE);
+             memcpy (htab->splt->contents, elf_i386_plt0_entry,
+                     sizeof(elf_i386_plt0_entry));
+             memset (htab->splt->contents + sizeof (elf_i386_plt0_entry),
+                     htab->plt0_pad_byte,
+                     PLT_ENTRY_SIZE - sizeof (elf_i386_plt0_entry));
              bfd_put_32 (output_bfd,
                          (htab->sgotplt->output_section->vma
                           + htab->sgotplt->output_offset
@@ -3250,12 +3419,69 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
                           + htab->sgotplt->output_offset
                           + 8),
                          htab->splt->contents + 8);
+
+             if (htab->is_vxworks)
+               {
+                 Elf_Internal_Rela rel;
+                 struct elf_link_hash_entry *hgot;
+
+                 /* The VxWorks GOT is relocated by the dynamic linker.
+                    Therefore, we must emit relocations rather than
+                    simply computing the values now.  */
+                 hgot = elf_link_hash_lookup (elf_hash_table (info),
+                                              "_GLOBAL_OFFSET_TABLE_",
+                                              FALSE, FALSE, FALSE);
+                 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4.
+                    On IA32 we use REL relocations so the addend goes in
+                    the PLT directly.  */
+                 rel.r_offset = (htab->splt->output_section->vma
+                                 + htab->splt->output_offset
+                                 + 2);
+                 rel.r_info = ELF32_R_INFO (hgot->indx, R_386_32);
+                 bfd_elf32_swap_reloc_out (output_bfd, &rel,
+                                           htab->srelplt2->contents);
+                 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8.  */
+                 rel.r_offset = (htab->splt->output_section->vma
+                                 + htab->splt->output_offset
+                                 + 8);
+                 rel.r_info = ELF32_R_INFO (hgot->indx, R_386_32);
+                 bfd_elf32_swap_reloc_out (output_bfd, &rel,
+                                           htab->srelplt2->contents +
+                                           sizeof (Elf32_External_Rel));
+               }
            }
 
          /* UnixWare sets the entsize of .plt to 4, although that doesn't
             really seem like the right value.  */
          elf_section_data (htab->splt->output_section)
            ->this_hdr.sh_entsize = 4;
+
+         /* Correct the .rel.plt.unloaded relocations.  */
+         if (htab->is_vxworks && !info->shared)
+           {
+             int num_plts = (htab->splt->size / PLT_ENTRY_SIZE) - 1;
+             unsigned char *p;
+
+             p = htab->srelplt2->contents;
+             if (info->shared)
+               p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel);
+             else
+               p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel);
+
+             for (; num_plts; num_plts--)
+               {
+                 Elf_Internal_Rela rel;
+                 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
+                 rel.r_info = ELF32_R_INFO (htab->hgot->indx, R_386_32);
+                 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
+                 p += sizeof (Elf32_External_Rel);
+
+                 bfd_elf32_swap_reloc_in (output_bfd, p, &rel);
+                 rel.r_info = ELF32_R_INFO (htab->hplt->indx, R_386_32);
+                 bfd_elf32_swap_reloc_out (output_bfd, &rel, p);
+                 p += sizeof (Elf32_External_Rel);
+               }
+           }
        }
     }
 
@@ -3366,3 +3592,74 @@ elf_i386_post_process_headers (bfd *abfd,
 #define        elf32_bed                               elf32_i386_fbsd_bed
 
 #include "elf32-target.h"
+
+/* VxWorks support.  */
+
+#undef TARGET_LITTLE_SYM
+#define TARGET_LITTLE_SYM              bfd_elf32_i386_vxworks_vec
+#undef TARGET_LITTLE_NAME
+#define TARGET_LITTLE_NAME             "elf32-i386-vxworks"
+
+
+/* Like elf_i386_link_hash_table_create but with tweaks for VxWorks.  */
+
+static struct bfd_link_hash_table *
+elf_i386_vxworks_link_hash_table_create (bfd *abfd)
+{
+  struct bfd_link_hash_table *ret;
+  struct elf_i386_link_hash_table *htab;
+
+  ret = elf_i386_link_hash_table_create (abfd);
+  if (ret)
+    {
+      htab = (struct elf_i386_link_hash_table *) ret;
+      htab->is_vxworks = 1;
+      htab->plt0_pad_byte = 0x90;
+    }
+
+  return ret;
+}
+
+
+/* Tweak magic VxWorks symbols as they are written to the output file.  */
+static bfd_boolean
+elf_i386_vxworks_link_output_symbol_hook (struct bfd_link_info *info
+                                           ATTRIBUTE_UNUSED,
+                                         const char *name,
+                                         Elf_Internal_Sym *sym,
+                                         asection *input_sec ATTRIBUTE_UNUSED,
+                                         struct elf_link_hash_entry *h
+                                           ATTRIBUTE_UNUSED)
+{
+  /* Ignore the first dummy symbol.  */
+  if (!name)
+    return TRUE;
+
+  return elf_vxworks_link_output_symbol_hook (name, sym);
+}
+
+#undef elf_backend_post_process_headers
+#undef bfd_elf32_bfd_link_hash_table_create
+#define bfd_elf32_bfd_link_hash_table_create \
+  elf_i386_vxworks_link_hash_table_create
+#undef elf_backend_add_symbol_hook
+#define elf_backend_add_symbol_hook \
+  elf_vxworks_add_symbol_hook
+#undef elf_backend_link_output_symbol_hook
+#define elf_backend_link_output_symbol_hook \
+  elf_i386_vxworks_link_output_symbol_hook
+#undef elf_backend_emit_relocs
+#define elf_backend_emit_relocs                        elf_vxworks_emit_relocs
+#undef elf_backend_final_write_processing
+#define elf_backend_final_write_processing \
+  elf_vxworks_final_write_processing
+
+/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
+   define it.  */
+#undef elf_backend_want_plt_sym
+#define elf_backend_want_plt_sym       1
+
+#undef elf32_bed
+#define elf32_bed                              elf32_i386_vxworks_bed
+
+#include "elf32-target.h"
This page took 0.048393 seconds and 4 git commands to generate.