Commit | Line | Data |
---|---|---|
a4b6fc86 AC |
1 | /* Target-dependent code for GNU/Linux running on x86-64, for GDB. |
2 | ||
1bac305b | 3 | Copyright 2001, 2003 Free Software Foundation, Inc. |
a4b6fc86 | 4 | |
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 | |
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., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "inferior.h" | |
26 | #include "gdbcore.h" | |
27 | #include "regcache.h" | |
84dc46cb | 28 | #include "osabi.h" |
53e95fcf | 29 | |
c4f35dd8 | 30 | #include "gdb_string.h" |
53e95fcf | 31 | |
c4f35dd8 MK |
32 | #include "x86-64-tdep.h" |
33 | ||
34 | #define LINUX_SIGTRAMP_INSN0 0x48 /* mov $NNNNNNNN, %rax */ | |
35 | #define LINUX_SIGTRAMP_OFFSET0 0 | |
36 | #define LINUX_SIGTRAMP_INSN1 0x0f /* syscall */ | |
37 | #define LINUX_SIGTRAMP_OFFSET1 7 | |
38 | ||
39 | static const unsigned char linux_sigtramp_code[] = | |
40 | { | |
41 | /* mov $__NR_rt_sigreturn, %rax */ | |
baed091b ML |
42 | LINUX_SIGTRAMP_INSN0, 0xc7, 0xc0, 0x0f, 0x00, 0x00, 0x00, |
43 | /* syscall */ | |
44 | LINUX_SIGTRAMP_INSN1, 0x05 | |
53e95fcf JS |
45 | }; |
46 | ||
47 | #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code) | |
48 | ||
49 | /* If PC is in a sigtramp routine, return the address of the start of | |
50 | the routine. Otherwise, return 0. */ | |
51 | ||
52 | static CORE_ADDR | |
53 | x86_64_linux_sigtramp_start (CORE_ADDR pc) | |
54 | { | |
55 | unsigned char buf[LINUX_SIGTRAMP_LEN]; | |
c4f35dd8 MK |
56 | |
57 | /* We only recognize a signal trampoline if PC is at the start of | |
58 | one of the two instructions. We optimize for finding the PC at | |
59 | the start, as will be the case when the trampoline is not the | |
60 | first frame on the stack. We assume that in the case where the | |
61 | PC is not at the start of the instruction sequence, there will be | |
62 | a few trailing readable bytes on the stack. */ | |
63 | ||
53e95fcf JS |
64 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) |
65 | return 0; | |
66 | ||
67 | if (buf[0] != LINUX_SIGTRAMP_INSN0) | |
68 | { | |
69 | if (buf[0] != LINUX_SIGTRAMP_INSN1) | |
70 | return 0; | |
71 | ||
72 | pc -= LINUX_SIGTRAMP_OFFSET1; | |
73 | ||
74 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0) | |
75 | return 0; | |
76 | } | |
77 | ||
78 | if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0) | |
79 | return 0; | |
80 | ||
81 | return pc; | |
82 | } | |
83 | ||
baed091b ML |
84 | /* Return whether PC is in a GNU/Linux sigtramp routine. */ |
85 | ||
c4f35dd8 MK |
86 | static int |
87 | x86_64_linux_pc_in_sigtramp (CORE_ADDR pc, char *name) | |
baed091b | 88 | { |
c4f35dd8 MK |
89 | /* If we have NAME, we can optimize the search. The trampoline is |
90 | named __restore_rt. However, it isn't dynamically exported from | |
91 | the shared C library, so the trampoline may appear to be part of | |
92 | the preceding function. This should always be sigaction, | |
93 | __sigaction, or __libc_sigaction (all aliases to the same | |
94 | function). */ | |
95 | if (name == NULL || strstr (name, "sigaction") != NULL) | |
96 | return (x86_64_linux_sigtramp_start (pc) != 0); | |
97 | ||
98 | return (strcmp ("__restore_rt", name) == 0); | |
baed091b ML |
99 | } |
100 | ||
c4f35dd8 MK |
101 | /* Offset to struct sigcontext in ucontext, from <asm/ucontext.h>. */ |
102 | #define X86_64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET 40 | |
b64bbf8c | 103 | |
c4f35dd8 MK |
104 | /* Assuming NEXT_FRAME is a frame following a GNU/Linux sigtramp |
105 | routine, return the address of the associated sigcontext structure. */ | |
baed091b | 106 | |
c4f35dd8 MK |
107 | static CORE_ADDR |
108 | x86_64_linux_sigcontext_addr (struct frame_info *next_frame) | |
baed091b | 109 | { |
c4f35dd8 MK |
110 | CORE_ADDR sp; |
111 | char buf[8]; | |
112 | ||
113 | frame_unwind_register (next_frame, SP_REGNUM, buf); | |
114 | sp = extract_unsigned_integer (buf, 8); | |
115 | ||
116 | /* The sigcontext structure is part of the user context. A pointer | |
117 | to the user context is passed as the third argument to the signal | |
118 | handler, i.e. in %rdx. Unfortunately %rdx isn't preserved across | |
119 | function calls so we can't use it. Fortunately the user context | |
120 | is part of the signal frame and the unwound %rsp directly points | |
121 | at it. */ | |
122 | return sp + X86_64_LINUX_UCONTEXT_SIGCONTEXT_OFFSET; | |
baed091b | 123 | } |
2213a65d MK |
124 | \f |
125 | ||
2b5e0749 MK |
126 | /* From <asm/sigcontext.h>. */ |
127 | static int x86_64_linux_sc_reg_offset[X86_64_NUM_GREGS] = | |
128 | { | |
129 | 13 * 8, /* %rax */ | |
130 | 11 * 8, /* %rbx */ | |
131 | 14 * 8, /* %rcx */ | |
132 | 12 * 8, /* %rdx */ | |
133 | 9 * 8, /* %rsi */ | |
134 | 8 * 8, /* %rdi */ | |
135 | 10 * 8, /* %rbp */ | |
136 | 15 * 8, /* %rsp */ | |
137 | 0 * 8, /* %r8 */ | |
138 | 1 * 8, /* %r9 */ | |
139 | 2 * 8, /* %r10 */ | |
140 | 3 * 8, /* %r11 */ | |
141 | 4 * 8, /* %r12 */ | |
142 | 5 * 8, /* %r13 */ | |
143 | 6 * 8, /* %r14 */ | |
144 | 7 * 8, /* %r15 */ | |
145 | 16 * 8, /* %rip */ | |
146 | 17 * 8, /* %eflags */ | |
147 | -1, /* %ds */ | |
148 | -1, /* %es */ | |
149 | ||
150 | /* FIXME: kettenis/2002030531: The registers %fs and %gs are | |
151 | available in `struct sigcontext'. However, they only occupy two | |
152 | bytes instead of four, which makes using them here rather | |
153 | difficult. Leave them out for now. */ | |
154 | -1, /* %fs */ | |
155 | -1 /* %gs */ | |
156 | }; | |
157 | ||
2213a65d MK |
158 | static void |
159 | x86_64_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
160 | { | |
c4f35dd8 | 161 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
2213a65d | 162 | x86_64_init_abi (info, gdbarch); |
c4f35dd8 MK |
163 | |
164 | set_gdbarch_pc_in_sigtramp (gdbarch, x86_64_linux_pc_in_sigtramp); | |
165 | ||
166 | tdep->sigcontext_addr = x86_64_linux_sigcontext_addr; | |
2b5e0749 MK |
167 | tdep->sc_reg_offset = x86_64_linux_sc_reg_offset; |
168 | tdep->sc_num_regs = X86_64_NUM_GREGS; | |
2213a65d | 169 | } |
c4f35dd8 | 170 | \f |
2213a65d MK |
171 | |
172 | /* Provide a prototype to silence -Wmissing-prototypes. */ | |
173 | extern void _initialize_x86_64_linux_tdep (void); | |
174 | ||
175 | void | |
176 | _initialize_x86_64_linux_tdep (void) | |
177 | { | |
178 | gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_LINUX, | |
179 | x86_64_linux_init_abi); | |
180 | } |