[gdb/testsuite] define jit function name via macro
[binutils-gdb.git] / gdb / testsuite / lib / jit-elf-helpers.exp
1 # Copyright 2020 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 # Magic constants used to calculate a starting address when linking
17 # "jit" shared libraries. When loaded, will be mapped by jit-elf-main
18 # to the same address.
19
20 set jit_load_address 0x7000000
21 set jit_load_increment 0x1000000
22
23 # Compile jit-elf-main.c as an executable.
24 #
25 # BINSUFFIX is appended to the binary name.
26 # OPTIONS is passed to gdb_compile when compiling the program.
27 #
28 # On success, return 0.
29 # On failure, return -1.
30 proc compile_jit_main {main_srcfile main_binfile options} {
31 global jit_load_address jit_load_increment
32
33 set options [concat \
34 $options \
35 additional_flags=-DLOAD_ADDRESS=$jit_load_address \
36 additional_flags=-DLOAD_INCREMENT=$jit_load_increment \
37 debug]
38
39 if { [gdb_compile ${main_srcfile} ${main_binfile} \
40 executable $options] != "" } {
41 untested "failed to compile ${main_basename}.c"
42 return -1
43 }
44
45 return 0
46 }
47
48 # Compile jit-elf-main.c as a shared library.
49 #
50 # OPTIONS is passed to gdb_compile when compiling the program.
51 #
52 # On success, return 0.
53 # On failure, return -1.
54 proc compile_jit_elf_main_as_so {main_solib_srcfile main_solib_binfile options} {
55 global jit_load_address jit_load_increment
56
57 set options [list \
58 additional_flags="-DMAIN=jit_dl_main" \
59 additional_flags=-DLOAD_ADDRESS=$jit_load_address \
60 additional_flags=-DLOAD_INCREMENT=$jit_load_increment \
61 debug]
62
63 if { [gdb_compile_shlib ${main_solib_srcfile} ${main_solib_binfile} \
64 $options] != "" } {
65 untested "failed to compile ${main_solib_basename}.c as a shared library"
66 return -1
67 }
68
69 return 0
70 }
71
72 # Compile jit-elf-solib.c as a shared library in multiple copies and
73 # upload them to the target.
74 #
75 # On success, return a list of target path to the shared libraries.
76 # On failure, return -1.
77 proc compile_and_download_n_jit_so {jit_solib_basename jit_solib_srcfile count} {
78 global jit_load_address jit_load_increment
79 set binfiles_target {}
80
81 for {set i 1} {$i <= $count} {incr i} {
82 set binfile [standard_output_file ${jit_solib_basename}.$i.so]
83
84 # Note: compiling without debug info by default: some test
85 # do symbol renaming by munging on ELF symbol table, and that
86 # wouldn't work for .debug sections. Also, output for "info
87 # function" changes when debug info is present.
88 set addr [format 0x%x [expr $jit_load_address + $jit_load_increment * [expr $i-1]]]
89 # Using -Ttext-segment flag to ask linked to relocate everything
90 # in the compiled shared library against a fixed base address. Combined
91 # with mapping the resulting binary to the same fixed base it allows
92 # to dynamically execute functions from it without any further adjustments.
93 set options [list \
94 additional_flags=-DFUNCTION_NAME=[format "jit_function_%04d" $i] \
95 additional_flags=-Xlinker \
96 additional_flags=-Ttext-segment=$addr]
97 if { [gdb_compile_shlib ${jit_solib_srcfile} ${binfile} $options] != "" } {
98 untested "failed to compile ${jit_solib_basename}.c as a shared library"
99 return -1
100 }
101
102 set path [gdb_remote_download target ${binfile}]
103 lappend binfiles_target $path
104 }
105
106 return $binfiles_target
107 }