1# Copyright (C) 2009, 2010, 2011 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
16if {[skip_shlib_tests]} {
17    return 0
18}
19
20set testfile "gnu-ifunc"
21set executable ${testfile}
22set srcfile ${testfile}.c
23set binfile ${objdir}/${subdir}/${executable}
24set staticexecutable ${executable}-static
25set staticbinfile ${objdir}/${subdir}/${staticexecutable}
26
27set libfile "${testfile}-lib"
28set libsrc ${libfile}.c
29set lib_so ${objdir}/${subdir}/${libfile}.so
30# $lib_o must not have {debug}, it would override the STT_GNU_IFUNC ELF markers.
31set lib_o ${objdir}/${subdir}/${libfile}.o
32
33# We need DWARF for the "final" function as we "step" into the function and GDB
34# would step-over the "final" function if there would be no line number debug
35# information (DWARF) available.
36#
37# We must not have DWARF for the "gnu_ifunc" function as DWARF has no way to
38# express the STT_GNU_IFUNC type and it would be considered as a regular
39# function due to DWARF by GDB.  In ELF gnu-ifunc is expressed by the
40# STT_GNU_IFUNC type.
41#
42# Both functions need to be in the same shared library file but
43# gdb_compile_shlib has no way to specify source-specific compilation options.
44#
45# Therefore $libfile contains only the STT_GNU_IFUNC function with no DWARF
46# referencing all the other parts from the main executable with DWARF.
47
48set lib_opts {}
49set exec_opts [list debug shlib=$lib_so]
50
51if [get_compiler_info ${binfile}] {
52    return -1
53}
54
55if { [gdb_compile_shlib ${srcdir}/${subdir}/$libsrc $lib_so $lib_opts] != ""
56     || [gdb_compile ${srcdir}/${subdir}/$srcfile $binfile executable $exec_opts] != ""} {
57    untested "Could not compile dynamic executable $binfile."
58    return -1
59}
60
61# Start with a fresh gdb.
62
63clean_restart $executable
64gdb_load_shlibs ${lib_so}
65
66if ![runto_main] then {
67    fail "Can't run to main"
68    return 1;
69}
70
71# The "if" condition is artifical to test regression of a former patch.
72gdb_breakpoint "[gdb_get_line_number "break-at-nextcall"] if i && gnu_ifunc (i) != 42"
73
74gdb_breakpoint [gdb_get_line_number "break-at-call"]
75gdb_continue_to_breakpoint "break-at-call" ".*break-at-call.*"
76
77# Test GDB will automatically indirect the call.
78
79gdb_test "p gnu_ifunc (3)" " = 4"
80
81# Test GDB will skip the gnu_ifunc resolver on first call.
82
83gdb_test "step" "\r\nfinal .*"
84
85# Test GDB will not break before the final chosen implementation.
86
87# Also test a former patch regression:
88# Continuing.
89# Error in testing breakpoint condition:
90# Attempt to take address of value not located in memory.
91#
92# Breakpoint 2, main () at ./gdb.base/gnu-ifunc.c:33
93
94gdb_test "continue" "Continuing.\r\n\r\nBreakpoint .* (at|in) .*break-at-nextcall.*" \
95	 "continue to break-at-nextcall"
96
97gdb_breakpoint "gnu_ifunc"
98
99gdb_continue_to_breakpoint "nextcall gnu_ifunc"
100
101gdb_test "frame" "#0 +(0x\[0-9a-f\]+ in +)?final \\(.*" "nextcall gnu_ifunc skipped"
102
103
104# Check any commands not doing an inferior call access the address of the
105# STT_GNU_IFUNC resolver, not the target function.
106
107if {[istarget powerpc64-*] && [is_lp64_target]} {
108    # With only minimal symbols GDB provides the function descriptors.  With
109    # full debug info the function code would be displayed.
110    set func_prefix {\.}
111} else {
112    set func_prefix {}
113}
114
115gdb_test "p gnu_ifunc" " = {<text gnu-indirect-function variable, no debug info>} 0x\[0-9a-f\]+ <${func_prefix}gnu_ifunc>" "p gnu_ifunc executing"
116gdb_test "info sym gnu_ifunc" "gnu_ifunc in section .*" "info sym gnu_ifunc executing"
117
118set test "info addr gnu_ifunc"
119gdb_test_multiple $test $test {
120    -re "Symbol \"gnu_ifunc\" is at (0x\[0-9a-f\]+) in .*$gdb_prompt $" {
121	pass $test
122    }
123}
124gdb_test "info sym $expect_out(1,string)" "gnu_ifunc in section .*" "info sym <gnu_ifunc-address>"
125
126
127# Test statically linked ifunc resolving during inferior start.
128# https://bugzilla.redhat.com/show_bug.cgi?id=624967
129
130if ![target_info exists gdb_stub] {
131
132    # Compile $staticbinfile separately as it may exit on error (ld/12595).
133
134    if { [gdb_compile ${srcdir}/${subdir}/$libsrc $lib_o object {}] != ""
135	 || [gdb_compile "${srcdir}/${subdir}/$srcfile $lib_o" $staticbinfile executable {debug}] != "" } {
136	untested "Could not compile static executable $staticbinfile."
137	return -1
138    }
139
140    clean_restart $staticexecutable
141
142    gdb_breakpoint "gnu_ifunc"
143    gdb_breakpoint "main"
144    gdb_run_cmd
145    gdb_test "" "Breakpoint \[0-9\]*, main .*" "static gnu_ifunc"
146}
147