Bug 17773 - Gap between sections and section headers when ld -s is used
Summary: Gap between sections and section headers when ld -s is used
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.25
: P2 normal
Target Milestone: 2.26
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-30 15:52 UTC by Michael Deutschmann
Modified: 2015-01-05 13:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Deutschmann 2014-12-30 15:52:04 UTC
When the "-s" option is used, the binutils 2.25 linker produces ELF executables with a 4-byte gap between the last section and the section headers.

The 2.24 linker doesn't do that, and the gap is not present if a file is linked without "-s" and separately stripped.

I'm observing this on i386-pc-linux-uclibc with the normal (not gold) linker.
Comment 1 H.J. Lu 2014-12-30 16:15:23 UTC
Please provide a small standalone testcase.
Comment 2 Michael Deutschmann 2014-12-30 17:35:09 UTC
Just the two-line assembler file:

.global _start
_start: hlt

suffices to demonstrate the behavior.

If this is assembled (with as directly, no options besides -o) and linked (directly, just -s and -o) on 2.25, the resulting file gives the following under readelf -S:

There are 3 section headers, starting at offset 0x6c:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        08048054 000054 000001 00  AX  0   0  1
  [ 2] .shstrtab         STRTAB          00000000 000055 000011 00      0   0  1

The last section ends just before 0x66 (0x55 + 0x11).  With alignment, that should place the section header table at 0x68.  But it's actually at 0x6c.
Comment 3 H.J. Lu 2014-12-30 21:01:19 UTC
It is caused by

commit a485e98ea0cbb61ea9da1e7858da545e0bcf1a46
Author: Alan Modra <amodra@gmail.com>
Date:   Wed Sep 10 14:26:25 2014 +0930

    Move ELF section headers to end of object file
    
    Currently, section ordering differs a little for non-loaded reloc
    sections output by ld -emit-relocs or ld -r and that after passing
    such objects through objcopy.  Not that it really matters, but it
    would be better for a simple objcopy to produce an unchanged output
    object file.  Also, section headers are put somewhere in the middle of
    the non-loaded sections, again slightly differently for ld and
    objcopy.  This patch fixes these discrepancies and puts section
    headers last, which is where gold puts them, and is where
    bfd_from_remote_memory wrongly assumed they will be found.
    
    bfd/
    	* elf.c (assign_file_positions_except_relocs): Move section header
    	placement to..
    	(_bfd_elf_assign_file_positions_for_relocs): ..here.  Make static.
    	* elf-bfd.h (_bfd_elf_assign_file_positions_for_relocs): Delete.
    	* elflink.c (bfd_elf_final_link): Don't call above function.
Comment 4 Sourceware Commits 2014-12-30 22:06:44 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, hjl/pr17773 has been created
        at  b72a529b6aeb4bbd07a393b0e14ea997e8c4b1ae (commit)

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b72a529b6aeb4bbd07a393b0e14ea997e8c4b1ae

commit b72a529b6aeb4bbd07a393b0e14ea997e8c4b1ae
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Dec 30 14:01:27 2014 -0800

    Assign file position for .strtab only if needed
    
    bfd/
    
    	PR ld/17773
    	* elflink.c (bfd_elf_final_link): Assign the file position for
    	the symbol string table only there are symbols to be emitted.
    
    ld/testsuite/
    
    	PR ld/17773
    	* ld-elf/binutils.exp (binutils_test): Add an optional
    	readelf_options.  Replace -l with $readelf_options.  Add a
    	gap test.
    	* ld/testsuite/ld-elf/gap.s: New file.

-----------------------------------------------------------------------
Comment 5 H.J. Lu 2014-12-30 22:07:17 UTC
A patch is posted at

https://sourceware.org/ml/binutils/2014-12/msg00313.html
Comment 6 Sourceware Commits 2014-12-31 03:10:27 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  ee3b52e917c3a19850232a6a20f5698090619fc6 (commit)
      from  24fbeb8dc2c20f86c9940cc62e6af192a9d11b1a (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ee3b52e917c3a19850232a6a20f5698090619fc6

commit ee3b52e917c3a19850232a6a20f5698090619fc6
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Dec 30 19:09:11 2014 -0800

    Assign file position for .strtab only if needed
    
    bfd/
    
    	PR ld/17773
    	* elflink.c (bfd_elf_final_link): Assign the file position for
    	the symbol string table only there are symbols to be emitted.
    
    ld/testsuite/
    
    	PR ld/17773
    	* ld-elf/binutils.exp (binutils_test): Add an optional
    	readelf_options.  Replace -l with $readelf_options.  Add a
    	gap test.
    	* ld/testsuite/ld-elf/gap.s: New file.

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog                        |    6 +++++
 bfd/elflink.c                        |   38 +++++++++++++++++----------------
 ld/testsuite/ChangeLog               |    8 +++++++
 ld/testsuite/ld-elf/binutils.exp     |   15 ++++++++-----
 ld/testsuite/ld-elf/{lma.s => gap.s} |    0
 5 files changed, 43 insertions(+), 24 deletions(-)
 copy ld/testsuite/ld-elf/{lma.s => gap.s} (100%)
Comment 7 Sourceware Commits 2015-01-05 13:01:15 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, binutils-2_25-branch has been updated
       via  77ce4dbedbae824d8637e7e55903f7cd0709d49e (commit)
      from  15a7f882647865ed9c262997915fbb243e54ebb6 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=77ce4dbedbae824d8637e7e55903f7cd0709d49e

commit 77ce4dbedbae824d8637e7e55903f7cd0709d49e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Jan 5 04:42:37 2015 -0800

    Assign file position for .strtab only if needed
    
    bfd/
    
    	PR ld/17773
    	* elflink.c (bfd_elf_final_link): Assign the file position for
    	the symbol string table only there are symbols to be emitted.
    
    ld/testsuite/
    
    	PR ld/17773
    	* ld-elf/binutils.exp (binutils_test): Add an optional
    	readelf_options.  Replace -l with $readelf_options.  Add a
    	gap test.
    	* ld/testsuite/ld-elf/gap.s: New file.

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog                        |    6 +++++
 bfd/elflink.c                        |   38 +++++++++++++++++----------------
 ld/testsuite/ChangeLog               |    8 +++++++
 ld/testsuite/ld-elf/binutils.exp     |   15 ++++++++-----
 ld/testsuite/ld-elf/{lma.s => gap.s} |    0
 5 files changed, 43 insertions(+), 24 deletions(-)
 copy ld/testsuite/ld-elf/{lma.s => gap.s} (100%)
Comment 8 H.J. Lu 2015-01-05 13:05:04 UTC
Fixed for 2.26 and the next 2.25 release.