* elf-hppa.h (elf_hppa_relocate_section): If relocatable, return
[binutils-gdb.git] / bfd / elf32-s390.c
index f608fb0aa6dbefaa88b6ca9a7e2ef2439bd114ff..8067018dd890b1d7f3b7eeadb5b8d3beb3a7530b 100644 (file)
@@ -1,5 +1,5 @@
 /* IBM S/390-specific support for 32-bit ELF
-   Copyright 2000, 2001 Free Software Foundation, Inc.
+   Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
    Contributed by Carl B. Pedersen and Martin Schwidefsky.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -45,7 +45,7 @@ static boolean elf_s390_check_relocs
   PARAMS ((bfd *, struct bfd_link_info *, asection *,
           const Elf_Internal_Rela *));
 static asection *elf_s390_gc_mark_hook
-  PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
+  PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
           struct elf_link_hash_entry *, Elf_Internal_Sym *));
 static boolean elf_s390_gc_sweep_hook
   PARAMS ((bfd *, struct bfd_link_info *, asection *,
@@ -69,8 +69,7 @@ static enum elf_reloc_type_class elf_s390_reloc_type_class
 static boolean elf_s390_finish_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
 static boolean elf_s390_object_p PARAMS ((bfd *));
-
-#define USE_RELA 1             /* We want RELA relocations, not REL.  */
+static boolean elf_s390_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
 
 #include "elf/s390.h"
 
@@ -110,6 +109,10 @@ static reloc_howto_type elf_howto_table[] =
   HOWTO(R_390_PC16,      0, 1, 16,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16",    false, 0,0x0000ffff,  true),
   HOWTO(R_390_PC16DBL,   1, 1, 16,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC16DBL", false, 0,0x0000ffff,  true),
   HOWTO(R_390_PLT16DBL,  1, 1, 16,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT16DBL", false, 0,0x0000ffff,  true),
+  HOWTO(R_390_PC32DBL,  1, 2, 32,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PC32DBL", false, 0,0xffffffff,  true),
+  HOWTO(R_390_PLT32DBL,         1, 2, 32,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_PLT32DBL", false, 0,0xffffffff,  true),
+  HOWTO(R_390_GOTPCDBL,  1, 2, 32,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTPCDBL", false, 0,0xffffffff,  true),
+  HOWTO(R_390_GOTENT,   1, 2, 32,  true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_390_GOTENT",   false, 0,0xffffffff,  true),
 };
 
 /* GNU extension to record C++ vtable hierarchy.  */
@@ -165,6 +168,14 @@ elf_s390_reloc_type_lookup (abfd, code)
       return &elf_howto_table[(int) R_390_PC16DBL];
     case BFD_RELOC_390_PLT16DBL:
       return &elf_howto_table[(int) R_390_PLT16DBL];
+    case BFD_RELOC_390_PC32DBL:
+      return &elf_howto_table[(int) R_390_PC32DBL];
+    case BFD_RELOC_390_PLT32DBL:
+      return &elf_howto_table[(int) R_390_PLT32DBL];
+    case BFD_RELOC_390_GOTPCDBL:
+      return &elf_howto_table[(int) R_390_GOTPCDBL];
+    case BFD_RELOC_390_GOTENT:
+      return &elf_howto_table[(int) R_390_GOTENT];
     case BFD_RELOC_VTABLE_INHERIT:
       return &elf32_s390_vtinherit_howto;
     case BFD_RELOC_VTABLE_ENTRY:
@@ -465,13 +476,13 @@ elf_s390_link_hash_table_create (abfd)
   struct elf_s390_link_hash_table *ret;
   bfd_size_type amt = sizeof (struct elf_s390_link_hash_table);
 
-  ret = (struct elf_s390_link_hash_table *) bfd_alloc (abfd, amt);
+  ret = (struct elf_s390_link_hash_table *) bfd_malloc (amt);
   if (ret == NULL)
     return NULL;
 
   if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
     {
-      bfd_release (abfd, ret);
+      free (ret);
       return NULL;
     }
 
@@ -649,6 +660,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
        case R_390_GOT12:
         case R_390_GOT16:
        case R_390_GOT32:
+       case R_390_GOTENT:
          /* This symbol requires a global offset table entry.  */
          if (h != NULL)
            {
@@ -678,6 +690,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
 
        case R_390_GOTOFF:
        case R_390_GOTPC:
+       case R_390_GOTPCDBL:
          if (htab->sgot == NULL)
            {
              if (htab->elf.dynobj == NULL)
@@ -688,6 +701,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
          break;
 
         case R_390_PLT16DBL:
+        case R_390_PLT32DBL:
        case R_390_PLT32:
          /* This symbol requires a procedure linkage table entry.  We
              actually build the entry in adjust_dynamic_symbol,
@@ -710,6 +724,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
        case R_390_32:
         case R_390_PC16:
         case R_390_PC16DBL:
+       case R_390_PC32DBL:
        case R_390_PC32:
          if (h != NULL && !info->shared)
            {
@@ -751,6 +766,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
               && (sec->flags & SEC_ALLOC) != 0
               && ((ELF32_R_TYPE (rel->r_info) != R_390_PC16
                    && ELF32_R_TYPE (rel->r_info) != R_390_PC16DBL
+                   && ELF32_R_TYPE (rel->r_info) != R_390_PC32DBL
                    && ELF32_R_TYPE (rel->r_info) != R_390_PC32)
                   || (h != NULL
                       && (! info->symbolic
@@ -853,6 +869,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
              p->count += 1;
              if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
                  || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
+                 || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
                  || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
                p->pc_count += 1;
            }
@@ -868,7 +885,7 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
          /* This relocation describes which C++ vtable entries are actually
             used.  Record for later use during GC.  */
         case R_390_GNU_VTENTRY:
-          if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_offset))
+          if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
             return false;
           break;
 
@@ -884,8 +901,8 @@ elf_s390_check_relocs (abfd, info, sec, relocs)
    relocation.  */
 
 static asection *
-elf_s390_gc_mark_hook (abfd, info, rel, h, sym)
-     bfd *abfd;
+elf_s390_gc_mark_hook (sec, info, rel, h, sym)
+     asection *sec;
      struct bfd_link_info *info ATTRIBUTE_UNUSED;
      Elf_Internal_Rela *rel;
      struct elf_link_hash_entry *h;
@@ -915,15 +932,7 @@ elf_s390_gc_mark_hook (abfd, info, rel, h, sym)
        }
     }
   else
-    {
-      if (!(elf_bad_symtab (abfd)
-           && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
-         && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
-               && sym->st_shndx != SHN_COMMON))
-       {
-         return bfd_section_from_elf_index (abfd, sym->st_shndx);
-       }
-    }
+    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
 
   return NULL;
 }
@@ -959,6 +968,8 @@ elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
       case R_390_GOT32:
       case R_390_GOTOFF:
       case R_390_GOTPC:
+      case R_390_GOTPCDBL:
+      case R_390_GOTENT:
        r_symndx = ELF32_R_SYM (rel->r_info);
        if (r_symndx >= symtab_hdr->sh_info)
          {
@@ -979,6 +990,7 @@ elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
       case R_390_32:
       case R_390_PC16:
       case R_390_PC16DBL:
+      case R_390_PC32DBL:
       case R_390_PC32:
        r_symndx = ELF32_R_SYM (rel->r_info);
        if (r_symndx >= symtab_hdr->sh_info)
@@ -999,6 +1011,7 @@ elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
                {
                  if (ELF32_R_TYPE (rel->r_info) == R_390_PC16
                      || ELF32_R_TYPE (rel->r_info) == R_390_PC16DBL
+                     || ELF32_R_TYPE (rel->r_info) == R_390_PC32DBL
                      || ELF32_R_TYPE (rel->r_info) == R_390_PC32)
                    p->pc_count -= 1;
                  p->count -= 1;
@@ -1010,6 +1023,7 @@ elf_s390_gc_sweep_hook (abfd, info, sec, relocs)
        break;
 
       case R_390_PLT16DBL:
+      case R_390_PLT32DBL:
       case R_390_PLT32:
        r_symndx = ELF32_R_SYM (rel->r_info);
        if (r_symndx >= symtab_hdr->sh_info)
@@ -1046,14 +1060,16 @@ elf_s390_adjust_dynamic_symbol (info, h)
 
   /* If this is a function, put it in the procedure linkage table.  We
      will fill in the contents of the procedure linkage table later
-     (although we could actually do it here). */
+     (although we could actually do it here).  */
   if (h->type == STT_FUNC
       || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
     {
       if (h->plt.refcount <= 0
          || (! info->shared
              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
-             && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
+             && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
+             && h->root.type != bfd_link_hash_undefweak
+             && h->root.type != bfd_link_hash_undefined))
        {
          /* This case can occur if we saw a PLT32 reloc in an input
              file, but the symbol was never referred to by a dynamic
@@ -1194,10 +1210,12 @@ allocate_dynrelocs (h, inf)
   struct elf_s390_link_hash_entry *eh;
   struct elf_s390_dyn_relocs *p;
 
-  if (h->root.type == bfd_link_hash_indirect
-      || h->root.type == bfd_link_hash_warning)
+  if (h->root.type == bfd_link_hash_indirect)
     return true;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   info = (struct bfd_link_info *) inf;
   htab = elf_s390_hash_table (info);
 
@@ -1364,6 +1382,9 @@ readonly_dynrelocs (h, inf)
   struct elf_s390_link_hash_entry *eh;
   struct elf_s390_dyn_relocs *p;
 
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
   eh = (struct elf_s390_link_hash_entry *) h;
   for (p = eh->dyn_relocs; p != NULL; p = p->next)
     {
@@ -1443,10 +1464,12 @@ elf_s390_size_dynamic_sections (output_bfd, info)
                     linker script /DISCARD/, so we'll be discarding
                     the relocs too.  */
                }
-             else
+             else if (p->count != 0)
                {
                  srela = elf_section_data (p->sec)->sreloc;
                  srela->_raw_size += p->count * sizeof (Elf32_External_Rela);
+                 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
+                   info->flags |= DF_TEXTREL;
                }
            }
        }
@@ -1568,7 +1591,9 @@ elf_s390_size_dynamic_sections (output_bfd, info)
 
          /* If any dynamic relocs apply to a read-only section,
             then we need a DT_TEXTREL entry.  */
-         elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, (PTR) info);
+         if ((info->flags & DF_TEXTREL) == 0)
+           elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
+                                   (PTR) info);
 
          if ((info->flags & DF_TEXTREL) != 0)
            {
@@ -1603,6 +1628,9 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
 
+  if (info->relocateable)
+    return true;
+
   htab = elf_s390_hash_table (info);
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (input_bfd);
@@ -1632,30 +1660,9 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
          bfd_set_error (bfd_error_bad_value);
          return false;
        }
-      howto = elf_howto_table + r_type;
 
+      howto = elf_howto_table + r_type;
       r_symndx = ELF32_R_SYM (rel->r_info);
-
-      if (info->relocateable)
-       {
-         /* This is a relocateable link.  We don't have to change
-            anything, unless the reloc is against a section symbol,
-            in which case we have to adjust according to where the
-            section symbol winds up in the output section.  */
-         if (r_symndx < symtab_hdr->sh_info)
-           {
-             sym = local_syms + r_symndx;
-             if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
-               {
-                 sec = local_sections[r_symndx];
-                 rel->r_addend += sec->output_offset + sym->st_value;
-               }
-           }
-
-         continue;
-       }
-
-      /* This is a final link.  */
       h = NULL;
       sym = NULL;
       sec = NULL;
@@ -1715,6 +1722,7 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
         case R_390_GOT12:
         case R_390_GOT16:
         case R_390_GOT32:
+       case R_390_GOTENT:
           /* Relocation is to the entry for this symbol in the global
              offset table.  */
          if (htab->sgot == NULL)
@@ -1802,6 +1810,16 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
            abort ();
 
          relocation = htab->sgot->output_offset + off;
+
+         /*
+          * For @GOTENT the relocation is against the offset between
+          * the instruction and the symbols entry in the GOT and not
+          * between the start of the GOT and the symbols entry. We
+          * add the vma of the GOT to get the correct value.
+          */
+         if (r_type == R_390_GOTENT)
+           relocation += htab->sgot->output_section->vma;
+
           break;
 
         case R_390_GOTOFF:
@@ -1817,12 +1835,14 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
           break;
 
         case R_390_GOTPC:
+       case R_390_GOTPCDBL:
           /* Use global offset table as symbol value.  */
           relocation = htab->sgot->output_section->vma;
          unresolved_reloc = false;
           break;
 
         case R_390_PLT16DBL:
+        case R_390_PLT32DBL:
         case R_390_PLT32:
           /* Relocation is to the entry for this symbol in the
              procedure linkage table.  */
@@ -1852,6 +1872,7 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
         case R_390_32:
         case R_390_PC16:
         case R_390_PC16DBL:
+        case R_390_PC32DBL:
         case R_390_PC32:
          /* r_symndx will be zero only for relocs against symbols
             from removed linkonce sections, or sections discarded by
@@ -1863,6 +1884,7 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
           if ((info->shared
               && ((r_type != R_390_PC16
                    && r_type != R_390_PC16DBL
+                   && r_type != R_390_PC32DBL
                    && r_type != R_390_PC32)
                   || (h != NULL
                       && h->dynindx != -1
@@ -1890,39 +1912,31 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
                 time.  */
 
               skip = false;
-
-              if (elf_section_data (input_section)->stab_info == NULL)
-                outrel.r_offset = rel->r_offset;
-              else
-                {
-                  off = (_bfd_stab_section_offset
-                         (output_bfd, htab->elf.stab_info, input_section,
-                          &elf_section_data (input_section)->stab_info,
-                          rel->r_offset));
-                  if (off == (bfd_vma) -1)
-                    skip = true;
-                  outrel.r_offset = off;
-                }
-
+              relocate = false;
+
+             outrel.r_offset =
+               _bfd_elf_section_offset (output_bfd, info, input_section,
+                                        rel->r_offset);
+             if (outrel.r_offset == (bfd_vma) -1)
+               skip = true;
+             else if (outrel.r_offset == (bfd_vma) -2)
+               skip = true, relocate = true;
               outrel.r_offset += (input_section->output_section->vma
                                   + input_section->output_offset);
 
               if (skip)
-                {
-                  memset (&outrel, 0, sizeof outrel);
-                  relocate = false;
-                }
+               memset (&outrel, 0, sizeof outrel);
               else if (h != NULL
                       && h->dynindx != -1
                       && (r_type == R_390_PC16
                           || r_type == R_390_PC16DBL
+                          || r_type == R_390_PC32DBL
                           || r_type == R_390_PC32
                           || !info->shared
                           || !info->symbolic
                           || (h->elf_link_hash_flags
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
                 {
-                 relocate = false;
                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
                  outrel.r_addend = rel->r_addend;
                 }
@@ -1955,9 +1969,11 @@ elf_s390_relocate_section (output_bfd, info, input_bfd, input_section,
          break;
        }
 
+      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
+        because such sections are not SEC_ALLOC and thus ld.so will
+        not process them.  */
       if (unresolved_reloc
-         && !(info->shared
-              && (input_section->flags & SEC_DEBUGGING) != 0
+         && !((input_section->flags & SEC_DEBUGGING) != 0
               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
        (*_bfd_error_handler)
          (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
@@ -2383,7 +2399,38 @@ static boolean
 elf_s390_object_p (abfd)
      bfd *abfd;
 {
-  return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_esa);
+  return bfd_default_set_arch_mach (abfd, bfd_arch_s390, bfd_mach_s390_31);
+}
+
+static boolean
+elf_s390_grok_prstatus (abfd, note)
+     bfd * abfd;
+     Elf_Internal_Note * note;
+{
+  int offset;
+  unsigned int raw_size;
+
+  switch (note->descsz)
+    {
+      default:
+       return false;
+
+      case 224:                /* S/390 Linux.  */
+       /* 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 = 144;
+       break;
+    }
+
+  /* Make a ".reg/999" section.  */
+  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
+                                         raw_size, note->descpos + offset);
 }
 
 #define TARGET_BIG_SYM bfd_elf32_s390_vec
@@ -2400,6 +2447,7 @@ elf_s390_object_p (abfd)
 #define elf_backend_want_plt_sym       0
 #define elf_backend_got_header_size    12
 #define elf_backend_plt_header_size    PLT_ENTRY_SIZE
+#define elf_backend_rela_normal                1
 
 #define elf_info_to_howto                     elf_s390_info_to_howto
 
@@ -2419,6 +2467,7 @@ elf_s390_object_p (abfd)
 #define elf_backend_relocate_section          elf_s390_relocate_section
 #define elf_backend_size_dynamic_sections     elf_s390_size_dynamic_sections
 #define elf_backend_reloc_type_class         elf_s390_reloc_type_class
+#define elf_backend_grok_prstatus            elf_s390_grok_prstatus
 
 #define elf_backend_object_p            elf_s390_object_p