commit:     02aa6328a720c86d0157c4582f7e5bac72ae9296
Author:     James Le Cuirot <chewi <AT> gentoo <DOT> org>
AuthorDate: Sat Jun 11 21:11:12 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Jun 25 21:39:28 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=02aa6328

sys-libs/glibc: Add stack-realign flag for compat with old 32-bit x86 binaries

Older 32-bit x86 binaries aligned the stack to 4 bytes, whereas modern
binaries align to 16 bytes. These older binaries sometimes segfault when
newer libraries use SSE instructions. This is becoming increasingly
common. Applying the -mstackrealign flag to the 32-bit build works
around the issue but at a performance cost. Other popular
distributions always apply this.

[sam: There's no good choices here. As Ionen pointed out (I'd missed
any reports of this), this ends up getting worse with GCC 12's
default-on vectorisation at -O2. Let's make it optional for now for
32-bit/x86 (irrelevant for other arches, it's specific to x86 ABI).

ncurses is going to need similar treatment. If we end up having
to do this for far more packages, we may revisit and e.g.
just append-flags in ebuilds for right ABI and tell users
to set -mno-stackrealign, or similar.

Another option would be to set this globally by default (again,
this is only ever for x86), but it'd possibly be a big performance
hit (and bad enough doing it in glibc, but it's unavoidable).

The only saving grace here is that there aren't _that_ many
libraries with such longevity & ABI stability from back then
that older applications are using.]

Bug: https://bugs.gentoo.org/616402
Bug: https://github.com/taviso/123elf/issues/12
Signed-off-by: James Le Cuirot <chewi <AT> gentoo.org>
Closes: https://github.com/gentoo/gentoo/pull/25858
Signed-off-by: Sam James <sam <AT> gentoo.org>

 sys-libs/glibc/glibc-2.35-r7.ebuild | 31 ++++++++++++++++++-------------
 sys-libs/glibc/glibc-9999.ebuild    | 31 ++++++++++++++++++-------------
 sys-libs/glibc/metadata.xml         |  1 +
 3 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/sys-libs/glibc/glibc-2.35-r7.ebuild 
b/sys-libs/glibc/glibc-2.35-r7.ebuild
index a9b4a0d16717..3e8358532526 100644
--- a/sys-libs/glibc/glibc-2.35-r7.ebuild
+++ b/sys-libs/glibc/glibc-2.35-r7.ebuild
@@ -44,7 +44,7 @@ SRC_URI+=" 
https://gitweb.gentoo.org/proj/locale-gen.git/snapshot/locale-gen-${L
 SRC_URI+=" multilib-bootstrap? ( 
https://dev.gentoo.org/~dilfridge/distfiles/gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz
 )"
 SRC_URI+=" systemd? ( 
https://gitweb.gentoo.org/proj/toolchain/glibc-systemd.git/snapshot/glibc-systemd-${GLIBC_SYSTEMD_VER}.tar.gz
 )"
 
-IUSE="audit caps cet +clone3 compile-locales +crypt custom-cflags doc 
experimental-loong gd headers-only +multiarch multilib multilib-bootstrap nscd 
profile selinux +ssp +static-libs suid systemd systemtap test vanilla"
+IUSE="audit caps cet +clone3 compile-locales +crypt custom-cflags doc 
experimental-loong gd headers-only +multiarch multilib multilib-bootstrap nscd 
profile selinux +ssp stack-realign +static-libs suid systemd systemtap test 
vanilla"
 
 # Minimum kernel version that glibc requires
 MIN_KERN_VER="3.2.0"
@@ -305,22 +305,27 @@ setup_target_flags() {
                                export CFLAGS="-march=${t} ${CFLAGS}"
                                einfo "Auto adding -march=${t} to CFLAGS 
#185404"
                        fi
+                       # For compatibility with older binaries at slight 
performance cost.
+                       use stack-realign && export CFLAGS+=" -mstackrealign"
                ;;
                amd64)
                        # -march needed for #185404 #199334
                        # TODO: See cross-compile issues listed above for x86.
-                       [[ ${ABI} == x86 ]] &&
-                       if ! do_compile_test "${CFLAGS_x86}" 'void f(int i, 
void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n'; 
then
-                               local t=${CTARGET_OPT:-${CTARGET}}
-                               t=${t%%-*}
-                               # Normally the target is x86_64-xxx, so turn 
that into the -march that
-                               # gcc actually accepts. #528708
-                               [[ ${t} == "x86_64" ]] && t="x86-64"
-                               filter-flags '-march=*'
-                               # ugly, ugly, ugly.  ugly.
-                               CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} filter-flags 
'-march=*'; echo "${CFLAGS}")
-                               export CFLAGS_x86="${CFLAGS_x86} -march=${t}"
-                               einfo "Auto adding -march=${t} to CFLAGS_x86 
#185404 (ABI=${ABI})"
+                       if [[ ${ABI} == x86 ]]; then
+                               if ! do_compile_test "${CFLAGS_x86}" 'void 
f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint 
main(){return 0;}\n'; then
+                                       local t=${CTARGET_OPT:-${CTARGET}}
+                                       t=${t%%-*}
+                                       # Normally the target is x86_64-xxx, so 
turn that into the -march that
+                                       # gcc actually accepts. #528708
+                                       [[ ${t} == "x86_64" ]] && t="x86-64"
+                                       filter-flags '-march=*'
+                                       # ugly, ugly, ugly.  ugly.
+                                       CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} 
filter-flags '-march=*'; echo "${CFLAGS}")
+                                       export CFLAGS_x86="${CFLAGS_x86} 
-march=${t}"
+                                       einfo "Auto adding -march=${t} to 
CFLAGS_x86 #185404 (ABI=${ABI})"
+                               fi
+                               # For compatibility with older binaries at 
slight performance cost.
+                               use stack-realign && export CFLAGS_x86+=" 
-mstackrealign"
                        fi
                ;;
                mips)

diff --git a/sys-libs/glibc/glibc-9999.ebuild b/sys-libs/glibc/glibc-9999.ebuild
index 2a030e2f9928..b35b2febd192 100644
--- a/sys-libs/glibc/glibc-9999.ebuild
+++ b/sys-libs/glibc/glibc-9999.ebuild
@@ -44,7 +44,7 @@ SRC_URI+=" 
https://gitweb.gentoo.org/proj/locale-gen.git/snapshot/locale-gen-${L
 SRC_URI+=" multilib-bootstrap? ( 
https://dev.gentoo.org/~dilfridge/distfiles/gcc-multilib-bootstrap-${GCC_BOOTSTRAP_VER}.tar.xz
 )"
 SRC_URI+=" systemd? ( 
https://gitweb.gentoo.org/proj/toolchain/glibc-systemd.git/snapshot/glibc-systemd-${GLIBC_SYSTEMD_VER}.tar.gz
 )"
 
-IUSE="audit caps cet compile-locales +crypt custom-cflags doc gd headers-only 
+multiarch multilib multilib-bootstrap nscd profile selinux +ssp +static-libs 
suid systemd systemtap test vanilla"
+IUSE="audit caps cet compile-locales +crypt custom-cflags doc gd headers-only 
+multiarch multilib multilib-bootstrap nscd profile selinux +ssp stack-realign 
+static-libs suid systemd systemtap test vanilla"
 
 # Minimum kernel version that glibc requires
 MIN_KERN_VER="3.2.0"
@@ -305,22 +305,27 @@ setup_target_flags() {
                                export CFLAGS="-march=${t} ${CFLAGS}"
                                einfo "Auto adding -march=${t} to CFLAGS 
#185404"
                        fi
+                       # For compatibility with older binaries at slight 
performance cost.
+                       use stack-realign && export CFLAGS+=" -mstackrealign"
                ;;
                amd64)
                        # -march needed for #185404 #199334
                        # TODO: See cross-compile issues listed above for x86.
-                       [[ ${ABI} == x86 ]] &&
-                       if ! do_compile_test "${CFLAGS_x86}" 'void f(int i, 
void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint main(){return 0;}\n'; 
then
-                               local t=${CTARGET_OPT:-${CTARGET}}
-                               t=${t%%-*}
-                               # Normally the target is x86_64-xxx, so turn 
that into the -march that
-                               # gcc actually accepts. #528708
-                               [[ ${t} == "x86_64" ]] && t="x86-64"
-                               filter-flags '-march=*'
-                               # ugly, ugly, ugly.  ugly.
-                               CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} filter-flags 
'-march=*'; echo "${CFLAGS}")
-                               export CFLAGS_x86="${CFLAGS_x86} -march=${t}"
-                               einfo "Auto adding -march=${t} to CFLAGS_x86 
#185404 (ABI=${ABI})"
+                       if [[ ${ABI} == x86 ]]; then
+                               if ! do_compile_test "${CFLAGS_x86}" 'void 
f(int i, void *p) {if (__sync_fetch_and_add(&i, 1)) f(i, p);}\nint 
main(){return 0;}\n'; then
+                                       local t=${CTARGET_OPT:-${CTARGET}}
+                                       t=${t%%-*}
+                                       # Normally the target is x86_64-xxx, so 
turn that into the -march that
+                                       # gcc actually accepts. #528708
+                                       [[ ${t} == "x86_64" ]] && t="x86-64"
+                                       filter-flags '-march=*'
+                                       # ugly, ugly, ugly.  ugly.
+                                       CFLAGS_x86=$(CFLAGS=${CFLAGS_x86} 
filter-flags '-march=*'; echo "${CFLAGS}")
+                                       export CFLAGS_x86="${CFLAGS_x86} 
-march=${t}"
+                                       einfo "Auto adding -march=${t} to 
CFLAGS_x86 #185404 (ABI=${ABI})"
+                               fi
+                               # For compatibility with older binaries at 
slight performance cost.
+                               use stack-realign && export CFLAGS_x86+=" 
-mstackrealign"
                        fi
                ;;
                mips)

diff --git a/sys-libs/glibc/metadata.xml b/sys-libs/glibc/metadata.xml
index e00a008ac5c3..bcaf604fa542 100644
--- a/sys-libs/glibc/metadata.xml
+++ b/sys-libs/glibc/metadata.xml
@@ -17,6 +17,7 @@
  <flag name="multilib-bootstrap">Provide prebuilt libgcc.a and crt files if 
missing. Only needed for ABI switch.</flag>
  <flag name="nscd">Build, and enable support for, the Name Service Cache 
Daemon</flag>
  <flag name="ssp">protect stack of glibc internals</flag>
+ <flag name="stack-realign">Realign the stack in the 32-bit build for 
compatibility with older binaries at slight performance cost</flag>
  <flag name="static-pie">Enable static PIE support (runtime files for 
-static-pie gcc option).</flag>
  <flag name="suid">Make internal pt_chown helper setuid -- not needed if using 
Linux and have /dev/pts mounted with gid=5</flag>
  <flag name="systemtap">enable systemtap static probe points</flag>

Reply via email to