1# Expect script for ld-auto-import tests
2#   Copyright (C) 2002-2016 Free Software Foundation, Inc.
3#
4# This file is part of the GNU Binutils.
5#
6# This program 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# Written by Ralf.Habacker@freenet.de
22# Based on ls-shared/shared.exp by Ian Lance Taylor (ian@cygnus.com)
23#
24
25# Note:
26#
27# This script tests some auto-import functionality:
28#
29#  A. "auto importing direct from a dll" functionality, which dramatically reduces the
30#     linking time for big libraries and applications by skipping creating/using
31#     import libraries. Instead it links directly to the related dll or to a symlinked
32#     dll for replacing regular import libraries. The test has 6 stages:
33#
34#     1. compile and link a test dll exporting some text and data symbols and a
35#     standard import library
36#
37#     2. create a symbolic link to this dll to simulate a replaced import library.
38#
39#     3. compile and link a client application with the standard import library.
40#     This should produce no errors.
41#
42#     4. compile and link a client application with the created dll.
43#     This should also produce no errors.
44#
45#     5. compile and link a client application using the "import library".
46#     This should also produce no errors.
47#
48#     6. compile and link a client application with auto-import disabled.
49#     This should produce a linking error.
50#
51# B. runtime check if there are no segfaults when importing const data variables
52#
53
54# This test can only be run if ld generates native executables.
55if ![isnative] then {return}
56
57# This test can only be run on a couple of platforms.
58# Square bracket expressions seem to confuse istarget.
59if { ![istarget *-pc-cygwin]
60     && ![istarget *-pc-mingw*] } {
61    return
62}
63
64if [istarget *-pc-mingw*] {
65    # FIXME: Add support for this target.
66    unsupported "mingw currently not supported"
67}
68
69# No compiler, no test.
70if { [which $CC] == 0 } {
71    untested "Auto import test (compiler not found)"
72    return
73}
74
75# ld_special_link
76#	link a program using ld, without including any libraries
77#
78proc ld_special_link { ld target objects } {
79    global host_triplet
80    global link_output
81
82    if { [which $ld] == 0 } then {
83	perror "$ld does not exist"
84	return 0
85    }
86
87    if [is_endian_output_format $objects] then {
88	set flags [big_or_little_endian]
89    } else {
90	set flags ""
91    }
92
93    verbose -log "$ld $flags -o $target $objects"
94
95    catch "exec $ld $flags -o $target $objects" link_output
96    set exec_output [prune_warnings $link_output]
97
98    # We don't care if we get a warning about a non-existent start
99    # symbol, since the default linker script might use ENTRY.
100    regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
101
102    # We don't care if we get a message about creating a library file.
103    regsub -all "(^|\n)(Creating library file\[^\n\]*\n?)" $exec_output "\\1" exec_output
104
105    if [string match "" $exec_output] then {
106	return 1
107    }
108
109    verbose -log "$exec_output"
110    return 0
111}
112
113set tmpdir tmpdir
114set SHCFLAG ""
115
116if [istarget *-pc-cygwin] {
117    # Set some libs needed for cygwin.
118    set MYLIBS "-L/usr/lib -lcygwin -L/usr/lib/w32api -lkernel32"
119
120    # Compile the dll.
121    if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/dll.c $tmpdir/dll.o] {
122	fail "compiling shared lib"
123    }
124    if ![ld_special_link "$ld -shared --enable-auto-import -e __cygwin_dll_entry@12 --out-implib=$tmpdir/libstandard.dll.a" $tmpdir/dll.dll "$tmpdir/dll.o $MYLIBS"] {
125	fail "linking shared lib"
126    }
127
128    # Create symbolic link.
129    catch "exec ln -fs dll.dll $tmpdir/libsymlinked_dll.dll.a" ln_catch
130
131    # Compile and link the client program.
132    if ![ld_compile "$CC $CFLAGS $SHCFLAG" $srcdir/$subdir/client.c $tmpdir/client.o] {
133        fail "compiling client"
134    }
135
136    # Check linking with import library.
137    set msg "linking auto-import client using a standard import library"
138    if [ld_special_link $ld $tmpdir/client-linklib.exe "--enable-auto-import --enable-runtime-pseudo-reloc /lib/crt0.o $tmpdir/client.o -L$tmpdir -lstandard $MYLIBS"] {
139	pass $msg
140    } else {
141	fail $msg
142    }
143
144    # Check linking directly with dll.
145    set msg "linking auto-import client using the dll"
146    if [ld_special_link $ld $tmpdir/client-linkdll.exe "--enable-auto-import --enable-runtime-pseudo-reloc /lib/crt0.o $tmpdir/client.o -L$tmpdir -ldll $MYLIBS"] {
147	pass $msg
148    } else {
149	fail $msg
150    }
151
152    # Check linking with symlinked dll.
153    set msg "linking auto-import client using symbolic linked dll"
154    if [ld_special_link $ld $tmpdir/client-symlinkeddll.exe "--enable-auto-import --enable-runtime-pseudo-reloc /lib/crt0.o $tmpdir/client.o -L$tmpdir -lsymlinked_dll $MYLIBS"] {
155	pass $msg
156    } else {
157	fail $msg
158    }
159
160    # Check linking with disabled auto-import, this must produce linking error.
161    set msg "linking with disabled auto-import"
162    if ![ld_special_link $ld $tmpdir/client-failed.exe "--disable-auto-import --enable-runtime-pseudo-reloc /lib/crt0.o $tmpdir/client.o -L$tmpdir -ldll $MYLIBS"] {
163	pass $msg
164    } else {
165	fail $msg
166    }
167
168    # Check that the app works - ie that there is output when the applications runs.
169    set msg "application runtime segfault check"
170    catch "exec $tmpdir/client-linklib.exe" exec_output
171    if ![string match "" $exec_output] then {
172    	pass $msg
173    } else {
174    	fail $msg
175    }
176}
177