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