1 # Copyright 2022-2023 Free Software Foundation, Inc.
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 3 of the License, or
5 # (at your option) any later version.
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # Test stepping through a runtime loader / dynamic linker (RTLD):
17 # While it'd be nice to have a test which steps through an actual
18 # runtime loader / dynamic linker, constructing such a test would be
19 # non-portable; we would need to know implementation details such
20 # as the names of some of the symbols and the order of calls to
21 # various functions that implement the RTLD. So, instead, we'll use a
22 # program which doesn't even pretend to implement this functionality,
23 # but which will instead be invoked in the same fashion (for ELF
24 # binaries anyway) as would be expected for an ELF-based RTLD.
26 # To that end, we have two programs, one which will pretend to be an
27 # RTLD and the other which will be caused to use the pretend RTLD.
29 # When the main program is run, the pretend/fake RTLD is run instead,
30 # due to it being specified as the ELF interpreter for the main
31 # program. Within GDB, we then attempt to do some simple debugging
32 # involving 'step', 'next', and 'finish'.
34 # This test can't be run on targets lacking shared library support
35 # or for non-ELF targets. (We're not really testing or building
36 # shared libraries here, but having a RTLD implies having shared
37 # libraries on the target.)
38 if { [skip_shlib_tests] || ![is_elf_target] } {
42 # (Pretend) RTLD file names and flags:
43 set rtld_basename ${::gdb_test_file_name}-rtld
44 set srcfile_rtld ${srcdir}/${subdir}/${rtld_basename}.c
45 set binfile_rtld [standard_output_file ${rtld_basename}]
47 # Placing 'pie' in the flag list (for rtld_flags) doesn't work, but
48 # using -static-pie -FPIE in additional_flags does. Apparently, when
49 # 'pie' is listed, gdb_compile will (on Linux) use both -fPIE and
50 # -pie. Testing shows that use of -pie creates a dynamically linked
51 # executable when either a static or static-pie executable is desired
52 # instead. (This is probably fragile.)
54 # While developing this code on Fedora Linux, it was found that (only)
55 # the flags -static-pie -fPIE were needed for Fedora 35 through Fedora
56 # 38. The source file rtld-step-rtld.c didn't need the _start()
57 # function either. And, better still, it was possible to call
58 # printf() to output progress messages in the pretend/fake RTLD.
59 # Sadly, these output statements had to be removed in order to obtain
60 # code which would work on other Linux distributions / releases.
62 # When testing against earlier versions of Fedora, RHEL 9, and
63 # also Ubuntu 22.04, that short flag list didn't work. For these
64 # linux releases, it was found that -nostdlib -lc were also required.
65 # Due to the use of -nostdlib, a _start() function had to be added
68 # Finally, on FreeBSD, it was found that in order to end up with a
69 # statically linked executable, -static was also needed.
70 # Unfortunately, when attempting to run the rtld-step-main under GDB
71 # on FreeBSD 13.1, this message was/is encountered:
73 # ELF interpreter /path/to/rtld-step-rtld not found, error 22
75 # So, sadly, this test does not currently work on FreeBSD. If you try
76 # to make it work on FreeBSD, you'll probably need to enable the
77 # declarations for __progname and environ in rtld-step-rtld.c.
79 # If this test becomes broken at some point in the future, you might
80 # try removing -static from the flags below as it is not needed for
83 # Also, because the RTLD is static, you'll need static versions of
84 # libc/glibc installed on your system. (A message such as "cannot
85 # find -lc" is a clue that you're missing a static version of libc.)
87 set rtld_flags [list debug additional_flags=[list -static-pie -fPIE \
88 -nostdlib -static -lc]]
90 if { ![gdb_can_simple_compile static-pie-static-libc \
91 "void _start (void) { _exit (0); }" \
92 executable $rtld_flags] } {
93 set reason "-static-pie not supported or static libc missing"
94 untested "failed to compile ($reason)"
98 # Main program file names and flags:
99 set main_basename ${::gdb_test_file_name}-main
100 set srcfile_main ${srcdir}/${subdir}/${main_basename}.c
101 set binfile_main [standard_output_file ${main_basename}]
102 set main_flags [list debug additional_flags="-Wl,--dynamic-linker=${binfile_rtld}"]
104 # Compile pretend RTLD:
105 if { [gdb_compile ${srcfile_rtld} ${binfile_rtld} executable $rtld_flags] != "" } {
106 untested "failed to compile"
110 # Compile main program:
111 if { [gdb_compile ${srcfile_main} ${binfile_main} executable $main_flags] != "" } {
112 untested "failed to compile"
116 clean_restart ${binfile_main}
122 # Running the command 'info sharedlibrary' should output a path to
123 # the pretend/fake RTLD along with the address range. Check that
124 # this path is present and, if so, extract the address range.
125 gdb_test_multiple "info sharedlibrary" "" {
126 -re -wrap "($hex)\[ \t\]+($hex)\[ \t\]+Yes\[ \t\]+$fullname_syntax$rtld_basename" {
127 set rtld_lower $expect_out(1,string)
128 set rtld_upper $expect_out(2,string)
134 set pc [get_hexadecimal_valueof "\$pc" 0]
136 # Verify that PC is in the address range of the pretend/fake RTLD.
137 gdb_assert { $rtld_lower <= $pc && $pc < $rtld_upper } "pc is in rtld"
139 gdb_test "next" {bar \(\);} "next over foo 0"
140 gdb_test "step" {bar \(\) at.*foo \(1\);.*} "step into bar"
141 gdb_test "step" {baz \(.*?\);} "step into foo 1"
142 gdb_test "finish" {Run till exit.*bar \(\).*baz.*} "finish out of foo 1"
143 gdb_test "next" {foo \(2\);} "next over baz in bar"
144 gdb_test "step" {baz \(.*?\);} "step into foo 2"
145 gdb_test "next" "\}\[\r\n\]+" "next over baz in foo"
146 gdb_test "step" "bar \\(\\).*}\[\r\n\]+.*" "step out of foo back into bar"