* elfxx-mips.c (MIPS_RELOC_RELA_P): New macro.
[deliverable/binutils-gdb.git] / bfd / elfxx-mips.c
index 3c8bb5d8be8a1f7d3031ccf1bae9a4ec73960b0e..98dfdd8547814a7d5bc3bd584a2a409904b70b4d 100644 (file)
@@ -345,7 +345,7 @@ static bfd_reloc_status_type mips_elf_calculate_relocation
   PARAMS ((bfd *, bfd *, asection *, struct bfd_link_info *,
           const Elf_Internal_Rela *, bfd_vma, reloc_howto_type *,
           Elf_Internal_Sym *, asection **, bfd_vma *, const char **,
-          boolean *));
+          boolean *, boolean));
 static bfd_vma mips_elf_obtain_contents
   PARAMS ((reloc_howto_type *, const Elf_Internal_Rela *, bfd *, bfd_byte *));
 static boolean mips_elf_perform_relocation
@@ -443,6 +443,23 @@ static bfd *reldyn_sorting_bfd;
 #define MIPS_ELF_RTYPE_TO_HOWTO(abfd, rtype, rela)                     \
   (get_elf_backend_data (abfd)->elf_backend_mips_rtype_to_howto (rtype, rela))
 
+/* Determine whether the internal relocation of index REL_IDX is REL
+   (zero) or RELA (non-zero).  The assumption is that, if there are
+   two relocation sections for this section, one of them is REL and
+   the other is RELA.  If the index of the relocation we're testing is
+   in range for the first relocation section, check that the external
+   relocation size is that for RELA.  It is also assumed that, if
+   rel_idx is not in range for the first section, and this first
+   section contains REL relocs, then the relocation is in the second
+   section, that is RELA.  */
+#define MIPS_RELOC_RELA_P(abfd, sec, rel_idx)                          \
+  ((NUM_SHDR_ENTRIES (&elf_section_data (sec)->rel_hdr)                        \
+    * get_elf_backend_data (abfd)->s->int_rels_per_ext_rel             \
+    > (bfd_vma)(rel_idx))                                              \
+   == (elf_section_data (sec)->rel_hdr.sh_entsize                      \
+       == (ABI_64_P (abfd) ? sizeof (Elf64_External_Rela)              \
+          : sizeof (Elf32_External_Rela))))
+
 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
    from smaller values.  Start with zero, widen, *then* decrement.  */
 #define MINUS_ONE      (((bfd_vma)0) - 1)
@@ -475,6 +492,8 @@ static bfd *reldyn_sorting_bfd;
     : "/usr/lib/libc.so.1")
 
 #ifdef BFD64
+#define MNAME(bfd,pre,pos) \
+  (ABI_64_P (bfd) ? CONCAT4 (pre,64,_,pos) : CONCAT4 (pre,32,_,pos))
 #define ELF_R_SYM(bfd, i)                                      \
   (ABI_64_P (bfd) ? ELF64_R_SYM (i) : ELF32_R_SYM (i))
 #define ELF_R_TYPE(bfd, i)                                     \
@@ -482,6 +501,7 @@ static bfd *reldyn_sorting_bfd;
 #define ELF_R_INFO(bfd, s, t)                                  \
   (ABI_64_P (bfd) ? ELF64_R_INFO (s, t) : ELF32_R_INFO (s, t))
 #else
+#define MNAME(bfd,pre,pos) CONCAT4 (pre,32,_,pos)
 #define ELF_R_SYM(bfd, i)                                      \
   (ELF32_R_SYM (i))
 #define ELF_R_TYPE(bfd, i)                                     \
@@ -1901,6 +1921,7 @@ mips_elf_create_got_section (abfd, info)
   flagword flags;
   register asection *s;
   struct elf_link_hash_entry *h;
+  struct bfd_link_hash_entry *bh;
   struct mips_got_info *g;
   bfd_size_type amt;
 
@@ -1920,13 +1941,14 @@ mips_elf_create_got_section (abfd, info)
   /* Define the symbol _GLOBAL_OFFSET_TABLE_.  We don't do this in the
      linker script because we don't want to define the symbol if we
      are not creating a global offset table.  */
-  h = NULL;
+  bh = NULL;
   if (! (_bfd_generic_link_add_one_symbol
         (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
          (bfd_vma) 0, (const char *) NULL, false,
-         get_elf_backend_data (abfd)->collect,
-         (struct bfd_link_hash_entry **) &h)))
+         get_elf_backend_data (abfd)->collect, &bh)))
     return false;
+
+  h = (struct elf_link_hash_entry *) bh;
   h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
   h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
   h->type = STT_OBJECT;
@@ -2004,7 +2026,7 @@ static bfd_reloc_status_type
 mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
                               relocation, addend, howto, local_syms,
                               local_sections, valuep, namep,
-                              require_jalxp)
+                              require_jalxp, save_addend)
      bfd *abfd;
      bfd *input_bfd;
      asection *input_section;
@@ -2017,6 +2039,7 @@ mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
      bfd_vma *valuep;
      const char **namep;
      boolean *require_jalxp;
+     boolean save_addend;
 {
   /* The eventual value we will return.  */
   bfd_vma value;
@@ -2041,7 +2064,7 @@ mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
   struct mips_elf_link_hash_entry *h = NULL;
   /* True if the symbol referred to by this relocation is a local
      symbol.  */
-  boolean local_p;
+  boolean local_p, was_local_p;
   /* True if the symbol referred to by this relocation is "_gp_disp".  */
   boolean gp_disp_p = false;
   Elf_Internal_Shdr *symtab_hdr;
@@ -2069,6 +2092,7 @@ mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   local_p = mips_elf_local_relocation_p (input_bfd, relocation,
                                         local_sections, false);
+  was_local_p = local_p;
   if (! elf_bad_symtab (input_bfd))
     extsymoff = symtab_hdr->sh_info;
   else
@@ -2456,10 +2480,19 @@ mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
         order.  We don't need to do anything special here; the
         differences are handled in mips_elf_perform_relocation.  */
     case R_MIPS_GPREL16:
-      if (local_p)
-       value = mips_elf_sign_extend (addend, 16) + symbol + gp0 - gp;
-      else
-       value = mips_elf_sign_extend (addend, 16) + symbol - gp;
+      /* Only sign-extend the addend if it was extracted from the
+        instruction.  If the addend was separate, leave it alone,
+        otherwise we may lose significant bits.  */
+      if (howto->partial_inplace)
+       addend = mips_elf_sign_extend (addend, 16);
+      value = symbol + addend - gp;
+      /* If the symbol was local, any earlier relocatable links will
+        have adjusted its addend with the gp offset, so compensate
+        for that now.  Don't do it for symbols forced local in this
+        link, though, since they won't have had the gp offset applied
+        to them before.  */
+      if (was_local_p)
+       value += gp0;
       overflowed_p = mips_elf_overflow_p (value, 16);
       break;
 
@@ -2492,7 +2525,9 @@ mips_elf_calculate_relocation (abfd, input_bfd, input_section, info,
       break;
 
     case R_MIPS_GPREL32:
-      value = (addend + symbol + gp0 - gp) & howto->dst_mask;
+      value = (addend + symbol + gp0 - gp);
+      if (!save_addend)
+       value &= howto->dst_mask;
       break;
 
     case R_MIPS_PC16:
@@ -2938,6 +2973,12 @@ mips_elf_create_dynamic_relocation (output_bfd, info, rel, h, sec,
         know where the shared library will wind up at load-time.  */
       outrel[0].r_info = ELF_R_INFO (output_bfd, (unsigned long) indx,
                                     R_MIPS_REL32);
+      outrel[1].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
+                                    ABI_64_P (output_bfd)
+                                    ? R_MIPS_64
+                                    : R_MIPS_NONE);
+      outrel[2].r_info = ELF_R_INFO (output_bfd, (unsigned long) 0,
+                                    R_MIPS_NONE);
 
       /* Adjust the output offset of the relocation to reference the
         correct location in the output file.  */
@@ -3825,15 +3866,17 @@ _bfd_mips_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
       && strcmp (*namep, "__rld_obj_head") == 0)
     {
       struct elf_link_hash_entry *h;
+      struct bfd_link_hash_entry *bh;
 
       /* Mark __rld_obj_head as dynamic.  */
-      h = NULL;
+      bh = NULL;
       if (! (_bfd_generic_link_add_one_symbol
             (info, abfd, *namep, BSF_GLOBAL, *secp,
              (bfd_vma) *valp, (const char *) NULL, false,
-             get_elf_backend_data (abfd)->collect,
-             (struct bfd_link_hash_entry **) &h)))
+             get_elf_backend_data (abfd)->collect, &bh)))
        return false;
+
+      h = (struct elf_link_hash_entry *) bh;
       h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
       h->type = STT_OBJECT;
@@ -3889,6 +3932,7 @@ _bfd_mips_elf_create_dynamic_sections (abfd, info)
      struct bfd_link_info *info;
 {
   struct elf_link_hash_entry *h;
+  struct bfd_link_hash_entry *bh;
   flagword flags;
   register asection *s;
   const char * const *namep;
@@ -3947,13 +3991,14 @@ _bfd_mips_elf_create_dynamic_sections (abfd, info)
     {
       for (namep = mips_elf_dynsym_rtproc_names; *namep != NULL; namep++)
        {
-         h = NULL;
+         bh = NULL;
          if (! (_bfd_generic_link_add_one_symbol
                 (info, abfd, *namep, BSF_GLOBAL, bfd_und_section_ptr,
                  (bfd_vma) 0, (const char *) NULL, false,
-                 get_elf_backend_data (abfd)->collect,
-                 (struct bfd_link_hash_entry **) &h)))
+                 get_elf_backend_data (abfd)->collect, &bh)))
            return false;
+
+         h = (struct elf_link_hash_entry *) bh;
          h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
          h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
          h->type = STT_SECTION;
@@ -3989,26 +4034,17 @@ _bfd_mips_elf_create_dynamic_sections (abfd, info)
 
   if (!info->shared)
     {
-      h = NULL;
-      if (SGI_COMPAT (abfd))
-       {
-         if (!(_bfd_generic_link_add_one_symbol
-               (info, abfd, "_DYNAMIC_LINK", BSF_GLOBAL, bfd_abs_section_ptr,
-                (bfd_vma) 0, (const char *) NULL, false,
-                get_elf_backend_data (abfd)->collect,
-                (struct bfd_link_hash_entry **) &h)))
-           return false;
-       }
-      else
-       {
-         /* For normal mips it is _DYNAMIC_LINKING.  */
-         if (!(_bfd_generic_link_add_one_symbol
-               (info, abfd, "_DYNAMIC_LINKING", BSF_GLOBAL,
-                bfd_abs_section_ptr, (bfd_vma) 0, (const char *) NULL, false,
-                get_elf_backend_data (abfd)->collect,
-                (struct bfd_link_hash_entry **) &h)))
-           return false;
-       }
+      const char *name;
+
+      name = SGI_COMPAT (abfd) ? "_DYNAMIC_LINK" : "_DYNAMIC_LINKING";
+      bh = NULL;
+      if (!(_bfd_generic_link_add_one_symbol
+           (info, abfd, name, BSF_GLOBAL, bfd_abs_section_ptr,
+            (bfd_vma) 0, (const char *) NULL, false,
+            get_elf_backend_data (abfd)->collect, &bh)))
+       return false;
+
+      h = (struct elf_link_hash_entry *) bh;
       h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
       h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
       h->type = STT_SECTION;
@@ -4025,26 +4061,15 @@ _bfd_mips_elf_create_dynamic_sections (abfd, info)
          s = bfd_get_section_by_name (abfd, ".rld_map");
          BFD_ASSERT (s != NULL);
 
-         h = NULL;
-         if (SGI_COMPAT (abfd))
-           {
-             if (!(_bfd_generic_link_add_one_symbol
-                   (info, abfd, "__rld_map", BSF_GLOBAL, s,
-                    (bfd_vma) 0, (const char *) NULL, false,
-                    get_elf_backend_data (abfd)->collect,
-                    (struct bfd_link_hash_entry **) &h)))
-               return false;
-           }
-         else
-           {
-             /* For normal mips the symbol is __RLD_MAP.  */
-             if (!(_bfd_generic_link_add_one_symbol
-                   (info, abfd, "__RLD_MAP", BSF_GLOBAL, s,
-                    (bfd_vma) 0, (const char *) NULL, false,
-                    get_elf_backend_data (abfd)->collect,
-                    (struct bfd_link_hash_entry **) &h)))
-               return false;
-           }
+         name = SGI_COMPAT (abfd) ? "__rld_map" : "__RLD_MAP";
+         bh = NULL;
+         if (!(_bfd_generic_link_add_one_symbol
+               (info, abfd, name, BSF_GLOBAL, s,
+                (bfd_vma) 0, (const char *) NULL, false,
+                get_elf_backend_data (abfd)->collect, &bh)))
+           return false;
+
+         h = (struct elf_link_hash_entry *) bh;
          h->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
          h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
          h->type = STT_OBJECT;
@@ -4123,7 +4148,7 @@ _bfd_mips_elf_check_relocs (abfd, info, sec, relocs)
                              sizeof CALL_FP_STUB - 1) == 0)
                continue;
 
-             sec_relocs = (_bfd_elf32_link_read_relocs
+             sec_relocs = (MNAME(abfd,_bfd_elf,link_read_relocs)
                            (abfd, o, (PTR) NULL,
                             (Elf_Internal_Rela *) NULL,
                             info->keep_memory));
@@ -5036,7 +5061,10 @@ _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
       else
        /* NewABI defaults to RELA relocations.  */
        howto = MIPS_ELF_RTYPE_TO_HOWTO (input_bfd, r_type,
-                                        NEWABI_P (input_bfd));
+                                        NEWABI_P (input_bfd)
+                                        && (MIPS_RELOC_RELA_P
+                                            (input_bfd, input_section,
+                                             rel - relocs)));
 
       if (!use_saved_addend_p)
        {
@@ -5261,7 +5289,8 @@ _bfd_mips_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                                             input_section, info, rel,
                                             addend, howto, local_syms,
                                             local_sections, &value,
-                                            &name, &require_jalx))
+                                            &name, &require_jalx,
+                                            use_saved_addend_p))
        {
        case bfd_reloc_continue:
          /* There's nothing to do.  */
@@ -6131,7 +6160,12 @@ _bfd_mips_elf_modify_segment_map (abfd)
      .dynamic end up in PT_DYNAMIC.  However, we do have to insert a
      PT_OPTIONS segment immediately following the program header
      table.  */
-  if (NEWABI_P (abfd))
+  if (NEWABI_P (abfd)
+      /* On non-IRIX6 new abi, we'll have already created a segment
+        for this section, so don't create another.  I'm not sure this
+        is not also the case for IRIX 6, but I can't test it right
+        now.  */
+      && IRIX_COMPAT (abfd) == ict_irix6)
     {
       for (s = abfd->sections; s; s = s->next)
        if (elf_section_data (s)->this_hdr.sh_type == SHT_MIPS_OPTIONS)
@@ -6470,9 +6504,10 @@ _bfd_mips_elf_discard_info (abfd, cookie, info)
   if (! tdata)
     return false;
 
-  cookie->rels = _bfd_elf32_link_read_relocs (abfd, o, (PTR) NULL,
-                                             (Elf_Internal_Rela *) NULL,
-                                             info->keep_memory);
+  cookie->rels = (MNAME(abfd,_bfd_elf,link_read_relocs)
+                 (abfd, o, (PTR) NULL,
+                  (Elf_Internal_Rela *) NULL,
+                  info->keep_memory));
   if (!cookie->rels)
     {
       free (tdata);
@@ -6484,7 +6519,7 @@ _bfd_mips_elf_discard_info (abfd, cookie, info)
 
   for (i = 0, skip = 0; i < o->_raw_size; i ++)
     {
-      if (_bfd_elf32_reloc_symbol_deleted_p (i * PDR_SIZE, cookie))
+      if (MNAME(abfd,_bfd_elf,reloc_symbol_deleted_p) (i * PDR_SIZE, cookie))
        {
          tdata[i] = 1;
          skip ++;
@@ -7588,17 +7623,7 @@ _bfd_mips_elf_final_link (abfd, info)
     }
 
   /* Invoke the regular ELF backend linker to do all the work.  */
-  if (ABI_64_P (abfd))
-    {
-#ifdef BFD64
-      if (!bfd_elf64_bfd_final_link (abfd, info))
-       return false;
-#else
-      abort ();
-      return false;
-#endif /* BFD64 */
-    }
-  else if (!bfd_elf32_bfd_final_link (abfd, info))
+  if (!MNAME(abfd,bfd_elf,bfd_final_link) (abfd, info))
     return false;
 
   /* Now write out the computed sections.  */
This page took 0.028983 seconds and 4 git commands to generate.