* elf32-vax.c (elf_vax_relocate_section)
[deliverable/binutils-gdb.git] / bfd / elf32-m68k.c
index f85aba00c7ef671ab6690254f70efc1657e72e03..bba4c791b27b7f5e65152f8004c6797fc6f30c70 100644 (file)
@@ -1,6 +1,7 @@
 /* Motorola 68k series support for 32-bit ELF
    Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+   2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
+   Free Software Foundation, Inc.
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -374,13 +375,17 @@ static reloc_howto_type howto_table[] = {
 };
 
 static void
-rtype_to_howto (abfd, cache_ptr, dst)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *cache_ptr;
-     Elf_Internal_Rela *dst;
+rtype_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
 {
-  BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
-  cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
+  unsigned int indx = ELF32_R_TYPE (dst->r_info);
+
+  if (indx >= (unsigned int) R_68K_max)
+    {
+      (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
+                            abfd, (int) indx);
+      indx = R_68K_NONE;
+    }
+  cache_ptr->howto = &howto_table[indx];
 }
 
 #define elf_info_to_howto rtype_to_howto
@@ -465,6 +470,7 @@ reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
 #define ELF_ARCH bfd_arch_m68k
+#define ELF_TARGET_ID M68K_ELF_DATA
 \f
 /* Functions for the m68k ELF linker.  */
 
@@ -933,7 +939,8 @@ struct elf_m68k_link_hash_table
 /* Get the m68k ELF linker hash table from a link_info structure.  */
 
 #define elf_m68k_hash_table(p) \
-  ((struct elf_m68k_link_hash_table *) (p)->hash)
+  (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
+  == M68K_ELF_DATA ? ((struct elf_m68k_link_hash_table *) ((p)->hash)) : NULL)
 
 /* Shortcut to multi-GOT data.  */
 #define elf_m68k_multi_got(INFO) (&elf_m68k_hash_table (INFO)->multi_got_)
@@ -941,10 +948,9 @@ struct elf_m68k_link_hash_table
 /* Create an entry in an m68k ELF linker hash table.  */
 
 static struct bfd_hash_entry *
-elf_m68k_link_hash_newfunc (entry, table, string)
-     struct bfd_hash_entry *entry;
-     struct bfd_hash_table *table;
-     const char *string;
+elf_m68k_link_hash_newfunc (struct bfd_hash_entry *entry,
+                           struct bfd_hash_table *table,
+                           const char *string)
 {
   struct bfd_hash_entry *ret = entry;
 
@@ -971,8 +977,7 @@ elf_m68k_link_hash_newfunc (entry, table, string)
 /* Create an m68k ELF linker hash table.  */
 
 static struct bfd_link_hash_table *
-elf_m68k_link_hash_table_create (abfd)
-     bfd *abfd;
+elf_m68k_link_hash_table_create (bfd *abfd)
 {
   struct elf_m68k_link_hash_table *ret;
   bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
@@ -983,7 +988,8 @@ elf_m68k_link_hash_table_create (abfd)
 
   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
                                      elf_m68k_link_hash_newfunc,
-                                     sizeof (struct elf_m68k_link_hash_entry)))
+                                     sizeof (struct elf_m68k_link_hash_entry),
+                                     M68K_ELF_DATA))
     {
       free (ret);
       return NULL;
@@ -1076,7 +1082,68 @@ elf32_m68k_object_p (bfd *abfd)
   return TRUE;
 }
 
+/* Somewhat reverse of elf32_m68k_object_p, this sets the e_flag
+   field based on the machine number.  */
+
+static void
+elf_m68k_final_write_processing (bfd *abfd,
+                                bfd_boolean linker ATTRIBUTE_UNUSED)
+{
+  int mach = bfd_get_mach (abfd);
+  unsigned long e_flags = elf_elfheader (abfd)->e_flags;
+
+  if (!e_flags)
+    {
+      unsigned int arch_mask;
+
+      arch_mask = bfd_m68k_mach_to_features (mach);
+
+      if (arch_mask & m68000)
+       e_flags = EF_M68K_M68000;
+      else if (arch_mask & cpu32)
+       e_flags = EF_M68K_CPU32;
+      else if (arch_mask & fido_a)
+       e_flags = EF_M68K_FIDO;
+      else
+       {
+         switch (arch_mask
+                 & (mcfisa_a | mcfisa_aa | mcfisa_b | mcfisa_c | mcfhwdiv | mcfusp))
+           {
+           case mcfisa_a:
+             e_flags |= EF_M68K_CF_ISA_A_NODIV;
+             break;
+           case mcfisa_a | mcfhwdiv:
+             e_flags |= EF_M68K_CF_ISA_A;
+             break;
+           case mcfisa_a | mcfisa_aa | mcfhwdiv | mcfusp:
+             e_flags |= EF_M68K_CF_ISA_A_PLUS;
+             break;
+           case mcfisa_a | mcfisa_b | mcfhwdiv:
+             e_flags |= EF_M68K_CF_ISA_B_NOUSP;
+             break;
+           case mcfisa_a | mcfisa_b | mcfhwdiv | mcfusp:
+             e_flags |= EF_M68K_CF_ISA_B;
+             break;
+           case mcfisa_a | mcfisa_c | mcfhwdiv | mcfusp:
+             e_flags |= EF_M68K_CF_ISA_C;
+             break;
+           case mcfisa_a | mcfisa_c | mcfusp:
+             e_flags |= EF_M68K_CF_ISA_C_NODIV;
+             break;
+           }
+         if (arch_mask & mcfmac)
+           e_flags |= EF_M68K_CF_MAC;
+         else if (arch_mask & mcfemac)
+           e_flags |= EF_M68K_CF_EMAC;
+         if (arch_mask & cfloat)
+           e_flags |= EF_M68K_CF_FLOAT | EF_M68K_CFV4E;
+       }
+      elf_elfheader (abfd)->e_flags = e_flags;
+    }
+}
+
 /* Keep m68k-specific flags in the ELF header.  */
+
 static bfd_boolean
 elf32_m68k_set_private_flags (abfd, flags)
      bfd *abfd;
@@ -1228,6 +1295,9 @@ elf32_m68k_print_private_bfd_data (bfd *abfd, void * ptr)
            case EF_M68K_CF_EMAC:
              mac = "emac";
              break;
+           case EF_M68K_CF_EMAC_B:
+             mac = "emac_b";
+             break;
            }
          if (mac)
            fprintf (file, " [%s]", mac);
@@ -2746,21 +2816,25 @@ elf_m68k_check_relocs (abfd, info, sec, relocs)
        case R_68K_8:
        case R_68K_16:
        case R_68K_32:
+         /* We don't need to handle relocs into sections not going into
+            the "real" output.  */
+         if ((sec->flags & SEC_ALLOC) == 0)
+             break;
+
          if (h != NULL)
            {
              /* Make sure a plt entry is created for this symbol if it
                 turns out to be a function defined by a dynamic object.  */
              h->plt.refcount++;
 
-             if (!info->shared)
+             if (info->executable)
                /* This symbol needs a non-GOT reference.  */
                h->non_got_ref = 1;
            }
 
          /* If we are creating a shared library, we need to copy the
             reloc into the shared library.  */
-         if (info->shared
-             && (sec->flags & SEC_ALLOC) != 0)
+         if (info->shared)
            {
              /* When creating a shared object, we must copy these
                 reloc types into the output file.  We create a reloc
@@ -2903,8 +2977,6 @@ elf_m68k_gc_sweep_hook (bfd *abfd,
   struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel, *relend;
   bfd *dynobj;
-  asection *sgot;
-  asection *srelgot;
   struct elf_m68k_got *got;
 
   if (info->relocatable)
@@ -2916,9 +2988,6 @@ elf_m68k_gc_sweep_hook (bfd *abfd,
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-
-  sgot = bfd_get_section_by_name (dynobj, ".got");
-  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   got = NULL;
 
   relend = relocs + sec->reloc_count;
@@ -3183,13 +3252,6 @@ elf_m68k_adjust_dynamic_symbol (info, h)
   if (!h->non_got_ref)
     return TRUE;
 
-  if (h->size == 0)
-    {
-      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
-                            h->root.root.string);
-      return TRUE;
-    }
-
   /* We must allocate the symbol in our .dynbss section, which will
      become part of the .bss section of the executable.  There will be
      an entry for this symbol in the .dynsym section.  The dynamic
@@ -3207,7 +3269,7 @@ elf_m68k_adjust_dynamic_symbol (info, h)
      copy the initial value out of the dynamic object and into the
      runtime process image.  We need to remember the offset into the
      .rela.bss section we are going to use.  */
-  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
+  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
     {
       asection *srel;
 
@@ -3402,9 +3464,6 @@ elf_m68k_discard_copies (h, inf)
   struct bfd_link_info *info = (struct bfd_link_info *) inf;
   struct elf_m68k_pcrel_relocs_copied *s;
 
-  if (h->root.type == bfd_link_hash_warning)
-    h = (struct elf_link_hash_entry *) h->root.u.i.link;
-
   if (!SYMBOL_CALLS_LOCAL (info, h))
     {
       if ((info->flags & DF_TEXTREL) == 0)
@@ -3420,6 +3479,18 @@ elf_m68k_discard_copies (h, inf)
              }
        }
 
+      /* Make sure undefined weak symbols are output as a dynamic symbol
+        in PIEs.  */
+      if (h->non_got_ref
+         && h->root.type == bfd_link_hash_undefweak
+         && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+         && h->dynindx == -1
+         && !h->forced_local)
+       {
+         if (! bfd_elf_link_record_dynamic_symbol (info, h))
+           return FALSE;
+       }
+
       return TRUE;
     }
 
@@ -3651,16 +3722,9 @@ elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
                                   unresolved_reloc, warned);
        }
 
-      if (sec != NULL && elf_discarded_section (sec))
-       {
-         /* For relocs against symbols from removed linkonce sections,
-            or sections discarded by a linker script, we just want the
-            section contents zeroed.  Avoid any special processing.  */
-         _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
-         rel->r_info = 0;
-         rel->r_addend = 0;
-         continue;
-       }
+      if (sec != NULL && discarded_section (sec))
+       RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
+                                        rel, 1, relend, howto, 0, contents);
 
       if (info->relocatable)
        continue;
@@ -3896,7 +3960,7 @@ elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_68K_TLS_LE32:
        case R_68K_TLS_LE16:
        case R_68K_TLS_LE8:
-         if (info->shared)
+         if (info->shared && !info->pie)
            {
              (*_bfd_error_handler)
                (_("%B(%A+0x%lx): R_68K_TLS_LE32 relocation not permitted "
@@ -3970,7 +4034,7 @@ elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_68K_PC16:
        case R_68K_PC32:
          if (info->shared
-             && r_symndx != 0
+             && r_symndx != STN_UNDEF
              && (input_section->flags & SEC_ALLOC) != 0
              && (h == NULL
                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
@@ -4093,7 +4157,9 @@ elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
         not process them.  */
       if (unresolved_reloc
          && !((input_section->flags & SEC_DEBUGGING) != 0
-              && h->def_dynamic))
+              && h->def_dynamic)
+         && _bfd_elf_section_offset (output_bfd, info, input_section,
+                                     rel->r_offset) != (bfd_vma) -1)
        {
          (*_bfd_error_handler)
            (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
@@ -4105,7 +4171,7 @@ elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
          return FALSE;
        }
 
-      if (r_symndx != 0
+      if (r_symndx != STN_UNDEF
          && r_type != R_68K_NONE
          && (h == NULL
              || h->root.type == bfd_link_hash_defined
@@ -4333,6 +4399,11 @@ elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
                  break;
 
                case R_68K_TLS_GD32:
+                 /* The value for this relocation is actually put in
+                    the second GOT slot.  */
+                 relocation = bfd_get_signed_32 (output_bfd,
+                                                 (sgot->contents
+                                                  + got_entry_offset + 4));
                  relocation += dtpoff_base (info);
                  break;
 
@@ -4682,34 +4753,44 @@ void
 bfd_elf_m68k_set_target_options (struct bfd_link_info *info, int got_handling)
 {
   struct elf_m68k_link_hash_table *htab;
-
-  htab = elf_m68k_hash_table (info);
+  bfd_boolean use_neg_got_offsets_p;
+  bfd_boolean allow_multigot_p;
+  bfd_boolean local_gp_p;
 
   switch (got_handling)
     {
     case 0:
       /* --got=single.  */
-      htab->local_gp_p = FALSE;
-      htab->use_neg_got_offsets_p = FALSE;
-      htab->allow_multigot_p = FALSE;
+      local_gp_p = FALSE;
+      use_neg_got_offsets_p = FALSE;
+      allow_multigot_p = FALSE;
       break;
 
     case 1:
       /* --got=negative.  */
-      htab->local_gp_p = TRUE;
-      htab->use_neg_got_offsets_p = TRUE;
-      htab->allow_multigot_p = FALSE;
+      local_gp_p = TRUE;
+      use_neg_got_offsets_p = TRUE;
+      allow_multigot_p = FALSE;
       break;
 
     case 2:
       /* --got=multigot.  */
-      htab->local_gp_p = TRUE;
-      htab->use_neg_got_offsets_p = TRUE;
-      htab->allow_multigot_p = TRUE;
+      local_gp_p = TRUE;
+      use_neg_got_offsets_p = TRUE;
+      allow_multigot_p = TRUE;
       break;
 
     default:
       BFD_ASSERT (FALSE);
+      return;
+    }
+
+  htab = elf_m68k_hash_table (info);
+  if (htab != NULL)
+    {
+      htab->local_gp_p = local_gp_p;
+      htab->use_neg_got_offsets_p = use_neg_got_offsets_p;
+      htab->allow_multigot_p = allow_multigot_p;
     }
 }
 
@@ -4760,6 +4841,7 @@ elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
                                        elf_m68k_adjust_dynamic_symbol
 #define elf_backend_size_dynamic_sections \
                                        elf_m68k_size_dynamic_sections
+#define elf_backend_final_write_processing     elf_m68k_final_write_processing
 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
 #define elf_backend_relocate_section   elf_m68k_relocate_section
 #define elf_backend_finish_dynamic_symbol \
This page took 0.029014 seconds and 4 git commands to generate.