2012-02-21 Pedro Alves <palves@redhat.com>
[external/binutils.git] / gdb / testsuite / gdb.base / attach-pie-misread.exp
1 # Copyright 2010-2012 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 # This test only works on GNU/Linux.
17 if { ![isnative] || [is_remote host] || [target_info exists use_gdb_stub]
18      || ![istarget *-linux*] || [skip_shlib_tests]} {
19     continue
20 }
21
22 load_lib prelink-support.exp
23
24 set test "attach-pie-misread"
25 set srcfile ${test}.c
26 set genfile ${objdir}/${subdir}/${test}-gen.h
27 set executable ${test}
28 set binfile ${objdir}/${subdir}/${executable}
29
30 if {[build_executable_own_libs ${test}.exp $executable $srcfile [list additional_flags=-fPIE ldflags=-pie]] == ""} {
31     return -1
32 }
33
34 # Program Headers:
35 #   Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
36 #   LOAD           0x000000 0x0000000000400000 0x0000000000400000 0x134f5ec 0x134f5ec R E 0x200000
37 #   LOAD           0x134f5f0 0x000000000194f5f0 0x000000000194f5f0 0x1dbc60 0x214088 RW  0x200000
38 #   DYNAMIC        0x134f618 0x000000000194f618 0x000000000194f618 0x000200 0x000200 RW  0x8
39 #
40 proc read_phdr {binfile test} {
41     set readelf_program [transform readelf]
42     set command "exec $readelf_program -Wl $binfile"
43     verbose -log "command is $command"
44     set result [catch $command output]
45     verbose -log "result is $result"
46     verbose -log "output is $output"
47     if {$result != 0} {
48         fail $test
49         return
50     }
51     if ![regexp {\nProgram Headers:\n *Type [^\n]* Align\n(.*?)\n\n} $output trash phdr] {
52         fail "$test (no Program Headers)"
53         return
54     }
55     if ![regexp -line {^ *DYNAMIC +0x[0-9a-f]+ +(0x[0-9a-f]+) } $phdr trash dynamic_vaddr] {
56         fail "$test (no DYNAMIC found)"
57         return
58     }
59     verbose -log "dynamic_vaddr is $dynamic_vaddr"
60     set align_max -1
61     foreach {trash align} [regexp -line -all -inline {^ *LOAD .* (0x[0-9]+)$} $phdr] {
62         if {$align_max < $align} {
63             set align_max $align
64         }
65     }
66     verbose -log "align_max is $align_max"
67     if {$align_max == -1} {
68         fail "$test (no LOAD found)"
69         return
70     }
71     pass $test
72     return [list $dynamic_vaddr $align_max]
73 }
74
75 set phdr [read_phdr $binfile "readelf initial scan"]
76 set dynamic_vaddr [lindex $phdr 0]
77 set align_max [lindex $phdr 1]
78
79 set stub_size [format 0x%x [expr "2 * $align_max - ($dynamic_vaddr & ($align_max - 1))"]]
80 verbose -log "stub_size is $stub_size"
81
82 # On x86_64 it is commonly about 4MB.
83 if {$stub_size > 25000000} {
84     xfail "stub size $stub_size is too large"
85     return
86 }
87
88 set test "generate stub"
89 set command "exec $binfile $stub_size >$genfile"
90 verbose -log "command is $command"
91 set result [catch $command output]
92 verbose -log "result is $result"
93 verbose -log "output is $output"
94 if {$result == 0} {
95     pass $test
96 } else {
97     fail $test
98 }
99
100 set prelink_args [build_executable_own_libs ${test}.exp $executable $srcfile [list "additional_flags=-fPIE -DGEN=\"$genfile\"" "ldflags=-pie"]]
101 if {$prelink_args == ""} {
102     return -1
103 }
104
105 # x86_64 file has 25MB, no need to keep it.
106 file delete -- $genfile
107
108 set phdr [read_phdr $binfile "readelf rebuilt with stub_size"]
109 set dynamic_vaddr_prelinkno [lindex $phdr 0]
110
111 if ![prelink_yes $prelink_args] {
112     return -1
113 }
114
115 set phdr [read_phdr $binfile "readelf with prelink -R"]
116 set dynamic_vaddr_prelinkyes [lindex $phdr 0]
117
118 set first_offset [format 0x%x [expr $dynamic_vaddr_prelinkyes - $dynamic_vaddr_prelinkno]]
119 verbose -log "first_offset is $first_offset"
120
121 set test "first offset is non-zero"
122 if {$first_offset == 0} {
123     fail "$test (-fPIE -pie in effect?)"
124 } else {
125     pass $test
126 }
127
128 set test "start inferior"
129 gdb_exit
130
131 set res [remote_spawn host $binfile];
132 if { $res < 0 || $res == "" } {
133     perror "Spawning $binfile failed."
134     fail $test
135     return
136 }
137 set pid [exp_pid -i $res]
138 gdb_expect {
139     -re "sleeping\r\n" {
140         pass $test
141     }
142     eof {
143         fail "$test (eof)"
144         remote_exec host "kill -9 $pid"
145         return
146     }
147     timeout {
148         fail "$test (timeout)"
149         remote_exec host "kill -9 $pid"
150         return
151     }
152 }
153
154 # Due to alignments it was reproducible with 1 on x86_64 but 2 on i686.
155 foreach align_mult {1 2} { with_test_prefix "shift-by-$align_mult" {
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
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.
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"
181     gdb_test_multiple "attach $pid" $test {
182         -re "Attaching to program: .*, process $pid\r\n" {
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: .*"
199 }}
200
201 remote_exec host "kill -9 $pid"