Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries via Gdb-patches <gdb-patches@sourceware.org>
To: Tom Tromey <tom@tromey.com>,
	Tom de Vries via Gdb-patches <gdb-patches@sourceware.org>
Subject: Re: [PATCH][gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie
Date: Fri, 8 Oct 2021 16:37:46 +0200	[thread overview]
Message-ID: <d8af773b-3eb3-ff23-8861-e7164ae3b91d@suse.de> (raw)
In-Reply-To: <6339c676-579f-f841-f6ba-d3026d1a45ad@suse.de>

[-- Attachment #1: Type: text/plain, Size: 314 bytes --]

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



[-- Attachment #2: 0001-gdb-testsuite-Prevent-compilation-fails-with-unix-fPIE-pie.patch --]
[-- Type: text/x-patch, Size: 4044 bytes --]

[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 {

  reply	other threads:[~2021-10-08 14:38 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 13:55 [committed][gdb/testsuite] Disable vgdb tests if xml not supported Tom de Vries via Gdb-patches
2021-09-30 17:30 ` Tom Tromey
2021-09-30 21:41   ` Tom de Vries via Gdb-patches
2021-09-30 21:47     ` Tom de Vries via Gdb-patches
2021-10-05 18:15     ` Tom Tromey
2021-10-06  7:40       ` [PATCH][gdb/testsuite] Add proc require in lib/gdb.exp Tom de Vries via Gdb-patches
2021-10-07 11:00         ` [PATCH][gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie Tom de Vries via Gdb-patches
2021-10-08 14:37           ` Tom de Vries via Gdb-patches [this message]
2021-10-11 10:28         ` [PATCH][gdb/testsuite] Add proc require in lib/gdb.exp Tom de Vries via Gdb-patches

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=d8af773b-3eb3-ff23-8861-e7164ae3b91d@suse.de \
    --to=gdb-patches@sourceware.org \
    --cc=tdevries@suse.de \
    --cc=tom@tromey.com \
    /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