]>
Commit | Line | Data |
---|---|---|
d3f0f853 PP |
1 | /* This test program is part of GDB, the GNU debugger. |
2 | ||
4a94e368 | 3 | Copyright 2011-2022 Free Software Foundation, Inc. |
d3f0f853 PP |
4 | |
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
47d48711 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d3f0f853 PP |
17 | |
18 | /* Simulate loading of JIT code. */ | |
19 | ||
20 | #include <elf.h> | |
21 | #include <link.h> | |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <stdint.h> | |
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/mman.h> | |
29 | #include <sys/stat.h> | |
64cdf930 | 30 | #include <unistd.h> |
d3f0f853 | 31 | |
946422b6 | 32 | #include "jit-protocol.h" |
2528f8ad | 33 | #include "jit-elf-util.h" |
7470fc63 | 34 | |
d3f0f853 | 35 | static void |
9a946945 | 36 | usage (void) |
d3f0f853 | 37 | { |
9a946945 | 38 | fprintf (stderr, "Usage: jit-elf-main libraries...\n"); |
d3f0f853 PP |
39 | exit (1); |
40 | } | |
41 | ||
64cdf930 PA |
42 | /* Defined by the .exp file if testing attach. */ |
43 | #ifndef ATTACH | |
44 | #define ATTACH 0 | |
45 | #endif | |
46 | ||
3b2a0cf2 JB |
47 | #ifndef MAIN |
48 | #define MAIN main | |
49 | #endif | |
50 | ||
80ad340c MS |
51 | /* Must be defined by .exp file when compiling to know |
52 | what address to map the ELF binary to. */ | |
53 | #ifndef LOAD_ADDRESS | |
54 | #error "Must define LOAD_ADDRESS" | |
55 | #endif | |
56 | #ifndef LOAD_INCREMENT | |
57 | #error "Must define LOAD_INCREMENT" | |
58 | #endif | |
59 | ||
64cdf930 PA |
60 | /* Used to spin waiting for GDB. */ |
61 | volatile int wait_for_gdb = ATTACH; | |
e0c45f30 | 62 | #define WAIT_FOR_GDB do {} while (wait_for_gdb) |
64cdf930 PA |
63 | |
64 | /* The current process's PID. GDB retrieves this. */ | |
65 | int mypid; | |
66 | ||
d3f0f853 | 67 | int |
3b2a0cf2 | 68 | MAIN (int argc, char *argv[]) |
d3f0f853 | 69 | { |
9a946945 | 70 | int i; |
64cdf930 | 71 | alarm (300); |
9a946945 MSG |
72 | /* Used as backing storage for GDB to populate argv. */ |
73 | char *fake_argv[10]; | |
64cdf930 PA |
74 | |
75 | mypid = getpid (); | |
9a946945 | 76 | /* gdb break here 0 */ |
d3f0f853 PP |
77 | |
78 | if (argc < 2) | |
d3f0f853 | 79 | { |
9a946945 | 80 | usage (); |
06500533 JK |
81 | exit (1); |
82 | } | |
d3f0f853 | 83 | |
9a946945 | 84 | for (i = 1; i < argc; ++i) |
06500533 | 85 | { |
2528f8ad | 86 | size_t obj_size; |
80ad340c | 87 | void *load_addr = (void *) (size_t) (LOAD_ADDRESS + (i - 1) * LOAD_INCREMENT); |
2528f8ad MS |
88 | printf ("Loading %s as JIT at %p\n", argv[i], load_addr); |
89 | void *addr = load_elf (argv[i], &obj_size, load_addr); | |
06500533 | 90 | |
2528f8ad MS |
91 | char name[32]; |
92 | sprintf (name, "jit_function_%04d", i); | |
93 | int (*jit_function) (void) = (int (*) (void)) load_symbol (addr, name); | |
06500533 | 94 | |
06500533 | 95 | /* Link entry at the end of the list. */ |
2528f8ad | 96 | struct jit_code_entry *const entry = calloc (1, sizeof (*entry)); |
06500533 | 97 | entry->symfile_addr = (const char *)addr; |
2528f8ad | 98 | entry->symfile_size = obj_size; |
06500533 JK |
99 | entry->prev_entry = __jit_debug_descriptor.relevant_entry; |
100 | __jit_debug_descriptor.relevant_entry = entry; | |
101 | ||
102 | if (entry->prev_entry != NULL) | |
103 | entry->prev_entry->next_entry = entry; | |
104 | else | |
105 | __jit_debug_descriptor.first_entry = entry; | |
106 | ||
107 | /* Notify GDB. */ | |
946422b6 | 108 | __jit_debug_descriptor.action_flag = JIT_REGISTER; |
06500533 | 109 | __jit_debug_register_code (); |
2528f8ad MS |
110 | |
111 | if (jit_function () != 42) | |
112 | { | |
113 | fprintf (stderr, "unexpected return value\n"); | |
114 | exit (1); | |
115 | } | |
06500533 | 116 | } |
d3f0f853 | 117 | |
06500533 | 118 | WAIT_FOR_GDB; i = 0; /* gdb break here 1 */ |
d3f0f853 | 119 | |
06500533 JK |
120 | /* Now unregister them all in reverse order. */ |
121 | while (__jit_debug_descriptor.relevant_entry != NULL) | |
122 | { | |
123 | struct jit_code_entry *const entry = | |
124 | __jit_debug_descriptor.relevant_entry; | |
125 | struct jit_code_entry *const prev_entry = entry->prev_entry; | |
126 | ||
127 | if (prev_entry != NULL) | |
128 | { | |
129 | prev_entry->next_entry = NULL; | |
130 | entry->prev_entry = NULL; | |
131 | } | |
132 | else | |
133 | __jit_debug_descriptor.first_entry = NULL; | |
134 | ||
135 | /* Notify GDB. */ | |
946422b6 | 136 | __jit_debug_descriptor.action_flag = JIT_UNREGISTER; |
06500533 JK |
137 | __jit_debug_register_code (); |
138 | ||
139 | __jit_debug_descriptor.relevant_entry = prev_entry; | |
140 | free (entry); | |
d3f0f853 | 141 | } |
06500533 | 142 | |
64cdf930 | 143 | WAIT_FOR_GDB; return 0; /* gdb break here 2 */ |
d3f0f853 | 144 | } |