*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / amd64-linux-tdep.c
1 /* Target-dependent code for GNU/Linux x86-64.
2
3 Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "defs.h"
25 #include "frame.h"
26 #include "gdbcore.h"
27 #include "regcache.h"
28 #include "osabi.h"
29 #include "symtab.h"
30 #include "gdbtypes.h"
31 #include "reggroups.h"
32 #include "amd64-linux-tdep.h"
33
34 #include "gdb_string.h"
35
36 #include "amd64-tdep.h"
37 #include "solib-svr4.h"
38
39 /* Mapping between the general-purpose registers in `struct user'
40 format and GDB's register cache layout. */
41
42 /* From <sys/reg.h>. */
43 static int amd64_linux_gregset_reg_offset[] =
44 {
45 10 * 8, /* %rax */
46 5 * 8, /* %rbx */
47 11 * 8, /* %rcx */
48 12 * 8, /* %rdx */
49 13 * 8, /* %rsi */
50 14 * 8, /* %rdi */
51 4 * 8, /* %rbp */
52 19 * 8, /* %rsp */
53 9 * 8, /* %r8 ... */
54 8 * 8,
55 7 * 8,
56 6 * 8,
57 3 * 8,
58 2 * 8,
59 1 * 8,
60 0 * 8, /* ... %r15 */
61 16 * 8, /* %rip */
62 18 * 8, /* %eflags */
63 17 * 8, /* %cs */
64 20 * 8, /* %ss */
65 23 * 8, /* %ds */
66 24 * 8, /* %es */
67 25 * 8, /* %fs */
68 26 * 8 /* %gs */
69 };
70 \f
71
72 /* Support for signal handlers. */
73
74 #define LINUX_SIGTRAMP_INSN0 0x48 /* mov $NNNNNNNN, %rax */
75 #define LINUX_SIGTRAMP_OFFSET0 0
76 #define LINUX_SIGTRAMP_INSN1 0x0f /* syscall */
77 #define LINUX_SIGTRAMP_OFFSET1 7
78
79 static const gdb_byte linux_sigtramp_code[] =
80 {
81 /* mov $__NR_rt_sigreturn, %rax */
82 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00,
83 /* syscall */
84 LINUX_SIGTRAMP_INSN1, 0x05
85 };
86
87 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
88
89 /* If PC is in a sigtramp routine, return the address of the start of
90 the routine. Otherwise, return 0. */
91
92 static CORE_ADDR
93 amd64_linux_sigtramp_start (struct frame_info *next_frame)
94 {
95 CORE_ADDR pc = frame_pc_unwind (next_frame);
96 gdb_byte buf[LINUX_SIGTRAMP_LEN];
97
98 /* We only recognize a signal trampoline if PC is at the start of
99 one of the two instructions. We optimize for finding the PC at
100 the start, as will be the case when the trampoline is not the
101 first frame on the stack. We assume that in the case where the
102 PC is not at the start of the instruction sequence, there will be
103 a few trailing readable bytes on the stack. */
104
105 if (!safe_frame_unwind_memory (next_frame, pc, buf, sizeof buf))
106 return 0;
107
108 if (buf[0] != LINUX_SIGTRAMP_INSN0)
109 {
110 if (buf[0] != LINUX_SIGTRAMP_INSN1)
111 return 0;
112
113 pc -= LINUX_SIGTRAMP_OFFSET1;
114 if (!safe_frame_unwind_memory (next_frame, pc, buf, sizeof buf))
115 return 0;
116 }
117
118 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
119 return 0;
120
121 return pc;
122 }
123
124 /* Return whether the frame preceding NEXT_FRAME corresponds to a
125 GNU/Linux sigtramp routine. */
126
127 static int
128 amd64_linux_sigtramp_p (struct frame_info *next_frame)
129 {
130 CORE_ADDR pc = frame_pc_unwind (next_frame);
131 char *name;
132
133 find_pc_partial_function (pc, &name, NULL, NULL);
134
135 /* If we have NAME, we can optimize the search. The trampoline is
136 named __restore_rt. However, it isn't dynamically exported from
137 the shared C library, so the trampoline may appear to be part of
138 the preceding function. This should always be sigaction,
139 __sigaction, or __libc_sigaction (all aliases to the same
140 function). */
141 if (name == NULL || strstr (name, "sigaction") != NULL)
142 return (amd64_linux_sigtramp_start (next_frame) != 0);
143
144 return (strcmp ("__restore_rt", name) == 0);
145 }
146
147 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
148 #define AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40
149
150 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
151 routine, return the address of the associated sigcontext structure. */
152
153 static CORE_ADDR
154 amd64_linux_sigcontext_addr (struct frame_info *next_frame)
155 {
156 CORE_ADDR sp;
157 gdb_byte buf[8];
158
159 frame_unwind_register (next_frame, SP_REGNUM, buf);
160 sp = extract_unsigned_integer (buf, 8);
161
162 /* The sigcontext structure is part of the user context. A pointer
163 to the user context is passed as the third argument to the signal
164 handler, i.e. in %rdx. Unfortunately %rdx isn't preserved across
165 function calls so we can't use it. Fortunately the user context
166 is part of the signal frame and the unwound %rsp directly points
167 at it. */
168 return sp + AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
169 }
170 \f
171
172 /* From <asm/sigcontext.h>. */
173 static int amd64_linux_sc_reg_offset[] =
174 {
175 13 * 8, /* %rax */
176 11 * 8, /* %rbx */
177 14 * 8, /* %rcx */
178 12 * 8, /* %rdx */
179 9 * 8, /* %rsi */
180 8 * 8, /* %rdi */
181 10 * 8, /* %rbp */
182 15 * 8, /* %rsp */
183 0 * 8, /* %r8 */
184 1 * 8, /* %r9 */
185 2 * 8, /* %r10 */
186 3 * 8, /* %r11 */
187 4 * 8, /* %r12 */
188 5 * 8, /* %r13 */
189 6 * 8, /* %r14 */
190 7 * 8, /* %r15 */
191 16 * 8, /* %rip */
192 17 * 8, /* %eflags */
193
194 /* FIXME: kettenis/2002030531: The registers %cs, %fs and %gs are
195 available in `struct sigcontext'. However, they only occupy two
196 bytes instead of four, which makes using them here rather
197 difficult. Leave them out for now. */
198 -1, /* %cs */
199 -1, /* %ss */
200 -1, /* %ds */
201 -1, /* %es */
202 -1, /* %fs */
203 -1 /* %gs */
204 };
205
206 /* Replacement register functions which know about %orig_rax. */
207
208 static const char *
209 amd64_linux_register_name (int reg)
210 {
211 if (reg == AMD64_LINUX_ORIG_RAX_REGNUM)
212 return "orig_rax";
213
214 return amd64_register_name (reg);
215 }
216
217 static struct type *
218 amd64_linux_register_type (struct gdbarch *gdbarch, int reg)
219 {
220 if (reg == AMD64_LINUX_ORIG_RAX_REGNUM)
221 return builtin_type_int64;
222
223 return amd64_register_type (gdbarch, reg);
224 }
225
226 static int
227 amd64_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
228 struct reggroup *group)
229 {
230 if (regnum == AMD64_LINUX_ORIG_RAX_REGNUM)
231 return (group == system_reggroup
232 || group == save_reggroup
233 || group == restore_reggroup);
234 return default_register_reggroup_p (gdbarch, regnum, group);
235 }
236
237 /* Set the program counter for process PTID to PC. */
238
239 static void
240 amd64_linux_write_pc (CORE_ADDR pc, ptid_t ptid)
241 {
242 write_register_pid (AMD64_RIP_REGNUM, pc, ptid);
243
244 /* We must be careful with modifying the program counter. If we
245 just interrupted a system call, the kernel might try to restart
246 it when we resume the inferior. On restarting the system call,
247 the kernel will try backing up the program counter even though it
248 no longer points at the system call. This typically results in a
249 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
250 "orig_rax" pseudo-register.
251
252 Note that "orig_rax" is saved when setting up a dummy call frame.
253 This means that it is properly restored when that frame is
254 popped, and that the interrupted system call will be restarted
255 when we resume the inferior on return from a function call from
256 within GDB. In all other cases the system call will not be
257 restarted. */
258 write_register_pid (AMD64_LINUX_ORIG_RAX_REGNUM, -1, ptid);
259 }
260
261 static void
262 amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
263 {
264 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
265
266 tdep->gregset_reg_offset = amd64_linux_gregset_reg_offset;
267 tdep->gregset_num_regs = ARRAY_SIZE (amd64_linux_gregset_reg_offset);
268 tdep->sizeof_gregset = 27 * 8;
269
270 amd64_init_abi (info, gdbarch);
271
272 tdep->sigtramp_p = amd64_linux_sigtramp_p;
273 tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
274 tdep->sc_reg_offset = amd64_linux_sc_reg_offset;
275 tdep->sc_num_regs = ARRAY_SIZE (amd64_linux_sc_reg_offset);
276
277 /* GNU/Linux uses SVR4-style shared libraries. */
278 set_solib_svr4_fetch_link_map_offsets
279 (gdbarch, svr4_lp64_fetch_link_map_offsets);
280
281 /* Add the %orig_rax register used for syscall restarting. */
282 set_gdbarch_write_pc (gdbarch, amd64_linux_write_pc);
283 set_gdbarch_num_regs (gdbarch, AMD64_LINUX_NUM_REGS);
284 set_gdbarch_register_name (gdbarch, amd64_linux_register_name);
285 set_gdbarch_register_type (gdbarch, amd64_linux_register_type);
286 set_gdbarch_register_reggroup_p (gdbarch, amd64_linux_register_reggroup_p);
287
288 /* Enable TLS support. */
289 set_gdbarch_fetch_tls_load_module_address (gdbarch,
290 svr4_fetch_objfile_link_map);
291 }
292 \f
293
294 /* Provide a prototype to silence -Wmissing-prototypes. */
295 extern void _initialize_amd64_linux_tdep (void);
296
297 void
298 _initialize_amd64_linux_tdep (void)
299 {
300 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
301 GDB_OSABI_LINUX, amd64_linux_init_abi);
302 }
This page took 0.035051 seconds and 4 git commands to generate.