]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/attach-pie-misread.exp
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / attach-pie-misread.exp
CommitLineData
4a94e368 1# Copyright 2010-2022 Free Software Foundation, Inc.
86e4bafc
JK
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# This test only works on GNU/Linux.
079670b9 17if { ![isnative] || [is_remote host] || [use_gdb_stub]
3a3dad98 18 || ![istarget *-linux*] || [skip_shlib_tests]} {
86e4bafc
JK
19 continue
20}
21
5e3b36f8
JK
22load_lib prelink-support.exp
23
a64d2530
TT
24standard_testfile .c
25set genfile [standard_output_file ${testfile}-gen.h]
26set executable $testfile
86e4bafc 27
6ef69a3f
TV
28if {[build_executable_own_libs ${testfile}.exp $executable $srcfile \
29 {pie}] == ""} {
86e4bafc
JK
30 return -1
31}
32
33# Program Headers:
34# Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
35# LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x134f5ec 0x134f5ec R E 0x200000
36# LOAD 0x134f5f0 0x000000000194f5f0 0x000000000194f5f0 0x1dbc60 0x214088 RW 0x200000
37# DYNAMIC 0x134f618 0x000000000194f618 0x000000000194f618 0x000200 0x000200 RW 0x8
38#
39proc read_phdr {binfile test} {
4fa7d390 40 set readelf_program [gdb_find_readelf]
86e4bafc
JK
41 set command "exec $readelf_program -Wl $binfile"
42 verbose -log "command is $command"
43 set result [catch $command output]
44 verbose -log "result is $result"
45 verbose -log "output is $output"
46 if {$result != 0} {
47 fail $test
48 return
49 }
50 if ![regexp {\nProgram Headers:\n *Type [^\n]* Align\n(.*?)\n\n} $output trash phdr] {
51 fail "$test (no Program Headers)"
52 return
53 }
54 if ![regexp -line {^ *DYNAMIC +0x[0-9a-f]+ +(0x[0-9a-f]+) } $phdr trash dynamic_vaddr] {
55 fail "$test (no DYNAMIC found)"
56 return
57 }
58 verbose -log "dynamic_vaddr is $dynamic_vaddr"
59 set align_max -1
60 foreach {trash align} [regexp -line -all -inline {^ *LOAD .* (0x[0-9]+)$} $phdr] {
61 if {$align_max < $align} {
62 set align_max $align
63 }
64 }
65 verbose -log "align_max is $align_max"
66 if {$align_max == -1} {
67 fail "$test (no LOAD found)"
68 return
69 }
70 pass $test
71 return [list $dynamic_vaddr $align_max]
72}
73
74set phdr [read_phdr $binfile "readelf initial scan"]
75set dynamic_vaddr [lindex $phdr 0]
76set align_max [lindex $phdr 1]
77
78set stub_size [format 0x%x [expr "2 * $align_max - ($dynamic_vaddr & ($align_max - 1))"]]
79verbose -log "stub_size is $stub_size"
80
81# On x86_64 it is commonly about 4MB.
82if {$stub_size > 25000000} {
83 xfail "stub size $stub_size is too large"
84 return
85}
86
87set test "generate stub"
88set command "exec $binfile $stub_size >$genfile"
89verbose -log "command is $command"
90set result [catch $command output]
91verbose -log "result is $result"
92verbose -log "output is $output"
93if {$result == 0} {
94 pass $test
95} else {
96 fail $test
97}
98
6ef69a3f
TV
99set prelink_args [build_executable_own_libs ${test}.exp $executable $srcfile \
100 [list pie "additional_flags=-DGEN=\"$genfile\""]]
5e3b36f8 101if {$prelink_args == ""} {
86e4bafc
JK
102 return -1
103}
104
105# x86_64 file has 25MB, no need to keep it.
106file delete -- $genfile
107
108set phdr [read_phdr $binfile "readelf rebuilt with stub_size"]
109set dynamic_vaddr_prelinkno [lindex $phdr 0]
110
5e3b36f8 111if ![prelink_yes $prelink_args] {
86e4bafc 112 return -1
86e4bafc
JK
113}
114
115set phdr [read_phdr $binfile "readelf with prelink -R"]
116set dynamic_vaddr_prelinkyes [lindex $phdr 0]
117
118set first_offset [format 0x%x [expr $dynamic_vaddr_prelinkyes - $dynamic_vaddr_prelinkno]]
119verbose -log "first_offset is $first_offset"
120
121set test "first offset is non-zero"
122if {$first_offset == 0} {
6ef69a3f 123 fail "$test (failing because PIE is not effect?)"
86e4bafc
JK
124} else {
125 pass $test
126}
127
128set test "start inferior"
129gdb_exit
130
2c8c5d37
PA
131set test_spawn_id [remote_spawn host $binfile]
132if { $test_spawn_id < 0 || $test_spawn_id == "" } {
86e4bafc
JK
133 perror "Spawning $binfile failed."
134 fail $test
135 return
136}
2c8c5d37 137set testpid [spawn_id_get_pid $test_spawn_id]
86e4bafc
JK
138gdb_expect {
139 -re "sleeping\r\n" {
140 pass $test
141 }
142 eof {
143 fail "$test (eof)"
2c8c5d37 144 wait -i $test_spawn_id
86e4bafc
JK
145 return
146 }
147 timeout {
148 fail "$test (timeout)"
2c8c5d37 149 kill_wait_spawned_process $test_spawn_id
86e4bafc
JK
150 return
151 }
152}
153
154# Due to alignments it was reproducible with 1 on x86_64 but 2 on i686.
0f4d39d5 155foreach align_mult {1 2} { with_test_prefix "shift-by-$align_mult" {
86e4bafc
JK
156
157 # FIXME: We believe there is enough room under FIRST_OFFSET.
158 set shifted_offset [format 0x%x [expr "$first_offset - $align_mult * $align_max"]]
159 verbose -log "shifted_offset is $shifted_offset"
160
5e3b36f8
JK
161 # For normal prelink (prelink_yes call), we need to supply $prelink_args.
162 # For the prelink `-r' option below, $prelink_args is not required.
163 # Moreover, if it was used, the problem would not longer be reproducible
164 # as the libraries would also get relocated.
86e4bafc
JK
165 set command "exec /usr/sbin/prelink -q -N --no-exec-shield -r $shifted_offset $binfile"
166 verbose -log "command is $command"
167 set result [catch $command output]
168 verbose -log "result is $result"
169 verbose -log "output is $output"
170
171 set test "prelink -r"
172 if {$result == 0 && $output == ""} {
173 pass $test
174 } else {
175 fail $test
176 }
177
178 clean_restart $executable
179
180 set test "attach"
2c8c5d37
PA
181 gdb_test_multiple "attach $testpid" $test {
182 -re "Attaching to program: .*, process $testpid\r\n" {
86e4bafc
JK
183 # Missing "$gdb_prompt $" is intentional.
184 pass $test
185 }
186 }
187
188 set test "error on Cannot access memory at address"
189 gdb_test_multiple "" $test {
190 -re "\r\nCannot access memory at address .*$gdb_prompt $" {
191 fail $test
192 }
193 -re "$gdb_prompt $" {
194 pass $test
195 }
196 }
197
198 gdb_test "detach" "Detaching from program: .*"
6a5870ce 199}}
86e4bafc 200
2c8c5d37 201kill_wait_spawned_process $test_spawn_id