]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/x86-64-linux-tdep.c
2002-11-18 Andrew Cagney <ac131313@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / x86-64-linux-tdep.c
CommitLineData
a4b6fc86
AC
1/* Target-dependent code for GNU/Linux running on x86-64, for GDB.
2
3 Copyright 2001 Free Software Foundation, Inc.
4
53e95fcf
JS
5 Contributed by Jiri Smid, SuSE Labs.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
23
24#include "defs.h"
25#include "inferior.h"
26#include "gdbcore.h"
30d52491 27#include "gdb_string.h"
53e95fcf
JS
28#include "regcache.h"
29#include "x86-64-tdep.h"
30#include "dwarf2cfi.h"
31
53e95fcf
JS
32#define LINUX_SIGTRAMP_INSN0 (0x48) /* mov $NNNNNNNN,%rax */
33#define LINUX_SIGTRAMP_OFFSET0 (0)
34#define LINUX_SIGTRAMP_INSN1 (0x0f) /* syscall */
35#define LINUX_SIGTRAMP_OFFSET1 (7)
36
37static const unsigned char linux_sigtramp_code[] = {
b64bbf8c 38 /* mov $__NR_rt_sigreturn,%rax */
baed091b
ML
39 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00,
40 /* syscall */
41 LINUX_SIGTRAMP_INSN1, 0x05
53e95fcf
JS
42};
43
44#define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
45
46/* If PC is in a sigtramp routine, return the address of the start of
47 the routine. Otherwise, return 0. */
48
49static CORE_ADDR
50x86_64_linux_sigtramp_start (CORE_ADDR pc)
51{
52 unsigned char buf[LINUX_SIGTRAMP_LEN];
53 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
54 return 0;
55
56 if (buf[0] != LINUX_SIGTRAMP_INSN0)
57 {
58 if (buf[0] != LINUX_SIGTRAMP_INSN1)
59 return 0;
60
61 pc -= LINUX_SIGTRAMP_OFFSET1;
62
63 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
64 return 0;
65 }
66
67 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
68 return 0;
69
70 return pc;
71}
72
baed091b 73#define LINUX_SIGINFO_SIZE 0
c74ae012 74
53e95fcf 75/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
baed091b
ML
76#define LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40
77
78/* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
79#define LINUX_SIGCONTEXT_PC_OFFSET 128
80#define LINUX_SIGCONTEXT_FP_OFFSET 120
53e95fcf 81
a4b6fc86
AC
82/* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
83 address of the associated sigcontext structure. */
baed091b 84static CORE_ADDR
53e95fcf
JS
85x86_64_linux_sigcontext_addr (struct frame_info *frame)
86{
87 CORE_ADDR pc;
baed091b 88 ULONGEST rsp;
53e95fcf
JS
89
90 pc = x86_64_linux_sigtramp_start (frame->pc);
91 if (pc)
92 {
93 if (frame->next)
94 /* If this isn't the top frame, the next frame must be for the
95 signal handler itself. The sigcontext structure is part of
96 the user context. */
c74ae012 97 return frame->next->frame + LINUX_SIGINFO_SIZE +
53e95fcf
JS
98 LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
99
100
101 /* This is the top frame. */
baed091b 102 rsp = read_register (SP_REGNUM);
b64bbf8c 103 return rsp + LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
53e95fcf
JS
104
105 }
106
107 error ("Couldn't recognize signal trampoline.");
108 return 0;
109}
110
a4b6fc86
AC
111/* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
112 saved program counter. */
53e95fcf 113
baed091b 114static CORE_ADDR
53e95fcf
JS
115x86_64_linux_sigtramp_saved_pc (struct frame_info *frame)
116{
117 CORE_ADDR addr;
118
119 addr = x86_64_linux_sigcontext_addr (frame);
120 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8);
121}
122
123/* Immediately after a function call, return the saved pc. */
124
125CORE_ADDR
126x86_64_linux_saved_pc_after_call (struct frame_info *frame)
127{
5a203e44 128 if ((get_frame_type (frame) == SIGTRAMP_FRAME))
53e95fcf
JS
129 return x86_64_linux_sigtramp_saved_pc (frame);
130
131 return read_memory_integer (read_register (SP_REGNUM), 8);
132}
133
134/* Saved Pc. Get it from sigcontext if within sigtramp. */
135CORE_ADDR
136x86_64_linux_frame_saved_pc (struct frame_info *frame)
137{
5a203e44 138 if ((get_frame_type (frame) == SIGTRAMP_FRAME))
53e95fcf
JS
139 return x86_64_linux_sigtramp_saved_pc (frame);
140 return cfi_get_ra (frame);
141}
baed091b
ML
142
143/* Return whether PC is in a GNU/Linux sigtramp routine. */
144
145int
146x86_64_linux_in_sigtramp (CORE_ADDR pc, char *name)
147{
148 if (name)
149 return STREQ ("__restore_rt", name);
b64bbf8c 150
baed091b
ML
151 return (x86_64_linux_sigtramp_start (pc) != 0);
152}
153
154CORE_ADDR
155x86_64_linux_frame_chain (struct frame_info *fi)
156{
b64bbf8c
ML
157 ULONGEST addr;
158 CORE_ADDR fp, pc;
159
5a203e44 160 if (!(get_frame_type (fi) == SIGTRAMP_FRAME))
b64bbf8c
ML
161 {
162 fp = cfi_frame_chain (fi);
163 if (fp)
baed091b 164 return fp;
b64bbf8c
ML
165 else
166 addr = fi->frame;
167 }
168 else
169 addr = fi->next->frame;
170
171 addr += LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
172
173 fp = read_memory_integer (addr + LINUX_SIGCONTEXT_FP_OFFSET, 8) + 8;
174
175 return fp;
baed091b
ML
176}
177
178void
179x86_64_init_frame_pc (int fromleaf, struct frame_info *fi)
180{
b64bbf8c
ML
181 CORE_ADDR addr;
182
5a203e44 183 if (fi->next && (get_frame_type (fi->next) == SIGTRAMP_FRAME))
b64bbf8c
ML
184 {
185 addr = fi->next->next->frame
186 + LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
187 fi->pc = read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8);
188 }
189 else
190 cfi_init_frame_pc (fromleaf, fi);
baed091b 191}