1# Copyright 2019-2020 Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16if {[skip_shlib_tests]} {
17    return 0
18}
19
20load_lib "ada.exp"
21
22if { [skip_ada_tests] } { return -1 }
23
24standard_ada_testfile foo
25
26set ofile ${binfile}.o
27
28set srcfile2 [file join [file dirname $srcfile] some_package.adb]
29set ofile2 [standard_output_file some_package.o]
30set sofile [standard_output_file libsome_package.so]
31
32set outdir [file dirname $binfile]
33
34# To make an Ada shared library we have to jump through a number of
35# hoops.
36
37# First compile to a .o.  We can't compile directly to a .so because
38# GCC rejects that:
39#    $ gcc -g -shared -fPIC -o qqz.o some_package.adb
40#    gcc: error: -c or -S required for Ada
41# And, we can't compile in "ada" mode because dejagnu will try to
42# invoke gnatmake, which we don't want.
43if {[target_compile_ada_from_dir $outdir $srcfile2 $ofile2 \
44	 object {debug additional_flags=-fPIC}] != ""} {
45    return -1
46}
47
48# Now turn the .o into a shared library.
49if {[gdb_compile_shlib $ofile2 $sofile \
50	 {debug additional_flags=-fPIC}] != ""} {
51    return -1
52}
53
54# Now we can compile the main program to an object file; but again, we
55# can't compile directly using gnatmake.
56if {[target_compile_ada_from_dir $outdir $srcfile $ofile object debug] != ""} {
57    return -1
58}
59
60set gnatbind [find_ada_tool gnatbind]
61set gnatlink [find_ada_tool gnatlink]
62
63with_cwd $outdir {
64    # Test if gnatbind is supported
65    set status [remote_exec host "$gnatbind --version"]
66    if {[lindex $status 0] == -1} {
67	unsupported "gnatbind foo"
68	return -1
69    }
70    # Bind.
71    set status [remote_exec host "$gnatbind -shared foo"]
72    if {[lindex $status 0] == 0} {
73	pass "gnatbind foo"
74    } else {
75	fail "gnatbind foo"
76	return -1
77    }
78
79    # Test if gnatlink is supported
80    set status [remote_exec host "$gnatlink --version"]
81    if {[lindex $status 0] == -1} {
82	unsupported "gnatlink foo"
83	return -1
84    }
85    # Finally, link.
86    if {[istarget "*-*-mingw*"]
87	|| [istarget *-*-cygwin*]
88	|| [istarget *-*-pe*]
89	|| [istarget arm*-*-symbianelf*]} {
90	# Do not need anything.
91	set linkarg ""
92    } elseif {[istarget *-*-freebsd*] || [istarget *-*-openbsd*]} {
93	set linkarg "-Wl,-rpath,$outdir"
94    } else {
95	set linkarg "-Wl,-rpath,\\\$ORIGIN"
96    }
97    set status [remote_exec host "$gnatlink foo $linkarg -Wl,-lsome_package"]
98    if {[lindex $status 0] == 0} {
99	pass "gnatlink foo"
100    } else {
101	fail "gnatlink foo"
102	return -1
103    }
104}
105
106clean_restart ${testfile}
107
108if {![runto_main]} then {
109   return 0
110}
111
112set can_catch_exceptions 0
113gdb_test_multiple "catch exception some_kind_of_error" "" {
114    -re "Catchpoint \[0-9\]+: `some_kind_of_error' Ada exception\r\n$gdb_prompt $" {
115	pass $gdb_test_name
116	set can_catch_exceptions 1
117    }
118
119    -re "Your Ada runtime appears to be missing some debugging information.\r\nCannot insert Ada exception catchpoint in this configuration.\r\n$gdb_prompt $" {
120	    unsupported $gdb_test_name
121    }
122}
123
124if { $can_catch_exceptions } {
125    gdb_test "cont" \
126	"Catchpoint \[0-9\]+, .* at .*foo\.adb:\[0-9\]+.*" \
127	"caught the exception"
128
129    gdb_test "print \$_ada_exception = some_package.some_kind_of_error'Address" \
130	" = true"
131}
132