Commit | Line | Data |
---|---|---|
86e4bafc JK |
1 | # Copyright 2010 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] || ![istarget *-linux*] || [skip_shlib_tests]} { | |
18 | continue | |
19 | } | |
20 | ||
21 | set test "attach-pie-misread" | |
22 | set srcfile ${test}.c | |
23 | set genfile ${objdir}/${subdir}/${test}-gen.h | |
24 | set executable ${test} | |
25 | set binfile ${objdir}/${subdir}/${executable} | |
26 | ||
27 | if {[build_executable ${test}.exp $executable $srcfile [list "additional_flags=-fPIE -pie"]] == -1} { | |
28 | return -1 | |
29 | } | |
30 | ||
31 | # Program Headers: | |
32 | # Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align | |
33 | # LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x134f5ec 0x134f5ec R E 0x200000 | |
34 | # LOAD 0x134f5f0 0x000000000194f5f0 0x000000000194f5f0 0x1dbc60 0x214088 RW 0x200000 | |
35 | # DYNAMIC 0x134f618 0x000000000194f618 0x000000000194f618 0x000200 0x000200 RW 0x8 | |
36 | # | |
37 | proc read_phdr {binfile test} { | |
38 | set readelf_program [transform readelf] | |
39 | set command "exec $readelf_program -Wl $binfile" | |
40 | verbose -log "command is $command" | |
41 | set result [catch $command output] | |
42 | verbose -log "result is $result" | |
43 | verbose -log "output is $output" | |
44 | if {$result != 0} { | |
45 | fail $test | |
46 | return | |
47 | } | |
48 | if ![regexp {\nProgram Headers:\n *Type [^\n]* Align\n(.*?)\n\n} $output trash phdr] { | |
49 | fail "$test (no Program Headers)" | |
50 | return | |
51 | } | |
52 | if ![regexp -line {^ *DYNAMIC +0x[0-9a-f]+ +(0x[0-9a-f]+) } $phdr trash dynamic_vaddr] { | |
53 | fail "$test (no DYNAMIC found)" | |
54 | return | |
55 | } | |
56 | verbose -log "dynamic_vaddr is $dynamic_vaddr" | |
57 | set align_max -1 | |
58 | foreach {trash align} [regexp -line -all -inline {^ *LOAD .* (0x[0-9]+)$} $phdr] { | |
59 | if {$align_max < $align} { | |
60 | set align_max $align | |
61 | } | |
62 | } | |
63 | verbose -log "align_max is $align_max" | |
64 | if {$align_max == -1} { | |
65 | fail "$test (no LOAD found)" | |
66 | return | |
67 | } | |
68 | pass $test | |
69 | return [list $dynamic_vaddr $align_max] | |
70 | } | |
71 | ||
72 | set phdr [read_phdr $binfile "readelf initial scan"] | |
73 | set dynamic_vaddr [lindex $phdr 0] | |
74 | set align_max [lindex $phdr 1] | |
75 | ||
76 | set stub_size [format 0x%x [expr "2 * $align_max - ($dynamic_vaddr & ($align_max - 1))"]] | |
77 | verbose -log "stub_size is $stub_size" | |
78 | ||
79 | # On x86_64 it is commonly about 4MB. | |
80 | if {$stub_size > 25000000} { | |
81 | xfail "stub size $stub_size is too large" | |
82 | return | |
83 | } | |
84 | ||
85 | set test "generate stub" | |
86 | set command "exec $binfile $stub_size >$genfile" | |
87 | verbose -log "command is $command" | |
88 | set result [catch $command output] | |
89 | verbose -log "result is $result" | |
90 | verbose -log "output is $output" | |
91 | if {$result == 0} { | |
92 | pass $test | |
93 | } else { | |
94 | fail $test | |
95 | } | |
96 | ||
97 | if {[build_executable ${test}.exp $executable $srcfile [list "additional_flags=-fPIE -pie -DGEN=\"$genfile\""]] == -1} { | |
98 | return -1 | |
99 | } | |
100 | ||
101 | # x86_64 file has 25MB, no need to keep it. | |
102 | file delete -- $genfile | |
103 | ||
104 | set phdr [read_phdr $binfile "readelf rebuilt with stub_size"] | |
105 | set dynamic_vaddr_prelinkno [lindex $phdr 0] | |
106 | ||
107 | set command "exec /usr/sbin/prelink -q -N --no-exec-shield -R $binfile" | |
108 | verbose -log "command is $command" | |
109 | set result [catch $command output] | |
110 | verbose -log "result is $result" | |
111 | verbose -log "output is $output" | |
112 | ||
113 | set test "prelink -R" | |
114 | if {$result == 0 && $output == ""} { | |
115 | pass $test | |
116 | } elseif {$result == 1 && [regexp {^(couldn't execute "/usr/sbin/prelink[^\r\n]*": no such file or directory\n?)*$} $output]} { | |
117 | untested attach-pie-misread.exp | |
118 | return -1 | |
119 | } else { | |
120 | fail $test | |
121 | } | |
122 | ||
123 | set phdr [read_phdr $binfile "readelf with prelink -R"] | |
124 | set dynamic_vaddr_prelinkyes [lindex $phdr 0] | |
125 | ||
126 | set first_offset [format 0x%x [expr $dynamic_vaddr_prelinkyes - $dynamic_vaddr_prelinkno]] | |
127 | verbose -log "first_offset is $first_offset" | |
128 | ||
129 | set test "first offset is non-zero" | |
130 | if {$first_offset == 0} { | |
131 | fail "$test (-fPIE -pie in effect?)" | |
132 | } else { | |
133 | pass $test | |
134 | } | |
135 | ||
136 | set test "start inferior" | |
137 | gdb_exit | |
138 | ||
139 | set res [remote_spawn host $binfile]; | |
140 | if { $res < 0 || $res == "" } { | |
141 | perror "Spawning $binfile failed." | |
142 | fail $test | |
143 | return | |
144 | } | |
145 | set pid [exp_pid -i $res] | |
146 | gdb_expect { | |
147 | -re "sleeping\r\n" { | |
148 | pass $test | |
149 | } | |
150 | eof { | |
151 | fail "$test (eof)" | |
152 | remote_exec host "kill -9 $pid" | |
153 | return | |
154 | } | |
155 | timeout { | |
156 | fail "$test (timeout)" | |
157 | remote_exec host "kill -9 $pid" | |
158 | return | |
159 | } | |
160 | } | |
161 | ||
162 | # Due to alignments it was reproducible with 1 on x86_64 but 2 on i686. | |
163 | foreach align_mult {1 2} { | |
164 | set old_ldprefix $pf_prefix | |
165 | lappend pf_prefix "shift-by-$align_mult:" | |
166 | ||
167 | # FIXME: We believe there is enough room under FIRST_OFFSET. | |
168 | set shifted_offset [format 0x%x [expr "$first_offset - $align_mult * $align_max"]] | |
169 | verbose -log "shifted_offset is $shifted_offset" | |
170 | ||
171 | set command "exec /usr/sbin/prelink -q -N --no-exec-shield -r $shifted_offset $binfile" | |
172 | verbose -log "command is $command" | |
173 | set result [catch $command output] | |
174 | verbose -log "result is $result" | |
175 | verbose -log "output is $output" | |
176 | ||
177 | set test "prelink -r" | |
178 | if {$result == 0 && $output == ""} { | |
179 | pass $test | |
180 | } else { | |
181 | fail $test | |
182 | } | |
183 | ||
184 | clean_restart $executable | |
185 | ||
186 | set test "attach" | |
187 | gdb_test_multiple "attach $pid" $test { | |
188 | -re "Attaching to program: .*, process $pid\r\n" { | |
189 | # Missing "$gdb_prompt $" is intentional. | |
190 | pass $test | |
191 | } | |
192 | } | |
193 | ||
194 | set test "error on Cannot access memory at address" | |
195 | gdb_test_multiple "" $test { | |
196 | -re "\r\nCannot access memory at address .*$gdb_prompt $" { | |
197 | fail $test | |
198 | } | |
199 | -re "$gdb_prompt $" { | |
200 | pass $test | |
201 | } | |
202 | } | |
203 | ||
204 | gdb_test "detach" "Detaching from program: .*" | |
205 | ||
206 | set pf_prefix $old_ldprefix | |
207 | } | |
208 | ||
209 | remote_exec host "kill -9 $pid" |