[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / jithost.c
1 /* Copyright (C) 2009-2018 Free Software Foundation, Inc.
2
3    This file is part of GDB.
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
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include <sys/mman.h>
24
25 #include JIT_READER_H  /* Please see jit-reader.exp for an explanation.  */
26 #include "jithost.h"
27 #include "jit-protocol.h"
28
29 void __attribute__((noinline)) __jit_debug_register_code () { }
30
31 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
32 struct jit_code_entry only_entry;
33
34 typedef void (jit_function_t) ();
35
36 /* The code of the jit_function_00 function that is copied into an
37    mmapped buffer in the inferior at run time.
38
39    The second instruction mangles the stack pointer, meaning that when
40    stopped at the third instruction, GDB needs assistance from the JIT
41    unwinder in order to be able to unwind successfully.  */
42 const unsigned char jit_function_00_code[] = {
43   0xcc,                         /* int3 */
44   0x48, 0x83, 0xf4, 0xff,       /* xor $0xffffffffffffffff, %rsp */
45   0x48, 0x83, 0xf4, 0xff,       /* xor $0xffffffffffffffff, %rsp */
46   0xc3                          /* ret */
47 };
48
49 int
50 main (int argc, char **argv)
51 {
52   struct jithost_abi *symfile;
53   char *code = mmap (NULL, getpagesize (), PROT_WRITE | PROT_EXEC,
54                      MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
55   jit_function_t *function = (jit_function_t *) code;
56
57   memcpy (code, jit_function_00_code, sizeof (jit_function_00_code));
58
59   symfile = malloc (sizeof (struct jithost_abi));
60   symfile->begin = code;
61   symfile->end = code + sizeof (jit_function_00_code);
62
63   only_entry.symfile_addr = symfile;
64   only_entry.symfile_size = sizeof (struct jithost_abi);
65
66   __jit_debug_descriptor.first_entry = &only_entry;
67   __jit_debug_descriptor.relevant_entry = &only_entry;
68   __jit_debug_descriptor.action_flag = JIT_REGISTER;
69   __jit_debug_descriptor.version = 1;
70   __jit_debug_register_code ();
71
72   function ();
73
74   return 0;
75 }