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