1 /* Target-dependent code for AMD64.
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Jiri Smid, SuSE Labs.
8 This file is part of GDB.
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
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
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.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "arch-utils.h"
26 #include "dummy-frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
38 #include "gdb_assert.h"
40 #include "amd64-tdep.h"
41 #include "i387-tdep.h"
43 /* Note that the AMD64 architecture was previously known as x86-64.
44 The latter is (forever) engraved into the canonical system name as
45 returned by config.guess, and used as the name for the AMD64 port
46 of GNU/Linux. The BSD's have renamed their ports to amd64; they
47 don't like to shout. For GDB we prefer the amd64_-prefix over the
48 x86_64_-prefix since it's so much easier to type. */
50 /* Register information. */
52 static const char *amd64_register_names[] =
54 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
56 /* %r8 is indeed register number 8. */
57 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
58 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
60 /* %st0 is register number 24. */
61 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
62 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
64 /* %xmm0 is register number 40. */
65 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
66 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
70 /* Total number of registers. */
71 #define AMD64_NUM_REGS ARRAY_SIZE (amd64_register_names)
73 /* Return the name of register REGNUM. */
76 amd64_register_name (struct gdbarch *gdbarch, int regnum)
78 if (regnum >= 0 && regnum < AMD64_NUM_REGS)
79 return amd64_register_names[regnum];
84 /* Return the GDB type object for the "standard" data type of data in
88 amd64_register_type (struct gdbarch *gdbarch, int regnum)
90 if (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_RDI_REGNUM)
91 return builtin_type_int64;
92 if (regnum == AMD64_RBP_REGNUM || regnum == AMD64_RSP_REGNUM)
93 return builtin_type_void_data_ptr;
94 if (regnum >= AMD64_R8_REGNUM && regnum <= AMD64_R15_REGNUM)
95 return builtin_type_int64;
96 if (regnum == AMD64_RIP_REGNUM)
97 return builtin_type_void_func_ptr;
98 if (regnum == AMD64_EFLAGS_REGNUM)
99 return i386_eflags_type;
100 if (regnum >= AMD64_CS_REGNUM && regnum <= AMD64_GS_REGNUM)
101 return builtin_type_int32;
102 if (regnum >= AMD64_ST0_REGNUM && regnum <= AMD64_ST0_REGNUM + 7)
103 return builtin_type_i387_ext;
104 if (regnum >= AMD64_FCTRL_REGNUM && regnum <= AMD64_FCTRL_REGNUM + 7)
105 return builtin_type_int32;
106 if (regnum >= AMD64_XMM0_REGNUM && regnum <= AMD64_XMM0_REGNUM + 15)
107 return i386_sse_type (gdbarch);
108 if (regnum == AMD64_MXCSR_REGNUM)
109 return i386_mxcsr_type;
111 internal_error (__FILE__, __LINE__, _("invalid regnum"));
114 /* DWARF Register Number Mapping as defined in the System V psABI,
117 static int amd64_dwarf_regmap[] =
119 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
120 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
121 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
122 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
124 /* Frame Pointer Register RBP. */
127 /* Stack Pointer Register RSP. */
130 /* Extended Integer Registers 8 - 15. */
131 8, 9, 10, 11, 12, 13, 14, 15,
133 /* Return Address RA. Mapped to RIP. */
136 /* SSE Registers 0 - 7. */
137 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
138 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
139 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
140 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
142 /* Extended SSE Registers 8 - 15. */
143 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
144 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
145 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
146 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
148 /* Floating Point Registers 0-7. */
149 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
150 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
151 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
152 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
154 /* Control and Status Flags Register. */
157 /* Selector Registers. */
167 /* Segment Base Address Registers. */
173 /* Special Selector Registers. */
177 /* Floating Point Control Registers. */
183 static const int amd64_dwarf_regmap_len =
184 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
186 /* Convert DWARF register number REG to the appropriate register
187 number used by GDB. */
190 amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
194 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
195 regnum = amd64_dwarf_regmap[reg];
198 warning (_("Unmapped DWARF Register #%d encountered."), reg);
205 /* Register classes as defined in the psABI. */
219 /* Return the union class of CLASS1 and CLASS2. See the psABI for
222 static enum amd64_reg_class
223 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
225 /* Rule (a): If both classes are equal, this is the resulting class. */
226 if (class1 == class2)
229 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
230 is the other class. */
231 if (class1 == AMD64_NO_CLASS)
233 if (class2 == AMD64_NO_CLASS)
236 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
237 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
240 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
241 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
242 return AMD64_INTEGER;
244 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
245 MEMORY is used as class. */
246 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
247 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
248 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
251 /* Rule (f): Otherwise class SSE is used. */
255 static void amd64_classify (struct type *type, enum amd64_reg_class class[2]);
257 /* Return non-zero if TYPE is a non-POD structure or union type. */
260 amd64_non_pod_p (struct type *type)
262 /* ??? A class with a base class certainly isn't POD, but does this
263 catch all non-POD structure types? */
264 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
270 /* Classify TYPE according to the rules for aggregate (structures and
271 arrays) and union types, and store the result in CLASS. */
274 amd64_classify_aggregate (struct type *type, enum amd64_reg_class class[2])
276 int len = TYPE_LENGTH (type);
278 /* 1. If the size of an object is larger than two eightbytes, or in
279 C++, is a non-POD structure or union type, or contains
280 unaligned fields, it has class memory. */
281 if (len > 16 || amd64_non_pod_p (type))
283 class[0] = class[1] = AMD64_MEMORY;
287 /* 2. Both eightbytes get initialized to class NO_CLASS. */
288 class[0] = class[1] = AMD64_NO_CLASS;
290 /* 3. Each field of an object is classified recursively so that
291 always two fields are considered. The resulting class is
292 calculated according to the classes of the fields in the
295 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
297 struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
299 /* All fields in an array have the same type. */
300 amd64_classify (subtype, class);
301 if (len > 8 && class[1] == AMD64_NO_CLASS)
308 /* Structure or union. */
309 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
310 || TYPE_CODE (type) == TYPE_CODE_UNION);
312 for (i = 0; i < TYPE_NFIELDS (type); i++)
314 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
315 int pos = TYPE_FIELD_BITPOS (type, i) / 64;
316 enum amd64_reg_class subclass[2];
318 /* Ignore static fields. */
319 if (TYPE_FIELD_STATIC (type, i))
322 gdb_assert (pos == 0 || pos == 1);
324 amd64_classify (subtype, subclass);
325 class[pos] = amd64_merge_classes (class[pos], subclass[0]);
327 class[1] = amd64_merge_classes (class[1], subclass[1]);
331 /* 4. Then a post merger cleanup is done: */
333 /* Rule (a): If one of the classes is MEMORY, the whole argument is
335 if (class[0] == AMD64_MEMORY || class[1] == AMD64_MEMORY)
336 class[0] = class[1] = AMD64_MEMORY;
338 /* Rule (b): If SSEUP is not preceeded by SSE, it is converted to
340 if (class[0] == AMD64_SSEUP)
341 class[0] = AMD64_SSE;
342 if (class[1] == AMD64_SSEUP && class[0] != AMD64_SSE)
343 class[1] = AMD64_SSE;
346 /* Classify TYPE, and store the result in CLASS. */
349 amd64_classify (struct type *type, enum amd64_reg_class class[2])
351 enum type_code code = TYPE_CODE (type);
352 int len = TYPE_LENGTH (type);
354 class[0] = class[1] = AMD64_NO_CLASS;
356 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
357 long, long long, and pointers are in the INTEGER class. Similarly,
358 range types, used by languages such as Ada, are also in the INTEGER
360 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
361 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
362 || code == TYPE_CODE_CHAR
363 || code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
364 && (len == 1 || len == 2 || len == 4 || len == 8))
365 class[0] = AMD64_INTEGER;
367 /* Arguments of types float, double, _Decimal32, _Decimal64 and __m64
369 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
370 && (len == 4 || len == 8))
372 class[0] = AMD64_SSE;
374 /* Arguments of types __float128, _Decimal128 and __m128 are split into
375 two halves. The least significant ones belong to class SSE, the most
376 significant one to class SSEUP. */
377 else if (code == TYPE_CODE_DECFLOAT && len == 16)
378 /* FIXME: __float128, __m128. */
379 class[0] = AMD64_SSE, class[1] = AMD64_SSEUP;
381 /* The 64-bit mantissa of arguments of type long double belongs to
382 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
384 else if (code == TYPE_CODE_FLT && len == 16)
385 /* Class X87 and X87UP. */
386 class[0] = AMD64_X87, class[1] = AMD64_X87UP;
389 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
390 || code == TYPE_CODE_UNION)
391 amd64_classify_aggregate (type, class);
394 static enum return_value_convention
395 amd64_return_value (struct gdbarch *gdbarch, struct type *func_type,
396 struct type *type, struct regcache *regcache,
397 gdb_byte *readbuf, const gdb_byte *writebuf)
399 enum amd64_reg_class class[2];
400 int len = TYPE_LENGTH (type);
401 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
402 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
407 gdb_assert (!(readbuf && writebuf));
409 /* 1. Classify the return type with the classification algorithm. */
410 amd64_classify (type, class);
412 /* 2. If the type has class MEMORY, then the caller provides space
413 for the return value and passes the address of this storage in
414 %rdi as if it were the first argument to the function. In effect,
415 this address becomes a hidden first argument.
417 On return %rax will contain the address that has been passed in
418 by the caller in %rdi. */
419 if (class[0] == AMD64_MEMORY)
421 /* As indicated by the comment above, the ABI guarantees that we
422 can always find the return value just after the function has
429 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
430 read_memory (addr, readbuf, TYPE_LENGTH (type));
433 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
436 gdb_assert (class[1] != AMD64_MEMORY);
437 gdb_assert (len <= 16);
439 for (i = 0; len > 0; i++, len -= 8)
447 /* 3. If the class is INTEGER, the next available register
448 of the sequence %rax, %rdx is used. */
449 regnum = integer_regnum[integer_reg++];
453 /* 4. If the class is SSE, the next available SSE register
454 of the sequence %xmm0, %xmm1 is used. */
455 regnum = sse_regnum[sse_reg++];
459 /* 5. If the class is SSEUP, the eightbyte is passed in the
460 upper half of the last used SSE register. */
461 gdb_assert (sse_reg > 0);
462 regnum = sse_regnum[sse_reg - 1];
467 /* 6. If the class is X87, the value is returned on the X87
468 stack in %st0 as 80-bit x87 number. */
469 regnum = AMD64_ST0_REGNUM;
471 i387_return_value (gdbarch, regcache);
475 /* 7. If the class is X87UP, the value is returned together
476 with the previous X87 value in %st0. */
477 gdb_assert (i > 0 && class[0] == AMD64_X87);
478 regnum = AMD64_ST0_REGNUM;
487 gdb_assert (!"Unexpected register class.");
490 gdb_assert (regnum != -1);
493 regcache_raw_read_part (regcache, regnum, offset, min (len, 8),
496 regcache_raw_write_part (regcache, regnum, offset, min (len, 8),
500 return RETURN_VALUE_REGISTER_CONVENTION;
505 amd64_push_arguments (struct regcache *regcache, int nargs,
506 struct value **args, CORE_ADDR sp, int struct_return)
508 static int integer_regnum[] =
510 AMD64_RDI_REGNUM, /* %rdi */
511 AMD64_RSI_REGNUM, /* %rsi */
512 AMD64_RDX_REGNUM, /* %rdx */
513 AMD64_RCX_REGNUM, /* %rcx */
517 static int sse_regnum[] =
519 /* %xmm0 ... %xmm7 */
520 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
521 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
522 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
523 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
525 struct value **stack_args = alloca (nargs * sizeof (struct value *));
526 int num_stack_args = 0;
527 int num_elements = 0;
533 /* Reserve a register for the "hidden" argument. */
537 for (i = 0; i < nargs; i++)
539 struct type *type = value_type (args[i]);
540 int len = TYPE_LENGTH (type);
541 enum amd64_reg_class class[2];
542 int needed_integer_regs = 0;
543 int needed_sse_regs = 0;
546 /* Classify argument. */
547 amd64_classify (type, class);
549 /* Calculate the number of integer and SSE registers needed for
551 for (j = 0; j < 2; j++)
553 if (class[j] == AMD64_INTEGER)
554 needed_integer_regs++;
555 else if (class[j] == AMD64_SSE)
559 /* Check whether enough registers are available, and if the
560 argument should be passed in registers at all. */
561 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
562 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
563 || (needed_integer_regs == 0 && needed_sse_regs == 0))
565 /* The argument will be passed on the stack. */
566 num_elements += ((len + 7) / 8);
567 stack_args[num_stack_args++] = args[i];
571 /* The argument will be passed in registers. */
572 const gdb_byte *valbuf = value_contents (args[i]);
575 gdb_assert (len <= 16);
577 for (j = 0; len > 0; j++, len -= 8)
585 regnum = integer_regnum[integer_reg++];
589 regnum = sse_regnum[sse_reg++];
593 gdb_assert (sse_reg > 0);
594 regnum = sse_regnum[sse_reg - 1];
599 gdb_assert (!"Unexpected register class.");
602 gdb_assert (regnum != -1);
603 memset (buf, 0, sizeof buf);
604 memcpy (buf, valbuf + j * 8, min (len, 8));
605 regcache_raw_write_part (regcache, regnum, offset, 8, buf);
610 /* Allocate space for the arguments on the stack. */
611 sp -= num_elements * 8;
613 /* The psABI says that "The end of the input argument area shall be
614 aligned on a 16 byte boundary." */
617 /* Write out the arguments to the stack. */
618 for (i = 0; i < num_stack_args; i++)
620 struct type *type = value_type (stack_args[i]);
621 const gdb_byte *valbuf = value_contents (stack_args[i]);
622 int len = TYPE_LENGTH (type);
624 write_memory (sp + element * 8, valbuf, len);
625 element += ((len + 7) / 8);
628 /* The psABI says that "For calls that may call functions that use
629 varargs or stdargs (prototype-less calls or calls to functions
630 containing ellipsis (...) in the declaration) %al is used as
631 hidden argument to specify the number of SSE registers used. */
632 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
637 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
638 struct regcache *regcache, CORE_ADDR bp_addr,
639 int nargs, struct value **args, CORE_ADDR sp,
640 int struct_return, CORE_ADDR struct_addr)
644 /* Pass arguments. */
645 sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
647 /* Pass "hidden" argument". */
650 store_unsigned_integer (buf, 8, struct_addr);
651 regcache_cooked_write (regcache, AMD64_RDI_REGNUM, buf);
654 /* Store return address. */
656 store_unsigned_integer (buf, 8, bp_addr);
657 write_memory (sp, buf, 8);
659 /* Finally, update the stack pointer... */
660 store_unsigned_integer (buf, 8, sp);
661 regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);
663 /* ...and fake a frame pointer. */
664 regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);
670 /* The maximum number of saved registers. This should include %rip. */
671 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
673 struct amd64_frame_cache
680 /* Saved registers. */
681 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
684 /* Do we have a frame? */
688 /* Initialize a frame cache. */
691 amd64_init_frame_cache (struct amd64_frame_cache *cache)
697 cache->sp_offset = -8;
700 /* Saved registers. We initialize these to -1 since zero is a valid
701 offset (that's where %rbp is supposed to be stored). */
702 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
703 cache->saved_regs[i] = -1;
706 /* Frameless until proven otherwise. */
707 cache->frameless_p = 1;
710 /* Allocate and initialize a frame cache. */
712 static struct amd64_frame_cache *
713 amd64_alloc_frame_cache (void)
715 struct amd64_frame_cache *cache;
717 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
718 amd64_init_frame_cache (cache);
722 /* Do a limited analysis of the prologue at PC and update CACHE
723 accordingly. Bail out early if CURRENT_PC is reached. Return the
724 address where the analysis stopped.
726 We will handle only functions beginning with:
729 movq %rsp, %rbp 0x48 0x89 0xe5
731 Any function that doesn't start with this sequence will be assumed
732 to have no prologue and thus no valid frame pointer in %rbp. */
735 amd64_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
736 struct amd64_frame_cache *cache)
738 static gdb_byte proto[3] = { 0x48, 0x89, 0xe5 }; /* movq %rsp, %rbp */
742 if (current_pc <= pc)
745 op = read_memory_unsigned_integer (pc, 1);
747 if (op == 0x55) /* pushq %rbp */
749 /* Take into account that we've executed the `pushq %rbp' that
750 starts this instruction sequence. */
751 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
752 cache->sp_offset += 8;
754 /* If that's all, return now. */
755 if (current_pc <= pc + 1)
758 /* Check for `movq %rsp, %rbp'. */
759 read_memory (pc + 1, buf, 3);
760 if (memcmp (buf, proto, 3) != 0)
763 /* OK, we actually have a frame. */
764 cache->frameless_p = 0;
771 /* Return PC of first real instruction. */
774 amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
776 struct amd64_frame_cache cache;
779 amd64_init_frame_cache (&cache);
780 pc = amd64_analyze_prologue (start_pc, 0xffffffffffffffffLL, &cache);
781 if (cache.frameless_p)
790 static struct amd64_frame_cache *
791 amd64_frame_cache (struct frame_info *next_frame, void **this_cache)
793 struct amd64_frame_cache *cache;
800 cache = amd64_alloc_frame_cache ();
803 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
805 amd64_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
807 if (cache->frameless_p)
809 /* We didn't find a valid frame. If we're at the start of a
810 function, or somewhere half-way its prologue, the function's
811 frame probably hasn't been fully setup yet. Try to
812 reconstruct the base address for the stack frame by looking
813 at the stack pointer. For truly "frameless" functions this
816 frame_unwind_register (next_frame, AMD64_RSP_REGNUM, buf);
817 cache->base = extract_unsigned_integer (buf, 8) + cache->sp_offset;
821 frame_unwind_register (next_frame, AMD64_RBP_REGNUM, buf);
822 cache->base = extract_unsigned_integer (buf, 8);
825 /* Now that we have the base address for the stack frame we can
826 calculate the value of %rsp in the calling frame. */
827 cache->saved_sp = cache->base + 16;
829 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
830 frame we find it at the same offset from the reconstructed base
832 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
834 /* Adjust all the saved registers such that they contain addresses
835 instead of offsets. */
836 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
837 if (cache->saved_regs[i] != -1)
838 cache->saved_regs[i] += cache->base;
844 amd64_frame_this_id (struct frame_info *next_frame, void **this_cache,
845 struct frame_id *this_id)
847 struct amd64_frame_cache *cache =
848 amd64_frame_cache (next_frame, this_cache);
850 /* This marks the outermost frame. */
851 if (cache->base == 0)
854 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
858 amd64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
859 int regnum, int *optimizedp,
860 enum lval_type *lvalp, CORE_ADDR *addrp,
861 int *realnump, gdb_byte *valuep)
863 struct gdbarch *gdbarch = get_frame_arch (next_frame);
864 struct amd64_frame_cache *cache =
865 amd64_frame_cache (next_frame, this_cache);
867 gdb_assert (regnum >= 0);
869 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
877 /* Store the value. */
878 store_unsigned_integer (valuep, 8, cache->saved_sp);
883 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
886 *lvalp = lval_memory;
887 *addrp = cache->saved_regs[regnum];
891 /* Read the value in from memory. */
892 read_memory (*addrp, valuep,
893 register_size (gdbarch, regnum));
899 *lvalp = lval_register;
903 frame_unwind_register (next_frame, (*realnump), valuep);
906 static const struct frame_unwind amd64_frame_unwind =
910 amd64_frame_prev_register
913 static const struct frame_unwind *
914 amd64_frame_sniffer (struct frame_info *next_frame)
916 return &amd64_frame_unwind;
920 /* Signal trampolines. */
922 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
923 64-bit variants. This would require using identical frame caches
924 on both platforms. */
926 static struct amd64_frame_cache *
927 amd64_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
929 struct amd64_frame_cache *cache;
930 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
938 cache = amd64_alloc_frame_cache ();
940 frame_unwind_register (next_frame, AMD64_RSP_REGNUM, buf);
941 cache->base = extract_unsigned_integer (buf, 8) - 8;
943 addr = tdep->sigcontext_addr (next_frame);
944 gdb_assert (tdep->sc_reg_offset);
945 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
946 for (i = 0; i < tdep->sc_num_regs; i++)
947 if (tdep->sc_reg_offset[i] != -1)
948 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
955 amd64_sigtramp_frame_this_id (struct frame_info *next_frame,
956 void **this_cache, struct frame_id *this_id)
958 struct amd64_frame_cache *cache =
959 amd64_sigtramp_frame_cache (next_frame, this_cache);
961 (*this_id) = frame_id_build (cache->base + 16, frame_pc_unwind (next_frame));
965 amd64_sigtramp_frame_prev_register (struct frame_info *next_frame,
967 int regnum, int *optimizedp,
968 enum lval_type *lvalp, CORE_ADDR *addrp,
969 int *realnump, gdb_byte *valuep)
971 /* Make sure we've initialized the cache. */
972 amd64_sigtramp_frame_cache (next_frame, this_cache);
974 amd64_frame_prev_register (next_frame, this_cache, regnum,
975 optimizedp, lvalp, addrp, realnump, valuep);
978 static const struct frame_unwind amd64_sigtramp_frame_unwind =
981 amd64_sigtramp_frame_this_id,
982 amd64_sigtramp_frame_prev_register
985 static const struct frame_unwind *
986 amd64_sigtramp_frame_sniffer (struct frame_info *next_frame)
988 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
990 /* We shouldn't even bother if we don't have a sigcontext_addr
992 if (tdep->sigcontext_addr == NULL)
995 if (tdep->sigtramp_p != NULL)
997 if (tdep->sigtramp_p (next_frame))
998 return &amd64_sigtramp_frame_unwind;
1001 if (tdep->sigtramp_start != 0)
1003 CORE_ADDR pc = frame_pc_unwind (next_frame);
1005 gdb_assert (tdep->sigtramp_end != 0);
1006 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
1007 return &amd64_sigtramp_frame_unwind;
1015 amd64_frame_base_address (struct frame_info *next_frame, void **this_cache)
1017 struct amd64_frame_cache *cache =
1018 amd64_frame_cache (next_frame, this_cache);
1023 static const struct frame_base amd64_frame_base =
1025 &amd64_frame_unwind,
1026 amd64_frame_base_address,
1027 amd64_frame_base_address,
1028 amd64_frame_base_address
1031 static struct frame_id
1032 amd64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1037 frame_unwind_register (next_frame, AMD64_RBP_REGNUM, buf);
1038 fp = extract_unsigned_integer (buf, 8);
1040 return frame_id_build (fp + 16, frame_pc_unwind (next_frame));
1043 /* 16 byte align the SP per frame requirements. */
1046 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1048 return sp & -(CORE_ADDR)16;
1052 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
1053 in the floating-point register set REGSET to register cache
1054 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
1057 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
1058 int regnum, const void *fpregs, size_t len)
1060 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
1062 gdb_assert (len == tdep->sizeof_fpregset);
1063 amd64_supply_fxsave (regcache, regnum, fpregs);
1066 /* Collect register REGNUM from the register cache REGCACHE and store
1067 it in the buffer specified by FPREGS and LEN as described by the
1068 floating-point register set REGSET. If REGNUM is -1, do this for
1069 all registers in REGSET. */
1072 amd64_collect_fpregset (const struct regset *regset,
1073 const struct regcache *regcache,
1074 int regnum, void *fpregs, size_t len)
1076 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
1078 gdb_assert (len == tdep->sizeof_fpregset);
1079 amd64_collect_fxsave (regcache, regnum, fpregs);
1082 /* Return the appropriate register set for the core section identified
1083 by SECT_NAME and SECT_SIZE. */
1085 static const struct regset *
1086 amd64_regset_from_core_section (struct gdbarch *gdbarch,
1087 const char *sect_name, size_t sect_size)
1089 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1091 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
1093 if (tdep->fpregset == NULL)
1094 tdep->fpregset = regset_alloc (gdbarch, amd64_supply_fpregset,
1095 amd64_collect_fpregset);
1097 return tdep->fpregset;
1100 return i386_regset_from_core_section (gdbarch, sect_name, sect_size);
1105 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1107 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1109 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
1110 floating-point registers. */
1111 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
1113 /* AMD64 has an FPU and 16 SSE registers. */
1114 tdep->st0_regnum = AMD64_ST0_REGNUM;
1115 tdep->num_xmm_regs = 16;
1117 /* This is what all the fuss is about. */
1118 set_gdbarch_long_bit (gdbarch, 64);
1119 set_gdbarch_long_long_bit (gdbarch, 64);
1120 set_gdbarch_ptr_bit (gdbarch, 64);
1122 /* In contrast to the i386, on AMD64 a `long double' actually takes
1123 up 128 bits, even though it's still based on the i387 extended
1124 floating-point format which has only 80 significant bits. */
1125 set_gdbarch_long_double_bit (gdbarch, 128);
1127 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
1128 set_gdbarch_register_name (gdbarch, amd64_register_name);
1129 set_gdbarch_register_type (gdbarch, amd64_register_type);
1131 /* Register numbers of various important registers. */
1132 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
1133 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
1134 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
1135 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
1137 /* The "default" register numbering scheme for AMD64 is referred to
1138 as the "DWARF Register Number Mapping" in the System V psABI.
1139 The preferred debugging format for all known AMD64 targets is
1140 actually DWARF2, and GCC doesn't seem to support DWARF (that is
1141 DWARF-1), but we provide the same mapping just in case. This
1142 mapping is also used for stabs, which GCC does support. */
1143 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1144 set_gdbarch_dwarf_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1145 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1147 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
1148 be in use on any of the supported AMD64 targets. */
1150 /* Call dummy code. */
1151 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
1152 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
1153 set_gdbarch_frame_red_zone_size (gdbarch, 128);
1155 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
1156 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
1157 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
1159 set_gdbarch_return_value (gdbarch, amd64_return_value);
1161 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
1163 /* Avoid wiring in the MMX registers for now. */
1164 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1165 tdep->mm0_regnum = -1;
1167 set_gdbarch_unwind_dummy_id (gdbarch, amd64_unwind_dummy_id);
1169 frame_unwind_append_sniffer (gdbarch, amd64_sigtramp_frame_sniffer);
1170 frame_unwind_append_sniffer (gdbarch, amd64_frame_sniffer);
1171 frame_base_set_default (gdbarch, &amd64_frame_base);
1173 /* If we have a register mapping, enable the generic core file support. */
1174 if (tdep->gregset_reg_offset)
1175 set_gdbarch_regset_from_core_section (gdbarch,
1176 amd64_regset_from_core_section);
1180 /* The 64-bit FXSAVE format differs from the 32-bit format in the
1181 sense that the instruction pointer and data pointer are simply
1182 64-bit offsets into the code segment and the data segment instead
1183 of a selector offset pair. The functions below store the upper 32
1184 bits of these pointers (instead of just the 16-bits of the segment
1187 /* Fill register REGNUM in REGCACHE with the appropriate
1188 floating-point or SSE register value from *FXSAVE. If REGNUM is
1189 -1, do this for all registers. This function masks off any of the
1190 reserved bits in *FXSAVE. */
1193 amd64_supply_fxsave (struct regcache *regcache, int regnum,
1196 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1197 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1199 i387_supply_fxsave (regcache, regnum, fxsave);
1201 if (fxsave && gdbarch_ptr_bit (gdbarch) == 64)
1203 const gdb_byte *regs = fxsave;
1205 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
1206 regcache_raw_supply (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
1207 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
1208 regcache_raw_supply (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);
1212 /* Fill register REGNUM (if it is a floating-point or SSE register) in
1213 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
1214 all registers. This function doesn't touch any of the reserved
1218 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
1221 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1222 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1223 gdb_byte *regs = fxsave;
1225 i387_collect_fxsave (regcache, regnum, fxsave);
1227 if (gdbarch_ptr_bit (gdbarch) == 64)
1229 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
1230 regcache_raw_collect (regcache, I387_FISEG_REGNUM (tdep), regs + 12);
1231 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
1232 regcache_raw_collect (regcache, I387_FOSEG_REGNUM (tdep), regs + 20);