[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / pie-execl.exp
1 # Copyright 2010-2018 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 # The problem was due to amd64_skip_prologue attempting to access inferior
17 # memory before the PIE (Position Independent Executable) gets relocated.
18
19 global inferior_spawn_id
20 global gdb_spawn_id
21
22 if ![istarget *-linux*] {
23     continue
24 }
25
26 # In remote mode we cannot use the 'set args' command, and this
27 # test requires it.
28 if { [target_info exists gdb_protocol] } then {
29     if { [target_info gdb_protocol] == "remote" } then {
30         continue
31     }
32 }
33
34 standard_testfile .c
35 set executable1 ${testfile}1
36 set executable2 ${testfile}2
37 set binfile1 ${binfile}1
38 set binfile2 ${binfile}2
39 set binfile2_test_msg OBJDIR/${subdir}/${executable2}
40
41 # Use conditional compilation according to `BIN' as GDB remembers the source
42 # file name of the breakpoint.
43
44 set opts [list debug additional_flags=-fPIE ldflags=-pie]
45 if {[build_executable ${testfile}.exp $executable1 $srcfile [concat $opts {additional_flags=-DBIN=1}]] == -1
46     || [build_executable ${testfile}.exp $executable2 $srcfile [concat $opts {additional_flags=-DBIN=2}]] == -1} {
47     return -1
48 }
49
50 clean_restart ${executable1}
51
52 gdb_test_no_output "set args ${binfile2}" "set args ${binfile2_test_msg}"
53
54 if ![runto_main] {
55     return -1
56 }
57
58 # Do not stop on `main' after re-exec.
59 delete_breakpoints
60
61 gdb_breakpoint "pie_execl_marker"
62 gdb_test "info breakpoints" ".*" ""
63
64 set addr1 ""
65 set test "pie_execl_marker address first"
66 gdb_test_multiple "p/x &pie_execl_marker" $test {
67     -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
68         set addr1 $expect_out(1,string)
69         pass $test
70     }
71 }
72 verbose -log "addr1 is $addr1"
73
74 set test "continue"
75 set matches_found 0
76 gdb_test_multiple $test $test {
77     -re "Error in re-setting breakpoint" {
78         fail $test
79     }
80     -re "Cannot access memory" {
81         fail $test
82     }
83     -i "$inferior_spawn_id" -re "pie-execl: re-exec" {
84         # output from inferior
85         incr matches_found
86         if { $matches_found == 2 } {
87             pass $test
88         } else {
89             exp_continue
90         }
91     }
92     -i "$gdb_spawn_id"
93     -re "executing new program.*\r\nBreakpoint \[0-9\]+,\[^\r\n\]* pie_execl_marker .*\r\n$gdb_prompt $" {
94         # output from gdb
95         incr matches_found
96         if { $matches_found == 2 } {
97             pass $test
98         } else {
99             exp_continue
100         }
101     }
102 }
103
104 gdb_test "info breakpoints" ".*" ""
105
106 set addr2 ""
107 set test "pie_execl_marker address second"
108 gdb_test_multiple "p/x &pie_execl_marker" $test {
109     -re " = (0x\[0-9a-f\]+)\r\n$gdb_prompt $" {
110         set addr2 $expect_out(1,string)
111         pass $test
112     }
113 }
114 verbose -log "addr2 is $addr2"
115
116 # Ensure we cannot get a false PASS and the inferior has really changed.
117 set test "pie_execl_marker address has changed"
118 if [string equal $addr1 $addr2] {
119     fail $test
120 } else {
121     pass $test
122 }