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