1# Copyright (C) 2003, 2005, 2007 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 17# Tests for shared object file relocation. If two shared objects have 18# the same load address (actually, overlapping load spaces), one of 19# them gets relocated at load-time. Check that gdb gets the right 20# values for the debugging and minimal symbols. 21 22if {[skip_shlib_tests]} { 23 return 0 24} 25 26if $tracelevel then { 27 strace $tracelevel 28} 29 30# 31# This file uses shreloc.c, shreloc1.c and shreloc2.c 32# 33 34set prms_id 0 35set bug_id 0 36 37set workdir ${objdir}/${subdir} 38set testfile "shreloc" 39set libfile1 "shreloc1" 40set libfile2 "shreloc2" 41set srcfile $srcdir/$subdir/$testfile.c 42set lib1src $srcdir/$subdir/$libfile1.c 43set lib2src $srcdir/$subdir/$libfile2.c 44set binfile $objdir/$subdir/$testfile 45set lib1_sl $objdir/$subdir/$libfile1.sl 46set lib2_sl $objdir/$subdir/$libfile2.sl 47 48if [get_compiler_info ${binfile}] { 49 return -1 50} 51 52set lib_opts "debug" 53set exec_opts [list debug shlib=$lib1_sl shlib=$lib2_sl] 54 55if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } { 56 lappend lib_opts "ldflags=-Wl,--image-base,0x04000000" 57} 58 59if [test_compiler_info "xlc-*"] { 60 61 # IBM's xlc compiler does not add static variables to the ELF symbol 62 # table by default. We need this option to make the variables show 63 # up in "maint print msymbols". 64 65 lappend lib_opts "additional_flags=-qstatsym" 66 67} 68 69if { [gdb_compile_shlib $lib1src $lib1_sl $lib_opts] != ""} { 70 untested "Could not build $lib1_sl." 71 return -1 72} elseif { [gdb_compile_shlib $lib2src $lib2_sl $lib_opts] != ""} { 73 untested "Could not build $lib1_s2." 74 return -1 75} elseif { [gdb_compile $srcfile $binfile executable $exec_opts] != ""} { 76 untested "Could not build $binfile." 77 return -1 78} 79 80# Start with a fresh gdb. 81 82gdb_exit 83gdb_start 84gdb_reinitialize_dir $srcdir/$subdir 85gdb_load ${workdir}/shreloc 86gdb_load_shlibs $lib1_sl $lib2_sl 87 88# Load up the shared objects 89if ![runto_main] then { 90 fail "Can't run to main" 91 return 0 92} 93 94proc get_var_address { var } { 95 global gdb_prompt hex 96 97 send_gdb "print &${var}\n" 98 # Match output like: 99 # $1 = (int *) 0x0 100 # $5 = (int (*)()) 0 101 # $6 = (int (*)()) 0x24 <function_bar> 102 gdb_expect { 103 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" 104 { 105 pass "get address of ${var}" 106 if { $expect_out(1,string) == "0" } { 107 return "0x0" 108 } else { 109 return $expect_out(1,string) 110 } 111 } 112 -re "${gdb_prompt} $" 113 { fail "get address of ${var} (unknown output)" } 114 timeout 115 { fail "get address of ${var} (timeout)" } 116 } 117 return "" 118} 119 120# 121# Check debugging symbol relocations 122# 123 124# Check extern function for relocation 125set fn_1_addr [get_var_address fn_1] 126set fn_2_addr [get_var_address fn_2] 127 128if { "${fn_1_addr}" == "${fn_2_addr}" } { 129 fail "relocated extern functions have different addresses" 130} else { 131 pass "relocated extern functions have different addresses" 132} 133 134# Check extern var for relocation 135set extern_var_1_addr [get_var_address extern_var_1] 136set extern_var_2_addr [get_var_address extern_var_2] 137 138if { "${extern_var_1_addr}" == "${extern_var_2_addr}" } { 139 fail "relocated extern variables have different addresses" 140} else { 141 pass "relocated extern variables have different addresses" 142} 143 144# Check static var for relocation 145set static_var_1_addr [get_var_address static_var_1] 146set static_var_2_addr [get_var_address static_var_2] 147 148if { "${static_var_1_addr}" == "${static_var_2_addr}" } { 149 fail "relocated static variables have different addresses" 150} else { 151 pass "relocated static variables have different addresses" 152} 153 154# 155# Check minimal symbol relocations 156# 157 158proc send_gdb_discard { command } { 159 # Send a command to gdb and discard output up to the next prompt 160 161 global gdb_prompt 162 163 send_gdb "${command}\n" 164 165 # Discard output 166 gdb_expect { 167 -re ".*\[\r\n]+${gdb_prompt} $" { 168 return 1 169 } 170 timeout { 171 fail "{$command} (timeout)" 172 return 0 173 } 174 } 175} 176 177proc get_msym_addrs { var msymfile } { 178 # Extract the list of values for symbols matching var in the 179 # minimal symbol output file 180 181 global gdb_prompt hex 182 set result "" 183 184 send_gdb "shell grep -E \" ${var}(\[ \t\]+.*)?\$\" ${msymfile}\n" 185 186 while 1 { 187 gdb_expect { 188 -re "\[\[\]\[ 0-9\]+\] . (${hex}) ${var}(\[ \t\]+\[^\r\n\]*)?\[\r\n\]+" { 189 set result [concat $result $expect_out(1,string)] 190 } 191 192 -re "$gdb_prompt $" { 193 pass "get_msym_addrs ${var}" 194 return "${result}" 195 } 196 197 -re "\[^\r\n\]*\[\r\n\]+" { 198 # Skip 199 } 200 201 timeout { 202 fail "get_msym_addrs ${var} (timeout)" 203 return -1 204 } 205 } 206 } 207} 208 209proc check_same {var msymfile} { 210 # Check that the minimal symbol values matching var are the same 211 212 set len [llength [lsort -unique [get_msym_addrs "${var}" "${msymfile}"]]] 213 214 if { $len == 1 } { 215 return 1 216 } else { 217 return 0 218 } 219} 220 221proc check_different {var msymfile} { 222 # Check that the minimal symbol values matching var are different 223 224 set addr_list [lsort [get_msym_addrs "${var}" "${msymfile}"]] 225 set prev "" 226 227 if { [llength ${addr_list}] < 2 } { 228 return 0 229 } 230 231 foreach addr ${addr_list} { 232 if { ${prev} == ${addr} } { 233 return 0 234 } 235 set prev ${addr} 236 } 237 238 return 1 239} 240 241set msymfile "${workdir}/shreloc.txt" 242 243if [send_gdb_discard "maint print msymbols ${msymfile}"] { 244 if {[check_different "static_var_\[12\]" "${msymfile}"]} { 245 pass "(msymbol) relocated static vars have different addresses" 246 } else { 247 fail "(msymbol) relocated static vars have different addresses" 248 } 249 250 if {[check_different "extern_var_\[12\]" "${msymfile}"]} { 251 pass "(msymbol) relocated extern vars have different addresses" 252 } else { 253 fail "(msymbol) relocated extern vars have different addresses" 254 } 255 256 if {[check_different "fn_\[12\]" "${msymfile}"]} { 257 pass "(msymbol) relocated functions have different addresses" 258 } else { 259 fail "(msymbol) relocated functions have different addresses" 260 } 261} 262 263if {([istarget "*pc-cygwin"] || [istarget "*pc-mingw32"]) } { 264 # 265 # We know the names of some absolute symbols included in the 266 # portable-executable (DLL) format. Check that they didn't get 267 # relocated. 268 # 269 # A better approach would be include absolute symbols via the assembler. 270 # 271 if {[check_same "_minor_os_version__" "${msymfile}"]} { 272 pass "Absolute symbols not relocated" 273 } else { 274 fail "Absolute symbols not relocated" 275 } 276} 277