bfd/
[binutils-gdb.git] / bfd / elfxx-mips.c
index 5f8693e46914cc9470442ea33cf4a72b89d82ead..3073f526d1f9b79af3778600ba9da96f50486359 100644 (file)
@@ -1,6 +1,6 @@
 /* MIPS-specific support for ELF
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
-   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
+   2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
    Free Software Foundation, Inc.
 
    Most of the information added by Ian Lance Taylor, Cygnus Support,
@@ -420,11 +420,6 @@ struct mips_elf_link_hash_entry
 struct mips_elf_link_hash_table
 {
   struct elf_link_hash_table root;
-#if 0
-  /* We no longer use this.  */
-  /* String section indices for the dynamic section symbols.  */
-  bfd_size_type dynsym_sec_strindex[SIZEOF_MIPS_DYNSYM_SECNAMES];
-#endif
 
   /* The number of .rtproc entries.  */
   bfd_size_type procedure_count;
@@ -432,8 +427,8 @@ struct mips_elf_link_hash_table
   /* The size of the .compact_rel section (if SGI_COMPAT).  */
   bfd_size_type compact_rel_size;
 
-  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic
-     entry is set to the address of __rld_obj_head as in IRIX5.  */
+  /* This flag indicates that the value of DT_MIPS_RLD_MAP dynamic entry
+     is set to the address of __rld_obj_head as in IRIX5 and IRIX6.  */
   bfd_boolean use_rld_obj_head;
 
   /* The  __rld_map or __rld_obj_head symbol. */
@@ -515,6 +510,22 @@ struct mips_htab_traverse_info
   bfd_boolean error;
 };
 
+/* MIPS ELF private object data.  */
+
+struct mips_elf_obj_tdata
+{
+  /* Generic ELF private object data.  */
+  struct elf_obj_tdata root;
+
+  /* Input BFD providing Tag_GNU_MIPS_ABI_FP attribute for output.  */
+  bfd *abi_fp_bfd;
+};
+
+/* Get MIPS ELF private object data from BFD's tdata.  */
+
+#define mips_elf_tdata(bfd) \
+  ((struct mips_elf_obj_tdata *) (bfd)->tdata.any)
+
 #define TLS_RELOC_P(r_type) \
   (r_type == R_MIPS_TLS_DTPMOD32               \
    || r_type == R_MIPS_TLS_DTPMOD64            \
@@ -1115,6 +1126,15 @@ mips_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   return (struct bfd_hash_entry *) ret;
 }
 
+/* Allocate MIPS ELF private object data.  */
+
+bfd_boolean
+_bfd_mips_elf_mkobject (bfd *abfd)
+{
+  return bfd_elf_allocate_object (abfd, sizeof (struct mips_elf_obj_tdata),
+                                 MIPS_ELF_DATA);
+}
+
 bfd_boolean
 _bfd_mips_elf_new_section_hook (bfd *abfd, asection *sec)
 {
@@ -2962,6 +2982,10 @@ mips_elf_count_global_tls_entries (void *arg1, void *arg2)
     = (struct mips_elf_link_hash_entry *) arg1;
   struct mips_elf_count_tls_arg *arg = arg2;
 
+  if (hm->root.root.type == bfd_link_hash_indirect
+      || hm->root.root.type == bfd_link_hash_warning)
+    return 1;
+
   if (hm->tls_type & GOT_TLS_GD)
     arg->needed += 2;
   if (hm->tls_type & GOT_TLS_IE)
@@ -2980,6 +3004,10 @@ mips_elf_count_global_tls_relocs (void *arg1, void *arg2)
     = (struct mips_elf_link_hash_entry *) arg1;
   struct mips_elf_count_tls_arg *arg = arg2;
 
+  if (hm->root.root.type == bfd_link_hash_indirect
+      || hm->root.root.type == bfd_link_hash_warning)
+    return 1;
+
   arg->needed += mips_tls_got_relocs (arg->info, hm->tls_type, &hm->root);
 
   return 1;
@@ -3620,15 +3648,11 @@ mips_elf_sort_hash_table_f (struct mips_elf_link_hash_entry *h, void *data)
       break;
 
     case GGA_NORMAL:
-      BFD_ASSERT (h->tls_type == GOT_NORMAL);
-
       h->root.dynindx = --hsd->min_got_dynindx;
       hsd->low = (struct elf_link_hash_entry *) h;
       break;
 
     case GGA_RELOC_ONLY:
-      BFD_ASSERT (h->tls_type == GOT_NORMAL);
-
       if (hsd->max_unref_got_dynindx == hsd->min_got_dynindx)
        hsd->low = (struct elf_link_hash_entry *) h;
       h->root.dynindx = hsd->max_unref_got_dynindx++;
@@ -3982,8 +4006,6 @@ mips_elf_recreate_got (void **entryp, void *data)
     }
   if (*slot == NULL)
     *slot = entry;
-  else
-    free (entry);
   return 1;
 }
 
@@ -4007,8 +4029,6 @@ mips_elf_resolve_final_got_entries (struct mips_got_info *g)
       if (new_got == NULL)
        return FALSE;
 
-      /* Each entry in g->got_entries has either been copied to new_got
-        or freed.  Now delete the hash table itself.  */
       htab_delete (g->got_entries);
       g->got_entries = new_got;
     }
@@ -4259,10 +4279,10 @@ mips_elf_merge_got_with (struct mips_elf_bfd2got_hash *bfd2got,
   estimate += from->local_gotno + to->local_gotno;
   estimate += from->tls_gotno + to->tls_gotno;
 
-  /* If we're merging with the primary got, we will always have
-     the full set of global entries.  Otherwise estimate those
+  /* If we're merging with the primary got, any TLS relocations will
+     come after the full set of global entries.  Otherwise estimate those
      conservatively as well.  */
-  if (to == arg->primary)
+  if (to == arg->primary && from->tls_gotno + to->tls_gotno)
     estimate += arg->global_count;
   else
     estimate += from->global_gotno + to->global_gotno;
@@ -4676,6 +4696,7 @@ mips_elf_multi_got (bfd *abfd, struct bfd_link_info *info,
         all non-TLS entries.  */
       g->tls_assigned_gotno = g->local_gotno + g->global_gotno;
       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
+      BFD_ASSERT (g->tls_assigned_gotno == assign);
 
       /* Move onto the next GOT.  It will be a secondary GOT if nonull.  */
       g = gn;
@@ -5305,7 +5326,7 @@ mips_elf_calculate_relocation (bfd *abfd, bfd *input_bfd,
          if (h->call_stub != NULL && h->call_fp_stub != NULL)
            {
              asection *o;
-             
+
              sec = NULL;
              for (o = input_bfd->sections; o != NULL; o = o->next)
                {
@@ -5894,7 +5915,7 @@ mips_elf_obtain_contents (reloc_howto_type *howto,
 /* It has been determined that the result of the RELOCATION is the
    VALUE.  Use HOWTO to place VALUE into the output file at the
    appropriate position.  The SECTION is the section to which the
-   relocation applies.  
+   relocation applies.
    CROSS_MODE_JUMP_P is true if the relocation field
    is a MIPS16 or microMIPS jump to standard MIPS code, or vice versa.
 
@@ -6269,6 +6290,9 @@ _bfd_elf_mips_mach (flagword flags)
     case E_MIPS_MACH_5500:
       return bfd_mach_mips5500;
 
+    case E_MIPS_MACH_5900:
+      return bfd_mach_mips5900;
+
     case E_MIPS_MACH_9000:
       return bfd_mach_mips9000;
 
@@ -7268,7 +7292,7 @@ _bfd_mips_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
     return FALSE;
   htab->sstubs = s;
 
-  if ((IRIX_COMPAT (abfd) == ict_irix5 || IRIX_COMPAT (abfd) == ict_none)
+  if (!mips_elf_hash_table (info)->use_rld_obj_head
       && !info->shared
       && bfd_get_linker_section (abfd, ".rld_map") == NULL)
     {
@@ -7812,14 +7836,14 @@ _bfd_mips_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
        {
          h = ((struct mips_elf_link_hash_entry *)
               sym_hashes[r_symndx - extsymoff]);
-         
+
          /* H is the symbol this stub is for.  */
-         
+
          if (CALL_FP_STUB_P (name))
            loc = &h->call_fp_stub;
          else
            loc = &h->call_stub;
-         
+
          /* If we already have an appropriate stub for this function, we
             don't need another one, so we can discard this one.  Since
             this function is called before the linker maps input sections
@@ -8543,7 +8567,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   if (! info->relocatable
       && hmips->possibly_dynamic_relocs != 0
       && (h->root.type == bfd_link_hash_defweak
-         || !h->def_regular
+         || (!h->def_regular && !ELF_COMMON_DEF_P (h))
          || info->shared))
     {
       bfd_boolean do_copy = TRUE;
@@ -8941,6 +8965,8 @@ mips_elf_lay_out_got (bfd *output_bfd, struct bfd_link_info *info)
       /* Set up TLS entries.  */
       g->tls_assigned_gotno = g->global_gotno + g->local_gotno;
       htab_traverse (g->got_entries, mips_elf_initialize_tls_index, g);
+      BFD_ASSERT (g->tls_assigned_gotno
+                 == g->global_gotno + g->local_gotno + g->tls_gotno);
 
       /* Allocate room for the TLS relocations.  */
       arg.info = info;
@@ -10129,7 +10155,7 @@ _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd,
 
   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
   name = h->root.root.string;
-  if (strcmp (name, "_DYNAMIC") == 0
+  if (h == elf_hash_table (info)->hdynamic
       || h == elf_hash_table (info)->hgot)
     sym->st_shndx = SHN_ABS;
   else if (strcmp (name, "_DYNAMIC_LINK") == 0
@@ -11001,6 +11027,10 @@ mips_set_isa_flags (bfd *abfd)
       val = E_MIPS_ARCH_4 | E_MIPS_MACH_5500;
       break;
 
+    case bfd_mach_mips5900:
+      val = E_MIPS_ARCH_3 | E_MIPS_MACH_5900;
+      break;
+
     case bfd_mach_mips9000:
       val = E_MIPS_ARCH_4 | E_MIPS_MACH_9000;
       break;
@@ -12764,7 +12794,7 @@ _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
          /* Fix the relocation's type.  */
          irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC10_S1);
 
-         /* Replace the the 32-bit opcode with a 16-bit opcode.  */
+         /* Replace the 32-bit opcode with a 16-bit opcode.  */
          bfd_put_16 (abfd,
                      (b_insn_16.match
                       | (opcode & 0x3ff)),             /* Addend value.  */
@@ -12791,7 +12821,7 @@ _bfd_mips_elf_relax_section (bfd *abfd, asection *sec,
          /* Fix the relocation's type.  */
          irel->r_info = ELF32_R_INFO (r_symndx, R_MICROMIPS_PC7_S1);
 
-         /* Replace the the 32-bit opcode with a 16-bit opcode.  */
+         /* Replace the 32-bit opcode with a 16-bit opcode.  */
          bfd_put_16 (abfd,
                      (bz_insns_16[fndopc].match
                       | BZ16_REG_FIELD (reg)
@@ -12917,7 +12947,7 @@ _bfd_mips_elf_link_hash_table_create (bfd *abfd)
   struct mips_elf_link_hash_table *ret;
   bfd_size_type amt = sizeof (struct mips_elf_link_hash_table);
 
-  ret = bfd_malloc (amt);
+  ret = bfd_zmalloc (amt);
   if (ret == NULL)
     return NULL;
 
@@ -12930,36 +12960,6 @@ _bfd_mips_elf_link_hash_table_create (bfd *abfd)
       return NULL;
     }
 
-#if 0
-  /* We no longer use this.  */
-  for (i = 0; i < SIZEOF_MIPS_DYNSYM_SECNAMES; i++)
-    ret->dynsym_sec_strindex[i] = (bfd_size_type) -1;
-#endif
-  ret->procedure_count = 0;
-  ret->compact_rel_size = 0;
-  ret->use_rld_obj_head = FALSE;
-  ret->rld_symbol = NULL;
-  ret->mips16_stubs_seen = FALSE;
-  ret->use_plts_and_copy_relocs = FALSE;
-  ret->is_vxworks = FALSE;
-  ret->small_data_overflow_reported = FALSE;
-  ret->srelbss = NULL;
-  ret->sdynbss = NULL;
-  ret->srelplt = NULL;
-  ret->srelplt2 = NULL;
-  ret->sgotplt = NULL;
-  ret->splt = NULL;
-  ret->sstubs = NULL;
-  ret->sgot = NULL;
-  ret->got_info = NULL;
-  ret->plt_header_size = 0;
-  ret->plt_entry_size = 0;
-  ret->lazy_stub_count = 0;
-  ret->function_stub_size = 0;
-  ret->strampoline = NULL;
-  ret->la25_stubs = NULL;
-  ret->add_stub_section = NULL;
-
   return &ret->root.root;
 }
 
@@ -13683,6 +13683,7 @@ static const struct mips_mach_extension mips_mach_extensions[] = {
   { bfd_mach_mips4300, bfd_mach_mips4000 },
   { bfd_mach_mips4100, bfd_mach_mips4000 },
   { bfd_mach_mips4010, bfd_mach_mips4000 },
+  { bfd_mach_mips5900, bfd_mach_mips4000 },
 
   /* MIPS32 extensions.  */
   { bfd_mach_mipsisa32r2, bfd_mach_mipsisa32 },
@@ -13749,6 +13750,12 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
 {
   obj_attribute *in_attr;
   obj_attribute *out_attr;
+  bfd *abi_fp_bfd;
+
+  abi_fp_bfd = mips_elf_tdata (obfd)->abi_fp_bfd;
+  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
+  if (!abi_fp_bfd && in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
+    mips_elf_tdata (obfd)->abi_fp_bfd = ibfd;
 
   if (!elf_known_obj_attributes_proc (obfd)[0].i)
     {
@@ -13764,24 +13771,13 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
 
   /* Check for conflicting Tag_GNU_MIPS_ABI_FP attributes and merge
      non-conflicting ones.  */
-  in_attr = elf_known_obj_attributes (ibfd)[OBJ_ATTR_GNU];
   out_attr = elf_known_obj_attributes (obfd)[OBJ_ATTR_GNU];
   if (in_attr[Tag_GNU_MIPS_ABI_FP].i != out_attr[Tag_GNU_MIPS_ABI_FP].i)
     {
       out_attr[Tag_GNU_MIPS_ABI_FP].type = 1;
       if (out_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
        out_attr[Tag_GNU_MIPS_ABI_FP].i = in_attr[Tag_GNU_MIPS_ABI_FP].i;
-      else if (in_attr[Tag_GNU_MIPS_ABI_FP].i == 0)
-       ;
-      else if (in_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
-       _bfd_error_handler
-         (_("Warning: %B uses unknown floating point ABI %d"), ibfd,
-          in_attr[Tag_GNU_MIPS_ABI_FP].i);
-      else if (out_attr[Tag_GNU_MIPS_ABI_FP].i > 4)
-       _bfd_error_handler
-         (_("Warning: %B uses unknown floating point ABI %d"), obfd,
-          out_attr[Tag_GNU_MIPS_ABI_FP].i);
-      else
+      else if (in_attr[Tag_GNU_MIPS_ABI_FP].i != 0)
        switch (out_attr[Tag_GNU_MIPS_ABI_FP].i)
          {
          case 1:
@@ -13789,24 +13785,30 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
              {
              case 2:
                _bfd_error_handler
-                 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-mdouble-float", "-msingle-float");
                break;
 
              case 3:
                _bfd_error_handler
-                 (_("Warning: %B uses hard float, %B uses soft float"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
                break;
 
              case 4:
                _bfd_error_handler
-                 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-mdouble-float", "-mips32r2 -mfp64");
                break;
 
              default:
-               abort ();
+               _bfd_error_handler
+                 (_("Warning: %B uses %s (set by %B), "
+                    "%B uses unknown floating point ABI %d"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-mdouble-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
+               break;
              }
            break;
 
@@ -13815,24 +13817,30 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
              {
              case 1:
                _bfd_error_handler
-                 (_("Warning: %B uses -msingle-float, %B uses -mdouble-float"),
-                  ibfd, obfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-msingle-float", "-mdouble-float");
                break;
 
              case 3:
                _bfd_error_handler
-                 (_("Warning: %B uses hard float, %B uses soft float"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
                break;
 
              case 4:
                _bfd_error_handler
-                 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-msingle-float", "-mips32r2 -mfp64");
                break;
 
              default:
-               abort ();
+               _bfd_error_handler
+                 (_("Warning: %B uses %s (set by %B), "
+                    "%B uses unknown floating point ABI %d"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-msingle-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
+               break;
              }
            break;
 
@@ -13843,12 +13851,17 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
              case 2:
              case 4:
                _bfd_error_handler
-                 (_("Warning: %B uses hard float, %B uses soft float"),
-                  ibfd, obfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-msoft-float", "-mhard-float");
                break;
 
              default:
-               abort ();
+               _bfd_error_handler
+                 (_("Warning: %B uses %s (set by %B), "
+                    "%B uses unknown floating point ABI %d"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-msoft-float", in_attr[Tag_GNU_MIPS_ABI_FP].i);
+               break;
              }
            break;
 
@@ -13857,29 +13870,79 @@ mips_elf_merge_obj_attributes (bfd *ibfd, bfd *obfd)
              {
              case 1:
                _bfd_error_handler
-                 (_("Warning: %B uses -msingle-float, %B uses -mips32r2 -mfp64"),
-                  ibfd, obfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-mips32r2 -mfp64", "-mdouble-float");
                break;
 
              case 2:
                _bfd_error_handler
-                 (_("Warning: %B uses -mdouble-float, %B uses -mips32r2 -mfp64"),
-                  ibfd, obfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-mips32r2 -mfp64", "-msingle-float");
                break;
 
              case 3:
                _bfd_error_handler
-                 (_("Warning: %B uses hard float, %B uses soft float"),
-                  obfd, ibfd);
+                 (_("Warning: %B uses %s (set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd, "-mhard-float", "-msoft-float");
                break;
 
              default:
-               abort ();
+               _bfd_error_handler
+                 (_("Warning: %B uses %s (set by %B), "
+                    "%B uses unknown floating point ABI %d"),
+                  obfd, abi_fp_bfd, ibfd,
+                  "-mips32r2 -mfp64", in_attr[Tag_GNU_MIPS_ABI_FP].i);
+               break;
              }
            break;
 
          default:
-           abort ();
+           switch (in_attr[Tag_GNU_MIPS_ABI_FP].i)
+             {
+             case 1:
+               _bfd_error_handler
+                 (_("Warning: %B uses unknown floating point ABI %d "
+                    "(set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mdouble-float");
+               break;
+
+             case 2:
+               _bfd_error_handler
+                 (_("Warning: %B uses unknown floating point ABI %d "
+                    "(set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msingle-float");
+               break;
+
+             case 3:
+               _bfd_error_handler
+                 (_("Warning: %B uses unknown floating point ABI %d "
+                    "(set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  out_attr[Tag_GNU_MIPS_ABI_FP].i, "-msoft-float");
+               break;
+
+             case 4:
+               _bfd_error_handler
+                 (_("Warning: %B uses unknown floating point ABI %d "
+                    "(set by %B), %B uses %s"),
+                  obfd, abi_fp_bfd, ibfd,
+                  out_attr[Tag_GNU_MIPS_ABI_FP].i, "-mips32r2 -mfp64");
+               break;
+
+             default:
+               _bfd_error_handler
+                 (_("Warning: %B uses unknown floating point ABI %d "
+                    "(set by %B), %B uses unknown floating point ABI %d"),
+                  obfd, abi_fp_bfd, ibfd,
+                  out_attr[Tag_GNU_MIPS_ABI_FP].i,
+                  in_attr[Tag_GNU_MIPS_ABI_FP].i);
+               break;
+             }
+           break;
          }
     }
 
@@ -13937,7 +14000,7 @@ _bfd_mips_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
 
       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
          && (bfd_get_arch_info (obfd)->the_default
-             || mips_mach_extends_p (bfd_get_mach (obfd), 
+             || mips_mach_extends_p (bfd_get_mach (obfd),
                                      bfd_get_mach (ibfd))))
        {
          if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),