* elfxx-target.h: Remove PTR cast.
[deliverable/binutils-gdb.git] / bfd / elf64-mips.c
index fda989fde987c4ac9219a2a8285e4d4c3761a3cf..041c22262be45de8e75a973558ab9f33b3f8adac 100644 (file)
@@ -1,5 +1,5 @@
 /* MIPS-specific support for 64-bit ELF
-   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002
+   Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
    Free Software Foundation, Inc.
    Ian Lance Taylor, Cygnus Support
    Linker support added by Mark Mitchell, CodeSourcery, LLC.
@@ -91,8 +91,14 @@ static void mips_elf64_info_to_howto_rela
   PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
 static long mips_elf64_get_reloc_upper_bound
   PARAMS ((bfd *, asection *));
+static long mips_elf64_canonicalize_reloc
+  PARAMS ((bfd *, asection *, arelent **, asymbol **));
+static long mips_elf64_get_dynamic_reloc_upper_bound PARAMS ((bfd *));
+static long mips_elf64_canonicalize_dynamic_reloc
+  PARAMS ((bfd *, arelent **, asymbol **));
 static bfd_boolean mips_elf64_slurp_one_reloc_table
-  PARAMS ((bfd *, asection *, asymbol **, const Elf_Internal_Shdr *));
+  PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,
+          arelent *, asymbol **, bfd_boolean));
 static bfd_boolean mips_elf64_slurp_reloc_table
   PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
 static void mips_elf64_write_relocs
@@ -125,6 +131,10 @@ static bfd_boolean mips_elf64_object_p
   PARAMS ((bfd *));
 static irix_compat_t elf64_mips_irix_compat
   PARAMS ((bfd *));
+static bfd_boolean elf64_mips_grok_prstatus
+  PARAMS ((bfd *, Elf_Internal_Note *));
+static bfd_boolean elf64_mips_grok_psinfo
+  PARAMS ((bfd *, Elf_Internal_Note *));
 
 extern const bfd_target bfd_elf64_bigmips_vec;
 extern const bfd_target bfd_elf64_littlemips_vec;
@@ -1217,6 +1227,38 @@ static reloc_howto_type elf_mips_gnu_vtentry_howto =
         0,                     /* dst_mask */
         FALSE);                /* pcrel_offset */
 \f
+/* 16 bit offset for pc-relative branches.  */
+static reloc_howto_type elf_mips_gnu_rel16_s2 =
+  HOWTO (R_MIPS_GNU_REL16_S2,  /* type */
+        2,                     /* rightshift */
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_signed, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_MIPS_GNU_REL16_S2", /* name */
+        TRUE,                  /* partial_inplace */
+        0x0000ffff,            /* src_mask */
+        0x0000ffff,            /* dst_mask */
+        TRUE);                 /* pcrel_offset */
+
+/* 16 bit offset for pc-relative branches.  */
+static reloc_howto_type elf_mips_gnu_rela16_s2 =
+  HOWTO (R_MIPS_GNU_REL16_S2,  /* type */
+        2,                     /* rightshift */
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_signed, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_MIPS_GNU_REL16_S2", /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0x0000ffff,            /* dst_mask */
+        TRUE);                 /* pcrel_offset */
+\f
 /* Swap in a MIPS 64-bit Rel reloc.  */
 
 static void
@@ -1405,15 +1447,17 @@ mips_elf64_hi16_reloc (abfd, reloc_entry, symbol, data, input_section,
      want to change anything.  */
   if (output_bfd != (bfd *) NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && (! reloc_entry->howto->partial_inplace
-         || reloc_entry->addend == 0))
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       reloc_entry->address += input_section->output_offset;
       return bfd_reloc_ok;
     }
 
-  if (((reloc_entry->addend & 0xffff) + 0x8000) & ~0xffff)
-    reloc_entry->addend += 0x8000;
+  if (reloc_entry->howto->partial_inplace)
+    {
+      if (((reloc_entry->addend & 0xffff) + 0x8000) & ~0xffff)
+       reloc_entry->addend += 0x8000;
+    }
 
   return bfd_reloc_continue;
 }
@@ -1446,7 +1490,8 @@ mips_elf64_got16_reloc (abfd, reloc_entry, symbol, data, input_section,
   /* If we're relocating, and this is a local symbol, we can handle it
      just like an R_MIPS_HI16.  */
   if (output_bfd != (bfd *) NULL
-      && (symbol->flags & BSF_SECTION_SYM) != 0)
+      && ((symbol->flags & BSF_SECTION_SYM) != 0
+         || (symbol->flags & BSF_LOCAL) == 0))
     return mips_elf64_hi16_reloc (abfd, reloc_entry, symbol, data,
                                  input_section, output_bfd, error_message);
 
@@ -1511,18 +1556,18 @@ mips_elf64_assign_gp (output_bfd, pgp)
    symbol value correctly.  We look up the symbol _gp in the output
    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
    target data.  We don't need to adjust the symbol value for an
-   external symbol if we are producing relocateable output.  */
+   external symbol if we are producing relocatable output.  */
 
 static bfd_reloc_status_type
-mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
+mips_elf64_final_gp (output_bfd, symbol, relocatable, error_message, pgp)
      bfd *output_bfd;
      asymbol *symbol;
-     bfd_boolean relocateable;
+     bfd_boolean relocatable;
      char **error_message;
      bfd_vma *pgp;
 {
   if (bfd_is_und_section (symbol->section)
-      && ! relocateable)
+      && ! relocatable)
     {
       *pgp = 0;
       return bfd_reloc_undefined;
@@ -1530,10 +1575,10 @@ mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message, pgp)
 
   *pgp = _bfd_get_gp_value (output_bfd);
   if (*pgp == 0
-      && (! relocateable
+      && (! relocatable
          || (symbol->flags & BSF_SECTION_SYM) != 0))
     {
-      if (relocateable)
+      if (relocatable)
        {
          /* Make up a value.  */
          *pgp = symbol->section->output_section->vma /*+ 0x4000*/;
@@ -1564,38 +1609,35 @@ mips_elf64_gprel16_reloc (abfd, reloc_entry, symbol, data, input_section,
      bfd *output_bfd;
      char **error_message;
 {
-  bfd_boolean relocateable;
+  bfd_boolean relocatable;
   bfd_reloc_status_type ret;
   bfd_vma gp;
 
-  /* If we're relocating, and this is an external symbol with no
-     addend, we don't want to change anything.  We will only have an
-     addend if this is a newly created reloc, not read from an ELF
-     file.  */
+  /* If we're relocating, and this is an external symbol, we don't want
+     to change anything.  */
   if (output_bfd != (bfd *) NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && (! reloc_entry->howto->partial_inplace
-         || reloc_entry->addend == 0))
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       reloc_entry->address += input_section->output_offset;
       return bfd_reloc_ok;
     }
 
   if (output_bfd != (bfd *) NULL)
-    relocateable = TRUE;
+    relocatable = TRUE;
   else
     {
-      relocateable = FALSE;
+      relocatable = FALSE;
       output_bfd = symbol->section->output_section->owner;
     }
 
-  ret = mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message,
+  ret = mips_elf64_final_gp (output_bfd, symbol, relocatable, error_message,
                             &gp);
   if (ret != bfd_reloc_ok)
     return ret;
 
   return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
-                                       input_section, relocateable,
+                                       input_section, relocatable,
                                        data, gp);
 }
 
@@ -1612,7 +1654,7 @@ mips_elf64_literal_reloc (abfd, reloc_entry, symbol, data, input_section,
      bfd *output_bfd;
      char **error_message;
 {
-  bfd_boolean relocateable;
+  bfd_boolean relocatable;
   bfd_reloc_status_type ret;
   bfd_vma gp;
 
@@ -1620,8 +1662,7 @@ mips_elf64_literal_reloc (abfd, reloc_entry, symbol, data, input_section,
      want to change anything.  */
   if (output_bfd != (bfd *) NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && (! reloc_entry->howto->partial_inplace
-         || reloc_entry->addend == 0))
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       reloc_entry->address += input_section->output_offset;
       return bfd_reloc_ok;
@@ -1629,20 +1670,20 @@ mips_elf64_literal_reloc (abfd, reloc_entry, symbol, data, input_section,
 
   /* FIXME: The entries in the .lit8 and .lit4 sections should be merged.  */
   if (output_bfd != (bfd *) NULL)
-    relocateable = TRUE;
+    relocatable = TRUE;
   else
     {
-      relocateable = FALSE;
+      relocatable = FALSE;
       output_bfd = symbol->section->output_section->owner;
     }
 
-  ret = mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message,
+  ret = mips_elf64_final_gp (output_bfd, symbol, relocatable, error_message,
                             &gp);
   if (ret != bfd_reloc_ok)
     return ret;
 
   return _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
-                                       input_section, relocateable,
+                                       input_section, relocatable,
                                        data, gp);
 }
 
@@ -1660,19 +1701,17 @@ mips_elf64_gprel32_reloc (abfd, reloc_entry, symbol, data, input_section,
      bfd *output_bfd;
      char **error_message;
 {
-  bfd_boolean relocateable;
+  bfd_boolean relocatable;
   bfd_reloc_status_type ret;
   bfd_vma gp;
   bfd_vma relocation;
-  unsigned long val;
+  bfd_vma val;
 
-  /* If we're relocating, and this is an external symbol with no
-     addend, we don't want to change anything.  We will only have an
-     addend if this is a newly created reloc, not read from an ELF
-     file.  */
+  /* If we're relocating, and this is an external symbol, we don't want
+     to change anything.  */
   if (output_bfd != (bfd *) NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && reloc_entry->addend == 0)
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       *error_message = (char *)
        _("32bits gp relative relocation occurs for an external symbol");
@@ -1680,21 +1719,18 @@ mips_elf64_gprel32_reloc (abfd, reloc_entry, symbol, data, input_section,
     }
 
   if (output_bfd != (bfd *) NULL)
-    {
-      relocateable = TRUE;
-      gp = _bfd_get_gp_value (output_bfd);
-    }
+    relocatable = TRUE;
   else
     {
-      relocateable = FALSE;
+      relocatable = FALSE;
       output_bfd = symbol->section->output_section->owner;
-
-      ret = mips_elf64_final_gp (output_bfd, symbol, relocateable,
-                                error_message, &gp);
-      if (ret != bfd_reloc_ok)
-       return ret;
     }
 
+    ret = mips_elf64_final_gp (output_bfd, symbol, relocatable,
+                              error_message, &gp);
+    if (ret != bfd_reloc_ok)
+      return ret;
+
   if (bfd_is_com_section (symbol->section))
     relocation = 0;
   else
@@ -1706,27 +1742,25 @@ mips_elf64_gprel32_reloc (abfd, reloc_entry, symbol, data, input_section,
   if (reloc_entry->address > input_section->_cooked_size)
     return bfd_reloc_outofrange;
 
-  if (reloc_entry->howto->src_mask == 0)
-    {
-      /* This case arises with the 64-bit MIPS ELF ABI.  */
-      val = 0;
-    }
-  else
-    val = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
-
   /* Set val to the offset into the section or symbol.  */
-  val += reloc_entry->addend;
+  val = reloc_entry->addend;
+
+  if (reloc_entry->howto->partial_inplace)
+    val += bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
 
   /* Adjust val for the final section location and GP value.  If we
-     are producing relocateable output, we don't want to do this for
+     are producing relocatable output, we don't want to do this for
      an external symbol.  */
-  if (! relocateable
+  if (! relocatable
       || (symbol->flags & BSF_SECTION_SYM) != 0)
     val += relocation - gp;
 
-  bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
+  if (reloc_entry->howto->partial_inplace)
+    bfd_put_32 (abfd, val, (bfd_byte *) data + reloc_entry->address);
+  else
+    reloc_entry->addend = val;
 
-  if (relocateable)
+  if (relocatable)
     reloc_entry->address += input_section->output_offset;
 
   return bfd_reloc_ok;
@@ -1750,15 +1784,17 @@ mips_elf64_shift6_reloc (abfd, reloc_entry, symbol, data, input_section,
      want to change anything.  */
   if (output_bfd != (bfd *) NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && (! reloc_entry->howto->partial_inplace
-         || reloc_entry->addend == 0))
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       reloc_entry->address += input_section->output_offset;
       return bfd_reloc_ok;
     }
 
-  reloc_entry->addend = (reloc_entry->addend & 0x00007c0)
-                       | (reloc_entry->addend & 0x00000800) >> 9;
+  if (reloc_entry->howto->partial_inplace)
+    {
+      reloc_entry->addend = ((reloc_entry->addend & 0x00007c0)
+                            | (reloc_entry->addend & 0x00000800) >> 9);
+    }
 
   return bfd_reloc_continue;
 }
@@ -1812,33 +1848,33 @@ mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
      bfd *output_bfd;
      char **error_message;
 {
-  bfd_boolean relocateable;
+  bfd_boolean relocatable;
   bfd_reloc_status_type ret;
   bfd_vma gp;
-  unsigned short extend, insn;
-  unsigned long final;
+  unsigned short extend = 0;
+  unsigned short insn = 0;
+  bfd_signed_vma val;
+  bfd_vma relocation;
 
   /* If we're relocating, and this is an external symbol with no
-     addend, we don't want to change anything.  We will only have an
-     addend if this is a newly created reloc, not read from an ELF
-     file.  */
+     addend, we don't want to change anything.  */
   if (output_bfd != NULL
       && (symbol->flags & BSF_SECTION_SYM) == 0
-      && reloc_entry->addend == 0)
+      && (symbol->flags & BSF_LOCAL) != 0)
     {
       reloc_entry->address += input_section->output_offset;
       return bfd_reloc_ok;
     }
 
   if (output_bfd != NULL)
-    relocateable = TRUE;
+    relocatable = TRUE;
   else
     {
-      relocateable = FALSE;
+      relocatable = FALSE;
       output_bfd = symbol->section->output_section->owner;
     }
 
-  ret = mips_elf64_final_gp (output_bfd, symbol, relocateable, error_message,
+  ret = mips_elf64_final_gp (output_bfd, symbol, relocatable, error_message,
                             &gp);
   if (ret != bfd_reloc_ok)
     return ret;
@@ -1846,33 +1882,55 @@ mips16_gprel_reloc (abfd, reloc_entry, symbol, data, input_section,
   if (reloc_entry->address > input_section->_cooked_size)
     return bfd_reloc_outofrange;
 
-  /* Pick up the mips16 extend instruction and the real instruction.  */
-  extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
-  insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
-
-  /* Stuff the current addend back as a 32 bit value, do the usual
-     relocation, and then clean up.  */
-  bfd_put_32 (abfd,
-             (bfd_vma) (((extend & 0x1f) << 11)
-                        | (extend & 0x7e0)
-                        | (insn & 0x1f)),
-             (bfd_byte *) data + reloc_entry->address);
-
-  ret = _bfd_mips_elf_gprel16_with_gp (abfd, symbol, reloc_entry,
-                                      input_section, relocateable, data, gp);
-
-  final = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
-  bfd_put_16 (abfd,
-             (bfd_vma) ((extend & 0xf800)
-                        | ((final >> 11) & 0x1f)
-                        | (final & 0x7e0)),
-             (bfd_byte *) data + reloc_entry->address);
-  bfd_put_16 (abfd,
-             (bfd_vma) ((insn & 0xffe0)
-                        | (final & 0x1f)),
-             (bfd_byte *) data + reloc_entry->address + 2);
+  if (bfd_is_com_section (symbol->section))
+    relocation = 0;
+  else
+    relocation = symbol->value;
+
+  relocation += symbol->section->output_section->vma;
+  relocation += symbol->section->output_offset;
 
-  return ret;
+  /* Set val to the offset into the section or symbol.  */
+  val = reloc_entry->addend;
+
+  if (reloc_entry->howto->partial_inplace)
+    {
+      /* Pick up the mips16 extend instruction and the real instruction.  */
+      extend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address);
+      insn = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
+      val += ((extend & 0x1f) << 11) | (extend & 0x7e0) | (insn & 0x1f);
+    }
+
+  _bfd_mips_elf_sign_extend(val, 16);
+
+  /* Adjust val for the final section location and GP value.  If we
+     are producing relocatable output, we don't want to do this for
+     an external symbol.  */
+  if (! relocatable
+      || (symbol->flags & BSF_SECTION_SYM) != 0)
+    val += relocation - gp;
+
+  if (reloc_entry->howto->partial_inplace)
+    {
+      bfd_put_16 (abfd,
+                 (bfd_vma) ((extend & 0xf800)
+                            | ((val >> 11) & 0x1f)
+                            | (val & 0x7e0)),
+                 (bfd_byte *) data + reloc_entry->address);
+      bfd_put_16 (abfd,
+                 (bfd_vma) ((insn & 0xffe0)
+                            | (val & 0x1f)),
+                 (bfd_byte *) data + reloc_entry->address + 2);
+    }
+  else
+    reloc_entry->addend = val;
+
+  if (relocatable)
+    reloc_entry->address += input_section->output_offset;
+  else if (((val & ~0xffff) != ~0xffff) && ((val & ~0xffff) != 0))
+    return bfd_reloc_overflow;
+
+  return bfd_reloc_ok;
 }
 \f
 /* A mapping from BFD reloc types to MIPS ELF reloc types.  */
@@ -1950,6 +2008,8 @@ bfd_elf64_bfd_reloc_type_lookup (abfd, code)
       return &elf_mips_gnu_vtinherit_howto;
     case BFD_RELOC_VTABLE_ENTRY:
       return &elf_mips_gnu_vtentry_howto;
+    case BFD_RELOC_16_PCREL_S2:
+      return &elf_mips_gnu_rela16_s2;
     default:
       bfd_set_error (bfd_error_bad_value);
       return NULL;
@@ -1973,6 +2033,11 @@ mips_elf64_rtype_to_howto (r_type, rela_p)
       return &elf_mips_gnu_vtinherit_howto;
     case R_MIPS_GNU_VTENTRY:
       return &elf_mips_gnu_vtentry_howto;
+    case R_MIPS_GNU_REL16_S2:
+      if (rela_p)
+       return &elf_mips_gnu_rela16_s2;
+      else
+       return &elf_mips_gnu_rel16_s2;
     default:
       BFD_ASSERT (r_type < (unsigned int) R_MIPS_max);
       if (rela_p)
@@ -2014,20 +2079,104 @@ mips_elf64_get_reloc_upper_bound (abfd, sec)
   return (sec->reloc_count * 3 + 1) * sizeof (arelent *);
 }
 
-/* Read the relocations from one reloc section.  */
+static long
+mips_elf64_get_dynamic_reloc_upper_bound (abfd)
+     bfd *abfd;
+{
+  return _bfd_elf_get_dynamic_reloc_upper_bound (abfd) * 3;
+}
+
+/* We must also copy more relocations than the corresponding functions
+   in elf.c would, so the two following functions are slightly
+   modified from elf.c, that multiply the external relocation count by
+   3 to obtain the internal relocation count.  */
+
+static long
+mips_elf64_canonicalize_reloc (abfd, section, relptr, symbols)
+     bfd *abfd;
+     sec_ptr section;
+     arelent **relptr;
+     asymbol **symbols;
+{
+  arelent *tblptr;
+  unsigned int i;
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
+
+  if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
+    return -1;
+
+  tblptr = section->relocation;
+  for (i = 0; i < section->reloc_count * 3; i++)
+    *relptr++ = tblptr++;
+
+  *relptr = NULL;
+
+  return section->reloc_count * 3;
+}
+
+static long
+mips_elf64_canonicalize_dynamic_reloc (abfd, storage, syms)
+     bfd *abfd;
+     arelent **storage;
+     asymbol **syms;
+{
+  bfd_boolean (*slurp_relocs)
+    PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
+  asection *s;
+  long ret;
+
+  if (elf_dynsymtab (abfd) == 0)
+    {
+      bfd_set_error (bfd_error_invalid_operation);
+      return -1;
+    }
+
+  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
+  ret = 0;
+  for (s = abfd->sections; s != NULL; s = s->next)
+    {
+      if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
+         && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
+             || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
+       {
+         arelent *p;
+         long count, i;
+
+         if (! (*slurp_relocs) (abfd, s, syms, TRUE))
+           return -1;
+         count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize * 3;
+         p = s->relocation;
+         for (i = 0; i < count; i++)
+           *storage++ = p++;
+         ret += count;
+       }
+    }
+
+  *storage = NULL;
+
+  return ret;
+}
+
+/* Read the relocations from one reloc section.  This is mostly copied
+   from elfcode.h, except for the changes to expand one external
+   relocation to 3 internal ones.  We must unfortunately set
+   reloc_count to the number of external relocations, because a lot of
+   generic code seems to depend on this.  */
 
 static bfd_boolean
-mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, rel_hdr)
+mips_elf64_slurp_one_reloc_table (abfd, asect, rel_hdr, reloc_count,
+                                 relents, symbols, dynamic)
      bfd *abfd;
      asection *asect;
+     Elf_Internal_Shdr *rel_hdr;
+     bfd_size_type reloc_count;
+     arelent *relents;
      asymbol **symbols;
-     const Elf_Internal_Shdr *rel_hdr;
+     bfd_boolean dynamic;
 {
   PTR allocated = NULL;
   bfd_byte *native_relocs;
-  arelent *relents;
   arelent *relent;
-  bfd_vma count;
   bfd_vma i;
   int entsize;
   reloc_howto_type *howto_table;
@@ -2037,26 +2186,24 @@ mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, rel_hdr)
     return FALSE;
 
   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
-      || (bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size))
+      || (bfd_bread (allocated, rel_hdr->sh_size, abfd)
+         != rel_hdr->sh_size))
     goto error_return;
 
   native_relocs = (bfd_byte *) allocated;
 
-  relents = asect->relocation + asect->reloc_count;
-
   entsize = rel_hdr->sh_entsize;
   BFD_ASSERT (entsize == sizeof (Elf64_Mips_External_Rel)
              || entsize == sizeof (Elf64_Mips_External_Rela));
 
-  count = rel_hdr->sh_size / entsize;
-
   if (entsize == sizeof (Elf64_Mips_External_Rel))
     howto_table = mips_elf64_howto_table_rel;
   else
     howto_table = mips_elf64_howto_table_rela;
 
-  relent = relents;
-  for (i = 0; i < count; i++, native_relocs += entsize)
+  for (i = 0, relent = relents;
+       i < reloc_count;
+       i++, native_relocs += entsize)
     {
       Elf64_Mips_Internal_Rela rela;
       bfd_boolean used_sym, used_ssym;
@@ -2158,7 +2305,7 @@ mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, rel_hdr)
             object file, and absolute for an executable file or
             shared library.  The address of a BFD reloc is always
             section relative.  */
-         if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
+         if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
            relent->address = rela.r_offset;
          else
            relent->address = rela.r_offset - asect->vma;
@@ -2185,7 +2332,10 @@ mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, rel_hdr)
 }
 
 /* Read the relocations.  On Irix 6, there can be two reloc sections
-   associated with a single data section.  */
+   associated with a single data section.  This is copied from
+   elfcode.h as well, with changes as small as accounting for 3
+   internal relocs per external reloc and resetting reloc_count to
+   zero before processing the relocs of a section.  */
 
 static bfd_boolean
 mips_elf64_slurp_reloc_table (abfd, asect, symbols, dynamic)
@@ -2194,39 +2344,72 @@ mips_elf64_slurp_reloc_table (abfd, asect, symbols, dynamic)
      asymbol **symbols;
      bfd_boolean dynamic;
 {
-  bfd_size_type amt;
   struct bfd_elf_section_data * const d = elf_section_data (asect);
+  Elf_Internal_Shdr *rel_hdr;
+  Elf_Internal_Shdr *rel_hdr2;
+  bfd_size_type reloc_count;
+  bfd_size_type reloc_count2;
+  arelent *relents;
+  bfd_size_type amt;
 
-  if (dynamic)
+  if (asect->relocation != NULL)
+    return TRUE;
+
+  if (! dynamic)
     {
-      bfd_set_error (bfd_error_invalid_operation);
-      return FALSE;
-    }
+      if ((asect->flags & SEC_RELOC) == 0
+         || asect->reloc_count == 0)
+       return TRUE;
 
-  if (asect->relocation != NULL
-      || (asect->flags & SEC_RELOC) == 0
-      || asect->reloc_count == 0)
-    return TRUE;
+      rel_hdr = &d->rel_hdr;
+      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
+      rel_hdr2 = d->rel_hdr2;
+      reloc_count2 = (rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0);
+
+      BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
+      BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
+                 || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
+
+    }
+  else
+    {
+      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
+        case because relocations against this section may use the
+        dynamic symbol table, and in that case bfd_section_from_shdr
+        in elf.c does not update the RELOC_COUNT.  */
+      if (asect->_raw_size == 0)
+       return TRUE;
+
+      rel_hdr = &d->this_hdr;
+      reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
+      rel_hdr2 = NULL;
+      reloc_count2 = 0;
+    }
 
   /* Allocate space for 3 arelent structures for each Rel structure.  */
-  amt = asect->reloc_count;
-  amt *= 3 * sizeof (arelent);
-  asect->relocation = (arelent *) bfd_alloc (abfd, amt);
-  if (asect->relocation == NULL)
+  amt = (reloc_count + reloc_count2) * 3 * sizeof (arelent);
+  relents = (arelent *) bfd_alloc (abfd, amt);
+  if (relents == NULL)
     return FALSE;
 
   /* The slurp_one_reloc_table routine increments reloc_count.  */
   asect->reloc_count = 0;
 
-  if (! mips_elf64_slurp_one_reloc_table (abfd, asect, symbols, &d->rel_hdr))
+  if (! mips_elf64_slurp_one_reloc_table (abfd, asect,
+                                         rel_hdr, reloc_count,
+                                         relents,
+                                         symbols, dynamic))
     return FALSE;
   if (d->rel_hdr2 != NULL)
     {
-      if (! mips_elf64_slurp_one_reloc_table (abfd, asect, symbols,
-                                             d->rel_hdr2))
+      if (! mips_elf64_slurp_one_reloc_table (abfd, asect,
+                                             rel_hdr2, reloc_count2,
+                                             relents + reloc_count * 3,
+                                             symbols, dynamic))
        return FALSE;
     }
 
+  asect->relocation = relents;
   return TRUE;
 }
 
@@ -2529,6 +2712,71 @@ elf64_mips_irix_compat (abfd)
     return ict_none;
 }
 \f
+/* Support for core dump NOTE sections.  */
+static bfd_boolean
+elf64_mips_grok_prstatus (abfd, note)
+     bfd *abfd;
+     Elf_Internal_Note *note;
+{
+  int offset;
+  unsigned int raw_size;
+
+  switch (note->descsz)
+    {
+      default:
+       return FALSE;
+
+      case 480:                /* Linux/MIPS - N64 kernel */
+       /* 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 + 32);
+
+       /* pr_reg */
+       offset = 112;
+       raw_size = 360;
+
+       break;
+    }
+
+  /* Make a ".reg/999" section.  */
+  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
+                                         raw_size, note->descpos + offset);
+}
+
+static bfd_boolean
+elf64_mips_grok_psinfo (abfd, note)
+     bfd *abfd;
+     Elf_Internal_Note *note;
+{
+  switch (note->descsz)
+    {
+      default:
+       return FALSE;
+
+      case 136:                /* Linux/MIPS - N64 kernel elf_prpsinfo */
+       elf_tdata (abfd)->core_program
+        = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
+       elf_tdata (abfd)->core_command
+        = _bfd_elfcore_strndup (abfd, note->descdata + 56, 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);
+
+    if (0 < n && command[n - 1] == ' ')
+      command[n - 1] = '\0';
+  }
+
+  return TRUE;
+}
+\f
 /* ECOFF swapping routines.  These are used when dealing with the
    .mdebug section, which is in the ECOFF debugging format.  */
 static const struct ecoff_debug_swap mips_elf64_ecoff_debug_swap =
@@ -2589,7 +2837,7 @@ const struct elf_size_info mips_elf64_size_info =
   4,            /* hash-table entry size */
   3,            /* internal relocations per external relocations */
   64,          /* arch_size */
-  8,           /* file_align */
+  3,           /* log_file_align */
   ELFCLASS64,
   EV_CURRENT,
   bfd_elf64_write_out_phdrs,
@@ -2659,6 +2907,9 @@ const struct elf_size_info mips_elf64_size_info =
 #define elf_backend_ecoff_debug_swap   &mips_elf64_ecoff_debug_swap
 #define elf_backend_size_info          mips_elf64_size_info
 
+#define elf_backend_grok_prstatus      elf64_mips_grok_prstatus
+#define elf_backend_grok_psinfo                elf64_mips_grok_psinfo
+
 #define elf_backend_got_header_size    (4 * MIPS_RESERVED_GOTNO)
 #define elf_backend_plt_header_size    0
 
@@ -2674,6 +2925,7 @@ const struct elf_size_info mips_elf64_size_info =
    MIPS-specific function only applies to IRIX5, which had no 64-bit
    ABI.  */
 #define bfd_elf64_find_nearest_line    _bfd_mips_elf_find_nearest_line
+#define bfd_elf64_new_section_hook     _bfd_mips_elf_new_section_hook
 #define bfd_elf64_set_section_contents _bfd_mips_elf_set_section_contents
 #define bfd_elf64_bfd_get_relocated_section_contents \
                                _bfd_elf_mips_get_relocated_section_contents
@@ -2687,6 +2939,10 @@ const struct elf_size_info mips_elf64_size_info =
                                _bfd_mips_elf_print_private_bfd_data
 
 #define bfd_elf64_get_reloc_upper_bound mips_elf64_get_reloc_upper_bound
+#define bfd_elf64_canonicalize_reloc mips_elf64_canonicalize_reloc
+#define bfd_elf64_get_dynamic_reloc_upper_bound mips_elf64_get_dynamic_reloc_upper_bound
+#define bfd_elf64_canonicalize_dynamic_reloc mips_elf64_canonicalize_dynamic_reloc
+#define bfd_elf64_bfd_relax_section     _bfd_mips_relax_section
 
 /* MIPS ELF64 archive functions.  */
 #define bfd_elf64_archive_functions
This page took 0.033691 seconds and 4 git commands to generate.