* defs.h (extract_signed_integer, extract_unsigned_integer,
[deliverable/binutils-gdb.git] / gdb / amd64-linux-tdep.c
CommitLineData
51433e4b 1/* Target-dependent code for GNU/Linux x86-64.
a4b6fc86 2
0fb0cc75 3 Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6aba47ca 4 Free Software Foundation, Inc.
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
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
53e95fcf
JS
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
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
53e95fcf
JS
21
22#include "defs.h"
35669430 23#include "arch-utils.h"
187e21d1 24#include "frame.h"
53e95fcf
JS
25#include "gdbcore.h"
26#include "regcache.h"
84dc46cb 27#include "osabi.h"
911bc6ee 28#include "symtab.h"
8695c747
DJ
29#include "gdbtypes.h"
30#include "reggroups.h"
31#include "amd64-linux-tdep.h"
4aa995e1 32#include "linux-tdep.h"
53e95fcf 33
c4f35dd8 34#include "gdb_string.h"
53e95fcf 35
9c1488cb 36#include "amd64-tdep.h"
187e21d1 37#include "solib-svr4.h"
eba29c8c
ML
38
39/* Mapping between the general-purpose registers in `struct user'
187e21d1 40 format and GDB's register cache layout. */
eba29c8c 41
187e21d1
MK
42/* From <sys/reg.h>. */
43static int amd64_linux_gregset_reg_offset[] =
eba29c8c 44{
187e21d1
MK
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 */
eba29c8c 69};
187e21d1 70\f
eba29c8c 71
187e21d1 72/* Support for signal handlers. */
c4f35dd8
MK
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
4252dc94 79static const gdb_byte linux_sigtramp_code[] =
c4f35dd8
MK
80{
81 /* mov $__NR_rt_sigreturn, %rax */
baed091b
ML
82 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00,
83 /* syscall */
84 LINUX_SIGTRAMP_INSN1, 0x05
53e95fcf
JS
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
92static CORE_ADDR
10458914 93amd64_linux_sigtramp_start (struct frame_info *this_frame)
53e95fcf 94{
10458914 95 CORE_ADDR pc = get_frame_pc (this_frame);
4252dc94 96 gdb_byte buf[LINUX_SIGTRAMP_LEN];
c4f35dd8
MK
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
10458914 105 if (!safe_frame_unwind_memory (this_frame, pc, buf, sizeof buf))
53e95fcf
JS
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;
10458914 114 if (!safe_frame_unwind_memory (this_frame, pc, buf, sizeof buf))
53e95fcf
JS
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
10458914
DJ
124/* Return whether THIS_FRAME corresponds to a GNU/Linux sigtramp
125 routine. */
baed091b 126
c4f35dd8 127static int
10458914 128amd64_linux_sigtramp_p (struct frame_info *this_frame)
baed091b 129{
10458914 130 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
131 char *name;
132
133 find_pc_partial_function (pc, &name, NULL, NULL);
134
c4f35dd8
MK
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)
10458914 142 return (amd64_linux_sigtramp_start (this_frame) != 0);
c4f35dd8
MK
143
144 return (strcmp ("__restore_rt", name) == 0);
baed091b
ML
145}
146
c4f35dd8 147/* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
51433e4b 148#define AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40
b64bbf8c 149
10458914
DJ
150/* Assuming THIS_FRAME is a GNU/Linux sigtramp routine, return the
151 address of the associated sigcontext structure. */
baed091b 152
c4f35dd8 153static CORE_ADDR
10458914 154amd64_linux_sigcontext_addr (struct frame_info *this_frame)
baed091b 155{
e17a4113
UW
156 struct gdbarch *gdbarch = get_frame_arch (this_frame);
157 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c4f35dd8 158 CORE_ADDR sp;
4252dc94 159 gdb_byte buf[8];
c4f35dd8 160
10458914 161 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
e17a4113 162 sp = extract_unsigned_integer (buf, 8, byte_order);
c4f35dd8
MK
163
164 /* The sigcontext structure is part of the user context. A pointer
165 to the user context is passed as the third argument to the signal
166 handler, i.e. in %rdx. Unfortunately %rdx isn't preserved across
167 function calls so we can't use it. Fortunately the user context
168 is part of the signal frame and the unwound %rsp directly points
169 at it. */
51433e4b 170 return sp + AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
baed091b 171}
2213a65d
MK
172\f
173
2b5e0749 174/* From <asm/sigcontext.h>. */
51433e4b 175static int amd64_linux_sc_reg_offset[] =
2b5e0749
MK
176{
177 13 * 8, /* %rax */
178 11 * 8, /* %rbx */
179 14 * 8, /* %rcx */
180 12 * 8, /* %rdx */
181 9 * 8, /* %rsi */
182 8 * 8, /* %rdi */
183 10 * 8, /* %rbp */
184 15 * 8, /* %rsp */
185 0 * 8, /* %r8 */
186 1 * 8, /* %r9 */
187 2 * 8, /* %r10 */
188 3 * 8, /* %r11 */
189 4 * 8, /* %r12 */
190 5 * 8, /* %r13 */
191 6 * 8, /* %r14 */
192 7 * 8, /* %r15 */
193 16 * 8, /* %rip */
194 17 * 8, /* %eflags */
2b5e0749 195
af233647 196 /* FIXME: kettenis/2002030531: The registers %cs, %fs and %gs are
2b5e0749
MK
197 available in `struct sigcontext'. However, they only occupy two
198 bytes instead of four, which makes using them here rather
199 difficult. Leave them out for now. */
af233647
MK
200 -1, /* %cs */
201 -1, /* %ss */
202 -1, /* %ds */
203 -1, /* %es */
2b5e0749
MK
204 -1, /* %fs */
205 -1 /* %gs */
206};
207
8695c747
DJ
208/* Replacement register functions which know about %orig_rax. */
209
210static const char *
d93859e2 211amd64_linux_register_name (struct gdbarch *gdbarch, int reg)
8695c747
DJ
212{
213 if (reg == AMD64_LINUX_ORIG_RAX_REGNUM)
214 return "orig_rax";
215
d93859e2 216 return amd64_register_name (gdbarch, reg);
8695c747
DJ
217}
218
219static struct type *
220amd64_linux_register_type (struct gdbarch *gdbarch, int reg)
221{
222 if (reg == AMD64_LINUX_ORIG_RAX_REGNUM)
df4df182 223 return builtin_type (gdbarch)->builtin_int64;
8695c747
DJ
224
225 return amd64_register_type (gdbarch, reg);
226}
227
228static int
229amd64_linux_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
230 struct reggroup *group)
231{
232 if (regnum == AMD64_LINUX_ORIG_RAX_REGNUM)
233 return (group == system_reggroup
234 || group == save_reggroup
235 || group == restore_reggroup);
236 return default_register_reggroup_p (gdbarch, regnum, group);
237}
238
239/* Set the program counter for process PTID to PC. */
240
241static void
61a1198a 242amd64_linux_write_pc (struct regcache *regcache, CORE_ADDR pc)
8695c747 243{
61a1198a 244 regcache_cooked_write_unsigned (regcache, AMD64_RIP_REGNUM, pc);
8695c747
DJ
245
246 /* We must be careful with modifying the program counter. If we
247 just interrupted a system call, the kernel might try to restart
248 it when we resume the inferior. On restarting the system call,
249 the kernel will try backing up the program counter even though it
250 no longer points at the system call. This typically results in a
251 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
252 "orig_rax" pseudo-register.
253
254 Note that "orig_rax" is saved when setting up a dummy call frame.
255 This means that it is properly restored when that frame is
256 popped, and that the interrupted system call will be restarted
257 when we resume the inferior on return from a function call from
258 within GDB. In all other cases the system call will not be
259 restarted. */
61a1198a 260 regcache_cooked_write_unsigned (regcache, AMD64_LINUX_ORIG_RAX_REGNUM, -1);
8695c747
DJ
261}
262
2213a65d 263static void
51433e4b 264amd64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2213a65d 265{
c4f35dd8 266 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
187e21d1
MK
267
268 tdep->gregset_reg_offset = amd64_linux_gregset_reg_offset;
269 tdep->gregset_num_regs = ARRAY_SIZE (amd64_linux_gregset_reg_offset);
270 tdep->sizeof_gregset = 27 * 8;
271
90f90721 272 amd64_init_abi (info, gdbarch);
c4f35dd8 273
911bc6ee 274 tdep->sigtramp_p = amd64_linux_sigtramp_p;
51433e4b
MK
275 tdep->sigcontext_addr = amd64_linux_sigcontext_addr;
276 tdep->sc_reg_offset = amd64_linux_sc_reg_offset;
277 tdep->sc_num_regs = ARRAY_SIZE (amd64_linux_sc_reg_offset);
187e21d1
MK
278
279 /* GNU/Linux uses SVR4-style shared libraries. */
280 set_solib_svr4_fetch_link_map_offsets
281 (gdbarch, svr4_lp64_fetch_link_map_offsets);
b2756930 282
8695c747
DJ
283 /* Add the %orig_rax register used for syscall restarting. */
284 set_gdbarch_write_pc (gdbarch, amd64_linux_write_pc);
285 set_gdbarch_num_regs (gdbarch, AMD64_LINUX_NUM_REGS);
286 set_gdbarch_register_name (gdbarch, amd64_linux_register_name);
287 set_gdbarch_register_type (gdbarch, amd64_linux_register_type);
288 set_gdbarch_register_reggroup_p (gdbarch, amd64_linux_register_reggroup_p);
289
b2756930
KB
290 /* Enable TLS support. */
291 set_gdbarch_fetch_tls_load_module_address (gdbarch,
292 svr4_fetch_objfile_link_map);
35669430
DE
293
294 /* Displaced stepping. */
295 set_gdbarch_displaced_step_copy_insn (gdbarch,
296 amd64_displaced_step_copy_insn);
297 set_gdbarch_displaced_step_fixup (gdbarch, amd64_displaced_step_fixup);
298 set_gdbarch_displaced_step_free_closure (gdbarch,
299 simple_displaced_step_free_closure);
300 set_gdbarch_displaced_step_location (gdbarch,
301 displaced_step_at_entry_point);
4aa995e1
PA
302
303 set_gdbarch_get_siginfo_type (gdbarch, linux_get_siginfo_type);
2213a65d 304}
c4f35dd8 305\f
2213a65d
MK
306
307/* Provide a prototype to silence -Wmissing-prototypes. */
51433e4b 308extern void _initialize_amd64_linux_tdep (void);
2213a65d
MK
309
310void
51433e4b 311_initialize_amd64_linux_tdep (void)
2213a65d 312{
51433e4b
MK
313 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64,
314 GDB_OSABI_LINUX, amd64_linux_init_abi);
2213a65d 315}
This page took 0.559518 seconds and 4 git commands to generate.