[v2,0/7] ELF: Strip section header in ELF objects

Message ID 20230531200617.951996-1-hjl.tools@gmail.com
Headers
Series ELF: Strip section header in ELF objects |

Message

H.J. Lu May 31, 2023, 8:06 p.m. UTC
  Changes in v2:

1. Rename the objcopy and strip option to --strip-section-headers.
2. Also strip non-alloc sections.

Section header isn't mandatory on ELF executable nor shared library.
This patch set adds a new linker option, -z nosectionheader, to omit ELF
section header, a new objcopy and strip option, --strip-section-headers,
to remove ELF section header.

H.J. Lu (5):
  ELF: Discard non-alloc sections without section header
  bfd: Improve nm and objdump without section header
  binutils: Add a --strip-section-headers test
  ld: Add tests for -z nosectionheader and --strip-section-headers
  ld: Add -z nosectionheader test to bootstrap.exp

Kaylee Blake (2):
  ELF: Strip section header in ELF objects
  ld: Add a simple test for -z nosectionheader

 bfd/bfd-in2.h                                 |   6 +-
 bfd/bfd.c                                     |   6 +-
 bfd/elf-bfd.h                                 |  15 +
 bfd/elf.c                                     | 755 ++++++++++++++++--
 bfd/elfcode.h                                 |  91 ++-
 bfd/elflink.c                                 |  12 +
 bfd/elfxx-target.h                            |   4 +-
 binutils/NEWS                                 |   5 +-
 binutils/doc/binutils.texi                    |  10 +
 binutils/objcopy.c                            |  38 +-
 binutils/testsuite/binutils-all/objcopy.exp   |   5 +
 .../binutils-all/strip-section-headers-1.d    |   5 +
 ld/NEWS                                       |   3 +
 ld/emultempl/elf.em                           |   4 +
 ld/ld.h                                       |   3 +
 ld/ld.texi                                    |   5 +
 ld/ldlang.c                                   |   8 +
 ld/lexsup.c                                   |   8 +
 ld/testsuite/ld-bootstrap/bootstrap.exp       |  10 +-
 ld/testsuite/ld-elf/hash-2.d                  |  11 +
 ld/testsuite/ld-elf/no-section-header.exp     | 370 +++++++++
 ld/testsuite/ld-elf/nosectionheader.d         |  12 +
 ld/testsuite/ld-elf/pr25617-1-no-sec-hdr.nd   |   3 +
 ld/testsuite/ld-elf/pr25617-1-no-sec-hdr.rd   |  20 +
 .../ld-elf/pr25617-1-static-no-sec-hdr.rd     |  12 +
 ld/testsuite/ld-elf/pr25617-1a-no-sec-hdr.nd  |   3 +
 ld/testsuite/ld-elf/pr25617-1a-no-sec-hdr.rd  |  20 +
 ld/testsuite/ld-elf/pr25617-1a-sec-hdr.rd     |  19 +
 ld/testsuite/ld-elf/pr25617-1a.c              |  11 +
 ld/testsuite/ld-elf/pr25617-1b.c              |  15 +
 ld/testsuite/ld-elf/start-noheader.rd         |  11 +
 .../ld-elf/start-shared-noheader-gnu.rd       |  22 +
 .../ld-elf/start-shared-noheader-sysv.rd      |  22 +
 ld/testsuite/ld-elf/start-shared-noheader.nd  |   5 +
 34 files changed, 1462 insertions(+), 87 deletions(-)
 create mode 100644 binutils/testsuite/binutils-all/strip-section-headers-1.d
 create mode 100644 ld/testsuite/ld-elf/hash-2.d
 create mode 100644 ld/testsuite/ld-elf/no-section-header.exp
 create mode 100644 ld/testsuite/ld-elf/nosectionheader.d
 create mode 100644 ld/testsuite/ld-elf/pr25617-1-no-sec-hdr.nd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1-no-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1-static-no-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1a-no-sec-hdr.nd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1a-no-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1a-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr25617-1a.c
 create mode 100644 ld/testsuite/ld-elf/pr25617-1b.c
 create mode 100644 ld/testsuite/ld-elf/start-noheader.rd
 create mode 100644 ld/testsuite/ld-elf/start-shared-noheader-gnu.rd
 create mode 100644 ld/testsuite/ld-elf/start-shared-noheader-sysv.rd
 create mode 100644 ld/testsuite/ld-elf/start-shared-noheader.nd
  

Comments

Alan Modra June 1, 2023, 11:49 p.m. UTC | #1
On Wed, May 31, 2023 at 01:06:10PM -0700, H.J. Lu wrote:
> Changes in v2:

I started reviewing this patchset, but stopped after seeing the
following results.  Even if these fails are all just testsuite issues
they indicate you haven't tested on much other than x86_64-linux.

arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
d30v-elf  +FAIL: ld-elf/nosectionheader
dlx-elf  +FAIL: ld-elf/nosectionheader
fr30-elf  +FAIL: ld-elf/nosectionheader
frv-elf  +FAIL: ld-elf/nosectionheader
frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
ft32-elf  +FAIL: ld-elf/nosectionheader
h8300-linux-gnu  +FAIL: ld-elf/hash-2
hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
iq2000-elf  +FAIL: ld-elf/nosectionheader
lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
mn10200-elf  +FAIL: ld-elf/nosectionheader
msp430-elf  +FAIL: ld-elf/nosectionheader
mt-elf  +FAIL: ld-elf/nosectionheader
nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
pj-elf  +FAIL: ld-elf/nosectionheader
riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both (start)
riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu (start)
riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv (start)
riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both (start)
riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu (start)
riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv (start)
s12z-elf  +FAIL: ld-elf/nosectionheader
xgate-elf  +FAIL: ld-elf/nosectionheader
  
H.J. Lu June 5, 2023, 3:31 p.m. UTC | #2
On Thu, Jun 1, 2023 at 4:49 PM Alan Modra <amodra@gmail.com> wrote:
>
> On Wed, May 31, 2023 at 01:06:10PM -0700, H.J. Lu wrote:
> > Changes in v2:
>
> I started reviewing this patchset, but stopped after seeing the
> following results.  Even if these fails are all just testsuite issues
> they indicate you haven't tested on much other than x86_64-linux.
>
> arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> arc-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> arc-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> bfin-linux-uclibc  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> bfin-linux-uclibc  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> csky-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> csky-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)

Fixed in v3 to allow extra symbols.

> d30v-elf  +FAIL: ld-elf/nosectionheader
> dlx-elf  +FAIL: ld-elf/nosectionheader
> fr30-elf  +FAIL: ld-elf/nosectionheader
> frv-elf  +FAIL: ld-elf/nosectionheader

Fixed in v3 to exclude the generic ELF targets.

> frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> frv-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> frv-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> ft32-elf  +FAIL: ld-elf/nosectionheader
> h8300-linux-gnu  +FAIL: ld-elf/hash-2

Fixed in v3 to require shared library support.

> hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> hppa-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> hppa-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> iq2000-elf  +FAIL: ld-elf/nosectionheader
> lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> lm32-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> lm32-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> mn10200-elf  +FAIL: ld-elf/nosectionheader
> msp430-elf  +FAIL: ld-elf/nosectionheader
> mt-elf  +FAIL: ld-elf/nosectionheader
> nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both -shared (start)
> nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu -shared (start)
> nds32le-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv -shared (start)
> nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both -shared (start)
> nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu -shared (start)
> nds32le-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv -shared (start)
> pj-elf  +FAIL: ld-elf/nosectionheader
> riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=both (start)
> riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=gnu (start)
> riscv64-linux-gnu  +FAIL: objcopy --strip-section-headers --hash-style=sysv (start)
> riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=both (start)
> riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=gnu (start)
> riscv64-linux-gnu  +FAIL: strip --strip-section-headers --hash-style=sysv (start)
> s12z-elf  +FAIL: ld-elf/nosectionheader
> xgate-elf  +FAIL: ld-elf/nosectionheader
>
>

Will submit the v3 patch set.

Thanks.