1 # Copyright 2013-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Test solib-search-path, and in the case of solib-svr4.c whether l_addr_p
17 # is properly reset when the path is changed.
19 require allow_shlib_tests
20 require {!is_remote target}
22 # Build "wrong" and "right" versions of the libraries in separate directories.
23 set wrong_lib_subdir "solib-search-wrong"
24 set right_lib_subdir "solib-search-right"
27 set libname1 "solib-search-lib1"
28 set srcfile1_lib ${srcdir}/${subdir}/${libname1}.c
29 set wrong_binfile1_lib \
30 [standard_output_file ${wrong_lib_subdir}/${libname1}.so]
31 set right_binfile1_lib \
32 [standard_output_file ${right_lib_subdir}/${libname1}.so]
33 # Second library file.
34 set libname2 "solib-search-lib2"
35 set srcfile2_lib ${srcdir}/${subdir}/${libname2}.c
36 set wrong_binfile2_lib \
37 [standard_output_file ${wrong_lib_subdir}/${libname2}.so]
38 set right_binfile2_lib \
39 [standard_output_file ${right_lib_subdir}/${libname2}.so]
40 # Link with the library that lives here.
41 # This is so that we can replace what gdb sees with the wrong copy,
42 # and then tell gdb to use the right copy that lives someplace else.
43 set binfile1_lib [standard_output_file ${libname1}.so]
44 set binfile2_lib [standard_output_file ${libname2}.so]
46 # When this test was written, GDB's ability to track down shared
47 # libraries for a core file based on the build-id much poorer. As GDB
48 # has improved we now need to disable build-ids in order for this test
49 # to function as expected.
50 set lib_flags [list debug no-build-id ldflags=-Wl,-Bsymbolic]
51 set wrong_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=1"
52 set right_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=8192 additional_flags=-DRIGHT"
56 set bin_flags [list debug shlib=${binfile1_lib} shlib=${binfile2_lib}]
58 remote_exec build "rm -rf [standard_output_file ${wrong_lib_subdir}]"
59 remote_exec build "rm -rf [standard_output_file ${right_lib_subdir}]"
60 remote_exec build "mkdir [standard_output_file ${wrong_lib_subdir}]"
61 remote_exec build "mkdir [standard_output_file ${right_lib_subdir}]"
63 if { [gdb_compile_shlib ${srcfile1_lib} ${wrong_binfile1_lib} $wrong_lib_flags] != ""
64 || [gdb_compile_shlib ${srcfile2_lib} ${wrong_binfile2_lib} $wrong_lib_flags] != ""
65 || [gdb_compile_shlib ${srcfile1_lib} ${right_binfile1_lib} $right_lib_flags] != ""
66 || [gdb_compile_shlib ${srcfile2_lib} ${right_binfile2_lib} $right_lib_flags] != "" } {
67 untested "failed to compile shared library"
71 # Build the test binary using the right copies of the libraries.
72 remote_exec build "ln -sf ${right_lib_subdir}/${libname1}.so ${binfile1_lib}"
73 remote_exec build "ln -sf ${right_lib_subdir}/${libname2}.so ${binfile2_lib}"
74 if { [gdb_compile $srcdir/$subdir/${srcfile} ${binfile} \
75 executable $bin_flags] != "" } {
76 untested "failed to compile"
80 clean_restart $testfile
82 if { ![runto_main] } {
86 gdb_breakpoint "break_here"
87 gdb_continue "break_here"
89 set corefile [standard_output_file solib-search.core]
90 set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"]
92 if {!$core_supported} {
96 # Before we restart gdb, replace the libs with the "wrong" ones.
97 remote_exec build "ln -sf ${wrong_lib_subdir}/${libname1}.so ${binfile1_lib}"
98 remote_exec build "ln -sf ${wrong_lib_subdir}/${libname2}.so ${binfile2_lib}"
100 clean_restart $testfile
102 set core_loaded [gdb_core_cmd $corefile "re-load generated corefile"]
103 if { $core_loaded == -1 } {
104 # No use proceeding from here.
108 proc test_backtrace { expect_fail } {
114 if { $expect_fail } {
115 set testname "backtrace, with wrong libs"
117 set testname "backtrace, with right libs"
119 # N.B. The order of the tests here is important.
120 # We need to count each function in the backtrace, and expect matches
121 # the first one it finds.
122 gdb_test_multiple "backtrace" $testname {
123 -re "\[^\r\n\]* in lib2_func4 \[^\r\n\]*" {
127 -re "\[^\r\n\]* in lib1_func3 \[^\r\n\]*" {
131 -re "\[^\r\n\]* in lib2_func2 \[^\r\n\]*" {
135 -re "\[^\r\n\]* in lib1_func1 \[^\r\n\]*" {
139 -re "\[^\r\n\]* in main \[^\r\n\]*" {
143 -re "\[\r\n\]$gdb_prompt $" {
144 pass "$testname, data collection"
149 if { $expect_fail } {
150 # If the backtrace output is correct the test isn't sufficiently
151 # testing what it should.
152 if { $count == $total_expected } {
156 if { $count != $total_expected } {
167 # Verify the backtrace is messed up.
170 # Remove the copies gdb currently sees: the absolute path is encoded in
171 # the core file and we want gdb to find the right copies elsewhere.
172 remote_exec build "rm -f ${binfile1_lib}"
173 remote_exec build "rm -f ${binfile2_lib}"
174 # Set solib-search-path to use the correct copies of libraries.
175 gdb_test "set solib-search-path [standard_output_file ${right_lib_subdir}]" \
177 "set solib-search-path"
179 # Verify gdb has properly updated the location of the libraries.
181 gdb_test "p lib1_size" " = 8192"
182 gdb_test "p lib2_size" " = 8192"