]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/elf64-x86-64.c
Correct spelling of "relocatable".
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
index dc627fb6c42f605994a3be7f03dc244de2498043..76af7df9e6856a9479c35c76b7281af2f6afd713 100644 (file)
@@ -324,6 +324,12 @@ elf64_x86_64_grok_psinfo (abfd, note)
 
 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
 
+/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
+   copying dynamic variables from a shared lib into an app's dynbss
+   section, and instead use a dynamic relocation to point into the
+   shared lib.  */
+#define ELIMINATE_COPY_RELOCS 1
+
 /* The size in bytes of an entry in the global offset table.  */
 
 #define GOT_ENTRY_SIZE 8
@@ -616,7 +622,18 @@ elf64_x86_64_copy_indirect_symbol (bed, dir, ind)
       eind->tls_type = GOT_UNKNOWN;
     }
 
-  _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
+  if (ELIMINATE_COPY_RELOCS
+      && ind->root.type != bfd_link_hash_indirect
+      && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
+    /* If called to transfer flags for a weakdef during processing
+       of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
+       We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
+    dir->elf_link_hash_flags |=
+      (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
+                                  | ELF_LINK_HASH_REF_REGULAR
+                                  | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
+  else
+    _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
 }
 
 static bfd_boolean
@@ -688,7 +705,7 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs)
   const Elf_Internal_Rela *rel_end;
   asection *sreloc;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
   htab = elf64_x86_64_hash_table (info);
@@ -914,7 +931,8 @@ elf64_x86_64_check_relocs (abfd, info, sec, relocs)
                           || h->root.type == bfd_link_hash_defweak
                           || (h->elf_link_hash_flags
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
-             || (!info->shared
+             || (ELIMINATE_COPY_RELOCS
+                 && !info->shared
                  && (sec->flags & SEC_ALLOC) != 0
                  && h != NULL
                  && (h->root.type == bfd_link_hash_defweak
@@ -1091,9 +1109,6 @@ elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
   struct elf_link_hash_entry **sym_hashes;
   bfd_signed_vma *local_got_refcounts;
   const Elf_Internal_Rela *rel, *relend;
-  unsigned long r_symndx;
-  int r_type;
-  struct elf_link_hash_entry *h;
 
   elf_section_data (sec)->local_dynrel = NULL;
 
@@ -1103,85 +1118,79 @@ elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
 
   relend = relocs + sec->reloc_count;
   for (rel = relocs; rel < relend; rel++)
-    switch ((r_type = elf64_x86_64_tls_transition (info,
-                                                  ELF64_R_TYPE (rel->r_info),
-                                                  ELF64_R_SYM (rel->r_info)
-                                                  >= symtab_hdr->sh_info)))
-      {
-      case R_X86_64_TLSLD:
-       if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
-         elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
-       break;
-
-      case R_X86_64_TLSGD:
-      case R_X86_64_GOTTPOFF:
-      case R_X86_64_GOT32:
-      case R_X86_64_GOTPCREL:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           if (h->got.refcount > 0)
-             h->got.refcount -= 1;
-         }
-       else if (local_got_refcounts != NULL)
-         {
-           if (local_got_refcounts[r_symndx] > 0)
-             local_got_refcounts[r_symndx] -= 1;
-         }
-       break;
+    {
+      unsigned long r_symndx;
+      unsigned int r_type;
+      struct elf_link_hash_entry *h = NULL;
 
-      case R_X86_64_8:
-      case R_X86_64_16:
-      case R_X86_64_32:
-      case R_X86_64_64:
-      case R_X86_64_32S:
-      case R_X86_64_PC8:
-      case R_X86_64_PC16:
-      case R_X86_64_PC32:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           struct elf64_x86_64_link_hash_entry *eh;
-           struct elf64_x86_64_dyn_relocs **pp;
-           struct elf64_x86_64_dyn_relocs *p;
+      r_symndx = ELF64_R_SYM (rel->r_info);
+      if (r_symndx >= symtab_hdr->sh_info)
+       {
+         struct elf64_x86_64_link_hash_entry *eh;
+         struct elf64_x86_64_dyn_relocs **pp;
+         struct elf64_x86_64_dyn_relocs *p;
 
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+         eh = (struct elf64_x86_64_link_hash_entry *) h;
 
-           if (!info->shared && h->plt.refcount > 0)
-             h->plt.refcount -= 1;
+         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
+           if (p->sec == sec)
+             {
+               /* Everything must go for SEC.  */
+               *pp = p->next;
+               break;
+             }
+       }
 
-           eh = (struct elf64_x86_64_link_hash_entry *) h;
+      r_type = ELF64_R_TYPE (rel->r_info);
+      r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
+      switch (r_type)
+       {
+       case R_X86_64_TLSLD:
+         if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
+           elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
+         break;
 
-           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-             if (p->sec == sec)
-               {
-                 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
-                     || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
-                     || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
-                   p->pc_count -= 1;
-                 p->count -= 1;
-                 if (p->count == 0)
-                   *pp = p->next;
-                 break;
-               }
-         }
-       break;
+       case R_X86_64_TLSGD:
+       case R_X86_64_GOTTPOFF:
+       case R_X86_64_GOT32:
+       case R_X86_64_GOTPCREL:
+         if (h != NULL)
+           {
+             if (h->got.refcount > 0)
+               h->got.refcount -= 1;
+           }
+         else if (local_got_refcounts != NULL)
+           {
+             if (local_got_refcounts[r_symndx] > 0)
+               local_got_refcounts[r_symndx] -= 1;
+           }
+         break;
 
+       case R_X86_64_8:
+       case R_X86_64_16:
+       case R_X86_64_32:
+       case R_X86_64_64:
+       case R_X86_64_32S:
+       case R_X86_64_PC8:
+       case R_X86_64_PC16:
+       case R_X86_64_PC32:
+         if (info->shared)
+           break;
+         /* Fall thru */
 
-      case R_X86_64_PLT32:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           if (h->plt.refcount > 0)
-             h->plt.refcount -= 1;
-         }
-       break;
+       case R_X86_64_PLT32:
+         if (h != NULL)
+           {
+             if (h->plt.refcount > 0)
+               h->plt.refcount -= 1;
+           }
+         break;
 
-      default:
-       break;
-      }
+       default:
+         break;
+       }
+    }
 
   return TRUE;
 }
@@ -1198,8 +1207,6 @@ elf64_x86_64_adjust_dynamic_symbol (info, h)
      struct elf_link_hash_entry *h;
 {
   struct elf64_x86_64_link_hash_table *htab;
-  struct elf64_x86_64_link_hash_entry * eh;
-  struct elf64_x86_64_dyn_relocs *p;
   asection *s;
   unsigned int power_of_two;
 
@@ -1244,6 +1251,10 @@ elf64_x86_64_adjust_dynamic_symbol (info, h)
                  || h->weakdef->root.type == bfd_link_hash_defweak);
       h->root.u.def.section = h->weakdef->root.u.def.section;
       h->root.u.def.value = h->weakdef->root.u.def.value;
+      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
+       h->elf_link_hash_flags
+         = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
+            | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
       return TRUE;
     }
 
@@ -1269,20 +1280,26 @@ elf64_x86_64_adjust_dynamic_symbol (info, h)
       return TRUE;
     }
 
-  eh = (struct elf64_x86_64_link_hash_entry *) h;
-  for (p = eh->dyn_relocs; p != NULL; p = p->next)
+  if (ELIMINATE_COPY_RELOCS)
     {
-      s = p->sec->output_section;
-      if (s != NULL && (s->flags & SEC_READONLY) != 0)
-       break;
-    }
+      struct elf64_x86_64_link_hash_entry * eh;
+      struct elf64_x86_64_dyn_relocs *p;
 
-  /* If we didn't find any dynamic relocs in read-only sections, then
-     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
-  if (p == NULL)
-    {
-      h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
-      return TRUE;
+      eh = (struct elf64_x86_64_link_hash_entry *) h;
+      for (p = eh->dyn_relocs; p != NULL; p = p->next)
+       {
+         s = p->sec->output_section;
+         if (s != NULL && (s->flags & SEC_READONLY) != 0)
+           break;
+       }
+
+      /* If we didn't find any dynamic relocs in read-only sections, then
+        we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
+      if (p == NULL)
+       {
+         h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
+         return TRUE;
+       }
     }
 
   /* We must allocate the symbol in our .dynbss section, which will
@@ -1368,7 +1385,9 @@ allocate_dynrelocs (h, inf)
   htab = elf64_x86_64_hash_table (info);
 
   if (htab->elf.dynamic_sections_created
-      && h->plt.refcount > 0)
+      && h->plt.refcount > 0
+      && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+         || h->root.type != bfd_link_hash_undefweak))
     {
       /* Make sure this symbol is output as a dynamic symbol.
         Undefined weak syms won't yet be marked as dynamic.  */
@@ -1461,7 +1480,9 @@ allocate_dynrelocs (h, inf)
        htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
       else if (tls_type == GOT_TLS_GD)
        htab->srelgot->_raw_size += 2 * sizeof (Elf64_External_Rela);
-      else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
+      else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+               || h->root.type != bfd_link_hash_undefweak)
+              && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
        htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
     }
   else
@@ -1495,8 +1516,14 @@ allocate_dynrelocs (h, inf)
                pp = &p->next;
            }
        }
+
+      /* Also discard relocs on undefined weak syms with non-default
+        visibility.  */
+      if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+         && h->root.type == bfd_link_hash_undefweak)
+       eh->dyn_relocs = NULL;
     }
-  else
+  else if (ELIMINATE_COPY_RELOCS)
     {
       /* For the non-shared case, discard space for relocs against
         symbols which turn out to need copy relocs or are not
@@ -1591,7 +1618,7 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info)
   if (htab->elf.dynamic_sections_created)
     {
       /* Set the contents of the .interp section to the interpreter.  */
-      if (! info->shared)
+      if (info->executable)
        {
          s = bfd_get_section_by_name (dynobj, ".interp");
          if (s == NULL)
@@ -1752,7 +1779,7 @@ elf64_x86_64_size_dynamic_sections (output_bfd, info)
 #define add_dynamic_entry(TAG, VAL) \
   bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
 
-      if (! info->shared)
+      if (info->executable)
        {
          if (!add_dynamic_entry (DT_DEBUG, 0))
            return FALSE;
@@ -1845,7 +1872,7 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
   Elf_Internal_Rela *rel;
   Elf_Internal_Rela *relend;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
   htab = elf64_x86_64_hash_table (info);
@@ -1959,7 +1986,9 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
                      && (info->symbolic
                          || h->dynindx == -1
                          || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
-                     && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
+                     && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
+                 || (ELF_ST_VISIBILITY (h->other)
+                     && h->root.type == bfd_link_hash_undefweak))
                {
                  /* This is actually a static link, or it is a -Bsymbolic
                     link and the symbol is defined locally, or the symbol
@@ -2078,6 +2107,9 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
            break;
 
          if ((info->shared
+              && (h == NULL
+                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+                  || h->root.type != bfd_link_hash_undefweak)
               && ((r_type != R_X86_64_PC8
                    && r_type != R_X86_64_PC16
                    && r_type != R_X86_64_PC32)
@@ -2086,7 +2118,8 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
                       && (! info->symbolic
                           || (h->elf_link_hash_flags
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
-             || (!info->shared
+             || (ELIMINATE_COPY_RELOCS
+                 && !info->shared
                  && h != NULL
                  && h->dynindx != -1
                  && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
@@ -2328,7 +2361,7 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
 
          if ((off & 1) != 0)
            off &= ~1;
-          else
+         else
            {
              Elf_Internal_Rela outrel;
              bfd_byte *loc;
@@ -2360,7 +2393,7 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
                {
                  if (indx == 0)
                    {
-                     BFD_ASSERT (! unresolved_reloc);
+                     BFD_ASSERT (! unresolved_reloc);
                      bfd_put_64 (output_bfd,
                                  relocation - dtpoff_base (info),
                                  htab->sgot->contents + off + GOT_ENTRY_SIZE);
@@ -2406,12 +2439,12 @@ elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
                 addq foo@gottpoff(%rip), %rax */
              BFD_ASSERT (rel->r_offset >= 4);
              for (i = 0; i < 4; i++)
-               BFD_ASSERT (bfd_get_8 (input_bfd,
+               BFD_ASSERT (bfd_get_8 (input_bfd,
                                       contents + rel->r_offset - 4 + i)
                            == tlsgd[i]);
              BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
              for (i = 0; i < 4; i++)
-               BFD_ASSERT (bfd_get_8 (input_bfd,
+               BFD_ASSERT (bfd_get_8 (input_bfd,
                                       contents + rel->r_offset + 4 + i)
                            == tlsgd[i+4]);
              BFD_ASSERT (rel + 1 < relend);