aboutsummaryrefslogtreecommitdiff
path: root/ld/testsuite/ld-empic/empic.exp
blob: f72b1f36c1c5d23a33dbddc473cf694fd96e115f (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
# Expect script for ld-empic tests
#   Copyright 1994, 1995, 1996 Free Software Foundation, Inc.
#
# This file 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Written by Ian Lance Taylor (ian@cygnus.com)
#

# Test the handling of MIPS embedded PIC code.  This test essentially
# tests the compiler and assembler as well as the linker, since MIPS
# embedded PIC is a GNU enhancement to standard MIPS tools.

# Embedded PIC is only supported for MIPS ECOFF targets.
if ![istarget mips*-*-ecoff*] {
    return
}

set testname relax

if { [which $CC] == 0 } {
    untested $testname
    return
}

# Test that relaxation works correctly.  This testsuite was composed
# (by experimentation) to force the linker to relax twice--that is,
# the first relaxation pass will force another call to be out of
# range, requiring a second relaxation pass.
if { ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/relax1.c tmpdir/relax1.o]
     || ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/relax2.c tmpdir/relax2.o]
     || ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/relax3.c tmpdir/relax3.o]
     || ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/relax4.c tmpdir/relax4.o] } {
    unresolved $testname
    return
}

if ![ld_simple_link $ld tmpdir/relax "--relax -T $srcdir/$subdir/relax.t tmpdir/relax1.o tmpdir/relax2.o tmpdir/relax3.o tmpdir/relax4.o"] {
    fail $testname
} else {
    # Check that the relaxation produced the correct result.  Check
    # each bal instruction.  Some will go directly to the start of a
    # function, which is OK.  Some will form part of the five
    # instruction expanded call sequence, in which case we compute the
    # real destination and make sure it is the start of a function.
    # Some bal instructions are used to locate the start of the
    # function in order to do position independent addressing into the
    # text section, in which case we just check that it correctly
    # computes the start of the function.

    # Get the symbol table.
    if ![ld_nm $nm "" tmpdir/relax] {
	unresolved $testname
	return
    }

    # Get a disassembly.
    send_log "$objdump -d tmpdir/relax >tmpdir/relax.dis\n"
    verbose "$objdump -d tmpdir/relax >tmpdir/relax.dis"
    catch "exec $objdump -d tmpdir/relax >tmpdir/relax.dis" exec_output
    if ![string match "" $exec_output] {
	send_log "$exec_output\n"
	verbose $exec_output
	unresolved $testname
	return
    }

    set balcnt 0
    set file [open tmpdir/relax.dis r]
    while { [gets $file line] != -1 } {
	verbose "$line" 2

	if ![string match "*bal*" $line] {
	    continue
	}

	verbose "$line"

	incr balcnt

	if ![regexp "^(\[0-9a-fA-F\]+) (<\[a-z+0-9A-Z.\]+>)? bal (\[0-9a-fA-F\]+)" $line whole addr label dest] {
	    perror "unrecognized format for $line"
	    unresolved $testname
	    return
	}

	if "0x$addr + 8 != 0x$dest" {
	    # This is a straight function call.  All function calls in
	    # this example are to either foo or bar.
	    if "0x$dest != $nm_output(foo) && 0x$dest != $nm_output(bar)" {
		send_log "fail 1\n"
		send_log "$line\n"
		fail $testname
		return
	    }
	} else {
	    # Pick up the next line.  If it is sll, this is a switch
	    # prologue, and there is not much we can do to test it.
	    # Otherwise, it should be lui, and the next instruction
	    # should be an addiu, followed by an addu to $31.
	    if { [gets $file l] == -1 } {
		send_log "fail 2\n"
		send_log "$line\n"
		fail $testname
		return
	    }
	    verbose $l

	    if [string match "*sll*" $l] {
		continue
	    }
	    if ![regexp "lui (\[\$a-z0-9\]+),(\[0-9a-fA-Fx\]+)" $l whole reg upper] {
		send_log "fail 3\n"
		send_log "$line\n"
		send_log "$l\n"
		fail $testname
		return
	    }

	    if { [gets $file l] == -1 } {
		send_log "fail 4\n"
		send_log "$line\n"
		fail $testname
		return
	    }
	    verbose "$l"
	    if ![regexp "addiu \\$reg,\\$reg,(\[-0-9\]+)" $l whole lower] {
		send_log "fail 5\n"
		send_log "$line\n"
		send_log "$l\n"
		send_log "addiu \\$reg,\\$reg,(\[-0-9\]+)\n"
		fail $testname
		return
	    }

	    if { [gets $file l] == -1 } {
		send_log "fail 6\n"
		send_log "$line\n"
		fail $testname
		return
	    }
	    verbose "$l"
	    if ![regexp "addu \\$reg,\\$reg,\\\$ra" $l] {
		send_log "fail 7\n"
		send_log "$line\n"
		send_log "$l\n"
		fail $testname
		return
	    }

	    # The next line will be jalr in the case of an expanded
	    # call.  Otherwise, the code is getting the start of the
	    # function, and the next line can be anything.

	    if { [gets $file l] == -1 } {
		send_log "fail 8\n"
		send_log "$line\n"
		fail $testname
		return
	    }
	    verbose "$l"	
	    if [string match "*jalr*" $l] {
		set dest [expr 0x$addr + 8 + ($upper << 16) + $lower]
		if { $dest != $nm_output(foo) && $dest != $nm_output(bar) } {
		    send_log "fail 9\n"
		    send_log "$line\n"
		    fail $testname
		    return
		}
	    } else {
		set dest [expr ($upper << 16) + $lower]
		if ![regexp "<(\[.a-z\]+)\\+(\[0-9a-fA-F\]+)>" $label whole base offset] {
		    send_log "fail 10\n"
		    send_log "$line\n"
		    fail $testname
		    return
		}
		set offset 0x$offset
		if { $base == ".foo" } {
		    set offset [expr $offset - ($nm_output(foo) - 0x30)]
		}
		if { $offset + 8 != - $dest } {
		    send_log "fail 11\n"
		    send_log "$line\n"
		    fail $testname
		    return
		}
	    }
	}
    }

    close $file

    if {$balcnt < 10} {
	send_log "fail 12\n"
	fail $testname
    } else {
	verbose "$balcnt bal instructions"
	pass $testname
    }
}

# We now test actually running embedded MIPS PIC code.  This can only
# be done on a MIPS host with the same endianness as our target.
if [istarget mipsel-*-*] {
    if ![ishost mips*-*-ultrix*] {
	return
    }
} else {
    if ![ishost mips*-*-irix*] {
	return
    }
}

set testname "run embedded PIC code"

# Compile the program which will run the test.  This code must be
# compiled for the host, not the target.
send_log "$CC_FOR_HOST $CFLAGS_FOR_HOST -o tmpdir/run $srcdir/$subdir/run.c\n"
verbose "$CC_FOR_HOST $CFLAGS_FOR_HOST -o tmpdir/run $srcdir/$subdir/run.c"
catch "exec $CC_FOR_HOST $CFLAGS_FOR_HOST -o tmpdir/run $srcdir/$subdir/run.c" exec_output
if ![string match "" $exec_output] {
    send_log "$exec_output\n"
    verbose "$exec_output"
    unresolved $testname
    return
}

# Compile and link the test.
if { ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/runtesti.s tmpdir/runtesti.o]
     || ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/runtest1.c tmpdir/runtest1.o]
     || ![ld_compile "$CC $CFLAGS -membedded-pic" $srcdir/$subdir/runtest2.c tmpdir/runtest2.o] } {
    unresolved $testname
    return
}
if ![ld_simple_link $ld tmpdir/runtest "--embedded-relocs tmpdir/runtesti.o tmpdir/runtest1.o tmpdir/runtest2.o"] {
    fail $testname
} else {
    # Now run the test.
    send_log "tmpdir/run tmpdir/runtest\n"
    verbose "tmpdir/run tmpdir/runtest"
    catch "exec tmpdir/run tmpdir/runtest" exec_output
    if [string match "*ran and returned 0*" $exec_output] {
	send_log "$exec_output\n"
	verbose "$exec_output"
	pass $testname
    } else {
	send_log "$exec_output\n"
	verbose "$exec_output"
	fail $testname
    }
}