1 /* Target-dependent code for GNU/Linux x86-64.
3 Copyright (C) 2001, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
30 #include "reggroups.h"
31 #include "amd64-linux-tdep.h"
33 #include "gdb_string.h"
35 #include "amd64-tdep.h"
36 #include "solib-svr4.h"
38 /* Mapping between the general-purpose registers in `struct user'
39 format and GDB's register cache layout. */
41 /* From <sys/reg.h>. */
42 static int amd64_linux_gregset_reg_offset
[] =
71 /* Support for signal handlers. */
73 #define LINUX_SIGTRAMP_INSN0 0x48 /* mov $NNNNNNNN, %rax */
74 #define LINUX_SIGTRAMP_OFFSET0 0
75 #define LINUX_SIGTRAMP_INSN1 0x0f /* syscall */
76 #define LINUX_SIGTRAMP_OFFSET1 7
78 static const gdb_byte linux_sigtramp_code
[] =
80 /* mov $__NR_rt_sigreturn, %rax */
81 LINUX_SIGTRAMP_INSN0
, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00,
83 LINUX_SIGTRAMP_INSN1
, 0x05
86 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
88 /* If PC is in a sigtramp routine, return the address of the start of
89 the routine. Otherwise, return 0. */
92 amd64_linux_sigtramp_start (struct frame_info
*next_frame
)
94 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
95 gdb_byte buf
[LINUX_SIGTRAMP_LEN
];
97 /* We only recognize a signal trampoline if PC is at the start of
98 one of the two instructions. We optimize for finding the PC at
99 the start, as will be the case when the trampoline is not the
100 first frame on the stack. We assume that in the case where the
101 PC is not at the start of the instruction sequence, there will be
102 a few trailing readable bytes on the stack. */
104 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, sizeof buf
))
107 if (buf
[0] != LINUX_SIGTRAMP_INSN0
)
109 if (buf
[0] != LINUX_SIGTRAMP_INSN1
)
112 pc
-= LINUX_SIGTRAMP_OFFSET1
;
113 if (!safe_frame_unwind_memory (next_frame
, pc
, buf
, sizeof buf
))
117 if (memcmp (buf
, linux_sigtramp_code
, LINUX_SIGTRAMP_LEN
) != 0)
123 /* Return whether the frame preceding NEXT_FRAME corresponds to a
124 GNU/Linux sigtramp routine. */
127 amd64_linux_sigtramp_p (struct frame_info
*next_frame
)
129 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
132 find_pc_partial_function (pc
, &name
, NULL
, NULL
);
134 /* If we have NAME, we can optimize the search. The trampoline is
135 named __restore_rt. However, it isn't dynamically exported from
136 the shared C library, so the trampoline may appear to be part of
137 the preceding function. This should always be sigaction,
138 __sigaction, or __libc_sigaction (all aliases to the same
140 if (name
== NULL
|| strstr (name
, "sigaction") != NULL
)
141 return (amd64_linux_sigtramp_start (next_frame
) != 0);
143 return (strcmp ("__restore_rt", name
) == 0);
146 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
147 #define AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40
149 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
150 routine, return the address of the associated sigcontext structure. */
153 amd64_linux_sigcontext_addr (struct frame_info
*next_frame
)
158 frame_unwind_register (next_frame
, SP_REGNUM
, buf
);
159 sp
= extract_unsigned_integer (buf
, 8);
161 /* The sigcontext structure is part of the user context. A pointer
162 to the user context is passed as the third argument to the signal
163 handler, i.e. in %rdx. Unfortunately %rdx isn't preserved across
164 function calls so we can't use it. Fortunately the user context
165 is part of the signal frame and the unwound %rsp directly points
167 return sp
+ AMD64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET
;
171 /* From <asm/sigcontext.h>. */
172 static int amd64_linux_sc_reg_offset
[] =
191 17 * 8, /* %eflags */
193 /* FIXME: kettenis/2002030531: The registers %cs, %fs and %gs are
194 available in `struct sigcontext'. However, they only occupy two
195 bytes instead of four, which makes using them here rather
196 difficult. Leave them out for now. */
205 /* Replacement register functions which know about %orig_rax. */
208 amd64_linux_register_name (int reg
)
210 if (reg
== AMD64_LINUX_ORIG_RAX_REGNUM
)
213 return amd64_register_name (reg
);
217 amd64_linux_register_type (struct gdbarch
*gdbarch
, int reg
)
219 if (reg
== AMD64_LINUX_ORIG_RAX_REGNUM
)
220 return builtin_type_int64
;
222 return amd64_register_type (gdbarch
, reg
);
226 amd64_linux_register_reggroup_p (struct gdbarch
*gdbarch
, int regnum
,
227 struct reggroup
*group
)
229 if (regnum
== AMD64_LINUX_ORIG_RAX_REGNUM
)
230 return (group
== system_reggroup
231 || group
== save_reggroup
232 || group
== restore_reggroup
);
233 return default_register_reggroup_p (gdbarch
, regnum
, group
);
236 /* Set the program counter for process PTID to PC. */
239 amd64_linux_write_pc (CORE_ADDR pc
, ptid_t ptid
)
241 write_register_pid (AMD64_RIP_REGNUM
, pc
, ptid
);
243 /* We must be careful with modifying the program counter. If we
244 just interrupted a system call, the kernel might try to restart
245 it when we resume the inferior. On restarting the system call,
246 the kernel will try backing up the program counter even though it
247 no longer points at the system call. This typically results in a
248 SIGSEGV or SIGILL. We can prevent this by writing `-1' in the
249 "orig_rax" pseudo-register.
251 Note that "orig_rax" is saved when setting up a dummy call frame.
252 This means that it is properly restored when that frame is
253 popped, and that the interrupted system call will be restarted
254 when we resume the inferior on return from a function call from
255 within GDB. In all other cases the system call will not be
257 write_register_pid (AMD64_LINUX_ORIG_RAX_REGNUM
, -1, ptid
);
261 amd64_linux_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
263 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
265 tdep
->gregset_reg_offset
= amd64_linux_gregset_reg_offset
;
266 tdep
->gregset_num_regs
= ARRAY_SIZE (amd64_linux_gregset_reg_offset
);
267 tdep
->sizeof_gregset
= 27 * 8;
269 amd64_init_abi (info
, gdbarch
);
271 tdep
->sigtramp_p
= amd64_linux_sigtramp_p
;
272 tdep
->sigcontext_addr
= amd64_linux_sigcontext_addr
;
273 tdep
->sc_reg_offset
= amd64_linux_sc_reg_offset
;
274 tdep
->sc_num_regs
= ARRAY_SIZE (amd64_linux_sc_reg_offset
);
276 /* GNU/Linux uses SVR4-style shared libraries. */
277 set_solib_svr4_fetch_link_map_offsets
278 (gdbarch
, svr4_lp64_fetch_link_map_offsets
);
280 /* Add the %orig_rax register used for syscall restarting. */
281 set_gdbarch_write_pc (gdbarch
, amd64_linux_write_pc
);
282 set_gdbarch_num_regs (gdbarch
, AMD64_LINUX_NUM_REGS
);
283 set_gdbarch_register_name (gdbarch
, amd64_linux_register_name
);
284 set_gdbarch_register_type (gdbarch
, amd64_linux_register_type
);
285 set_gdbarch_register_reggroup_p (gdbarch
, amd64_linux_register_reggroup_p
);
287 /* Enable TLS support. */
288 set_gdbarch_fetch_tls_load_module_address (gdbarch
,
289 svr4_fetch_objfile_link_map
);
293 /* Provide a prototype to silence -Wmissing-prototypes. */
294 extern void _initialize_amd64_linux_tdep (void);
297 _initialize_amd64_linux_tdep (void)
299 gdbarch_register_osabi (bfd_arch_i386
, bfd_mach_x86_64
,
300 GDB_OSABI_LINUX
, amd64_linux_init_abi
);