From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: libc-alpha@sourceware.org,
Siddhesh Poyarekar <siddhesh@sourceware.org>,
John Paul Adrian Glaubitz <glaubitz@physik.fu-berlin.de>
Subject: [PATCH] csu: Fix static when PIE is enabled but static PIE is not supported (BZ #29575)
Date: Wed, 5 Oct 2022 14:08:51 -0300 [thread overview]
Message-ID: <20221005170851.2350343-1-adhemerval.zanella@linaro.org> (raw)
On architectures where static PIE is not supported, both libc and
static binaries are built still with -fPIE and -DPIC. It leads to
wrong code for some architectures (for instance sparc).
This patch only enables -DPIC for .o files iff static-pie is also
supported. Since the same rule is used for both libc.a objects and
installed binaries, the -fno-pie is added if static-pie is not
supported (with a strategy similar to how stack protector is
disabled for some architectures).
Checked on x86_64-linux-gnu (with and without --disable-static-pie)
and I also checked on sparc64-linux-gnu to verify that static binaries
are not broken and both binaries and tests continue to be built
for PIE.
---
Makeconfig | 15 +++++++++++++++
Makerules | 7 +++++++
csu/Makefile | 4 ++++
stdio-common/Makefile | 4 ++--
4 files changed, 28 insertions(+), 2 deletions(-)
diff --git a/Makeconfig b/Makeconfig
index 842f49eb58..4b0d9003cd 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -404,6 +404,9 @@ else
# for PIE to support exception.
static-pie-ldflag = -Wl,-pie,--no-dynamic-linker,--eh-frame-hdr,-z,text
endif # have-static-pie
+else
+no-static-pie = $(no-pie-ccflag)
+no-pic-flag = -UPIC
endif # enable-static-pie
endif # build-pie-default
@@ -901,6 +904,18 @@ define elide-stack-protector
$(if $(filter $(@F),$(patsubst %,%$(1),$(2))), $(no-stack-protector))
endef
+# If static-pie is not supported, add the compiler option to avoid building it
+# for pie and to remove the PIC define. The first argument is the extension
+# (.o, .os, .oS) and the second is a list of routines that this path should be
+# applied to.
+define elide-pie
+$(if $(filter $(@F),$(patsubst %,%$(1),$(2))), $(no-static-pie))
+endef
+# Smae as elide-pie, but remove the PIC flag.
+define elide-pic-flag
+$(if $(filter $(@F),$(patsubst %,%$(1),$(2))), $(no-pic-flag))
+endef
+
# The program that makes Emacs-style TAGS files.
ETAGS := etags
diff --git a/Makerules b/Makerules
index 09c0cf8357..0fcaf565eb 100644
--- a/Makerules
+++ b/Makerules
@@ -1239,6 +1239,13 @@ tests += $(foreach t,$(tests-time64),$(t))
xtests += $(foreach t,$(xtests-time64),$(t))
endif
+# Disable static-pie for libc.a if target does not support it.
+CFLAGS-.o += $(call elide-pie,.o,$(routines))
+CFLAGS-.op += $(call elide-pie,.op,$(routines))
+# And also remove the PIC flag.
+CPPFLAGS-.o += $(call elide-pic-flag,.o,$(routines))
+CPPFLAGS-.op += $(call elide-pic-flag,.op,$(routines))
+
# The only difference between MODULE_NAME=testsuite and MODULE_NAME=nonlib is
# that almost all internal declarations from config.h, libc-symbols.h, and
# include/*.h are not available to 'testsuite' code, but are to 'nonlib' code.
diff --git a/csu/Makefile b/csu/Makefile
index f71a5eb6c6..62a819608f 100644
--- a/csu/Makefile
+++ b/csu/Makefile
@@ -66,6 +66,10 @@ CFLAGS-.os += $(call elide-stack-protector,.os,$(filter-out \
$(ssp-safe.os), \
$(routines) $(extra-no-ssp)))
+# Remove PIC flag for init objects.
+CPPFLAGS-.o += $(call elide-pic-flag,.o,$(basename $(extra-objs)))
+CPPFLAGS-.op += $(call elide-pic-flag,.op,$(basename $(extra-objs)))
+
ifeq (yes,$(build-shared))
extra-objs += S$(start-installed-name) gmon-start.os
ifneq ($(start-installed-name),$(static-start-installed-name))
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 9c98c02884..6eee9ba73c 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -265,7 +265,7 @@ $(objpfx)errlist-data-aux-shared.S: errlist-data-gen.c
$(objpfx)errlist-data-aux.S: errlist-data-gen.c
$(make-target-directory)
- $(compile-command.c) $(pie-default) $(no-stack-protector) -S
+ $(compile-command.c) $(pic-cppflags) $(pic-ccflag) $(no-stack-protector) -S
ifndef no_deps
-include $(objpfx)errlist-data-aux.S.d $(objpfx)errlist-data-aux-shared.S.d
@@ -280,7 +280,7 @@ $(objpfx)siglist-aux-shared.S: siglist-gen.c
$(objpfx)siglist-aux.S: siglist-gen.c
$(make-target-directory)
- $(compile-command.c) $(pie-default) $(no-stack-protector) -S
+ $(compile-command.c) $(pic-cppflags) $(pic-ccflag) $(no-stack-protector) -S
ifndef no_deps
-include $(objpfx)siglist-aux.S.d $(objpfx)siglist-aux-shared.S.d
--
2.34.1
next reply other threads:[~2022-10-05 17:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-05 17:08 Adhemerval Zanella [this message]
2022-10-05 19:04 ` Siddhesh Poyarekar
2022-10-06 18:09 ` Adhemerval Zanella Netto
2022-10-06 18:21 ` Siddhesh Poyarekar
2022-10-06 19:41 ` Adhemerval Zanella Netto
2022-11-06 15:14 ` John Paul Adrian Glaubitz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221005170851.2350343-1-adhemerval.zanella@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=glaubitz@physik.fu-berlin.de \
--cc=libc-alpha@sourceware.org \
--cc=siddhesh@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).