x86: Add _bfd_x86_elf_size_dynamic_sections
authorH.J. Lu <hjl.tools@gmail.com>
Sat, 2 Sep 2017 14:37:05 +0000 (07:37 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Sat, 2 Sep 2017 22:12:04 +0000 (15:12 -0700)
elf_i386_size_dynamic_sections and elf_x86_64_size_dynamic_sections are
very similar, except for the followings:

1. elf_i386_size_dynamic_sections checks GOT_TLS_IE and GOT_TLS_IE_BOTH.
elf_x86_64_size_dynamic_sections checks only GOT_TLS_IE.  Since
GOT_TLS_IE_BOTH is never true for x86-64, it is OK to check GOT_TLS_IE
for both i386 and x86-64.
2, x86-64 sets tlsdesc_plt, but i386 doesn't.  We set tlsdesc_plt only
if target_id == X86_64_ELF_DATA.
3. x86-64 has

  if (s != htab->elf.srelplt)
    s->reloc_count = 0;

and i386 has

  s->reloc_count = 0;

i386 did have

  if (s != htab->srelplt)
    s->reloc_count = 0;

in the original commit:

commit 67a4f2b710581acc83afecff55424af285ecbc28
Author: Alexandre Oliva <aoliva@redhat.com>
Date:   Wed Jan 18 21:07:51 2006 +0000

But it was removed by

commit 5ae0bfb60a576344d7f701605346282c1144499e
Author: Richard Sandiford <rdsandiford@googlemail.com>
Date:   Tue Feb 28 07:16:12 2006 +0000

    bfd/
            * elf32-i386.c (elf_i386_link_hash_table): Add next_tls_desc_index.
            (elf_i386_link_hash_table_create): Initialize it.
            (elf_i386_compute_jump_table_size): Use it instead of
            srelplt->reloc_count.
            (allocate_dynrelocs): Likewise.
            (elf_i386_size_dynamic_sections): Likewise.
            (elf_i386_relocate_section): Likewise.

A later commit:

commit e1f987424b7b3f5ac63a2a6ae044a202a44b8ff8
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Oct 21 15:13:37 2011 +0000

    Put IRELATIVE relocations after JUMP_SLOT.

    bfd/

    2011-10-21  H.J. Lu  <hongjiu.lu@intel.com>

            PR ld/13302
            * elf32-i386.c (elf_i386_link_hash_table): Add next_jump_slot_index
            and next_irelative_index.
            (elf_i386_link_hash_table_create): Initialize next_jump_slot_index
            and next_irelative_index.
            (elf_i386_allocate_dynrelocs): Increment reloc_count instead of
            next_tls_desc_index.
            (elf_i386_size_dynamic_sections): Set next_tls_desc_index and
            next_irelative_index from reloc_count.
            (elf_i386_finish_dynamic_symbol): Put R_386_IRELATIVE after
            R_386_JUMP_SLOT.

changed it back to use reloc_count again. So it is correct to use

  if (s != htab->elf.srelplt)
    s->reloc_count = 0;

for both i386 and x86-64 now.
4. i386 and x86-64 use different DT_XXXs.  They are handled by adding
them to elf_x86_link_hash_table.

With these changes, we can share _bfd_x86_elf_size_dynamic_sections in
elf32-i386.c and elf64-x86-64.c.

* elf32-i386.c (elf_i386_convert_load): Renamed to ...
(_bfd_i386_elf_convert_load): This.  Remove static.
(elf_i386_size_dynamic_sections): Removed.
(elf_backend_size_dynamic_sections): Likewise.
* elf64-x86-64.c (elf_x86_64_convert_load): Renamed to ...
(_bfd_x86_64_elf_convert_load): This.  Remove static.
(elf_x86_64_size_dynamic_sections): Removed.
(elf_backend_size_dynamic_sections): Likewise.
* elfxx-x86.c (_bfd_x86_elf_allocate_dynrelocs): Renamed to ...
(elf_x86_allocate_dynrelocs): This.  Make it static.
(_bfd_x86_elf_allocate_local_dynrelocs): Renamed to ...
(elf_x86_allocate_local_dynreloc): This.  Make it static.
(elf_i386_is_reloc_section): New function.
(elf_x86_64_is_reloc_section): Likewise.
(_bfd_x86_elf_link_hash_table_create): Initialize convert_load,
is_reloc_section, dt_reloc, dt_reloc_sz and dt_reloc_ent.
Rearrange got_entry_size initialization.
(_bfd_x86_elf_size_dynamic_sections): New function.
* elfxx-x86.h (elf_x86_link_hash_table): Add convert_load,
is_reloc_section, dt_reloc, dt_reloc_sz and dt_reloc_ent.
(_bfd_i386_elf_convert_load): New.
(_bfd_x86_64_elf_convert_load): Likewise.
(_bfd_x86_elf_size_dynamic_sections): Likewise.
(elf_backend_size_dynamic_sections): Likewise.
(_bfd_x86_elf_allocate_dynrelocs): Removed.
(_bfd_x86_elf_allocate_local_dynrelocs): Likewise.

bfd/elf32-i386.c
bfd/elf64-x86-64.c
bfd/elfxx-x86.c
bfd/elfxx-x86.h

index a123bbd..ce9cf3a 100644 (file)
@@ -1949,9 +1949,9 @@ error_return:
 
 /* Convert load via the GOT slot to load immediate.  */
 
-static bfd_boolean
-elf_i386_convert_load (bfd *abfd, asection *sec,
-                      struct bfd_link_info *link_info)
+bfd_boolean
+_bfd_i386_elf_convert_load (bfd *abfd, asection *sec,
+                           struct bfd_link_info *link_info)
 {
   struct elf_x86_link_hash_table *htab;
   Elf_Internal_Shdr *symtab_hdr;
@@ -2077,401 +2077,6 @@ elf_i386_convert_load (bfd *abfd, asection *sec,
   return FALSE;
 }
 
-/* Set the sizes of the dynamic sections.  */
-
-static bfd_boolean
-elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
-{
-  struct elf_x86_link_hash_table *htab;
-  bfd *dynobj;
-  asection *s;
-  bfd_boolean relocs;
-  bfd *ibfd;
-
-  htab = elf_x86_hash_table (info, I386_ELF_DATA);
-  if (htab == NULL)
-    return FALSE;
-  dynobj = htab->elf.dynobj;
-  if (dynobj == NULL)
-    abort ();
-
-  /* Set up .got offsets for local syms, and space for local dynamic
-     relocs.  */
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
-    {
-      bfd_signed_vma *local_got;
-      bfd_signed_vma *end_local_got;
-      char *local_tls_type;
-      bfd_vma *local_tlsdesc_gotent;
-      bfd_size_type locsymcount;
-      Elf_Internal_Shdr *symtab_hdr;
-      asection *srel;
-
-      if (! is_x86_elf (ibfd, htab))
-       continue;
-
-      for (s = ibfd->sections; s != NULL; s = s->next)
-       {
-         struct elf_dyn_relocs *p;
-
-         if (!elf_i386_convert_load (ibfd, s, info))
-           return FALSE;
-
-         for (p = ((struct elf_dyn_relocs *)
-                    elf_section_data (s)->local_dynrel);
-              p != NULL;
-              p = p->next)
-           {
-             if (!bfd_is_abs_section (p->sec)
-                 && bfd_is_abs_section (p->sec->output_section))
-               {
-                 /* Input section has been discarded, either because
-                    it is a copy of a linkonce section or due to
-                    linker script /DISCARD/, so we'll be discarding
-                    the relocs too.  */
-               }
-             else if (htab->is_vxworks
-                      && strcmp (p->sec->output_section->name,
-                                 ".tls_vars") == 0)
-               {
-                 /* Relocations in vxworks .tls_vars sections are
-                    handled specially by the loader.  */
-               }
-             else if (p->count != 0)
-               {
-                 srel = elf_section_data (p->sec)->sreloc;
-                 srel->size += p->count * sizeof (Elf32_External_Rel);
-                 if ((p->sec->output_section->flags & SEC_READONLY) != 0
-                     && (info->flags & DF_TEXTREL) == 0)
-                   {
-                     info->flags |= DF_TEXTREL;
-                     if ((info->warn_shared_textrel && bfd_link_pic (info))
-                         || info->error_textrel)
-                       /* xgettext:c-format */
-                       info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
-                                               p->sec->owner, p->sec);
-                   }
-               }
-           }
-       }
-
-      local_got = elf_local_got_refcounts (ibfd);
-      if (!local_got)
-       continue;
-
-      symtab_hdr = &elf_symtab_hdr (ibfd);
-      locsymcount = symtab_hdr->sh_info;
-      end_local_got = local_got + locsymcount;
-      local_tls_type = elf_x86_local_got_tls_type (ibfd);
-      local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
-      s = htab->elf.sgot;
-      srel = htab->elf.srelgot;
-      for (; local_got < end_local_got;
-          ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
-       {
-         *local_tlsdesc_gotent = (bfd_vma) -1;
-         if (*local_got > 0)
-           {
-             if (GOT_TLS_GDESC_P (*local_tls_type))
-               {
-                 *local_tlsdesc_gotent = htab->elf.sgotplt->size
-                   - elf_x86_compute_jump_table_size (htab);
-                 htab->elf.sgotplt->size += 8;
-                 *local_got = (bfd_vma) -2;
-               }
-             if (! GOT_TLS_GDESC_P (*local_tls_type)
-                 || GOT_TLS_GD_P (*local_tls_type))
-               {
-                 *local_got = s->size;
-                 s->size += 4;
-                 if (GOT_TLS_GD_P (*local_tls_type)
-                     || *local_tls_type == GOT_TLS_IE_BOTH)
-                   s->size += 4;
-               }
-             if (bfd_link_pic (info)
-                 || GOT_TLS_GD_ANY_P (*local_tls_type)
-                 || (*local_tls_type & GOT_TLS_IE))
-               {
-                 if (*local_tls_type == GOT_TLS_IE_BOTH)
-                   srel->size += 2 * sizeof (Elf32_External_Rel);
-                 else if (GOT_TLS_GD_P (*local_tls_type)
-                          || ! GOT_TLS_GDESC_P (*local_tls_type))
-                   srel->size += sizeof (Elf32_External_Rel);
-                 if (GOT_TLS_GDESC_P (*local_tls_type))
-                   htab->elf.srelplt->size += sizeof (Elf32_External_Rel);
-               }
-           }
-         else
-           *local_got = (bfd_vma) -1;
-       }
-    }
-
-  if (htab->tls_ld_or_ldm_got.refcount > 0)
-    {
-      /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
-        relocs.  */
-      htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
-      htab->elf.sgot->size += 8;
-      htab->elf.srelgot->size += sizeof (Elf32_External_Rel);
-    }
-  else
-    htab->tls_ld_or_ldm_got.offset = -1;
-
-  /* Allocate global sym .plt and .got entries, and space for global
-     sym dynamic relocs.  */
-  elf_link_hash_traverse (&htab->elf, _bfd_x86_elf_allocate_dynrelocs,
-                         info);
-
-  /* Allocate .plt and .got entries, and space for local symbols.  */
-  htab_traverse (htab->loc_hash_table,
-                _bfd_x86_elf_allocate_local_dynrelocs,
-                info);
-
-  /* For every jump slot reserved in the sgotplt, reloc_count is
-     incremented.  However, when we reserve space for TLS descriptors,
-     it's not incremented, so in order to compute the space reserved
-     for them, it suffices to multiply the reloc count by the jump
-     slot size.
-
-     PR ld/13302: We start next_irelative_index at the end of .rela.plt
-     so that R_386_IRELATIVE entries come last.  */
-  if (htab->elf.srelplt)
-    {
-      htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
-      htab->sgotplt_jump_table_size
-       = elf_x86_compute_jump_table_size (htab);
-      htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
-    }
-  else if (htab->elf.irelplt)
-    htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
-
-
-  if (htab->elf.sgotplt)
-    {
-      /* Don't allocate .got.plt section if there are no GOT nor PLT
-         entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
-      if ((htab->elf.hgot == NULL
-          || !htab->elf.hgot->ref_regular_nonweak)
-         && (htab->elf.sgotplt->size
-             == get_elf_backend_data (output_bfd)->got_header_size)
-         && (htab->elf.splt == NULL
-             || htab->elf.splt->size == 0)
-         && (htab->elf.sgot == NULL
-             || htab->elf.sgot->size == 0)
-         && (htab->elf.iplt == NULL
-             || htab->elf.iplt->size == 0)
-         && (htab->elf.igotplt == NULL
-             || htab->elf.igotplt->size == 0))
-       htab->elf.sgotplt->size = 0;
-    }
-
-  if (_bfd_elf_eh_frame_present (info))
-    {
-      if (htab->plt_eh_frame != NULL
-         && htab->elf.splt != NULL
-         && htab->elf.splt->size != 0
-         && !bfd_is_abs_section (htab->elf.splt->output_section))
-       htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
-
-      if (htab->plt_got_eh_frame != NULL
-         && htab->plt_got != NULL
-         && htab->plt_got->size != 0
-         && !bfd_is_abs_section (htab->plt_got->output_section))
-       htab->plt_got_eh_frame->size
-         = htab->non_lazy_plt->eh_frame_plt_size;
-
-      /* Unwind info for the second PLT and .plt.got sections are
-        identical.  */
-      if (htab->plt_second_eh_frame != NULL
-         && htab->plt_second != NULL
-         && htab->plt_second->size != 0
-         && !bfd_is_abs_section (htab->plt_second->output_section))
-       htab->plt_second_eh_frame->size
-         = htab->non_lazy_plt->eh_frame_plt_size;
-    }
-
-  /* We now have determined the sizes of the various dynamic sections.
-     Allocate memory for them.  */
-  relocs = FALSE;
-  for (s = dynobj->sections; s != NULL; s = s->next)
-    {
-      bfd_boolean strip_section = TRUE;
-
-      if ((s->flags & SEC_LINKER_CREATED) == 0)
-       continue;
-
-      if (s == htab->elf.splt
-         || s == htab->elf.sgot)
-       {
-         /* Strip this section if we don't need it; see the
-            comment below.  */
-         /* We'd like to strip these sections if they aren't needed, but if
-            we've exported dynamic symbols from them we must leave them.
-            It's too late to tell BFD to get rid of the symbols.  */
-
-         if (htab->elf.hplt != NULL)
-           strip_section = FALSE;
-       }
-      else if (s == htab->elf.sgotplt
-              || s == htab->elf.iplt
-              || s == htab->elf.igotplt
-              || s == htab->plt_second
-              || s == htab->plt_got
-              || s == htab->plt_eh_frame
-              || s == htab->plt_got_eh_frame
-              || s == htab->plt_second_eh_frame
-              || s == htab->elf.sdynbss
-              || s == htab->elf.sdynrelro)
-       {
-         /* Strip these too.  */
-       }
-      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel"))
-       {
-         if (s->size != 0
-             && s != htab->elf.srelplt
-             && s != htab->srelplt2)
-           relocs = TRUE;
-
-         /* We use the reloc_count field as a counter if we need
-            to copy relocs into the output file.  */
-         s->reloc_count = 0;
-       }
-      else
-       {
-         /* It's not one of our sections, so don't allocate space.  */
-         continue;
-       }
-
-      if (s->size == 0)
-       {
-         /* If we don't need this section, strip it from the
-            output file.  This is mostly to handle .rel.bss and
-            .rel.plt.  We must create both sections in
-            create_dynamic_sections, because they must be created
-            before the linker maps input sections to output
-            sections.  The linker does that before
-            adjust_dynamic_symbol is called, and it is that
-            function which decides whether anything needs to go
-            into these sections.  */
-         if (strip_section)
-           s->flags |= SEC_EXCLUDE;
-         continue;
-       }
-
-      if ((s->flags & SEC_HAS_CONTENTS) == 0)
-       continue;
-
-      /* Allocate memory for the section contents.  We use bfd_zalloc
-        here in case unused entries are not reclaimed before the
-        section's contents are written out.  This should not happen,
-        but this way if it does, we get a R_386_NONE reloc instead
-        of garbage.  */
-      s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
-      if (s->contents == NULL)
-       return FALSE;
-    }
-
-  if (htab->plt_eh_frame != NULL
-      && htab->plt_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_eh_frame->contents,
-             htab->plt.eh_frame_plt,
-             htab->plt_eh_frame->size);
-      bfd_put_32 (dynobj, htab->elf.splt->size,
-                 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
-    }
-
-  if (htab->plt_got_eh_frame != NULL
-      && htab->plt_got_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_got_eh_frame->contents,
-             htab->non_lazy_plt->eh_frame_plt,
-             htab->plt_got_eh_frame->size);
-      bfd_put_32 (dynobj, htab->plt_got->size,
-                 (htab->plt_got_eh_frame->contents
-                  + PLT_FDE_LEN_OFFSET));
-    }
-
-  if (htab->plt_second_eh_frame != NULL
-      && htab->plt_second_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_second_eh_frame->contents,
-             htab->non_lazy_plt->eh_frame_plt,
-             htab->plt_second_eh_frame->size);
-      bfd_put_32 (dynobj, htab->plt_second->size,
-                 (htab->plt_second_eh_frame->contents
-                  + PLT_FDE_LEN_OFFSET));
-    }
-
-  if (htab->elf.dynamic_sections_created)
-    {
-      /* Add some entries to the .dynamic section.  We fill in the
-        values later, in elf_i386_finish_dynamic_sections, but we
-        must add the entries now so that we get the correct size for
-        the .dynamic section.  The DT_DEBUG entry is filled in by the
-        dynamic linker and used by the debugger.  */
-#define add_dynamic_entry(TAG, VAL) \
-  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
-
-      if (bfd_link_executable (info))
-       {
-         if (!add_dynamic_entry (DT_DEBUG, 0))
-           return FALSE;
-       }
-
-      if (htab->elf.splt->size != 0)
-       {
-         /* DT_PLTGOT is used by prelink even if there is no PLT
-            relocation.  */
-         if (!add_dynamic_entry (DT_PLTGOT, 0))
-           return FALSE;
-       }
-
-      if (htab->elf.srelplt->size != 0)
-       {
-         if (!add_dynamic_entry (DT_PLTRELSZ, 0)
-             || !add_dynamic_entry (DT_PLTREL, DT_REL)
-             || !add_dynamic_entry (DT_JMPREL, 0))
-           return FALSE;
-       }
-
-      if (relocs)
-       {
-         if (!add_dynamic_entry (DT_REL, 0)
-             || !add_dynamic_entry (DT_RELSZ, 0)
-             || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel)))
-           return FALSE;
-
-         /* If any dynamic relocs apply to a read-only section,
-            then we need a DT_TEXTREL entry.  */
-         if ((info->flags & DF_TEXTREL) == 0)
-           elf_link_hash_traverse (&htab->elf,
-                                   _bfd_x86_elf_readonly_dynrelocs,
-                                   info);
-
-         if ((info->flags & DF_TEXTREL) != 0)
-           {
-             if (htab->readonly_dynrelocs_against_ifunc)
-               {
-                 info->callbacks->einfo
-                   (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
-                 bfd_set_error (bfd_error_bad_value);
-                 return FALSE;
-               }
-
-             if (!add_dynamic_entry (DT_TEXTREL, 0))
-               return FALSE;
-           }
-       }
-      if (htab->is_vxworks
-         && !elf_vxworks_add_dynamic_entries (output_bfd, info))
-       return FALSE;
-    }
-#undef add_dynamic_entry
-
-  return TRUE;
-}
-
 /* Set the correct type for an x86 ELF section.  We do this by the
    section name, which is a hack, but ought to work.  */
 
@@ -5192,7 +4797,6 @@ elf_i386_link_setup_gnu_properties (struct bfd_link_info *info)
 #define elf_backend_grok_psinfo                      elf_i386_grok_psinfo
 #define elf_backend_reloc_type_class         elf_i386_reloc_type_class
 #define elf_backend_relocate_section         elf_i386_relocate_section
-#define elf_backend_size_dynamic_sections     elf_i386_size_dynamic_sections
 #define elf_backend_setup_gnu_properties      elf_i386_link_setup_gnu_properties
 
 #include "elf32-target.h"
index 08a8c92..fa295a5 100644 (file)
@@ -2392,9 +2392,9 @@ error_return:
 
 /* Convert load via the GOT slot to load immediate.  */
 
-static bfd_boolean
-elf_x86_64_convert_load (bfd *abfd, asection *sec,
-                        struct bfd_link_info *link_info)
+bfd_boolean
+_bfd_x86_64_elf_convert_load (bfd *abfd, asection *sec,
+                             struct bfd_link_info *link_info)
 {
   Elf_Internal_Shdr *symtab_hdr;
   Elf_Internal_Rela *internal_relocs;
@@ -2520,405 +2520,6 @@ elf_x86_64_convert_load (bfd *abfd, asection *sec,
   return FALSE;
 }
 
-/* Set the sizes of the dynamic sections.  */
-
-static bfd_boolean
-elf_x86_64_size_dynamic_sections (bfd *output_bfd,
-                                 struct bfd_link_info *info)
-{
-  struct elf_x86_link_hash_table *htab;
-  bfd *dynobj;
-  asection *s;
-  bfd_boolean relocs;
-  bfd *ibfd;
-  const struct elf_backend_data *bed;
-
-  htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
-  if (htab == NULL)
-    return FALSE;
-  bed = get_elf_backend_data (output_bfd);
-
-  dynobj = htab->elf.dynobj;
-  if (dynobj == NULL)
-    abort ();
-
-  /* Set up .got offsets for local syms, and space for local dynamic
-     relocs.  */
-  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
-    {
-      bfd_signed_vma *local_got;
-      bfd_signed_vma *end_local_got;
-      char *local_tls_type;
-      bfd_vma *local_tlsdesc_gotent;
-      bfd_size_type locsymcount;
-      Elf_Internal_Shdr *symtab_hdr;
-      asection *srel;
-
-      if (! is_x86_elf (ibfd, htab))
-       continue;
-
-      for (s = ibfd->sections; s != NULL; s = s->next)
-       {
-         struct elf_dyn_relocs *p;
-
-         if (!elf_x86_64_convert_load (ibfd, s, info))
-           return FALSE;
-
-         for (p = (struct elf_dyn_relocs *)
-                   (elf_section_data (s)->local_dynrel);
-              p != NULL;
-              p = p->next)
-           {
-             if (!bfd_is_abs_section (p->sec)
-                 && bfd_is_abs_section (p->sec->output_section))
-               {
-                 /* Input section has been discarded, either because
-                    it is a copy of a linkonce section or due to
-                    linker script /DISCARD/, so we'll be discarding
-                    the relocs too.  */
-               }
-             else if (p->count != 0)
-               {
-                 srel = elf_section_data (p->sec)->sreloc;
-                 srel->size += p->count * bed->s->sizeof_rela;
-                 if ((p->sec->output_section->flags & SEC_READONLY) != 0
-                     && (info->flags & DF_TEXTREL) == 0)
-                   {
-                     info->flags |= DF_TEXTREL;
-                     if ((info->warn_shared_textrel && bfd_link_pic (info))
-                         || info->error_textrel)
-                       /* xgettext:c-format */
-                       info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
-                                               p->sec->owner, p->sec);
-                   }
-               }
-           }
-       }
-
-      local_got = elf_local_got_refcounts (ibfd);
-      if (!local_got)
-       continue;
-
-      symtab_hdr = &elf_symtab_hdr (ibfd);
-      locsymcount = symtab_hdr->sh_info;
-      end_local_got = local_got + locsymcount;
-      local_tls_type = elf_x86_local_got_tls_type (ibfd);
-      local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
-      s = htab->elf.sgot;
-      srel = htab->elf.srelgot;
-      for (; local_got < end_local_got;
-          ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
-       {
-         *local_tlsdesc_gotent = (bfd_vma) -1;
-         if (*local_got > 0)
-           {
-             if (GOT_TLS_GDESC_P (*local_tls_type))
-               {
-                 *local_tlsdesc_gotent = htab->elf.sgotplt->size
-                   - elf_x86_compute_jump_table_size (htab);
-                 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
-                 *local_got = (bfd_vma) -2;
-               }
-             if (! GOT_TLS_GDESC_P (*local_tls_type)
-                 || GOT_TLS_GD_P (*local_tls_type))
-               {
-                 *local_got = s->size;
-                 s->size += GOT_ENTRY_SIZE;
-                 if (GOT_TLS_GD_P (*local_tls_type))
-                   s->size += GOT_ENTRY_SIZE;
-               }
-             if (bfd_link_pic (info)
-                 || GOT_TLS_GD_ANY_P (*local_tls_type)
-                 || *local_tls_type == GOT_TLS_IE)
-               {
-                 if (GOT_TLS_GDESC_P (*local_tls_type))
-                   {
-                     htab->elf.srelplt->size
-                       += bed->s->sizeof_rela;
-                     htab->tlsdesc_plt = (bfd_vma) -1;
-                   }
-                 if (! GOT_TLS_GDESC_P (*local_tls_type)
-                     || GOT_TLS_GD_P (*local_tls_type))
-                   srel->size += bed->s->sizeof_rela;
-               }
-           }
-         else
-           *local_got = (bfd_vma) -1;
-       }
-    }
-
-  if (htab->tls_ld_or_ldm_got.refcount > 0)
-    {
-      /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
-        relocs.  */
-      htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
-      htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
-      htab->elf.srelgot->size += bed->s->sizeof_rela;
-    }
-  else
-    htab->tls_ld_or_ldm_got.offset = -1;
-
-  /* Allocate global sym .plt and .got entries, and space for global
-     sym dynamic relocs.  */
-  elf_link_hash_traverse (&htab->elf, _bfd_x86_elf_allocate_dynrelocs,
-                         info);
-
-  /* Allocate .plt and .got entries, and space for local symbols.  */
-  htab_traverse (htab->loc_hash_table,
-                _bfd_x86_elf_allocate_local_dynrelocs,
-                info);
-
-  /* For every jump slot reserved in the sgotplt, reloc_count is
-     incremented.  However, when we reserve space for TLS descriptors,
-     it's not incremented, so in order to compute the space reserved
-     for them, it suffices to multiply the reloc count by the jump
-     slot size.
-
-     PR ld/13302: We start next_irelative_index at the end of .rela.plt
-     so that R_X86_64_IRELATIVE entries come last.  */
-  if (htab->elf.srelplt)
-    {
-      htab->sgotplt_jump_table_size
-       = elf_x86_compute_jump_table_size (htab);
-      htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
-    }
-  else if (htab->elf.irelplt)
-    htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
-
-  if (htab->tlsdesc_plt)
-    {
-      /* If we're not using lazy TLS relocations, don't generate the
-        PLT and GOT entries they require.  */
-      if ((info->flags & DF_BIND_NOW))
-       htab->tlsdesc_plt = 0;
-      else
-       {
-         htab->tlsdesc_got = htab->elf.sgot->size;
-         htab->elf.sgot->size += GOT_ENTRY_SIZE;
-         /* Reserve room for the initial entry.
-            FIXME: we could probably do away with it in this case.  */
-         if (htab->elf.splt->size == 0)
-           htab->elf.splt->size = htab->plt.plt_entry_size;
-         htab->tlsdesc_plt = htab->elf.splt->size;
-         htab->elf.splt->size += htab->plt.plt_entry_size;
-       }
-    }
-
-  if (htab->elf.sgotplt)
-    {
-      /* Don't allocate .got.plt section if there are no GOT nor PLT
-        entries and there is no refeence to _GLOBAL_OFFSET_TABLE_.  */
-      if ((htab->elf.hgot == NULL
-          || !htab->elf.hgot->ref_regular_nonweak)
-         && (htab->elf.sgotplt->size
-             == get_elf_backend_data (output_bfd)->got_header_size)
-         && (htab->elf.splt == NULL
-             || htab->elf.splt->size == 0)
-         && (htab->elf.sgot == NULL
-             || htab->elf.sgot->size == 0)
-         && (htab->elf.iplt == NULL
-             || htab->elf.iplt->size == 0)
-         && (htab->elf.igotplt == NULL
-             || htab->elf.igotplt->size == 0))
-       htab->elf.sgotplt->size = 0;
-    }
-
-  if (_bfd_elf_eh_frame_present (info))
-    {
-      if (htab->plt_eh_frame != NULL
-         && htab->elf.splt != NULL
-         && htab->elf.splt->size != 0
-         && !bfd_is_abs_section (htab->elf.splt->output_section))
-       htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
-
-      if (htab->plt_got_eh_frame != NULL
-         && htab->plt_got != NULL
-         && htab->plt_got->size != 0
-         && !bfd_is_abs_section (htab->plt_got->output_section))
-       htab->plt_got_eh_frame->size
-         = htab->non_lazy_plt->eh_frame_plt_size;
-
-      /* Unwind info for the second PLT and .plt.got sections are
-        identical.  */
-      if (htab->plt_second_eh_frame != NULL
-         && htab->plt_second != NULL
-         && htab->plt_second->size != 0
-         && !bfd_is_abs_section (htab->plt_second->output_section))
-       htab->plt_second_eh_frame->size
-         = htab->non_lazy_plt->eh_frame_plt_size;
-    }
-
-  /* We now have determined the sizes of the various dynamic sections.
-     Allocate memory for them.  */
-  relocs = FALSE;
-  for (s = dynobj->sections; s != NULL; s = s->next)
-    {
-      if ((s->flags & SEC_LINKER_CREATED) == 0)
-       continue;
-
-      if (s == htab->elf.splt
-         || s == htab->elf.sgot
-         || s == htab->elf.sgotplt
-         || s == htab->elf.iplt
-         || s == htab->elf.igotplt
-         || s == htab->plt_second
-         || s == htab->plt_got
-         || s == htab->plt_eh_frame
-         || s == htab->plt_got_eh_frame
-         || s == htab->plt_second_eh_frame
-         || s == htab->elf.sdynbss
-         || s == htab->elf.sdynrelro)
-       {
-         /* Strip this section if we don't need it; see the
-            comment below.  */
-       }
-      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
-       {
-         if (s->size != 0 && s != htab->elf.srelplt)
-           relocs = TRUE;
-
-         /* We use the reloc_count field as a counter if we need
-            to copy relocs into the output file.  */
-         if (s != htab->elf.srelplt)
-           s->reloc_count = 0;
-       }
-      else
-       {
-         /* It's not one of our sections, so don't allocate space.  */
-         continue;
-       }
-
-      if (s->size == 0)
-       {
-         /* If we don't need this section, strip it from the
-            output file.  This is mostly to handle .rela.bss and
-            .rela.plt.  We must create both sections in
-            create_dynamic_sections, because they must be created
-            before the linker maps input sections to output
-            sections.  The linker does that before
-            adjust_dynamic_symbol is called, and it is that
-            function which decides whether anything needs to go
-            into these sections.  */
-
-         s->flags |= SEC_EXCLUDE;
-         continue;
-       }
-
-      if ((s->flags & SEC_HAS_CONTENTS) == 0)
-       continue;
-
-      /* Allocate memory for the section contents.  We use bfd_zalloc
-        here in case unused entries are not reclaimed before the
-        section's contents are written out.  This should not happen,
-        but this way if it does, we get a R_X86_64_NONE reloc instead
-        of garbage.  */
-      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
-      if (s->contents == NULL)
-       return FALSE;
-    }
-
-  if (htab->plt_eh_frame != NULL
-      && htab->plt_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_eh_frame->contents,
-             htab->plt.eh_frame_plt, htab->plt_eh_frame->size);
-      bfd_put_32 (dynobj, htab->elf.splt->size,
-                 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
-    }
-
-  if (htab->plt_got_eh_frame != NULL
-      && htab->plt_got_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_got_eh_frame->contents,
-             htab->non_lazy_plt->eh_frame_plt,
-             htab->plt_got_eh_frame->size);
-      bfd_put_32 (dynobj, htab->plt_got->size,
-                 (htab->plt_got_eh_frame->contents
-                  + PLT_FDE_LEN_OFFSET));
-    }
-
-  if (htab->plt_second_eh_frame != NULL
-      && htab->plt_second_eh_frame->contents != NULL)
-    {
-      memcpy (htab->plt_second_eh_frame->contents,
-             htab->non_lazy_plt->eh_frame_plt,
-             htab->plt_second_eh_frame->size);
-      bfd_put_32 (dynobj, htab->plt_second->size,
-                 (htab->plt_second_eh_frame->contents
-                  + PLT_FDE_LEN_OFFSET));
-    }
-
-  if (htab->elf.dynamic_sections_created)
-    {
-      /* Add some entries to the .dynamic section.  We fill in the
-        values later, in elf_x86_64_finish_dynamic_sections, but we
-        must add the entries now so that we get the correct size for
-        the .dynamic section.  The DT_DEBUG entry is filled in by the
-        dynamic linker and used by the debugger.  */
-#define add_dynamic_entry(TAG, VAL) \
-  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
-
-      if (bfd_link_executable (info))
-       {
-         if (!add_dynamic_entry (DT_DEBUG, 0))
-           return FALSE;
-       }
-
-      if (htab->elf.splt->size != 0)
-       {
-         /* DT_PLTGOT is used by prelink even if there is no PLT
-            relocation.  */
-         if (!add_dynamic_entry (DT_PLTGOT, 0))
-           return FALSE;
-       }
-
-      if (htab->elf.srelplt->size != 0)
-       {
-         if (!add_dynamic_entry (DT_PLTRELSZ, 0)
-             || !add_dynamic_entry (DT_PLTREL, DT_RELA)
-             || !add_dynamic_entry (DT_JMPREL, 0))
-           return FALSE;
-       }
-
-      if (htab->tlsdesc_plt
-         && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
-             || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
-       return FALSE;
-
-      if (relocs)
-       {
-         if (!add_dynamic_entry (DT_RELA, 0)
-             || !add_dynamic_entry (DT_RELASZ, 0)
-             || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
-           return FALSE;
-
-         /* If any dynamic relocs apply to a read-only section,
-            then we need a DT_TEXTREL entry.  */
-         if ((info->flags & DF_TEXTREL) == 0)
-           elf_link_hash_traverse (&htab->elf,
-                                   _bfd_x86_elf_readonly_dynrelocs,
-                                   info);
-
-         if ((info->flags & DF_TEXTREL) != 0)
-           {
-             if (htab->readonly_dynrelocs_against_ifunc)
-               {
-                 info->callbacks->einfo
-                   (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
-                 bfd_set_error (bfd_error_bad_value);
-                 return FALSE;
-               }
-
-             if (!add_dynamic_entry (DT_TEXTREL, 0))
-               return FALSE;
-           }
-       }
-    }
-#undef add_dynamic_entry
-
-  return TRUE;
-}
-
 /* Return the relocation value for @tpoff relocation
    if STT_TLS virtual address is ADDRESS.  */
 
@@ -5821,7 +5422,6 @@ elf_x86_64_special_sections[]=
 #endif
 #define elf_backend_reloc_type_class       elf_x86_64_reloc_type_class
 #define elf_backend_relocate_section       elf_x86_64_relocate_section
-#define elf_backend_size_dynamic_sections   elf_x86_64_size_dynamic_sections
 #define elf_backend_init_index_section     _bfd_elf_init_1_index_section
 #define elf_backend_object_p               elf64_x86_64_elf_object_p
 #define bfd_elf64_get_synthetic_symtab     elf_x86_64_get_synthetic_symtab
index 646b251..874799e 100644 (file)
@@ -82,8 +82,8 @@ _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
 /* Allocate space in .plt, .got and associated reloc sections for
    dynamic relocs.  */
 
-bfd_boolean
-_bfd_x86_elf_allocate_dynrelocs (struct elf_link_hash_entry *h,
+static bfd_boolean
+elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h,
                                 void *inf)
 {
   struct bfd_link_info *info;
@@ -573,8 +573,8 @@ _bfd_x86_elf_readonly_dynrelocs (struct elf_link_hash_entry *h,
 /* Allocate space in .plt, .got and associated reloc sections for
    local dynamic relocs.  */
 
-bfd_boolean
-_bfd_x86_elf_allocate_local_dynrelocs (void **slot, void *inf)
+static bfd_boolean
+elf_x86_allocate_local_dynreloc (void **slot, void *inf)
 {
   struct elf_link_hash_entry *h
     = (struct elf_link_hash_entry *) *slot;
@@ -586,7 +586,7 @@ _bfd_x86_elf_allocate_local_dynrelocs (void **slot, void *inf)
       || h->root.type != bfd_link_hash_defined)
     abort ();
 
-  return _bfd_x86_elf_allocate_dynrelocs (h, inf);
+  return elf_x86_allocate_dynrelocs (h, inf);
 }
 
 /* Find and/or create a hash entry for local symbol.  */
@@ -721,6 +721,20 @@ elf_x86_link_hash_table_free (bfd *obfd)
   _bfd_elf_link_hash_table_free (obfd);
 }
 
+static bfd_boolean
+elf_i386_is_reloc_section (const char *secname)
+{
+  return CONST_STRNEQ (secname, ".rel");
+}
+
+#ifdef BFD64
+static bfd_boolean
+elf_x86_64_is_reloc_section (const char *secname)
+{
+  return CONST_STRNEQ (secname, ".rela");
+}
+#endif
+
 /* Create an x86 ELF linker hash table.  */
 
 struct bfd_link_hash_table *
@@ -745,12 +759,21 @@ _bfd_x86_elf_link_hash_table_create (bfd *abfd)
     }
 
 #ifdef BFD64
+  /* NB: If BFD64 isn't defined, only i386 will be supported.  */
+  if (bed->target_id == X86_64_ELF_DATA)
+    {
+      ret->convert_load = _bfd_x86_64_elf_convert_load;
+      ret->is_reloc_section = elf_x86_64_is_reloc_section;
+      ret->dt_reloc = DT_RELA;
+      ret->dt_reloc_sz = DT_RELASZ;
+      ret->dt_reloc_ent = DT_RELAENT;
+      ret->got_entry_size = 8;
+    }
   if (ABI_64_P (abfd))
     {
       ret->r_info = elf64_r_info;
       ret->r_sym = elf64_r_sym;
       ret->sizeof_reloc = sizeof (Elf64_External_Rela);
-      ret->got_entry_size = 8;
       ret->pointer_r_type = R_X86_64_64;
       ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
       ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
@@ -764,7 +787,6 @@ _bfd_x86_elf_link_hash_table_create (bfd *abfd)
       if (bed->target_id == X86_64_ELF_DATA)
        {
          ret->sizeof_reloc = sizeof (Elf32_External_Rela);
-         ret->got_entry_size = 8;
          ret->pointer_r_type = R_X86_64_32;
          ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
          ret->dynamic_interpreter_size
@@ -773,6 +795,11 @@ _bfd_x86_elf_link_hash_table_create (bfd *abfd)
        }
       else
        {
+         ret->convert_load = _bfd_i386_elf_convert_load;
+         ret->is_reloc_section = elf_i386_is_reloc_section;
+         ret->dt_reloc = DT_REL;
+         ret->dt_reloc_sz = DT_RELSZ;
+         ret->dt_reloc_ent = DT_RELENT;
          ret->sizeof_reloc = sizeof (Elf32_External_Rel);
          ret->got_entry_size = 4;
          ret->pointer_r_type = R_386_32;
@@ -839,6 +866,432 @@ _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
   return _bfd_elf_link_check_relocs (abfd, info);
 }
 
+/* Set the sizes of the dynamic sections.  */
+
+bfd_boolean
+_bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
+                                   struct bfd_link_info *info)
+{
+  struct elf_x86_link_hash_table *htab;
+  bfd *dynobj;
+  asection *s;
+  bfd_boolean relocs;
+  bfd *ibfd;
+  const struct elf_backend_data *bed
+    = get_elf_backend_data (output_bfd);
+
+  htab = elf_x86_hash_table (info, bed->target_id);
+  if (htab == NULL)
+    return FALSE;
+  dynobj = htab->elf.dynobj;
+  if (dynobj == NULL)
+    abort ();
+
+  /* Set up .got offsets for local syms, and space for local dynamic
+     relocs.  */
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
+    {
+      bfd_signed_vma *local_got;
+      bfd_signed_vma *end_local_got;
+      char *local_tls_type;
+      bfd_vma *local_tlsdesc_gotent;
+      bfd_size_type locsymcount;
+      Elf_Internal_Shdr *symtab_hdr;
+      asection *srel;
+
+      if (! is_x86_elf (ibfd, htab))
+       continue;
+
+      for (s = ibfd->sections; s != NULL; s = s->next)
+       {
+         struct elf_dyn_relocs *p;
+
+         if (!htab->convert_load (ibfd, s, info))
+           return FALSE;
+
+         for (p = ((struct elf_dyn_relocs *)
+                    elf_section_data (s)->local_dynrel);
+              p != NULL;
+              p = p->next)
+           {
+             if (!bfd_is_abs_section (p->sec)
+                 && bfd_is_abs_section (p->sec->output_section))
+               {
+                 /* Input section has been discarded, either because
+                    it is a copy of a linkonce section or due to
+                    linker script /DISCARD/, so we'll be discarding
+                    the relocs too.  */
+               }
+             else if (htab->is_vxworks
+                      && strcmp (p->sec->output_section->name,
+                                 ".tls_vars") == 0)
+               {
+                 /* Relocations in vxworks .tls_vars sections are
+                    handled specially by the loader.  */
+               }
+             else if (p->count != 0)
+               {
+                 srel = elf_section_data (p->sec)->sreloc;
+                 srel->size += p->count * htab->sizeof_reloc;
+                 if ((p->sec->output_section->flags & SEC_READONLY) != 0
+                     && (info->flags & DF_TEXTREL) == 0)
+                   {
+                     info->flags |= DF_TEXTREL;
+                     if ((info->warn_shared_textrel && bfd_link_pic (info))
+                         || info->error_textrel)
+                       /* xgettext:c-format */
+                       info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
+                                               p->sec->owner, p->sec);
+                   }
+               }
+           }
+       }
+
+      local_got = elf_local_got_refcounts (ibfd);
+      if (!local_got)
+       continue;
+
+      symtab_hdr = &elf_symtab_hdr (ibfd);
+      locsymcount = symtab_hdr->sh_info;
+      end_local_got = local_got + locsymcount;
+      local_tls_type = elf_x86_local_got_tls_type (ibfd);
+      local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
+      s = htab->elf.sgot;
+      srel = htab->elf.srelgot;
+      for (; local_got < end_local_got;
+          ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
+       {
+         *local_tlsdesc_gotent = (bfd_vma) -1;
+         if (*local_got > 0)
+           {
+             if (GOT_TLS_GDESC_P (*local_tls_type))
+               {
+                 *local_tlsdesc_gotent = htab->elf.sgotplt->size
+                   - elf_x86_compute_jump_table_size (htab);
+                 htab->elf.sgotplt->size += 2 * htab->got_entry_size;
+                 *local_got = (bfd_vma) -2;
+               }
+             if (! GOT_TLS_GDESC_P (*local_tls_type)
+                 || GOT_TLS_GD_P (*local_tls_type))
+               {
+                 *local_got = s->size;
+                 s->size += htab->got_entry_size;
+                 if (GOT_TLS_GD_P (*local_tls_type)
+                     || *local_tls_type == GOT_TLS_IE_BOTH)
+                   s->size += htab->got_entry_size;
+               }
+             if (bfd_link_pic (info)
+                 || GOT_TLS_GD_ANY_P (*local_tls_type)
+                 || (*local_tls_type & GOT_TLS_IE))
+               {
+                 if (*local_tls_type == GOT_TLS_IE_BOTH)
+                   srel->size += 2 * htab->sizeof_reloc;
+                 else if (GOT_TLS_GD_P (*local_tls_type)
+                          || ! GOT_TLS_GDESC_P (*local_tls_type))
+                   srel->size += htab->sizeof_reloc;
+                 if (GOT_TLS_GDESC_P (*local_tls_type))
+                   {
+                     htab->elf.srelplt->size += htab->sizeof_reloc;
+                     if (bed->target_id == X86_64_ELF_DATA)
+                       htab->tlsdesc_plt = (bfd_vma) -1;
+                   }
+               }
+           }
+         else
+           *local_got = (bfd_vma) -1;
+       }
+    }
+
+  if (htab->tls_ld_or_ldm_got.refcount > 0)
+    {
+      /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
+         or R_X86_64_TLSLD relocs.  */
+      htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
+      htab->elf.sgot->size += 2 * htab->got_entry_size;
+      htab->elf.srelgot->size += htab->sizeof_reloc;
+    }
+  else
+    htab->tls_ld_or_ldm_got.offset = -1;
+
+  /* Allocate global sym .plt and .got entries, and space for global
+     sym dynamic relocs.  */
+  elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
+                         info);
+
+  /* Allocate .plt and .got entries, and space for local symbols.  */
+  htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
+                info);
+
+  /* For every jump slot reserved in the sgotplt, reloc_count is
+     incremented.  However, when we reserve space for TLS descriptors,
+     it's not incremented, so in order to compute the space reserved
+     for them, it suffices to multiply the reloc count by the jump
+     slot size.
+
+     PR ld/13302: We start next_irelative_index at the end of .rela.plt
+     so that R_{386,X86_64}_IRELATIVE entries come last.  */
+  if (htab->elf.srelplt)
+    {
+      htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
+      htab->sgotplt_jump_table_size
+       = elf_x86_compute_jump_table_size (htab);
+      htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
+    }
+  else if (htab->elf.irelplt)
+    htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
+
+  if (htab->tlsdesc_plt)
+    {
+      /* NB: tlsdesc_plt is set only for x86-64.  If we're not using
+        lazy TLS relocations, don't generate the PLT and GOT entries
+        they require.  */
+      if ((info->flags & DF_BIND_NOW))
+       htab->tlsdesc_plt = 0;
+      else
+       {
+         htab->tlsdesc_got = htab->elf.sgot->size;
+         htab->elf.sgot->size += htab->got_entry_size;
+         /* Reserve room for the initial entry.
+            FIXME: we could probably do away with it in this case.  */
+         if (htab->elf.splt->size == 0)
+           htab->elf.splt->size = htab->plt.plt_entry_size;
+         htab->tlsdesc_plt = htab->elf.splt->size;
+         htab->elf.splt->size += htab->plt.plt_entry_size;
+       }
+    }
+
+  if (htab->elf.sgotplt)
+    {
+      /* Don't allocate .got.plt section if there are no GOT nor PLT
+        entries and there is no reference to _GLOBAL_OFFSET_TABLE_.  */
+      if ((htab->elf.hgot == NULL
+          || !htab->elf.hgot->ref_regular_nonweak)
+         && (htab->elf.sgotplt->size == bed->got_header_size)
+         && (htab->elf.splt == NULL
+             || htab->elf.splt->size == 0)
+         && (htab->elf.sgot == NULL
+             || htab->elf.sgot->size == 0)
+         && (htab->elf.iplt == NULL
+             || htab->elf.iplt->size == 0)
+         && (htab->elf.igotplt == NULL
+             || htab->elf.igotplt->size == 0))
+       htab->elf.sgotplt->size = 0;
+    }
+
+  if (_bfd_elf_eh_frame_present (info))
+    {
+      if (htab->plt_eh_frame != NULL
+         && htab->elf.splt != NULL
+         && htab->elf.splt->size != 0
+         && !bfd_is_abs_section (htab->elf.splt->output_section))
+       htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
+
+      if (htab->plt_got_eh_frame != NULL
+         && htab->plt_got != NULL
+         && htab->plt_got->size != 0
+         && !bfd_is_abs_section (htab->plt_got->output_section))
+       htab->plt_got_eh_frame->size
+         = htab->non_lazy_plt->eh_frame_plt_size;
+
+      /* Unwind info for the second PLT and .plt.got sections are
+        identical.  */
+      if (htab->plt_second_eh_frame != NULL
+         && htab->plt_second != NULL
+         && htab->plt_second->size != 0
+         && !bfd_is_abs_section (htab->plt_second->output_section))
+       htab->plt_second_eh_frame->size
+         = htab->non_lazy_plt->eh_frame_plt_size;
+    }
+
+  /* We now have determined the sizes of the various dynamic sections.
+     Allocate memory for them.  */
+  relocs = FALSE;
+  for (s = dynobj->sections; s != NULL; s = s->next)
+    {
+      bfd_boolean strip_section = TRUE;
+
+      if ((s->flags & SEC_LINKER_CREATED) == 0)
+       continue;
+
+      if (s == htab->elf.splt
+         || s == htab->elf.sgot)
+       {
+         /* Strip this section if we don't need it; see the
+            comment below.  */
+         /* We'd like to strip these sections if they aren't needed, but if
+            we've exported dynamic symbols from them we must leave them.
+            It's too late to tell BFD to get rid of the symbols.  */
+
+         if (htab->elf.hplt != NULL)
+           strip_section = FALSE;
+       }
+      else if (s == htab->elf.sgotplt
+              || s == htab->elf.iplt
+              || s == htab->elf.igotplt
+              || s == htab->plt_second
+              || s == htab->plt_got
+              || s == htab->plt_eh_frame
+              || s == htab->plt_got_eh_frame
+              || s == htab->plt_second_eh_frame
+              || s == htab->elf.sdynbss
+              || s == htab->elf.sdynrelro)
+       {
+         /* Strip these too.  */
+       }
+      else if (htab->is_reloc_section (bfd_get_section_name (dynobj, s)))
+       {
+         if (s->size != 0
+             && s != htab->elf.srelplt
+             && s != htab->srelplt2)
+           relocs = TRUE;
+
+         /* We use the reloc_count field as a counter if we need
+            to copy relocs into the output file.  */
+         if (s != htab->elf.srelplt)
+           s->reloc_count = 0;
+       }
+      else
+       {
+         /* It's not one of our sections, so don't allocate space.  */
+         continue;
+       }
+
+      if (s->size == 0)
+       {
+         /* If we don't need this section, strip it from the
+            output file.  This is mostly to handle .rel.bss and
+            .rel.plt.  We must create both sections in
+            create_dynamic_sections, because they must be created
+            before the linker maps input sections to output
+            sections.  The linker does that before
+            adjust_dynamic_symbol is called, and it is that
+            function which decides whether anything needs to go
+            into these sections.  */
+         if (strip_section)
+           s->flags |= SEC_EXCLUDE;
+         continue;
+       }
+
+      if ((s->flags & SEC_HAS_CONTENTS) == 0)
+       continue;
+
+      /* Allocate memory for the section contents.  We use bfd_zalloc
+        here in case unused entries are not reclaimed before the
+        section's contents are written out.  This should not happen,
+        but this way if it does, we get a R_386_NONE or R_X86_64_NONE
+        reloc instead of garbage.  */
+      s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
+      if (s->contents == NULL)
+       return FALSE;
+    }
+
+  if (htab->plt_eh_frame != NULL
+      && htab->plt_eh_frame->contents != NULL)
+    {
+      memcpy (htab->plt_eh_frame->contents,
+             htab->plt.eh_frame_plt,
+             htab->plt_eh_frame->size);
+      bfd_put_32 (dynobj, htab->elf.splt->size,
+                 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
+    }
+
+  if (htab->plt_got_eh_frame != NULL
+      && htab->plt_got_eh_frame->contents != NULL)
+    {
+      memcpy (htab->plt_got_eh_frame->contents,
+             htab->non_lazy_plt->eh_frame_plt,
+             htab->plt_got_eh_frame->size);
+      bfd_put_32 (dynobj, htab->plt_got->size,
+                 (htab->plt_got_eh_frame->contents
+                  + PLT_FDE_LEN_OFFSET));
+    }
+
+  if (htab->plt_second_eh_frame != NULL
+      && htab->plt_second_eh_frame->contents != NULL)
+    {
+      memcpy (htab->plt_second_eh_frame->contents,
+             htab->non_lazy_plt->eh_frame_plt,
+             htab->plt_second_eh_frame->size);
+      bfd_put_32 (dynobj, htab->plt_second->size,
+                 (htab->plt_second_eh_frame->contents
+                  + PLT_FDE_LEN_OFFSET));
+    }
+
+  if (htab->elf.dynamic_sections_created)
+    {
+      /* Add some entries to the .dynamic section.  We fill in the
+        values later, in elf_{i386,x86_64}_finish_dynamic_sections,
+        but we must add the entries now so that we get the correct
+        size for the .dynamic section.  The DT_DEBUG entry is filled
+        in by the dynamic linker and used by the debugger.  */
+#define add_dynamic_entry(TAG, VAL) \
+  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
+
+      if (bfd_link_executable (info))
+       {
+         if (!add_dynamic_entry (DT_DEBUG, 0))
+           return FALSE;
+       }
+
+      if (htab->elf.splt->size != 0)
+       {
+         /* DT_PLTGOT is used by prelink even if there is no PLT
+            relocation.  */
+         if (!add_dynamic_entry (DT_PLTGOT, 0))
+           return FALSE;
+       }
+
+      if (htab->elf.srelplt->size != 0)
+       {
+         if (!add_dynamic_entry (DT_PLTRELSZ, 0)
+             || !add_dynamic_entry (DT_PLTREL, htab->dt_reloc)
+             || !add_dynamic_entry (DT_JMPREL, 0))
+           return FALSE;
+       }
+
+      if (htab->tlsdesc_plt
+         && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
+             || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
+       return FALSE;
+
+      if (relocs)
+       {
+         if (!add_dynamic_entry (htab->dt_reloc, 0)
+             || !add_dynamic_entry (htab->dt_reloc_sz, 0)
+             || !add_dynamic_entry (htab->dt_reloc_ent,
+                                    htab->sizeof_reloc))
+           return FALSE;
+
+         /* If any dynamic relocs apply to a read-only section,
+            then we need a DT_TEXTREL entry.  */
+         if ((info->flags & DF_TEXTREL) == 0)
+           elf_link_hash_traverse (&htab->elf,
+                                   _bfd_x86_elf_readonly_dynrelocs,
+                                   info);
+
+         if ((info->flags & DF_TEXTREL) != 0)
+           {
+             if (htab->readonly_dynrelocs_against_ifunc)
+               {
+                 info->callbacks->einfo
+                   (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
+                 bfd_set_error (bfd_error_bad_value);
+                 return FALSE;
+               }
+
+             if (!add_dynamic_entry (DT_TEXTREL, 0))
+               return FALSE;
+           }
+       }
+      if (htab->is_vxworks
+         && !elf_vxworks_add_dynamic_entries (output_bfd, info))
+       return FALSE;
+    }
+#undef add_dynamic_entry
+
+  return TRUE;
+}
+
 bfd_boolean
 _bfd_x86_elf_always_size_sections (bfd *output_bfd,
                                   struct bfd_link_info *info)
index 8b99835..61244f5 100644 (file)
@@ -319,8 +319,14 @@ struct elf_x86_link_hash_table
 
   bfd_vma (*r_info) (bfd_vma, bfd_vma);
   bfd_vma (*r_sym) (bfd_vma);
+  bfd_boolean (*convert_load) (bfd *, asection *,
+                              struct bfd_link_info *);
+  bfd_boolean (*is_reloc_section) (const char *);
   enum elf_target_id target_id;
   unsigned int sizeof_reloc;
+  unsigned int dt_reloc;
+  unsigned int dt_reloc_sz;
+  unsigned int dt_reloc_ent;
   unsigned int got_entry_size;
   unsigned int pointer_r_type;
   int dynamic_interpreter_size;
@@ -398,6 +404,12 @@ struct elf_x86_plt
    && elf_tdata (bfd) != NULL                          \
    && elf_object_id (bfd) == (htab)->target_id)
 
+extern bfd_boolean _bfd_i386_elf_convert_load
+  (bfd *, asection *, struct bfd_link_info *);
+
+extern bfd_boolean _bfd_x86_64_elf_convert_load
+  (bfd *, asection *, struct bfd_link_info *);
+
 extern bfd_boolean _bfd_x86_elf_mkobject
   (bfd *);
 
@@ -407,12 +419,6 @@ extern void _bfd_x86_elf_set_tls_module_base
 extern bfd_vma _bfd_x86_elf_dtpoff_base
   (struct bfd_link_info *);
 
-extern bfd_boolean _bfd_x86_elf_allocate_dynrelocs
-  (struct elf_link_hash_entry *, void *);
-
-extern bfd_boolean _bfd_x86_elf_allocate_local_dynrelocs
-  (void **, void *);
-
 extern bfd_boolean _bfd_x86_elf_readonly_dynrelocs
   (struct elf_link_hash_entry *, void *);
 
@@ -438,6 +444,9 @@ extern int _bfd_x86_elf_compare_relocs
 extern bfd_boolean _bfd_x86_elf_link_check_relocs
   (bfd *, struct bfd_link_info *);
 
+extern bfd_boolean _bfd_x86_elf_size_dynamic_sections
+  (bfd *, struct bfd_link_info *);
+
 extern bfd_boolean _bfd_x86_elf_always_size_sections
   (bfd *, struct bfd_link_info *);
 
@@ -488,6 +497,8 @@ extern bfd * _bfd_x86_elf_link_setup_gnu_properties
 #define bfd_elf32_bfd_link_check_relocs \
   _bfd_x86_elf_link_check_relocs
 
+#define elf_backend_size_dynamic_sections \
+  _bfd_x86_elf_size_dynamic_sections
 #define elf_backend_always_size_sections \
   _bfd_x86_elf_always_size_sections
 #define elf_backend_merge_symbol_attribute \