1# Expect script for export table in executables tests
2#   Copyright (C) 2003-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 Fabrizio Gennari <fabrizio.ge@tiscalinet.it>
22# Based on auto-import.exp  by Ralf.Habacker@freenet.de
23#
24
25# This test can only be run on a cygwin platforms.
26if {![istarget *-pc-cygwin]} {
27    verbose "Not a cygwin target."
28    return
29}
30
31# No compiler, no test.
32if { [which $CC] == 0 } {
33    untested "Exe export test (no compiler available)"
34    return
35}
36
37proc run_dlltool { lib_file def_file } {
38    global dlltool
39    global base_dir
40    global as
41
42    if ![info exists dlltool] then {
43	set dlltool [findfile $base_dir/../binutils/dlltool]
44    }
45
46    if { [which $dlltool] == 0 } then {
47	verbose "$dlltool does not exist"
48	return 0
49    }
50
51    verbose "$dlltool --as $as -l $lib_file -d $def_file"
52    catch "exec $dlltool --as $as -l $lib_file -d $def_file" dlltool_output
53
54    #remove empty lines
55    regsub -all "\n+" $dlltool_output "" dlltool_output
56
57    if [string match "" $dlltool_output] then {
58        return 1
59    }
60
61    verbose -log "$dlltool_output"
62    return 0
63}
64
65# ld_special_link
66#       A copy of ld_simple_link (from ld-lib.exp) with extra
67#       code to strip warnings about creating libraries.
68#
69proc ld_special_link { ld target objects } {
70    global host_triplet
71    global link_output
72
73    if { [which $ld] == 0 } then {
74	verbose "$ld does not exist"
75	return 0
76    }
77
78    if [is_endian_output_format $objects] then {
79        set flags [big_or_little_endian]
80    } else {
81        set flags ""
82    }
83
84    verbose -log "$ld $flags -o $target $objects"
85    catch "exec $ld $flags -o $target $objects" link_output
86
87    set exec_output [prune_warnings $link_output]
88
89    # We don't care if we get a warning about a non-existent start
90    # symbol, since the default linker script might use ENTRY.
91    regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
92
93    # We don't care if we get a message about creating a library file.
94    regsub -all "(^|\n)(Creating library file\[^\n\]*\n?)" $exec_output "\\1" exec_output
95
96    if [string match "" $exec_output] then {
97        return 1
98    }
99
100    verbose -log "$exec_output"
101    return 0
102}
103
104set tmpdir tmpdir
105
106# Set some libs needed for cygwin.
107set MYLDFLAGS "-Wl,--out-implib,$tmpdir/testexe.lib -nostartfiles -nostdlib"
108
109# Build an export library for testdll
110if ![run_dlltool $tmpdir/testdll.lib $srcdir/$subdir/testdll.def] {
111    fail "building an export library for the shared lib"
112    return
113}
114
115# Compile the executable.
116if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/testexe.c $tmpdir/testexe.o] {
117    fail "compiling executable"
118    return
119}
120
121if ![ld_special_link "$CC $LDFLAGS $MYLDFLAGS -e _testexe_main@16" $tmpdir/testexe.exe "$tmpdir/testexe.o $srcdir/$subdir/testexe.def $tmpdir/testdll.lib -lkernel32"] {
122    fail "linking executable"
123    return
124}
125
126# Compile the dll.
127if ![ld_compile "$CC $CFLAGS" $srcdir/$subdir/testdll.c $tmpdir/testdll.o] {
128    fail "compiling shared lib"
129    return
130}
131
132if ![ld_special_link "$CC $LDFLAGS -nostartfiles -nostdlib -e _testdll_main@12" $tmpdir/testdll.dll "$tmpdir/testdll.o $srcdir/$subdir/testdll.def $tmpdir/testexe.lib"] {
133    fail "linking shared lib"
134    return
135}
136
137# This is as far as we can go with a cross-compiler
138if ![isnative] then {
139    verbose "Not running natively, so cannot execute binary"
140    pass "Compile and link and executable with an export table"
141    return
142}
143
144verbose -log "executing $tmpdir/testexe.exe"
145catch "exec $tmpdir/testexe.exe" prog_output
146
147set expected ""
148if [string match $expected $prog_output] then {
149    pass "export table in executable"
150} else {
151    verbose $prog_output
152    fail "Output does not match expected string $expected"
153}
154