| 1 | /* Copyright (C) 2009-2019 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_stack_mangle_t) (void); |
| 35 | typedef long (jit_function_add_t) (long a, long b); |
| 36 | |
| 37 | /* The code of the jit_function_00 function that is copied into an |
| 38 | mmapped buffer in the inferior at run time. |
| 39 | |
| 40 | The second instruction mangles the stack pointer, meaning that when |
| 41 | stopped at the third instruction, GDB needs assistance from the JIT |
| 42 | unwinder in order to be able to unwind successfully. */ |
| 43 | static const unsigned char jit_function_stack_mangle_code[] = { |
| 44 | 0xcc, /* int3 */ |
| 45 | 0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */ |
| 46 | 0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */ |
| 47 | 0xc3 /* ret */ |
| 48 | }; |
| 49 | |
| 50 | /* And another "JIT-ed" function, with the prototype `jit_function_add_t`. */ |
| 51 | static const unsigned char jit_function_add_code[] = { |
| 52 | 0x48, 0x01, 0xfe, /* add %rdi,%rsi */ |
| 53 | 0x48, 0x89, 0xf0, /* mov %rsi,%rax */ |
| 54 | 0xc3, /* retq */ |
| 55 | }; |
| 56 | |
| 57 | int |
| 58 | main (int argc, char **argv) |
| 59 | { |
| 60 | struct jithost_abi *symfile = malloc (sizeof (struct jithost_abi)); |
| 61 | char *code = mmap (NULL, getpagesize (), PROT_WRITE | PROT_EXEC, |
| 62 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 63 | char *code_end = code; |
| 64 | |
| 65 | /* "JIT" function_stack_mangle. */ |
| 66 | memcpy (code_end, jit_function_stack_mangle_code, |
| 67 | sizeof (jit_function_stack_mangle_code)); |
| 68 | jit_function_stack_mangle_t *function_stack_mangle |
| 69 | = (jit_function_stack_mangle_t *) code_end; |
| 70 | symfile->function_stack_mangle.begin = code_end; |
| 71 | code_end += sizeof (jit_function_stack_mangle_code); |
| 72 | symfile->function_stack_mangle.end = code_end; |
| 73 | |
| 74 | /* "JIT" function_add. */ |
| 75 | memcpy (code_end, jit_function_add_code, sizeof (jit_function_add_code)); |
| 76 | jit_function_add_t *function_add = (jit_function_add_t *) code_end; |
| 77 | symfile->function_add.begin = code_end; |
| 78 | code_end += sizeof (jit_function_add_code); |
| 79 | symfile->function_add.end = code_end; |
| 80 | |
| 81 | /* Bounds of the whole object. */ |
| 82 | symfile->object.begin = code; |
| 83 | symfile->object.end = code_end; |
| 84 | |
| 85 | only_entry.symfile_addr = symfile; |
| 86 | only_entry.symfile_size = sizeof (struct jithost_abi); |
| 87 | |
| 88 | __jit_debug_descriptor.first_entry = &only_entry; |
| 89 | __jit_debug_descriptor.relevant_entry = &only_entry; |
| 90 | __jit_debug_descriptor.action_flag = JIT_REGISTER; |
| 91 | __jit_debug_descriptor.version = 1; |
| 92 | __jit_debug_register_code (); |
| 93 | |
| 94 | function_stack_mangle (); |
| 95 | function_add (5, 6); |
| 96 | |
| 97 | return 0; |
| 98 | } |