aboutsummaryrefslogtreecommitdiff
path: root/binutils/testsuite/binutils-all/readelf.exp
blob: 8815fee59241fdc65da20a3806d8661504e3bfd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#   Copyright 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.

# Please email any bugs, comments, and/or additions to this file to:
# bug-dejagnu@prep.ai.mit.edu

# Written by Nick Clifton <nickc@cygnus.com>
# Based on scripts written by Ian Lance Taylor <ian@cygnus.com>
# and Ken Raeburn <raeburn@cygnus.com>.

# First some helpful procedures, then the tests themselves

# Return the contents of the filename given
proc file_contents { filename } {
    set file [open $filename r]
    set contents [read $file]
    close $file
    return $contents
}

# regexp_diff, based on simple_diff taken from ld test suite
#	compares two files line-by-line
#	file1 contains strings, file2 contains regexps and #-comments
#	blank lines are ignored in either file
#	returns non-zero if differences exist
#
proc regexp_diff { file_1 file_2 } {

    set eof -1
    set end_1 0
    set end_2 0
    set differences 0
    set diff_pass 0

    if [file exists $file_1] then {
	set file_a [open $file_1 r]
    } else {
	warning "$file_1 doesn't exist"
	return 1
    }

    if [file exists $file_2] then {
	set file_b [open $file_2 r]
    } else {
	fail "$file_2 doesn't exist"
	close $file_a
	return 1
    }

    verbose " Regexp-diff'ing: $file_1 $file_2" 2

    while { 1 } {
	set line_a ""
	set line_b ""
	while { [string length $line_a] == 0 } {
	    if { [gets $file_a line_a] == $eof } {
		set end_1 1
		break
	    }
	}
	while { [string length $line_b] == 0 || [string match "#*" $line_b] } {
	    if [ string match "#pass" $line_b ] {
		set end_2 1
		set diff_pass 1
		break
	    } elseif [ string match "#..." $line_b ] {
		if { [gets $file_b line_b] == $eof } {
		    set end_2 1
		    break
		}
		verbose "looking for \"^$line_b$\"" 3
		while { ![regexp "^$line_b$" "$line_a"] } {
		    verbose "skipping    \"$line_a\"" 3
		    if { [gets $file_a line_a] == $eof } {
			set end_1 1
			break
		    }
		}
		break
	    }
	    if { [gets $file_b line_b] == $eof } {
		set end_2 1
		break
	    }
	}

        if { $diff_pass } { 
            break 
        } elseif { $end_1 && $end_2 } { 
            break
        } elseif { $end_1 } {
            send_log "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1\n"
            verbose "extra regexps in $file_2 starting with \"^$line_b$\"\nEOF from $file_1" 3
            set differences 1
            break
        } elseif { $end_2 } {
            send_log "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n"
            verbose "extra lines in $file_1 starting with \"^$line_a$\"\nEOF from $file_2\n" 3
            set differences 1
            break
        } else {
            verbose "regexp \"^$line_b$\"\nline   \"$line_a\"" 3
            if ![regexp "^$line_b$" "$line_a"] {
		send_log "regexp_diff match failure\n"
		send_log "regexp \"^$line_b$\"\nline   \"$line_a\"\n"
		set differences 1
            }
        }
    }

    if { $differences == 0 && !$diff_pass && [eof $file_a] != [eof $file_b] } {
	send_log "$file_1 and $file_2 are different lengths\n"
	verbose "$file_1 and $file_2 are different lengths" 3
	set differences 1
    }

    close $file_a
    close $file_b

    return $differences
}

# Find out the size by reading the output of the EI_CLASS field.
# Similar to the test for readelf -h, but we're just looking for the
# EI_CLASS line here.
proc readelf_find_size { binary_file } {
    global READELF
    global READELFFLAGS
    global readelf_size

    set readelf_size ""
    set testname "finding out ELF size with readelf -h"
    catch "exec $READELF $READELFFLAGS -h $binary_file > readelf.out" got

    if ![string match "" $got] then {
	send_log $got
	fail $testname
	return
    }

    if { ! [regexp "\n\[ \]*Class:\[ \]*ELF(\[0-9\]+)\n" \
	    [file_contents readelf.out] nil readelf_size] } {
	verbose -log "EI_CLASS field not found in output"
	verbose -log "output is \n[file_contents readelf.out]"
	fail $testname
	return
    } else {
	verbose -log "ELF size is $readelf_size"
    }

    pass $testname
}

# Run an individual readelf test.
# Basically readelf is run on the binary_file with the given options.
# Readelf's output is captured and then compared against the contents
# of the regexp_file-readelf_size if it exists, else regexp_file.

proc readelf_test { options binary_file regexp_file xfails } {

    global READELF
    global READELFFLAGS
    global readelf_size
    global srcdir
    global subdir
    
    send_log "exec $READELF $READELFFLAGS $options $binary_file > readelf.out\n"
    catch "exec $READELF $READELFFLAGS $options $binary_file > readelf.out" got

    foreach xfail $xfails {
	setup_xfail $xfail
    }

    if ![string match "" $got] then {
	fail "readelf $options (reason: unexpected output)"
	send_log $got
	send_log "\n"
	return
    }

    set target_machine ""
    if [istarget "mips*-*-*"] then {
	if { [istarget "mips*-*-*linux*"] } then {
	    set target_machine tmips
	} else {
	    set target_machine mips
	}
    }

    if { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$readelf_size-$target_machine] } then {
	set regexp_file $regexp_file-$readelf_size-$target_machine
    } elseif { $target_machine != "" && [file exists $srcdir/$subdir/$regexp_file-$target_machine] } then {
	set regexp_file $regexp_file-$target_machine
    } elseif { [file exists $srcdir/$subdir/$regexp_file-$readelf_size] } then {
	set regexp_file $regexp_file-$readelf_size
    }

    if { [regexp_diff readelf.out $srcdir/$subdir/$regexp_file] } then {
	fail "readelf $options"
	verbose "output is \n[file_contents readelf.out]" 2
	return
    }

    pass "readelf $options"
}

# Simple proc to skip certain expected warning messages.

proc prune_readelf_wi_warnings { text } {
    regsub -all "(^|\n)(.*Skipping unexpected symbol type.*)" $text "\\1" text
    return $text
}

# Testing the "readelf -wi" option is difficult because there
# is no guaranteed order to the output, and because some ports
# will use indirect string references, whilst others will use
# direct references.  So instead of having an expected output
# file, like the other readelf tests, we grep for strings that
# really ought to be there.

proc readelf_wi_test {} {
    global READELF
    global READELFFLAGS
    global srcdir
    global subdir
    
    # Compile the second test file.
    if { [target_compile $srcdir/$subdir/testprog.c tmpdir/testprog.o object debug] != "" } {
	verbose "Unable to compile test file."
	untested "readelf -wi"
	return
    }

    # Download it.
    set tempfile [remote_download host tmpdir/testprog.o]

    # Run "readelf -wi" on it.
    send_log "exec $READELF $READELFFLAGS -wi $tempfile > readelf.out\n"
    catch "exec $READELF $READELFFLAGS -wi $tempfile > readelf.out" got

    # Upload the results.
    set output [remote_upload host readelf.out]

    file_on_host delete $tempfile
    
    # Strip any superflous warnings.
    set got [prune_readelf_wi_warnings $got]

    if ![string match "" $got] then {
	fail "readelf $options (reason: unexpected output)"
	send_log $got
	send_log "\n"
	return
    }

    if ![file size $output] then {
	# If the output file is empty, then this target does not
	# generate dwarf2 output.  This is not a failure.
	verbose "No output from 'readelf -wi'"
	untested "readelf -wi"
	return
    }
    
    # Search for strings that should be in the output.
    set sought {
	".*DW_TAG_compile_unit.*"
	".*DW_TAG_subprogram.*"
	".*DW_TAG_base_type.*"
	".*DW_AT_producer.*(GNU C|indirect string).*"
	".*DW_AT_language.*ANSI C.*"
	".*DW_AT_name.*(testprog.c|indirect string).*"
	".*DW_AT_name.*fn.*"
	".*DW_AT_name.*(main|indirect string).*"
	".*\(DW_OP_addr: 0\).*"
    }
    
    foreach looked_for $sought {	
	set lines [grep $output $looked_for]
	if ![llength $lines] then {
	    fail "readelf -wi: missing: $looked_for"
	    send_log readelf.out
	    return
	}
    }

    file_on_host delete $output
    
    # All done.
    pass "readelf -wi"
}


# Exclude non-ELF targets.
if ![is_elf_format] {
    verbose "$READELF is only intended for ELF targets" 2
    return
}

if ![is_remote host] {
    if {[which $READELF] == 0} then {
        perror "$READELF does not exist"
        return
    }
}

send_user "Version [binutil_version $READELF]"

# Assemble the test file.
if {![binutils_assemble $srcdir/$subdir/bintest.s tmpdir/bintest.o]} then {
    perror "unresolved 1"
    unresolved "readelf - failed to assemble"
    return
}

if ![is_remote host] {
    set tempfile tmpdir/bintest.o
} else {
    set tempfile [remote_download host tmpdir/bintest.o]
}

# First, determine the size, so specific output matchers can be used.
readelf_find_size $tempfile

# Run the tests.
readelf_test -h $tempfile readelf.h  {}
readelf_test -S $tempfile readelf.s  {}
readelf_test -s $tempfile readelf.ss {}
readelf_test -r $tempfile readelf.r  {}

readelf_wi_test