From patchwork Wed Oct 5 17:08:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 58404 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A8B2A3858D3C for ; Wed, 5 Oct 2022 17:09:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A8B2A3858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1664989758; bh=qL8ZN//YLfMtDL9dkyAw7oS8l7d48CIpA+WUgrHtUgU=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vYyxPxD0CkMeS+WaUDAI890A9Ns9EEx3tYXqk4gWZH2qng3dRm6z5Q3EER2I0f1rO Z5iKOLCrdDTCL/zlVMK0/6I/3tkPZAkA+/mj2sID61h2N1bAoN7KSm4TQSCd3VTQ7V sVzaMGib4YEzLAd1Xyg2IRJFem2F77r1ajdIB2QE= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc31.google.com (mail-oo1-xc31.google.com [IPv6:2607:f8b0:4864:20::c31]) by sourceware.org (Postfix) with ESMTPS id BE08B3858CDA for ; Wed, 5 Oct 2022 17:08:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE08B3858CDA Received: by mail-oo1-xc31.google.com with SMTP id c17-20020a4aa4d1000000b0047653e7c5f3so11229732oom.1 for ; Wed, 05 Oct 2022 10:08:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date; bh=qL8ZN//YLfMtDL9dkyAw7oS8l7d48CIpA+WUgrHtUgU=; b=IZxjIW5lYQSctsoWbi3oJA3n2Sy+MA51p1K6D5/QJXapmebkOMvHYUHt3UWyG/AkbJ 078QWVOlUL6kZmf9LzqRb75sMUCrmObvSRqoMBIcLC9BK5g0ySoh21Z/F5axEWiHVwK3 blXr8fXJmrqfKLJIeXqI5zBLYjt6ioe5q5rdQpZg8LSy3HjW6JKhTqeWUgjH8kIwhaer 4X3ayfl0itC4ESK3F7GDV3t1GKVQxax6DC4pTq9+tvzCeB1iRaS49NjyPmROBW0vQEyX MdYB6+ivMBIXyTr1njQI9KL6+AQy+OAA8a48Wxsd6pHLo6jNQgRsxiDjVQYGqDaNN/R7 Q/lg== X-Gm-Message-State: ACrzQf2VP64bORHg4a7Zd04cuzrU4BUtJJesOSPPAw39mY9R7oNnvAP7 hQ5Glx5rbQ1SSkw2EGI4sLBdBYFQHQbhC+TM X-Google-Smtp-Source: AMsMyM4Q6QAkuDAP4LbX1f6DdLhQzW22yu0CGrGvadlSpiZIjF/yrTHVsaWk+gvZsY1G6lBByw181g== X-Received: by 2002:a9d:6a0a:0:b0:655:fd57:a6aa with SMTP id g10-20020a9d6a0a000000b00655fd57a6aamr189812otn.291.1664989734791; Wed, 05 Oct 2022 10:08:54 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:b87d:99c2:20cb:d5b8:a210]) by smtp.gmail.com with ESMTPSA id m6-20020a056870058600b00132e63ee5e6sm727124oap.54.2022.10.05.10.08.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 10:08:54 -0700 (PDT) To: libc-alpha@sourceware.org, Siddhesh Poyarekar , John Paul Adrian Glaubitz 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 Message-Id: <20221005170851.2350343-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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