From patchwork Fri May 13 20:46:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 53980 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 D5DBC395A447 for ; Fri, 13 May 2022 20:47:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5DBC395A447 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1652474836; bh=122h0i5GXpDvQR42RXdC4b/JGLJN7TKvrqoesx/9Wlw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=GS0g/D+fENzVLzgqSmClmXFiQbaAz4wlu5uzt4LNe8qDadDPH3zo3TeTvOU9vZAQx ROeY/KUVD+Ged4tIYdU1pUG08JU4TNKBBCfc9E9m/zPC7PnrN1kTGVJRgca0eEKJgT joDR+QcpSDpDUlYVeXoTiMp+XXEthIczpt/KwnMU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id 06158395A43A for ; Fri, 13 May 2022 20:46:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 06158395A43A Received: by mail-pl1-x632.google.com with SMTP id c9so9056106plh.2 for ; Fri, 13 May 2022 13:46:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=122h0i5GXpDvQR42RXdC4b/JGLJN7TKvrqoesx/9Wlw=; b=DL5yb94ycuRiJ5VZtsAwonXkpj1fXvFS6u5Hqs3Fm5OlIgVbk4+xMcYkjIac8L2rcw jqsHq/3+46KTvyuTjf1w9AEekidqp1GTjHNbny1eM7DdrgenLDi0vkPFgXiv1j3O3/X0 gu3nQ0bD1EduQAGywZZIDCllHXQSk9EzD5ksft/RDjiYnH6DMBBpX1CzBYgq+TnA3BcX W99MupZLG6G7FJ+N0L9d9jqtZjPDW4B55FhCZph1SeiXb1+xnPhGp0be1ynzhnVlGJeh Xmj8BfNxvZFUv9r6F5vojoahnH1gLUM1SJ6VWQV4smZOr1sTqXYgKG/+6Ne9BnqJK2RS W/9Q== X-Gm-Message-State: AOAM533+joZHGpv/jryNIgTkJ0dGBeoJ9SIUms5A14C34Q2PwgvdyrOQ PVE7FpR5iNj5s1oKZl/1NrouzqFS47k= X-Google-Smtp-Source: ABdhPJwr/eJ3lbQd9cKl007J7aKf+mHhsmPafyGOCt6G0bS7hV1LVo/YuIksP4K10ejXc5/cYvJEYA== X-Received: by 2002:a17:902:e945:b0:15f:22cd:c6cc with SMTP id b5-20020a170902e94500b0015f22cdc6ccmr6625730pll.162.1652474784751; Fri, 13 May 2022 13:46:24 -0700 (PDT) Received: from gnu-tgl-3.localdomain ([172.58.88.122]) by smtp.gmail.com with ESMTPSA id ik18-20020a170902ab1200b0015e8d4eb1e1sm2281734plb.43.2022.05.13.13.46.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 13:46:24 -0700 (PDT) Received: from gnu-tgl-3.. (localhost [IPv6:::1]) by gnu-tgl-3.localdomain (Postfix) with ESMTP id 366CAC000E for ; Fri, 13 May 2022 13:46:23 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] Enable DT_RELR automatically Date: Fri, 13 May 2022 13:46:23 -0700 Message-Id: <20220513204623.3695073-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3028.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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: "H.J. Lu via Libc-alpha" From: "H.J. Lu" Reply-To: "H.J. Lu" Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Enable DT_RELR in glibc shared libraries and position independent executables (PIE) automatically if linker supports -z pack-relative-relocs. Reviewed-by: Florian Weimer --- Makeconfig | 12 ++++++++++++ Makerules | 2 ++ elf/Makefile | 5 ++++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Makeconfig b/Makeconfig index 760f14e92f..c87d644044 100644 --- a/Makeconfig +++ b/Makeconfig @@ -362,6 +362,15 @@ relro-LDFLAGS = -Wl,-z,relro LDFLAGS.so += $(relro-LDFLAGS) LDFLAGS-rtld += $(relro-LDFLAGS) +# Linker options to enable and disable DT_RELR. +ifeq ($(have-dt-relr),yes) +dt-relr-ldflag = -Wl,-z,pack-relative-relocs +no-dt-relr-ldflag = -Wl,-z,nopack-relative-relocs +else +dt-relr-ldflag = +no-dt-relr-ldflag = +endif + ifeq (no,$(build-pie-default)) pie-default = $(no-pie-ccflag) else # build-pie-default @@ -370,6 +379,7 @@ pic-default = -DPIC pie-default = $(pie-ccflag) ifeq (yes,$(enable-static-pie)) +static-pie-dt-relr-ldflag = $(dt-relr-ldflag) ifeq (yes,$(have-static-pie)) static-pie-ldflag = -static-pie else @@ -404,6 +414,7 @@ link-extra-libs-tests = $(libsupport) # Command for linking PIE programs with the C library. ifndef +link-pie +link-pie-before-inputs = $(if $($(@F)-no-pie),$(no-pie-ldflag),-pie) \ + $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \ -Wl,-O1 -nostdlib -nostartfiles \ $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \ $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \ @@ -436,6 +447,7 @@ endif ifndef +link-static +link-static-before-inputs = -nostdlib -nostartfiles -static \ $(if $($(@F)-no-pie),$(no-pie-ldflag),$(static-pie-ldflag)) \ + $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(static-pie-dt-relr-ldflag)) \ $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \ $(firstword $(CRT-$(@F)) $(csu-objpfx)$(real-static-start-installed-name)) \ $(+preinit) $(+prectorT) diff --git a/Makerules b/Makerules index 354528b8c7..dfe89e9e39 100644 --- a/Makerules +++ b/Makerules @@ -536,6 +536,7 @@ lib%.so: lib%_pic.a $(+preinit) $(+postinit) $(link-libc-deps) define build-shlib-helper $(LINK.o) -shared -static-libgcc -Wl,-O1 $(sysdep-LDFLAGS) \ $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) $(rtld-LDFLAGS) \ + $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \ $(extra-B-$(@F:lib%.so=%).so) -B$(csu-objpfx) \ $(extra-B-$(@F:lib%.so=%).so) $(load-map-file) \ -Wl,-soname=lib$(libprefix)$(@F:lib%.so=%).so$($(@F)-version) \ @@ -588,6 +589,7 @@ endef define build-module-helper $(LINK.o) -shared -static-libgcc $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \ $(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) \ + $(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \ -B$(csu-objpfx) $(load-map-file) \ $(LDFLAGS.so) $(LDFLAGS-$(@F:%.so=%).so) \ $(link-test-modules-rpath-link) \ diff --git a/elf/Makefile b/elf/Makefile index fc9860edee..fa98e188df 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1335,6 +1335,7 @@ $(objpfx)ld.so: $(objpfx)librtld.os $(ld-map) # if the sanity check below fails. $(LINK.o) -nostdlib -nostartfiles -shared -o $@.new \ $(LDFLAGS-rtld) -Wl,-z,defs $(z-now-$(bind-now)) \ + $(dt-relr-ldflag) \ $(filter-out $(map-file),$^) $(load-map-file) \ -Wl,-soname=$(rtld-installed-name) $(call after-link,$@.new) @@ -1659,6 +1660,7 @@ $(objpfx)nodlopen2.out: $(objpfx)nodlopenmod2.so $(objpfx)filtmod1.so: $(objpfx)filtmod1.os $(objpfx)filtmod2.so $(LINK.o) -shared -o $@ -B$(csu-objpfx) $(LDFLAGS.so) \ + $(dt-relr-ldflag) \ -L$(subst :, -L,$(rpath-link)) \ -Wl,-rpath-link=$(rpath-link) \ $< -Wl,-F,$(objpfx)filtmod2.so @@ -2462,7 +2464,7 @@ $(objpfx)tst-big-note: $(objpfx)tst-big-note-lib.so # artificial, large note in tst-big-note-lib.o and invalidate the # test. $(objpfx)tst-big-note-lib.so: $(objpfx)tst-big-note-lib.o - $(LINK.o) -shared -o $@ $(LDFLAGS.so) $< + $(LINK.o) -shared -o $@ $(LDFLAGS.so) $(dt-relr-ldflag) $< $(objpfx)tst-unwind-ctor: $(objpfx)tst-unwind-ctor-lib.so @@ -2771,6 +2773,7 @@ $(objpfx)tst-ro-dynamic: $(objpfx)tst-ro-dynamic-mod.so $(objpfx)tst-ro-dynamic-mod.so: $(objpfx)tst-ro-dynamic-mod.os \ tst-ro-dynamic-mod.map $(LINK.o) -nostdlib -nostartfiles -shared -o $@ \ + $(dt-relr-ldflag) \ -Wl,--script=tst-ro-dynamic-mod.map \ $(objpfx)tst-ro-dynamic-mod.os