Commit | Line | Data |
---|---|---|
a4b6fc86 AC |
1 | /* Target-dependent code for GNU/Linux running on x86-64, for GDB. |
2 | ||
1bac305b | 3 | Copyright 2001, 2003 Free Software Foundation, Inc. |
a4b6fc86 | 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" | |
84dc46cb | 31 | #include "osabi.h" |
53e95fcf | 32 | |
53e95fcf JS |
33 | #define LINUX_SIGTRAMP_INSN0 (0x48) /* mov $NNNNNNNN,%rax */ |
34 | #define LINUX_SIGTRAMP_OFFSET0 (0) | |
35 | #define LINUX_SIGTRAMP_INSN1 (0x0f) /* syscall */ | |
36 | #define LINUX_SIGTRAMP_OFFSET1 (7) | |
37 | ||
38 | static const unsigned char linux_sigtramp_code[] = { | |
b64bbf8c | 39 | /* mov $__NR_rt_sigreturn,%rax */ |
baed091b ML |
40 | LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, |
41 | /* syscall */ | |
42 | LINUX_SIGTRAMP_INSN1, 0x05 | |
53e95fcf JS |
43 | }; |
44 | ||
45 | #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code) | |
46 | ||
47 | /* If PC is in a sigtramp routine, return the address of the start of | |
48 | the routine. Otherwise, return 0. */ | |
49 | ||
50 | static CORE_ADDR | |
51 | x86_64_linux_sigtramp_start (CORE_ADDR pc) | |
52 | { | |
53 | unsigned char buf[LINUX_SIGTRAMP_LEN]; | |
54 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
55 | return 0; | |
56 | ||
57 | if (buf[0] != LINUX_SIGTRAMP_INSN0) | |
58 | { | |
59 | if (buf[0] != LINUX_SIGTRAMP_INSN1) | |
60 | return 0; | |
61 | ||
62 | pc -= LINUX_SIGTRAMP_OFFSET1; | |
63 | ||
64 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
65 | return 0; | |
66 | } | |
67 | ||
68 | if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0) | |
69 | return 0; | |
70 | ||
71 | return pc; | |
72 | } | |
73 | ||
baed091b | 74 | #define LINUX_SIGINFO_SIZE 0 |
c74ae012 | 75 | |
53e95fcf | 76 | /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */ |
baed091b ML |
77 | #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40 |
78 | ||
79 | /* Offset to saved PC in sigcontext, from <asm/sigcontext.h>. */ | |
80 | #define LINUX_SIGCONTEXT_PC_OFFSET 128 | |
81 | #define LINUX_SIGCONTEXT_FP_OFFSET 120 | |
53e95fcf | 82 | |
a4b6fc86 AC |
83 | /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the |
84 | address of the associated sigcontext structure. */ | |
baed091b | 85 | static CORE_ADDR |
53e95fcf JS |
86 | x86_64_linux_sigcontext_addr (struct frame_info *frame) |
87 | { | |
88 | CORE_ADDR pc; | |
baed091b | 89 | ULONGEST rsp; |
53e95fcf | 90 | |
50abf9e5 | 91 | pc = x86_64_linux_sigtramp_start (get_frame_pc (frame)); |
53e95fcf JS |
92 | if (pc) |
93 | { | |
11c02a10 | 94 | if (get_next_frame (frame)) |
53e95fcf JS |
95 | /* If this isn't the top frame, the next frame must be for the |
96 | signal handler itself. The sigcontext structure is part of | |
97 | the user context. */ | |
11c02a10 | 98 | return get_frame_base (get_next_frame (frame)) + LINUX_SIGINFO_SIZE + |
53e95fcf JS |
99 | LINUX_UCONTEXT_SIGCONTEXT_OFFSET; |
100 | ||
101 | ||
102 | /* This is the top frame. */ | |
baed091b | 103 | rsp = read_register (SP_REGNUM); |
b64bbf8c | 104 | return rsp + LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET; |
53e95fcf JS |
105 | |
106 | } | |
107 | ||
108 | error ("Couldn't recognize signal trampoline."); | |
109 | return 0; | |
110 | } | |
111 | ||
a4b6fc86 AC |
112 | /* Assuming FRAME is for a GNU/Linux sigtramp routine, return the |
113 | saved program counter. */ | |
53e95fcf | 114 | |
baed091b | 115 | static CORE_ADDR |
53e95fcf JS |
116 | x86_64_linux_sigtramp_saved_pc (struct frame_info *frame) |
117 | { | |
118 | CORE_ADDR addr; | |
119 | ||
120 | addr = x86_64_linux_sigcontext_addr (frame); | |
121 | return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8); | |
122 | } | |
123 | ||
124 | /* Immediately after a function call, return the saved pc. */ | |
125 | ||
126 | CORE_ADDR | |
127 | x86_64_linux_saved_pc_after_call (struct frame_info *frame) | |
128 | { | |
5a203e44 | 129 | if ((get_frame_type (frame) == SIGTRAMP_FRAME)) |
53e95fcf JS |
130 | return x86_64_linux_sigtramp_saved_pc (frame); |
131 | ||
132 | return read_memory_integer (read_register (SP_REGNUM), 8); | |
133 | } | |
134 | ||
135 | /* Saved Pc. Get it from sigcontext if within sigtramp. */ | |
136 | CORE_ADDR | |
137 | x86_64_linux_frame_saved_pc (struct frame_info *frame) | |
138 | { | |
5a203e44 | 139 | if ((get_frame_type (frame) == SIGTRAMP_FRAME)) |
53e95fcf JS |
140 | return x86_64_linux_sigtramp_saved_pc (frame); |
141 | return cfi_get_ra (frame); | |
142 | } | |
baed091b ML |
143 | |
144 | /* Return whether PC is in a GNU/Linux sigtramp routine. */ | |
145 | ||
146 | int | |
147 | x86_64_linux_in_sigtramp (CORE_ADDR pc, char *name) | |
148 | { | |
149 | if (name) | |
bde58177 | 150 | return strcmp ("__restore_rt", name) == 0; |
b64bbf8c | 151 | |
baed091b ML |
152 | return (x86_64_linux_sigtramp_start (pc) != 0); |
153 | } | |
154 | ||
155 | CORE_ADDR | |
156 | x86_64_linux_frame_chain (struct frame_info *fi) | |
157 | { | |
b64bbf8c ML |
158 | ULONGEST addr; |
159 | CORE_ADDR fp, pc; | |
160 | ||
5a203e44 | 161 | if (!(get_frame_type (fi) == SIGTRAMP_FRAME)) |
b64bbf8c ML |
162 | { |
163 | fp = cfi_frame_chain (fi); | |
164 | if (fp) | |
baed091b | 165 | return fp; |
b64bbf8c | 166 | else |
1e2330ba | 167 | addr = get_frame_base (fi); |
b64bbf8c ML |
168 | } |
169 | else | |
11c02a10 | 170 | addr = get_frame_base (get_next_frame (fi)); |
b64bbf8c ML |
171 | |
172 | addr += LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET; | |
173 | ||
174 | fp = read_memory_integer (addr + LINUX_SIGCONTEXT_FP_OFFSET, 8) + 8; | |
175 | ||
176 | return fp; | |
baed091b ML |
177 | } |
178 | ||
97f46953 | 179 | CORE_ADDR |
baed091b ML |
180 | x86_64_init_frame_pc (int fromleaf, struct frame_info *fi) |
181 | { | |
b64bbf8c ML |
182 | CORE_ADDR addr; |
183 | ||
11c02a10 AC |
184 | if (get_next_frame (fi) |
185 | && (get_frame_type (get_next_frame (fi)) == SIGTRAMP_FRAME)) | |
b64bbf8c | 186 | { |
97f46953 | 187 | addr = get_frame_base (get_next_frame (get_next_frame (fi))) |
b64bbf8c | 188 | + LINUX_SIGINFO_SIZE + LINUX_UCONTEXT_SIGCONTEXT_OFFSET; |
97f46953 | 189 | return read_memory_integer (addr + LINUX_SIGCONTEXT_PC_OFFSET, 8); |
b64bbf8c ML |
190 | } |
191 | else | |
97f46953 | 192 | return cfi_init_frame_pc (fromleaf, fi); |
baed091b | 193 | } |
2213a65d MK |
194 | \f |
195 | ||
196 | static void | |
197 | x86_64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
198 | { | |
199 | x86_64_init_abi (info, gdbarch); | |
200 | } | |
201 | ||
202 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
203 | extern void _initialize_x86_64_linux_tdep (void); | |
204 | ||
205 | void | |
206 | _initialize_x86_64_linux_tdep (void) | |
207 | { | |
208 | gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_LINUX, | |
209 | x86_64_linux_init_abi); | |
210 | } |