1# Copyright 2012-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 -1
18}
19
20proc test {hidden dlopen version_id_main lang} {
21    global srcdir subdir
22
23    set main "print-file-var-main"
24
25    set suffix "-hidden$hidden-dlopen$dlopen-version_id_main$version_id_main"
26
27    # Normally we place different builds (C/C++) of a test into
28    # subdirectories within the test build directory, however, using
29    # gdb_load_shlib doesn't work well with this approach, so instead
30    # add a language specific suffix to the binary and library names.
31    if { $lang == "c" } {
32	set suffix "${suffix}_c"
33    } else {
34	set suffix "${suffix}_cpp"
35    }
36
37    set executable $main$suffix
38
39    set lib1 "print-file-var-lib1"
40    set lib2 "print-file-var-lib2"
41
42    set libobj1 [standard_output_file ${lib1}$suffix.so]
43    set libobj2 [standard_output_file ${lib2}$suffix.so]
44
45    set lib_opts { debug additional_flags=-fPIC $lang }
46    lappend lib_opts "additional_flags=-DHIDDEN=$hidden"
47
48    if { [gdb_compile_shlib ${srcdir}/${subdir}/${lib1}.c \
49	      ${libobj1} \
50	      ${lib_opts} ] != "" } {
51	return -1
52    }
53    if { [gdb_compile_shlib ${srcdir}/${subdir}/${lib2}.c \
54	      ${libobj2} \
55	      ${lib_opts} ] != "" } {
56	return -1
57    }
58
59    set main_opts [list debug shlib=${libobj1} $lang]
60
61    if {$dlopen} {
62	lappend main_opts "shlib_load" \
63	    "additional_flags=-DSHLIB_NAME=\"$libobj2\""
64    } else {
65	lappend main_opts "shlib=${libobj2}"
66    }
67
68    lappend main_opts "additional_flags=-DVERSION_ID_MAIN=$version_id_main"
69
70    if { [gdb_compile "${srcdir}/${subdir}/${main}.c" \
71	      [standard_output_file ${executable}] \
72	      executable \
73	      $main_opts]
74	 != ""} {
75	return -1
76    }
77
78    clean_restart $executable
79    gdb_load_shlib $libobj1
80    gdb_load_shlib $libobj2
81
82    if ![runto_main] {
83	untested "could not run to main"
84	return -1
85    }
86
87    # Try printing "this_version_num" qualified with the name of the file
88    # where the variables are defined.  There are three global variables
89    # with that name, and some systems such as GNU/Linux merge them
90    # into one single entity, while some other systems such as Windows
91    # keep them separate.  In the first situation, we have to verify
92    # that GDB does not randomly select the wrong instance, even when
93    # a specific filename is used to qualified the lookup.  And in the
94    # second case, we have to verify that GDB does select the instance
95    # defined in the given filename.
96    #
97    # To avoid adding target-specific code in this testcase, the program
98    # sets three local variables named 'vm', 'v1' and 'v2' with the value of
99    # our global variables.  This allows us to compare the value that
100    # GDB returns for each query against the actual value seen by
101    # the program itself.
102
103    # Get past the initialization of the v* variables.
104
105    set bp_location \
106	[gdb_get_line_number "STOP" "${main}.c"]
107    gdb_test "break $main.c:$bp_location" \
108	"Breakpoint \[0-9\]+ at 0x\[0-9a-fA-F\]+: .*" \
109	"breakpoint at STOP marker"
110
111    gdb_test "continue" \
112	"Breakpoint \[0-9\]+, main \\(\\) at.*STOP.*" \
113	"continue to STOP marker"
114
115    # Now check the value of this_version_id in all of
116    # print-file-var-main.c, print-file-var-lib1.c and
117    # print-file-var-lib2.c.
118
119    # Compare the values of $sym1 and $sym2.
120    proc compare {sym1 sym2} {
121	with_test_prefix "sym1=$sym1,sym2=$sym2" {
122	    # Done this way instead of comparing the symbols with "print $sym1
123	    # == sym2" in GDB directly so that the values of the symbols end
124	    # up visible in the logs, for debug purposes.
125	    set vsym1 [get_integer_valueof $sym1 -1]
126	    set vsym2 [get_integer_valueof $sym2 -1]
127	    gdb_assert {$vsym1 == $vsym2} "$sym1 == $sym2"
128	}
129    }
130
131    if $version_id_main {
132	compare "'print-file-var-main.c'::this_version_id" "vm"
133	compare "this_version_id" "vm"
134    }
135
136    compare "'print-file-var-lib1.c'::this_version_id" "v1"
137    compare "'print-file-var-lib2.c'::this_version_id" "v2"
138}
139
140# Only test C++ if we are able.  Always use C.
141if { [skip_cplus_tests] || [get_compiler_info "c++"] } {
142    set lang_list {c}
143} else {
144    set lang_list {c c++}
145}
146
147foreach_with_prefix lang $lang_list {
148    foreach_with_prefix hidden {0 1} {
149	foreach_with_prefix dlopen {0 1} {
150	    foreach_with_prefix version_id_main {0 1} {
151		test $hidden $dlopen $version_id_main $lang
152	    }
153	}
154    }
155}
156