Fix date in ChangeLog entry
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
index 27f0a54fdbb7fe7a535a459b04011344b40269fb..432ab588546cef4d9508797c8b08cc2bfc87805b 100644 (file)
@@ -970,16 +970,16 @@ elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
 /* Destroy an X86-64 ELF linker hash table.  */
 
 static void
-elf_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
+elf_x86_64_link_hash_table_free (bfd *obfd)
 {
   struct elf_x86_64_link_hash_table *htab
-    = (struct elf_x86_64_link_hash_table *) hash;
+    = (struct elf_x86_64_link_hash_table *) obfd->link.hash;
 
   if (htab->loc_hash_table)
     htab_delete (htab->loc_hash_table);
   if (htab->loc_hash_memory)
     objalloc_free ((struct objalloc *) htab->loc_hash_memory);
-  _bfd_elf_link_hash_table_free (hash);
+  _bfd_elf_link_hash_table_free (obfd);
 }
 
 /* Create an X86-64 ELF linker hash table.  */
@@ -1027,10 +1027,10 @@ elf_x86_64_link_hash_table_create (bfd *abfd)
   ret->loc_hash_memory = objalloc_create ();
   if (!ret->loc_hash_table || !ret->loc_hash_memory)
     {
-      free (ret);
+      elf_x86_64_link_hash_table_free (abfd);
       return NULL;
     }
-  (void) elf_x86_64_link_hash_table_free;
+  ret->elf.root.hash_table_free = elf_x86_64_link_hash_table_free;
 
   return &ret->elf.root;
 }
@@ -1629,11 +1629,16 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
            case R_X86_64_PC32_BND:
            case R_X86_64_PLT32_BND:
+           case R_X86_64_PC32:
+           case R_X86_64_PLT32:
+           case R_X86_64_32:
+           case R_X86_64_64:
              /* MPX PLT is supported only if elf_x86_64_arch_bed
                 is used in 64-bit mode.  */
              if (ABI_64_P (abfd)
-                 && (get_elf_x86_64_backend_data (abfd)
-                     == &elf_x86_64_arch_bed))
+                     && info->bndplt
+                     && (get_elf_x86_64_backend_data (abfd)
+                         == &elf_x86_64_arch_bed))
                {
                  elf_x86_64_hash_entry (h)->has_bnd_reloc = TRUE;
 
@@ -1675,11 +1680,7 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
                }
 
            case R_X86_64_32S:
-           case R_X86_64_32:
-           case R_X86_64_64:
-           case R_X86_64_PC32:
            case R_X86_64_PC64:
-           case R_X86_64_PLT32:
            case R_X86_64_GOTPCREL:
            case R_X86_64_GOTPCREL64:
              if (htab->elf.dynobj == NULL)
@@ -1752,14 +1753,6 @@ elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
 
            if (h != NULL)
              {
-               if (r_type == R_X86_64_GOTPLT64)
-                 {
-                   /* This relocation indicates that we also need
-                      a PLT entry, as this is a function.  We don't need
-                      a PLT entry for local symbols.  */
-                   h->needs_plt = 1;
-                   h->plt.refcount += 1;
-                 }
                h->got.refcount += 1;
                old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
              }
@@ -2181,8 +2174,6 @@ elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
        case R_X86_64_GOTPLT64:
          if (h != NULL)
            {
-             if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
-               h->plt.refcount -= 1;
              if (h->got.refcount > 0)
                h->got.refcount -= 1;
              if (h->type == STT_GNU_IFUNC)
@@ -2777,7 +2768,7 @@ elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
   /* Nothing to do if there are no codes, no relocations or no output.  */
   if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
       || sec->reloc_count == 0
-      || discarded_section (sec))
+      || bfd_is_abs_section (sec->output_section))
     return TRUE;
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
@@ -2824,6 +2815,7 @@ elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
 
          /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation.  */
          if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
+             && irel->r_offset >= 2
              && bfd_get_8 (input_bfd,
                            contents + irel->r_offset - 2) == 0x8b)
            {
@@ -2854,6 +2846,7 @@ elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
          && h->type != STT_GNU_IFUNC
          && h != htab->elf.hdynamic
          && SYMBOL_REFERENCES_LOCAL (link_info, h)
+         && irel->r_offset >= 2
          && bfd_get_8 (input_bfd,
                        contents + irel->r_offset - 2) == 0x8b)
        {
@@ -3718,12 +3711,7 @@ elf_x86_64_relocate_section (bfd *output_bfd,
        case R_X86_64_GOTPCREL64:
          /* Use global offset table entry as symbol value.  */
        case R_X86_64_GOTPLT64:
-         /* This is the same as GOT64 for relocation purposes, but
-            indicates the existence of a PLT entry.  The difficulty is,
-            that we must calculate the GOT slot offset from the PLT
-            offset, if this symbol got a PLT entry (it was global).
-            Additionally if it's computed from the PLT entry, then that
-            GOT offset is relative to .got.plt, not to .got.  */
+         /* This is obsolete and treated the the same as GOT64.  */
          base_got = htab->elf.sgot;
 
          if (htab->elf.sgot == NULL)
@@ -4281,17 +4269,27 @@ direct:
              else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
                {
                  /* IE->LE transition:
-                    Originally it can be one of:
+                    For 64bit, originally it can be one of:
                     movq foo@gottpoff(%rip), %reg
                     addq foo@gottpoff(%rip), %reg
                     We change it into:
                     movq $foo, %reg
                     leaq foo(%reg), %reg
-                    addq $foo, %reg.  */
+                    addq $foo, %reg.
+                    For 32bit, originally it can be one of:
+                    movq foo@gottpoff(%rip), %reg
+                    addl foo@gottpoff(%rip), %reg
+                    We change it into:
+                    movq $foo, %reg
+                    leal foo(%reg), %reg
+                    addl $foo, %reg. */
 
                  unsigned int val, type, reg;
 
-                 val = bfd_get_8 (input_bfd, contents + roff - 3);
+                 if (roff >= 3)
+                   val = bfd_get_8 (input_bfd, contents + roff - 3);
+                 else
+                   val = 0;
                  type = bfd_get_8 (input_bfd, contents + roff - 2);
                  reg = bfd_get_8 (input_bfd, contents + roff - 1);
                  reg >>= 3;
@@ -4311,8 +4309,8 @@ direct:
                    }
                  else if (reg == 4)
                    {
-                     /* addq -> addq - addressing with %rsp/%r12 is
-                        special  */
+                     /* addq/addl -> addq/addl - addressing with %rsp/%r12
+                        is special  */
                      if (val == 0x4c)
                        bfd_put_8 (output_bfd, 0x49,
                                   contents + roff - 3);
@@ -4326,7 +4324,7 @@ direct:
                    }
                  else
                    {
-                     /* addq -> leaq */
+                     /* addq/addl -> leaq/leal */
                      if (val == 0x4c)
                        bfd_put_8 (output_bfd, 0x4d,
                                   contents + roff - 3);
@@ -4751,6 +4749,8 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
       bfd_byte *loc;
       asection *plt, *gotplt, *relplt, *resolved_plt;
       const struct elf_backend_data *bed;
+      bfd_boolean gotplt_after_plt;
+      int32_t plt_got_pcrel_offset;
 
       /* When building a static executable, use .iplt, .igot.plt and
         .rela.iplt sections for STT_GNU_IFUNC symbols.  */
@@ -4855,14 +4855,23 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
 
       /* Put offset the PC-relative instruction referring to the GOT entry,
         subtracting the size of that instruction.  */
-      bfd_put_32 (output_bfd,
-                 (gotplt->output_section->vma
-                  + gotplt->output_offset
-                  + got_offset
-                  - resolved_plt->output_section->vma
-                  - resolved_plt->output_offset
-                  - plt_offset
-                  - plt_got_insn_size),
+      plt_got_pcrel_offset = (gotplt->output_section->vma
+                             + gotplt->output_offset
+                             + got_offset
+                             - resolved_plt->output_section->vma
+                             - resolved_plt->output_offset
+                             - plt_offset
+                             - plt_got_insn_size);
+
+      /* Check PC-relative offset overflow in PLT entry.  */
+      gotplt_after_plt = (gotplt->output_section->vma
+                         > resolved_plt->output_section->vma);
+      if ((gotplt_after_plt && plt_got_pcrel_offset < 0)
+         || (!gotplt_after_plt && plt_got_pcrel_offset > 0))
+       info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
+                               output_bfd, h->root.root.string);
+
+      bfd_put_32 (output_bfd, plt_got_pcrel_offset,
                  resolved_plt->contents + plt_offset + plt_got_offset);
 
       /* Fill in the entry in the global offset table, initially this
@@ -5292,14 +5301,85 @@ elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
   return TRUE;
 }
 
-/* Return address for Ith PLT stub in section PLT, for relocation REL
-   or (bfd_vma) -1 if it should not be included.  */
+/* Return address in section PLT for the Ith GOTPLT relocation, for
+   relocation REL or (bfd_vma) -1 if it should not be included.  */
 
 static bfd_vma
 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
-                       const arelent *rel ATTRIBUTE_UNUSED)
+                       const arelent *rel)
+{
+  bfd *abfd;
+  const struct elf_x86_64_backend_data *bed;
+  bfd_vma plt_offset;
+
+  /* Only match R_X86_64_JUMP_SLOT and R_X86_64_IRELATIVE.  */
+  if (rel->howto->type != R_X86_64_JUMP_SLOT
+      && rel->howto->type != R_X86_64_IRELATIVE)
+    return (bfd_vma) -1;
+
+  abfd = plt->owner;
+  bed = get_elf_x86_64_backend_data (abfd);
+  plt_offset = bed->plt_entry_size;
+
+  if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_GNU)
+    return plt->vma + (i + 1) * plt_offset;
+
+  while (plt_offset < plt->size)
+    {
+      bfd_vma reloc_index;
+      bfd_byte reloc_index_raw[4];
+
+      if (!bfd_get_section_contents (abfd, (asection *) plt,
+                                    reloc_index_raw,
+                                    plt_offset + bed->plt_reloc_offset,
+                                    sizeof (reloc_index_raw)))
+       return (bfd_vma) -1;
+
+      reloc_index = H_GET_32 (abfd, reloc_index_raw);
+      if (reloc_index == i)
+       return plt->vma + plt_offset;
+      plt_offset += bed->plt_entry_size;
+    }
+
+  abort ();
+}
+
+/* Return offset in .plt.bnd section for the Ith GOTPLT relocation with
+   PLT section, or (bfd_vma) -1 if it should not be included.  */
+
+static bfd_vma
+elf_x86_64_plt_sym_val_offset_plt_bnd (bfd_vma i, const asection *plt)
 {
-  return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
+  const struct elf_x86_64_backend_data *bed = &elf_x86_64_bnd_arch_bed;
+  bfd *abfd = plt->owner;
+  bfd_vma plt_offset = bed->plt_entry_size;
+
+  if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_GNU)
+    return i * sizeof (elf_x86_64_legacy_plt2_entry);
+
+  while (plt_offset < plt->size)
+    {
+      bfd_vma reloc_index;
+      bfd_byte reloc_index_raw[4];
+
+      if (!bfd_get_section_contents (abfd, (asection *) plt,
+                                    reloc_index_raw,
+                                    plt_offset + bed->plt_reloc_offset,
+                                    sizeof (reloc_index_raw)))
+       return (bfd_vma) -1;
+
+      reloc_index = H_GET_32 (abfd, reloc_index_raw);
+      if (reloc_index == i)
+       {
+         /* This is the index in .plt section.  */
+         long plt_index = plt_offset / bed->plt_entry_size;
+         /* Return the offset in .plt.bnd section.  */
+         return (plt_index - 1) * sizeof (elf_x86_64_legacy_plt2_entry);
+       }
+      plt_offset += bed->plt_entry_size;
+    }
+
+  abort ();
 }
 
 /* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
@@ -5322,8 +5402,11 @@ elf_x86_64_get_synthetic_symtab (bfd *abfd,
   size_t size;
   Elf_Internal_Shdr *hdr;
   char *names;
-  asection *plt;
-  bfd_vma addr;
+  asection *plt, *plt_push;
+
+  plt_push = bfd_get_section_by_name (abfd, ".plt");
+  if (plt_push == NULL)
+    return 0;
 
   plt = bfd_get_section_by_name (abfd, ".plt.bnd");
   /* Use the generic ELF version if there is no .plt.bnd section.  */
@@ -5369,11 +5452,17 @@ elf_x86_64_get_synthetic_symtab (bfd *abfd,
   names = (char *) (s + count);
   p = relplt->relocation;
   n = 0;
-  addr = 0;
   for (i = 0; i < count; i++, p++)
     {
+      bfd_vma offset;
       size_t len;
 
+      if (p->howto->type != R_X86_64_JUMP_SLOT
+         && p->howto->type != R_X86_64_IRELATIVE)
+       continue;
+
+      offset = elf_x86_64_plt_sym_val_offset_plt_bnd (i, plt_push);
+
       *s = **p->sym_ptr_ptr;
       /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set.  Since
         we are defining a symbol, ensure one of them is set.  */
@@ -5381,7 +5470,7 @@ elf_x86_64_get_synthetic_symtab (bfd *abfd,
        s->flags |= BSF_GLOBAL;
       s->flags |= BSF_SYNTHETIC;
       s->section = plt;
-      s->value = addr;
+      s->value = offset;
       s->name = names;
       s->udata.p = NULL;
       len = strlen ((*p->sym_ptr_ptr)->name);
@@ -5403,7 +5492,6 @@ elf_x86_64_get_synthetic_symtab (bfd *abfd,
       memcpy (names, "@plt", sizeof ("@plt"));
       names += sizeof ("@plt");
       ++s, ++n;
-      addr += sizeof (elf_x86_64_legacy_plt2_entry);
     }
 
   return n;
@@ -5460,9 +5548,10 @@ elf_x86_64_add_symbol_hook (bfd *abfd,
       return TRUE;
     }
 
-  if ((abfd->flags & DYNAMIC) == 0
-      && (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
+       || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
+      && (abfd->flags & DYNAMIC) == 0
+      && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
     elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
 
   return TRUE;
This page took 0.02808 seconds and 4 git commands to generate.