1# Copyright 2002-2016 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# relocate.exp -- Expect script to test loading symbols from unrelocated 17# object files. 18 19standard_testfile .c 20append binfile .o 21 22remote_exec build "rm -f ${binfile}" 23if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {debug}] != "" } { 24 untested relocate.exp 25 return -1 26} 27 28proc get_var_address { var } { 29 global gdb_prompt hex 30 31 # Match output like: 32 # $1 = (int *) 0x0 33 # $5 = (int (*)()) 0 34 # $6 = (int (*)()) 0x24 <function_bar> 35 36 gdb_test_multiple "print &${var}" "get address of ${var}" { 37 -re "\\\$\[0-9\]+ = \\(.*\\) (0|$hex)( <${var}>)?\[\r\n\]+${gdb_prompt} $" { 38 pass "get address of ${var}" 39 if { $expect_out(1,string) == "0" } { 40 return "0x0" 41 } else { 42 return $expect_out(1,string) 43 } 44 } 45 } 46 return "" 47} 48 49 50 51gdb_exit 52gdb_start 53gdb_reinitialize_dir $srcdir/$subdir 54 55#Check that invalid options are rejected. 56foreach x {"-raednow" "readnow" "foo" "-readnow s"} { 57 gdb_test "add-symbol-file ${binfile} 0 $x" \ 58 "USAGE: add-symbol-file <filename> <textaddress>.*-readnow.*-s <secname> <addr>.*" \ 59 "add-symbol-file: unknown option $x" 60} 61 62# Load the object file. 63gdb_test "add-symbol-file ${binfile} 0" \ 64 "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \ 65 "add-symbol-file ${testfile}.o 0" \ 66 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x0\[\r\n\]+\\(y or n\\) " \ 67 "y" 68 69# Print the addresses of static variables. 70set static_foo_addr [get_var_address static_foo] 71set static_bar_addr [get_var_address static_bar] 72 73# Make sure they have different addresses. 74if { "${static_foo_addr}" == "${static_bar_addr}" } { 75 fail "static variables have different addresses" 76} else { 77 pass "static variables have different addresses" 78} 79 80# Print the addresses of global variables. 81set global_foo_addr [get_var_address global_foo] 82set global_bar_addr [get_var_address global_bar] 83 84# Make sure they have different addresses. 85if { "${global_foo_addr}" == "${global_bar_addr}" } { 86 fail "global variables have different addresses" 87} else { 88 pass "global variables have different addresses" 89} 90 91# Print the addresses of functions. 92set function_foo_addr [get_var_address function_foo] 93set function_bar_addr [get_var_address function_bar] 94 95# Make sure they have different addresses. 96if { "${function_foo_addr}" == "${function_bar_addr}" } { 97 fail "functions have different addresses" 98} else { 99 pass "functions have different addresses" 100} 101 102# Now use a variable as an offset to add-symbol-file, and check that 103# the functions' addresses change. 104 105gdb_exit 106gdb_start 107gdb_reinitialize_dir $srcdir/$subdir 108 109gdb_test_no_output "set \$offset = 0x10000" 110 111# Load the object file. 112gdb_test "add-symbol-file ${binfile} \$offset" \ 113 "Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \ 114 "add-symbol-file ${testfile}.o \$offset" \ 115 "add symbol table from file \".*${testfile}\\.o\" at\[ \t\r\n\]+\.text_addr = 0x10000\[\r\n\]+\\(y or n\\) " \ 116 "y" 117 118# Print the addresses of functions. 119set new_function_foo_addr [get_var_address function_foo] 120 121# Make sure they have different addresses. 122if { "${function_foo_addr}" == "${new_function_foo_addr}" } { 123 fail "function foo has a different address" 124} else { 125 pass "function foo has a different address" 126} 127 128# Now try loading the object as an exec-file; we should be able to print 129# the values of variables after we do this. 130 131gdb_exit 132gdb_start 133gdb_reinitialize_dir $srcdir/$subdir 134gdb_file_cmd ${binfile} 135 136# Check the values of the variables. 137gdb_test "print static_foo" "\\\$$decimal = 1" 138gdb_test "print static_bar" "\\\$$decimal = 2" 139gdb_test "print global_foo" "\\\$$decimal = 3" 140gdb_test "print global_bar" "\\\$$decimal = 4" 141