1# Expect script for complex PE tests that require a C compiler and the ability
2# to run target executables natively, in addition to the just-built binutils.
3#   Copyright (C) 2006-2017 Free Software Foundation, Inc.
4#
5# This file is part of the GNU Binutils.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20# MA 02110-1301, USA.
21#
22# Based on the script pe-run.exp written by Pedro Alves.
23# Written by Kai Tietz <kai.tietz@onevision.com>
24#
25
26# Note:
27#
28# This test checks the "direct linking to a dll" functionality with stdcall
29# and fastcall fixup.
30#
31# The test has 7 stages:
32#
33# 1. compile and link a test dll with ".dll" extension.
34#
35# 2. compile and link a test dll with ".sl" (i.e. != ".dll") extension.
36#
37# 3. compile and link a client application linking directly to the ".dll" dll built in 1.
38#    This should produce no errors.
39#
40# 4. compile and link a client application linking directly to the ".sl" dll built in 2.
41#    This should produce no errors.
42#
43# 5. compile and link a client application linking directly to a symlink into
44#    the ".dll" dll built in 1.
45#    This should produce no errors.
46#
47# 6. compile and link a client application linking directly to a symlink into
48#    the ".sl" dll built in 1.
49#    This should produce no errors.
50#
51# 7. run the produced executables
52
53# This test can only be run on PE/COFF platforms.
54if {![is_pecoff_format]} {
55    return
56}
57
58# No compiler, no test.
59if { [which $CC] == 0 } {
60    untested "Direct linking to dll fastcall/stdcall test"
61    return
62}
63
64set tmpdir tmpdir
65
66proc test_direct2_link_dll {} {
67    global CC
68    global CFLAGS
69    global srcdir
70    global subdir
71    global tmpdir
72
73    # Compile the dll.
74    if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct2_dll.c $tmpdir/direct2_dll.o ] {
75	fail "compiling shared lib fastcall/stdcall"
76    } elseif ![ld_simple_link "$CC -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.dll "$tmpdir/direct2_dll.o" ] {
77	fail "linking shared lib (.dll) fastcall/stdcall"
78    } elseif ![ld_simple_link "$CC -shared -Wl,--enable-stdcall-fixup -Wl,--kill-at " $tmpdir/direct2_dll.sl "$tmpdir/direct2_dll.o" ] {
79	fail "linking shared lib (.sl) fastcall/stdcall"
80    } else {
81	# Compile and link the client program.
82	if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/direct2_client.c $tmpdir/direct2_client.o ] {
83	    fail "compiling client fastcall/stdcall"
84	} else {
85	    # Check linking directly to direct2_dll.dll.
86	    set msg "linking client (.dll) fastcall/stdcall"
87	    if [ld_simple_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_dll.exe \
88	      "$tmpdir/direct2_client.o $tmpdir/direct2_dll.dll" ] {
89		pass $msg
90	    } else {
91		fail $msg
92	    }
93
94	    # Check linking directly to direct2_dll.sl.
95	    set msg "linking client (.sl) fastcall/stdcall"
96	    if [ld_simple_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_sl.exe \
97	      "$tmpdir/direct2_client.o $tmpdir/direct2_dll.sl" ] {
98		pass $msg
99	    } else {
100		fail $msg
101	    }
102
103	    # Check dll direct linking through symlink to .dll.
104	    # Create symbolic link.
105	    catch "exec ln -fs direct2_dll.dll $tmpdir/libdirect2_dll.dll.a" ln_catch
106	    set msg "linking client (symlink -> .dll) fastcall/stdcall"
107	    if [ld_simple_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_dll.exe \
108	      "$tmpdir/direct2_client.o $tmpdir/libdirect2_dll.dll.a" ] {
109	        pass $msg
110	    } else {
111		fail $msg
112	    }
113
114	    # Check dll direct linking through symlink to .sl.
115	    # Create symbolic link.
116	    catch "exec ln -fs direct2_dll.sl $tmpdir/libdirect2_sl.dll.a" ln_catch
117	    set msg "linking client (symlink -> .sl) fastcall/stdcall"
118	    if [ld_simple_link "$CC $CFLAGS -Wl,--enable-stdcall-fixup -Wl,--enable-auto-import" $tmpdir/direct2_client_symlink_sl.exe \
119	      "$tmpdir/direct2_client.o $tmpdir/libdirect2_sl.dll.a" ] {
120		pass $msg
121	    } else {
122		fail $msg
123	    }
124	}
125    }
126}
127
128proc directdll_execute {exe msg} {
129    set expected ""
130    catch "exec $exe" prog_output
131    if [string match $expected $prog_output] then {
132        pass $msg
133    } else {
134        verbose $prog_output
135        fail $msg
136    }
137}
138
139test_direct2_link_dll
140
141# This is as far as we can go with a cross-compiler
142if ![isnative] then {
143    verbose "Not running natively, so cannot execute binaries"
144    return
145}
146
147directdll_execute "$tmpdir/direct2_client_dll.exe" "running direct linked dll (.dll) fastcall/stdcall"
148directdll_execute "$tmpdir/direct2_client_sl.exe" "running direct linked dll (.sl) fastcall/stdcall"
149directdll_execute "$tmpdir/direct2_client_symlink_sl.exe" "running direct linked dll (symlink -> .sl) fastcall/stdcall"
150directdll_execute "$tmpdir/direct2_client_symlink_dll.exe" "running direct linked dll (symlink -> .dll) fastcall/stdcall"
151