1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* Copyright (C) 2009-2019 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include JIT_READER_H /* Please see jit-reader.exp for an explanation. */
#include "jithost.h"
#include "jit-protocol.h"
void __attribute__((noinline)) __jit_debug_register_code () { }
struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
struct jit_code_entry only_entry;
typedef void (jit_function_stack_mangle_t) (void);
typedef long (jit_function_add_t) (long a, long b);
/* The code of the jit_function_00 function that is copied into an
mmapped buffer in the inferior at run time.
The second instruction mangles the stack pointer, meaning that when
stopped at the third instruction, GDB needs assistance from the JIT
unwinder in order to be able to unwind successfully. */
static const unsigned char jit_function_stack_mangle_code[] = {
0xcc, /* int3 */
0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */
0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */
0xc3 /* ret */
};
/* And another "JIT-ed" function, with the prototype `jit_function_add_t`. */
static const unsigned char jit_function_add_code[] = {
0x48, 0x01, 0xfe, /* add %rdi,%rsi */
0x48, 0x89, 0xf0, /* mov %rsi,%rax */
0xc3, /* retq */
};
int
main (int argc, char **argv)
{
struct jithost_abi *symfile = malloc (sizeof (struct jithost_abi));
char *code = mmap (NULL, getpagesize (), PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
char *code_end = code;
/* "JIT" function_stack_mangle. */
memcpy (code_end, jit_function_stack_mangle_code,
sizeof (jit_function_stack_mangle_code));
jit_function_stack_mangle_t *function_stack_mangle
= (jit_function_stack_mangle_t *) code_end;
symfile->function_stack_mangle.begin = code_end;
code_end += sizeof (jit_function_stack_mangle_code);
symfile->function_stack_mangle.end = code_end;
/* "JIT" function_add. */
memcpy (code_end, jit_function_add_code, sizeof (jit_function_add_code));
jit_function_add_t *function_add = (jit_function_add_t *) code_end;
symfile->function_add.begin = code_end;
code_end += sizeof (jit_function_add_code);
symfile->function_add.end = code_end;
/* Bounds of the whole object. */
symfile->object.begin = code;
symfile->object.end = code_end;
only_entry.symfile_addr = symfile;
only_entry.symfile_size = sizeof (struct jithost_abi);
__jit_debug_descriptor.first_entry = &only_entry;
__jit_debug_descriptor.relevant_entry = &only_entry;
__jit_debug_descriptor.action_flag = JIT_REGISTER;
__jit_debug_descriptor.version = 1;
__jit_debug_register_code ();
function_stack_mangle ();
function_add (5, 6);
return 0;
}
|