1# Copyright 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# Test loading symbols from unrelocated C++ object files.
17
18set testfile cp-relocate
19set srcfile ${testfile}.cc
20set binfile ${objdir}/${subdir}/${testfile}.o
21
22if { [skip_cplus_tests] } { continue }
23
24if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" object {c++ debug}] != "" } {
25     untested cp-relocate.exp
26     return -1
27}
28
29proc get_func_address { func } {
30    global gdb_prompt hex
31
32    set rfunc [string_to_regexp $func]
33    gdb_test_multiple "print '${func}'" "get address of ${func}" {
34	-re "\\\$\[0-9\]+ = \\{.*\\} (0|($hex) <${rfunc}>)\[\r\n\]+${gdb_prompt} $" {
35	    # $1 = {int ()} 0x24 <function_bar>
36	    # But if the function is at zero, the name may be omitted.
37	    pass "get address of ${func}"
38	    if { $expect_out(1,string) == "0" } {
39		return "0x0"
40	    } else {
41		return $expect_out(2,string)
42	    }
43	}
44    }
45  return ""
46}
47
48# Load the file as an executable; GDB should assign non-overlapping
49# section offsets.
50gdb_exit
51gdb_start
52gdb_reinitialize_dir $srcdir/$subdir
53gdb_file_cmd ${binfile}
54
55# Find the interesting functions.  We go to a little effort to find
56# the right function names here, to work around PR c++/40.
57set func1_name ""
58set func2_name ""
59gdb_test_multiple "info functions func<.>" "info functions" {
60    -re "\r\nint (\[^\r\]*func<1>\[^\r]*);" {
61	set func1_name $expect_out(1,string)
62	exp_continue
63    }
64    -re "\r\nint (\[^\r\]*func<2>\[^\r]*);" {
65	set func2_name $expect_out(1,string)
66	exp_continue
67    }
68    -re "$gdb_prompt $" {
69	if { ${func1_name} != "" && ${func2_name} != "" } {
70	    pass "info functions"
71	} else {
72	    fail "info functions"
73	    return -1
74	}
75    }
76}
77
78# Check that all the functions have different addresses.
79set func1_addr [get_func_address "$func1_name"]
80set func2_addr [get_func_address "$func2_name"]
81set caller_addr [get_func_address "caller"]
82
83if { "${func1_addr}" == "${func2_addr}"
84     || "${func1_addr}" == "${func2_addr}"
85     || "${func2_addr}" == "${caller_addr}" } {
86  fail "C++ functions have different addresses"
87} else {
88  pass "C++ functions have different addresses"
89}
90
91# Figure out the names of the sections containing the template
92# functions.
93set func1_sec ""
94set func2_sec ""
95gdb_test_multiple "info file" "info file" {
96    -re "($hex) - ($hex) is (\[^\r\]*)\r" {
97	if { $expect_out(1,string) <= $func1_addr
98	     && $expect_out(2,string) > $func1_addr } {
99	    set func1_sec $expect_out(3,string)
100	} elseif { $expect_out(1,string) <= $func2_addr
101	    && $expect_out(2,string) > $func2_addr } {
102	    set func2_sec $expect_out(3,string)
103	}
104	exp_continue
105    }
106    -re "$gdb_prompt $" {
107	if { ${func1_sec} != "" && ${func2_sec} != "" } {
108	    pass "info file"
109	} else {
110	    fail "info file"
111	    return -1
112	}
113    }
114}
115
116if { $func1_sec == $func2_sec } {
117    untested "cp-relocate.exp - template functions in same sections"
118    return -1
119}
120
121# Now start a clean GDB, for add-symbol-file tests.
122gdb_exit
123gdb_start
124gdb_reinitialize_dir $srcdir/$subdir
125
126gdb_test "add-symbol-file ${binfile} 0 -s ${func1_sec} 0x10000 -s ${func2_sec} 0x20000" \
127	"Reading symbols from .*${testfile}\\.o\\.\\.\\.done\\.(|\r\nUsing host libthread_db library .*libthread_db.so.*\\.)" \
128	"add-symbol-file ${testfile}.o" \
129	"add symbol table from file \".*${testfile}\\.o\" at.*\\(y or n\\) " \
130	"y"
131
132# Make sure the function addresses were updated.
133gdb_test "break *'$func1_name'" \
134    "Breakpoint $decimal at 0x1....: file .*"
135gdb_test "break *'$func2_name'" \
136    "Breakpoint $decimal at 0x2....: file .*"
137