2002-09-03 Michal Ludvig <mludvig@suse.cz>
[binutils-gdb.git] / gdb / x86-64-linux-tdep.c
1 /* Target-dependent code for GNU/Linux running on x86-64, for GDB.
2
3 Copyright 2001 Free Software Foundation, Inc.
4
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"
27 #include "gdb_string.h"
28 #include "regcache.h"
29 #include "x86-64-tdep.h"
30 #include "dwarf2cfi.h"
31
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
37 static const unsigned char linux_sigtramp_code[] = {
38 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x89, 0x00, 0x00, 0x00, /* mov $0x89,%rax */
39 LINUX_SIGTRAMP_INSN1, 0x05 /* syscall */
40 };
41
42 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
43
44 /* If PC is in a sigtramp routine, return the address of the start of
45 the routine. Otherwise, return 0. */
46
47 static CORE_ADDR
48 x86_64_linux_sigtramp_start (CORE_ADDR pc)
49 {
50 unsigned char buf[LINUX_SIGTRAMP_LEN];
51 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
52 return 0;
53
54 if (buf[0] != LINUX_SIGTRAMP_INSN0)
55 {
56 if (buf[0] != LINUX_SIGTRAMP_INSN1)
57 return 0;
58
59 pc -= LINUX_SIGTRAMP_OFFSET1;
60
61 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
62 return 0;
63 }
64
65 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
66 return 0;
67
68 return pc;
69 }
70
71 #define LINUX_SIGINFO_SIZE 128
72
73 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
74 #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36)
75
76 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
77 address of the associated sigcontext structure. */
78 CORE_ADDR
79 x86_64_linux_sigcontext_addr (struct frame_info *frame)
80 {
81 CORE_ADDR pc;
82
83 pc = x86_64_linux_sigtramp_start (frame->pc);
84 if (pc)
85 {
86 if (frame->next)
87 /* If this isn't the top frame, the next frame must be for the
88 signal handler itself. The sigcontext structure is part of
89 the user context. */
90 return frame->next->frame + LINUX_SIGINFO_SIZE +
91 LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
92
93
94 /* This is the top frame. */
95 return read_register (SP_REGNUM) + LINUX_SIGINFO_SIZE +
96 LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
97
98 }
99
100 error ("Couldn't recognize signal trampoline.");
101 return 0;
102 }
103
104 /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */
105 #define LINUX_SIGCONTEXT_PC_OFFSET (136)
106
107 /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the
108 saved program counter. */
109
110 CORE_ADDR
111 x86_64_linux_sigtramp_saved_pc (struct frame_info *frame)
112 {
113 CORE_ADDR addr;
114
115 addr = x86_64_linux_sigcontext_addr (frame);
116 return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8);
117 }
118
119 /* Immediately after a function call, return the saved pc. */
120
121 CORE_ADDR
122 x86_64_linux_saved_pc_after_call (struct frame_info *frame)
123 {
124 if (frame->signal_handler_caller)
125 return x86_64_linux_sigtramp_saved_pc (frame);
126
127 return read_memory_integer (read_register (SP_REGNUM), 8);
128 }
129
130 /* Saved Pc. Get it from sigcontext if within sigtramp. */
131 CORE_ADDR
132 x86_64_linux_frame_saved_pc (struct frame_info *frame)
133 {
134 if (frame->signal_handler_caller)
135 return x86_64_linux_sigtramp_saved_pc (frame);
136 return cfi_get_ra (frame);
137 }