1 /* Target-dependent code for GNU/Linux running on x86-64, for GDB.
3 Copyright 2001, 2003 Free Software Foundation, Inc.
5 Contributed by Jiri Smid, SuSE Labs.
7 This file is part of GDB.
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.
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.
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. */
30 #include "gdb_string.h"
32 #include "x86-64-tdep.h"
33 #include "x86-64-linux-tdep.h"
35 /* Register indexes to 'struct user' come from <sys/reg.h>. */
54 #define USER_EFLAGS 18
62 /* Mapping between the general-purpose registers in `struct user'
63 format and GDB's register array layout. */
65 static int user_to_gdb_regmap[] =
67 USER_RAX, USER_RBX, USER_RCX, USER_RDX,
68 USER_RSI, USER_RDI, USER_RBP, USER_RSP,
69 USER_R8, USER_R9, USER_R10, USER_R11,
70 USER_R12, USER_R13, USER_R14, USER_R15,
71 USER_RIP, USER_EFLAGS,
72 USER_DS, USER_ES, USER_FS, USER_GS
75 /* Fill GDB's register array with the general-purpose register values
79 x86_64_linux_supply_gregset (char *regp)
83 for (i = 0; i < X86_64_NUM_GREGS; i++)
84 supply_register (i, regp + (user_to_gdb_regmap[i] * 8));
87 /* Fill register REGNO (if it is a general-purpose register) in
88 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
89 do this for all registers. */
92 x86_64_linux_fill_gregset (char *regp, int regno)
96 for (i = 0; i < X86_64_NUM_GREGS; i++)
97 if (regno == -1 || regno == i)
98 regcache_collect (i, regp + (user_to_gdb_regmap[i] * 8));
101 /* The register sets used in GNU/Linux ELF core-dumps are identical to
102 the register sets used by `ptrace'. The corresponding types are
103 `elf_gregset_t' for the general-purpose registers (with
104 `elf_greg_t' the type of a single GP register) and `elf_fpregset_t'
105 for the floating-point registers. */
108 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
109 int which, CORE_ADDR ignore)
113 case 0: /* Integer registers. */
114 if (core_reg_size != 216)
115 warning ("Wrong size register set in core file.");
117 x86_64_linux_supply_gregset (core_reg_sect);
120 case 2: /* Floating point registers. */
121 case 3: /* "Extended" floating point registers. This is gdb-speak
123 if (core_reg_size != 512)
124 warning ("Wrong size XMM register set in core file.");
126 x86_64_supply_fxsave (current_regcache, -1, core_reg_sect);
130 /* Don't know what kind of register request this is; just ignore it. */
135 static struct core_fns x86_64_core_fns =
137 bfd_target_elf_flavour, /* core_flavour */
138 default_check_format, /* check_format */
139 default_core_sniffer, /* core_sniffer */
140 fetch_core_registers, /* core_read_registers */
144 #define LINUX_SIGTRAMP_INSN0 0x48 /* mov $NNNNNNNN, %rax */
145 #define LINUX_SIGTRAMP_OFFSET0 0
146 #define LINUX_SIGTRAMP_INSN1 0x0f /* syscall */
147 #define LINUX_SIGTRAMP_OFFSET1 7
149 static const unsigned char linux_sigtramp_code[] =
151 /* mov $__NR_rt_sigreturn, %rax */
152 LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00,
154 LINUX_SIGTRAMP_INSN1, 0x05
157 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
159 /* If PC is in a sigtramp routine, return the address of the start of
160 the routine. Otherwise, return 0. */
163 x86_64_linux_sigtramp_start (CORE_ADDR pc)
165 unsigned char buf[LINUX_SIGTRAMP_LEN];
167 /* We only recognize a signal trampoline if PC is at the start of
168 one of the two instructions. We optimize for finding the PC at
169 the start, as will be the case when the trampoline is not the
170 first frame on the stack. We assume that in the case where the
171 PC is not at the start of the instruction sequence, there will be
172 a few trailing readable bytes on the stack. */
174 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
177 if (buf[0] != LINUX_SIGTRAMP_INSN0)
179 if (buf[0] != LINUX_SIGTRAMP_INSN1)
182 pc -= LINUX_SIGTRAMP_OFFSET1;
184 if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
188 if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
194 /* Return whether PC is in a GNU/Linux sigtramp routine. */
197 x86_64_linux_pc_in_sigtramp (CORE_ADDR pc, char *name)
199 /* If we have NAME, we can optimize the search. The trampoline is
200 named __restore_rt. However, it isn't dynamically exported from
201 the shared C library, so the trampoline may appear to be part of
202 the preceding function. This should always be sigaction,
203 __sigaction, or __libc_sigaction (all aliases to the same
205 if (name == NULL || strstr (name, "sigaction") != NULL)
206 return (x86_64_linux_sigtramp_start (pc) != 0);
208 return (strcmp ("__restore_rt", name) == 0);
211 /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */
212 #define X86_64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40
214 /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp
215 routine, return the address of the associated sigcontext structure. */
218 x86_64_linux_sigcontext_addr (struct frame_info *next_frame)
223 frame_unwind_register (next_frame, SP_REGNUM, buf);
224 sp = extract_unsigned_integer (buf, 8);
226 /* The sigcontext structure is part of the user context. A pointer
227 to the user context is passed as the third argument to the signal
228 handler, i.e. in %rdx. Unfortunately %rdx isn't preserved across
229 function calls so we can't use it. Fortunately the user context
230 is part of the signal frame and the unwound %rsp directly points
232 return sp + X86_64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET;
236 /* From <asm/sigcontext.h>. */
237 static int x86_64_linux_sc_reg_offset[] =
256 17 * 8, /* %eflags */
260 /* FIXME: kettenis/2002030531: The registers %fs and %gs are
261 available in `struct sigcontext'. However, they only occupy two
262 bytes instead of four, which makes using them here rather
263 difficult. Leave them out for now. */
269 x86_64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
271 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
272 x86_64_init_abi (info, gdbarch);
274 set_gdbarch_pc_in_sigtramp (gdbarch, x86_64_linux_pc_in_sigtramp);
276 tdep->sigcontext_addr = x86_64_linux_sigcontext_addr;
277 tdep->sc_reg_offset = x86_64_linux_sc_reg_offset;
278 tdep->sc_num_regs = ARRAY_SIZE (x86_64_linux_sc_reg_offset);
282 /* Provide a prototype to silence -Wmissing-prototypes. */
283 extern void _initialize_x86_64_linux_tdep (void);
286 _initialize_x86_64_linux_tdep (void)
288 add_core_fns (&x86_64_core_fns);
290 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_LINUX,
291 x86_64_linux_init_abi);