1 /* Get previous frame state for an existing frame state.
2 Copyright (C) 2016 The Qt Company Ltd.
3 This file is part of elfutils.
4
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
7
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
11
12 or
13
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
17
18 or both in parallel, as here.
19
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
24
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <stdlib.h>
34 #include <assert.h>
35
36 #define BACKEND x86_64_
37 #include "libebl_CPU.h"
38
39 /* There was no CFI. Maybe we happen to have a frame pointer and can unwind from that? */
40
41 bool
x86_64_unwind(Ebl * ebl,Dwarf_Addr pc,ebl_tid_registers_t * setfunc,ebl_tid_registers_get_t * getfunc,ebl_pid_memory_read_t * readfunc,void * arg,bool * signal_framep)42 x86_64_unwind (Ebl *ebl __attribute__ ((unused)),
43 Dwarf_Addr pc __attribute__ ((unused)),
44 ebl_tid_registers_t *setfunc, ebl_tid_registers_get_t *getfunc,
45 ebl_pid_memory_read_t *readfunc, void *arg,
46 bool *signal_framep __attribute__ ((unused)))
47 {
48 // Register 6 is supposed to be rbp, thus the conventional frame pointer
49 const int fpReg = 6;
50 const int spReg = 7;
51
52 Dwarf_Word fp;
53 if (!getfunc(fpReg, 1, &fp, arg) || fp == 0)
54 return false;
55
56 // Try to read old sp, so that we can avoid infinite loops below
57 Dwarf_Word sp;
58 if (!getfunc(spReg, 1, &sp, arg))
59 sp = 0;
60
61 Dwarf_Word prev_fp;
62 if (!readfunc(fp, &prev_fp, arg))
63 prev_fp = 0;
64
65 Dwarf_Word ret;
66 if (!readfunc(fp + 8, &ret, arg))
67 return false;
68
69 if (!setfunc(fpReg, 1, &prev_fp, arg))
70 return false;
71
72 fp += 16; // Pop fp and return address and write result to sp
73 if (!setfunc(spReg, 1, &fp, arg))
74 return false;
75
76 if (!setfunc(-1, 1, &ret, arg))
77 return false;
78
79 // If the sp didn't move up we don't actually have a new stack
80 // frame but rather some random data that doesn't include frame
81 // pointers. Break the unwinding then.
82 if (sp >= fp)
83 return false;
84
85 return true;
86 }
87