gas: sparc: fix collision of registers and pseudo-ops.
[deliverable/binutils-gdb.git] / bfd / elf32-arc.c
index 0a4f4807e041ac8b99cfaf7ff0875c295ca9f374..82bfe02615300dbd8e4085b28435c31bd1a81fd3 100644 (file)
@@ -26,6 +26,7 @@
 #include "elf/arc.h"
 #include "libiberty.h"
 #include "opcode/arc-func.h"
+#include "opcode/arc.h"
 #include "arc-plt.h"
 
 #ifdef DEBUG
@@ -69,20 +70,6 @@ name_for_global_symbol (struct elf_link_hash_entry *h)
     bfd_elf32_swap_reloca_out (BFD, &_rel, _loc);                      \
   }
 
-struct arc_local_data
-{
-  bfd_vma        sdata_begin_symbol_vma;
-  asection *      sdata_output_section;
-  bfd_vma        got_symbol_vma;
-};
-
-struct arc_local_data global_arc_data =
-{
-  .sdata_begin_symbol_vma = 0,
-  .sdata_output_section = NULL,
-  .got_symbol_vma = 0,
-};
-
 struct dynamic_sections
 {
   bfd_boolean    initialized;
@@ -127,10 +114,10 @@ enum tls_type_e
 
 enum tls_got_entries
 {
-  NONE = 0,
-  MOD,
-  OFF,
-  MOD_AND_OFF
+  TLS_GOT_NONE = 0,
+  TLS_GOT_MOD,
+  TLS_GOT_OFF,
+  TLS_GOT_MOD_AND_OFF
 };
 
 struct got_entry
@@ -693,13 +680,16 @@ arc_elf_final_write_processing (bfd * abfd,
       emf = EM_ARC_COMPACT2;
       break;
     default:
-      abort ();
+      goto DO_NOTHING;
     }
 
   elf_elfheader (abfd)->e_machine = emf;
 
   /* Record whatever is the current syscall ABI version.  */
   elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
+
+DO_NOTHING:
+  return;
 }
 
 #define BFD_DEBUG_PIC(...)
@@ -722,6 +712,8 @@ struct arc_relocation_data
   bfd_signed_vma  got_symbol_vma;
 
   bfd_boolean    should_relocate;
+
+  const char *    symbol_name;
 };
 
 static void
@@ -738,7 +730,7 @@ debug_arc_reloc (struct arc_relocation_data reloc_data)
           (unsigned int) reloc_data.sym_value);
   if (reloc_data.sym_section != NULL)
     {
-      PR_DEBUG ("IN IF\n");
+      PR_DEBUG (" Symbol Section:\n");
       PR_DEBUG (
               "  section name = %s, output_offset 0x%08x",
               reloc_data.sym_section->name,
@@ -749,8 +741,8 @@ debug_arc_reloc (struct arc_relocation_data reloc_data)
                   ", output_section->vma = 0x%08x",
                   ((unsigned int) reloc_data.sym_section->output_section->vma));
        }
-
       PR_DEBUG ( "\n");
+      PR_DEBUG ("  file: %s\n", reloc_data.sym_section->owner->filename);
     }
   else
     {
@@ -769,6 +761,7 @@ debug_arc_reloc (struct arc_relocation_data reloc_data)
               (unsigned int) (reloc_data.input_section->output_section->vma +
               reloc_data.input_section->output_offset +
               reloc_data.reloc_offset));
+      PR_DEBUG ("  file: %s\n", reloc_data.input_section->owner->filename);
     }
   else
     {
@@ -788,6 +781,52 @@ middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
   return insn;
 }
 
+/* This function is called for relocations that are otherwise marked as NOT
+   requiring overflow checks.  In here we perform non-standard checks of
+   the relocation value.  */
+
+static inline bfd_reloc_status_type
+arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
+                             bfd_signed_vma relocation,
+                            struct bfd_link_info *info ATTRIBUTE_UNUSED)
+{
+  switch (reloc_data.howto->type)
+    {
+    case R_ARC_NPS_CMEM16:
+      if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
+        {
+          if (reloc_data.reloc_addend == 0)
+            (*_bfd_error_handler)
+              (_("%B(%A+0x%lx): CMEM relocation to `%s' is invalid, "
+                 "16 MSB should be 0x%04x (value is 0x%lx)"),
+               reloc_data.input_section->owner,
+               reloc_data.input_section,
+               reloc_data.reloc_offset,
+               reloc_data.symbol_name,
+               NPS_CMEM_HIGH_VALUE,
+               (relocation));
+          else
+            (*_bfd_error_handler)
+              (_("%B(%A+0x%lx): CMEM relocation to `%s+0x%lx' is invalid, "
+                 "16 MSB should be 0x%04x (value is 0x%lx)"),
+               reloc_data.input_section->owner,
+               reloc_data.input_section,
+               reloc_data.reloc_offset,
+               reloc_data.symbol_name,
+               reloc_data.reloc_addend,
+               NPS_CMEM_HIGH_VALUE,
+               (relocation));
+          return bfd_reloc_overflow;
+        }
+      break;
+
+    default:
+      break;
+    }
+
+  return bfd_reloc_ok;
+}
+
 #define ME(reloc) (reloc)
 
 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
@@ -821,7 +860,9 @@ middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
            (reloc_data.input_section->output_section->vma \
             + reloc_data.input_section->output_offset \
             + (reloc_data.reloc_offset))))
-#define SECTSTART (bfd_signed_vma) (reloc_data.input_section->output_offset)
+#define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
+                                   + reloc_data.sym_section->output_offset)
+
 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
 #define TLS_REL (bfd_signed_vma) \
   ((elf_hash_table (info))->tls_sec->output_section->vma)
@@ -896,6 +937,7 @@ arc_do_relocation (bfd_byte * contents,
   bfd_vma orig_insn ATTRIBUTE_UNUSED;
   bfd * abfd = reloc_data.input_section->owner;
   struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
+  bfd_reloc_status_type flag;
 
   if (reloc_data.should_relocate == FALSE)
     return bfd_reloc_ok;
@@ -936,34 +978,34 @@ arc_do_relocation (bfd_byte * contents,
 
   /* Check for relocation overflow.  */
   if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
-    {
-      bfd_reloc_status_type flag;
-      flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
-                                reloc_data.howto->bitsize,
-                                reloc_data.howto->rightshift,
-                                bfd_arch_bits_per_address (abfd),
-                                relocation);
+    flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
+                               reloc_data.howto->bitsize,
+                               reloc_data.howto->rightshift,
+                               bfd_arch_bits_per_address (abfd),
+                               relocation);
+  else
+    flag = arc_special_overflow_checks (reloc_data, relocation, info);
 
 #undef  DEBUG_ARC_RELOC
 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
-      if (flag != bfd_reloc_ok)
-       {
-         PR_DEBUG ( "Relocation overflows !!!!\n");
+  if (flag != bfd_reloc_ok)
+    {
+      PR_DEBUG ( "Relocation overflows !!!!\n");
 
-         DEBUG_ARC_RELOC (reloc_data);
+      DEBUG_ARC_RELOC (reloc_data);
 
-         PR_DEBUG (
-                 "Relocation value = signed -> %d, unsigned -> %u"
-                 ", hex -> (0x%08x)\n",
-                 (int) relocation,
-                 (unsigned int) relocation,
-                 (unsigned int) relocation);
-         return flag;
-       }
+      PR_DEBUG (
+                "Relocation value = signed -> %d, unsigned -> %u"
+                ", hex -> (0x%08x)\n",
+                (int) relocation,
+                (unsigned int) relocation,
+                (unsigned int) relocation);
+      return flag;
     }
 #undef  DEBUG_ARC_RELOC
 #define DEBUG_ARC_RELOC(A)
 
+  /* Write updated instruction back to memory.  */
   switch (reloc_data.howto->size)
     {
       case 2:
@@ -1055,15 +1097,16 @@ elf_arc_relocate_section (bfd *            output_bfd,
   struct elf_link_hash_entry ** sym_hashes;
   struct got_entry **     local_got_ents;
   Elf_Internal_Rela *     rel;
+  Elf_Internal_Rela *     wrel;
   Elf_Internal_Rela *     relend;
   struct elf_link_hash_table *htab = elf_hash_table (info);
 
   symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
   sym_hashes = elf_sym_hashes (input_bfd);
 
-  rel = relocs;
+  rel = wrel = relocs;
   relend = relocs + input_section->reloc_count;
-  for (; rel < relend; rel++)
+  for (; rel < relend; wrel++, rel++)
     {
       enum elf_arc_reloc_type       r_type;
       reloc_howto_type *           howto;
@@ -1095,7 +1138,7 @@ elf_arc_relocate_section (bfd *              output_bfd,
          bfd_set_error (bfd_error_bad_value);
          return FALSE;
        }
-      howto = &elf_arc_howto_table[r_type];
+      howto = arc_elf_howto (r_type);
 
       r_symndx = ELF32_R_SYM (rel->r_info);
 
@@ -1130,8 +1173,6 @@ elf_arc_relocate_section (bfd *              output_bfd,
                  );
                }
            }
-
-         continue;
        }
 
       h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
@@ -1160,14 +1201,71 @@ elf_arc_relocate_section (bfd *            output_bfd,
 
       if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
        {
-         local_got_ents = arc_get_local_got_ents (output_bfd);
-         struct got_entry *entry = local_got_ents[r_symndx];
-
          sym = local_syms + r_symndx;
          sec = local_sections[r_symndx];
+       }
+      else
+       {
+         /* TODO: This code is repeated from below.  We should
+            clean it and remove duplications.
+            Sec is used check for discarded sections.
+            Need to redesign code below.  */
+
+         /* Get the symbol's entry in the symtab.  */
+         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+         while (h->root.type == bfd_link_hash_indirect
+                || h->root.type == bfd_link_hash_warning)
+           h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+         /* If we have encountered a definition for this symbol.  */
+         if (h->root.type == bfd_link_hash_defined
+             || h->root.type == bfd_link_hash_defweak)
+           {
+             reloc_data.sym_value = h->root.u.def.value;
+             sec = h->root.u.def.section;
+           }
+       }
+
+      /* Clean relocs for symbols in discarded sections.  */
+      if (sec != NULL && discarded_section (sec))
+       {
+         _bfd_clear_contents (howto, input_bfd, input_section,
+                              contents + rel->r_offset);
+         rel->r_offset = rel->r_offset;
+         rel->r_info = 0;
+         rel->r_addend = 0;
+
+         /* For ld -r, remove relocations in debug sections against
+            sections defined in discarded sections.  Not done for
+            eh_frame editing code expects to be present.  */
+          if (bfd_link_relocatable (info)
+              && (input_section->flags & SEC_DEBUGGING))
+            wrel--;
+
+         continue;
+       }
+
+      if (bfd_link_relocatable (info))
+       {
+         if (wrel != rel)
+           *wrel = *rel;
+         continue;
+       }
+
+      if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
+       {
+         struct got_entry *entry;
+
+         local_got_ents = arc_get_local_got_ents (output_bfd);
+         entry = local_got_ents[r_symndx];
 
          reloc_data.sym_value = sym->st_value;
          reloc_data.sym_section = sec;
+         reloc_data.symbol_name =
+           bfd_elf_string_from_elf_section (input_bfd,
+                                            symtab_hdr->sh_link,
+                                            sym->st_name);
 
          /* Mergeable section handling.  */
          if ((sec->flags & SEC_MERGE)
@@ -1283,7 +1381,10 @@ elf_arc_relocate_section (bfd *             output_bfd,
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
-         BFD_ASSERT ((h->dynindx == -1) >= (h->forced_local != 0));
+         /* TODO: Need to validate what was the intention.  */
+         /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
+         reloc_data.symbol_name = h->root.root.string;
+
          /* If we have encountered a definition for this symbol.  */
          if (h->root.type == bfd_link_hash_defined
              || h->root.type == bfd_link_hash_defweak)
@@ -1349,25 +1450,19 @@ elf_arc_relocate_section (bfd *            output_bfd,
                {
                  /* Fail if it is linking for PIE and the symbol is
                     undefined.  */
-                 if (bfd_link_executable (info)
-                     && !(*info->callbacks->undefined_symbol)
-                       (info, h->root.root.string, input_bfd, input_section,
-                        rel->r_offset, TRUE))
-                   {
-                     return FALSE;
-                   }
+                 if (bfd_link_executable (info))
+                   (*info->callbacks->undefined_symbol)
+                     (info, h->root.root.string, input_bfd, input_section,
+                      rel->r_offset, TRUE);
                  reloc_data.sym_value = h->plt.offset;
                  reloc_data.sym_section = htab->splt;
 
                  reloc_data.should_relocate = TRUE;
                }
-             else if (!bfd_link_pic (info)
-                      && !(*info->callbacks->undefined_symbol)
-                      (info, h->root.root.string, input_bfd, input_section,
-                       rel->r_offset, TRUE))
-               {
-                 return FALSE;
-               }
+             else if (!bfd_link_pic (info))
+               (*info->callbacks->undefined_symbol)
+                 (info, h->root.root.string, input_bfd, input_section,
+                  rel->r_offset, TRUE);
            }
 
          if (h->got.glist != NULL)
@@ -1401,7 +1496,7 @@ elf_arc_relocate_section (bfd *              output_bfd,
                          bfd_put_32 (output_bfd,
                                      sym_value - sec_vma,
                                      htab->sgot->contents + entry->offset
-                                     + (entry->existing_entries == MOD_AND_OFF ? 4 : 0));
+                                     + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0));
 
                          ARC_DEBUG ("arc_info: FIXED -> %s value = 0x%x "
                                     "@ 0x%x, for symbol %s\n",
@@ -1409,7 +1504,7 @@ elf_arc_relocate_section (bfd *              output_bfd,
                                      "GOT_TLS_IE"),
                                     sym_value - sec_vma,
                                     htab->sgot->contents + entry->offset
-                                    + (entry->existing_entries == MOD_AND_OFF ? 4 : 0),
+                                    + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0),
                                     h->root.root.string);
 
                          entry->processed = TRUE;
@@ -1481,7 +1576,7 @@ elf_arc_relocate_section (bfd *              output_bfd,
          case R_ARC_32_ME:
          case R_ARC_PC32:
          case R_ARC_32_PCREL:
-           if (bfd_link_pic (info) && !bfd_link_pie (info)
+           if ((bfd_link_pic (info) || bfd_link_pie (info))
                && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
                    || (h != NULL
                        && h->dynindx != -1
@@ -1508,13 +1603,20 @@ elf_arc_relocate_section (bfd *            output_bfd,
                outrel.r_offset += (input_section->output_section->vma
                                    + input_section->output_offset);
 
+#define IS_ARC_PCREL_TYPE(TYPE) \
+  (   (TYPE == R_ARC_PC32) \
+   || (TYPE == R_ARC_32_PCREL))
                if (skip)
                  {
                    memset (&outrel, 0, sizeof outrel);
                    relocate = FALSE;
                  }
-               else if (r_type == R_ARC_PC32
-                        || r_type == R_ARC_32_PCREL)
+               else if (h != NULL
+                        && h->dynindx != -1
+                        && ((IS_ARC_PCREL_TYPE (r_type))
+                        || !(bfd_link_executable (info)
+                             || SYMBOLIC_BIND (info, h))
+                        || ! h->def_regular))
                  {
                    BFD_ASSERT (h != NULL);
                    if ((input_section->flags & SEC_ALLOC) != 0)
@@ -1537,31 +1639,9 @@ elf_arc_relocate_section (bfd *             output_bfd,
                       dynamic symbol table, and it's a regular symbol
                       not defined in a shared object, then treat the
                       symbol as local, resolve it now.  */
-                   if (h == NULL
-                       || ((info->symbolic || h->dynindx == -1)
-                           && h->def_regular)
-                       || h->forced_local)
-                     {
-                       relocate = TRUE;
-                       /* outrel.r_addend = 0; */
-                       outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
-                     }
-                   else
-                     {
-                       BFD_ASSERT (h->dynindx != -1);
-
-                       /* This type of dynamic relocation cannot be created
-                          for code sections.  */
-                       BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
-
-                       if ((input_section->flags & SEC_ALLOC) != 0)
-                         relocate = FALSE;
-                       else
-                         relocate = TRUE;
-
-                       BFD_ASSERT (h->dynindx != -1);
-                       outrel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_32);
-                     }
+                   relocate = TRUE;
+                   /* outrel.r_addend = 0; */
+                   outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
                  }
 
                BFD_ASSERT (sreloc->contents != 0);
@@ -1591,6 +1671,17 @@ elf_arc_relocate_section (bfd *             output_bfd,
 
       DEBUG_ARC_RELOC (reloc_data);
 
+      /* Make sure we have with a dynamic linker.  In case of GOT and PLT
+         the sym_section should point to .got or .plt respectively.  */
+      if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
+         && reloc_data.sym_section == NULL)
+       {
+         (*_bfd_error_handler)
+           (_("GOT and PLT relocations cannot be fixed with a non dynamic linker."));
+         bfd_set_error (bfd_error_bad_value);
+         return FALSE;
+       }
+
       if (arc_do_relocation (contents, reloc_data, info) != bfd_reloc_ok)
        return FALSE;
     }
@@ -1706,7 +1797,7 @@ elf_arc_check_relocs (bfd *                        abfd,
          bfd_set_error (bfd_error_bad_value);
          return FALSE;
        }
-      howto = &elf_arc_howto_table[r_type];
+      howto = arc_elf_howto (r_type);
 
       if (dynobj == NULL
          && (is_reloc_for_GOT (howto) == TRUE
@@ -1733,10 +1824,11 @@ elf_arc_check_relocs (bfd *                      abfd,
               and the dynamic linker can not resolve these.  However
               the error should not occur for e.g. debugging or
               non-readonly sections.  */
-           if (bfd_link_dll (info) && !bfd_link_pie (info)
+           if ((bfd_link_dll (info) && !bfd_link_pie (info))
                && (sec->flags & SEC_ALLOC) != 0
-               && (sec->flags & SEC_READONLY) == 0
-               && (sec->flags & SEC_CODE) != 0)
+               && (sec->flags & SEC_READONLY) != 0
+               && ((sec->flags & SEC_CODE) != 0
+                   || (sec->flags & SEC_DEBUGGING) != 0))
              {
                const char *name;
                if (h)
@@ -1763,7 +1855,7 @@ elf_arc_check_relocs (bfd *                        abfd,
            /* FALLTHROUGH */
          case R_ARC_PC32:
          case R_ARC_32_PCREL:
-           if (bfd_link_pic (info) && !bfd_link_pie (info)
+           if ((bfd_link_pic (info) || bfd_link_pie (info))
                && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
                    || (h != NULL
                        && h->dynindx != -1
@@ -1806,7 +1898,7 @@ elf_arc_check_relocs (bfd *                        abfd,
                                                  bfd_link_pic (info),
                                                  NULL);
                  new_got_entry_to_list (&(local_got_ents[r_symndx]),
-                                        GOT_NORMAL, offset, NONE);
+                                        GOT_NORMAL, offset, TLS_GOT_NONE);
                }
            }
          else
@@ -1818,7 +1910,7 @@ elf_arc_check_relocs (bfd *                        abfd,
                  bfd_vma offset =
                    ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
                  new_got_entry_to_list (&h->got.glist,
-                                        GOT_NORMAL, offset, NONE);
+                                        GOT_NORMAL, offset, TLS_GOT_NONE);
                }
            }
        }
@@ -1847,7 +1939,7 @@ elf_arc_check_relocs (bfd *                        abfd,
 
          if (type != GOT_UNKNOWN && !symbol_has_entry_of_type (*list, type))
            {
-             enum tls_got_entries entries = NONE;
+             enum tls_got_entries entries = TLS_GOT_NONE;
              bfd_vma offset =
                ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
 
@@ -1855,11 +1947,11 @@ elf_arc_check_relocs (bfd *                      abfd,
                {
                  bfd_vma ATTRIBUTE_UNUSED notneeded =
                    ADD_SYMBOL_REF_SEC_AND_RELOC (got, TRUE, h);
-                 entries = MOD_AND_OFF;
+                 entries = TLS_GOT_MOD_AND_OFF;
                }
 
-             if (entries == NONE)
-               entries = OFF;
+             if (entries == TLS_GOT_NONE)
+               entries = TLS_GOT_OFF;
 
              new_got_entry_to_list (list, type, offset, entries);
            }
@@ -1960,7 +2052,7 @@ plt_do_relocs_for_symbol (bfd *abfd,
 
       /* TODO: being ME is not a property of the relocation but of the
         section of which is applying the relocation. */
-      if (IS_MIDDLE_ENDIAN (reloc->symbol) || bfd_big_endian (abfd))
+      if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
        {
          relocation =
              ((relocation & 0xffff0000) >> 16) |
@@ -2004,9 +2096,19 @@ GOT_ENTRY_OFFSET = 0x%x, GOT_ENTRY_VMA = 0x%x, for symbol %s\n",
             + got_offset,
             h->root.root.string);
 
-  memcpy (htab->splt->contents + h->plt.offset,
-         plt_data->elem,
-         plt_data->elem_size);
+
+  {
+    bfd_vma i = 0;
+    uint16_t *ptr = (uint16_t *) plt_data->elem;
+    for (i = 0; i < plt_data->elem_size/2; i++)
+      {
+       uint16_t data = ptr[i];
+       bfd_put_16 (output_bfd,
+                   (bfd_vma) data,
+                   htab->splt->contents + h->plt.offset + (i*2));
+      }
+  }
+
   plt_do_relocs_for_symbol (output_bfd, htab,
                            plt_data->elem_relocs,
                            h->plt.offset,
@@ -2044,8 +2146,17 @@ relocate_plt_for_entry (bfd *abfd,
   struct plt_version_t *plt_data = arc_get_plt_version (info);
   struct elf_link_hash_table *htab = elf_hash_table (info);
 
-  memcpy (htab->splt->contents, plt_data->entry,
-         plt_data->entry_size);
+  {
+    bfd_vma i = 0;
+    uint16_t *ptr = (uint16_t *) plt_data->entry;
+    for (i = 0; i < plt_data->entry_size/2; i++)
+      {
+       uint16_t data = ptr[i];
+       bfd_put_16 (abfd,
+                   (bfd_vma) data,
+                   htab->splt->contents + (i*2));
+      }
+  }
   PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
 }
 
@@ -2060,7 +2171,6 @@ elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
                              struct elf_link_hash_entry *h)
 {
   asection *s;
-  unsigned int power_of_two;
   bfd *dynobj = (elf_hash_table (info))->dynobj;
   struct elf_link_hash_table *htab = elf_hash_table (info);
 
@@ -2116,11 +2226,6 @@ elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
       return TRUE;
     }
 
-  /* If there are no non-GOT references, we do not need a copy
-     relocation.  */
-  if (!h->non_got_ref)
-    return TRUE;
-
   /* This is a reference to a symbol defined by a dynamic object which
      is not a function.  */
 
@@ -2128,9 +2233,21 @@ elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
      only references to the symbol are via the global offset table.
      For such cases we need not do anything here; the relocations will
      be handled correctly by relocate_section.  */
-  if (bfd_link_pic (info))
+  if (!bfd_link_executable (info))
+    return TRUE;
+
+  /* If there are no non-GOT references, we do not need a copy
+     relocation.  */
+  if (!h->non_got_ref)
     return TRUE;
 
+  /* If -z nocopyreloc was given, we won't generate them either.  */
+  if (info->nocopyreloc)
+    {
+      h->non_got_ref = 0;
+      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
@@ -2141,8 +2258,8 @@ elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
      both the dynamic object and the regular object will refer to the
      same memory location for the variable.  */
 
-  s = bfd_get_section_by_name (dynobj, ".dynbss");
-  BFD_ASSERT (s != NULL);
+  if (htab == NULL)
+    return FALSE;
 
   /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
      copy the initial value out of the dynamic object and into the
@@ -2158,28 +2275,10 @@ elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
       h->needs_copy = 1;
     }
 
-  /* We need to figure out the alignment required for this symbol.  I
-     have no idea how ELF linkers handle this.  */
-  power_of_two = bfd_log2 (h->size);
-  if (power_of_two > 3)
-    power_of_two = 3;
-
-  /* Apply the required alignment.  */
-  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
-  if (power_of_two > bfd_get_section_alignment (dynobj, s))
-    {
-      if (! bfd_set_section_alignment (dynobj, s, power_of_two))
-       return FALSE;
-    }
-
-  /* Define the symbol as being at this point in the section.  */
-  h->root.u.def.section = s;
-  h->root.u.def.value = s->size;
-
-  /* Increment the section size to make room for the symbol.  */
-  s->size += h->size;
+  s = bfd_get_section_by_name (dynobj, ".dynbss");
+  BFD_ASSERT (s != NULL);
 
-  return TRUE;
+  return _bfd_elf_adjust_dynamic_copy (info, h, s);
 }
 
 /* Function :  elf_arc_finish_dynamic_symbol
@@ -2237,16 +2336,16 @@ elf_arc_finish_dynamic_symbol (bfd * output_bfd,
                }
              list->created_dyn_relocation = TRUE;
            }
-         else if (list->existing_entries != NONE)
+         else if (list->existing_entries != TLS_GOT_NONE)
            {
              struct elf_link_hash_table *htab = elf_hash_table (info);
              enum tls_got_entries e = list->existing_entries;
 
              BFD_ASSERT (list->type != GOT_TLS_GD
-                         || list->existing_entries == MOD_AND_OFF);
+                         || list->existing_entries == TLS_GOT_MOD_AND_OFF);
 
              bfd_vma dynindx = h->dynindx == -1 ? 0 : h->dynindx;
-             if (e == MOD_AND_OFF || e == MOD)
+             if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_MOD)
                {
                  ADD_RELA (output_bfd, got, got_offset, dynindx,
                            R_ARC_TLS_DTPMOD, 0);
@@ -2258,7 +2357,7 @@ GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
                             + htab->sgot->output_offset + got_offset,
                             dynindx, 0);
                }
-             if (e == MOD_AND_OFF || e == OFF)
+             if (e == TLS_GOT_MOD_AND_OFF || e == TLS_GOT_OFF)
                {
                  bfd_vma addend = 0;
                  if (list->type == GOT_TLS_IE)
@@ -2266,7 +2365,7 @@ GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
                                         htab->sgot->contents + got_offset);
 
                  ADD_RELA (output_bfd, got,
-                           got_offset + (e == MOD_AND_OFF ? 4 : 0),
+                           got_offset + (e == TLS_GOT_MOD_AND_OFF ? 4 : 0),
                            dynindx,
                            (list->type == GOT_TLS_IE ?
                             R_ARC_TLS_TPOFF : R_ARC_TLS_DTPOFF),
@@ -2321,19 +2420,13 @@ GOT_OFFSET = 0x%x, GOT_VMA = 0x%x, INDEX = %d, ADDEND = 0x%x\n",
   return TRUE;
 }
 
-#define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION, ASSERT)    \
+#define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION)            \
   case TAG:                                                    \
   if (SYMBOL != NULL)                                          \
-    {                                                          \
-      h = elf_link_hash_lookup (elf_hash_table (info),         \
-                               SYMBOL, FALSE, FALSE, TRUE);    \
-    }                                                          \
+    h = elf_link_hash_lookup (elf_hash_table (info),           \
+                             SYMBOL, FALSE, FALSE, TRUE);      \
   else if (SECTION != NULL)                                    \
-    {                                                          \
-      s = bfd_get_section_by_name (output_bfd, SECTION);       \
-      BFD_ASSERT (s != NULL || !ASSERT);                       \
-      do_it = TRUE;                                            \
-    }                                                          \
+    s = bfd_get_linker_section (dynobj, SECTION);              \
   break;
 
 /* Function :  elf_arc_finish_dynamic_sections
@@ -2371,15 +2464,15 @@ elf_arc_finish_dynamic_sections (bfd * output_bfd,
 
          switch (internal_dyn.d_tag)
            {
-             GET_SYMBOL_OR_SECTION (DT_INIT, "_init", NULL, TRUE)
-             GET_SYMBOL_OR_SECTION (DT_FINI, "_fini", NULL, TRUE)
-             GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt", TRUE)
-             GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt", TRUE)
-             GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt", TRUE)
-             GET_SYMBOL_OR_SECTION (DT_RELASZ, NULL, ".rela.plt", FALSE)
-             GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version", TRUE)
-             GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d", TRUE)
-             GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r", TRUE)
+             GET_SYMBOL_OR_SECTION (DT_INIT, "_init", NULL)
+             GET_SYMBOL_OR_SECTION (DT_FINI, "_fini", NULL)
+             GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
+             GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
+             GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
+             GET_SYMBOL_OR_SECTION (DT_RELASZ, NULL, ".rela.plt")
+             GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
+             GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
+             GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
              default:
                break;
            }
@@ -2416,7 +2509,8 @@ elf_arc_finish_dynamic_sections (bfd * output_bfd,
                  case DT_VERSYM:
                  case DT_VERDEF:
                  case DT_VERNEED:
-                   internal_dyn.d_un.d_ptr = s->vma;
+                   internal_dyn.d_un.d_ptr = (s->output_section->vma
+                                              + s->output_offset);
                    do_it = TRUE;
                    break;
 
@@ -2436,7 +2530,7 @@ elf_arc_finish_dynamic_sections (bfd * output_bfd,
                }
            }
 
-         if (do_it == TRUE)
+         if (do_it)
            bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
        }
 
@@ -2453,17 +2547,24 @@ elf_arc_finish_dynamic_sections (bfd * output_bfd,
   /* Fill in the first three entries in the global offset table.  */
   if (htab->sgot)
     {
-      if (htab->sgot->size > 0 || htab->sgotplt->size > 0)
+      struct elf_link_hash_entry *h;
+      h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
+                                FALSE, FALSE, TRUE);
+
+       if (h != NULL && h->root.type != bfd_link_hash_undefined
+           && h->root.u.def.section != NULL)
        {
+         asection *sec = h->root.u.def.section;
+
          if (ds.sdyn == NULL)
            bfd_put_32 (output_bfd, (bfd_vma) 0,
-                       htab->sgotplt->contents);
+                       sec->contents);
          else
            bfd_put_32 (output_bfd,
                        ds.sdyn->output_section->vma + ds.sdyn->output_offset,
-                       htab->sgotplt->contents);
-         bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 4);
-         bfd_put_32 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + 8);
+                       sec->contents);
+         bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
+         bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
        }
     }
 
@@ -2591,7 +2692,7 @@ elf_arc_size_dynamic_sections (bfd * output_bfd,
       if (relocs_exist == TRUE)
        if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
            || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
-           || !_bfd_elf_add_dynamic_entry (info, DT_RELENT,
+           || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
                                            sizeof (Elf32_External_Rela))
           )
          return FALSE;
@@ -2604,6 +2705,32 @@ elf_arc_size_dynamic_sections (bfd * output_bfd,
   return TRUE;
 }
 
+
+/* Classify dynamic relocs such that -z combreloc can reorder and combine
+   them.  */
+static enum elf_reloc_type_class
+elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                           const asection *rel_sec ATTRIBUTE_UNUSED,
+                           const Elf_Internal_Rela *rela)
+{
+  switch ((int) ELF32_R_TYPE (rela->r_info))
+    {
+    case R_ARC_RELATIVE:
+      return reloc_class_relative;
+    case R_ARC_JMP_SLOT:
+      return reloc_class_plt;
+    case R_ARC_COPY:
+      return reloc_class_copy;
+    /* TODO: Needed in future to support ifunc.  */
+    /*
+    case R_ARC_IRELATIVE:
+      return reloc_class_ifunc;
+    */
+    default:
+      return reloc_class_normal;
+    }
+}
+
 const struct elf_size_info arc_elf32_size_info =
 {
   sizeof (Elf32_External_Ehdr),
@@ -2673,11 +2800,10 @@ elf_arc_add_symbol_hook (bfd * abfd,
                         asection ** secp ATTRIBUTE_UNUSED,
                         bfd_vma * valp ATTRIBUTE_UNUSED)
 {
-  if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
-       || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
+  if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
       && (abfd->flags & DYNAMIC) == 0
       && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
-    elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
+    elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
 
   return TRUE;
 }
@@ -2707,6 +2833,8 @@ elf_arc_add_symbol_hook (bfd * abfd,
 #define elf_backend_check_relocs            elf_arc_check_relocs
 #define elf_backend_create_dynamic_sections  _bfd_elf_create_dynamic_sections
 
+#define elf_backend_reloc_type_class           elf32_arc_reloc_type_class
+
 #define elf_backend_adjust_dynamic_symbol    elf_arc_adjust_dynamic_symbol
 #define elf_backend_finish_dynamic_symbol    elf_arc_finish_dynamic_symbol
 
@@ -2725,4 +2853,6 @@ elf_arc_add_symbol_hook (bfd * abfd,
 #define elf_backend_may_use_rela_p     1
 #define elf_backend_default_use_rela_p 1
 
+#define elf_backend_default_execstack  0
+
 #include "elf32-target.h"
This page took 0.038204 seconds and 4 git commands to generate.