exe-export.exp 4.6 KB

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