[v2] static-pie: Skip the empty PT_LOAD segment at offset 0 [BZ #32763]

Message ID CAMe9rOqwA4HLLZxdy3WULZ0T5RHY7RKfii06DNaFLvpB=S68dA@mail.gmail.com (mailing list archive)
State Committed
Headers
Series [v2] static-pie: Skip the empty PT_LOAD segment at offset 0 [BZ #32763] |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

H.J. Lu March 5, 2025, 4:23 a.m. UTC
  On Wed, Mar 5, 2025 at 11:02 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> As shown in
>
> https://sourceware.org/bugzilla/show_bug.cgi?id=32761
>
> linker may generate more than one PT_LOAD segments at offset 0
>

This is intentional.  Here is the v2 patch:

As shown in

https://sourceware.org/bugzilla/show_bug.cgi?id=25237

linker may generate an empty PT_LOAD segments at offset 0:

Elf file type is EXEC (Executable file)
Entry point 0x4000e8
There are 3 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000000f0 0x00000000000000f0  R E    0x1000
  LOAD           0x0000000000000000 0x0000000000410000 0x0000000000410000
                 0x0000000000000000 0x0000000000b5dce8  RW     0x10000
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x10

 Section to Segment mapping:
  Segment Sections...
   00     .text
   01     .bss
   02

Skip the empty PT_LOAD segment at offset 0 to support such binaries.
This fixes BZ #32763.
  

Comments

Sam James March 5, 2025, 10:48 p.m. UTC | #1
"H.J. Lu" <hjl.tools@gmail.com> writes:

> On Wed, Mar 5, 2025 at 11:02 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> As shown in
>>
>> https://sourceware.org/bugzilla/show_bug.cgi?id=32761
>>
>> linker may generate more than one PT_LOAD segments at offset 0
>>
>
> This is intentional.  Here is the v2 patch:
>
> As shown in
>
> https://sourceware.org/bugzilla/show_bug.cgi?id=25237
>
> linker may generate an empty PT_LOAD segments at offset 0:
>
> Elf file type is EXEC (Executable file)
> Entry point 0x4000e8
> There are 3 program headers, starting at offset 64
>
> Program Headers:
>   Type           Offset             VirtAddr           PhysAddr
>                  FileSiz            MemSiz              Flags  Align
>   LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
>                  0x00000000000000f0 0x00000000000000f0  R E    0x1000
>   LOAD           0x0000000000000000 0x0000000000410000 0x0000000000410000
>                  0x0000000000000000 0x0000000000b5dce8  RW     0x10000
>   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000000 0x0000000000000000  RW     0x10
>
>  Section to Segment mapping:
>   Segment Sections...
>    00     .text
>    01     .bss
>    02
>
> Skip the empty PT_LOAD segment at offset 0 to support such binaries.
> This fixes BZ #32763.

Thanks. It looks good to me after reviewing the other linker bugs and
Alan's linker change.
  
H.J. Lu March 5, 2025, 11:03 p.m. UTC | #2
On Thu, Mar 6, 2025 at 6:48 AM Sam James <sam@gentoo.org> wrote:
>
> "H.J. Lu" <hjl.tools@gmail.com> writes:
>
> > On Wed, Mar 5, 2025 at 11:02 AM H.J. Lu <hjl.tools@gmail.com> wrote:
> >>
> >> As shown in
> >>
> >> https://sourceware.org/bugzilla/show_bug.cgi?id=32761
> >>
> >> linker may generate more than one PT_LOAD segments at offset 0
> >>
> >
> > This is intentional.  Here is the v2 patch:
> >
> > As shown in
> >
> > https://sourceware.org/bugzilla/show_bug.cgi?id=25237
> >
> > linker may generate an empty PT_LOAD segments at offset 0:
> >
> > Elf file type is EXEC (Executable file)
> > Entry point 0x4000e8
> > There are 3 program headers, starting at offset 64
> >
> > Program Headers:
> >   Type           Offset             VirtAddr           PhysAddr
> >                  FileSiz            MemSiz              Flags  Align
> >   LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
> >                  0x00000000000000f0 0x00000000000000f0  R E    0x1000
> >   LOAD           0x0000000000000000 0x0000000000410000 0x0000000000410000
> >                  0x0000000000000000 0x0000000000b5dce8  RW     0x10000
> >   GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
> >                  0x0000000000000000 0x0000000000000000  RW     0x10
> >
> >  Section to Segment mapping:
> >   Segment Sections...
> >    00     .text
> >    01     .bss
> >    02
> >
> > Skip the empty PT_LOAD segment at offset 0 to support such binaries.
> > This fixes BZ #32763.
>
> Thanks. It looks good to me after reviewing the other linker bugs and
> Alan's linker change.

Here is the v3 patch I am checking in.  The main addition is

@@ -2082,6 +2086,7 @@ $(objpfx)tst-array5-static-cmp.out: tst-array5-static.exp
\

 CFLAGS-tst-pie1.c += $(pie-ccflag)
 CFLAGS-tst-pie2.c += $(pie-ccflag)
+CFLAGS-tst-pie-bss.c += $(pie-ccflag)
 CFLAGS-tst-pie-address.c += $(pie-ccflag)

 $(objpfx)tst-piemod1.so: $(libsupport)

for glibc configured with --disable-default-pie
  
Joseph Myers March 6, 2025, 2:37 p.m. UTC | #3
The new test fails to build for MicroBlaze (which defines 
MAX_OFILE_ALIGNMENT to (32768*8) in GCC, so __attribute__ ((aligned 
(65536))) is unsupported).
  

Patch

From bc979ea99ae8e6192741057cd34348b3998cd9e3 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 5 Mar 2025 10:19:59 +0800
Subject: [PATCH v2] static-pie: Skip the empty PT_LOAD segment at offset 0 [BZ
 #32763]

As shown in

https://sourceware.org/bugzilla/show_bug.cgi?id=25237

linker may generate an empty PT_LOAD segments at offset 0:

Elf file type is EXEC (Executable file)
Entry point 0x4000e8
There are 3 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x00000000000000f0 0x00000000000000f0  R E    0x1000
  LOAD           0x0000000000000000 0x0000000000410000 0x0000000000410000
                 0x0000000000000000 0x0000000000b5dce8  RW     0x10000
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x10

 Section to Segment mapping:
  Segment Sections...
   00     .text
   01     .bss
   02

Skip the empty PT_LOAD segment at offset 0 to support such binaries.
This fixes BZ #32763.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 elf/Makefile              |  4 ++++
 elf/dl-reloc-static-pie.c |  3 ++-
 elf/tst-pie-bss-static.c  | 19 +++++++++++++++++++
 elf/tst-pie-bss.c         | 30 ++++++++++++++++++++++++++++++
 4 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 elf/tst-pie-bss-static.c
 create mode 100644 elf/tst-pie-bss.c

diff --git a/elf/Makefile b/elf/Makefile
index 41f01b5c89..4f33106a51 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -1137,12 +1137,14 @@  tests += \
   tst-dlopen-pie \
   tst-dlopen-self-pie \
   tst-dlopen-tlsmodid-pie \
+  tst-pie-bss \
   tst-pie1 \
   tst-pie2 \
   # tests
 tests-pie += \
   tst-dlopen-self-pie \
   tst-dlopen-tlsmodid-pie \
+  tst-pie-bss \
   tst-pie1 \
   tst-pie2 \
   # tests-pie
@@ -1157,9 +1159,11 @@  LDFLAGS-tst-pie-address += $(load-address-ldflag)=$(pde-load-address)
 ifeq (yes,$(enable-static-pie))
 tests += \
   tst-pie-address-static \
+  tst-pie-bss-static \
   # tests
 tests-static += \
   tst-pie-address-static \
+  tst-pie-bss-static \
   # tests-static
 LDFLAGS-tst-pie-address-static += \
   $(load-address-ldflag)=$(pde-load-address)
diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
index e34bf5f7ce..758bf9893e 100644
--- a/elf/dl-reloc-static-pie.c
+++ b/elf/dl-reloc-static-pie.c
@@ -51,7 +51,8 @@  _dl_relocate_static_pie (void)
     switch (ph->p_type)
       {
       case PT_LOAD:
-	if (ph->p_offset == 0)
+	/* Skip the empty PT_LOAD segment at offset 0.  */
+	if (ph->p_filesz != 0 && ph->p_offset == 0)
 	  file_p_vaddr = ph->p_vaddr;
 	break;
       case PT_DYNAMIC:
diff --git a/elf/tst-pie-bss-static.c b/elf/tst-pie-bss-static.c
new file mode 100644
index 0000000000..e9c1c8819a
--- /dev/null
+++ b/elf/tst-pie-bss-static.c
@@ -0,0 +1,19 @@ 
+/* Test static PIE with the empty PT_LOAD segment at offset 0.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include "tst-pie-bss.c"
diff --git a/elf/tst-pie-bss.c b/elf/tst-pie-bss.c
new file mode 100644
index 0000000000..e041eae4b1
--- /dev/null
+++ b/elf/tst-pie-bss.c
@@ -0,0 +1,30 @@ 
+/* Test PIE with the empty PT_LOAD segment at offset 0.
+   Copyright (C) 2025 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+
+char bss[0xb5dce8] __attribute__ ((aligned(65536)));
+
+static int
+do_test (void)
+{
+  printf ("Hello\n");
+  return 0;
+}
+
+#include <support/test-driver.c>
-- 
2.48.1