[0/7] ELF: Don't require section header on ELF objects

Message ID 20200308234242.1225816-1-hjl.tools@gmail.com
Headers show
Series
  • ELF: Don't require section header on ELF objects
Related show

Message

H.J. Lu March 8, 2020, 11:42 p.m.
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 when building an executable or shared library, an
objcopy and strip option, --remove-section-header, to remove ELF
section header from an executable or shared library.

The PT_DYNAMIC segment contains DT_HASH/DT_GNU_HASH/DT_MIPS_XHASH,
DT_STRTAB, DT_SYMTAB, DT_STRSZ and DT_SYMENT entries, which can be
used to reconstruct dynamic symbol table when section header isn't
available.  For DT_HASH, the number of dynamic symbol table entries
equals the number of chains.  For DT_GNU_HASH/DT_MIPS_XHASH, only
defined symbols with non-STB_LOCAL indings are in hash table.  Since
in dynamic symbol table, all symbols with STB_LOCAL binding are placed
before symbols with other bindings and all undefined symbols are placed
before defined ones, the highest symbol index in DT_GNU_HASH and
DT_MIPS_XHASH is the highest dynamic symbol table index.


H.J. Lu (5):
  bfd: Reconstruct dynamic symbol table from PT_DYNAMIC segment
  readelf: Compute dynamic symbol table size from hash table
  binutils: Add --remove-section-header tests
  ld: Add tests for -z nosectionheader and --remove-section-header
  ld: Add -z nosectionheader test to bootstrap.exp

Kaylee Blake (2):
  ELF: Don't require section header on ELF objects
  ld: Add a simple test for -z nosectionheader

 bfd/bfd-in2.h                                 |   8 +-
 bfd/bfd.c                                     |   8 +-
 bfd/elf-bfd.h                                 |   8 +
 bfd/elf.c                                     | 443 ++++++++++++++++++
 bfd/elfcode.h                                 | 174 ++++++-
 bfd/elflink.c                                 | 148 ++++--
 bfd/elfxx-target.h                            |   6 +-
 binutils/NEWS                                 |   3 +
 binutils/doc/binutils.texi                    |  12 +
 binutils/objcopy.c                            |  54 ++-
 binutils/readelf.c                            | 226 ++++-----
 binutils/testsuite/binutils-all/objcopy.exp   |  13 +
 .../testsuite/binutils-all/remove-header-1.d  |   8 +
 ld/NEWS                                       |   3 +
 ld/emultempl/elf.em                           |   4 +
 ld/ld.h                                       |   3 +
 ld/ld.texi                                    |   6 +
 ld/ldlang.c                                   |   4 +
 ld/lexsup.c                                   |  12 +
 ld/testsuite/ld-bootstrap/bootstrap.exp       |  10 +-
 ld/testsuite/ld-elf/hash.d                    |   8 +-
 ld/testsuite/ld-elf/no-section-header.exp     | 336 +++++++++++++
 ld/testsuite/ld-elf/nosectionheader.d         |  12 +
 ld/testsuite/ld-elf/pr13195.d                 |   2 +-
 ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.nd  |   3 +
 ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.rd  |  20 +
 ld/testsuite/ld-elf/pr22393-2a-sec-hdr.rd     |  19 +
 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.nd  |   3 +
 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.rd  |  20 +
 .../ld-elf/pr22393-2b-static-no-sec-hdr.rd    |  12 +
 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 +
 ld/testsuite/ld-elfvsb/hidden2.d              |   2 +-
 ld/testsuite/ld-mips-elf/hash2.d              |   8 +-
 36 files changed, 1458 insertions(+), 200 deletions(-)
 create mode 100644 binutils/testsuite/binutils-all/remove-header-1.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/pr22393-2a-no-sec-hdr.nd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2a-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.nd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.rd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2b-static-no-sec-hdr.rd
 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

-- 
2.24.1

Comments

Fangrui Song March 19, 2020, 12:48 a.m. | #1
>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 when building an executable or shared library, an

>objcopy and strip option, --remove-section-header, to remove ELF

>section header from an executable or shared library.

>

>The PT_DYNAMIC segment contains DT_HASH/DT_GNU_HASH/DT_MIPS_XHASH,

>DT_STRTAB, DT_SYMTAB, DT_STRSZ and DT_SYMENT entries, which can be

>used to reconstruct dynamic symbol table when section header isn't

>available.  For DT_HASH, the number of dynamic symbol table entries

>equals the number of chains.  For DT_GNU_HASH/DT_MIPS_XHASH, only

>defined symbols with non-STB_LOCAL indings are in hash table.  Since

>in dynamic symbol table, all symbols with STB_LOCAL binding are placed

>before symbols with other bindings and all undefined symbols are placed

>before defined ones, the highest symbol index in DT_GNU_HASH and

>DT_MIPS_XHASH is the highest dynamic symbol table index.

>

>

>H.J. Lu (5):

>  bfd: Reconstruct dynamic symbol table from PT_DYNAMIC segment

>  readelf: Compute dynamic symbol table size from hash table

>  binutils: Add --remove-section-header tests

>  ld: Add tests for -z nosectionheader and --remove-section-header

>  ld: Add -z nosectionheader test to bootstrap.exp

>

>Kaylee Blake (2):

>  ELF: Don't require section header on ELF objects

>  ld: Add a simple test for -z nosectionheader

>

> bfd/bfd-in2.h                                 |   8 +-

> bfd/bfd.c                                     |   8 +-

> bfd/elf-bfd.h                                 |   8 +

> bfd/elf.c                                     | 443 ++++++++++++++++++

> bfd/elfcode.h                                 | 174 ++++++-

> bfd/elflink.c                                 | 148 ++++--

> bfd/elfxx-target.h                            |   6 +-

> binutils/NEWS                                 |   3 +

> binutils/doc/binutils.texi                    |  12 +

> binutils/objcopy.c                            |  54 ++-

> binutils/readelf.c                            | 226 ++++-----

> binutils/testsuite/binutils-all/objcopy.exp   |  13 +

> .../testsuite/binutils-all/remove-header-1.d  |   8 +

> ld/NEWS                                       |   3 +

> ld/emultempl/elf.em                           |   4 +

> ld/ld.h                                       |   3 +

> ld/ld.texi                                    |   6 +

> ld/ldlang.c                                   |   4 +

> ld/lexsup.c                                   |  12 +

> ld/testsuite/ld-bootstrap/bootstrap.exp       |  10 +-

> ld/testsuite/ld-elf/hash.d                    |   8 +-

> ld/testsuite/ld-elf/no-section-header.exp     | 336 +++++++++++++

> ld/testsuite/ld-elf/nosectionheader.d         |  12 +

> ld/testsuite/ld-elf/pr13195.d                 |   2 +-

> ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.nd  |   3 +

> ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.rd  |  20 +

> ld/testsuite/ld-elf/pr22393-2a-sec-hdr.rd     |  19 +

> ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.nd  |   3 +

> ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.rd  |  20 +

> .../ld-elf/pr22393-2b-static-no-sec-hdr.rd    |  12 +

> 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 +

> ld/testsuite/ld-elfvsb/hidden2.d              |   2 +-

> ld/testsuite/ld-mips-elf/hash2.d              |   8 +-

> 36 files changed, 1458 insertions(+), 200 deletions(-)

> create mode 100644 binutils/testsuite/binutils-all/remove-header-1.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/pr22393-2a-no-sec-hdr.nd

> create mode 100644 ld/testsuite/ld-elf/pr22393-2a-no-sec-hdr.rd

> create mode 100644 ld/testsuite/ld-elf/pr22393-2a-sec-hdr.rd

> create mode 100644 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.nd

> create mode 100644 ld/testsuite/ld-elf/pr22393-2b-no-sec-hdr.rd

> create mode 100644 ld/testsuite/ld-elf/pr22393-2b-static-no-sec-hdr.rd

> 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

>

>-- 

>2.24.1


For objcopy --remove-section-header, we can probably implement an
enhanced feature --strip-sections first. eu-strip and llvm-objcopy
(since https://reviews.llvm.org/D38335) have --strip-sections.

--strip-sections strips the section header table and sections not
covered by a segment. In llvm-objcopy, a non-SHF_ALLOC section can
covered by a segment.


For -z nosectionheader, I am questioning about the potential use case.
As others have commented on the thread "ELF: Don't require section header on ELF objects",
such shared objects should not be used for link. For runtime only use
cases, perhaps we can do that with a post-processing tool.


For readelf dumping of PT_DYNAMIC and DT_GNU_HASH, I think they are useful features.
H.J. Lu via Binutils March 19, 2020, 1:32 a.m. | #2
On Wed, Mar 18, 2020 at 5:48 PM Fangrui Song <i@maskray.me> wrote:
>

> >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 when building an executable or shared library, an

> >objcopy and strip option, --remove-section-header, to remove ELF

> >section header from an executable or shared library.

> >

> >The PT_DYNAMIC segment contains DT_HASH/DT_GNU_HASH/DT_MIPS_XHASH,

> >DT_STRTAB, DT_SYMTAB, DT_STRSZ and DT_SYMENT entries, which can be

> >used to reconstruct dynamic symbol table when section header isn't

> >available.  For DT_HASH, the number of dynamic symbol table entries

> >equals the number of chains.  For DT_GNU_HASH/DT_MIPS_XHASH, only

> >defined symbols with non-STB_LOCAL indings are in hash table.  Since

> >in dynamic symbol table, all symbols with STB_LOCAL binding are placed

> >before symbols with other bindings and all undefined symbols are placed

> >before defined ones, the highest symbol index in DT_GNU_HASH and

> >DT_MIPS_XHASH is the highest dynamic symbol table index.

> >

> >

> >H.J. Lu (5):

> >  bfd: Reconstruct dynamic symbol table from PT_DYNAMIC segment

> >  readelf: Compute dynamic symbol table size from hash table

> >  binutils: Add --remove-section-header tests

> >  ld: Add tests for -z nosectionheader and --remove-section-header

> >  ld: Add -z nosectionheader test to bootstrap.exp

> >

> >Kaylee Blake (2):

> >  ELF: Don't require section header on ELF objects

> >  ld: Add a simple test for -z nosectionheader


> For objcopy --remove-section-header, we can probably implement an

> enhanced feature --strip-sections first. eu-strip and llvm-objcopy

> (since https://reviews.llvm.org/D38335) have --strip-sections.


I have renamed --remove-section-header to --strip-sections on
users/hjl/pr25617/master branch:

https://gitlab.com/x86-binutils/binutils-gdb/-/tree/users/hjl/pr25617/master

Kaylee, is your paper work with FSF in order? I will submit the updated
patch set after your paper is on file with FSF.

> --strip-sections strips the section header table and sections not

> covered by a segment. In llvm-objcopy, a non-SHF_ALLOC section can

> covered by a segment.


I think objcopy does it by default.

>

> For -z nosectionheader, I am questioning about the potential use case.

> As others have commented on the thread "ELF: Don't require section header on ELF objects",

> such shared objects should not be used for link. For runtime only use

> cases, perhaps we can do that with a post-processing tool.


-z nosectionheader works on both executable and shared library.   The
use case for shared library can be dlopen-only shared library.

>

> For readelf dumping of PT_DYNAMIC and DT_GNU_HASH, I think they are useful features.


I submitted  a separate readelf patch:

https://sourceware.org/pipermail/binutils/2020-March/110211.html

Thanks.

-- 
H.J.
H.J. Lu via Binutils March 19, 2020, 1:45 a.m. | #3
On 19/3/20 12:02 pm, H.J. Lu wrote:
> Kaylee, is your paper work with FSF in order? I will submit the updated

> patch set after your paper is on file with FSF.


I'm waiting on a response from them at the moment.

-- 
Kaylee Blake <klkblake@gmail.com>
C is the worst language, except for all the others.
H.J. Lu via Binutils May 2, 2020, 2:19 p.m. | #4
On Wed, Mar 18, 2020 at 6:46 PM Kaylee Blake via Binutils
<binutils@sourceware.org> wrote:
>

> On 19/3/20 12:02 pm, H.J. Lu wrote:

> > Kaylee, is your paper work with FSF in order? I will submit the updated

> > patch set after your paper is on file with FSF.

>

> I'm waiting on a response from them at the moment.

>


Hi Kaylee,

Any update on your paper work with FSF?

-- 
H.J.
H.J. Lu via Binutils Aug. 7, 2020, 1:11 p.m. | #5
On Tue, May 5, 2020 at 6:42 PM Kaylee Blake <klkblake@gmail.com> wrote:
>

> On 2/5/20 11:49 pm, H.J. Lu wrote:

> > On Wed, Mar 18, 2020 at 6:46 PM Kaylee Blake via Binutils

> > <binutils@sourceware.org> wrote:

> >>

> >> On 19/3/20 12:02 pm, H.J. Lu wrote:

> >>> Kaylee, is your paper work with FSF in order? I will submit the updated

> >>> patch set after your paper is on file with FSF.

> >>

> >> I'm waiting on a response from them at the moment.

> >>

> >

> > Hi Kaylee,

> >

> > Any update on your paper work with FSF?

> >

>

> Still waiting; apparently their work process has been dramatically

> slowed by the whole COVID-19 situation.

>

> --

> Kaylee Blake <klkblake@gmail.com>

> C is the worst language, except for all the others.


Hi,

I submitted a set of binutils patches:

https://sourceware.org/pipermail/binutils/2020-March/000013.html

including contribution from Kaylee Blake <klkblake@gmail.com>.
Can someone check if Kaylee's paperwork is on file with FSF?

Thanks.

-- 
H.J.
H.J. Lu via Binutils Aug. 14, 2020, 3:25 p.m. | #6
On 7/8/20 10:41 pm, H.J. Lu wrote:
> On Tue, May 5, 2020 at 6:42 PM Kaylee Blake <klkblake@gmail.com> wrote:

>>

>> On 2/5/20 11:49 pm, H.J. Lu wrote:

>>> On Wed, Mar 18, 2020 at 6:46 PM Kaylee Blake via Binutils

>>> <binutils@sourceware.org> wrote:

>>>>

>>>> On 19/3/20 12:02 pm, H.J. Lu wrote:

>>>>> Kaylee, is your paper work with FSF in order? I will submit the updated

>>>>> patch set after your paper is on file with FSF.

>>>>

>>>> I'm waiting on a response from them at the moment.

>>>>

>>>

>>> Hi Kaylee,

>>>

>>> Any update on your paper work with FSF?

>>>

>>

>> Still waiting; apparently their work process has been dramatically

>> slowed by the whole COVID-19 situation.

>>

>> --

>> Kaylee Blake <klkblake@gmail.com>

>> C is the worst language, except for all the others.

> 

> Hi,

> 

> I submitted a set of binutils patches:

> 

> https://sourceware.org/pipermail/binutils/2020-March/000013.html

> 

> including contribution from Kaylee Blake <klkblake@gmail.com>.

> Can someone check if Kaylee's paperwork is on file with FSF?

> 

> Thanks.

> 


I needed clarification on some of the language in the contract, and with
them being so busy that has been taking a while. They've confirmed it's
still in their work queue.

-- 
Kaylee Blake <klkblake@gmail.com>
C is the worst language, except for all the others.
H.J. Lu via Binutils April 2, 2021, 1:45 p.m. | #7
On Fri, Aug 14, 2020 at 8:25 AM Kaylee Blake <klkblake@gmail.com> wrote:
>

> On 7/8/20 10:41 pm, H.J. Lu wrote:

> > On Tue, May 5, 2020 at 6:42 PM Kaylee Blake <klkblake@gmail.com> wrote:

> >>

> >> On 2/5/20 11:49 pm, H.J. Lu wrote:

> >>> On Wed, Mar 18, 2020 at 6:46 PM Kaylee Blake via Binutils

> >>> <binutils@sourceware.org> wrote:

> >>>>

> >>>> On 19/3/20 12:02 pm, H.J. Lu wrote:

> >>>>> Kaylee, is your paper work with FSF in order? I will submit the updated

> >>>>> patch set after your paper is on file with FSF.

> >>>>

> >>>> I'm waiting on a response from them at the moment.

> >>>>

> >>>

> >>> Hi Kaylee,

> >>>

> >>> Any update on your paper work with FSF?

> >>>

> >>

> >> Still waiting; apparently their work process has been dramatically

> >> slowed by the whole COVID-19 situation.

> >>

> >> --

> >> Kaylee Blake <klkblake@gmail.com>

> >> C is the worst language, except for all the others.

> >

> > Hi,

> >

> > I submitted a set of binutils patches:

> >

> > https://sourceware.org/pipermail/binutils/2020-March/000013.html

> >

> > including contribution from Kaylee Blake <klkblake@gmail.com>.

> > Can someone check if Kaylee's paperwork is on file with FSF?

> >

> > Thanks.

> >

>

> I needed clarification on some of the language in the contract, and with

> them being so busy that has been taking a while. They've confirmed it's

> still in their work queue.

>

> --

> Kaylee Blake <klkblake@gmail.com>

> C is the worst language, except for all the others.


Can someone check if Kaylee's paperwork is on file with FSF?

Thanks.

-- 
H.J.
H.J. Lu via Binutils April 2, 2021, 3:21 p.m. | #8
Hi,

On Fri, 2021-04-02 at 06:45 -0700, H.J. Lu via Gcc wrote:
> On Fri, Aug 14, 2020 at 8:25 AM Kaylee Blake <klkblake@gmail.com> wrote:

> > I needed clarification on some of the language in the contract, and with

> > them being so busy that has been taking a while. They've confirmed it's

> > still in their work queue.

> > 

> Can someone check if Kaylee's paperwork is on file with FSF?


I cannot find anything, sorry. It might be best to contact the
Copyright Clerk at fsf-records@gnu.org with any details you can provide
about when and for which name/email address the assignment was
submitted.

Note that it might be a holiday this Friday/Monday over there.
Also the FSF might be a bit busy at the moment with changing its
management team and board:
https://www.fsf.org/blogs/executive-director/management-team-members-resigning

Cheers,

Mark