diff mbox series

[v2] elf: Properly align all PT_LOAD segments [BZ #28676]

Message ID 20211217173949.4049-1-hjl.tools@gmail.com
State Superseded
Headers show
Series [v2] elf: Properly align all PT_LOAD segments [BZ #28676] | expand

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

H.J. Lu Dec. 17, 2021, 5:39 p.m. UTC
Changes in v2:

1. Enable tst-p_align1 test if linker sets p_align according to
sh_addralign.
2. Use the x86-64 tst-p_alignmod1.so binary only as a fallback.

----
Linker may set p_align of a PT_LOAD segment larger than p_align of the
first PT_LOAD segment to satisfy a section alignment:

Elf file type is DYN (Shared object file)
Entry point 0x0
There are 10 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000834 0x0000000000000834  R E    0x1000
  LOAD           0x0000000000000e00 0x0000000000001e00 0x0000000000001e00
                 0x0000000000000230 0x0000000000000230  RW     0x1000
  LOAD           0x0000000000400000 0x0000000000400000 0x0000000000400000
                 0x0000000000000004 0x0000000000000008  RW     0x400000
...

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.property .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame
   01     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt
   02     .data .bss

We should align the first PT_LOAD segment to the maximum p_align of all
PT_LOAD segments, similar to the kernel commit:

commit ce81bb256a224259ab686742a6284930cbe4f1fa
Author: Chris Kennelly <ckennelly@google.com>
Date:   Thu Oct 15 20:12:32 2020 -0700

    fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

This patch includes an x86-64 binary of tst-p_alignmod1.so to verify that
shared libraries with such PT_LOAD segments are mapped properly, in case
that linker doesn't set p_align according to sh_addralign.
---
 configure                               |  39 ++++++++++++++++++++++++
 configure.ac                            |  29 ++++++++++++++++++
 elf/Makefile                            |  12 ++++++++
 elf/dl-load.c                           |   9 +++++-
 elf/tst-p_align1.c                      |  27 ++++++++++++++++
 elf/tst-p_alignmod1.c                   |   1 +
 sysdeps/x86_64/64/Makefile              |  12 ++++++++
 sysdeps/x86_64/64/tst-p_alignmod1.so.xz | Bin 0 -> 7476 bytes
 8 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 elf/tst-p_align1.c
 create mode 100644 elf/tst-p_alignmod1.c
 create mode 100644 sysdeps/x86_64/64/Makefile
 create mode 100755 sysdeps/x86_64/64/tst-p_alignmod1.so.xz

GIT binary patch
literal 7476
zcmcK9b6Xvb!+`OVwQR3CnaiDO*~W6q_=a_A)iRfD+pcAwbh7PEw$?J9-<x<|!Ts;&
zy6!gtFFgYQ0P%3CNfsVJ14je^09-i_iatL4P&bVMVhJ+8ZABwW*CJuT+^RPhS2iR9
zC=GX-@MYU5NA5q69QBanCtOl;oSC}%b0<{(vZK4y4EzcPH{oIgP#e74Bi6NqXl@vu
zp(oXpcT)!81$<nDsc8-kchh|8D1J7#0gXEu`p@=jwy?sMG%~>R)x4!N6`jFH%WV59
zNcijB90~aHiXx37Zdj^YmL2%)mmEixo`hph&FR9b0Ah+H!*!NhKbMOz5_oANN=L?C
zkr@K<8%WnMtsTeI>}3nXmd6&QEHU88g}{PH@e+W2Ai&F>oDz2X^;|M+ydU$?Z11ge
z9JW9g^))9C(ykQczc4``jWK#a=-W4@5~9M|IelkhbQ`M@W|(7Ffr$;K2TM-(Q@X44
zG3hkjfD(B`8@^vq-e@mIcHPF3(?JLU(ovYCz*?EkESwZCxrZ&L)jwoe0ytVuihW>g
zjI;Y7#uUH4z69=-!U4h^<)UV%6?p6ueC=4~UaWXl>JpAp+FAe)b<Vx`uZ^J|e?cxk
zAp6(WJ$t;aym~@;v+l~f533duhkE?d&l)h&p2{zs-GgQA@LJ3&A8k#lYuop3oXYqV
z)^AgRt1uFrbeWnav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#fGyCK^>*Df=B1Rp
zqQT~YGD4Q5NxN&f;3CzsWek0y6UFmwnvhHSvs#s9Xxr~y3W?HBLZGxBhND$@xo#W~
z@>tj)MpBb|E?VLgVFcT&deIkYUE^ty%)1?@elJmodd7=$50PBO!hr*dCM^f_#zgu_
z9*Z}Tt)l%?LVu^F5!0eP9Ake}{2~*Op*RGSimXo@p#`G6pth#4ioGX(v1PGP|DTD-
zdoT4UW;E!q<9}^$?xccF8J{Uz$E=MY?8&7L61cm9(8^P($fVuBkJ3PcYV-^DlWz=%
z&F*5R^s~`s`56wMX`aOWrt<r3A^sB%5;CfKW)X^*g(Nn2t+sLq4x-%y@h3S_zT-=g
zSYjR<wafXILv`U;zgp<#h-76t0(rYxwEoZ{jtkL;@+No3ZLJ%U=>|B34adeIIM@Cw
zi=%*(O!`?nPlJSmzi>K)JxwmrWT<qS=J-AFES*#PqRyhK5#T*#BrbiFVMm6@sy?DL
ztvS^aSW73?E#T=1KLk6x?C|2vh@_gzkXt~MaVnfiFfPgB;#!kYiLanEgbO&oMM)Ph
z-L|m7FOe|v4_LW>^IAJ>1F_~20Y~WnIS2r=eLJI859K6U{GVZmFqn+zsHr*^rv?hG
zofR83jLeXe(6J3_k2V>Ph8r9q>ud)$%?1GY+>KeO=p`osaPgr0`v5qE4aAU!L7Nx&
M|GiNFqQ1fZ0T@Mr`Tzg`

literal 0
HcmV?d00001
diff mbox series

Patch

diff --git a/configure b/configure
index 954bae803e..fa1f500f8c 100755
--- a/configure
+++ b/configure
@@ -6282,6 +6282,45 @@  fi
 $as_echo "$libc_cv_has_glob_dat" >&6; }
 
 
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking whether the linker sets p_align following sh_addralign" >&5
+$as_echo_n "checking whether the linker sets p_align following sh_addralign... " >&6; }
+if ${libc_cv_p_align_follow_sh_addralign+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat > conftest.c <<EOF
+void
+bar (void)
+{
+}
+int foo __attribute__ ((aligned (0x4000))) = 1;
+EOF
+if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+			-fPIC -shared -o conftest.so conftest.c
+			-Wl,-z,max-page-size=0x1000
+			-nostdlib -nostartfiles $no_ssp
+			1>&5'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+then
+  if $READELF -lW conftest.so | egrep 'LOAD.*0x1000$' > /dev/null &&
+     $READELF -lW conftest.so | egrep 'LOAD.*0x4000$' > /dev/null; then
+    libc_cv_p_align_follow_sh_addralign=yes
+  else
+    libc_cv_p_align_follow_sh_addralign=no
+  fi
+else
+  libc_cv_p_align_follow_sh_addralign=no
+fi
+rm -f conftest*
+fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $libc_cv_p_align_follow_sh_addralign" >&5
+$as_echo "$libc_cv_p_align_follow_sh_addralign" >&6; }
+config_vars="$config_vars
+have-p_align-follow-sh_addralign = $libc_cv_p_align_follow_sh_addralign"
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for -fno-toplevel-reorder -fno-section-anchors" >&5
 $as_echo_n "checking for -fno-toplevel-reorder -fno-section-anchors... " >&6; }
 if ${libc_cv_fno_toplevel_reorder+:} false; then :
diff --git a/configure.ac b/configure.ac
index 277d3527d2..3e500eec01 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1472,6 +1472,35 @@  fi
 rm -f conftest*])
 AC_SUBST(libc_cv_has_glob_dat)
 
+AC_CACHE_CHECK(whether the linker sets p_align following sh_addralign,
+	       libc_cv_p_align_follow_sh_addralign, [dnl
+cat > conftest.c <<EOF
+void
+bar (void)
+{
+}
+int foo __attribute__ ((aligned (0x4000))) = 1;
+EOF
+if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS
+			-fPIC -shared -o conftest.so conftest.c
+			-Wl,-z,max-page-size=0x1000
+			-nostdlib -nostartfiles $no_ssp
+			1>&AS_MESSAGE_LOG_FD])
+then
+dnl Look for 0x1000 and 0x4000 p_align.
+  if $READELF -lW conftest.so | egrep 'LOAD.*0x1000$' > /dev/null &&
+     $READELF -lW conftest.so | egrep 'LOAD.*0x4000$' > /dev/null; then
+    libc_cv_p_align_follow_sh_addralign=yes
+  else
+    libc_cv_p_align_follow_sh_addralign=no
+  fi
+else
+  libc_cv_p_align_follow_sh_addralign=no
+fi
+rm -f conftest*])
+LIBC_CONFIG_VAR([have-p_align-follow-sh_addralign],
+		[$libc_cv_p_align_follow_sh_addralign])
+
 AC_CACHE_CHECK(for -fno-toplevel-reorder -fno-section-anchors, libc_cv_fno_toplevel_reorder, [dnl
 cat > conftest.c <<EOF
 int foo;
diff --git a/elf/Makefile b/elf/Makefile
index fe42caeb0e..f55ece919c 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -378,6 +378,15 @@  modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
 		tst-auditmod18 \
 		tst-audit18mod \
 
+ifeq (yes,$(have-p_align-follow-sh_addralign))
+tests += \
+  tst-p_align1 \
+
+modules-names += \
+  tst-p_alignmod1 \
+
+endif
+
 # Most modules build with _ISOMAC defined, but those filtered out
 # depend on internal headers.
 modules-names-tests = $(filter-out ifuncmod% tst-tlsmod%,\
@@ -1995,3 +2004,6 @@  $(objpfx)tst-ro-dynamic-mod.so: $(objpfx)tst-ro-dynamic-mod.os \
 		$(objpfx)tst-ro-dynamic-mod.os
 
 $(objpfx)tst-rtld-run-static.out: $(objpfx)/ldconfig
+
+$(objpfx)tst-p_align1: $(objpfx)tst-p_alignmod1.so
+$(objpfx)tst-p_alignmod1.so: $(libsupport)
diff --git a/elf/dl-load.c b/elf/dl-load.c
index 721593135e..e61515771a 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1101,6 +1101,7 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
     size_t nloadcmds = 0;
     bool has_holes = false;
     bool empty_dynamic = false;
+    ElfW(Addr) p_align_max = 0;
 
     /* The struct is initialized to zero so this is not necessary:
     l->l_ld = 0;
@@ -1151,7 +1152,9 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
 	  c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
 	  c->dataend = ph->p_vaddr + ph->p_filesz;
 	  c->allocend = ph->p_vaddr + ph->p_memsz;
-	  c->mapalign = ph->p_align;
+	  /* Remember the maximum p_align.  */
+	  if (ph->p_align > p_align_max)
+	    p_align_max = ph->p_align;
 	  c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
 
 	  /* Determine whether there is a gap between the last segment
@@ -1226,6 +1229,10 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
 	goto lose;
       }
 
+    /* Align all PT_LOAD segments to the maximum p_align.  */
+    for (size_t i = 0; i < nloadcmds; i++)
+      loadcmds[i].mapalign = p_align_max;
+
     /* dlopen of an executable is not valid because it is not possible
        to perform proper relocations, handle static TLS, or run the
        ELF constructors.  For PIE, the check needs the dynamic
diff --git a/elf/tst-p_align1.c b/elf/tst-p_align1.c
new file mode 100644
index 0000000000..cab9793220
--- /dev/null
+++ b/elf/tst-p_align1.c
@@ -0,0 +1,27 @@ 
+/* Check different alignments of PT_LOAD segments in a shared library.
+   Copyright (C) 2021 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/>.  */
+
+extern int do_load_test (void);
+
+static int
+do_test (void)
+{
+  return do_load_test ();
+}
+
+#include <support/test-driver.c>
diff --git a/elf/tst-p_alignmod1.c b/elf/tst-p_alignmod1.c
new file mode 100644
index 0000000000..2a98f0482f
--- /dev/null
+++ b/elf/tst-p_alignmod1.c
@@ -0,0 +1 @@ 
+#include "tst-alignmod3.c"
diff --git a/sysdeps/x86_64/64/Makefile b/sysdeps/x86_64/64/Makefile
new file mode 100644
index 0000000000..c60c160604
--- /dev/null
+++ b/sysdeps/x86_64/64/Makefile
@@ -0,0 +1,12 @@ 
+ifeq ($(subdir),elf)
+ifneq (yes,$(have-p_align-follow-sh_addralign))
+# Test alignments for all PT_LOAD segments with a tst-p_alignmod1.so
+# binary if linker doesn't set p_align according to sh_addralign.
+tests += \
+  tst-p_align1 \
+
+$(objpfx)tst-p_alignmod1.so: $(..)sysdeps/x86_64/64/tst-p_alignmod1.so.xz
+	cp -f $< $(objpfx)
+	unxz -f $(objpfx)tst-p_alignmod1.so.xz
+endif
+endif
diff --git a/sysdeps/x86_64/64/tst-p_alignmod1.so.xz b/sysdeps/x86_64/64/tst-p_alignmod1.so.xz
new file mode 100755
index 0000000000000000000000000000000000000000..0b0d4d953379649e2f3624b3305b3fc5c250eee2