* lib/future.exp (gdb_find_ldd): New proc.
[deliverable/binutils-gdb.git] / gdb / testsuite / lib / prelink-support.exp
1 # Copyright (C) 2010-2013 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 # Return nul-terminated string read from section SECTION of EXEC. Return ""
17 # if no such section or nul-terminated string was found. Function is useful
18 # for sections ".interp" or ".gnu_debuglink".
19
20 proc section_get {exec section} {
21 global objdir
22 global subdir
23 set tmp "${objdir}/${subdir}/section_get.tmp"
24 set objcopy_program [transform objcopy]
25
26 set command "exec $objcopy_program -O binary --set-section-flags $section=A --change-section-address $section=0 -j $section $exec $tmp"
27 verbose -log "command is $command"
28 set result [catch $command output]
29 verbose -log "result is $result"
30 verbose -log "output is $output"
31 if {$result == 1} {
32 return ""
33 }
34 set fi [open $tmp]
35 fconfigure $fi -translation binary
36 set data [read $fi]
37 close $fi
38 file delete $tmp
39 # .interp has size $len + 1 but .gnu_debuglink contains garbage after \000.
40 set len [string first \000 $data]
41 if {$len < 0} {
42 verbose -log "section $section not found"
43 return ""
44 }
45 set retval [string range $data 0 [expr $len - 1]]
46 verbose -log "section $section is <$retval>"
47 return $retval
48 }
49
50 # Resolve symlinks.
51
52 proc symlink_resolve {file} {
53 set loop 0
54 while {[file type $file] == "link"} {
55 set target [file readlink $file]
56 if {[file pathtype $target] == "relative"} {
57 set src2 [file dirname $file]/$target
58 } else {
59 set src2 $target
60 }
61 verbose -log "Resolved symlink $file targetting $target as $src2"
62 set file $src2
63
64 set loop [expr $loop + 1]
65 if {$loop > 30} {
66 fail "Looping symlink resolution for $file"
67 return ""
68 }
69 }
70 return $file
71 }
72
73 # Copy SRC to DEST, resolving any symlinks in SRC. Return nonzero iff
74 # the copy was succesful.
75 #
76 # This function is guaranteed to never raise any exception, even when the copy
77 # fails.
78
79 proc file_copy {src dest} {
80 set src [symlink_resolve $src]
81 # Test name would contain unstable directory name for symlink-unresolved
82 # $src.
83 set test "copy [file tail $src] to [file tail $dest]"
84 set command "file copy -force -- $src $dest"
85 verbose -log "command is $command"
86 if [catch $command] {
87 fail $test
88 return 0
89 } else {
90 pass $test
91 return 1
92 }
93 }
94
95 # Wrap function build_executable so that the resulting executable is fully
96 # self-sufficient (without dependencies on system libraries). Parameter
97 # INTERP may be used to specify a loader (ld.so) to be used that is
98 # different from the default system one. INTERP can be set to "no" if no ld.so
99 # copy should be made. Libraries on which the executable depends are copied
100 # into directory DIR. Default DIR value to
101 # `${objdir}/${subdir}/${EXECUTABLE}.d'.
102 #
103 # In case of success, return a string containing the arguments to be used
104 # in order to perform a prelink of the executable obtained. Return the
105 # empty string in case of failure.
106 #
107 # This can be useful when trying to prelink an executable which might
108 # depend on system libraries. To properly prelink an executable, all
109 # of its dynamically linked libraries must be prelinked as well. If
110 # the executable depends on some system libraries, we may not have
111 # sufficient write priviledges on these files to perform the prelink.
112 # This is why we make a copy of these shared libraries, and link the
113 # executable against these copies instead.
114 #
115 # Function recognizes only libraries listed by `ldd' after
116 # its ` => ' separator. That means $INTERP and any libraries not being linked
117 # with -Wl,-soname,NAME.so are not copied.
118
119 proc build_executable_own_libs {testname executable sources options {interp ""} {dir ""}} {
120 global objdir subdir
121
122 if {[build_executable $testname $executable $sources $options] == -1} {
123 return ""
124 }
125 set binfile ${objdir}/${subdir}/${executable}
126
127 set ldd [gdb_find_ldd]
128 set command "$ldd $binfile"
129 set test "ldd $executable"
130 set result [catch "exec $command" output]
131 verbose -log "result of $command is $result"
132 verbose -log "output of $command is $output"
133 if {$result != 0 || $output == ""} {
134 fail $test
135 } else {
136 pass $test
137 }
138
139 # gdb testsuite will put there also needless -lm.
140 set test "$test output contains libs"
141 set libs [regexp -all -inline -line {^.* => (/[^ ]+).*$} $output]
142 if {[llength $libs] == 0} {
143 fail $test
144 } else {
145 pass $test
146 }
147
148 if {$dir == ""} {
149 set dir ${binfile}.d
150 }
151 file delete -force -- $dir
152 file mkdir $dir
153
154 if {$interp == ""} {
155 set interp_system [section_get $binfile .interp]
156 if {$interp_system == ""} {
157 fail "$test could not find .interp"
158 } else {
159 set interp ${dir}/[file tail $interp_system]
160 file_copy $interp_system $interp
161 }
162 }
163 if {$interp == "no"} {
164 set interp ""
165 }
166
167 set dests {}
168 foreach {trash abspath} $libs {
169 set dest "$dir/[file tail $abspath]"
170 file_copy $abspath $dest
171 lappend dests $dest
172 }
173
174 # Do not lappend it so that "-rpath $dir" overrides any possible "-rpath"s
175 # specified by the caller to be able to link it for ldd" above.
176 set options [linsert $options 0 "ldflags=-Wl,-rpath,$dir"]
177 if {$interp != ""} {
178 set options [linsert $options 0 "ldflags=-Wl,--dynamic-linker,$interp"]
179 }
180
181 if {[build_executable $testname $executable $sources $options] == -1} {
182 return ""
183 }
184
185 set prelink_args "--ld-library-path=$dir $binfile [concat $dests]"
186 if {$interp != ""} {
187 set prelink_args "--dynamic-linker=$interp $prelink_args $interp"
188 }
189 return $prelink_args
190 }
191
192 # Unprelink ARG. Reported test name can be specified by NAME. Return non-zero
193 # on success, zero on failure.
194
195 proc prelink_no {arg {name {}}} {
196 if {$name == ""} {
197 set name [file tail $arg]
198 }
199 set test "unprelink $name"
200 set command "exec /usr/sbin/prelink -uN $arg"
201 verbose -log "command is $command"
202 set result [catch $command output]
203 verbose -log "result is $result"
204 verbose -log "output is $output"
205 if {$result == 1 && [regexp {^(couldn't execute "/usr/sbin/prelink[^\r\n]*": no such file or directory\n?)*$} $output]} {
206 # Without prelink, at least verify that all the binaries do not
207 # contain the ".gnu.prelink_undo" section (which would mean that they
208 # have already been prelinked).
209 set test "$test (missing /usr/sbin/prelink)"
210 foreach bin [split $arg] {
211 if [string match "-*" $bin] {
212 # Skip prelink options.
213 continue
214 }
215 set readelf_program [transform readelf]
216 set command "exec $readelf_program -WS $bin"
217 verbose -log "command is $command"
218 set result [catch $command output]
219 verbose -log "result is $result"
220 verbose -log "output is $output"
221 if {$result != 0 || [string match {* .gnu.prelink_undo *} $output]} {
222 fail "$test ($bin is already prelinked)"
223 return 0
224 }
225 }
226 pass $test
227 return 1
228 }
229 if {$result == 0 && $output == ""} {
230 verbose -log "$name has been now unprelinked"
231 set command "exec /usr/sbin/prelink -uN $arg"
232 verbose -log "command is $command"
233 set result [catch $command output]
234 verbose -log "result is $result"
235 verbose -log "output is $output"
236 }
237 # Last line does miss the trailing \n. There can be multiple such messages
238 # as ARG may list multiple files.
239 if {$result == 1 && [regexp {^([^\r\n]*prelink[^\r\n]*: [^ ]* does not have .gnu.prelink_undo section\n?)*$} $output]} {
240 pass $test
241 return 1
242 } else {
243 fail $test
244 return 0
245 }
246 }
247
248 # Prelink ARG. Reported test name can be specified by NAME. Return non-zero
249 # on success, zero on failure.
250
251 proc prelink_yes {arg {name ""}} {
252 if {$name == ""} {
253 set name [file tail $arg]
254 }
255
256 # Try to unprelink it first so that, if it has been already prelinked
257 # before, we get a different address now, making the new result unaffected
258 # by any previous prelinking.
259 if ![prelink_no $arg "$name pre-unprelink"] {
260 return 0
261 }
262
263 set test "prelink $name"
264
265 # `--no-exec-shield' is for i386, where prelink in the exec-shield mode is
266 # forced to push all the libraries tight together, in order to fit into
267 # the first two memory areas (either the ASCII Shield area or at least
268 # below the executable). If the prelink was performed in exec-shield
269 # mode, prelink could have no choice on how to randomize the single new
270 # unprelinked library address without wasting space in the first one/two
271 # memory areas. In such case prelink could place $ARG repeatedly at the
272 # same place and we could have false prelink results on
273 # gdb.base/prelink.exp and others. To prevent this from happening, we use
274 # the --no-exec-shield switch. This may have some consequences in terms
275 # of security, but we do not care in our case.
276
277 set command "exec /usr/sbin/prelink -qNR --no-exec-shield $arg"
278
279 verbose -log "command is $command"
280 set result [catch $command output]
281 verbose -log "result is $result"
282 verbose -log "output is $output"
283 if {$result == 1 && [regexp {^(couldn't execute "/usr/sbin/prelink[^\r\n]*": no such file or directory\n?)*$} $output]} {
284 set test "$test (missing /usr/sbin/prelink)"
285
286 # We could not find prelink. We could check whether $args is already
287 # prelinked but we don't, because:
288 # - It is unlikely that someone uninstalls prelink after having
289 # prelinked the system ld.so;
290 # - We still cannot change its prelinked address.
291 # Therefore, we just skip the test.
292
293 xfail $test
294 return 0
295 }
296 if {$result == 0 && $output == ""} {
297 pass $test
298 return 1
299 } elseif {$result == 1 \
300 && [string match -nocase "*: Not enough room to add .dynamic entry" $output]} {
301 # Linker should have reserved some entries for prelink.
302 xfail $test
303 return 0
304 } else {
305 fail $test
306 return 0
307 }
308 }
This page took 0.035208 seconds and 4 git commands to generate.