PATCH: PR ld/17842: [ARM] Gap between sections and section headers when ld -s is used when ld -s is used

Alan Modra amodra@gmail.com
Thu Jan 15 01:58:00 GMT 2015


On Wed, Jan 14, 2015 at 05:41:26PM -0800, H.J. Lu wrote:
> On Wed, Jan 14, 2015 at 5:04 PM, Alan Modra <amodra@gmail.com> wrote:
> > On Wed, Jan 14, 2015 at 10:07:39AM -0800, H.J. Lu wrote:
> >>       * elflink.c (bfd_elf_final_link): Assign the file position for
> >>       the symbol table only there are symbols to be emitted.
> >
> > Not OK.  Search for places where elf_link_output_sym is called.
> >
> 
> For info->strip == strip_all, can backend still output local symbols?

Yes, and when emit_relocs too.  I'm testing the following.

diff --git a/bfd/elflink.c b/bfd/elflink.c
index 73d2360..c07cedf 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -8540,6 +8532,11 @@ elf_link_flush_output_syms (struct elf_final_link_info *flinfo,
 
       hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr;
       pos = hdr->sh_offset + hdr->sh_size;
+      if (hdr->sh_size == 0)
+	{
+	  pos = elf_next_file_pos (flinfo->output_bfd);
+	  pos = _bfd_elf_assign_file_position_for_section (hdr, pos, TRUE);
+	}
       amt = flinfo->symbuf_count * bed->s->sizeof_sym;
       if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0
 	  || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt)
@@ -10609,7 +10606,6 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   bfd_size_type max_internal_reloc_count;
   bfd_size_type max_sym_count;
   bfd_size_type max_sym_shndx_count;
-  file_ptr off;
   Elf_Internal_Sym elfsym;
   unsigned int i;
   Elf_Internal_Shdr *symtab_hdr;
@@ -10885,9 +10881,6 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   /* sh_offset is set just below.  */
   symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align;
 
-  off = elf_next_file_pos (abfd);
-  off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE);
-
   /* Note that at this point elf_next_file_pos (abfd) is
      incorrect.  We do not yet know the size of the .symtab section.
      We correct next_file_pos below, after we do know the size.  */
@@ -11294,31 +11287,29 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
     return FALSE;
 
   /* Now we know the size of the symtab section.  */
-  off += symtab_hdr->sh_size;
-
-  symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
-  if (symtab_shndx_hdr->sh_name != 0)
-    {
-      symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
-      symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
-      symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
-      amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
-      symtab_shndx_hdr->sh_size = amt;
-
-      off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
-						       off, TRUE);
-
-      if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
-	  || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
-	return FALSE;
-    }
-
-
-  if (bfd_get_symcount (abfd) > 0)
+  if (bfd_get_symcount (abfd) != 0)
     {
       /* Finish up and write out the symbol string table (.strtab)
 	 section.  */
       Elf_Internal_Shdr *symstrtab_hdr;
+      file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size;
+
+      symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
+      if (symtab_shndx_hdr->sh_name != 0)
+	{
+	  symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
+	  symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
+	  symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
+	  amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx);
+	  symtab_shndx_hdr->sh_size = amt;
+
+	  off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr,
+							   off, TRUE);
+
+	  if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
+	      || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
+	    return FALSE;
+	}
 
       symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
       /* sh_name was set in prep_headers.  */
@@ -11600,6 +11591,8 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
 	    {
 	      /* The contents of the .dynstr section are actually in a
 		 stringtab.  */
+	      file_ptr off;
+
 	      off = elf_section_data (o->output_section)->this_hdr.sh_offset;
 	      if (bfd_seek (abfd, off, SEEK_SET) != 0
 		  || ! _bfd_elf_strtab_emit (abfd,

-- 
Alan Modra
Australia Development Lab, IBM



More information about the Binutils mailing list