Commit | Line | Data |
---|---|---|
d3f0f853 PP |
1 | /* This test program is part of GDB, the GNU debugger. |
2 | ||
618f726f | 3 | Copyright 2011-2016 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> | |
30 | ||
7470fc63 AT |
31 | /* ElfW is coming from linux. On other platforms it does not exist. |
32 | Let us define it here. */ | |
33 | #ifndef ElfW | |
34 | # if (defined (_LP64) || defined (__LP64__)) | |
35 | # define WORDSIZE 64 | |
36 | # else | |
37 | # define WORDSIZE 32 | |
38 | # endif /* _LP64 || __LP64__ */ | |
39 | #define ElfW(type) _ElfW (Elf, WORDSIZE, type) | |
40 | #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t) | |
41 | #define _ElfW_1(e,w,t) e##w##t | |
42 | #endif /* !ElfW */ | |
43 | ||
d3f0f853 PP |
44 | typedef enum |
45 | { | |
46 | JIT_NOACTION = 0, | |
47 | JIT_REGISTER_FN, | |
48 | JIT_UNREGISTER_FN | |
49 | } jit_actions_t; | |
50 | ||
51 | struct jit_code_entry | |
52 | { | |
53 | struct jit_code_entry *next_entry; | |
54 | struct jit_code_entry *prev_entry; | |
55 | const char *symfile_addr; | |
56 | uint64_t symfile_size; | |
57 | }; | |
58 | ||
59 | struct jit_descriptor | |
60 | { | |
61 | uint32_t version; | |
62 | /* This type should be jit_actions_t, but we use uint32_t | |
63 | to be explicit about the bitwidth. */ | |
64 | uint32_t action_flag; | |
65 | struct jit_code_entry *relevant_entry; | |
66 | struct jit_code_entry *first_entry; | |
67 | }; | |
68 | ||
69 | /* GDB puts a breakpoint in this function. */ | |
70 | void __attribute__((noinline)) __jit_debug_register_code () { } | |
71 | ||
72 | /* Make sure to specify the version statically, because the | |
73 | debugger may check the version before we can set it. */ | |
74 | struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 }; | |
75 | ||
76 | static void | |
77 | usage (const char *const argv0) | |
78 | { | |
79 | fprintf (stderr, "Usage: %s library [count]\n", argv0); | |
80 | exit (1); | |
81 | } | |
82 | ||
83 | /* Update .p_vaddr and .sh_addr as if the code was JITted to ADDR. */ | |
84 | ||
85 | static void | |
86 | update_locations (const void *const addr, int idx) | |
87 | { | |
88 | const ElfW (Ehdr) *const ehdr = (ElfW (Ehdr) *)addr; | |
89 | ElfW (Shdr) *const shdr = (ElfW (Shdr) *)((char *)addr + ehdr->e_shoff); | |
90 | ElfW (Phdr) *const phdr = (ElfW (Phdr) *)((char *)addr + ehdr->e_phoff); | |
91 | int i; | |
92 | ||
93 | for (i = 0; i < ehdr->e_phnum; ++i) | |
94 | if (phdr[i].p_type == PT_LOAD) | |
95 | phdr[i].p_vaddr += (ElfW (Addr))addr; | |
96 | ||
97 | for (i = 0; i < ehdr->e_shnum; ++i) | |
98 | { | |
99 | if (shdr[i].sh_type == SHT_STRTAB) | |
100 | { | |
101 | /* Note: we update both .strtab and .dynstr. The latter would | |
102 | not be correct if this were a regular shared library (.hash | |
103 | would be wrong), but this is a simulation -- the library is | |
104 | never exposed to the dynamic loader, so it all ends up ok. */ | |
105 | char *const strtab = (char *)((ElfW (Addr))addr + shdr[i].sh_offset); | |
106 | char *const strtab_end = strtab + shdr[i].sh_size; | |
107 | char *p; | |
108 | ||
109 | for (p = strtab; p < strtab_end; p += strlen (p) + 1) | |
110 | if (strcmp (p, "jit_function_XXXX") == 0) | |
111 | sprintf (p, "jit_function_%04d", idx); | |
112 | } | |
113 | ||
114 | if (shdr[i].sh_flags & SHF_ALLOC) | |
115 | shdr[i].sh_addr += (ElfW (Addr))addr; | |
116 | } | |
117 | } | |
118 | ||
3b2a0cf2 JB |
119 | #ifndef MAIN |
120 | #define MAIN main | |
121 | #endif | |
122 | ||
d3f0f853 | 123 | int |
3b2a0cf2 | 124 | MAIN (int argc, char *argv[]) |
d3f0f853 PP |
125 | { |
126 | /* These variables are here so they can easily be set from jit.exp. */ | |
127 | const char *libname = NULL; | |
128 | int count = 0; | |
129 | ||
130 | count = count; /* gdb break here 0 */ | |
131 | ||
132 | if (argc < 2) | |
133 | usage (argv[0]); | |
134 | else | |
135 | { | |
136 | int i, fd; | |
137 | struct stat st; | |
138 | ||
139 | if (libname == NULL) | |
140 | /* Only set if not already set from GDB. */ | |
141 | libname = argv[1]; | |
142 | ||
143 | if (argc > 2 && count == 0) | |
144 | /* Only set if not already set from GDB. */ | |
145 | count = atoi (argv[2]); | |
146 | ||
147 | printf ("%s:%d: libname = %s, count = %d\n", __FILE__, __LINE__, | |
148 | libname, count); | |
149 | ||
150 | if ((fd = open (libname, O_RDONLY)) == -1) | |
151 | { | |
152 | fprintf (stderr, "open (\"%s\", O_RDONLY): %s\n", libname, | |
153 | strerror (errno)); | |
154 | exit (1); | |
155 | } | |
156 | ||
157 | if (fstat (fd, &st) != 0) | |
158 | { | |
159 | fprintf (stderr, "fstat (\"%d\"): %s\n", fd, strerror (errno)); | |
160 | exit (1); | |
161 | } | |
162 | ||
163 | for (i = 0; i < count; ++i) | |
164 | { | |
165 | const void *const addr = mmap (0, st.st_size, PROT_READ|PROT_WRITE, | |
166 | MAP_PRIVATE, fd, 0); | |
167 | struct jit_code_entry *const entry = calloc (1, sizeof (*entry)); | |
168 | ||
169 | if (addr == MAP_FAILED) | |
170 | { | |
171 | fprintf (stderr, "mmap: %s\n", strerror (errno)); | |
172 | exit (1); | |
173 | } | |
174 | ||
175 | update_locations (addr, i); | |
176 | ||
177 | /* Link entry at the end of the list. */ | |
178 | entry->symfile_addr = (const char *)addr; | |
179 | entry->symfile_size = st.st_size; | |
180 | entry->prev_entry = __jit_debug_descriptor.relevant_entry; | |
181 | __jit_debug_descriptor.relevant_entry = entry; | |
182 | ||
183 | if (entry->prev_entry != NULL) | |
184 | entry->prev_entry->next_entry = entry; | |
185 | else | |
186 | __jit_debug_descriptor.first_entry = entry; | |
187 | ||
188 | /* Notify GDB. */ | |
189 | __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; | |
190 | __jit_debug_register_code (); | |
191 | } | |
192 | ||
193 | i = 0; /* gdb break here 1 */ | |
194 | ||
195 | /* Now unregister them all in reverse order. */ | |
196 | while (__jit_debug_descriptor.relevant_entry != NULL) | |
197 | { | |
198 | struct jit_code_entry *const entry = | |
199 | __jit_debug_descriptor.relevant_entry; | |
200 | struct jit_code_entry *const prev_entry = entry->prev_entry; | |
201 | ||
202 | if (prev_entry != NULL) | |
203 | { | |
204 | prev_entry->next_entry = NULL; | |
205 | entry->prev_entry = NULL; | |
206 | } | |
207 | else | |
208 | __jit_debug_descriptor.first_entry = NULL; | |
209 | ||
210 | /* Notify GDB. */ | |
211 | __jit_debug_descriptor.action_flag = JIT_UNREGISTER_FN; | |
212 | __jit_debug_register_code (); | |
213 | ||
214 | __jit_debug_descriptor.relevant_entry = prev_entry; | |
215 | free (entry); | |
216 | } | |
217 | } | |
218 | return 0; /* gdb break here 2 */ | |
219 | } |