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 <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include JIT_READER_H  /* Please see jit-reader.exp for an explanation.  */
24#include "jit-reader-host.h"
25
26GDB_DECLARE_GPL_COMPATIBLE_READER;
27
28enum register_mapping
29{
30  AMD64_RA = 16,
31  AMD64_RBP = 6,
32  AMD64_RSP = 7,
33};
34
35struct reader_state
36{
37  struct {
38    uintptr_t begin;
39    uintptr_t end;
40  } func_stack_mangle;
41};
42
43static enum gdb_status
44read_debug_info (struct gdb_reader_funcs *self,
45		 struct gdb_symbol_callbacks *cbs,
46                 void *memory, long memory_sz)
47{
48  struct jithost_abi *symfile = memory;
49  struct gdb_object *object = cbs->object_open (cbs);
50  struct gdb_symtab *symtab = cbs->symtab_open (cbs, object, "");
51
52  struct reader_state *state = (struct reader_state *) self->priv_data;
53
54  /* Record the stack mangle function's range, for the unwinder.  */
55  state->func_stack_mangle.begin
56    = (uintptr_t) symfile->function_stack_mangle.begin;
57  state->func_stack_mangle.end
58    = (uintptr_t) symfile->function_stack_mangle.end;
59
60  cbs->block_open (cbs, symtab, NULL,
61		   (GDB_CORE_ADDR) symfile->function_stack_mangle.begin,
62		   (GDB_CORE_ADDR) symfile->function_stack_mangle.end,
63		   "jit_function_stack_mangle");
64
65  cbs->block_open (cbs, symtab, NULL,
66		   (GDB_CORE_ADDR) symfile->function_add.begin,
67		   (GDB_CORE_ADDR) symfile->function_add.end,
68		   "jit_function_add");
69
70  cbs->symtab_close (cbs, symtab);
71  cbs->object_close (cbs, object);
72  return GDB_SUCCESS;
73}
74
75static void
76free_reg_value (struct gdb_reg_value *value)
77{
78  free (value);
79}
80
81static void
82write_register (struct gdb_unwind_callbacks *callbacks, int dw_reg,
83                uintptr_t value)
84{
85  const int size = sizeof (uintptr_t);
86  struct gdb_reg_value *reg_val =
87    malloc (sizeof (struct gdb_reg_value) + size - 1);
88  reg_val->defined = 1;
89  reg_val->free = free_reg_value;
90
91  memcpy (reg_val->value, &value, size);
92  callbacks->reg_set (callbacks, dw_reg, reg_val);
93}
94
95static int
96read_register (struct gdb_unwind_callbacks *callbacks, int dw_reg,
97	       uintptr_t *value)
98{
99  const int size = sizeof (uintptr_t);
100  struct gdb_reg_value *reg_val = callbacks->reg_get (callbacks, dw_reg);
101  if (reg_val->size != size || !reg_val->defined)
102    {
103      reg_val->free (reg_val);
104      return 0;
105    }
106  memcpy (value, reg_val->value, size);
107  reg_val->free (reg_val);
108  return 1;
109}
110
111/* Read the stack pointer into *VALUE.  IP is the address the inferior
112   is currently stopped at.  Takes care of demangling the stack
113   pointer if necessary.  */
114
115static int
116read_sp (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs,
117	 uintptr_t ip, uintptr_t *value)
118{
119  struct reader_state *state = (struct reader_state *) self->priv_data;
120  uintptr_t sp;
121
122  if (!read_register (cbs, AMD64_RSP, &sp))
123    return GDB_FAIL;
124
125  /* If stopped at the instruction after the "xor $-1, %rsp", demangle
126     the stack pointer back.  */
127  if (ip == state->func_stack_mangle.begin + 5)
128    sp ^= (uintptr_t) -1;
129
130  *value = sp;
131  return GDB_SUCCESS;
132}
133
134static enum gdb_status
135unwind_frame (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs)
136{
137  const int word_size = sizeof (uintptr_t);
138  uintptr_t prev_sp, this_sp;
139  uintptr_t prev_ip, this_ip;
140  uintptr_t prev_bp, this_bp;
141  struct reader_state *state = (struct reader_state *) self->priv_data;
142
143  if (!read_register (cbs, AMD64_RA, &this_ip))
144    return GDB_FAIL;
145
146  if (this_ip >= state->func_stack_mangle.end
147      || this_ip < state->func_stack_mangle.begin)
148    return GDB_FAIL;
149
150  /* Unwind RBP in order to make the unwinder that tries to unwind
151     from the just-unwound frame happy.  */
152  if (!read_register (cbs, AMD64_RBP, &this_bp))
153    return GDB_FAIL;
154  /* RBP is unmodified.  */
155  prev_bp = this_bp;
156
157  /* Fetch the demangled stack pointer.  */
158  if (!read_sp (self, cbs, this_ip, &this_sp))
159    return GDB_FAIL;
160
161  /* The return address is saved on the stack.  */
162  if (cbs->target_read (this_sp, &prev_ip, word_size) == GDB_FAIL)
163    return GDB_FAIL;
164  prev_sp = this_sp + word_size;
165
166  write_register (cbs, AMD64_RA, prev_ip);
167  write_register (cbs, AMD64_RSP, prev_sp);
168  write_register (cbs, AMD64_RBP, prev_bp);
169  return GDB_SUCCESS;
170}
171
172static struct gdb_frame_id
173get_frame_id (struct gdb_reader_funcs *self, struct gdb_unwind_callbacks *cbs)
174{
175  struct reader_state *state = (struct reader_state *) self->priv_data;
176  struct gdb_frame_id frame_id;
177  uintptr_t ip;
178  uintptr_t sp;
179
180  read_register (cbs, AMD64_RA, &ip);
181  read_sp (self, cbs, ip, &sp);
182
183  frame_id.code_address = (GDB_CORE_ADDR) state->func_stack_mangle.begin;
184  frame_id.stack_address = (GDB_CORE_ADDR) sp;
185
186  return frame_id;
187}
188
189static void
190destroy_reader (struct gdb_reader_funcs *self)
191{
192  free (self->priv_data);
193  free (self);
194}
195
196struct gdb_reader_funcs *
197gdb_init_reader (void)
198{
199  struct reader_state *state = calloc (1, sizeof (struct reader_state));
200  struct gdb_reader_funcs *reader_funcs =
201    malloc (sizeof (struct gdb_reader_funcs));
202
203  reader_funcs->reader_version = GDB_READER_INTERFACE_VERSION;
204  reader_funcs->priv_data = state;
205  reader_funcs->read = read_debug_info;
206  reader_funcs->unwind = unwind_frame;
207  reader_funcs->get_frame_id = get_frame_id;
208  reader_funcs->destroy = destroy_reader;
209
210  return reader_funcs;
211}
212