* elfxx-target.h: Remove PTR cast.
[deliverable/binutils-gdb.git] / bfd / elf64-mips.c
index 404504ae3d609c15a7f11f768462a5d2dcc5d705..041c22262be45de8e75a973558ab9f33b3f8adac 100644 (file)
@@ -306,11 +306,9 @@ static reloc_howto_type mips_elf64_howto_table_rel[] =
         0x0000ffff,            /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* 16 bit PC relative reference.  Note that the ABI document has a typo
-     and claims R_MIPS_PC16 to be not rightshifted, rendering it useless.
-     We do the right thing here.  */
+  /* 16 bit PC relative reference.  */
   HOWTO (R_MIPS_PC16,          /* type */
-        2,                     /* rightshift */
+        0,                     /* rightshift */
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
         16,                    /* bitsize */
         TRUE,                  /* pc_relative */
@@ -803,11 +801,9 @@ static reloc_howto_type mips_elf64_howto_table_rela[] =
         0x0000ffff,            /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* 16 bit PC relative reference.  Note that the ABI document has a typo
-     and claims R_MIPS_PC16 to be not rightshifted, rendering it useless.
-     We do the right thing here.  */
+  /* 16 bit PC relative reference.  */
   HOWTO (R_MIPS_PC16,          /* type */
-        2,                     /* rightshift */
+        0,                     /* rightshift */
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
         16,                    /* bitsize */
         TRUE,                  /* pc_relative */
@@ -1231,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
@@ -1419,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;
 }
@@ -1460,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);
 
@@ -1525,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;
@@ -1544,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*/;
@@ -1578,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);
 }
 
@@ -1626,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;
 
@@ -1634,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;
@@ -1643,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);
 }
 
@@ -1674,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");
@@ -1694,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
@@ -1720,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;
@@ -1764,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;
 }
@@ -1826,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;
@@ -1860,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.  */
@@ -1904,7 +1948,7 @@ static const struct elf_reloc_map mips_reloc_map[] =
   /* There is no BFD reloc for R_MIPS_REL32.  */
   { BFD_RELOC_64, R_MIPS_64 },
   { BFD_RELOC_CTOR, R_MIPS_64 },
-  { BFD_RELOC_16_PCREL_S2, R_MIPS_PC16 },
+  { BFD_RELOC_16_PCREL, R_MIPS_PC16 },
   { BFD_RELOC_HI16_S, R_MIPS_HI16 },
   { BFD_RELOC_LO16, R_MIPS_LO16 },
   { BFD_RELOC_GPREL16, R_MIPS_GPREL16 },
@@ -1964,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;
@@ -1987,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)
@@ -2049,7 +2100,7 @@ mips_elf64_canonicalize_reloc (abfd, section, relptr, symbols)
 {
   arelent *tblptr;
   unsigned int i;
-  struct elf_backend_data *bed = get_elf_backend_data (abfd);
+  const struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
     return -1;
@@ -2786,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,
@@ -2891,6 +2942,7 @@ const struct elf_size_info mips_elf64_size_info =
 #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.034345 seconds and 4 git commands to generate.