Debian Codegraph @

libctf/testsuite/lib/ctf-lib.exp

raw
1 # Support routines for libctf testsuite.
2 # Copyright (C) 1994-2025 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This file is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20
21 load_file $srcdir/../../ld/testsuite/lib/ld-lib.exp
22
23 # Returns true if the target linker deduplicates CTF.
24 proc check_ctf_linker_dedup { } {
25 global ctf_linker_dedup_saved
26
27 if {![info exists ctf_linker_dedup_saved]} {
28 set ctf_linker_dedup_saved 0
29
30 if ([check_ctf_available]) {
31 global objdump srcdir
32
33 set basename "tmpdir/ctf_linker_dedups[pid]"
34 compile_one_cc $srcdir/libctf-lookup/ambiguous-struct-A.c ${basename}-A.o "-gctf -fPIC -c"
35 compile_one_cc $srcdir/libctf-lookup/ambiguous-struct-B.c ${basename}-B.o "-gctf -fPIC -c"
36 compile_one_cc "${basename}-A.o ${basename}-B.o" $basename.so "-gctf -fPIC -shared"
37 if {! [remote_file host exists $basename.so] } {
38 return 0
39 }
40
41 # Don't use run_host_cmd: it dumps the entire output into the log,
42 # even on success.
43 set cmdret [remote_exec host [concat sh -c [list "$objdump --ctf $basename.so >dump.out 2>dump.err"]] "" "/dev/null"]
44 set cmdret [lindex $cmdret 0]
45 remote_upload host "dump.out"
46 remote_upload host "dump.err"
47 set dump_out [prune_warnings [file_contents "dump.out"]]
48 set dump_err [prune_warnings [file_contents "dump.err"]]
49 remote_file host delete "dump.out" "dump.err"
50 remote_file build delete "dump.out" "dump.err"
51
52 if {$cmdret != 0} {
53 verbose -log "failed with $cmdret: stderr: $dump_err"
54 verbose -log "output: $dump_out"
55 return 0;
56 }
57
58 remote_file host delete $basename.so ${basename}-A.o ${basename}-B.o
59 if [regexp {CTF archive member: } $dump_out] {
60 set ctf_linker_dedup_saved 1
61 }
62 }
63 }
64 return $ctf_linker_dedup_saved
65 }
66
67
68 proc run_native_host_cmd { command } {
69 global link_output
70 global ld
71
72 verbose -log "$command"
73 set run_output ""
74 try {
75 set run_output [exec "sh" "-c" "$command" "2>@1"]
76 set status 0
77 } trap CHILDSTATUS {results options} {
78 set status [lindex [dict get $options -errorcode] 2]
79 set run_output $results
80 }
81 regsub "\n$" $run_output "" run_output
82 if { [lindex $status 0] != 0 && [string match "" $run_output] } then {
83 append run_output "child process exited abnormally"
84 }
85
86 if [string match "" $run_output] then {
87 return ""
88 }
89
90 verbose -log "$run_output"
91 return "$run_output"
92 }
93
94 # Compile and link a C source file for execution on the host.
95 proc compile_link_one_host_cc { src output additional_args } {
96 global CC
97 global CFLAGS
98
99 return [run_native_host_cmd "./libtool --quiet --tag=CC --mode=link $CC $CFLAGS $src -o $output $additional_args" ]
100 }
101
102 # run_lookup_test FILE
103 #
104 # Compile with the host compiler and link a .c file into a "lookup" binary, then
105 # compile and optionally link together a bunch of .s or .c files with CTF info
106 # and pass the name of the resulting binary to the "lookup" binary and check the
107 # output. (If none is specified, the binary is expected to generate its own CTF
108 # for testing purposes.)
109 #
110 # As with run_dump_test, this is all driven by a file (in this case, a .lk file)
111 # beginning with zero or more option lines, which specify the names of the
112 # lookup binary's source file, the source file(s) with CTF info to compile
113 # together, and whether to link them. The optional lines have the syntax:
114 #
115 # # OPTION: VALUE
116 #
117 # OPTION is the name of some option, like "name" or "lookup", and
118 # VALUE is OPTION's value. The valid options are described below.
119 # Whitespace is ignored everywhere, except within VALUE. The option
120 # list ends with the first line that doesn't match the above syntax.
121 # However, a line within the options that begins with a #, but doesn't
122 # have a recognizable option name followed by a colon, is considered a
123 # comment and entirely ignored.
124 #
125 # The interesting options are:
126 #
127 # name: TEST-NAME
128 # The name of this test, passed to DejaGNU's `pass' and `fail'
129 # commands. If omitted, this defaults to FILE, the root of the
130 # lookup .c file's name.
131 #
132 # lookup: SOURCE
133 # Compile the file SOURCE.c. If omitted, the lookup source defaults
134 # to FILE.c.
135 #
136 # source: SOURCE
137 # Assemble the file SOURCE.c and pass it to the LOOKUP program.
138 #
139 # nonshared:
140 # If set, do not link with -shared.
141 #
142 # link:
143 # If set, link the SOURCE together even if only one file is specified.
144 #
145 # link_flags:
146 # If set, extra flags to pass to the linker.
147 #
148 # lookup_link:
149 # If set, extra libraries to link the lookup program with.
150 #
151 # xfail: GLOB|PROC ...
152 # This test is expected to fail on a specified list of targets.
153 #
154 # no_cross:
155 # If set, do not run this test when host != target.
156 #
157 # host:
158 # If set, only run this test on hosts matching the given glob.
159 #
160 # wrapper:
161 # Wrap invocations of LOOKUP in this command. (Useful for valgrind
162 # invocations, etc.)
163 #
164 # Each option may occur at most once unless otherwise mentioned.
165 #
166 # After the option lines come regexp lines. run_lookup_test calls
167 # regexp_diff to compare the output of the lookup program against the
168 # regexps in FILE.d.
169 #
170 proc run_lookup_test { name } {
171 global CC_FOR_TARGET CFLAGS_FOR_TARGET LIBS TEST_CROSS
172 global copyfile env runtests srcdir subdir verbose
173
174 if ![runtest_file_p $runtests $name] then {
175 return
176 }
177
178 if [string match "*/*" $name] {
179 set file $name
180 set name [file tail $name]
181 } else {
182 set file "$srcdir/$subdir/$name"
183 }
184 # strip off the srcdir, which contains build host specific pathnames
185 set file4log [string map [list $srcdir/ ""] $file]
186
187 set opt_array [slurp_options "${file}.lk"]
188 if { $opt_array == -1 } {
189 perror "error reading options from $file.lk"
190 unresolved $file4log
191 return
192 }
193 set run_ld 0
194 set shared "-shared"
195 set opts(link) {}
196 set opts(link_flags) {}
197 set opts(lookup_link) {}
198 set opts(nonshared) {}
199 set opts(lookup) {}
200 set opts(name) {}
201 set opts(source) {}
202 set opts(xfail) {}
203 set opts(no_cross) {}
204 set opts(host) {}
205 set opts(wrapper) {}
206
207 foreach i $opt_array {
208 set opt_name [lindex $i 0]
209 set opt_val [lindex $i 1]
210 if { $opt_name == "" } {
211 set in_extra 1
212 continue
213 }
214 if ![info exists opts($opt_name)] {
215 perror "unknown option $opt_name in file $file.lk"
216 unresolved $file4log
217 return
218 }
219
220 set opts($opt_name) [concat $opts($opt_name) $opt_val]
221 }
222
223 if { [llength $opts(no_cross)] != 0
224 && "$TEST_CROSS" eq "yes" } {
225 untested "$file4log not tested when cross-compiling"
226 return
227 }
228
229 if { [llength $opts(host)] != 0 && ![ishost $opts(host)] } {
230 untested "$file4log only runs on $opts(host)"
231 return
232 }
233
234 if { [llength $opts(lookup)] == 0 } {
235 set opts(lookup) "$file.c"
236 } else {
237 set opts(lookup) "[file dirname $file]/$opts(lookup)"
238 }
239
240 if { [llength $opts(name)] == 0 } {
241 set opts(name) $opts(lookup)
242 }
243
244 if { [llength $opts(link)] != 0
245 || [llength $opts(source)] > 1 } {
246 set run_ld 1
247 }
248
249 if { [llength $opts(nonshared)] != 0 } {
250 set shared ""
251 }
252
253 set testname $file4log
254
255 # Compile and link the lookup program.
256 set comp_output [prune_warnings [compile_link_one_host_cc $opts(lookup) "tmpdir/lookup" "libctf.la $opts(lookup_link)"]]
257
258 if { $comp_output != ""} {
259 send_log "compilation of lookup program $opts(lookup) failed with <$comp_output>"
260 perror "compilation of lookup program $opts(lookup) failed"
261 fail $testname
262 return 0
263 }
264
265 # Compile the inputs and posibly link them together.
266
267 set lookup_output ""
268 if { [llength $opts(source)] > 0 } {
269 set lookup_flags ""
270 if { $run_ld } {
271 set lookup_output "tmpdir/out.so"
272 set lookup_flags "-gctf -fPIC $shared $opts(link_flags)"
273 } else {
274 set lookup_output "tmpdir/out.o"
275 set lookup_flags "-gctf -fPIC -c"
276 }
277 if [board_info [target_info name] exists cflags] {
278 append lookup_flags " [board_info [target_info name] cflags]"
279 }
280 if [board_info [target_info name] exists ldflags] {
281 append lookup_flags " [board_info [target_info name] ldflags]"
282 }
283 set src {}
284 foreach sfile $opts(source) {
285 if [is_remote host] {
286 lappend src [remote_download host [file join [file dirname $file] $sfile]]
287 } else {
288 lappend src [file join [file dirname $file] $sfile]
289 }
290 }
291
292 set comp_output [prune_warnings [run_host_cmd "$CC_FOR_TARGET" "$CFLAGS_FOR_TARGET $lookup_flags [concat $src] -o $lookup_output"]]
293
294 if { $comp_output != ""} {
295 send_log "compilation of CTF program [concat $src] failed with <$comp_output>"
296 fail $testname
297 return 0
298 }
299 }
300
301 # Time to setup xfailures.
302 foreach targ $opts(xfail) {
303 if [match_target $targ] {
304 setup_xfail "*-*-*"
305 break
306 }
307 }
308
309 # Invoke the lookup program on the outputs, possibly through the wrapper.
310
311 if { [llength $opts(wrapper)] == 0 } {
312 set results [run_host_cmd tmpdir/lookup $lookup_output]
313 } else {
314 set results [run_host_cmd "$opts(wrapper) tmpdir/lookup" $lookup_output]
315 }
316
317 if { [regexp {^UNSUPPORTED: (.*)$} $results -> reason] } {
318 unsupported "$testname: $reason"
319 return 0
320 }
321
322 set f [open "tmpdir/lookup.out" "w"]
323 puts $f $results
324 close $f
325
326 if { [regexp_diff "tmpdir/lookup.out" "${file}.lk"] } then {
327 fail $testname
328 if { $verbose == 2 } then { verbose "output is [file_contents tmpdir/lookup.out]" 2 }
329 return 0
330 }
331
332 pass $testname
333 return 0
334 }