1 # Copyright 2022-2024 Free Software Foundation, Inc.
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.
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.
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/>.
16 # Check the BFD filename (as used in the symfile name) that is
17 # automatically generated for in-memory BFD files, as used by the JIT
20 # Additionally, check that GDB cau use 'dump binary memory' to write
21 # out the in-memory JIT files.
23 require allow_shlib_tests
25 load_lib jit-elf-helpers.exp
27 # The main code that loads and registers JIT objects.
28 set main_basename "jit-elf-main"
29 set main_srcfile ${srcdir}/${subdir}/${main_basename}.c
30 set main_binfile [standard_output_file ${main_basename}]
32 # The shared library that gets loaded as JIT objects.
33 set jit_solib_basename jit-elf-solib
34 set jit_solib_srcfile ${srcdir}/${subdir}/${jit_solib_basename}.c
36 # Compile two shared libraries to use as JIT objects.
37 set jit_solibs_target [compile_and_download_n_jit_so \
38 $jit_solib_basename $jit_solib_srcfile 2 \
40 if { $jit_solibs_target == -1 } {
44 # Compile the main code (which loads the JIT objects).
45 if { [compile_jit_main ${main_srcfile} ${main_binfile} {}] != 0 } {
49 clean_restart $::main_binfile
50 if { ![runto_main] } {
54 # Poke desired values directly into inferior instead of using "set
55 # args" because "set args" does not work under gdbserver.
56 set count [expr [llength $jit_solibs_target] + 1]
57 gdb_test_no_output "set var argc=$count" "forging argc"
58 gdb_test_no_output "set var argv=fake_argv" "forging argv"
59 for {set i 1} {$i < $count} {incr i} {
60 set jit_solib_target [lindex $jit_solibs_target [expr $i-1]]
61 gdb_test_no_output "set var argv\[$i\]=\"${jit_solib_target}\"" \
65 # Run until the JIT libraries are loaded.
66 gdb_breakpoint [gdb_get_line_number "break here 1" $::main_srcfile]
67 gdb_continue_to_breakpoint "break here 1"
69 # Confirm that the two expected functions are available.
70 gdb_test "info function ^jit_function" \
72 "File \[^\r\n\]+jit-elf-solib.c:" \
73 "${decimal}:\\s+int jit_function_0001\\(\\);" \
74 "${decimal}:\\s+int jit_function_0002\\(\\);"]
76 # Capture the addresses of each JIT symfile.
78 set symfile_lengths {}
79 gdb_test_multiple "maint info jit" "" {
80 -re "^maint info jit\r\n" {
83 -re "^jit_code_entry address\\s+symfile address\\s+symfile size\\s*\r\n" {
86 -re "^${hex}\\s+(${hex})\\s+(${decimal})\\s*\r\n" {
87 lappend symfile_addrs $expect_out(1,string)
88 lappend symfile_lengths $expect_out(2,string)
91 -re "^$gdb_prompt $" {
95 # Now check the 'maint info symtabs' output to ensure that each
96 # symfile is mentioned, and that the names are as expected.
98 gdb_test_multiple "maint info symtabs" "" {
99 -re "^maint info symtabs\r\n" {
102 -re "^\\\}\\s*\r\n" {
105 -re "^\\\{ objfile <in-memory@($hex-$hex)>\\s+\[^\r\n\]+\r\n" {
106 lappend bfd_name_addrs $expect_out(1,string)
109 -re "^\\\{ objfile (\\S+)\\s+\[^\r\n\]+\r\n" {
112 -re "^\\s+\[^\r\n\]+\r\n" {
115 -re "^$gdb_prompt $" {
119 # Now dump each JIT solib using the 'dump binary memory' command.
121 foreach addr $symfile_addrs len $symfile_lengths {
123 set output [standard_output_file "dump-elf-solib.${count}.so"]
124 set end [expr $addr + $len]
125 gdb_test_no_output "dump binary memory $output $addr $end" \
126 "dump jit solib $count"
128 gdb_assert { [cmp_binary_files $output [standard_output_file "jit-elf-solib.${count}.so"]] == 0} \
129 "check dump of jit solib $count is as expected"
133 # Check that each of the expected jit symfile addresses was mentioned
134 # in an in-memory BFD filename.
136 foreach addr $symfile_addrs len $symfile_lengths {
137 # Drop any loading zeros from the symfile address.
138 set start [format 0x%x $addr]
140 # Calculate the end address.
141 set end [format 0x%x [expr $addr + $len]]
143 # This is what we expect the address range to look like in the BFD
145 set rng "$start-$end"
147 # Check there was a BFD with the expected address in its name.
148 gdb_assert { [expr [lsearch -exact $bfd_name_addrs $rng] != -1] } \
149 "check for in-memory bfd $count"
153 # Continue until the JIT libraries are unloaded.
154 gdb_breakpoint [gdb_get_line_number "break here 2" $::main_srcfile]
155 gdb_continue_to_breakpoint "break here 2"
157 # All jit librares must have been unregistered.
158 gdb_test "info function jit_function" \
159 "All functions matching regular expression \"jit_function\":"