amd64-tdep.c revision 1.1 1 1.1 christos /* Target-dependent code for AMD64.
2 1.1 christos
3 1.1 christos Copyright (C) 2001-2014 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos Contributed by Jiri Smid, SuSE Labs.
6 1.1 christos
7 1.1 christos This file is part of GDB.
8 1.1 christos
9 1.1 christos This program is free software; you can redistribute it and/or modify
10 1.1 christos it under the terms of the GNU General Public License as published by
11 1.1 christos the Free Software Foundation; either version 3 of the License, or
12 1.1 christos (at your option) any later version.
13 1.1 christos
14 1.1 christos This program is distributed in the hope that it will be useful,
15 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
16 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 1.1 christos GNU General Public License for more details.
18 1.1 christos
19 1.1 christos You should have received a copy of the GNU General Public License
20 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 1.1 christos
22 1.1 christos #include "defs.h"
23 1.1 christos #include "opcode/i386.h"
24 1.1 christos #include "dis-asm.h"
25 1.1 christos #include "arch-utils.h"
26 1.1 christos #include "block.h"
27 1.1 christos #include "dummy-frame.h"
28 1.1 christos #include "frame.h"
29 1.1 christos #include "frame-base.h"
30 1.1 christos #include "frame-unwind.h"
31 1.1 christos #include "inferior.h"
32 1.1 christos #include "gdbcmd.h"
33 1.1 christos #include "gdbcore.h"
34 1.1 christos #include "objfiles.h"
35 1.1 christos #include "regcache.h"
36 1.1 christos #include "regset.h"
37 1.1 christos #include "symfile.h"
38 1.1 christos #include "disasm.h"
39 1.1 christos #include "gdb_assert.h"
40 1.1 christos #include "exceptions.h"
41 1.1 christos #include "amd64-tdep.h"
42 1.1 christos #include "i387-tdep.h"
43 1.1 christos
44 1.1 christos #include "features/i386/amd64.c"
45 1.1 christos #include "features/i386/amd64-avx.c"
46 1.1 christos #include "features/i386/amd64-mpx.c"
47 1.1 christos #include "features/i386/x32.c"
48 1.1 christos #include "features/i386/x32-avx.c"
49 1.1 christos
50 1.1 christos #include "ax.h"
51 1.1 christos #include "ax-gdb.h"
52 1.1 christos
53 1.1 christos /* Note that the AMD64 architecture was previously known as x86-64.
54 1.1 christos The latter is (forever) engraved into the canonical system name as
55 1.1 christos returned by config.guess, and used as the name for the AMD64 port
56 1.1 christos of GNU/Linux. The BSD's have renamed their ports to amd64; they
57 1.1 christos don't like to shout. For GDB we prefer the amd64_-prefix over the
58 1.1 christos x86_64_-prefix since it's so much easier to type. */
59 1.1 christos
60 1.1 christos /* Register information. */
61 1.1 christos
62 1.1 christos static const char *amd64_register_names[] =
63 1.1 christos {
64 1.1 christos "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
65 1.1 christos
66 1.1 christos /* %r8 is indeed register number 8. */
67 1.1 christos "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
68 1.1 christos "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
69 1.1 christos
70 1.1 christos /* %st0 is register number 24. */
71 1.1 christos "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
72 1.1 christos "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
73 1.1 christos
74 1.1 christos /* %xmm0 is register number 40. */
75 1.1 christos "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
76 1.1 christos "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
77 1.1 christos "mxcsr",
78 1.1 christos };
79 1.1 christos
80 1.1 christos static const char *amd64_ymm_names[] =
81 1.1 christos {
82 1.1 christos "ymm0", "ymm1", "ymm2", "ymm3",
83 1.1 christos "ymm4", "ymm5", "ymm6", "ymm7",
84 1.1 christos "ymm8", "ymm9", "ymm10", "ymm11",
85 1.1 christos "ymm12", "ymm13", "ymm14", "ymm15"
86 1.1 christos };
87 1.1 christos
88 1.1 christos static const char *amd64_ymmh_names[] =
89 1.1 christos {
90 1.1 christos "ymm0h", "ymm1h", "ymm2h", "ymm3h",
91 1.1 christos "ymm4h", "ymm5h", "ymm6h", "ymm7h",
92 1.1 christos "ymm8h", "ymm9h", "ymm10h", "ymm11h",
93 1.1 christos "ymm12h", "ymm13h", "ymm14h", "ymm15h"
94 1.1 christos };
95 1.1 christos
96 1.1 christos static const char *amd64_mpx_names[] =
97 1.1 christos {
98 1.1 christos "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
99 1.1 christos };
100 1.1 christos
101 1.1 christos /* DWARF Register Number Mapping as defined in the System V psABI,
102 1.1 christos section 3.6. */
103 1.1 christos
104 1.1 christos static int amd64_dwarf_regmap[] =
105 1.1 christos {
106 1.1 christos /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
107 1.1 christos AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
108 1.1 christos AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
109 1.1 christos AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
110 1.1 christos
111 1.1 christos /* Frame Pointer Register RBP. */
112 1.1 christos AMD64_RBP_REGNUM,
113 1.1 christos
114 1.1 christos /* Stack Pointer Register RSP. */
115 1.1 christos AMD64_RSP_REGNUM,
116 1.1 christos
117 1.1 christos /* Extended Integer Registers 8 - 15. */
118 1.1 christos AMD64_R8_REGNUM, /* %r8 */
119 1.1 christos AMD64_R9_REGNUM, /* %r9 */
120 1.1 christos AMD64_R10_REGNUM, /* %r10 */
121 1.1 christos AMD64_R11_REGNUM, /* %r11 */
122 1.1 christos AMD64_R12_REGNUM, /* %r12 */
123 1.1 christos AMD64_R13_REGNUM, /* %r13 */
124 1.1 christos AMD64_R14_REGNUM, /* %r14 */
125 1.1 christos AMD64_R15_REGNUM, /* %r15 */
126 1.1 christos
127 1.1 christos /* Return Address RA. Mapped to RIP. */
128 1.1 christos AMD64_RIP_REGNUM,
129 1.1 christos
130 1.1 christos /* SSE Registers 0 - 7. */
131 1.1 christos AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
132 1.1 christos AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
133 1.1 christos AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
134 1.1 christos AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
135 1.1 christos
136 1.1 christos /* Extended SSE Registers 8 - 15. */
137 1.1 christos AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
138 1.1 christos AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
139 1.1 christos AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
140 1.1 christos AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
141 1.1 christos
142 1.1 christos /* Floating Point Registers 0-7. */
143 1.1 christos AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
144 1.1 christos AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
145 1.1 christos AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
146 1.1 christos AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
147 1.1 christos
148 1.1 christos /* Control and Status Flags Register. */
149 1.1 christos AMD64_EFLAGS_REGNUM,
150 1.1 christos
151 1.1 christos /* Selector Registers. */
152 1.1 christos AMD64_ES_REGNUM,
153 1.1 christos AMD64_CS_REGNUM,
154 1.1 christos AMD64_SS_REGNUM,
155 1.1 christos AMD64_DS_REGNUM,
156 1.1 christos AMD64_FS_REGNUM,
157 1.1 christos AMD64_GS_REGNUM,
158 1.1 christos -1,
159 1.1 christos -1,
160 1.1 christos
161 1.1 christos /* Segment Base Address Registers. */
162 1.1 christos -1,
163 1.1 christos -1,
164 1.1 christos -1,
165 1.1 christos -1,
166 1.1 christos
167 1.1 christos /* Special Selector Registers. */
168 1.1 christos -1,
169 1.1 christos -1,
170 1.1 christos
171 1.1 christos /* Floating Point Control Registers. */
172 1.1 christos AMD64_MXCSR_REGNUM,
173 1.1 christos AMD64_FCTRL_REGNUM,
174 1.1 christos AMD64_FSTAT_REGNUM
175 1.1 christos };
176 1.1 christos
177 1.1 christos static const int amd64_dwarf_regmap_len =
178 1.1 christos (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
179 1.1 christos
180 1.1 christos /* Convert DWARF register number REG to the appropriate register
181 1.1 christos number used by GDB. */
182 1.1 christos
183 1.1 christos static int
184 1.1 christos amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
185 1.1 christos {
186 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
187 1.1 christos int ymm0_regnum = tdep->ymm0_regnum;
188 1.1 christos int regnum = -1;
189 1.1 christos
190 1.1 christos if (reg >= 0 && reg < amd64_dwarf_regmap_len)
191 1.1 christos regnum = amd64_dwarf_regmap[reg];
192 1.1 christos
193 1.1 christos if (regnum == -1)
194 1.1 christos warning (_("Unmapped DWARF Register #%d encountered."), reg);
195 1.1 christos else if (ymm0_regnum >= 0
196 1.1 christos && i386_xmm_regnum_p (gdbarch, regnum))
197 1.1 christos regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
198 1.1 christos
199 1.1 christos return regnum;
200 1.1 christos }
201 1.1 christos
202 1.1 christos /* Map architectural register numbers to gdb register numbers. */
203 1.1 christos
204 1.1 christos static const int amd64_arch_regmap[16] =
205 1.1 christos {
206 1.1 christos AMD64_RAX_REGNUM, /* %rax */
207 1.1 christos AMD64_RCX_REGNUM, /* %rcx */
208 1.1 christos AMD64_RDX_REGNUM, /* %rdx */
209 1.1 christos AMD64_RBX_REGNUM, /* %rbx */
210 1.1 christos AMD64_RSP_REGNUM, /* %rsp */
211 1.1 christos AMD64_RBP_REGNUM, /* %rbp */
212 1.1 christos AMD64_RSI_REGNUM, /* %rsi */
213 1.1 christos AMD64_RDI_REGNUM, /* %rdi */
214 1.1 christos AMD64_R8_REGNUM, /* %r8 */
215 1.1 christos AMD64_R9_REGNUM, /* %r9 */
216 1.1 christos AMD64_R10_REGNUM, /* %r10 */
217 1.1 christos AMD64_R11_REGNUM, /* %r11 */
218 1.1 christos AMD64_R12_REGNUM, /* %r12 */
219 1.1 christos AMD64_R13_REGNUM, /* %r13 */
220 1.1 christos AMD64_R14_REGNUM, /* %r14 */
221 1.1 christos AMD64_R15_REGNUM /* %r15 */
222 1.1 christos };
223 1.1 christos
224 1.1 christos static const int amd64_arch_regmap_len =
225 1.1 christos (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
226 1.1 christos
227 1.1 christos /* Convert architectural register number REG to the appropriate register
228 1.1 christos number used by GDB. */
229 1.1 christos
230 1.1 christos static int
231 1.1 christos amd64_arch_reg_to_regnum (int reg)
232 1.1 christos {
233 1.1 christos gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
234 1.1 christos
235 1.1 christos return amd64_arch_regmap[reg];
236 1.1 christos }
237 1.1 christos
238 1.1 christos /* Register names for byte pseudo-registers. */
239 1.1 christos
240 1.1 christos static const char *amd64_byte_names[] =
241 1.1 christos {
242 1.1 christos "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
243 1.1 christos "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
244 1.1 christos "ah", "bh", "ch", "dh"
245 1.1 christos };
246 1.1 christos
247 1.1 christos /* Number of lower byte registers. */
248 1.1 christos #define AMD64_NUM_LOWER_BYTE_REGS 16
249 1.1 christos
250 1.1 christos /* Register names for word pseudo-registers. */
251 1.1 christos
252 1.1 christos static const char *amd64_word_names[] =
253 1.1 christos {
254 1.1 christos "ax", "bx", "cx", "dx", "si", "di", "bp", "",
255 1.1 christos "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
256 1.1 christos };
257 1.1 christos
258 1.1 christos /* Register names for dword pseudo-registers. */
259 1.1 christos
260 1.1 christos static const char *amd64_dword_names[] =
261 1.1 christos {
262 1.1 christos "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
263 1.1 christos "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
264 1.1 christos "eip"
265 1.1 christos };
266 1.1 christos
267 1.1 christos /* Return the name of register REGNUM. */
268 1.1 christos
269 1.1 christos static const char *
270 1.1 christos amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
271 1.1 christos {
272 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
273 1.1 christos if (i386_byte_regnum_p (gdbarch, regnum))
274 1.1 christos return amd64_byte_names[regnum - tdep->al_regnum];
275 1.1 christos else if (i386_ymm_regnum_p (gdbarch, regnum))
276 1.1 christos return amd64_ymm_names[regnum - tdep->ymm0_regnum];
277 1.1 christos else if (i386_word_regnum_p (gdbarch, regnum))
278 1.1 christos return amd64_word_names[regnum - tdep->ax_regnum];
279 1.1 christos else if (i386_dword_regnum_p (gdbarch, regnum))
280 1.1 christos return amd64_dword_names[regnum - tdep->eax_regnum];
281 1.1 christos else
282 1.1 christos return i386_pseudo_register_name (gdbarch, regnum);
283 1.1 christos }
284 1.1 christos
285 1.1 christos static struct value *
286 1.1 christos amd64_pseudo_register_read_value (struct gdbarch *gdbarch,
287 1.1 christos struct regcache *regcache,
288 1.1 christos int regnum)
289 1.1 christos {
290 1.1 christos gdb_byte raw_buf[MAX_REGISTER_SIZE];
291 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
292 1.1 christos enum register_status status;
293 1.1 christos struct value *result_value;
294 1.1 christos gdb_byte *buf;
295 1.1 christos
296 1.1 christos result_value = allocate_value (register_type (gdbarch, regnum));
297 1.1 christos VALUE_LVAL (result_value) = lval_register;
298 1.1 christos VALUE_REGNUM (result_value) = regnum;
299 1.1 christos buf = value_contents_raw (result_value);
300 1.1 christos
301 1.1 christos if (i386_byte_regnum_p (gdbarch, regnum))
302 1.1 christos {
303 1.1 christos int gpnum = regnum - tdep->al_regnum;
304 1.1 christos
305 1.1 christos /* Extract (always little endian). */
306 1.1 christos if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
307 1.1 christos {
308 1.1 christos /* Special handling for AH, BH, CH, DH. */
309 1.1 christos status = regcache_raw_read (regcache,
310 1.1 christos gpnum - AMD64_NUM_LOWER_BYTE_REGS,
311 1.1 christos raw_buf);
312 1.1 christos if (status == REG_VALID)
313 1.1 christos memcpy (buf, raw_buf + 1, 1);
314 1.1 christos else
315 1.1 christos mark_value_bytes_unavailable (result_value, 0,
316 1.1 christos TYPE_LENGTH (value_type (result_value)));
317 1.1 christos }
318 1.1 christos else
319 1.1 christos {
320 1.1 christos status = regcache_raw_read (regcache, gpnum, raw_buf);
321 1.1 christos if (status == REG_VALID)
322 1.1 christos memcpy (buf, raw_buf, 1);
323 1.1 christos else
324 1.1 christos mark_value_bytes_unavailable (result_value, 0,
325 1.1 christos TYPE_LENGTH (value_type (result_value)));
326 1.1 christos }
327 1.1 christos }
328 1.1 christos else if (i386_dword_regnum_p (gdbarch, regnum))
329 1.1 christos {
330 1.1 christos int gpnum = regnum - tdep->eax_regnum;
331 1.1 christos /* Extract (always little endian). */
332 1.1 christos status = regcache_raw_read (regcache, gpnum, raw_buf);
333 1.1 christos if (status == REG_VALID)
334 1.1 christos memcpy (buf, raw_buf, 4);
335 1.1 christos else
336 1.1 christos mark_value_bytes_unavailable (result_value, 0,
337 1.1 christos TYPE_LENGTH (value_type (result_value)));
338 1.1 christos }
339 1.1 christos else
340 1.1 christos i386_pseudo_register_read_into_value (gdbarch, regcache, regnum,
341 1.1 christos result_value);
342 1.1 christos
343 1.1 christos return result_value;
344 1.1 christos }
345 1.1 christos
346 1.1 christos static void
347 1.1 christos amd64_pseudo_register_write (struct gdbarch *gdbarch,
348 1.1 christos struct regcache *regcache,
349 1.1 christos int regnum, const gdb_byte *buf)
350 1.1 christos {
351 1.1 christos gdb_byte raw_buf[MAX_REGISTER_SIZE];
352 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
353 1.1 christos
354 1.1 christos if (i386_byte_regnum_p (gdbarch, regnum))
355 1.1 christos {
356 1.1 christos int gpnum = regnum - tdep->al_regnum;
357 1.1 christos
358 1.1 christos if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
359 1.1 christos {
360 1.1 christos /* Read ... AH, BH, CH, DH. */
361 1.1 christos regcache_raw_read (regcache,
362 1.1 christos gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
363 1.1 christos /* ... Modify ... (always little endian). */
364 1.1 christos memcpy (raw_buf + 1, buf, 1);
365 1.1 christos /* ... Write. */
366 1.1 christos regcache_raw_write (regcache,
367 1.1 christos gpnum - AMD64_NUM_LOWER_BYTE_REGS, raw_buf);
368 1.1 christos }
369 1.1 christos else
370 1.1 christos {
371 1.1 christos /* Read ... */
372 1.1 christos regcache_raw_read (regcache, gpnum, raw_buf);
373 1.1 christos /* ... Modify ... (always little endian). */
374 1.1 christos memcpy (raw_buf, buf, 1);
375 1.1 christos /* ... Write. */
376 1.1 christos regcache_raw_write (regcache, gpnum, raw_buf);
377 1.1 christos }
378 1.1 christos }
379 1.1 christos else if (i386_dword_regnum_p (gdbarch, regnum))
380 1.1 christos {
381 1.1 christos int gpnum = regnum - tdep->eax_regnum;
382 1.1 christos
383 1.1 christos /* Read ... */
384 1.1 christos regcache_raw_read (regcache, gpnum, raw_buf);
385 1.1 christos /* ... Modify ... (always little endian). */
386 1.1 christos memcpy (raw_buf, buf, 4);
387 1.1 christos /* ... Write. */
388 1.1 christos regcache_raw_write (regcache, gpnum, raw_buf);
389 1.1 christos }
390 1.1 christos else
391 1.1 christos i386_pseudo_register_write (gdbarch, regcache, regnum, buf);
392 1.1 christos }
393 1.1 christos
394 1.1 christos
395 1.1 christos
397 1.1 christos /* Register classes as defined in the psABI. */
398 1.1 christos
399 1.1 christos enum amd64_reg_class
400 1.1 christos {
401 1.1 christos AMD64_INTEGER,
402 1.1 christos AMD64_SSE,
403 1.1 christos AMD64_SSEUP,
404 1.1 christos AMD64_X87,
405 1.1 christos AMD64_X87UP,
406 1.1 christos AMD64_COMPLEX_X87,
407 1.1 christos AMD64_NO_CLASS,
408 1.1 christos AMD64_MEMORY
409 1.1 christos };
410 1.1 christos
411 1.1 christos /* Return the union class of CLASS1 and CLASS2. See the psABI for
412 1.1 christos details. */
413 1.1 christos
414 1.1 christos static enum amd64_reg_class
415 1.1 christos amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
416 1.1 christos {
417 1.1 christos /* Rule (a): If both classes are equal, this is the resulting class. */
418 1.1 christos if (class1 == class2)
419 1.1 christos return class1;
420 1.1 christos
421 1.1 christos /* Rule (b): If one of the classes is NO_CLASS, the resulting class
422 1.1 christos is the other class. */
423 1.1 christos if (class1 == AMD64_NO_CLASS)
424 1.1 christos return class2;
425 1.1 christos if (class2 == AMD64_NO_CLASS)
426 1.1 christos return class1;
427 1.1 christos
428 1.1 christos /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
429 1.1 christos if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
430 1.1 christos return AMD64_MEMORY;
431 1.1 christos
432 1.1 christos /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
433 1.1 christos if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
434 1.1 christos return AMD64_INTEGER;
435 1.1 christos
436 1.1 christos /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
437 1.1 christos MEMORY is used as class. */
438 1.1 christos if (class1 == AMD64_X87 || class1 == AMD64_X87UP
439 1.1 christos || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
440 1.1 christos || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
441 1.1 christos return AMD64_MEMORY;
442 1.1 christos
443 1.1 christos /* Rule (f): Otherwise class SSE is used. */
444 1.1 christos return AMD64_SSE;
445 1.1 christos }
446 1.1 christos
447 1.1 christos static void amd64_classify (struct type *type, enum amd64_reg_class class[2]);
448 1.1 christos
449 1.1 christos /* Return non-zero if TYPE is a non-POD structure or union type. */
450 1.1 christos
451 1.1 christos static int
452 1.1 christos amd64_non_pod_p (struct type *type)
453 1.1 christos {
454 1.1 christos /* ??? A class with a base class certainly isn't POD, but does this
455 1.1 christos catch all non-POD structure types? */
456 1.1 christos if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
457 1.1 christos return 1;
458 1.1 christos
459 1.1 christos return 0;
460 1.1 christos }
461 1.1 christos
462 1.1 christos /* Classify TYPE according to the rules for aggregate (structures and
463 1.1 christos arrays) and union types, and store the result in CLASS. */
464 1.1 christos
465 1.1 christos static void
466 1.1 christos amd64_classify_aggregate (struct type *type, enum amd64_reg_class class[2])
467 1.1 christos {
468 1.1 christos /* 1. If the size of an object is larger than two eightbytes, or in
469 1.1 christos C++, is a non-POD structure or union type, or contains
470 1.1 christos unaligned fields, it has class memory. */
471 1.1 christos if (TYPE_LENGTH (type) > 16 || amd64_non_pod_p (type))
472 1.1 christos {
473 1.1 christos class[0] = class[1] = AMD64_MEMORY;
474 1.1 christos return;
475 1.1 christos }
476 1.1 christos
477 1.1 christos /* 2. Both eightbytes get initialized to class NO_CLASS. */
478 1.1 christos class[0] = class[1] = AMD64_NO_CLASS;
479 1.1 christos
480 1.1 christos /* 3. Each field of an object is classified recursively so that
481 1.1 christos always two fields are considered. The resulting class is
482 1.1 christos calculated according to the classes of the fields in the
483 1.1 christos eightbyte: */
484 1.1 christos
485 1.1 christos if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
486 1.1 christos {
487 1.1 christos struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
488 1.1 christos
489 1.1 christos /* All fields in an array have the same type. */
490 1.1 christos amd64_classify (subtype, class);
491 1.1 christos if (TYPE_LENGTH (type) > 8 && class[1] == AMD64_NO_CLASS)
492 1.1 christos class[1] = class[0];
493 1.1 christos }
494 1.1 christos else
495 1.1 christos {
496 1.1 christos int i;
497 1.1 christos
498 1.1 christos /* Structure or union. */
499 1.1 christos gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
500 1.1 christos || TYPE_CODE (type) == TYPE_CODE_UNION);
501 1.1 christos
502 1.1 christos for (i = 0; i < TYPE_NFIELDS (type); i++)
503 1.1 christos {
504 1.1 christos struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
505 1.1 christos int pos = TYPE_FIELD_BITPOS (type, i) / 64;
506 1.1 christos enum amd64_reg_class subclass[2];
507 1.1 christos int bitsize = TYPE_FIELD_BITSIZE (type, i);
508 1.1 christos int endpos;
509 1.1 christos
510 1.1 christos if (bitsize == 0)
511 1.1 christos bitsize = TYPE_LENGTH (subtype) * 8;
512 1.1 christos endpos = (TYPE_FIELD_BITPOS (type, i) + bitsize - 1) / 64;
513 1.1 christos
514 1.1 christos /* Ignore static fields. */
515 1.1 christos if (field_is_static (&TYPE_FIELD (type, i)))
516 1.1 christos continue;
517 1.1 christos
518 1.1 christos gdb_assert (pos == 0 || pos == 1);
519 1.1 christos
520 1.1 christos amd64_classify (subtype, subclass);
521 1.1 christos class[pos] = amd64_merge_classes (class[pos], subclass[0]);
522 1.1 christos if (bitsize <= 64 && pos == 0 && endpos == 1)
523 1.1 christos /* This is a bit of an odd case: We have a field that would
524 1.1 christos normally fit in one of the two eightbytes, except that
525 1.1 christos it is placed in a way that this field straddles them.
526 1.1 christos This has been seen with a structure containing an array.
527 1.1 christos
528 1.1 christos The ABI is a bit unclear in this case, but we assume that
529 1.1 christos this field's class (stored in subclass[0]) must also be merged
530 1.1 christos into class[1]. In other words, our field has a piece stored
531 1.1 christos in the second eight-byte, and thus its class applies to
532 1.1 christos the second eight-byte as well.
533 1.1 christos
534 1.1 christos In the case where the field length exceeds 8 bytes,
535 1.1 christos it should not be necessary to merge the field class
536 1.1 christos into class[1]. As LEN > 8, subclass[1] is necessarily
537 1.1 christos different from AMD64_NO_CLASS. If subclass[1] is equal
538 1.1 christos to subclass[0], then the normal class[1]/subclass[1]
539 1.1 christos merging will take care of everything. For subclass[1]
540 1.1 christos to be different from subclass[0], I can only see the case
541 1.1 christos where we have a SSE/SSEUP or X87/X87UP pair, which both
542 1.1 christos use up all 16 bytes of the aggregate, and are already
543 1.1 christos handled just fine (because each portion sits on its own
544 1.1 christos 8-byte). */
545 1.1 christos class[1] = amd64_merge_classes (class[1], subclass[0]);
546 1.1 christos if (pos == 0)
547 1.1 christos class[1] = amd64_merge_classes (class[1], subclass[1]);
548 1.1 christos }
549 1.1 christos }
550 1.1 christos
551 1.1 christos /* 4. Then a post merger cleanup is done: */
552 1.1 christos
553 1.1 christos /* Rule (a): If one of the classes is MEMORY, the whole argument is
554 1.1 christos passed in memory. */
555 1.1 christos if (class[0] == AMD64_MEMORY || class[1] == AMD64_MEMORY)
556 1.1 christos class[0] = class[1] = AMD64_MEMORY;
557 1.1 christos
558 1.1 christos /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
559 1.1 christos SSE. */
560 1.1 christos if (class[0] == AMD64_SSEUP)
561 1.1 christos class[0] = AMD64_SSE;
562 1.1 christos if (class[1] == AMD64_SSEUP && class[0] != AMD64_SSE)
563 1.1 christos class[1] = AMD64_SSE;
564 1.1 christos }
565 1.1 christos
566 1.1 christos /* Classify TYPE, and store the result in CLASS. */
567 1.1 christos
568 1.1 christos static void
569 1.1 christos amd64_classify (struct type *type, enum amd64_reg_class class[2])
570 1.1 christos {
571 1.1 christos enum type_code code = TYPE_CODE (type);
572 1.1 christos int len = TYPE_LENGTH (type);
573 1.1 christos
574 1.1 christos class[0] = class[1] = AMD64_NO_CLASS;
575 1.1 christos
576 1.1 christos /* Arguments of types (signed and unsigned) _Bool, char, short, int,
577 1.1 christos long, long long, and pointers are in the INTEGER class. Similarly,
578 1.1 christos range types, used by languages such as Ada, are also in the INTEGER
579 1.1 christos class. */
580 1.1 christos if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
581 1.1 christos || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
582 1.1 christos || code == TYPE_CODE_CHAR
583 1.1 christos || code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
584 1.1 christos && (len == 1 || len == 2 || len == 4 || len == 8))
585 1.1 christos class[0] = AMD64_INTEGER;
586 1.1 christos
587 1.1 christos /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
588 1.1 christos are in class SSE. */
589 1.1 christos else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
590 1.1 christos && (len == 4 || len == 8))
591 1.1 christos /* FIXME: __m64 . */
592 1.1 christos class[0] = AMD64_SSE;
593 1.1 christos
594 1.1 christos /* Arguments of types __float128, _Decimal128 and __m128 are split into
595 1.1 christos two halves. The least significant ones belong to class SSE, the most
596 1.1 christos significant one to class SSEUP. */
597 1.1 christos else if (code == TYPE_CODE_DECFLOAT && len == 16)
598 1.1 christos /* FIXME: __float128, __m128. */
599 1.1 christos class[0] = AMD64_SSE, class[1] = AMD64_SSEUP;
600 1.1 christos
601 1.1 christos /* The 64-bit mantissa of arguments of type long double belongs to
602 1.1 christos class X87, the 16-bit exponent plus 6 bytes of padding belongs to
603 1.1 christos class X87UP. */
604 1.1 christos else if (code == TYPE_CODE_FLT && len == 16)
605 1.1 christos /* Class X87 and X87UP. */
606 1.1 christos class[0] = AMD64_X87, class[1] = AMD64_X87UP;
607 1.1 christos
608 1.1 christos /* Arguments of complex T where T is one of the types float or
609 1.1 christos double get treated as if they are implemented as:
610 1.1 christos
611 1.1 christos struct complexT {
612 1.1 christos T real;
613 1.1 christos T imag;
614 1.1 christos }; */
615 1.1 christos else if (code == TYPE_CODE_COMPLEX && len == 8)
616 1.1 christos class[0] = AMD64_SSE;
617 1.1 christos else if (code == TYPE_CODE_COMPLEX && len == 16)
618 1.1 christos class[0] = class[1] = AMD64_SSE;
619 1.1 christos
620 1.1 christos /* A variable of type complex long double is classified as type
621 1.1 christos COMPLEX_X87. */
622 1.1 christos else if (code == TYPE_CODE_COMPLEX && len == 32)
623 1.1 christos class[0] = AMD64_COMPLEX_X87;
624 1.1 christos
625 1.1 christos /* Aggregates. */
626 1.1 christos else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
627 1.1 christos || code == TYPE_CODE_UNION)
628 1.1 christos amd64_classify_aggregate (type, class);
629 1.1 christos }
630 1.1 christos
631 1.1 christos static enum return_value_convention
632 1.1 christos amd64_return_value (struct gdbarch *gdbarch, struct value *function,
633 1.1 christos struct type *type, struct regcache *regcache,
634 1.1 christos gdb_byte *readbuf, const gdb_byte *writebuf)
635 1.1 christos {
636 1.1 christos enum amd64_reg_class class[2];
637 1.1 christos int len = TYPE_LENGTH (type);
638 1.1 christos static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
639 1.1 christos static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
640 1.1 christos int integer_reg = 0;
641 1.1 christos int sse_reg = 0;
642 1.1 christos int i;
643 1.1 christos
644 1.1 christos gdb_assert (!(readbuf && writebuf));
645 1.1 christos
646 1.1 christos /* 1. Classify the return type with the classification algorithm. */
647 1.1 christos amd64_classify (type, class);
648 1.1 christos
649 1.1 christos /* 2. If the type has class MEMORY, then the caller provides space
650 1.1 christos for the return value and passes the address of this storage in
651 1.1 christos %rdi as if it were the first argument to the function. In effect,
652 1.1 christos this address becomes a hidden first argument.
653 1.1 christos
654 1.1 christos On return %rax will contain the address that has been passed in
655 1.1 christos by the caller in %rdi. */
656 1.1 christos if (class[0] == AMD64_MEMORY)
657 1.1 christos {
658 1.1 christos /* As indicated by the comment above, the ABI guarantees that we
659 1.1 christos can always find the return value just after the function has
660 1.1 christos returned. */
661 1.1 christos
662 1.1 christos if (readbuf)
663 1.1 christos {
664 1.1 christos ULONGEST addr;
665 1.1 christos
666 1.1 christos regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
667 1.1 christos read_memory (addr, readbuf, TYPE_LENGTH (type));
668 1.1 christos }
669 1.1 christos
670 1.1 christos return RETURN_VALUE_ABI_RETURNS_ADDRESS;
671 1.1 christos }
672 1.1 christos
673 1.1 christos /* 8. If the class is COMPLEX_X87, the real part of the value is
674 1.1 christos returned in %st0 and the imaginary part in %st1. */
675 1.1 christos if (class[0] == AMD64_COMPLEX_X87)
676 1.1 christos {
677 1.1 christos if (readbuf)
678 1.1 christos {
679 1.1 christos regcache_raw_read (regcache, AMD64_ST0_REGNUM, readbuf);
680 1.1 christos regcache_raw_read (regcache, AMD64_ST1_REGNUM, readbuf + 16);
681 1.1 christos }
682 1.1 christos
683 1.1 christos if (writebuf)
684 1.1 christos {
685 1.1 christos i387_return_value (gdbarch, regcache);
686 1.1 christos regcache_raw_write (regcache, AMD64_ST0_REGNUM, writebuf);
687 1.1 christos regcache_raw_write (regcache, AMD64_ST1_REGNUM, writebuf + 16);
688 1.1 christos
689 1.1 christos /* Fix up the tag word such that both %st(0) and %st(1) are
690 1.1 christos marked as valid. */
691 1.1 christos regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
692 1.1 christos }
693 1.1 christos
694 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
695 1.1 christos }
696 1.1 christos
697 1.1 christos gdb_assert (class[1] != AMD64_MEMORY);
698 1.1 christos gdb_assert (len <= 16);
699 1.1 christos
700 1.1 christos for (i = 0; len > 0; i++, len -= 8)
701 1.1 christos {
702 1.1 christos int regnum = -1;
703 1.1 christos int offset = 0;
704 1.1 christos
705 1.1 christos switch (class[i])
706 1.1 christos {
707 1.1 christos case AMD64_INTEGER:
708 1.1 christos /* 3. If the class is INTEGER, the next available register
709 1.1 christos of the sequence %rax, %rdx is used. */
710 1.1 christos regnum = integer_regnum[integer_reg++];
711 1.1 christos break;
712 1.1 christos
713 1.1 christos case AMD64_SSE:
714 1.1 christos /* 4. If the class is SSE, the next available SSE register
715 1.1 christos of the sequence %xmm0, %xmm1 is used. */
716 1.1 christos regnum = sse_regnum[sse_reg++];
717 1.1 christos break;
718 1.1 christos
719 1.1 christos case AMD64_SSEUP:
720 1.1 christos /* 5. If the class is SSEUP, the eightbyte is passed in the
721 1.1 christos upper half of the last used SSE register. */
722 1.1 christos gdb_assert (sse_reg > 0);
723 1.1 christos regnum = sse_regnum[sse_reg - 1];
724 1.1 christos offset = 8;
725 1.1 christos break;
726 1.1 christos
727 1.1 christos case AMD64_X87:
728 1.1 christos /* 6. If the class is X87, the value is returned on the X87
729 1.1 christos stack in %st0 as 80-bit x87 number. */
730 1.1 christos regnum = AMD64_ST0_REGNUM;
731 1.1 christos if (writebuf)
732 1.1 christos i387_return_value (gdbarch, regcache);
733 1.1 christos break;
734 1.1 christos
735 1.1 christos case AMD64_X87UP:
736 1.1 christos /* 7. If the class is X87UP, the value is returned together
737 1.1 christos with the previous X87 value in %st0. */
738 1.1 christos gdb_assert (i > 0 && class[0] == AMD64_X87);
739 1.1 christos regnum = AMD64_ST0_REGNUM;
740 1.1 christos offset = 8;
741 1.1 christos len = 2;
742 1.1 christos break;
743 1.1 christos
744 1.1 christos case AMD64_NO_CLASS:
745 1.1 christos continue;
746 1.1 christos
747 1.1 christos default:
748 1.1 christos gdb_assert (!"Unexpected register class.");
749 1.1 christos }
750 1.1 christos
751 1.1 christos gdb_assert (regnum != -1);
752 1.1 christos
753 1.1 christos if (readbuf)
754 1.1 christos regcache_raw_read_part (regcache, regnum, offset, min (len, 8),
755 1.1 christos readbuf + i * 8);
756 1.1 christos if (writebuf)
757 1.1 christos regcache_raw_write_part (regcache, regnum, offset, min (len, 8),
758 1.1 christos writebuf + i * 8);
759 1.1 christos }
760 1.1 christos
761 1.1 christos return RETURN_VALUE_REGISTER_CONVENTION;
762 1.1 christos }
763 1.1 christos
764 1.1 christos
766 1.1 christos static CORE_ADDR
767 1.1 christos amd64_push_arguments (struct regcache *regcache, int nargs,
768 1.1 christos struct value **args, CORE_ADDR sp, int struct_return)
769 1.1 christos {
770 1.1 christos static int integer_regnum[] =
771 1.1 christos {
772 1.1 christos AMD64_RDI_REGNUM, /* %rdi */
773 1.1 christos AMD64_RSI_REGNUM, /* %rsi */
774 1.1 christos AMD64_RDX_REGNUM, /* %rdx */
775 1.1 christos AMD64_RCX_REGNUM, /* %rcx */
776 1.1 christos AMD64_R8_REGNUM, /* %r8 */
777 1.1 christos AMD64_R9_REGNUM /* %r9 */
778 1.1 christos };
779 1.1 christos static int sse_regnum[] =
780 1.1 christos {
781 1.1 christos /* %xmm0 ... %xmm7 */
782 1.1 christos AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
783 1.1 christos AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
784 1.1 christos AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
785 1.1 christos AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
786 1.1 christos };
787 1.1 christos struct value **stack_args = alloca (nargs * sizeof (struct value *));
788 1.1 christos int num_stack_args = 0;
789 1.1 christos int num_elements = 0;
790 1.1 christos int element = 0;
791 1.1 christos int integer_reg = 0;
792 1.1 christos int sse_reg = 0;
793 1.1 christos int i;
794 1.1 christos
795 1.1 christos /* Reserve a register for the "hidden" argument. */
796 1.1 christos if (struct_return)
797 1.1 christos integer_reg++;
798 1.1 christos
799 1.1 christos for (i = 0; i < nargs; i++)
800 1.1 christos {
801 1.1 christos struct type *type = value_type (args[i]);
802 1.1 christos int len = TYPE_LENGTH (type);
803 1.1 christos enum amd64_reg_class class[2];
804 1.1 christos int needed_integer_regs = 0;
805 1.1 christos int needed_sse_regs = 0;
806 1.1 christos int j;
807 1.1 christos
808 1.1 christos /* Classify argument. */
809 1.1 christos amd64_classify (type, class);
810 1.1 christos
811 1.1 christos /* Calculate the number of integer and SSE registers needed for
812 1.1 christos this argument. */
813 1.1 christos for (j = 0; j < 2; j++)
814 1.1 christos {
815 1.1 christos if (class[j] == AMD64_INTEGER)
816 1.1 christos needed_integer_regs++;
817 1.1 christos else if (class[j] == AMD64_SSE)
818 1.1 christos needed_sse_regs++;
819 1.1 christos }
820 1.1 christos
821 1.1 christos /* Check whether enough registers are available, and if the
822 1.1 christos argument should be passed in registers at all. */
823 1.1 christos if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
824 1.1 christos || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
825 1.1 christos || (needed_integer_regs == 0 && needed_sse_regs == 0))
826 1.1 christos {
827 1.1 christos /* The argument will be passed on the stack. */
828 1.1 christos num_elements += ((len + 7) / 8);
829 1.1 christos stack_args[num_stack_args++] = args[i];
830 1.1 christos }
831 1.1 christos else
832 1.1 christos {
833 1.1 christos /* The argument will be passed in registers. */
834 1.1 christos const gdb_byte *valbuf = value_contents (args[i]);
835 1.1 christos gdb_byte buf[8];
836 1.1 christos
837 1.1 christos gdb_assert (len <= 16);
838 1.1 christos
839 1.1 christos for (j = 0; len > 0; j++, len -= 8)
840 1.1 christos {
841 1.1 christos int regnum = -1;
842 1.1 christos int offset = 0;
843 1.1 christos
844 1.1 christos switch (class[j])
845 1.1 christos {
846 1.1 christos case AMD64_INTEGER:
847 1.1 christos regnum = integer_regnum[integer_reg++];
848 1.1 christos break;
849 1.1 christos
850 1.1 christos case AMD64_SSE:
851 1.1 christos regnum = sse_regnum[sse_reg++];
852 1.1 christos break;
853 1.1 christos
854 1.1 christos case AMD64_SSEUP:
855 1.1 christos gdb_assert (sse_reg > 0);
856 1.1 christos regnum = sse_regnum[sse_reg - 1];
857 1.1 christos offset = 8;
858 1.1 christos break;
859 1.1 christos
860 1.1 christos default:
861 1.1 christos gdb_assert (!"Unexpected register class.");
862 1.1 christos }
863 1.1 christos
864 1.1 christos gdb_assert (regnum != -1);
865 1.1 christos memset (buf, 0, sizeof buf);
866 1.1 christos memcpy (buf, valbuf + j * 8, min (len, 8));
867 1.1 christos regcache_raw_write_part (regcache, regnum, offset, 8, buf);
868 1.1 christos }
869 1.1 christos }
870 1.1 christos }
871 1.1 christos
872 1.1 christos /* Allocate space for the arguments on the stack. */
873 1.1 christos sp -= num_elements * 8;
874 1.1 christos
875 1.1 christos /* The psABI says that "The end of the input argument area shall be
876 1.1 christos aligned on a 16 byte boundary." */
877 1.1 christos sp &= ~0xf;
878 1.1 christos
879 1.1 christos /* Write out the arguments to the stack. */
880 1.1 christos for (i = 0; i < num_stack_args; i++)
881 1.1 christos {
882 1.1 christos struct type *type = value_type (stack_args[i]);
883 1.1 christos const gdb_byte *valbuf = value_contents (stack_args[i]);
884 1.1 christos int len = TYPE_LENGTH (type);
885 1.1 christos
886 1.1 christos write_memory (sp + element * 8, valbuf, len);
887 1.1 christos element += ((len + 7) / 8);
888 1.1 christos }
889 1.1 christos
890 1.1 christos /* The psABI says that "For calls that may call functions that use
891 1.1 christos varargs or stdargs (prototype-less calls or calls to functions
892 1.1 christos containing ellipsis (...) in the declaration) %al is used as
893 1.1 christos hidden argument to specify the number of SSE registers used. */
894 1.1 christos regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
895 1.1 christos return sp;
896 1.1 christos }
897 1.1 christos
898 1.1 christos static CORE_ADDR
899 1.1 christos amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
900 1.1 christos struct regcache *regcache, CORE_ADDR bp_addr,
901 1.1 christos int nargs, struct value **args, CORE_ADDR sp,
902 1.1 christos int struct_return, CORE_ADDR struct_addr)
903 1.1 christos {
904 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
905 1.1 christos gdb_byte buf[8];
906 1.1 christos
907 1.1 christos /* Pass arguments. */
908 1.1 christos sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
909 1.1 christos
910 1.1 christos /* Pass "hidden" argument". */
911 1.1 christos if (struct_return)
912 1.1 christos {
913 1.1 christos store_unsigned_integer (buf, 8, byte_order, struct_addr);
914 1.1 christos regcache_cooked_write (regcache, AMD64_RDI_REGNUM, buf);
915 1.1 christos }
916 1.1 christos
917 1.1 christos /* Store return address. */
918 1.1 christos sp -= 8;
919 1.1 christos store_unsigned_integer (buf, 8, byte_order, bp_addr);
920 1.1 christos write_memory (sp, buf, 8);
921 1.1 christos
922 1.1 christos /* Finally, update the stack pointer... */
923 1.1 christos store_unsigned_integer (buf, 8, byte_order, sp);
924 1.1 christos regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);
925 1.1 christos
926 1.1 christos /* ...and fake a frame pointer. */
927 1.1 christos regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);
928 1.1 christos
929 1.1 christos return sp + 16;
930 1.1 christos }
931 1.1 christos
932 1.1 christos /* Displaced instruction handling. */
934 1.1 christos
935 1.1 christos /* A partially decoded instruction.
936 1.1 christos This contains enough details for displaced stepping purposes. */
937 1.1 christos
938 1.1 christos struct amd64_insn
939 1.1 christos {
940 1.1 christos /* The number of opcode bytes. */
941 1.1 christos int opcode_len;
942 1.1 christos /* The offset of the rex prefix or -1 if not present. */
943 1.1 christos int rex_offset;
944 1.1 christos /* The offset to the first opcode byte. */
945 1.1 christos int opcode_offset;
946 1.1 christos /* The offset to the modrm byte or -1 if not present. */
947 1.1 christos int modrm_offset;
948 1.1 christos
949 1.1 christos /* The raw instruction. */
950 1.1 christos gdb_byte *raw_insn;
951 1.1 christos };
952 1.1 christos
953 1.1 christos struct displaced_step_closure
954 1.1 christos {
955 1.1 christos /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
956 1.1 christos int tmp_used;
957 1.1 christos int tmp_regno;
958 1.1 christos ULONGEST tmp_save;
959 1.1 christos
960 1.1 christos /* Details of the instruction. */
961 1.1 christos struct amd64_insn insn_details;
962 1.1 christos
963 1.1 christos /* Amount of space allocated to insn_buf. */
964 1.1 christos int max_len;
965 1.1 christos
966 1.1 christos /* The possibly modified insn.
967 1.1 christos This is a variable-length field. */
968 1.1 christos gdb_byte insn_buf[1];
969 1.1 christos };
970 1.1 christos
971 1.1 christos /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
972 1.1 christos ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
973 1.1 christos at which point delete these in favor of libopcodes' versions). */
974 1.1 christos
975 1.1 christos static const unsigned char onebyte_has_modrm[256] = {
976 1.1 christos /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
977 1.1 christos /* ------------------------------- */
978 1.1 christos /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
979 1.1 christos /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
980 1.1 christos /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
981 1.1 christos /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
982 1.1 christos /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
983 1.1 christos /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
984 1.1 christos /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
985 1.1 christos /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
986 1.1 christos /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
987 1.1 christos /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
988 1.1 christos /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
989 1.1 christos /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
990 1.1 christos /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
991 1.1 christos /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
992 1.1 christos /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
993 1.1 christos /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
994 1.1 christos /* ------------------------------- */
995 1.1 christos /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
996 1.1 christos };
997 1.1 christos
998 1.1 christos static const unsigned char twobyte_has_modrm[256] = {
999 1.1 christos /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1000 1.1 christos /* ------------------------------- */
1001 1.1 christos /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1002 1.1 christos /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
1003 1.1 christos /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
1004 1.1 christos /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
1005 1.1 christos /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1006 1.1 christos /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1007 1.1 christos /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1008 1.1 christos /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
1009 1.1 christos /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1010 1.1 christos /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1011 1.1 christos /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
1012 1.1 christos /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
1013 1.1 christos /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1014 1.1 christos /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1015 1.1 christos /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1016 1.1 christos /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1017 1.1 christos /* ------------------------------- */
1018 1.1 christos /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1019 1.1 christos };
1020 1.1 christos
1021 1.1 christos static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
1022 1.1 christos
1023 1.1 christos static int
1024 1.1 christos rex_prefix_p (gdb_byte pfx)
1025 1.1 christos {
1026 1.1 christos return REX_PREFIX_P (pfx);
1027 1.1 christos }
1028 1.1 christos
1029 1.1 christos /* Skip the legacy instruction prefixes in INSN.
1030 1.1 christos We assume INSN is properly sentineled so we don't have to worry
1031 1.1 christos about falling off the end of the buffer. */
1032 1.1 christos
1033 1.1 christos static gdb_byte *
1034 1.1 christos amd64_skip_prefixes (gdb_byte *insn)
1035 1.1 christos {
1036 1.1 christos while (1)
1037 1.1 christos {
1038 1.1 christos switch (*insn)
1039 1.1 christos {
1040 1.1 christos case DATA_PREFIX_OPCODE:
1041 1.1 christos case ADDR_PREFIX_OPCODE:
1042 1.1 christos case CS_PREFIX_OPCODE:
1043 1.1 christos case DS_PREFIX_OPCODE:
1044 1.1 christos case ES_PREFIX_OPCODE:
1045 1.1 christos case FS_PREFIX_OPCODE:
1046 1.1 christos case GS_PREFIX_OPCODE:
1047 1.1 christos case SS_PREFIX_OPCODE:
1048 1.1 christos case LOCK_PREFIX_OPCODE:
1049 1.1 christos case REPE_PREFIX_OPCODE:
1050 1.1 christos case REPNE_PREFIX_OPCODE:
1051 1.1 christos ++insn;
1052 1.1 christos continue;
1053 1.1 christos default:
1054 1.1 christos break;
1055 1.1 christos }
1056 1.1 christos break;
1057 1.1 christos }
1058 1.1 christos
1059 1.1 christos return insn;
1060 1.1 christos }
1061 1.1 christos
1062 1.1 christos /* Return an integer register (other than RSP) that is unused as an input
1063 1.1 christos operand in INSN.
1064 1.1 christos In order to not require adding a rex prefix if the insn doesn't already
1065 1.1 christos have one, the result is restricted to RAX ... RDI, sans RSP.
1066 1.1 christos The register numbering of the result follows architecture ordering,
1067 1.1 christos e.g. RDI = 7. */
1068 1.1 christos
1069 1.1 christos static int
1070 1.1 christos amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1071 1.1 christos {
1072 1.1 christos /* 1 bit for each reg */
1073 1.1 christos int used_regs_mask = 0;
1074 1.1 christos
1075 1.1 christos /* There can be at most 3 int regs used as inputs in an insn, and we have
1076 1.1 christos 7 to choose from (RAX ... RDI, sans RSP).
1077 1.1 christos This allows us to take a conservative approach and keep things simple.
1078 1.1 christos E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1079 1.1 christos that implicitly specify RAX. */
1080 1.1 christos
1081 1.1 christos /* Avoid RAX. */
1082 1.1 christos used_regs_mask |= 1 << EAX_REG_NUM;
1083 1.1 christos /* Similarily avoid RDX, implicit operand in divides. */
1084 1.1 christos used_regs_mask |= 1 << EDX_REG_NUM;
1085 1.1 christos /* Avoid RSP. */
1086 1.1 christos used_regs_mask |= 1 << ESP_REG_NUM;
1087 1.1 christos
1088 1.1 christos /* If the opcode is one byte long and there's no ModRM byte,
1089 1.1 christos assume the opcode specifies a register. */
1090 1.1 christos if (details->opcode_len == 1 && details->modrm_offset == -1)
1091 1.1 christos used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1092 1.1 christos
1093 1.1 christos /* Mark used regs in the modrm/sib bytes. */
1094 1.1 christos if (details->modrm_offset != -1)
1095 1.1 christos {
1096 1.1 christos int modrm = details->raw_insn[details->modrm_offset];
1097 1.1 christos int mod = MODRM_MOD_FIELD (modrm);
1098 1.1 christos int reg = MODRM_REG_FIELD (modrm);
1099 1.1 christos int rm = MODRM_RM_FIELD (modrm);
1100 1.1 christos int have_sib = mod != 3 && rm == 4;
1101 1.1 christos
1102 1.1 christos /* Assume the reg field of the modrm byte specifies a register. */
1103 1.1 christos used_regs_mask |= 1 << reg;
1104 1.1 christos
1105 1.1 christos if (have_sib)
1106 1.1 christos {
1107 1.1 christos int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1108 1.1 christos int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1109 1.1 christos used_regs_mask |= 1 << base;
1110 1.1 christos used_regs_mask |= 1 << idx;
1111 1.1 christos }
1112 1.1 christos else
1113 1.1 christos {
1114 1.1 christos used_regs_mask |= 1 << rm;
1115 1.1 christos }
1116 1.1 christos }
1117 1.1 christos
1118 1.1 christos gdb_assert (used_regs_mask < 256);
1119 1.1 christos gdb_assert (used_regs_mask != 255);
1120 1.1 christos
1121 1.1 christos /* Finally, find a free reg. */
1122 1.1 christos {
1123 1.1 christos int i;
1124 1.1 christos
1125 1.1 christos for (i = 0; i < 8; ++i)
1126 1.1 christos {
1127 1.1 christos if (! (used_regs_mask & (1 << i)))
1128 1.1 christos return i;
1129 1.1 christos }
1130 1.1 christos
1131 1.1 christos /* We shouldn't get here. */
1132 1.1 christos internal_error (__FILE__, __LINE__, _("unable to find free reg"));
1133 1.1 christos }
1134 1.1 christos }
1135 1.1 christos
1136 1.1 christos /* Extract the details of INSN that we need. */
1137 1.1 christos
1138 1.1 christos static void
1139 1.1 christos amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1140 1.1 christos {
1141 1.1 christos gdb_byte *start = insn;
1142 1.1 christos int need_modrm;
1143 1.1 christos
1144 1.1 christos details->raw_insn = insn;
1145 1.1 christos
1146 1.1 christos details->opcode_len = -1;
1147 1.1 christos details->rex_offset = -1;
1148 1.1 christos details->opcode_offset = -1;
1149 1.1 christos details->modrm_offset = -1;
1150 1.1 christos
1151 1.1 christos /* Skip legacy instruction prefixes. */
1152 1.1 christos insn = amd64_skip_prefixes (insn);
1153 1.1 christos
1154 1.1 christos /* Skip REX instruction prefix. */
1155 1.1 christos if (rex_prefix_p (*insn))
1156 1.1 christos {
1157 1.1 christos details->rex_offset = insn - start;
1158 1.1 christos ++insn;
1159 1.1 christos }
1160 1.1 christos
1161 1.1 christos details->opcode_offset = insn - start;
1162 1.1 christos
1163 1.1 christos if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1164 1.1 christos {
1165 1.1 christos /* Two or three-byte opcode. */
1166 1.1 christos ++insn;
1167 1.1 christos need_modrm = twobyte_has_modrm[*insn];
1168 1.1 christos
1169 1.1 christos /* Check for three-byte opcode. */
1170 1.1 christos switch (*insn)
1171 1.1 christos {
1172 1.1 christos case 0x24:
1173 1.1 christos case 0x25:
1174 1.1 christos case 0x38:
1175 1.1 christos case 0x3a:
1176 1.1 christos case 0x7a:
1177 1.1 christos case 0x7b:
1178 1.1 christos ++insn;
1179 1.1 christos details->opcode_len = 3;
1180 1.1 christos break;
1181 1.1 christos default:
1182 1.1 christos details->opcode_len = 2;
1183 1.1 christos break;
1184 1.1 christos }
1185 1.1 christos }
1186 1.1 christos else
1187 1.1 christos {
1188 1.1 christos /* One-byte opcode. */
1189 1.1 christos need_modrm = onebyte_has_modrm[*insn];
1190 1.1 christos details->opcode_len = 1;
1191 1.1 christos }
1192 1.1 christos
1193 1.1 christos if (need_modrm)
1194 1.1 christos {
1195 1.1 christos ++insn;
1196 1.1 christos details->modrm_offset = insn - start;
1197 1.1 christos }
1198 1.1 christos }
1199 1.1 christos
1200 1.1 christos /* Update %rip-relative addressing in INSN.
1201 1.1 christos
1202 1.1 christos %rip-relative addressing only uses a 32-bit displacement.
1203 1.1 christos 32 bits is not enough to be guaranteed to cover the distance between where
1204 1.1 christos the real instruction is and where its copy is.
1205 1.1 christos Convert the insn to use base+disp addressing.
1206 1.1 christos We set base = pc + insn_length so we can leave disp unchanged. */
1207 1.1 christos
1208 1.1 christos static void
1209 1.1 christos fixup_riprel (struct gdbarch *gdbarch, struct displaced_step_closure *dsc,
1210 1.1 christos CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1211 1.1 christos {
1212 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1213 1.1 christos const struct amd64_insn *insn_details = &dsc->insn_details;
1214 1.1 christos int modrm_offset = insn_details->modrm_offset;
1215 1.1 christos gdb_byte *insn = insn_details->raw_insn + modrm_offset;
1216 1.1 christos CORE_ADDR rip_base;
1217 1.1 christos int32_t disp;
1218 1.1 christos int insn_length;
1219 1.1 christos int arch_tmp_regno, tmp_regno;
1220 1.1 christos ULONGEST orig_value;
1221 1.1 christos
1222 1.1 christos /* %rip+disp32 addressing mode, displacement follows ModRM byte. */
1223 1.1 christos ++insn;
1224 1.1 christos
1225 1.1 christos /* Compute the rip-relative address. */
1226 1.1 christos disp = extract_signed_integer (insn, sizeof (int32_t), byte_order);
1227 1.1 christos insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf,
1228 1.1 christos dsc->max_len, from);
1229 1.1 christos rip_base = from + insn_length;
1230 1.1 christos
1231 1.1 christos /* We need a register to hold the address.
1232 1.1 christos Pick one not used in the insn.
1233 1.1 christos NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1234 1.1 christos arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1235 1.1 christos tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1236 1.1 christos
1237 1.1 christos /* REX.B should be unset as we were using rip-relative addressing,
1238 1.1 christos but ensure it's unset anyway, tmp_regno is not r8-r15. */
1239 1.1 christos if (insn_details->rex_offset != -1)
1240 1.1 christos dsc->insn_buf[insn_details->rex_offset] &= ~REX_B;
1241 1.1 christos
1242 1.1 christos regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1243 1.1 christos dsc->tmp_regno = tmp_regno;
1244 1.1 christos dsc->tmp_save = orig_value;
1245 1.1 christos dsc->tmp_used = 1;
1246 1.1 christos
1247 1.1 christos /* Convert the ModRM field to be base+disp. */
1248 1.1 christos dsc->insn_buf[modrm_offset] &= ~0xc7;
1249 1.1 christos dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1250 1.1 christos
1251 1.1 christos regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1252 1.1 christos
1253 1.1 christos if (debug_displaced)
1254 1.1 christos fprintf_unfiltered (gdb_stdlog, "displaced: %%rip-relative addressing used.\n"
1255 1.1 christos "displaced: using temp reg %d, old value %s, new value %s\n",
1256 1.1 christos dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1257 1.1 christos paddress (gdbarch, rip_base));
1258 1.1 christos }
1259 1.1 christos
1260 1.1 christos static void
1261 1.1 christos fixup_displaced_copy (struct gdbarch *gdbarch,
1262 1.1 christos struct displaced_step_closure *dsc,
1263 1.1 christos CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1264 1.1 christos {
1265 1.1 christos const struct amd64_insn *details = &dsc->insn_details;
1266 1.1 christos
1267 1.1 christos if (details->modrm_offset != -1)
1268 1.1 christos {
1269 1.1 christos gdb_byte modrm = details->raw_insn[details->modrm_offset];
1270 1.1 christos
1271 1.1 christos if ((modrm & 0xc7) == 0x05)
1272 1.1 christos {
1273 1.1 christos /* The insn uses rip-relative addressing.
1274 1.1 christos Deal with it. */
1275 1.1 christos fixup_riprel (gdbarch, dsc, from, to, regs);
1276 1.1 christos }
1277 1.1 christos }
1278 1.1 christos }
1279 1.1 christos
1280 1.1 christos struct displaced_step_closure *
1281 1.1 christos amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1282 1.1 christos CORE_ADDR from, CORE_ADDR to,
1283 1.1 christos struct regcache *regs)
1284 1.1 christos {
1285 1.1 christos int len = gdbarch_max_insn_length (gdbarch);
1286 1.1 christos /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
1287 1.1 christos continually watch for running off the end of the buffer. */
1288 1.1 christos int fixup_sentinel_space = len;
1289 1.1 christos struct displaced_step_closure *dsc =
1290 1.1 christos xmalloc (sizeof (*dsc) + len + fixup_sentinel_space);
1291 1.1 christos gdb_byte *buf = &dsc->insn_buf[0];
1292 1.1 christos struct amd64_insn *details = &dsc->insn_details;
1293 1.1 christos
1294 1.1 christos dsc->tmp_used = 0;
1295 1.1 christos dsc->max_len = len + fixup_sentinel_space;
1296 1.1 christos
1297 1.1 christos read_memory (from, buf, len);
1298 1.1 christos
1299 1.1 christos /* Set up the sentinel space so we don't have to worry about running
1300 1.1 christos off the end of the buffer. An excessive number of leading prefixes
1301 1.1 christos could otherwise cause this. */
1302 1.1 christos memset (buf + len, 0, fixup_sentinel_space);
1303 1.1 christos
1304 1.1 christos amd64_get_insn_details (buf, details);
1305 1.1 christos
1306 1.1 christos /* GDB may get control back after the insn after the syscall.
1307 1.1 christos Presumably this is a kernel bug.
1308 1.1 christos If this is a syscall, make sure there's a nop afterwards. */
1309 1.1 christos {
1310 1.1 christos int syscall_length;
1311 1.1 christos
1312 1.1 christos if (amd64_syscall_p (details, &syscall_length))
1313 1.1 christos buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1314 1.1 christos }
1315 1.1 christos
1316 1.1 christos /* Modify the insn to cope with the address where it will be executed from.
1317 1.1 christos In particular, handle any rip-relative addressing. */
1318 1.1 christos fixup_displaced_copy (gdbarch, dsc, from, to, regs);
1319 1.1 christos
1320 1.1 christos write_memory (to, buf, len);
1321 1.1 christos
1322 1.1 christos if (debug_displaced)
1323 1.1 christos {
1324 1.1 christos fprintf_unfiltered (gdb_stdlog, "displaced: copy %s->%s: ",
1325 1.1 christos paddress (gdbarch, from), paddress (gdbarch, to));
1326 1.1 christos displaced_step_dump_bytes (gdb_stdlog, buf, len);
1327 1.1 christos }
1328 1.1 christos
1329 1.1 christos return dsc;
1330 1.1 christos }
1331 1.1 christos
1332 1.1 christos static int
1333 1.1 christos amd64_absolute_jmp_p (const struct amd64_insn *details)
1334 1.1 christos {
1335 1.1 christos const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1336 1.1 christos
1337 1.1 christos if (insn[0] == 0xff)
1338 1.1 christos {
1339 1.1 christos /* jump near, absolute indirect (/4) */
1340 1.1 christos if ((insn[1] & 0x38) == 0x20)
1341 1.1 christos return 1;
1342 1.1 christos
1343 1.1 christos /* jump far, absolute indirect (/5) */
1344 1.1 christos if ((insn[1] & 0x38) == 0x28)
1345 1.1 christos return 1;
1346 1.1 christos }
1347 1.1 christos
1348 1.1 christos return 0;
1349 1.1 christos }
1350 1.1 christos
1351 1.1 christos static int
1352 1.1 christos amd64_absolute_call_p (const struct amd64_insn *details)
1353 1.1 christos {
1354 1.1 christos const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1355 1.1 christos
1356 1.1 christos if (insn[0] == 0xff)
1357 1.1 christos {
1358 1.1 christos /* Call near, absolute indirect (/2) */
1359 1.1 christos if ((insn[1] & 0x38) == 0x10)
1360 1.1 christos return 1;
1361 1.1 christos
1362 1.1 christos /* Call far, absolute indirect (/3) */
1363 1.1 christos if ((insn[1] & 0x38) == 0x18)
1364 1.1 christos return 1;
1365 1.1 christos }
1366 1.1 christos
1367 1.1 christos return 0;
1368 1.1 christos }
1369 1.1 christos
1370 1.1 christos static int
1371 1.1 christos amd64_ret_p (const struct amd64_insn *details)
1372 1.1 christos {
1373 1.1 christos /* NOTE: gcc can emit "repz ; ret". */
1374 1.1 christos const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1375 1.1 christos
1376 1.1 christos switch (insn[0])
1377 1.1 christos {
1378 1.1 christos case 0xc2: /* ret near, pop N bytes */
1379 1.1 christos case 0xc3: /* ret near */
1380 1.1 christos case 0xca: /* ret far, pop N bytes */
1381 1.1 christos case 0xcb: /* ret far */
1382 1.1 christos case 0xcf: /* iret */
1383 1.1 christos return 1;
1384 1.1 christos
1385 1.1 christos default:
1386 1.1 christos return 0;
1387 1.1 christos }
1388 1.1 christos }
1389 1.1 christos
1390 1.1 christos static int
1391 1.1 christos amd64_call_p (const struct amd64_insn *details)
1392 1.1 christos {
1393 1.1 christos const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1394 1.1 christos
1395 1.1 christos if (amd64_absolute_call_p (details))
1396 1.1 christos return 1;
1397 1.1 christos
1398 1.1 christos /* call near, relative */
1399 1.1 christos if (insn[0] == 0xe8)
1400 1.1 christos return 1;
1401 1.1 christos
1402 1.1 christos return 0;
1403 1.1 christos }
1404 1.1 christos
1405 1.1 christos /* Return non-zero if INSN is a system call, and set *LENGTHP to its
1406 1.1 christos length in bytes. Otherwise, return zero. */
1407 1.1 christos
1408 1.1 christos static int
1409 1.1 christos amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1410 1.1 christos {
1411 1.1 christos const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1412 1.1 christos
1413 1.1 christos if (insn[0] == 0x0f && insn[1] == 0x05)
1414 1.1 christos {
1415 1.1 christos *lengthp = 2;
1416 1.1 christos return 1;
1417 1.1 christos }
1418 1.1 christos
1419 1.1 christos return 0;
1420 1.1 christos }
1421 1.1 christos
1422 1.1 christos /* Fix up the state of registers and memory after having single-stepped
1423 1.1 christos a displaced instruction. */
1424 1.1 christos
1425 1.1 christos void
1426 1.1 christos amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1427 1.1 christos struct displaced_step_closure *dsc,
1428 1.1 christos CORE_ADDR from, CORE_ADDR to,
1429 1.1 christos struct regcache *regs)
1430 1.1 christos {
1431 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1432 1.1 christos /* The offset we applied to the instruction's address. */
1433 1.1 christos ULONGEST insn_offset = to - from;
1434 1.1 christos gdb_byte *insn = dsc->insn_buf;
1435 1.1 christos const struct amd64_insn *insn_details = &dsc->insn_details;
1436 1.1 christos
1437 1.1 christos if (debug_displaced)
1438 1.1 christos fprintf_unfiltered (gdb_stdlog,
1439 1.1 christos "displaced: fixup (%s, %s), "
1440 1.1 christos "insn = 0x%02x 0x%02x ...\n",
1441 1.1 christos paddress (gdbarch, from), paddress (gdbarch, to),
1442 1.1 christos insn[0], insn[1]);
1443 1.1 christos
1444 1.1 christos /* If we used a tmp reg, restore it. */
1445 1.1 christos
1446 1.1 christos if (dsc->tmp_used)
1447 1.1 christos {
1448 1.1 christos if (debug_displaced)
1449 1.1 christos fprintf_unfiltered (gdb_stdlog, "displaced: restoring reg %d to %s\n",
1450 1.1 christos dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
1451 1.1 christos regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1452 1.1 christos }
1453 1.1 christos
1454 1.1 christos /* The list of issues to contend with here is taken from
1455 1.1 christos resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1456 1.1 christos Yay for Free Software! */
1457 1.1 christos
1458 1.1 christos /* Relocate the %rip back to the program's instruction stream,
1459 1.1 christos if necessary. */
1460 1.1 christos
1461 1.1 christos /* Except in the case of absolute or indirect jump or call
1462 1.1 christos instructions, or a return instruction, the new rip is relative to
1463 1.1 christos the displaced instruction; make it relative to the original insn.
1464 1.1 christos Well, signal handler returns don't need relocation either, but we use the
1465 1.1 christos value of %rip to recognize those; see below. */
1466 1.1 christos if (! amd64_absolute_jmp_p (insn_details)
1467 1.1 christos && ! amd64_absolute_call_p (insn_details)
1468 1.1 christos && ! amd64_ret_p (insn_details))
1469 1.1 christos {
1470 1.1 christos ULONGEST orig_rip;
1471 1.1 christos int insn_len;
1472 1.1 christos
1473 1.1 christos regcache_cooked_read_unsigned (regs, AMD64_RIP_REGNUM, &orig_rip);
1474 1.1 christos
1475 1.1 christos /* A signal trampoline system call changes the %rip, resuming
1476 1.1 christos execution of the main program after the signal handler has
1477 1.1 christos returned. That makes them like 'return' instructions; we
1478 1.1 christos shouldn't relocate %rip.
1479 1.1 christos
1480 1.1 christos But most system calls don't, and we do need to relocate %rip.
1481 1.1 christos
1482 1.1 christos Our heuristic for distinguishing these cases: if stepping
1483 1.1 christos over the system call instruction left control directly after
1484 1.1 christos the instruction, the we relocate --- control almost certainly
1485 1.1 christos doesn't belong in the displaced copy. Otherwise, we assume
1486 1.1 christos the instruction has put control where it belongs, and leave
1487 1.1 christos it unrelocated. Goodness help us if there are PC-relative
1488 1.1 christos system calls. */
1489 1.1 christos if (amd64_syscall_p (insn_details, &insn_len)
1490 1.1 christos && orig_rip != to + insn_len
1491 1.1 christos /* GDB can get control back after the insn after the syscall.
1492 1.1 christos Presumably this is a kernel bug.
1493 1.1 christos Fixup ensures its a nop, we add one to the length for it. */
1494 1.1 christos && orig_rip != to + insn_len + 1)
1495 1.1 christos {
1496 1.1 christos if (debug_displaced)
1497 1.1 christos fprintf_unfiltered (gdb_stdlog,
1498 1.1 christos "displaced: syscall changed %%rip; "
1499 1.1 christos "not relocating\n");
1500 1.1 christos }
1501 1.1 christos else
1502 1.1 christos {
1503 1.1 christos ULONGEST rip = orig_rip - insn_offset;
1504 1.1 christos
1505 1.1 christos /* If we just stepped over a breakpoint insn, we don't backup
1506 1.1 christos the pc on purpose; this is to match behaviour without
1507 1.1 christos stepping. */
1508 1.1 christos
1509 1.1 christos regcache_cooked_write_unsigned (regs, AMD64_RIP_REGNUM, rip);
1510 1.1 christos
1511 1.1 christos if (debug_displaced)
1512 1.1 christos fprintf_unfiltered (gdb_stdlog,
1513 1.1 christos "displaced: "
1514 1.1 christos "relocated %%rip from %s to %s\n",
1515 1.1 christos paddress (gdbarch, orig_rip),
1516 1.1 christos paddress (gdbarch, rip));
1517 1.1 christos }
1518 1.1 christos }
1519 1.1 christos
1520 1.1 christos /* If the instruction was PUSHFL, then the TF bit will be set in the
1521 1.1 christos pushed value, and should be cleared. We'll leave this for later,
1522 1.1 christos since GDB already messes up the TF flag when stepping over a
1523 1.1 christos pushfl. */
1524 1.1 christos
1525 1.1 christos /* If the instruction was a call, the return address now atop the
1526 1.1 christos stack is the address following the copied instruction. We need
1527 1.1 christos to make it the address following the original instruction. */
1528 1.1 christos if (amd64_call_p (insn_details))
1529 1.1 christos {
1530 1.1 christos ULONGEST rsp;
1531 1.1 christos ULONGEST retaddr;
1532 1.1 christos const ULONGEST retaddr_len = 8;
1533 1.1 christos
1534 1.1 christos regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
1535 1.1 christos retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
1536 1.1 christos retaddr = (retaddr - insn_offset) & 0xffffffffUL;
1537 1.1 christos write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
1538 1.1 christos
1539 1.1 christos if (debug_displaced)
1540 1.1 christos fprintf_unfiltered (gdb_stdlog,
1541 1.1 christos "displaced: relocated return addr at %s "
1542 1.1 christos "to %s\n",
1543 1.1 christos paddress (gdbarch, rsp),
1544 1.1 christos paddress (gdbarch, retaddr));
1545 1.1 christos }
1546 1.1 christos }
1547 1.1 christos
1548 1.1 christos /* If the instruction INSN uses RIP-relative addressing, return the
1549 1.1 christos offset into the raw INSN where the displacement to be adjusted is
1550 1.1 christos found. Returns 0 if the instruction doesn't use RIP-relative
1551 1.1 christos addressing. */
1552 1.1 christos
1553 1.1 christos static int
1554 1.1 christos rip_relative_offset (struct amd64_insn *insn)
1555 1.1 christos {
1556 1.1 christos if (insn->modrm_offset != -1)
1557 1.1 christos {
1558 1.1 christos gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
1559 1.1 christos
1560 1.1 christos if ((modrm & 0xc7) == 0x05)
1561 1.1 christos {
1562 1.1 christos /* The displacement is found right after the ModRM byte. */
1563 1.1 christos return insn->modrm_offset + 1;
1564 1.1 christos }
1565 1.1 christos }
1566 1.1 christos
1567 1.1 christos return 0;
1568 1.1 christos }
1569 1.1 christos
1570 1.1 christos static void
1571 1.1 christos append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
1572 1.1 christos {
1573 1.1 christos target_write_memory (*to, buf, len);
1574 1.1 christos *to += len;
1575 1.1 christos }
1576 1.1 christos
1577 1.1 christos static void
1578 1.1 christos amd64_relocate_instruction (struct gdbarch *gdbarch,
1579 1.1 christos CORE_ADDR *to, CORE_ADDR oldloc)
1580 1.1 christos {
1581 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1582 1.1 christos int len = gdbarch_max_insn_length (gdbarch);
1583 1.1 christos /* Extra space for sentinels. */
1584 1.1 christos int fixup_sentinel_space = len;
1585 1.1 christos gdb_byte *buf = xmalloc (len + fixup_sentinel_space);
1586 1.1 christos struct amd64_insn insn_details;
1587 1.1 christos int offset = 0;
1588 1.1 christos LONGEST rel32, newrel;
1589 1.1 christos gdb_byte *insn;
1590 1.1 christos int insn_length;
1591 1.1 christos
1592 1.1 christos read_memory (oldloc, buf, len);
1593 1.1 christos
1594 1.1 christos /* Set up the sentinel space so we don't have to worry about running
1595 1.1 christos off the end of the buffer. An excessive number of leading prefixes
1596 1.1 christos could otherwise cause this. */
1597 1.1 christos memset (buf + len, 0, fixup_sentinel_space);
1598 1.1 christos
1599 1.1 christos insn = buf;
1600 1.1 christos amd64_get_insn_details (insn, &insn_details);
1601 1.1 christos
1602 1.1 christos insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
1603 1.1 christos
1604 1.1 christos /* Skip legacy instruction prefixes. */
1605 1.1 christos insn = amd64_skip_prefixes (insn);
1606 1.1 christos
1607 1.1 christos /* Adjust calls with 32-bit relative addresses as push/jump, with
1608 1.1 christos the address pushed being the location where the original call in
1609 1.1 christos the user program would return to. */
1610 1.1 christos if (insn[0] == 0xe8)
1611 1.1 christos {
1612 1.1 christos gdb_byte push_buf[16];
1613 1.1 christos unsigned int ret_addr;
1614 1.1 christos
1615 1.1 christos /* Where "ret" in the original code will return to. */
1616 1.1 christos ret_addr = oldloc + insn_length;
1617 1.1 christos push_buf[0] = 0x68; /* pushq $... */
1618 1.1 christos store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
1619 1.1 christos /* Push the push. */
1620 1.1 christos append_insns (to, 5, push_buf);
1621 1.1 christos
1622 1.1 christos /* Convert the relative call to a relative jump. */
1623 1.1 christos insn[0] = 0xe9;
1624 1.1 christos
1625 1.1 christos /* Adjust the destination offset. */
1626 1.1 christos rel32 = extract_signed_integer (insn + 1, 4, byte_order);
1627 1.1 christos newrel = (oldloc - *to) + rel32;
1628 1.1 christos store_signed_integer (insn + 1, 4, byte_order, newrel);
1629 1.1 christos
1630 1.1 christos if (debug_displaced)
1631 1.1 christos fprintf_unfiltered (gdb_stdlog,
1632 1.1 christos "Adjusted insn rel32=%s at %s to"
1633 1.1 christos " rel32=%s at %s\n",
1634 1.1 christos hex_string (rel32), paddress (gdbarch, oldloc),
1635 1.1 christos hex_string (newrel), paddress (gdbarch, *to));
1636 1.1 christos
1637 1.1 christos /* Write the adjusted jump into its displaced location. */
1638 1.1 christos append_insns (to, 5, insn);
1639 1.1 christos return;
1640 1.1 christos }
1641 1.1 christos
1642 1.1 christos offset = rip_relative_offset (&insn_details);
1643 1.1 christos if (!offset)
1644 1.1 christos {
1645 1.1 christos /* Adjust jumps with 32-bit relative addresses. Calls are
1646 1.1 christos already handled above. */
1647 1.1 christos if (insn[0] == 0xe9)
1648 1.1 christos offset = 1;
1649 1.1 christos /* Adjust conditional jumps. */
1650 1.1 christos else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1651 1.1 christos offset = 2;
1652 1.1 christos }
1653 1.1 christos
1654 1.1 christos if (offset)
1655 1.1 christos {
1656 1.1 christos rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1657 1.1 christos newrel = (oldloc - *to) + rel32;
1658 1.1 christos store_signed_integer (insn + offset, 4, byte_order, newrel);
1659 1.1 christos if (debug_displaced)
1660 1.1 christos fprintf_unfiltered (gdb_stdlog,
1661 1.1 christos "Adjusted insn rel32=%s at %s to"
1662 1.1 christos " rel32=%s at %s\n",
1663 1.1 christos hex_string (rel32), paddress (gdbarch, oldloc),
1664 1.1 christos hex_string (newrel), paddress (gdbarch, *to));
1665 1.1 christos }
1666 1.1 christos
1667 1.1 christos /* Write the adjusted instruction into its displaced location. */
1668 1.1 christos append_insns (to, insn_length, buf);
1669 1.1 christos }
1670 1.1 christos
1671 1.1 christos
1672 1.1 christos /* The maximum number of saved registers. This should include %rip. */
1674 1.1 christos #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
1675 1.1 christos
1676 1.1 christos struct amd64_frame_cache
1677 1.1 christos {
1678 1.1 christos /* Base address. */
1679 1.1 christos CORE_ADDR base;
1680 1.1 christos int base_p;
1681 1.1 christos CORE_ADDR sp_offset;
1682 1.1 christos CORE_ADDR pc;
1683 1.1 christos
1684 1.1 christos /* Saved registers. */
1685 1.1 christos CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
1686 1.1 christos CORE_ADDR saved_sp;
1687 1.1 christos int saved_sp_reg;
1688 1.1 christos
1689 1.1 christos /* Do we have a frame? */
1690 1.1 christos int frameless_p;
1691 1.1 christos };
1692 1.1 christos
1693 1.1 christos /* Initialize a frame cache. */
1694 1.1 christos
1695 1.1 christos static void
1696 1.1 christos amd64_init_frame_cache (struct amd64_frame_cache *cache)
1697 1.1 christos {
1698 1.1 christos int i;
1699 1.1 christos
1700 1.1 christos /* Base address. */
1701 1.1 christos cache->base = 0;
1702 1.1 christos cache->base_p = 0;
1703 1.1 christos cache->sp_offset = -8;
1704 1.1 christos cache->pc = 0;
1705 1.1 christos
1706 1.1 christos /* Saved registers. We initialize these to -1 since zero is a valid
1707 1.1 christos offset (that's where %rbp is supposed to be stored).
1708 1.1 christos The values start out as being offsets, and are later converted to
1709 1.1 christos addresses (at which point -1 is interpreted as an address, still meaning
1710 1.1 christos "invalid"). */
1711 1.1 christos for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
1712 1.1 christos cache->saved_regs[i] = -1;
1713 1.1 christos cache->saved_sp = 0;
1714 1.1 christos cache->saved_sp_reg = -1;
1715 1.1 christos
1716 1.1 christos /* Frameless until proven otherwise. */
1717 1.1 christos cache->frameless_p = 1;
1718 1.1 christos }
1719 1.1 christos
1720 1.1 christos /* Allocate and initialize a frame cache. */
1721 1.1 christos
1722 1.1 christos static struct amd64_frame_cache *
1723 1.1 christos amd64_alloc_frame_cache (void)
1724 1.1 christos {
1725 1.1 christos struct amd64_frame_cache *cache;
1726 1.1 christos
1727 1.1 christos cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1728 1.1 christos amd64_init_frame_cache (cache);
1729 1.1 christos return cache;
1730 1.1 christos }
1731 1.1 christos
1732 1.1 christos /* GCC 4.4 and later, can put code in the prologue to realign the
1733 1.1 christos stack pointer. Check whether PC points to such code, and update
1734 1.1 christos CACHE accordingly. Return the first instruction after the code
1735 1.1 christos sequence or CURRENT_PC, whichever is smaller. If we don't
1736 1.1 christos recognize the code, return PC. */
1737 1.1 christos
1738 1.1 christos static CORE_ADDR
1739 1.1 christos amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1740 1.1 christos struct amd64_frame_cache *cache)
1741 1.1 christos {
1742 1.1 christos /* There are 2 code sequences to re-align stack before the frame
1743 1.1 christos gets set up:
1744 1.1 christos
1745 1.1 christos 1. Use a caller-saved saved register:
1746 1.1 christos
1747 1.1 christos leaq 8(%rsp), %reg
1748 1.1 christos andq $-XXX, %rsp
1749 1.1 christos pushq -8(%reg)
1750 1.1 christos
1751 1.1 christos 2. Use a callee-saved saved register:
1752 1.1 christos
1753 1.1 christos pushq %reg
1754 1.1 christos leaq 16(%rsp), %reg
1755 1.1 christos andq $-XXX, %rsp
1756 1.1 christos pushq -8(%reg)
1757 1.1 christos
1758 1.1 christos "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1759 1.1 christos
1760 1.1 christos 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1761 1.1 christos 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1762 1.1 christos */
1763 1.1 christos
1764 1.1 christos gdb_byte buf[18];
1765 1.1 christos int reg, r;
1766 1.1 christos int offset, offset_and;
1767 1.1 christos
1768 1.1 christos if (target_read_code (pc, buf, sizeof buf))
1769 1.1 christos return pc;
1770 1.1 christos
1771 1.1 christos /* Check caller-saved saved register. The first instruction has
1772 1.1 christos to be "leaq 8(%rsp), %reg". */
1773 1.1 christos if ((buf[0] & 0xfb) == 0x48
1774 1.1 christos && buf[1] == 0x8d
1775 1.1 christos && buf[3] == 0x24
1776 1.1 christos && buf[4] == 0x8)
1777 1.1 christos {
1778 1.1 christos /* MOD must be binary 10 and R/M must be binary 100. */
1779 1.1 christos if ((buf[2] & 0xc7) != 0x44)
1780 1.1 christos return pc;
1781 1.1 christos
1782 1.1 christos /* REG has register number. */
1783 1.1 christos reg = (buf[2] >> 3) & 7;
1784 1.1 christos
1785 1.1 christos /* Check the REX.R bit. */
1786 1.1 christos if (buf[0] == 0x4c)
1787 1.1 christos reg += 8;
1788 1.1 christos
1789 1.1 christos offset = 5;
1790 1.1 christos }
1791 1.1 christos else
1792 1.1 christos {
1793 1.1 christos /* Check callee-saved saved register. The first instruction
1794 1.1 christos has to be "pushq %reg". */
1795 1.1 christos reg = 0;
1796 1.1 christos if ((buf[0] & 0xf8) == 0x50)
1797 1.1 christos offset = 0;
1798 1.1 christos else if ((buf[0] & 0xf6) == 0x40
1799 1.1 christos && (buf[1] & 0xf8) == 0x50)
1800 1.1 christos {
1801 1.1 christos /* Check the REX.B bit. */
1802 1.1 christos if ((buf[0] & 1) != 0)
1803 1.1 christos reg = 8;
1804 1.1 christos
1805 1.1 christos offset = 1;
1806 1.1 christos }
1807 1.1 christos else
1808 1.1 christos return pc;
1809 1.1 christos
1810 1.1 christos /* Get register. */
1811 1.1 christos reg += buf[offset] & 0x7;
1812 1.1 christos
1813 1.1 christos offset++;
1814 1.1 christos
1815 1.1 christos /* The next instruction has to be "leaq 16(%rsp), %reg". */
1816 1.1 christos if ((buf[offset] & 0xfb) != 0x48
1817 1.1 christos || buf[offset + 1] != 0x8d
1818 1.1 christos || buf[offset + 3] != 0x24
1819 1.1 christos || buf[offset + 4] != 0x10)
1820 1.1 christos return pc;
1821 1.1 christos
1822 1.1 christos /* MOD must be binary 10 and R/M must be binary 100. */
1823 1.1 christos if ((buf[offset + 2] & 0xc7) != 0x44)
1824 1.1 christos return pc;
1825 1.1 christos
1826 1.1 christos /* REG has register number. */
1827 1.1 christos r = (buf[offset + 2] >> 3) & 7;
1828 1.1 christos
1829 1.1 christos /* Check the REX.R bit. */
1830 1.1 christos if (buf[offset] == 0x4c)
1831 1.1 christos r += 8;
1832 1.1 christos
1833 1.1 christos /* Registers in pushq and leaq have to be the same. */
1834 1.1 christos if (reg != r)
1835 1.1 christos return pc;
1836 1.1 christos
1837 1.1 christos offset += 5;
1838 1.1 christos }
1839 1.1 christos
1840 1.1 christos /* Rigister can't be %rsp nor %rbp. */
1841 1.1 christos if (reg == 4 || reg == 5)
1842 1.1 christos return pc;
1843 1.1 christos
1844 1.1 christos /* The next instruction has to be "andq $-XXX, %rsp". */
1845 1.1 christos if (buf[offset] != 0x48
1846 1.1 christos || buf[offset + 2] != 0xe4
1847 1.1 christos || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
1848 1.1 christos return pc;
1849 1.1 christos
1850 1.1 christos offset_and = offset;
1851 1.1 christos offset += buf[offset + 1] == 0x81 ? 7 : 4;
1852 1.1 christos
1853 1.1 christos /* The next instruction has to be "pushq -8(%reg)". */
1854 1.1 christos r = 0;
1855 1.1 christos if (buf[offset] == 0xff)
1856 1.1 christos offset++;
1857 1.1 christos else if ((buf[offset] & 0xf6) == 0x40
1858 1.1 christos && buf[offset + 1] == 0xff)
1859 1.1 christos {
1860 1.1 christos /* Check the REX.B bit. */
1861 1.1 christos if ((buf[offset] & 0x1) != 0)
1862 1.1 christos r = 8;
1863 1.1 christos offset += 2;
1864 1.1 christos }
1865 1.1 christos else
1866 1.1 christos return pc;
1867 1.1 christos
1868 1.1 christos /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
1869 1.1 christos 01. */
1870 1.1 christos if (buf[offset + 1] != 0xf8
1871 1.1 christos || (buf[offset] & 0xf8) != 0x70)
1872 1.1 christos return pc;
1873 1.1 christos
1874 1.1 christos /* R/M has register. */
1875 1.1 christos r += buf[offset] & 7;
1876 1.1 christos
1877 1.1 christos /* Registers in leaq and pushq have to be the same. */
1878 1.1 christos if (reg != r)
1879 1.1 christos return pc;
1880 1.1 christos
1881 1.1 christos if (current_pc > pc + offset_and)
1882 1.1 christos cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
1883 1.1 christos
1884 1.1 christos return min (pc + offset + 2, current_pc);
1885 1.1 christos }
1886 1.1 christos
1887 1.1 christos /* Similar to amd64_analyze_stack_align for x32. */
1888 1.1 christos
1889 1.1 christos static CORE_ADDR
1890 1.1 christos amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1891 1.1 christos struct amd64_frame_cache *cache)
1892 1.1 christos {
1893 1.1 christos /* There are 2 code sequences to re-align stack before the frame
1894 1.1 christos gets set up:
1895 1.1 christos
1896 1.1 christos 1. Use a caller-saved saved register:
1897 1.1 christos
1898 1.1 christos leaq 8(%rsp), %reg
1899 1.1 christos andq $-XXX, %rsp
1900 1.1 christos pushq -8(%reg)
1901 1.1 christos
1902 1.1 christos or
1903 1.1 christos
1904 1.1 christos [addr32] leal 8(%rsp), %reg
1905 1.1 christos andl $-XXX, %esp
1906 1.1 christos [addr32] pushq -8(%reg)
1907 1.1 christos
1908 1.1 christos 2. Use a callee-saved saved register:
1909 1.1 christos
1910 1.1 christos pushq %reg
1911 1.1 christos leaq 16(%rsp), %reg
1912 1.1 christos andq $-XXX, %rsp
1913 1.1 christos pushq -8(%reg)
1914 1.1 christos
1915 1.1 christos or
1916 1.1 christos
1917 1.1 christos pushq %reg
1918 1.1 christos [addr32] leal 16(%rsp), %reg
1919 1.1 christos andl $-XXX, %esp
1920 1.1 christos [addr32] pushq -8(%reg)
1921 1.1 christos
1922 1.1 christos "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
1923 1.1 christos
1924 1.1 christos 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
1925 1.1 christos 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
1926 1.1 christos
1927 1.1 christos "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
1928 1.1 christos
1929 1.1 christos 0x83 0xe4 0xf0 andl $-16, %esp
1930 1.1 christos 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
1931 1.1 christos */
1932 1.1 christos
1933 1.1 christos gdb_byte buf[19];
1934 1.1 christos int reg, r;
1935 1.1 christos int offset, offset_and;
1936 1.1 christos
1937 1.1 christos if (target_read_memory (pc, buf, sizeof buf))
1938 1.1 christos return pc;
1939 1.1 christos
1940 1.1 christos /* Skip optional addr32 prefix. */
1941 1.1 christos offset = buf[0] == 0x67 ? 1 : 0;
1942 1.1 christos
1943 1.1 christos /* Check caller-saved saved register. The first instruction has
1944 1.1 christos to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg". */
1945 1.1 christos if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
1946 1.1 christos && buf[offset + 1] == 0x8d
1947 1.1 christos && buf[offset + 3] == 0x24
1948 1.1 christos && buf[offset + 4] == 0x8)
1949 1.1 christos {
1950 1.1 christos /* MOD must be binary 10 and R/M must be binary 100. */
1951 1.1 christos if ((buf[offset + 2] & 0xc7) != 0x44)
1952 1.1 christos return pc;
1953 1.1 christos
1954 1.1 christos /* REG has register number. */
1955 1.1 christos reg = (buf[offset + 2] >> 3) & 7;
1956 1.1 christos
1957 1.1 christos /* Check the REX.R bit. */
1958 1.1 christos if ((buf[offset] & 0x4) != 0)
1959 1.1 christos reg += 8;
1960 1.1 christos
1961 1.1 christos offset += 5;
1962 1.1 christos }
1963 1.1 christos else
1964 1.1 christos {
1965 1.1 christos /* Check callee-saved saved register. The first instruction
1966 1.1 christos has to be "pushq %reg". */
1967 1.1 christos reg = 0;
1968 1.1 christos if ((buf[offset] & 0xf6) == 0x40
1969 1.1 christos && (buf[offset + 1] & 0xf8) == 0x50)
1970 1.1 christos {
1971 1.1 christos /* Check the REX.B bit. */
1972 1.1 christos if ((buf[offset] & 1) != 0)
1973 1.1 christos reg = 8;
1974 1.1 christos
1975 1.1 christos offset += 1;
1976 1.1 christos }
1977 1.1 christos else if ((buf[offset] & 0xf8) != 0x50)
1978 1.1 christos return pc;
1979 1.1 christos
1980 1.1 christos /* Get register. */
1981 1.1 christos reg += buf[offset] & 0x7;
1982 1.1 christos
1983 1.1 christos offset++;
1984 1.1 christos
1985 1.1 christos /* Skip optional addr32 prefix. */
1986 1.1 christos if (buf[offset] == 0x67)
1987 1.1 christos offset++;
1988 1.1 christos
1989 1.1 christos /* The next instruction has to be "leaq 16(%rsp), %reg" or
1990 1.1 christos "leal 16(%rsp), %reg". */
1991 1.1 christos if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
1992 1.1 christos || buf[offset + 1] != 0x8d
1993 1.1 christos || buf[offset + 3] != 0x24
1994 1.1 christos || buf[offset + 4] != 0x10)
1995 1.1 christos return pc;
1996 1.1 christos
1997 1.1 christos /* MOD must be binary 10 and R/M must be binary 100. */
1998 1.1 christos if ((buf[offset + 2] & 0xc7) != 0x44)
1999 1.1 christos return pc;
2000 1.1 christos
2001 1.1 christos /* REG has register number. */
2002 1.1 christos r = (buf[offset + 2] >> 3) & 7;
2003 1.1 christos
2004 1.1 christos /* Check the REX.R bit. */
2005 1.1 christos if ((buf[offset] & 0x4) != 0)
2006 1.1 christos r += 8;
2007 1.1 christos
2008 1.1 christos /* Registers in pushq and leaq have to be the same. */
2009 1.1 christos if (reg != r)
2010 1.1 christos return pc;
2011 1.1 christos
2012 1.1 christos offset += 5;
2013 1.1 christos }
2014 1.1 christos
2015 1.1 christos /* Rigister can't be %rsp nor %rbp. */
2016 1.1 christos if (reg == 4 || reg == 5)
2017 1.1 christos return pc;
2018 1.1 christos
2019 1.1 christos /* The next instruction may be "andq $-XXX, %rsp" or
2020 1.1 christos "andl $-XXX, %esp". */
2021 1.1 christos if (buf[offset] != 0x48)
2022 1.1 christos offset--;
2023 1.1 christos
2024 1.1 christos if (buf[offset + 2] != 0xe4
2025 1.1 christos || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2026 1.1 christos return pc;
2027 1.1 christos
2028 1.1 christos offset_and = offset;
2029 1.1 christos offset += buf[offset + 1] == 0x81 ? 7 : 4;
2030 1.1 christos
2031 1.1 christos /* Skip optional addr32 prefix. */
2032 1.1 christos if (buf[offset] == 0x67)
2033 1.1 christos offset++;
2034 1.1 christos
2035 1.1 christos /* The next instruction has to be "pushq -8(%reg)". */
2036 1.1 christos r = 0;
2037 1.1 christos if (buf[offset] == 0xff)
2038 1.1 christos offset++;
2039 1.1 christos else if ((buf[offset] & 0xf6) == 0x40
2040 1.1 christos && buf[offset + 1] == 0xff)
2041 1.1 christos {
2042 1.1 christos /* Check the REX.B bit. */
2043 1.1 christos if ((buf[offset] & 0x1) != 0)
2044 1.1 christos r = 8;
2045 1.1 christos offset += 2;
2046 1.1 christos }
2047 1.1 christos else
2048 1.1 christos return pc;
2049 1.1 christos
2050 1.1 christos /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2051 1.1 christos 01. */
2052 1.1 christos if (buf[offset + 1] != 0xf8
2053 1.1 christos || (buf[offset] & 0xf8) != 0x70)
2054 1.1 christos return pc;
2055 1.1 christos
2056 1.1 christos /* R/M has register. */
2057 1.1 christos r += buf[offset] & 7;
2058 1.1 christos
2059 1.1 christos /* Registers in leaq and pushq have to be the same. */
2060 1.1 christos if (reg != r)
2061 1.1 christos return pc;
2062 1.1 christos
2063 1.1 christos if (current_pc > pc + offset_and)
2064 1.1 christos cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2065 1.1 christos
2066 1.1 christos return min (pc + offset + 2, current_pc);
2067 1.1 christos }
2068 1.1 christos
2069 1.1 christos /* Do a limited analysis of the prologue at PC and update CACHE
2070 1.1 christos accordingly. Bail out early if CURRENT_PC is reached. Return the
2071 1.1 christos address where the analysis stopped.
2072 1.1 christos
2073 1.1 christos We will handle only functions beginning with:
2074 1.1 christos
2075 1.1 christos pushq %rbp 0x55
2076 1.1 christos movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
2077 1.1 christos
2078 1.1 christos or (for the X32 ABI):
2079 1.1 christos
2080 1.1 christos pushq %rbp 0x55
2081 1.1 christos movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
2082 1.1 christos
2083 1.1 christos Any function that doesn't start with one of these sequences will be
2084 1.1 christos assumed to have no prologue and thus no valid frame pointer in
2085 1.1 christos %rbp. */
2086 1.1 christos
2087 1.1 christos static CORE_ADDR
2088 1.1 christos amd64_analyze_prologue (struct gdbarch *gdbarch,
2089 1.1 christos CORE_ADDR pc, CORE_ADDR current_pc,
2090 1.1 christos struct amd64_frame_cache *cache)
2091 1.1 christos {
2092 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2093 1.1 christos /* There are two variations of movq %rsp, %rbp. */
2094 1.1 christos static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
2095 1.1 christos static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
2096 1.1 christos /* Ditto for movl %esp, %ebp. */
2097 1.1 christos static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
2098 1.1 christos static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
2099 1.1 christos
2100 1.1 christos gdb_byte buf[3];
2101 1.1 christos gdb_byte op;
2102 1.1 christos
2103 1.1 christos if (current_pc <= pc)
2104 1.1 christos return current_pc;
2105 1.1 christos
2106 1.1 christos if (gdbarch_ptr_bit (gdbarch) == 32)
2107 1.1 christos pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
2108 1.1 christos else
2109 1.1 christos pc = amd64_analyze_stack_align (pc, current_pc, cache);
2110 1.1 christos
2111 1.1 christos op = read_code_unsigned_integer (pc, 1, byte_order);
2112 1.1 christos
2113 1.1 christos if (op == 0x55) /* pushq %rbp */
2114 1.1 christos {
2115 1.1 christos /* Take into account that we've executed the `pushq %rbp' that
2116 1.1 christos starts this instruction sequence. */
2117 1.1 christos cache->saved_regs[AMD64_RBP_REGNUM] = 0;
2118 1.1 christos cache->sp_offset += 8;
2119 1.1 christos
2120 1.1 christos /* If that's all, return now. */
2121 1.1 christos if (current_pc <= pc + 1)
2122 1.1 christos return current_pc;
2123 1.1 christos
2124 1.1 christos read_code (pc + 1, buf, 3);
2125 1.1 christos
2126 1.1 christos /* Check for `movq %rsp, %rbp'. */
2127 1.1 christos if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
2128 1.1 christos || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
2129 1.1 christos {
2130 1.1 christos /* OK, we actually have a frame. */
2131 1.1 christos cache->frameless_p = 0;
2132 1.1 christos return pc + 4;
2133 1.1 christos }
2134 1.1 christos
2135 1.1 christos /* For X32, also check for `movq %esp, %ebp'. */
2136 1.1 christos if (gdbarch_ptr_bit (gdbarch) == 32)
2137 1.1 christos {
2138 1.1 christos if (memcmp (buf, mov_esp_ebp_1, 2) == 0
2139 1.1 christos || memcmp (buf, mov_esp_ebp_2, 2) == 0)
2140 1.1 christos {
2141 1.1 christos /* OK, we actually have a frame. */
2142 1.1 christos cache->frameless_p = 0;
2143 1.1 christos return pc + 3;
2144 1.1 christos }
2145 1.1 christos }
2146 1.1 christos
2147 1.1 christos return pc + 1;
2148 1.1 christos }
2149 1.1 christos
2150 1.1 christos return pc;
2151 1.1 christos }
2152 1.1 christos
2153 1.1 christos /* Work around false termination of prologue - GCC PR debug/48827.
2154 1.1 christos
2155 1.1 christos START_PC is the first instruction of a function, PC is its minimal already
2156 1.1 christos determined advanced address. Function returns PC if it has nothing to do.
2157 1.1 christos
2158 1.1 christos 84 c0 test %al,%al
2159 1.1 christos 74 23 je after
2160 1.1 christos <-- here is 0 lines advance - the false prologue end marker.
2161 1.1 christos 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
2162 1.1 christos 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
2163 1.1 christos 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
2164 1.1 christos 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
2165 1.1 christos 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
2166 1.1 christos 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
2167 1.1 christos 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
2168 1.1 christos 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
2169 1.1 christos after: */
2170 1.1 christos
2171 1.1 christos static CORE_ADDR
2172 1.1 christos amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
2173 1.1 christos {
2174 1.1 christos struct symtab_and_line start_pc_sal, next_sal;
2175 1.1 christos gdb_byte buf[4 + 8 * 7];
2176 1.1 christos int offset, xmmreg;
2177 1.1 christos
2178 1.1 christos if (pc == start_pc)
2179 1.1 christos return pc;
2180 1.1 christos
2181 1.1 christos start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
2182 1.1 christos if (start_pc_sal.symtab == NULL
2183 1.1 christos || producer_is_gcc_ge_4 (start_pc_sal.symtab->producer) < 6
2184 1.1 christos || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
2185 1.1 christos return pc;
2186 1.1 christos
2187 1.1 christos next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
2188 1.1 christos if (next_sal.line != start_pc_sal.line)
2189 1.1 christos return pc;
2190 1.1 christos
2191 1.1 christos /* START_PC can be from overlayed memory, ignored here. */
2192 1.1 christos if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
2193 1.1 christos return pc;
2194 1.1 christos
2195 1.1 christos /* test %al,%al */
2196 1.1 christos if (buf[0] != 0x84 || buf[1] != 0xc0)
2197 1.1 christos return pc;
2198 1.1 christos /* je AFTER */
2199 1.1 christos if (buf[2] != 0x74)
2200 1.1 christos return pc;
2201 1.1 christos
2202 1.1 christos offset = 4;
2203 1.1 christos for (xmmreg = 0; xmmreg < 8; xmmreg++)
2204 1.1 christos {
2205 1.1 christos /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
2206 1.1 christos if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
2207 1.1 christos || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
2208 1.1 christos return pc;
2209 1.1 christos
2210 1.1 christos /* 0b01?????? */
2211 1.1 christos if ((buf[offset + 2] & 0xc0) == 0x40)
2212 1.1 christos {
2213 1.1 christos /* 8-bit displacement. */
2214 1.1 christos offset += 4;
2215 1.1 christos }
2216 1.1 christos /* 0b10?????? */
2217 1.1 christos else if ((buf[offset + 2] & 0xc0) == 0x80)
2218 1.1 christos {
2219 1.1 christos /* 32-bit displacement. */
2220 1.1 christos offset += 7;
2221 1.1 christos }
2222 1.1 christos else
2223 1.1 christos return pc;
2224 1.1 christos }
2225 1.1 christos
2226 1.1 christos /* je AFTER */
2227 1.1 christos if (offset - 4 != buf[3])
2228 1.1 christos return pc;
2229 1.1 christos
2230 1.1 christos return next_sal.end;
2231 1.1 christos }
2232 1.1 christos
2233 1.1 christos /* Return PC of first real instruction. */
2234 1.1 christos
2235 1.1 christos static CORE_ADDR
2236 1.1 christos amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2237 1.1 christos {
2238 1.1 christos struct amd64_frame_cache cache;
2239 1.1 christos CORE_ADDR pc;
2240 1.1 christos CORE_ADDR func_addr;
2241 1.1 christos
2242 1.1 christos if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
2243 1.1 christos {
2244 1.1 christos CORE_ADDR post_prologue_pc
2245 1.1 christos = skip_prologue_using_sal (gdbarch, func_addr);
2246 1.1 christos struct symtab *s = find_pc_symtab (func_addr);
2247 1.1 christos
2248 1.1 christos /* Clang always emits a line note before the prologue and another
2249 1.1 christos one after. We trust clang to emit usable line notes. */
2250 1.1 christos if (post_prologue_pc
2251 1.1 christos && (s != NULL
2252 1.1 christos && s->producer != NULL
2253 1.1 christos && strncmp (s->producer, "clang ", sizeof ("clang ") - 1) == 0))
2254 1.1 christos return max (start_pc, post_prologue_pc);
2255 1.1 christos }
2256 1.1 christos
2257 1.1 christos amd64_init_frame_cache (&cache);
2258 1.1 christos pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
2259 1.1 christos &cache);
2260 1.1 christos if (cache.frameless_p)
2261 1.1 christos return start_pc;
2262 1.1 christos
2263 1.1 christos return amd64_skip_xmm_prologue (pc, start_pc);
2264 1.1 christos }
2265 1.1 christos
2266 1.1 christos
2268 1.1 christos /* Normal frames. */
2269 1.1 christos
2270 1.1 christos static void
2271 1.1 christos amd64_frame_cache_1 (struct frame_info *this_frame,
2272 1.1 christos struct amd64_frame_cache *cache)
2273 1.1 christos {
2274 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2275 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2276 1.1 christos gdb_byte buf[8];
2277 1.1 christos int i;
2278 1.1 christos
2279 1.1 christos cache->pc = get_frame_func (this_frame);
2280 1.1 christos if (cache->pc != 0)
2281 1.1 christos amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
2282 1.1 christos cache);
2283 1.1 christos
2284 1.1 christos if (cache->frameless_p)
2285 1.1 christos {
2286 1.1 christos /* We didn't find a valid frame. If we're at the start of a
2287 1.1 christos function, or somewhere half-way its prologue, the function's
2288 1.1 christos frame probably hasn't been fully setup yet. Try to
2289 1.1 christos reconstruct the base address for the stack frame by looking
2290 1.1 christos at the stack pointer. For truly "frameless" functions this
2291 1.1 christos might work too. */
2292 1.1 christos
2293 1.1 christos if (cache->saved_sp_reg != -1)
2294 1.1 christos {
2295 1.1 christos /* Stack pointer has been saved. */
2296 1.1 christos get_frame_register (this_frame, cache->saved_sp_reg, buf);
2297 1.1 christos cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
2298 1.1 christos
2299 1.1 christos /* We're halfway aligning the stack. */
2300 1.1 christos cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
2301 1.1 christos cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
2302 1.1 christos
2303 1.1 christos /* This will be added back below. */
2304 1.1 christos cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
2305 1.1 christos }
2306 1.1 christos else
2307 1.1 christos {
2308 1.1 christos get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2309 1.1 christos cache->base = extract_unsigned_integer (buf, 8, byte_order)
2310 1.1 christos + cache->sp_offset;
2311 1.1 christos }
2312 1.1 christos }
2313 1.1 christos else
2314 1.1 christos {
2315 1.1 christos get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
2316 1.1 christos cache->base = extract_unsigned_integer (buf, 8, byte_order);
2317 1.1 christos }
2318 1.1 christos
2319 1.1 christos /* Now that we have the base address for the stack frame we can
2320 1.1 christos calculate the value of %rsp in the calling frame. */
2321 1.1 christos cache->saved_sp = cache->base + 16;
2322 1.1 christos
2323 1.1 christos /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
2324 1.1 christos frame we find it at the same offset from the reconstructed base
2325 1.1 christos address. If we're halfway aligning the stack, %rip is handled
2326 1.1 christos differently (see above). */
2327 1.1 christos if (!cache->frameless_p || cache->saved_sp_reg == -1)
2328 1.1 christos cache->saved_regs[AMD64_RIP_REGNUM] = 8;
2329 1.1 christos
2330 1.1 christos /* Adjust all the saved registers such that they contain addresses
2331 1.1 christos instead of offsets. */
2332 1.1 christos for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
2333 1.1 christos if (cache->saved_regs[i] != -1)
2334 1.1 christos cache->saved_regs[i] += cache->base;
2335 1.1 christos
2336 1.1 christos cache->base_p = 1;
2337 1.1 christos }
2338 1.1 christos
2339 1.1 christos static struct amd64_frame_cache *
2340 1.1 christos amd64_frame_cache (struct frame_info *this_frame, void **this_cache)
2341 1.1 christos {
2342 1.1 christos volatile struct gdb_exception ex;
2343 1.1 christos struct amd64_frame_cache *cache;
2344 1.1 christos
2345 1.1 christos if (*this_cache)
2346 1.1 christos return *this_cache;
2347 1.1 christos
2348 1.1 christos cache = amd64_alloc_frame_cache ();
2349 1.1 christos *this_cache = cache;
2350 1.1 christos
2351 1.1 christos TRY_CATCH (ex, RETURN_MASK_ERROR)
2352 1.1 christos {
2353 1.1 christos amd64_frame_cache_1 (this_frame, cache);
2354 1.1 christos }
2355 1.1 christos if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
2356 1.1 christos throw_exception (ex);
2357 1.1 christos
2358 1.1 christos return cache;
2359 1.1 christos }
2360 1.1 christos
2361 1.1 christos static enum unwind_stop_reason
2362 1.1 christos amd64_frame_unwind_stop_reason (struct frame_info *this_frame,
2363 1.1 christos void **this_cache)
2364 1.1 christos {
2365 1.1 christos struct amd64_frame_cache *cache =
2366 1.1 christos amd64_frame_cache (this_frame, this_cache);
2367 1.1 christos
2368 1.1 christos if (!cache->base_p)
2369 1.1 christos return UNWIND_UNAVAILABLE;
2370 1.1 christos
2371 1.1 christos /* This marks the outermost frame. */
2372 1.1 christos if (cache->base == 0)
2373 1.1 christos return UNWIND_OUTERMOST;
2374 1.1 christos
2375 1.1 christos return UNWIND_NO_REASON;
2376 1.1 christos }
2377 1.1 christos
2378 1.1 christos static void
2379 1.1 christos amd64_frame_this_id (struct frame_info *this_frame, void **this_cache,
2380 1.1 christos struct frame_id *this_id)
2381 1.1 christos {
2382 1.1 christos struct amd64_frame_cache *cache =
2383 1.1 christos amd64_frame_cache (this_frame, this_cache);
2384 1.1 christos
2385 1.1 christos if (!cache->base_p)
2386 1.1 christos (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2387 1.1 christos else if (cache->base == 0)
2388 1.1 christos {
2389 1.1 christos /* This marks the outermost frame. */
2390 1.1 christos return;
2391 1.1 christos }
2392 1.1 christos else
2393 1.1 christos (*this_id) = frame_id_build (cache->base + 16, cache->pc);
2394 1.1 christos }
2395 1.1 christos
2396 1.1 christos static struct value *
2397 1.1 christos amd64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
2398 1.1 christos int regnum)
2399 1.1 christos {
2400 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2401 1.1 christos struct amd64_frame_cache *cache =
2402 1.1 christos amd64_frame_cache (this_frame, this_cache);
2403 1.1 christos
2404 1.1 christos gdb_assert (regnum >= 0);
2405 1.1 christos
2406 1.1 christos if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2407 1.1 christos return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2408 1.1 christos
2409 1.1 christos if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2410 1.1 christos return frame_unwind_got_memory (this_frame, regnum,
2411 1.1 christos cache->saved_regs[regnum]);
2412 1.1 christos
2413 1.1 christos return frame_unwind_got_register (this_frame, regnum, regnum);
2414 1.1 christos }
2415 1.1 christos
2416 1.1 christos static const struct frame_unwind amd64_frame_unwind =
2417 1.1 christos {
2418 1.1 christos NORMAL_FRAME,
2419 1.1 christos amd64_frame_unwind_stop_reason,
2420 1.1 christos amd64_frame_this_id,
2421 1.1 christos amd64_frame_prev_register,
2422 1.1 christos NULL,
2423 1.1 christos default_frame_sniffer
2424 1.1 christos };
2425 1.1 christos
2426 1.1 christos /* Generate a bytecode expression to get the value of the saved PC. */
2428 1.1 christos
2429 1.1 christos static void
2430 1.1 christos amd64_gen_return_address (struct gdbarch *gdbarch,
2431 1.1 christos struct agent_expr *ax, struct axs_value *value,
2432 1.1 christos CORE_ADDR scope)
2433 1.1 christos {
2434 1.1 christos /* The following sequence assumes the traditional use of the base
2435 1.1 christos register. */
2436 1.1 christos ax_reg (ax, AMD64_RBP_REGNUM);
2437 1.1 christos ax_const_l (ax, 8);
2438 1.1 christos ax_simple (ax, aop_add);
2439 1.1 christos value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
2440 1.1 christos value->kind = axs_lvalue_memory;
2441 1.1 christos }
2442 1.1 christos
2443 1.1 christos
2445 1.1 christos /* Signal trampolines. */
2446 1.1 christos
2447 1.1 christos /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
2448 1.1 christos 64-bit variants. This would require using identical frame caches
2449 1.1 christos on both platforms. */
2450 1.1 christos
2451 1.1 christos static struct amd64_frame_cache *
2452 1.1 christos amd64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
2453 1.1 christos {
2454 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2455 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2456 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2457 1.1 christos volatile struct gdb_exception ex;
2458 1.1 christos struct amd64_frame_cache *cache;
2459 1.1 christos CORE_ADDR addr;
2460 1.1 christos gdb_byte buf[8];
2461 1.1 christos int i;
2462 1.1 christos
2463 1.1 christos if (*this_cache)
2464 1.1 christos return *this_cache;
2465 1.1 christos
2466 1.1 christos cache = amd64_alloc_frame_cache ();
2467 1.1 christos
2468 1.1 christos TRY_CATCH (ex, RETURN_MASK_ERROR)
2469 1.1 christos {
2470 1.1 christos get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2471 1.1 christos cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
2472 1.1 christos
2473 1.1 christos addr = tdep->sigcontext_addr (this_frame);
2474 1.1 christos gdb_assert (tdep->sc_reg_offset);
2475 1.1 christos gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2476 1.1 christos for (i = 0; i < tdep->sc_num_regs; i++)
2477 1.1 christos if (tdep->sc_reg_offset[i] != -1)
2478 1.1 christos cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2479 1.1 christos
2480 1.1 christos cache->base_p = 1;
2481 1.1 christos }
2482 1.1 christos if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
2483 1.1 christos throw_exception (ex);
2484 1.1 christos
2485 1.1 christos *this_cache = cache;
2486 1.1 christos return cache;
2487 1.1 christos }
2488 1.1 christos
2489 1.1 christos static enum unwind_stop_reason
2490 1.1 christos amd64_sigtramp_frame_unwind_stop_reason (struct frame_info *this_frame,
2491 1.1 christos void **this_cache)
2492 1.1 christos {
2493 1.1 christos struct amd64_frame_cache *cache =
2494 1.1 christos amd64_sigtramp_frame_cache (this_frame, this_cache);
2495 1.1 christos
2496 1.1 christos if (!cache->base_p)
2497 1.1 christos return UNWIND_UNAVAILABLE;
2498 1.1 christos
2499 1.1 christos return UNWIND_NO_REASON;
2500 1.1 christos }
2501 1.1 christos
2502 1.1 christos static void
2503 1.1 christos amd64_sigtramp_frame_this_id (struct frame_info *this_frame,
2504 1.1 christos void **this_cache, struct frame_id *this_id)
2505 1.1 christos {
2506 1.1 christos struct amd64_frame_cache *cache =
2507 1.1 christos amd64_sigtramp_frame_cache (this_frame, this_cache);
2508 1.1 christos
2509 1.1 christos if (!cache->base_p)
2510 1.1 christos (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2511 1.1 christos else if (cache->base == 0)
2512 1.1 christos {
2513 1.1 christos /* This marks the outermost frame. */
2514 1.1 christos return;
2515 1.1 christos }
2516 1.1 christos else
2517 1.1 christos (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
2518 1.1 christos }
2519 1.1 christos
2520 1.1 christos static struct value *
2521 1.1 christos amd64_sigtramp_frame_prev_register (struct frame_info *this_frame,
2522 1.1 christos void **this_cache, int regnum)
2523 1.1 christos {
2524 1.1 christos /* Make sure we've initialized the cache. */
2525 1.1 christos amd64_sigtramp_frame_cache (this_frame, this_cache);
2526 1.1 christos
2527 1.1 christos return amd64_frame_prev_register (this_frame, this_cache, regnum);
2528 1.1 christos }
2529 1.1 christos
2530 1.1 christos static int
2531 1.1 christos amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2532 1.1 christos struct frame_info *this_frame,
2533 1.1 christos void **this_cache)
2534 1.1 christos {
2535 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
2536 1.1 christos
2537 1.1 christos /* We shouldn't even bother if we don't have a sigcontext_addr
2538 1.1 christos handler. */
2539 1.1 christos if (tdep->sigcontext_addr == NULL)
2540 1.1 christos return 0;
2541 1.1 christos
2542 1.1 christos if (tdep->sigtramp_p != NULL)
2543 1.1 christos {
2544 1.1 christos if (tdep->sigtramp_p (this_frame))
2545 1.1 christos return 1;
2546 1.1 christos }
2547 1.1 christos
2548 1.1 christos if (tdep->sigtramp_start != 0)
2549 1.1 christos {
2550 1.1 christos CORE_ADDR pc = get_frame_pc (this_frame);
2551 1.1 christos
2552 1.1 christos gdb_assert (tdep->sigtramp_end != 0);
2553 1.1 christos if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2554 1.1 christos return 1;
2555 1.1 christos }
2556 1.1 christos
2557 1.1 christos return 0;
2558 1.1 christos }
2559 1.1 christos
2560 1.1 christos static const struct frame_unwind amd64_sigtramp_frame_unwind =
2561 1.1 christos {
2562 1.1 christos SIGTRAMP_FRAME,
2563 1.1 christos amd64_sigtramp_frame_unwind_stop_reason,
2564 1.1 christos amd64_sigtramp_frame_this_id,
2565 1.1 christos amd64_sigtramp_frame_prev_register,
2566 1.1 christos NULL,
2567 1.1 christos amd64_sigtramp_frame_sniffer
2568 1.1 christos };
2569 1.1 christos
2570 1.1 christos
2572 1.1 christos static CORE_ADDR
2573 1.1 christos amd64_frame_base_address (struct frame_info *this_frame, void **this_cache)
2574 1.1 christos {
2575 1.1 christos struct amd64_frame_cache *cache =
2576 1.1 christos amd64_frame_cache (this_frame, this_cache);
2577 1.1 christos
2578 1.1 christos return cache->base;
2579 1.1 christos }
2580 1.1 christos
2581 1.1 christos static const struct frame_base amd64_frame_base =
2582 1.1 christos {
2583 1.1 christos &amd64_frame_unwind,
2584 1.1 christos amd64_frame_base_address,
2585 1.1 christos amd64_frame_base_address,
2586 1.1 christos amd64_frame_base_address
2587 1.1 christos };
2588 1.1 christos
2589 1.1 christos /* Normal frames, but in a function epilogue. */
2590 1.1 christos
2591 1.1 christos /* The epilogue is defined here as the 'ret' instruction, which will
2592 1.1 christos follow any instruction such as 'leave' or 'pop %ebp' that destroys
2593 1.1 christos the function's stack frame. */
2594 1.1 christos
2595 1.1 christos static int
2596 1.1 christos amd64_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2597 1.1 christos {
2598 1.1 christos gdb_byte insn;
2599 1.1 christos struct symtab *symtab;
2600 1.1 christos
2601 1.1 christos symtab = find_pc_symtab (pc);
2602 1.1 christos if (symtab && symtab->epilogue_unwind_valid)
2603 1.1 christos return 0;
2604 1.1 christos
2605 1.1 christos if (target_read_memory (pc, &insn, 1))
2606 1.1 christos return 0; /* Can't read memory at pc. */
2607 1.1 christos
2608 1.1 christos if (insn != 0xc3) /* 'ret' instruction. */
2609 1.1 christos return 0;
2610 1.1 christos
2611 1.1 christos return 1;
2612 1.1 christos }
2613 1.1 christos
2614 1.1 christos static int
2615 1.1 christos amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2616 1.1 christos struct frame_info *this_frame,
2617 1.1 christos void **this_prologue_cache)
2618 1.1 christos {
2619 1.1 christos if (frame_relative_level (this_frame) == 0)
2620 1.1 christos return amd64_in_function_epilogue_p (get_frame_arch (this_frame),
2621 1.1 christos get_frame_pc (this_frame));
2622 1.1 christos else
2623 1.1 christos return 0;
2624 1.1 christos }
2625 1.1 christos
2626 1.1 christos static struct amd64_frame_cache *
2627 1.1 christos amd64_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
2628 1.1 christos {
2629 1.1 christos struct gdbarch *gdbarch = get_frame_arch (this_frame);
2630 1.1 christos enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2631 1.1 christos volatile struct gdb_exception ex;
2632 1.1 christos struct amd64_frame_cache *cache;
2633 1.1 christos gdb_byte buf[8];
2634 1.1 christos
2635 1.1 christos if (*this_cache)
2636 1.1 christos return *this_cache;
2637 1.1 christos
2638 1.1 christos cache = amd64_alloc_frame_cache ();
2639 1.1 christos *this_cache = cache;
2640 1.1 christos
2641 1.1 christos TRY_CATCH (ex, RETURN_MASK_ERROR)
2642 1.1 christos {
2643 1.1 christos /* Cache base will be %esp plus cache->sp_offset (-8). */
2644 1.1 christos get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2645 1.1 christos cache->base = extract_unsigned_integer (buf, 8,
2646 1.1 christos byte_order) + cache->sp_offset;
2647 1.1 christos
2648 1.1 christos /* Cache pc will be the frame func. */
2649 1.1 christos cache->pc = get_frame_pc (this_frame);
2650 1.1 christos
2651 1.1 christos /* The saved %esp will be at cache->base plus 16. */
2652 1.1 christos cache->saved_sp = cache->base + 16;
2653 1.1 christos
2654 1.1 christos /* The saved %eip will be at cache->base plus 8. */
2655 1.1 christos cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2656 1.1 christos
2657 1.1 christos cache->base_p = 1;
2658 1.1 christos }
2659 1.1 christos if (ex.reason < 0 && ex.error != NOT_AVAILABLE_ERROR)
2660 1.1 christos throw_exception (ex);
2661 1.1 christos
2662 1.1 christos return cache;
2663 1.1 christos }
2664 1.1 christos
2665 1.1 christos static enum unwind_stop_reason
2666 1.1 christos amd64_epilogue_frame_unwind_stop_reason (struct frame_info *this_frame,
2667 1.1 christos void **this_cache)
2668 1.1 christos {
2669 1.1 christos struct amd64_frame_cache *cache
2670 1.1 christos = amd64_epilogue_frame_cache (this_frame, this_cache);
2671 1.1 christos
2672 1.1 christos if (!cache->base_p)
2673 1.1 christos return UNWIND_UNAVAILABLE;
2674 1.1 christos
2675 1.1 christos return UNWIND_NO_REASON;
2676 1.1 christos }
2677 1.1 christos
2678 1.1 christos static void
2679 1.1 christos amd64_epilogue_frame_this_id (struct frame_info *this_frame,
2680 1.1 christos void **this_cache,
2681 1.1 christos struct frame_id *this_id)
2682 1.1 christos {
2683 1.1 christos struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
2684 1.1 christos this_cache);
2685 1.1 christos
2686 1.1 christos if (!cache->base_p)
2687 1.1 christos (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2688 1.1 christos else
2689 1.1 christos (*this_id) = frame_id_build (cache->base + 8, cache->pc);
2690 1.1 christos }
2691 1.1 christos
2692 1.1 christos static const struct frame_unwind amd64_epilogue_frame_unwind =
2693 1.1 christos {
2694 1.1 christos NORMAL_FRAME,
2695 1.1 christos amd64_epilogue_frame_unwind_stop_reason,
2696 1.1 christos amd64_epilogue_frame_this_id,
2697 1.1 christos amd64_frame_prev_register,
2698 1.1 christos NULL,
2699 1.1 christos amd64_epilogue_frame_sniffer
2700 1.1 christos };
2701 1.1 christos
2702 1.1 christos static struct frame_id
2703 1.1 christos amd64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2704 1.1 christos {
2705 1.1 christos CORE_ADDR fp;
2706 1.1 christos
2707 1.1 christos fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
2708 1.1 christos
2709 1.1 christos return frame_id_build (fp + 16, get_frame_pc (this_frame));
2710 1.1 christos }
2711 1.1 christos
2712 1.1 christos /* 16 byte align the SP per frame requirements. */
2713 1.1 christos
2714 1.1 christos static CORE_ADDR
2715 1.1 christos amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
2716 1.1 christos {
2717 1.1 christos return sp & -(CORE_ADDR)16;
2718 1.1 christos }
2719 1.1 christos
2720 1.1 christos
2722 1.1 christos /* Supply register REGNUM from the buffer specified by FPREGS and LEN
2723 1.1 christos in the floating-point register set REGSET to register cache
2724 1.1 christos REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
2725 1.1 christos
2726 1.1 christos static void
2727 1.1 christos amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2728 1.1 christos int regnum, const void *fpregs, size_t len)
2729 1.1 christos {
2730 1.1 christos const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2731 1.1 christos
2732 1.1 christos gdb_assert (len == tdep->sizeof_fpregset);
2733 1.1 christos amd64_supply_fxsave (regcache, regnum, fpregs);
2734 1.1 christos }
2735 1.1 christos
2736 1.1 christos /* Collect register REGNUM from the register cache REGCACHE and store
2737 1.1 christos it in the buffer specified by FPREGS and LEN as described by the
2738 1.1 christos floating-point register set REGSET. If REGNUM is -1, do this for
2739 1.1 christos all registers in REGSET. */
2740 1.1 christos
2741 1.1 christos static void
2742 1.1 christos amd64_collect_fpregset (const struct regset *regset,
2743 1.1 christos const struct regcache *regcache,
2744 1.1 christos int regnum, void *fpregs, size_t len)
2745 1.1 christos {
2746 1.1 christos const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2747 1.1 christos
2748 1.1 christos gdb_assert (len == tdep->sizeof_fpregset);
2749 1.1 christos amd64_collect_fxsave (regcache, regnum, fpregs);
2750 1.1 christos }
2751 1.1 christos
2752 1.1 christos /* Similar to amd64_supply_fpregset, but use XSAVE extended state. */
2753 1.1 christos
2754 1.1 christos static void
2755 1.1 christos amd64_supply_xstateregset (const struct regset *regset,
2756 1.1 christos struct regcache *regcache, int regnum,
2757 1.1 christos const void *xstateregs, size_t len)
2758 1.1 christos {
2759 1.1 christos amd64_supply_xsave (regcache, regnum, xstateregs);
2760 1.1 christos }
2761 1.1 christos
2762 1.1 christos /* Similar to amd64_collect_fpregset, but use XSAVE extended state. */
2763 1.1 christos
2764 1.1 christos static void
2765 1.1 christos amd64_collect_xstateregset (const struct regset *regset,
2766 1.1 christos const struct regcache *regcache,
2767 1.1 christos int regnum, void *xstateregs, size_t len)
2768 1.1 christos {
2769 1.1 christos amd64_collect_xsave (regcache, regnum, xstateregs, 1);
2770 1.1 christos }
2771 1.1 christos
2772 1.1 christos /* Return the appropriate register set for the core section identified
2773 1.1 christos by SECT_NAME and SECT_SIZE. */
2774 1.1 christos
2775 1.1 christos static const struct regset *
2776 1.1 christos amd64_regset_from_core_section (struct gdbarch *gdbarch,
2777 1.1 christos const char *sect_name, size_t sect_size)
2778 1.1 christos {
2779 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2780 1.1 christos
2781 1.1 christos if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
2782 1.1 christos {
2783 1.1 christos if (tdep->fpregset == NULL)
2784 1.1 christos tdep->fpregset = regset_alloc (gdbarch, amd64_supply_fpregset,
2785 1.1 christos amd64_collect_fpregset);
2786 1.1 christos
2787 1.1 christos return tdep->fpregset;
2788 1.1 christos }
2789 1.1 christos
2790 1.1 christos if (strcmp (sect_name, ".reg-xstate") == 0)
2791 1.1 christos {
2792 1.1 christos if (tdep->xstateregset == NULL)
2793 1.1 christos tdep->xstateregset = regset_alloc (gdbarch,
2794 1.1 christos amd64_supply_xstateregset,
2795 1.1 christos amd64_collect_xstateregset);
2796 1.1 christos
2797 1.1 christos return tdep->xstateregset;
2798 1.1 christos }
2799 1.1 christos
2800 1.1 christos return i386_regset_from_core_section (gdbarch, sect_name, sect_size);
2801 1.1 christos }
2802 1.1 christos
2803 1.1 christos
2805 1.1 christos /* Figure out where the longjmp will land. Slurp the jmp_buf out of
2806 1.1 christos %rdi. We expect its value to be a pointer to the jmp_buf structure
2807 1.1 christos from which we extract the address that we will land at. This
2808 1.1 christos address is copied into PC. This routine returns non-zero on
2809 1.1 christos success. */
2810 1.1 christos
2811 1.1 christos static int
2812 1.1 christos amd64_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
2813 1.1 christos {
2814 1.1 christos gdb_byte buf[8];
2815 1.1 christos CORE_ADDR jb_addr;
2816 1.1 christos struct gdbarch *gdbarch = get_frame_arch (frame);
2817 1.1 christos int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
2818 1.1 christos int len = TYPE_LENGTH (builtin_type (gdbarch)->builtin_func_ptr);
2819 1.1 christos
2820 1.1 christos /* If JB_PC_OFFSET is -1, we have no way to find out where the
2821 1.1 christos longjmp will land. */
2822 1.1 christos if (jb_pc_offset == -1)
2823 1.1 christos return 0;
2824 1.1 christos
2825 1.1 christos get_frame_register (frame, AMD64_RDI_REGNUM, buf);
2826 1.1 christos jb_addr= extract_typed_address
2827 1.1 christos (buf, builtin_type (gdbarch)->builtin_data_ptr);
2828 1.1 christos if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
2829 1.1 christos return 0;
2830 1.1 christos
2831 1.1 christos *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
2832 1.1 christos
2833 1.1 christos return 1;
2834 1.1 christos }
2835 1.1 christos
2836 1.1 christos static const int amd64_record_regmap[] =
2837 1.1 christos {
2838 1.1 christos AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
2839 1.1 christos AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
2840 1.1 christos AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
2841 1.1 christos AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
2842 1.1 christos AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
2843 1.1 christos AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
2844 1.1 christos };
2845 1.1 christos
2846 1.1 christos void
2847 1.1 christos amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2848 1.1 christos {
2849 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2850 1.1 christos const struct target_desc *tdesc = info.target_desc;
2851 1.1 christos static const char *const stap_integer_prefixes[] = { "$", NULL };
2852 1.1 christos static const char *const stap_register_prefixes[] = { "%", NULL };
2853 1.1 christos static const char *const stap_register_indirection_prefixes[] = { "(",
2854 1.1 christos NULL };
2855 1.1 christos static const char *const stap_register_indirection_suffixes[] = { ")",
2856 1.1 christos NULL };
2857 1.1 christos
2858 1.1 christos /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
2859 1.1 christos floating-point registers. */
2860 1.1 christos tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
2861 1.1 christos
2862 1.1 christos if (! tdesc_has_registers (tdesc))
2863 1.1 christos tdesc = tdesc_amd64;
2864 1.1 christos tdep->tdesc = tdesc;
2865 1.1 christos
2866 1.1 christos tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
2867 1.1 christos tdep->register_names = amd64_register_names;
2868 1.1 christos
2869 1.1 christos if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
2870 1.1 christos {
2871 1.1 christos tdep->ymmh_register_names = amd64_ymmh_names;
2872 1.1 christos tdep->num_ymm_regs = 16;
2873 1.1 christos tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
2874 1.1 christos }
2875 1.1 christos
2876 1.1 christos if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
2877 1.1 christos {
2878 1.1 christos tdep->mpx_register_names = amd64_mpx_names;
2879 1.1 christos tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
2880 1.1 christos tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
2881 1.1 christos }
2882 1.1 christos
2883 1.1 christos tdep->num_byte_regs = 20;
2884 1.1 christos tdep->num_word_regs = 16;
2885 1.1 christos tdep->num_dword_regs = 16;
2886 1.1 christos /* Avoid wiring in the MMX registers for now. */
2887 1.1 christos tdep->num_mmx_regs = 0;
2888 1.1 christos
2889 1.1 christos set_gdbarch_pseudo_register_read_value (gdbarch,
2890 1.1 christos amd64_pseudo_register_read_value);
2891 1.1 christos set_gdbarch_pseudo_register_write (gdbarch,
2892 1.1 christos amd64_pseudo_register_write);
2893 1.1 christos
2894 1.1 christos set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
2895 1.1 christos
2896 1.1 christos /* AMD64 has an FPU and 16 SSE registers. */
2897 1.1 christos tdep->st0_regnum = AMD64_ST0_REGNUM;
2898 1.1 christos tdep->num_xmm_regs = 16;
2899 1.1 christos
2900 1.1 christos /* This is what all the fuss is about. */
2901 1.1 christos set_gdbarch_long_bit (gdbarch, 64);
2902 1.1 christos set_gdbarch_long_long_bit (gdbarch, 64);
2903 1.1 christos set_gdbarch_ptr_bit (gdbarch, 64);
2904 1.1 christos
2905 1.1 christos /* In contrast to the i386, on AMD64 a `long double' actually takes
2906 1.1 christos up 128 bits, even though it's still based on the i387 extended
2907 1.1 christos floating-point format which has only 80 significant bits. */
2908 1.1 christos set_gdbarch_long_double_bit (gdbarch, 128);
2909 1.1 christos
2910 1.1 christos set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
2911 1.1 christos
2912 1.1 christos /* Register numbers of various important registers. */
2913 1.1 christos set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
2914 1.1 christos set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
2915 1.1 christos set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
2916 1.1 christos set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
2917 1.1 christos
2918 1.1 christos /* The "default" register numbering scheme for AMD64 is referred to
2919 1.1 christos as the "DWARF Register Number Mapping" in the System V psABI.
2920 1.1 christos The preferred debugging format for all known AMD64 targets is
2921 1.1 christos actually DWARF2, and GCC doesn't seem to support DWARF (that is
2922 1.1 christos DWARF-1), but we provide the same mapping just in case. This
2923 1.1 christos mapping is also used for stabs, which GCC does support. */
2924 1.1 christos set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
2925 1.1 christos set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
2926 1.1 christos
2927 1.1 christos /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
2928 1.1 christos be in use on any of the supported AMD64 targets. */
2929 1.1 christos
2930 1.1 christos /* Call dummy code. */
2931 1.1 christos set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
2932 1.1 christos set_gdbarch_frame_align (gdbarch, amd64_frame_align);
2933 1.1 christos set_gdbarch_frame_red_zone_size (gdbarch, 128);
2934 1.1 christos
2935 1.1 christos set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
2936 1.1 christos set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
2937 1.1 christos set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
2938 1.1 christos
2939 1.1 christos set_gdbarch_return_value (gdbarch, amd64_return_value);
2940 1.1 christos
2941 1.1 christos set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
2942 1.1 christos
2943 1.1 christos tdep->record_regmap = amd64_record_regmap;
2944 1.1 christos
2945 1.1 christos set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
2946 1.1 christos
2947 1.1 christos /* Hook the function epilogue frame unwinder. This unwinder is
2948 1.1 christos appended to the list first, so that it supercedes the other
2949 1.1 christos unwinders in function epilogues. */
2950 1.1 christos frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
2951 1.1 christos
2952 1.1 christos /* Hook the prologue-based frame unwinders. */
2953 1.1 christos frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
2954 1.1 christos frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
2955 1.1 christos frame_base_set_default (gdbarch, &amd64_frame_base);
2956 1.1 christos
2957 1.1 christos /* If we have a register mapping, enable the generic core file support. */
2958 1.1 christos if (tdep->gregset_reg_offset)
2959 1.1 christos set_gdbarch_regset_from_core_section (gdbarch,
2960 1.1 christos amd64_regset_from_core_section);
2961 1.1 christos
2962 1.1 christos set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
2963 1.1 christos
2964 1.1 christos set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
2965 1.1 christos
2966 1.1 christos set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
2967 1.1 christos
2968 1.1 christos /* SystemTap variables and functions. */
2969 1.1 christos set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
2970 1.1 christos set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
2971 1.1 christos set_gdbarch_stap_register_indirection_prefixes (gdbarch,
2972 1.1 christos stap_register_indirection_prefixes);
2973 1.1 christos set_gdbarch_stap_register_indirection_suffixes (gdbarch,
2974 1.1 christos stap_register_indirection_suffixes);
2975 1.1 christos set_gdbarch_stap_is_single_operand (gdbarch,
2976 1.1 christos i386_stap_is_single_operand);
2977 1.1 christos set_gdbarch_stap_parse_special_token (gdbarch,
2978 1.1 christos i386_stap_parse_special_token);
2979 1.1 christos }
2980 1.1 christos
2981 1.1 christos
2983 1.1 christos static struct type *
2984 1.1 christos amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
2985 1.1 christos {
2986 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2987 1.1 christos
2988 1.1 christos switch (regnum - tdep->eax_regnum)
2989 1.1 christos {
2990 1.1 christos case AMD64_RBP_REGNUM: /* %ebp */
2991 1.1 christos case AMD64_RSP_REGNUM: /* %esp */
2992 1.1 christos return builtin_type (gdbarch)->builtin_data_ptr;
2993 1.1 christos case AMD64_RIP_REGNUM: /* %eip */
2994 1.1 christos return builtin_type (gdbarch)->builtin_func_ptr;
2995 1.1 christos }
2996 1.1 christos
2997 1.1 christos return i386_pseudo_register_type (gdbarch, regnum);
2998 1.1 christos }
2999 1.1 christos
3000 1.1 christos void
3001 1.1 christos amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
3002 1.1 christos {
3003 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3004 1.1 christos const struct target_desc *tdesc = info.target_desc;
3005 1.1 christos
3006 1.1 christos amd64_init_abi (info, gdbarch);
3007 1.1 christos
3008 1.1 christos if (! tdesc_has_registers (tdesc))
3009 1.1 christos tdesc = tdesc_x32;
3010 1.1 christos tdep->tdesc = tdesc;
3011 1.1 christos
3012 1.1 christos tdep->num_dword_regs = 17;
3013 1.1 christos set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
3014 1.1 christos
3015 1.1 christos set_gdbarch_long_bit (gdbarch, 32);
3016 1.1 christos set_gdbarch_ptr_bit (gdbarch, 32);
3017 1.1 christos }
3018 1.1 christos
3019 1.1 christos /* Provide a prototype to silence -Wmissing-prototypes. */
3020 1.1 christos void _initialize_amd64_tdep (void);
3021 1.1 christos
3022 1.1 christos void
3023 1.1 christos _initialize_amd64_tdep (void)
3024 1.1 christos {
3025 1.1 christos initialize_tdesc_amd64 ();
3026 1.1 christos initialize_tdesc_amd64_avx ();
3027 1.1 christos initialize_tdesc_amd64_mpx ();
3028 1.1 christos initialize_tdesc_x32 ();
3029 1.1 christos initialize_tdesc_x32_avx ();
3030 1.1 christos }
3031 1.1 christos
3032 1.1 christos
3034 1.1 christos /* The 64-bit FXSAVE format differs from the 32-bit format in the
3035 1.1 christos sense that the instruction pointer and data pointer are simply
3036 1.1 christos 64-bit offsets into the code segment and the data segment instead
3037 1.1 christos of a selector offset pair. The functions below store the upper 32
3038 1.1 christos bits of these pointers (instead of just the 16-bits of the segment
3039 1.1 christos selector). */
3040 1.1 christos
3041 1.1 christos /* Fill register REGNUM in REGCACHE with the appropriate
3042 1.1 christos floating-point or SSE register value from *FXSAVE. If REGNUM is
3043 1.1 christos -1, do this for all registers. This function masks off any of the
3044 1.1 christos reserved bits in *FXSAVE. */
3045 1.1 christos
3046 1.1 christos void
3047 1.1 christos amd64_supply_fxsave (struct regcache *regcache, int regnum,
3048 1.1 christos const void *fxsave)
3049 1.1 christos {
3050 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
3051 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3052 1.1 christos
3053 1.1 christos i387_supply_fxsave (regcache, regnum, fxsave);
3054 1.1 christos
3055 1.1 christos if (fxsave
3056 1.1 christos && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3057 1.1 christos {
3058 1.1 christos const gdb_byte *regs = fxsave;
3059 1.1 christos
3060 1.1 christos if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3061 1.1 christos regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
3062 1.1 christos if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3063 1.1 christos regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
3064 1.1 christos }
3065 1.1 christos }
3066 1.1 christos
3067 1.1 christos /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
3068 1.1 christos
3069 1.1 christos void
3070 1.1 christos amd64_supply_xsave (struct regcache *regcache, int regnum,
3071 1.1 christos const void *xsave)
3072 1.1 christos {
3073 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
3074 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3075 1.1 christos
3076 1.1 christos i387_supply_xsave (regcache, regnum, xsave);
3077 1.1 christos
3078 1.1 christos if (xsave
3079 1.1 christos && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3080 1.1 christos {
3081 1.1 christos const gdb_byte *regs = xsave;
3082 1.1 christos
3083 1.1 christos if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3084 1.1 christos regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep),
3085 1.1 christos regs + 12);
3086 1.1 christos if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3087 1.1 christos regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep),
3088 1.1 christos regs + 20);
3089 1.1 christos }
3090 1.1 christos }
3091 1.1 christos
3092 1.1 christos /* Fill register REGNUM (if it is a floating-point or SSE register) in
3093 1.1 christos *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
3094 1.1 christos all registers. This function doesn't touch any of the reserved
3095 1.1 christos bits in *FXSAVE. */
3096 1.1 christos
3097 1.1 christos void
3098 1.1 christos amd64_collect_fxsave (const struct regcache *regcache, int regnum,
3099 1.1 christos void *fxsave)
3100 1.1 christos {
3101 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
3102 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3103 1.1 christos gdb_byte *regs = fxsave;
3104 1.1 christos
3105 1.1 christos i387_collect_fxsave (regcache, regnum, fxsave);
3106 1.1 christos
3107 1.1 christos if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3108 1.1 christos {
3109 1.1 christos if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3110 1.1 christos regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
3111 1.1 christos if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3112 1.1 christos regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
3113 1.1 christos }
3114 1.1 christos }
3115 1.1 christos
3116 1.1 christos /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
3117 1.1 christos
3118 1.1 christos void
3119 1.1 christos amd64_collect_xsave (const struct regcache *regcache, int regnum,
3120 1.1 christos void *xsave, int gcore)
3121 1.1 christos {
3122 1.1 christos struct gdbarch *gdbarch = get_regcache_arch (regcache);
3123 1.1 christos struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3124 1.1 christos gdb_byte *regs = xsave;
3125 1.1 christos
3126 i387_collect_xsave (regcache, regnum, xsave, gcore);
3127
3128 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3129 {
3130 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3131 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep),
3132 regs + 12);
3133 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3134 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep),
3135 regs + 20);
3136 }
3137 }
3138