1# Copyright 2013-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 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. 18 19if {[is_remote target] || [skip_shlib_tests]} { 20 untested "skipping remote target and shared library tests" 21 return -1 22} 23 24# This is required by gdb_compile_shlib. 25if {[get_compiler_info]} { 26 warning "Could not get compiler info" 27 untested "no compiler info" 28 return 1 29} 30 31# Build "wrong" and "right" versions of the libraries in separate directories. 32set wrong_lib_subdir "solib-search-wrong" 33set right_lib_subdir "solib-search-right" 34 35# First library file. 36set libname1 "solib-search-lib1" 37set srcfile1_lib ${srcdir}/${subdir}/${libname1}.c 38set wrong_binfile1_lib \ 39 [standard_output_file ${wrong_lib_subdir}/${libname1}.so] 40set right_binfile1_lib \ 41 [standard_output_file ${right_lib_subdir}/${libname1}.so] 42# Second library file. 43set libname2 "solib-search-lib2" 44set srcfile2_lib ${srcdir}/${subdir}/${libname2}.c 45set wrong_binfile2_lib \ 46 [standard_output_file ${wrong_lib_subdir}/${libname2}.so] 47set right_binfile2_lib \ 48 [standard_output_file ${right_lib_subdir}/${libname2}.so] 49# Link with the library that lives here. 50# This is so that we can replace what gdb sees with the wrong copy, 51# and then tell gdb to use the right copy that lives someplace else. 52set binfile1_lib [standard_output_file ${libname1}.so] 53set binfile2_lib [standard_output_file ${libname2}.so] 54 55set lib_flags [list debug ldflags=-Wl,-Bsymbolic] 56set wrong_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=1" 57set right_lib_flags "$lib_flags additional_flags=-DARRAY_SIZE=8192 -DRIGHT" 58 59# Binary file. 60standard_testfile .c 61set bin_flags [list debug shlib=${binfile1_lib} shlib=${binfile2_lib}] 62 63remote_exec build "rm -rf [standard_output_file ${wrong_lib_subdir}]" 64remote_exec build "rm -rf [standard_output_file ${right_lib_subdir}]" 65remote_exec build "mkdir [standard_output_file ${wrong_lib_subdir}]" 66remote_exec build "mkdir [standard_output_file ${right_lib_subdir}]" 67 68if { [gdb_compile_shlib ${srcfile1_lib} ${wrong_binfile1_lib} $wrong_lib_flags] != "" 69 || [gdb_compile_shlib ${srcfile2_lib} ${wrong_binfile2_lib} $wrong_lib_flags] != "" 70 || [gdb_compile_shlib ${srcfile1_lib} ${right_binfile1_lib} $right_lib_flags] != "" 71 || [gdb_compile_shlib ${srcfile2_lib} ${right_binfile2_lib} $right_lib_flags] != "" } { 72 untested "failed to compile shared library" 73 return -1 74} 75 76# Build the test binary using the right copies of the libraries. 77remote_exec build "ln -sf ${right_lib_subdir}/${libname1}.so ${binfile1_lib}" 78remote_exec build "ln -sf ${right_lib_subdir}/${libname2}.so ${binfile2_lib}" 79if { [gdb_compile $srcdir/$subdir/${srcfile} ${binfile} \ 80 executable $bin_flags] != "" } { 81 untested "failed to compile" 82 return -1 83} 84 85clean_restart $testfile 86 87if { ![runto_main] } { 88 fail "can't run to main" 89 return 90} 91 92gdb_breakpoint "break_here" 93gdb_continue "break_here" 94 95set corefile [standard_output_file solib-search.core] 96set core_supported [gdb_gcore_cmd "$corefile" "save a corefile"] 97 98if {!$core_supported} { 99 return -1 100} 101 102# Before we restart gdb, replace the libs with the "wrong" ones. 103remote_exec build "ln -sf ${wrong_lib_subdir}/${libname1}.so ${binfile1_lib}" 104remote_exec build "ln -sf ${wrong_lib_subdir}/${libname2}.so ${binfile2_lib}" 105 106clean_restart $testfile 107 108set core_loaded [gdb_core_cmd $corefile "re-load generated corefile"] 109if { $core_loaded == -1 } { 110 # No use proceeding from here. 111 return 112} 113 114proc test_backtrace { expect_fail } { 115 global gdb_prompt 116 117 set count 0 118 set total_expected 5 119 120 if { $expect_fail } { 121 set testname "backtrace (with wrong libs)" 122 } else { 123 set testname "backtrace (with right libs)" 124 } 125 # N.B. The order of the tests here is important. 126 # We need to count each function in the backtrace, and expect matches 127 # the first one it finds. 128 gdb_test_multiple "backtrace" $testname { 129 -re "\[^\r\n\]* in lib2_func4 \[^\r\n\]*" { 130 incr count 131 exp_continue 132 } 133 -re "\[^\r\n\]* in lib1_func3 \[^\r\n\]*" { 134 incr count 135 exp_continue 136 } 137 -re "\[^\r\n\]* in lib2_func2 \[^\r\n\]*" { 138 incr count 139 exp_continue 140 } 141 -re "\[^\r\n\]* in lib1_func1 \[^\r\n\]*" { 142 incr count 143 exp_continue 144 } 145 -re "\[^\r\n\]* in main \[^\r\n\]*" { 146 incr count 147 exp_continue 148 } 149 -re "\[\r\n\]$gdb_prompt $" { 150 pass "$testname (data collection)" 151 } 152 } 153 154 set fail 0 155 if { $expect_fail } { 156 # If the backtrace output is correct the test isn't sufficiently 157 # testing what it should. 158 if { $count == $total_expected } { 159 set fail 1 160 } 161 } else { 162 if { $count != $total_expected } { 163 set fail 1 164 } 165 } 166 if { $fail } { 167 fail $testname 168 } else { 169 pass $testname 170 } 171} 172 173# Verify the backtrace is messed up. 174test_backtrace 1 175 176# Remove the copies gdb currently sees: the absolute path is encoded in 177# the core file and we want gdb to find the right copies elsewhere. 178remote_exec build "rm -f ${binfile1_lib}" 179remote_exec build "rm -f ${binfile2_lib}" 180# Set solib-search-path to use the correct copies of libraries. 181gdb_test "set solib-search-path [standard_output_file ${right_lib_subdir}]" \ 182 "" \ 183 "set solib-search-path" 184 185# Verify gdb has properly updated the location of the libraries. 186test_backtrace 0 187gdb_test "p lib1_size" " = 8192" 188gdb_test "p lib2_size" " = 8192" 189 190