gdb: handle endbr64 instruction in amd64_analyze_prologue
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / jit-elf-main.c
CommitLineData
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
PP
47static void
48usage (const char *const argv0)
49{
50 fprintf (stderr, "Usage: %s library [count]\n", argv0);
51 exit (1);
52}
53
54/* Update .p_vaddr and .sh_addr as if the code was JITted to ADDR. */
55
56static void
57update_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. */
100volatile 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. */
104int mypid;
105
d3f0f853 106int
3b2a0cf2 107MAIN (int argc, char *argv[])
d3f0f853
PP
108{
109 /* These variables are here so they can easily be set from jit.exp. */
110 const char *libname = NULL;
06500533
JK
111 int count = 0, i, fd;
112 struct stat st;
d3f0f853 113
64cdf930
PA
114 alarm (300);
115
116 mypid = getpid ();
117
d3f0f853
PP
118 count = count; /* gdb break here 0 */
119
120 if (argc < 2)
d3f0f853 121 {
06500533
JK
122 usage (argv[0]);
123 exit (1);
124 }
d3f0f853 125
06500533
JK
126 if (libname == NULL)
127 /* Only set if not already set from GDB. */
128 libname = argv[1];
d3f0f853 129
06500533
JK
130 if (argc > 2 && count == 0)
131 /* Only set if not already set from GDB. */
132 count = atoi (argv[2]);
d3f0f853 133
06500533
JK
134 printf ("%s:%d: libname = %s, count = %d\n", __FILE__, __LINE__,
135 libname, count);
d3f0f853 136
06500533
JK
137 if ((fd = open (libname, O_RDONLY)) == -1)
138 {
139 fprintf (stderr, "open (\"%s\", O_RDONLY): %s\n", libname,
140 strerror (errno));
141 exit (1);
142 }
d3f0f853 143
06500533
JK
144 if (fstat (fd, &st) != 0)
145 {
146 fprintf (stderr, "fstat (\"%d\"): %s\n", fd, strerror (errno));
147 exit (1);
148 }
d3f0f853 149
06500533
JK
150 for (i = 0; i < count; ++i)
151 {
152 const void *const addr = mmap (0, st.st_size, PROT_READ|PROT_WRITE,
153 MAP_PRIVATE, fd, 0);
154 struct jit_code_entry *const entry = calloc (1, sizeof (*entry));
155
156 if (addr == MAP_FAILED)
157 {
158 fprintf (stderr, "mmap: %s\n", strerror (errno));
159 exit (1);
160 }
161
162 update_locations (addr, i);
163
164 /* Link entry at the end of the list. */
165 entry->symfile_addr = (const char *)addr;
166 entry->symfile_size = st.st_size;
167 entry->prev_entry = __jit_debug_descriptor.relevant_entry;
168 __jit_debug_descriptor.relevant_entry = entry;
169
170 if (entry->prev_entry != NULL)
171 entry->prev_entry->next_entry = entry;
172 else
173 __jit_debug_descriptor.first_entry = entry;
174
175 /* Notify GDB. */
946422b6 176 __jit_debug_descriptor.action_flag = JIT_REGISTER;
06500533
JK
177 __jit_debug_register_code ();
178 }
d3f0f853 179
06500533 180 WAIT_FOR_GDB; i = 0; /* gdb break here 1 */
d3f0f853 181
06500533
JK
182 /* Now unregister them all in reverse order. */
183 while (__jit_debug_descriptor.relevant_entry != NULL)
184 {
185 struct jit_code_entry *const entry =
186 __jit_debug_descriptor.relevant_entry;
187 struct jit_code_entry *const prev_entry = entry->prev_entry;
188
189 if (prev_entry != NULL)
190 {
191 prev_entry->next_entry = NULL;
192 entry->prev_entry = NULL;
193 }
194 else
195 __jit_debug_descriptor.first_entry = NULL;
196
197 /* Notify GDB. */
946422b6 198 __jit_debug_descriptor.action_flag = JIT_UNREGISTER;
06500533
JK
199 __jit_debug_register_code ();
200
201 __jit_debug_descriptor.relevant_entry = prev_entry;
202 free (entry);
d3f0f853 203 }
06500533 204
64cdf930 205 WAIT_FOR_GDB; return 0; /* gdb break here 2 */
d3f0f853 206}
This page took 1.087984 seconds and 4 git commands to generate.