1/* Copyright (C) 2009-2020 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 <assert.h> 19#include <stdio.h> 20#include <stdlib.h> 21#include <string.h> 22#include <unistd.h> 23 24#include <sys/mman.h> 25 26#include JIT_READER_H /* Please see jit-reader.exp for an explanation. */ 27#include "jit-reader-host.h" 28#include "jit-protocol.h" 29 30struct jit_code_entry only_entry; 31 32typedef void (jit_function_stack_mangle_t) (void); 33typedef long (jit_function_add_t) (long a, long b); 34 35/* The code of the jit_function_00 function that is copied into an 36 mmapped buffer in the inferior at run time. 37 38 The second instruction mangles the stack pointer, meaning that when 39 stopped at the third instruction, GDB needs assistance from the JIT 40 unwinder in order to be able to unwind successfully. */ 41static const unsigned char jit_function_stack_mangle_code[] = { 42 0xcc, /* int3 */ 43 0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */ 44 0x48, 0x83, 0xf4, 0xff, /* xor $0xffffffffffffffff, %rsp */ 45 0xc3 /* ret */ 46}; 47 48/* And another "JIT-ed" function, with the prototype `jit_function_add_t`. */ 49static const unsigned char jit_function_add_code[] = { 50 0x48, 0x01, 0xfe, /* add %rdi,%rsi */ 51 0x48, 0x89, 0xf0, /* mov %rsi,%rax */ 52 0xc3, /* retq */ 53}; 54 55int 56main (int argc, char **argv) 57{ 58 struct jithost_abi *symfile = malloc (sizeof (struct jithost_abi)); 59 char *code = mmap (NULL, getpagesize (), PROT_WRITE | PROT_EXEC, 60 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 61 char *code_end = code; 62 63 assert (code != MAP_FAILED); 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} 99