[gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie

Message ID 6339c676-579f-f841-f6ba-d3026d1a45ad@suse.de
State New
Headers show
Series
  • [gdb/testsuite] Prevent compilation fails with unix/-fPIE/-pie
Related show

Commit Message

Philippe Waroquiers via Gdb-patches Oct. 7, 2021, 11 a.m.
[ was: Re: [PATCH][gdb/testsuite] Add proc require in lib/gdb.exp ]

This follow-up patch uses the new proc require.

Any comments?

Thanks,
- Tom

Comments

Philippe Waroquiers via Gdb-patches Oct. 8, 2021, 2:37 p.m. | #1
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
[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 {

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
- adding "require pie_forced 0" in all affected test-cases.
such that we simply have:
...
UNTESTED: gdb.arch/amd64-disp-step.exp: nopie failed to prevent PIE executable
...

Likewise, add nopie_forced.

Tested on x86_64-linux.

---
 gdb/testsuite/gdb.arch/amd64-disp-step.exp         |  2 ++
 gdb/testsuite/gdb.arch/amd64-entry-value.exp       |  2 ++
 .../gdb.arch/amd64-invalid-stack-middle.exp        |  2 ++
 gdb/testsuite/gdb.arch/i386-float.exp              |  2 ++
 gdb/testsuite/gdb.arch/i386-signal.exp             |  2 ++
 gdb/testsuite/gdb.dwarf2/clztest.exp               |  2 ++
 gdb/testsuite/gdb.dwarf2/dw2-common-block.exp      |  2 ++
 gdb/testsuite/gdb.dwarf2/dw2-dup-frame.exp         |  3 +++
 gdb/testsuite/gdb.dwarf2/dw2-reg-undefined.exp     |  3 +++
 .../gdb.dwarf2/dw2-single-line-discriminators.exp  |  2 ++
 .../gdb.dwarf2/dw2-undefined-ret-addr.exp          |  3 +++
 gdb/testsuite/gdb.mi/mi-reg-undefined.exp          |  3 +++
 gdb/testsuite/lib/gdb.exp                          | 30 ++++++++++++++++++++++
 13 files changed, 58 insertions(+)

diff --git a/gdb/testsuite/gdb.arch/amd64-disp-step.exp b/gdb/testsuite/gdb.arch/amd64-disp-step.exp
index f30f29ea578..15fbcbf4051 100644
--- a/gdb/testsuite/gdb.arch/amd64-disp-step.exp
+++ b/gdb/testsuite/gdb.arch/amd64-disp-step.exp
@@ -23,6 +23,8 @@  if { ![istarget x86_64-*-* ] || ![is_lp64_target] } {
     return
 }
 
+require pie_forced 0
+
 set newline "\[\r\n\]*"
 
 set opts {debug nopie}
diff --git a/gdb/testsuite/gdb.arch/amd64-entry-value.exp b/gdb/testsuite/gdb.arch/amd64-entry-value.exp
index fdfa4a01b58..b9468f04897 100644
--- a/gdb/testsuite/gdb.arch/amd64-entry-value.exp
+++ b/gdb/testsuite/gdb.arch/amd64-entry-value.exp
@@ -13,6 +13,8 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+require pie_forced 0
+
 standard_testfile .s
 set opts {nopie}
 
diff --git a/gdb/testsuite/gdb.arch/amd64-invalid-stack-middle.exp b/gdb/testsuite/gdb.arch/amd64-invalid-stack-middle.exp
index f9e590d83bb..57bb1de9850 100644
--- a/gdb/testsuite/gdb.arch/amd64-invalid-stack-middle.exp
+++ b/gdb/testsuite/gdb.arch/amd64-invalid-stack-middle.exp
@@ -27,6 +27,8 @@ 
 # run twice, and we restart gdb before testing each different command to
 # ensure that nothing is being cached.
 
+require pie_forced 0
+
 standard_testfile .S
 
 if { ![istarget x86_64-*-* ] || ![is_lp64_target] } {
diff --git a/gdb/testsuite/gdb.arch/i386-float.exp b/gdb/testsuite/gdb.arch/i386-float.exp
index b96ff2ba13e..91b9142dbba 100644
--- a/gdb/testsuite/gdb.arch/i386-float.exp
+++ b/gdb/testsuite/gdb.arch/i386-float.exp
@@ -18,6 +18,8 @@ 
 
 # Test the x87 floating point information printout.
 
+require pie_forced 0
+
 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } then {
     verbose "Skipping i386 tests for x87 floating point support."
     return
diff --git a/gdb/testsuite/gdb.arch/i386-signal.exp b/gdb/testsuite/gdb.arch/i386-signal.exp
index aff796325c9..4354a956d28 100644
--- a/gdb/testsuite/gdb.arch/i386-signal.exp
+++ b/gdb/testsuite/gdb.arch/i386-signal.exp
@@ -15,6 +15,8 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+require pie_forced 0
+
 if { ![istarget "i?86-*-*"] && ![istarget "x86_64-*-*"] } then {
     verbose "Skipping i386 unwinder tests."
     return
diff --git a/gdb/testsuite/gdb.dwarf2/clztest.exp b/gdb/testsuite/gdb.dwarf2/clztest.exp
index 0f6d18f2c41..a038d3546f6 100644
--- a/gdb/testsuite/gdb.dwarf2/clztest.exp
+++ b/gdb/testsuite/gdb.dwarf2/clztest.exp
@@ -13,6 +13,8 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+require pie_forced 0
+
 load_lib dwarf.exp
 
 standard_testfile .S
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-common-block.exp b/gdb/testsuite/gdb.dwarf2/dw2-common-block.exp
index 5c78f07943d..afd9c87e286 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-common-block.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-common-block.exp
@@ -13,6 +13,8 @@ 
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+require pie_forced 0
+
 load_lib dwarf.exp
 
 # This test can only be run on targets which support DWARF-2 and use gas.
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-dup-frame.exp b/gdb/testsuite/gdb.dwarf2/dw2-dup-frame.exp
index 4864658a2a1..64a8e462c73 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-dup-frame.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-dup-frame.exp
@@ -12,6 +12,9 @@ 
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require pie_forced 0
+
 load_lib dwarf.exp
 
 # This test can only be run on targets which support DWARF-2 and use gas.
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-reg-undefined.exp b/gdb/testsuite/gdb.dwarf2/dw2-reg-undefined.exp
index b1c28b2f41c..195fb683553 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-reg-undefined.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-reg-undefined.exp
@@ -12,6 +12,9 @@ 
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require pie_forced 0
+
 load_lib dwarf.exp
 
 # This test can only be run on targets which support DWARF-2 and use gas.
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-single-line-discriminators.exp b/gdb/testsuite/gdb.dwarf2/dw2-single-line-discriminators.exp
index 752c4842467..c1af8090e32 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-single-line-discriminators.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-single-line-discriminators.exp
@@ -16,6 +16,8 @@ 
 # Test gdb's coalescing of multiple line number entries for the same line
 # but with different discriminators.  PR 17276.
 
+require pie_forced 0
+
 load_lib dwarf.exp
 
 # This test can only be run on targets which support DWARF-2 and use gas.
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-undefined-ret-addr.exp b/gdb/testsuite/gdb.dwarf2/dw2-undefined-ret-addr.exp
index f130ce784ee..d60d6da12ab 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-undefined-ret-addr.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-undefined-ret-addr.exp
@@ -12,6 +12,9 @@ 
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require pie_forced 0
+
 load_lib dwarf.exp
 
 standard_testfile .S
diff --git a/gdb/testsuite/gdb.mi/mi-reg-undefined.exp b/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
index f38e20003a3..3732f084b7a 100644
--- a/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
+++ b/gdb/testsuite/gdb.mi/mi-reg-undefined.exp
@@ -12,6 +12,9 @@ 
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require pie_forced 0
+
 load_lib dwarf.exp
 load_lib mi-support.exp
 set MIFLAGS "-i=mi"
diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp
index 89733c59605..ad0902f5a22 100644
--- a/gdb/testsuite/lib/gdb.exp
+++ b/gdb/testsuite/lib/gdb.exp
@@ -8073,6 +8073,34 @@  gdb_caching_proc have_fuse_ld_gold {
     return [gdb_simple_compile $me $src executable $flags]
 }
 
+# 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_simple_compile $me $src executable nopie
+    set res [exec_is_pie $obj]
+    if { $res == -1 } {
+	return -1
+    }
+    set res [expr $res == 1]
+    return $res
+}
+
+# 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_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 {
@@ -8224,6 +8252,8 @@  proc require { fn val } {
 
     switch $fn-$val {
 	gdb_skip_xml_test-0 { set msg "missing xml support" }
+	pie_forced-0 { set msg "nopie failed to prevent PIE executable" }
+	nopie_forced-0 { set msg "pie failed to generate PIE executable" }
 	default { set msg "$fn != $val" }
     }