]>
Commit | Line | Data |
---|---|---|
d3f0f853 PP |
1 | /* This test program is part of GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright 2011-2020 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 MS |
32 | #include "jit-protocol.h" |
33 | ||
7470fc63 AT |
34 | /* ElfW is coming from linux. On other platforms it does not exist. |
35 | Let us define it here. */ | |
36 | #ifndef ElfW | |
37 | # if (defined (_LP64) || defined (__LP64__)) | |
38 | # define WORDSIZE 64 | |
39 | # else | |
40 | # define WORDSIZE 32 | |
41 | # endif /* _LP64 || __LP64__ */ | |
42 | #define ElfW(type) _ElfW (Elf, WORDSIZE, type) | |
43 | #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t) | |
44 | #define _ElfW_1(e,w,t) e##w##t | |
45 | #endif /* !ElfW */ | |
46 | ||
d3f0f853 | 47 | static void |
9a946945 | 48 | usage (void) |
d3f0f853 | 49 | { |
9a946945 | 50 | fprintf (stderr, "Usage: jit-elf-main libraries...\n"); |
d3f0f853 PP |
51 | exit (1); |
52 | } | |
53 | ||
54 | /* Update .p_vaddr and .sh_addr as if the code was JITted to ADDR. */ | |
55 | ||
56 | static void | |
57 | update_locations (const void *const addr, int idx) | |
58 | { | |
59 | const ElfW (Ehdr) *const ehdr = (ElfW (Ehdr) *)addr; | |
60 | ElfW (Shdr) *const shdr = (ElfW (Shdr) *)((char *)addr + ehdr->e_shoff); | |
61 | ElfW (Phdr) *const phdr = (ElfW (Phdr) *)((char *)addr + ehdr->e_phoff); | |
62 | int i; | |
63 | ||
64 | for (i = 0; i < ehdr->e_phnum; ++i) | |
65 | if (phdr[i].p_type == PT_LOAD) | |
66 | phdr[i].p_vaddr += (ElfW (Addr))addr; | |
67 | ||
68 | for (i = 0; i < ehdr->e_shnum; ++i) | |
69 | { | |
70 | if (shdr[i].sh_type == SHT_STRTAB) | |
71 | { | |
72 | /* Note: we update both .strtab and .dynstr. The latter would | |
73 | not be correct if this were a regular shared library (.hash | |
74 | would be wrong), but this is a simulation -- the library is | |
75 | never exposed to the dynamic loader, so it all ends up ok. */ | |
76 | char *const strtab = (char *)((ElfW (Addr))addr + shdr[i].sh_offset); | |
77 | char *const strtab_end = strtab + shdr[i].sh_size; | |
78 | char *p; | |
79 | ||
80 | for (p = strtab; p < strtab_end; p += strlen (p) + 1) | |
81 | if (strcmp (p, "jit_function_XXXX") == 0) | |
82 | sprintf (p, "jit_function_%04d", idx); | |
83 | } | |
84 | ||
85 | if (shdr[i].sh_flags & SHF_ALLOC) | |
86 | shdr[i].sh_addr += (ElfW (Addr))addr; | |
87 | } | |
88 | } | |
89 | ||
64cdf930 PA |
90 | /* Defined by the .exp file if testing attach. */ |
91 | #ifndef ATTACH | |
92 | #define ATTACH 0 | |
93 | #endif | |
94 | ||
3b2a0cf2 JB |
95 | #ifndef MAIN |
96 | #define MAIN main | |
97 | #endif | |
98 | ||
64cdf930 PA |
99 | /* Used to spin waiting for GDB. */ |
100 | volatile int wait_for_gdb = ATTACH; | |
e0c45f30 | 101 | #define WAIT_FOR_GDB do {} while (wait_for_gdb) |
64cdf930 PA |
102 | |
103 | /* The current process's PID. GDB retrieves this. */ | |
104 | int mypid; | |
105 | ||
d3f0f853 | 106 | int |
3b2a0cf2 | 107 | MAIN (int argc, char *argv[]) |
d3f0f853 | 108 | { |
9a946945 | 109 | int i; |
64cdf930 | 110 | alarm (300); |
9a946945 MSG |
111 | /* Used as backing storage for GDB to populate argv. */ |
112 | char *fake_argv[10]; | |
64cdf930 PA |
113 | |
114 | mypid = getpid (); | |
9a946945 | 115 | /* gdb break here 0 */ |
d3f0f853 PP |
116 | |
117 | if (argc < 2) | |
d3f0f853 | 118 | { |
9a946945 | 119 | usage (); |
06500533 JK |
120 | exit (1); |
121 | } | |
d3f0f853 | 122 | |
9a946945 | 123 | for (i = 1; i < argc; ++i) |
06500533 | 124 | { |
9a946945 MSG |
125 | struct stat st; |
126 | int fd; | |
d3f0f853 | 127 | |
9a946945 MSG |
128 | printf ("%s:%d: libname = %s, i = %d\n", __FILE__, __LINE__, argv[i], i); |
129 | if ((fd = open (argv[i], O_RDONLY)) == -1) | |
130 | { | |
131 | fprintf (stderr, "open (\"%s\", O_RDONLY): %s\n", argv[i], | |
132 | strerror (errno)); | |
133 | exit (1); | |
134 | } | |
135 | ||
136 | if (fstat (fd, &st) != 0) | |
137 | { | |
138 | fprintf (stderr, "fstat (\"%d\"): %s\n", fd, strerror (errno)); | |
139 | exit (1); | |
140 | } | |
d3f0f853 | 141 | |
06500533 JK |
142 | const void *const addr = mmap (0, st.st_size, PROT_READ|PROT_WRITE, |
143 | MAP_PRIVATE, fd, 0); | |
144 | struct jit_code_entry *const entry = calloc (1, sizeof (*entry)); | |
145 | ||
146 | if (addr == MAP_FAILED) | |
147 | { | |
148 | fprintf (stderr, "mmap: %s\n", strerror (errno)); | |
149 | exit (1); | |
150 | } | |
151 | ||
152 | update_locations (addr, i); | |
153 | ||
154 | /* Link entry at the end of the list. */ | |
155 | entry->symfile_addr = (const char *)addr; | |
156 | entry->symfile_size = st.st_size; | |
157 | entry->prev_entry = __jit_debug_descriptor.relevant_entry; | |
158 | __jit_debug_descriptor.relevant_entry = entry; | |
159 | ||
160 | if (entry->prev_entry != NULL) | |
161 | entry->prev_entry->next_entry = entry; | |
162 | else | |
163 | __jit_debug_descriptor.first_entry = entry; | |
164 | ||
165 | /* Notify GDB. */ | |
946422b6 | 166 | __jit_debug_descriptor.action_flag = JIT_REGISTER; |
06500533 JK |
167 | __jit_debug_register_code (); |
168 | } | |
d3f0f853 | 169 | |
06500533 | 170 | WAIT_FOR_GDB; i = 0; /* gdb break here 1 */ |
d3f0f853 | 171 | |
06500533 JK |
172 | /* Now unregister them all in reverse order. */ |
173 | while (__jit_debug_descriptor.relevant_entry != NULL) | |
174 | { | |
175 | struct jit_code_entry *const entry = | |
176 | __jit_debug_descriptor.relevant_entry; | |
177 | struct jit_code_entry *const prev_entry = entry->prev_entry; | |
178 | ||
179 | if (prev_entry != NULL) | |
180 | { | |
181 | prev_entry->next_entry = NULL; | |
182 | entry->prev_entry = NULL; | |
183 | } | |
184 | else | |
185 | __jit_debug_descriptor.first_entry = NULL; | |
186 | ||
187 | /* Notify GDB. */ | |
946422b6 | 188 | __jit_debug_descriptor.action_flag = JIT_UNREGISTER; |
06500533 JK |
189 | __jit_debug_register_code (); |
190 | ||
191 | __jit_debug_descriptor.relevant_entry = prev_entry; | |
192 | free (entry); | |
d3f0f853 | 193 | } |
06500533 | 194 | |
64cdf930 | 195 | WAIT_FOR_GDB; return 0; /* gdb break here 2 */ |
d3f0f853 | 196 | } |