From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 9DE45385840C for ; Fri, 8 Oct 2021 14:37:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9DE45385840C Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id D2B711FD7D; Fri, 8 Oct 2021 14:37:46 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id BCDA113EFB; Fri, 8 Oct 2021 14:37:46 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id vq7qLLpXYGEiCwAAMHmgww (envelope-from ); Fri, 08 Oct 2021 14:37:46 +0000 Subject: Re: [PATCH][gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie To: Tom Tromey , Tom de Vries via Gdb-patches References: <20210929135520.GA16103@delia> <87pmsq0y4m.fsf@tromey.com> <8735pfz6ax.fsf@tromey.com> <5de83135-c797-d211-467a-de12d174ae67@suse.de> <6339c676-579f-f841-f6ba-d3026d1a45ad@suse.de> From: Tom de Vries Message-ID: Date: Fri, 8 Oct 2021 16:37:46 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: <6339c676-579f-f841-f6ba-d3026d1a45ad@suse.de> Content-Type: multipart/mixed; boundary="------------82840F8022238372E2659DBA" Content-Language: en-US X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Oct 2021 14:37:49 -0000 This is a multi-part message in MIME format. --------------82840F8022238372E2659DBA Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 10/7/21 1:00 PM, Tom de Vries via Gdb-patches wrote: > [ was: Re: [PATCH][gdb/testsuite] Add proc require in lib/gdb.exp ] > > This follow-up patch uses the new proc require. > And this version of the patch doesn't use proc require, but instead handles things in gdb_compile. Any comments? Thanks, - Tom --------------82840F8022238372E2659DBA Content-Type: text/x-patch; charset=UTF-8; name="0001-gdb-testsuite-Prevent-compilation-fails-with-unix-fPIE-pie.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename*0="0001-gdb-testsuite-Prevent-compilation-fails-with-unix-fPIE-"; filename*1="pie.patch" [gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie A regular test-case will produce an executable, and depending on the compiler default, it will be a PIE or not. A test-case can force one or the other using the pie and nopie options. However, when running with target board unix/-fPIE/-pie, the nopie option will have no effect, and likewise for target board unix/-fno-PIE/-no-pie and the pie option. When say we run test-case gdb.base/attach-pie-noexec.exp, which passes the pie option with target board unix/-fno-PIE/-no-pie we get: ... Running src/gdb/testsuite/gdb.base/attach-pie-noexec.exp ... gdb compile failed, pie failed to generate PIE executable === gdb Summary === # of untested testcases 1 ... However, this works only when we actually manage to generate an executable. There are other test-cases, like f.i. gdb.arch/amd64-disp-step.exp that specify nopie, but will generate a compilation failure with target board unix/-fPIE/-pie due to using a hard-coded .S file: ... Running src/gdb/testsuite/gdb.arch/amd64-disp-step.exp ... gdb compile failed, ld: outputs/gdb.arch/amd64-disp-step/amd64-disp-step0.o: \ relocation R_X86_64_32S against `.text' can not be used when making a PIE \ object; recompile with -fPIE collect2: error: ld returned 1 exit status === gdb Summary === # of untested testcases 1 ... Hide this compilation error by: - adding a gdb_caching_proc pie_forced, and - using it in gdb_compile to bail out before even trying compilation such that we simply have: ... UNTESTED: gdb.arch/amd64-disp-step.exp: failed to prepare ... Likewise, add nopie_forced. Tested on x86_64-linux. --- gdb/testsuite/lib/gdb.exp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 5642db4334d..78019fcd54d 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -4487,6 +4487,17 @@ proc gdb_compile {source dest type options} { lappend options "$flag" } + if { $pie != -1 && [nopie_forced] } { + set result "pie unsupported" + verbose -log "gdb_compile: $result" + return $result + } + if { $nopie != -1 && [pie_forced] } { + set result "nopie unsupported" + verbose -log "gdb_compile: $result" + return $result + } + if { $type == "executable" } { if { ([istarget "*-*-mingw*"] || [istarget "*-*-*djgpp"] @@ -8073,6 +8084,48 @@ gdb_caching_proc have_fuse_ld_gold { return [gdb_simple_compile $me $src executable $flags] } +# Helper function for pie_forced. +proc pie_forced_0 { } { return 0 } + +# Return 1 if nopie fails to prevent a PIE, 0 if nopie prevented a PIE, +# and -1 if an error occurred. +gdb_caching_proc pie_forced { + set me "pie_forced" + set src { int main() { return 0; } } + # gdb_compile calls pie_forced when nopie is passed, so pretend it + # returns 0, to allow us to find out the actual pie_forced value. + with_override pie_forced pie_forced_0 { + gdb_simple_compile $me $src executable nopie + } + set res [exec_is_pie $obj] + if { $res == -1 } { + return -1 + } + set res [expr $res == 1] + return $res +} + +# Helper function for nopie_forced. +proc nopie_forced_0 {} { return 0 } + +# Return 1 if pie fails to generated a PIE, 0 if pie generated a PIE, +# and -1 if an error occurred. +gdb_caching_proc nopie_forced { + set me "nopie_forced" + set src { int main() { return 0; } } + # gdb_compile calls nopie_forced when pie is passed, so pretend it + # returns 0, to allow us to find out the actual nopie_forced value. + with_override nopie_forced nopie_forced_0 { + gdb_simple_compile $me $src executable pie + } + set res [exec_is_pie $obj] + if { $res == -1 } { + return -1 + } + set res [expr $res == 0] + return $res +} + # Return 1 if compiler supports scalar_storage_order attribute, otherwise # return 0. gdb_caching_proc supports_scalar_storage_order_attribute { --------------82840F8022238372E2659DBA--