2004-05-14 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / bfd / elf32-i386.c
index 8f50a3dd49b28b3dd05a1a767397bdc4db688226..dee26a025cd37cca0735a32c5781971f1fc0b52e 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
-   Free Software Foundation, Inc.
+   Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+   2003, 2004 Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -346,29 +346,50 @@ elf_i386_is_local_label_name (bfd *abfd, const char *name)
 }
 \f
 /* Support for core dump NOTE sections.  */
+
 static bfd_boolean
 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 {
   int offset;
   size_t raw_size;
 
-  switch (note->descsz)
+  if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
     {
-      default:
-       return FALSE;
+      int pr_version = bfd_get_32 (abfd, note->descdata);
 
-      case 144:                /* Linux/i386 */
-       /* pr_cursig */
-       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
+      if (pr_version != 1)
+       return FALSE;
 
-       /* pr_pid */
-       elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
+      /* pr_cursig */
+      elf_tdata (abfd)->core_signal = bfd_get_32 (abfd, note->descdata + 20);
 
-       /* pr_reg */
-       offset = 72;
-       raw_size = 68;
+      /* pr_pid */
+      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
 
-       break;
+      /* pr_reg */
+      offset = 28;
+      raw_size = bfd_get_32 (abfd, note->descdata + 8);
+    }
+  else
+    {
+      switch (note->descsz)
+       {
+       default:
+         return FALSE;
+
+       case 144:               /* Linux/i386 */
+         /* pr_cursig */
+         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
+
+         /* pr_pid */
+         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
+
+         /* pr_reg */
+         offset = 72;
+         raw_size = 68;
+
+         break;
+       }
     }
 
   /* Make a ".reg/999" section.  */
@@ -379,22 +400,36 @@ elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 static bfd_boolean
 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 {
-  switch (note->descsz)
+  if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0)
     {
-      default:
+      int pr_version = bfd_get_32 (abfd, note->descdata);
+
+      if (pr_version != 1)
        return FALSE;
 
-      case 124:                /* Linux/i386 elf_prpsinfo */
-       elf_tdata (abfd)->core_program
-         = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
-       elf_tdata (abfd)->core_command
-         = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
+      elf_tdata (abfd)->core_program
+       = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17);
+      elf_tdata (abfd)->core_command
+       = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81);
+    }
+  else
+    {
+      switch (note->descsz)
+       {
+       default:
+         return FALSE;
+
+       case 124:               /* Linux/i386 elf_prpsinfo.  */
+         elf_tdata (abfd)->core_program
+           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
+         elf_tdata (abfd)->core_command
+           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
+       }
     }
 
   /* Note that for some reason, a spurious space is tacked
      onto the end of the args in some (at least one anyway)
      implementations, so strip it off if it exists.  */
-
   {
     char *command = elf_tdata (abfd)->core_command;
     int n = strlen (command);
@@ -546,20 +581,6 @@ elf_i386_mkobject (bfd *abfd)
   return TRUE;
 }
 
-static bfd_boolean
-elf_i386_object_p (bfd *abfd)
-{
-  /* Allocate our special target data.  */
-  struct elf_i386_obj_tdata *new_tdata;
-  bfd_size_type amt = sizeof (struct elf_i386_obj_tdata);
-  new_tdata = bfd_zalloc (abfd, amt);
-  if (new_tdata == NULL)
-    return FALSE;
-  new_tdata->root = *abfd->tdata.elf_obj_data;
-  abfd->tdata.any = new_tdata;
-  return TRUE;
-}
-
 /* i386 ELF linker hash table.  */
 
 struct elf_i386_link_hash_table
@@ -770,7 +791,9 @@ elf_i386_copy_indirect_symbol (const struct elf_backend_data *bed,
     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_REF_REGULAR_NONWEAK
+                                  | ELF_LINK_HASH_NEEDS_PLT
+                                  | ELF_LINK_POINTER_EQUALITY_NEEDED));
   else
     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
 }
@@ -1000,6 +1023,8 @@ elf_i386_check_relocs (bfd *abfd,
              /* 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;
            }
 
          /* If we are creating a shared library, and this is a reloc
@@ -1132,14 +1157,14 @@ elf_i386_check_relocs (bfd *abfd,
          /* This relocation describes the C++ object vtable hierarchy.
             Reconstruct it for later use during GC.  */
        case R_386_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_386_GNU_VTENTRY:
-         if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
+         if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
            return FALSE;
          break;
 
@@ -1433,17 +1458,6 @@ elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info,
   return TRUE;
 }
 
-/* This is the condition under which elf_i386_finish_dynamic_symbol
-   will be called from elflink.h.  If elflink.h doesn't call our
-   finish_dynamic_symbol routine, we'll need to do something about
-   initializing any .plt and .got entries in elf_i386_relocate_section.  */
-#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
-  ((DYN)                                                               \
-   && ((SHARED)                                                                \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)     \
-   && ((H)->dynindx != -1                                              \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
-
 /* Allocate space in .plt, .got and associated reloc sections for
    dynamic relocs.  */
 
@@ -1475,7 +1489,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       if (h->dynindx == -1
          && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
        {
-         if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+         if (! bfd_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
        }
 
@@ -1543,7 +1557,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       if (h->dynindx == -1
          && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
        {
-         if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+         if (! bfd_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
        }
 
@@ -1632,7 +1646,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          if (h->dynindx == -1
              && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
            {
-             if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+             if (! bfd_elf_link_record_dynamic_symbol (info, h))
                return FALSE;
            }
 
@@ -1870,7 +1884,7 @@ elf_i386_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
         the .dynamic section.  The DT_DEBUG entry is filled in by the
         dynamic linker and used by the debugger.  */
 #define add_dynamic_entry(TAG, VAL) \
-  bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
+  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
 
       if (info->executable)
        {
@@ -1953,10 +1967,10 @@ elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
 static bfd_vma
 dtpoff_base (struct bfd_link_info *info)
 {
-  /* If tls_segment is NULL, we should have signalled an error already.  */
-  if (elf_hash_table (info)->tls_segment == NULL)
+  /* If tls_sec is NULL, we should have signalled an error already.  */
+  if (elf_hash_table (info)->tls_sec == NULL)
     return 0;
-  return elf_hash_table (info)->tls_segment->start;
+  return elf_hash_table (info)->tls_sec->vma;
 }
 
 /* Return the relocation value for @tpoff relocation
@@ -1965,14 +1979,12 @@ dtpoff_base (struct bfd_link_info *info)
 static bfd_vma
 tpoff (struct bfd_link_info *info, bfd_vma address)
 {
-  struct elf_link_tls_segment *tls_segment
-    = elf_hash_table (info)->tls_segment;
+  struct elf_link_hash_table *htab = elf_hash_table (info);
 
-  /* If tls_segment is NULL, we should have signalled an error already.  */
-  if (tls_segment == NULL)
+  /* If tls_sec is NULL, we should have signalled an error already.  */
+  if (htab->tls_sec == NULL)
     return 0;
-  return (align_power (tls_segment->size, tls_segment->align)
-         + tls_segment->start - address);
+  return htab->tls_size + htab->tls_sec->vma - address;
 }
 
 /* Relocate an i386 ELF section.  */
@@ -2156,7 +2168,10 @@ elf_i386_relocate_section (bfd *output_bfd,
        {
          bfd_boolean warned;
 
-         RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx, symtab_hdr, relocation, sec, unresolved_reloc, info, warned);
+         RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
+                                  r_symndx, symtab_hdr, sym_hashes,
+                                  h, sec, relocation,
+                                  unresolved_reloc, warned);
        }
 
       switch (r_type)
@@ -2246,24 +2261,29 @@ elf_i386_relocate_section (bfd *output_bfd,
          if (off >= (bfd_vma) -2)
            abort ();
 
-         relocation = htab->sgot->output_offset + off;
+         relocation = htab->sgot->output_section->vma
+                      + htab->sgot->output_offset + off
+                      - htab->sgotplt->output_section->vma
+                      - htab->sgotplt->output_offset;
          break;
 
        case R_386_GOTOFF:
          /* Relocation is relative to the start of the global offset
             table.  */
 
-         /* Note that sgot->output_offset is not involved in this
-            calculation.  We always want the start of .got.  If we
-            defined _GLOBAL_OFFSET_TABLE in a different way, as is
+         /* 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
             permitted by the ABI, we might have to change this
             calculation.  */
-         relocation -= htab->sgot->output_section->vma;
+         relocation -= htab->sgotplt->output_section->vma
+                       + htab->sgotplt->output_offset;
          break;
 
        case R_386_GOTPC:
          /* Use global offset table as symbol value.  */
-         relocation = htab->sgot->output_section->vma;
+         relocation = htab->sgotplt->output_section->vma
+                      + htab->sgotplt->output_offset;
          unresolved_reloc = FALSE;
          break;
 
@@ -2684,12 +2704,15 @@ elf_i386_relocate_section (bfd *output_bfd,
            abort ();
          if (r_type == ELF32_R_TYPE (rel->r_info))
            {
-             relocation = htab->sgot->output_offset + off;
+             bfd_vma g_o_t = htab->sgotplt->output_section->vma
+                             + htab->sgotplt->output_offset;
+             relocation = htab->sgot->output_section->vma
+                          + htab->sgot->output_offset + off - g_o_t;
              if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE)
                  && tls_type == GOT_TLS_IE_BOTH)
                relocation += 4;
              if (r_type == R_386_TLS_IE)
-               relocation += htab->sgot->output_section->vma;
+               relocation += g_o_t;
              unresolved_reloc = FALSE;
            }
          else
@@ -2746,7 +2769,11 @@ elf_i386_relocate_section (bfd *output_bfd,
                  if (tls_type == GOT_TLS_IE_BOTH)
                    off += 4;
                }
-             bfd_put_32 (output_bfd, htab->sgot->output_offset + off,
+             bfd_put_32 (output_bfd,
+                         htab->sgot->output_section->vma
+                         + htab->sgot->output_offset + off
+                         - htab->sgotplt->output_section->vma
+                         - htab->sgotplt->output_offset,
                          contents + roff + 8);
              /* Skip R_386_PLT32.  */
              rel++;
@@ -2808,7 +2835,10 @@ elf_i386_relocate_section (bfd *output_bfd,
              bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc);
              htab->tls_ldm_got.offset |= 1;
            }
-         relocation = htab->sgot->output_offset + off;
+         relocation = htab->sgot->output_section->vma
+                      + htab->sgot->output_offset + off
+                      - htab->sgotplt->output_section->vma
+                      - htab->sgotplt->output_offset;
          unresolved_reloc = FALSE;
          break;
 
@@ -3005,11 +3035,16 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd,
       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
        {
          /* Mark the symbol as undefined, rather than as defined in
-            the .plt section.  Leave the value alone.  This is a clue
+            the .plt section.  Leave the value if there were any
+            relocations where pointer equality matters (this is a clue
             for the dynamic linker, to make function pointer
             comparisons work between an application and shared
-            library.  */
+            library), otherwise set it to zero.  If a function is only
+            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)
+           sym->st_value = 0;
        }
     }
 
@@ -3139,7 +3174,8 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
              continue;
 
            case DT_PLTGOT:
-             dyn.d_un.d_ptr = htab->sgot->output_section->vma;
+             s = htab->sgotplt;
+             dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
              break;
 
            case DT_JMPREL:
@@ -3225,9 +3261,24 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
 
       elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize = 4;
     }
+
+  if (htab->sgot && htab->sgot->_raw_size > 0)
+    elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 4;
+
   return TRUE;
 }
 
+/* Return address for Ith PLT stub in section PLT, for relocation REL
+   or (bfd_vma) -1 if it should not be included.  */
+
+static bfd_vma
+elf_i386_plt_sym_val (bfd_vma i, const asection *plt,
+                     const arelent *rel ATTRIBUTE_UNUSED)
+{
+  return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
+}
+
+
 #define TARGET_LITTLE_SYM              bfd_elf32_i386_vec
 #define TARGET_LITTLE_NAME             "elf32-i386"
 #define ELF_ARCH                       bfd_arch_i386
@@ -3246,7 +3297,6 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
 #define elf_info_to_howto_rel                elf_i386_info_to_howto_rel
 
 #define bfd_elf32_mkobject                   elf_i386_mkobject
-#define elf_backend_object_p                 elf_i386_object_p
 
 #define bfd_elf32_bfd_is_local_label_name     elf_i386_is_local_label_name
 #define bfd_elf32_bfd_link_hash_table_create  elf_i386_link_hash_table_create
@@ -3266,6 +3316,7 @@ elf_i386_finish_dynamic_sections (bfd *output_bfd,
 #define elf_backend_reloc_type_class         elf_i386_reloc_type_class
 #define elf_backend_relocate_section         elf_i386_relocate_section
 #define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
+#define elf_backend_plt_sym_val                      elf_i386_plt_sym_val
 
 #include "elf32-target.h"
 
This page took 0.030495 seconds and 4 git commands to generate.