1 /* Target-dependent code for the x86-64 for GDB, the GNU debugger.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
27 #include "arch-utils.h"
31 #include "x86-64-tdep.h"
32 #include "dwarf2cfi.h"
33 #include "gdb_assert.h"
35 /* Register numbers of various important registers. */
39 #define EFLAGS_REGNUM 17
41 #define XMM1_REGNUM 39
50 /* x86_64_register_raw_size_table[i] is the number of bytes of storage in
51 GDB's register array occupied by register i. */
52 static struct register_info x86_64_register_info_table[] = {
53 /* 0 */ {8, "rax", &builtin_type_int64},
54 /* 1 */ {8, "rbx", &builtin_type_int64},
55 /* 2 */ {8, "rcx", &builtin_type_int64},
56 /* 3 */ {8, "rdx", &builtin_type_int64},
57 /* 4 */ {8, "rsi", &builtin_type_int64},
58 /* 5 */ {8, "rdi", &builtin_type_int64},
59 /* 6 */ {8, "rbp", &builtin_type_void_func_ptr},
60 /* 7 */ {8, "rsp", &builtin_type_void_func_ptr},
61 /* 8 */ {8, "r8", &builtin_type_int64},
62 /* 9 */ {8, "r9", &builtin_type_int64},
63 /* 10 */ {8, "r10", &builtin_type_int64},
64 /* 11 */ {8, "r11", &builtin_type_int64},
65 /* 12 */ {8, "r12", &builtin_type_int64},
66 /* 13 */ {8, "r13", &builtin_type_int64},
67 /* 14 */ {8, "r14", &builtin_type_int64},
68 /* 15 */ {8, "r15", &builtin_type_int64},
69 /* 16 */ {8, "rip", &builtin_type_void_func_ptr},
70 /* 17 */ {4, "eflags", &builtin_type_int32},
71 /* 18 */ {4, "ds", &builtin_type_int32},
72 /* 19 */ {4, "es", &builtin_type_int32},
73 /* 20 */ {4, "fs", &builtin_type_int32},
74 /* 21 */ {4, "gs", &builtin_type_int32},
75 /* 22 */ {10, "st0", &builtin_type_i387_ext},
76 /* 23 */ {10, "st1", &builtin_type_i387_ext},
77 /* 24 */ {10, "st2", &builtin_type_i387_ext},
78 /* 25 */ {10, "st3", &builtin_type_i387_ext},
79 /* 26 */ {10, "st4", &builtin_type_i387_ext},
80 /* 27 */ {10, "st5", &builtin_type_i387_ext},
81 /* 28 */ {10, "st6", &builtin_type_i387_ext},
82 /* 29 */ {10, "st7", &builtin_type_i387_ext},
83 /* 30 */ {4, "fctrl", &builtin_type_int32},
84 /* 31 */ {4, "fstat", &builtin_type_int32},
85 /* 32 */ {4, "ftag", &builtin_type_int32},
86 /* 33 */ {4, "fiseg", &builtin_type_int32},
87 /* 34 */ {4, "fioff", &builtin_type_int32},
88 /* 35 */ {4, "foseg", &builtin_type_int32},
89 /* 36 */ {4, "fooff", &builtin_type_int32},
90 /* 37 */ {4, "fop", &builtin_type_int32},
91 /* 38 */ {16, "xmm0", &builtin_type_v4sf},
92 /* 39 */ {16, "xmm1", &builtin_type_v4sf},
93 /* 40 */ {16, "xmm2", &builtin_type_v4sf},
94 /* 41 */ {16, "xmm3", &builtin_type_v4sf},
95 /* 42 */ {16, "xmm4", &builtin_type_v4sf},
96 /* 43 */ {16, "xmm5", &builtin_type_v4sf},
97 /* 44 */ {16, "xmm6", &builtin_type_v4sf},
98 /* 45 */ {16, "xmm7", &builtin_type_v4sf},
99 /* 46 */ {16, "xmm8", &builtin_type_v4sf},
100 /* 47 */ {16, "xmm9", &builtin_type_v4sf},
101 /* 48 */ {16, "xmm10", &builtin_type_v4sf},
102 /* 49 */ {16, "xmm11", &builtin_type_v4sf},
103 /* 50 */ {16, "xmm12", &builtin_type_v4sf},
104 /* 51 */ {16, "xmm13", &builtin_type_v4sf},
105 /* 52 */ {16, "xmm14", &builtin_type_v4sf},
106 /* 53 */ {16, "xmm15", &builtin_type_v4sf},
107 /* 54 */ {4, "mxcsr", &builtin_type_int32}
110 /* This array is a mapping from Dwarf-2 register
111 numbering to GDB's one. Dwarf-2 numbering is
112 defined in x86-64 ABI, section 3.6. */
113 static int x86_64_dwarf2gdb_regno_map[] = {
114 0, 1, 2, 3, /* RAX - RDX */
115 4, 5, 6, 7, /* RSI, RDI, RBP, RSP */
116 8, 9, 10, 11, /* R8 - R11 */
117 12, 13, 14, 15, /* R12 - R15 */
118 -1, /* RA - not mapped */
119 XMM1_REGNUM - 1, XMM1_REGNUM, /* XMM0 ... */
120 XMM1_REGNUM + 1, XMM1_REGNUM + 2,
121 XMM1_REGNUM + 3, XMM1_REGNUM + 4,
122 XMM1_REGNUM + 5, XMM1_REGNUM + 6,
123 XMM1_REGNUM + 7, XMM1_REGNUM + 8,
124 XMM1_REGNUM + 9, XMM1_REGNUM + 10,
125 XMM1_REGNUM + 11, XMM1_REGNUM + 12,
126 XMM1_REGNUM + 13, XMM1_REGNUM + 14, /* ... XMM15 */
127 ST0_REGNUM + 0, ST0_REGNUM + 1, /* ST0 ... */
128 ST0_REGNUM + 2, ST0_REGNUM + 3,
129 ST0_REGNUM + 4, ST0_REGNUM + 5,
130 ST0_REGNUM + 6, ST0_REGNUM + 7 /* ... ST7 */
133 static int x86_64_dwarf2gdb_regno_map_length =
134 sizeof (x86_64_dwarf2gdb_regno_map) /
135 sizeof (x86_64_dwarf2gdb_regno_map[0]);
137 /* Number of all registers */
138 #define X86_64_NUM_REGS (sizeof (x86_64_register_info_table) / \
139 sizeof (x86_64_register_info_table[0]))
141 /* Number of general registers. */
142 #define X86_64_NUM_GREGS (22)
144 int x86_64_num_regs = X86_64_NUM_REGS;
145 int x86_64_num_gregs = X86_64_NUM_GREGS;
147 /* Did we already print a note about frame pointer? */
148 int omit_fp_note_printed = 0;
150 /* Number of bytes of storage in the actual machine representation for
153 x86_64_register_raw_size (int regno)
155 return x86_64_register_info_table[regno].size;
158 /* x86_64_register_byte_table[i] is the offset into the register file of the
159 start of register number i. We initialize this from
160 x86_64_register_info_table. */
161 int x86_64_register_byte_table[X86_64_NUM_REGS];
163 /* Index within `registers' of the first byte of the space for register REGNO. */
165 x86_64_register_byte (int regno)
167 return x86_64_register_byte_table[regno];
170 /* Return the GDB type object for the "standard" data type of data in
173 x86_64_register_virtual_type (int regno)
175 return *x86_64_register_info_table[regno].type;
178 /* FIXME: cagney/2002-11-11: Once the i386 and x86-64 targets are
179 merged, this function can go away. */
181 i386_fp_regnum_p (int regnum)
183 return (regnum < NUM_REGS
184 && (FP0_REGNUM && FP0_REGNUM <= (regnum) && (regnum) < FPC_REGNUM));
187 /* x86_64_register_convertible is true if register N's virtual format is
188 different from its raw format. Note that this definition assumes
189 that the host supports IEEE 32-bit floats, since it doesn't say
190 that SSE registers need conversion. Even if we can't find a
191 counterexample, this is still sloppy. */
193 x86_64_register_convertible (int regno)
195 return IS_FP_REGNUM (regno);
198 /* Convert data from raw format for register REGNUM in buffer FROM to
199 virtual format with type TYPE in buffer TO. In principle both
200 formats are identical except that the virtual format has two extra
201 bytes appended that aren't used. We set these to zero. */
203 x86_64_register_convert_to_virtual (int regnum, struct type *type,
204 char *from, char *to)
208 /* We only support floating-point values. */
209 if (TYPE_CODE (type) != TYPE_CODE_FLT)
211 warning ("Cannot convert floating-point register value "
212 "to non-floating-point type.");
213 memset (to, 0, TYPE_LENGTH (type));
216 /* First add the necessary padding. */
217 memcpy (buf, from, FPU_REG_RAW_SIZE);
218 memset (buf + FPU_REG_RAW_SIZE, 0, sizeof buf - FPU_REG_RAW_SIZE);
219 /* Convert to TYPE. This should be a no-op, if TYPE is equivalent
220 to the extended floating-point format used by the FPU. */
221 convert_typed_floating (to, type, buf,
222 x86_64_register_virtual_type (regnum));
225 /* Convert data from virtual format with type TYPE in buffer FROM to
226 raw format for register REGNUM in buffer TO. Simply omit the two
230 x86_64_register_convert_to_raw (struct type *type, int regnum,
231 char *from, char *to)
233 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12);
234 /* Simply omit the two unused bytes. */
235 memcpy (to, from, FPU_REG_RAW_SIZE);
238 /* Dwarf-2 <-> GDB register numbers mapping. */
240 x86_64_dwarf2_reg_to_regnum (int dw_reg)
242 if (dw_reg < 0 || dw_reg > x86_64_dwarf2gdb_regno_map_length)
244 warning ("Dwarf-2 uses unmapped register #%d\n", dw_reg);
248 return x86_64_dwarf2gdb_regno_map[dw_reg];
251 /* This is the variable that is set with "set disassembly-flavour", and
252 its legitimate values. */
253 static const char att_flavour[] = "att";
254 static const char intel_flavour[] = "intel";
255 static const char *valid_flavours[] = {
260 static const char *disassembly_flavour = att_flavour;
262 /* Push the return address (pointing to the call dummy) onto the stack
263 and return the new value for the stack pointer. */
266 x86_64_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
270 store_unsigned_integer (buf, 8, CALL_DUMMY_ADDRESS ());
271 write_memory (sp - 8, buf, 8);
276 x86_64_pop_frame (void)
278 generic_pop_current_frame (cfi_pop_frame);
282 /* The returning of values is done according to the special algorithm.
283 Some types are returned in registers an some (big structures) in memory.
287 #define MAX_CLASSES 4
289 enum x86_64_reg_class
292 X86_64_INTEGER_CLASS,
293 X86_64_INTEGERSI_CLASS,
303 /* Return the union class of CLASS1 and CLASS2.
304 See the x86-64 ABI for details. */
306 static enum x86_64_reg_class
307 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
309 /* Rule #1: If both classes are equal, this is the resulting class. */
310 if (class1 == class2)
313 /* Rule #2: If one of the classes is NO_CLASS, the resulting class
314 is the other class. */
315 if (class1 == X86_64_NO_CLASS)
317 if (class2 == X86_64_NO_CLASS)
320 /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */
321 if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
322 return X86_64_MEMORY_CLASS;
324 /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */
325 if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
326 || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
327 return X86_64_INTEGERSI_CLASS;
328 if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
329 || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
330 return X86_64_INTEGER_CLASS;
332 /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used. */
333 if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
334 || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
335 return X86_64_MEMORY_CLASS;
337 /* Rule #6: Otherwise class SSE is used. */
338 return X86_64_SSE_CLASS;
341 /* Classify the argument type. CLASSES will be filled by the register
342 class used to pass each word of the operand. The number of words
343 is returned. In case the parameter should be passed in memory, 0
344 is returned. As a special case for zero sized containers,
345 classes[0] will be NO_CLASS and 1 is returned.
347 See the x86-64 psABI for details. */
350 classify_argument (struct type *type,
351 enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
353 int bytes = TYPE_LENGTH (type);
354 int words = (bytes + 8 - 1) / 8;
356 switch (TYPE_CODE (type))
358 case TYPE_CODE_ARRAY:
359 case TYPE_CODE_STRUCT:
360 case TYPE_CODE_UNION:
363 enum x86_64_reg_class subclasses[MAX_CLASSES];
365 /* On x86-64 we pass structures larger than 16 bytes on the stack. */
369 for (i = 0; i < words; i++)
370 classes[i] = X86_64_NO_CLASS;
372 /* Zero sized arrays or structures are NO_CLASS. We return 0
373 to signalize memory class, so handle it as special case. */
376 classes[0] = X86_64_NO_CLASS;
379 switch (TYPE_CODE (type))
381 case TYPE_CODE_STRUCT:
384 for (j = 0; j < TYPE_NFIELDS (type); ++j)
386 int num = classify_argument (TYPE_FIELDS (type)[j].type,
388 (TYPE_FIELDS (type)[j].loc.
389 bitpos + bit_offset) % 256);
392 for (i = 0; i < num; i++)
395 (TYPE_FIELDS (type)[j].loc.bitpos +
398 merge_classes (subclasses[i], classes[i + pos]);
403 case TYPE_CODE_ARRAY:
407 num = classify_argument (TYPE_TARGET_TYPE (type),
408 subclasses, bit_offset);
412 /* The partial classes are now full classes. */
413 if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
414 subclasses[0] = X86_64_SSE_CLASS;
415 if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
416 subclasses[0] = X86_64_INTEGER_CLASS;
418 for (i = 0; i < words; i++)
419 classes[i] = subclasses[i % num];
422 case TYPE_CODE_UNION:
426 for (j = 0; j < TYPE_NFIELDS (type); ++j)
429 num = classify_argument (TYPE_FIELDS (type)[j].type,
430 subclasses, bit_offset);
433 for (i = 0; i < num; i++)
434 classes[i] = merge_classes (subclasses[i], classes[i]);
442 /* Final merger cleanup. */
443 for (i = 0; i < words; i++)
445 /* If one class is MEMORY, everything should be passed in
447 if (classes[i] == X86_64_MEMORY_CLASS)
450 /* The X86_64_SSEUP_CLASS should be always preceeded by
452 if (classes[i] == X86_64_SSEUP_CLASS
453 && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
454 classes[i] = X86_64_SSE_CLASS;
456 /* X86_64_X87UP_CLASS should be preceeded by X86_64_X87_CLASS. */
457 if (classes[i] == X86_64_X87UP_CLASS
458 && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
459 classes[i] = X86_64_SSE_CLASS;
468 if (!(bit_offset % 64))
469 classes[0] = X86_64_SSESF_CLASS;
471 classes[0] = X86_64_SSE_CLASS;
474 classes[0] = X86_64_SSEDF_CLASS;
477 classes[0] = X86_64_X87_CLASS;
478 classes[1] = X86_64_X87UP_CLASS;
490 if (bytes * 8 + bit_offset <= 32)
491 classes[0] = X86_64_INTEGERSI_CLASS;
493 classes[0] = X86_64_INTEGER_CLASS;
496 classes[0] = classes[1] = X86_64_INTEGER_CLASS;
503 default: /* Avoid warning. */
506 internal_error (__FILE__, __LINE__,
507 "classify_argument: unknown argument type");
510 /* Examine the argument and set *INT_NREGS and *SSE_NREGS to the
511 number of registers required based on the information passed in
512 CLASSES. Return 0 if parameter should be passed in memory. */
515 examine_argument (enum x86_64_reg_class classes[MAX_CLASSES],
516 int n, int *int_nregs, int *sse_nregs)
522 for (n--; n >= 0; n--)
525 case X86_64_INTEGER_CLASS:
526 case X86_64_INTEGERSI_CLASS:
529 case X86_64_SSE_CLASS:
530 case X86_64_SSESF_CLASS:
531 case X86_64_SSEDF_CLASS:
534 case X86_64_NO_CLASS:
535 case X86_64_SSEUP_CLASS:
536 case X86_64_X87_CLASS:
537 case X86_64_X87UP_CLASS:
539 case X86_64_MEMORY_CLASS:
540 internal_error (__FILE__, __LINE__,
541 "examine_argument: unexpected memory class");
546 #define RET_INT_REGS 2
547 #define RET_SSE_REGS 2
549 /* Check if the structure in value_type is returned in registers or in
550 memory. If this function returns 1, GDB will call
551 STORE_STRUCT_RETURN and EXTRACT_STRUCT_VALUE_ADDRESS else
552 STORE_RETURN_VALUE and EXTRACT_RETURN_VALUE will be used. */
554 x86_64_use_struct_convention (int gcc_p, struct type *value_type)
556 enum x86_64_reg_class class[MAX_CLASSES];
557 int n = classify_argument (value_type, class, 0);
562 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
563 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS);
566 /* Extract from an array REGBUF containing the (raw) register state, a
567 function return value of TYPE, and copy that, in virtual format,
571 x86_64_extract_return_value (struct type *type, char *regbuf, char *valbuf)
573 enum x86_64_reg_class class[MAX_CLASSES];
574 int n = classify_argument (type, class, 0);
580 int ret_int_r[RET_INT_REGS] = { RAX_REGNUM, RDX_REGNUM };
581 int ret_sse_r[RET_SSE_REGS] = { XMM0_REGNUM, XMM1_REGNUM };
584 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
585 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS)
588 memcpy (&addr, regbuf, REGISTER_RAW_SIZE (RAX_REGNUM));
589 read_memory (addr, valbuf, TYPE_LENGTH (type));
595 for (i = 0; i < n; i++)
599 case X86_64_NO_CLASS:
601 case X86_64_INTEGER_CLASS:
602 memcpy (valbuf + offset,
603 regbuf + REGISTER_BYTE (ret_int_r[(intreg + 1) / 2]),
608 case X86_64_INTEGERSI_CLASS:
609 memcpy (valbuf + offset,
610 regbuf + REGISTER_BYTE (ret_int_r[intreg / 2]), 4);
614 case X86_64_SSEDF_CLASS:
615 case X86_64_SSESF_CLASS:
616 case X86_64_SSE_CLASS:
617 memcpy (valbuf + offset,
618 regbuf + REGISTER_BYTE (ret_sse_r[(ssereg + 1) / 2]),
623 case X86_64_SSEUP_CLASS:
624 memcpy (valbuf + offset + 8,
625 regbuf + REGISTER_BYTE (ret_sse_r[ssereg / 2]), 8);
629 case X86_64_X87_CLASS:
630 memcpy (valbuf + offset, regbuf + REGISTER_BYTE (FP0_REGNUM),
634 case X86_64_X87UP_CLASS:
635 memcpy (valbuf + offset,
636 regbuf + REGISTER_BYTE (FP0_REGNUM) + 8, 8);
639 case X86_64_MEMORY_CLASS:
641 internal_error (__FILE__, __LINE__,
642 "Unexpected argument class");
649 x86_64_frame_init_saved_regs (struct frame_info *fi)
651 /* Do nothing. Everything is handled by the stack unwinding code. */
658 x86_64_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
659 int struct_return, CORE_ADDR struct_addr)
664 static int int_parameter_registers[INT_REGS] = {
665 5 /* RDI */ , 4 /* RSI */ ,
666 3 /* RDX */ , 2 /* RCX */ ,
667 8 /* R8 */ , 9 /* R9 */
670 static int sse_parameter_registers[SSE_REGS] = {
671 XMM1_REGNUM - 1, XMM1_REGNUM, XMM1_REGNUM + 1, XMM1_REGNUM + 2,
672 XMM1_REGNUM + 3, XMM1_REGNUM + 4, XMM1_REGNUM + 5, XMM1_REGNUM + 6,
673 XMM1_REGNUM + 7, XMM1_REGNUM + 8, XMM1_REGNUM + 9, XMM1_REGNUM + 10,
674 XMM1_REGNUM + 11, XMM1_REGNUM + 12, XMM1_REGNUM + 13, XMM1_REGNUM + 14
676 int stack_values_count = 0;
678 stack_values = alloca (nargs * sizeof (int));
679 for (i = 0; i < nargs; i++)
681 enum x86_64_reg_class class[MAX_CLASSES];
682 int n = classify_argument (args[i]->type, class, 0);
687 !examine_argument (class, n, &needed_intregs, &needed_sseregs)
688 || intreg / 2 + needed_intregs > INT_REGS
689 || ssereg / 2 + needed_sseregs > SSE_REGS)
691 stack_values[stack_values_count++] = i;
696 for (j = 0; j < n; j++)
701 case X86_64_NO_CLASS:
703 case X86_64_INTEGER_CLASS:
704 deprecated_write_register_gen (int_parameter_registers
706 VALUE_CONTENTS_ALL (args[i]) + offset);
710 case X86_64_INTEGERSI_CLASS:
711 deprecated_write_register_gen (int_parameter_registers[intreg / 2],
712 VALUE_CONTENTS_ALL (args[i]) + offset);
716 case X86_64_SSEDF_CLASS:
717 case X86_64_SSESF_CLASS:
718 case X86_64_SSE_CLASS:
719 deprecated_write_register_gen (sse_parameter_registers
721 VALUE_CONTENTS_ALL (args[i]) + offset);
725 case X86_64_SSEUP_CLASS:
726 deprecated_write_register_gen (sse_parameter_registers[ssereg / 2],
727 VALUE_CONTENTS_ALL (args[i]) + offset);
731 case X86_64_X87_CLASS:
732 case X86_64_MEMORY_CLASS:
733 stack_values[stack_values_count++] = i;
735 case X86_64_X87UP_CLASS:
738 internal_error (__FILE__, __LINE__,
739 "Unexpected argument class");
741 intreg += intreg % 2;
742 ssereg += ssereg % 2;
746 while (--stack_values_count >= 0)
748 struct value *arg = args[stack_values[stack_values_count]];
749 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
753 write_memory (sp, VALUE_CONTENTS_ALL (arg), len);
758 /* Write into the appropriate registers a function return value stored
759 in VALBUF of type TYPE, given in virtual format. */
761 x86_64_store_return_value (struct type *type, char *valbuf)
763 int len = TYPE_LENGTH (type);
765 if (TYPE_CODE_FLT == TYPE_CODE (type))
767 /* Floating-point return values can be found in %st(0). */
768 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
769 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
771 /* Copy straight over. */
772 deprecated_write_register_bytes (REGISTER_BYTE (FP0_REGNUM), valbuf,
777 char buf[FPU_REG_RAW_SIZE];
780 /* Convert the value found in VALBUF to the extended
781 floating point format used by the FPU. This is probably
782 not exactly how it would happen on the target itself, but
783 it is the best we can do. */
784 val = extract_floating (valbuf, TYPE_LENGTH (type));
785 floatformat_from_doublest (&floatformat_i387_ext, &val, buf);
786 deprecated_write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
792 int low_size = REGISTER_RAW_SIZE (0);
793 int high_size = REGISTER_RAW_SIZE (1);
796 deprecated_write_register_bytes (REGISTER_BYTE (0), valbuf, len);
797 else if (len <= (low_size + high_size))
799 deprecated_write_register_bytes (REGISTER_BYTE (0), valbuf,
801 deprecated_write_register_bytes (REGISTER_BYTE (1),
802 valbuf + low_size, len - low_size);
805 internal_error (__FILE__, __LINE__,
806 "Cannot store return value of %d bytes long.", len);
812 x86_64_register_name (int reg_nr)
814 if (reg_nr < 0 || reg_nr >= X86_64_NUM_REGS)
816 return x86_64_register_info_table[reg_nr].name;
820 x86_64_register_number (const char *name)
824 for (reg_nr = 0; reg_nr < X86_64_NUM_REGS; reg_nr++)
825 if (strcmp (name, x86_64_register_info_table[reg_nr].name) == 0)
832 /* We have two flavours of disassembly. The machinery on this page
833 deals with switching between those. */
836 gdb_print_insn_x86_64 (bfd_vma memaddr, disassemble_info * info)
838 if (disassembly_flavour == att_flavour)
839 return print_insn_i386_att (memaddr, info);
840 else if (disassembly_flavour == intel_flavour)
841 return print_insn_i386_intel (memaddr, info);
842 /* Never reached -- disassembly_flavour is always either att_flavour
844 internal_error (__FILE__, __LINE__, "failed internal consistency check");
848 /* Store the address of the place in which to copy the structure the
849 subroutine will return. This is called from call_function. */
851 x86_64_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
853 write_register (RDI_REGNUM, addr);
857 x86_64_frameless_function_invocation (struct frame_info *frame)
862 /* If a function with debugging information and known beginning
863 is detected, we will return pc of the next line in the source
864 code. With this approach we effectively skip the prolog. */
866 #define PROLOG_BUFSIZE 4
868 x86_64_skip_prologue (CORE_ADDR pc)
871 struct symtab_and_line v_sal;
872 struct symbol *v_function;
874 unsigned char prolog_buf[PROLOG_BUFSIZE];
876 /* We will handle only functions starting with: */
877 static unsigned char prolog_expect[PROLOG_BUFSIZE] =
879 0x55, /* pushq %rbp */
880 0x48, 0x89, 0xe5 /* movq %rsp, %rbp */
883 read_memory (pc, (char *) prolog_buf, PROLOG_BUFSIZE);
885 /* First check, whether pc points to pushq %rbp, movq %rsp, %rbp. */
886 for (i = 0; i < PROLOG_BUFSIZE; i++)
887 if (prolog_expect[i] != prolog_buf[i])
888 return pc; /* ... no, it doesn't. Nothing to skip. */
890 /* OK, we have found the prologue and want PC of the first
891 non-prologue instruction. */
892 pc += PROLOG_BUFSIZE;
894 v_function = find_pc_function (pc);
895 v_sal = find_pc_line (pc, 0);
897 /* If pc doesn't point to a function with debuginfo, some of the
898 following may be NULL. */
899 if (!v_function || !v_function->ginfo.value.block || !v_sal.symtab)
902 endaddr = BLOCK_END (SYMBOL_BLOCK_VALUE (v_function));
904 for (i = 0; i < v_sal.symtab->linetable->nitems; i++)
905 if (v_sal.symtab->linetable->item[i].pc >= pc
906 && v_sal.symtab->linetable->item[i].pc < endaddr)
908 pc = v_sal.symtab->linetable->item[i].pc;
915 /* Sequence of bytes for breakpoint instruction. */
916 static const unsigned char *
917 x86_64_breakpoint_from_pc (CORE_ADDR *pc, int *lenptr)
919 static unsigned char breakpoint[] = { 0xcc };
925 x86_64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
927 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
930 /* The x86-64 has 16 SSE registers. */
931 tdep->num_xmm_regs = 16;
933 /* This is what all the fuss is about. */
934 set_gdbarch_long_bit (gdbarch, 64);
935 set_gdbarch_long_long_bit (gdbarch, 64);
936 set_gdbarch_ptr_bit (gdbarch, 64);
938 /* In contrast to the i386, on the x86-64 a `long double' actually
939 takes up 128 bits, even though it's still based on the i387
940 extended floating-point format which has only 80 significant bits. */
941 set_gdbarch_long_double_bit (gdbarch, 128);
943 set_gdbarch_num_regs (gdbarch, X86_64_NUM_REGS);
945 /* Register numbers of various important registers. */
946 set_gdbarch_sp_regnum (gdbarch, 7); /* %rsp */
947 set_gdbarch_fp_regnum (gdbarch, 6); /* %rbp */
948 set_gdbarch_pc_regnum (gdbarch, 16); /* %rip */
949 set_gdbarch_ps_regnum (gdbarch, 17); /* %eflags */
950 set_gdbarch_fp0_regnum (gdbarch, X86_64_NUM_GREGS); /* %st(0) */
952 /* The "default" register numbering scheme for the x86-64 is
953 referred to as the "DWARF register number mapping" in the psABI.
954 The preferred debugging format for all known x86-64 targets is
955 actually DWARF2, and GCC doesn't seem to support DWARF (that is
956 DWARF-1), but we provide the same mapping just in case. This
957 mapping is also used for stabs, which GCC does support. */
958 set_gdbarch_stab_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
959 set_gdbarch_dwarf_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
960 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
962 /* We don't override SDB_REG_RO_REGNUM, sice COFF doesn't seem to be
963 in use on any of the supported x86-64 targets. */
965 set_gdbarch_register_name (gdbarch, x86_64_register_name);
966 set_gdbarch_register_size (gdbarch, 8);
968 /* Total amount of space needed to store our copies of the machine's
969 register (SIZEOF_GREGS + SIZEOF_FPU_REGS + SIZEOF_FPU_CTRL_REGS +
971 for (i = 0, sum = 0; i < X86_64_NUM_REGS; i++)
972 sum += x86_64_register_info_table[i].size;
973 set_gdbarch_register_bytes (gdbarch, sum);
975 set_gdbarch_register_raw_size (gdbarch, x86_64_register_raw_size);
976 set_gdbarch_register_byte (gdbarch, x86_64_register_byte);
977 set_gdbarch_register_virtual_type (gdbarch, x86_64_register_virtual_type);
979 /* FIXME: kettenis/20021026: As long as we don't support longjmp,
980 that is, as long as we have `tdep->jb_pc_offset == -1', using
981 i386_get_longjmp_target is fine. */
983 set_gdbarch_register_convertible (gdbarch, x86_64_register_convertible);
984 set_gdbarch_register_convert_to_virtual (gdbarch,
985 x86_64_register_convert_to_virtual);
986 set_gdbarch_register_convert_to_raw (gdbarch,
987 x86_64_register_convert_to_raw);
989 /* Getting saved registers is handled by unwind information. */
990 set_gdbarch_get_saved_register (gdbarch, cfi_get_saved_register);
992 /* FIXME: kettenis/20021026: Should we set parm_boundary to 64 here? */
993 set_gdbarch_read_fp (gdbarch, cfi_read_fp);
995 /* FIXME: kettenis/20021026: Should be undeprecated. */
996 set_gdbarch_extract_return_value (gdbarch, NULL);
997 set_gdbarch_deprecated_extract_return_value (gdbarch,
998 x86_64_extract_return_value);
999 set_gdbarch_push_arguments (gdbarch, x86_64_push_arguments);
1000 set_gdbarch_push_return_address (gdbarch, x86_64_push_return_address);
1001 set_gdbarch_pop_frame (gdbarch, x86_64_pop_frame);
1002 set_gdbarch_store_struct_return (gdbarch, x86_64_store_struct_return);
1003 /* FIXME: kettenis/20021026: Should be undeprecated. */
1004 set_gdbarch_store_return_value (gdbarch, NULL);
1005 set_gdbarch_deprecated_store_return_value (gdbarch,
1006 x86_64_store_return_value);
1007 /* Override, since this is handled by x86_64_extract_return_value. */
1008 set_gdbarch_extract_struct_value_address (gdbarch, NULL);
1009 set_gdbarch_use_struct_convention (gdbarch, x86_64_use_struct_convention);
1011 set_gdbarch_frame_init_saved_regs (gdbarch, x86_64_frame_init_saved_regs);
1012 set_gdbarch_skip_prologue (gdbarch, x86_64_skip_prologue);
1014 set_gdbarch_frame_chain (gdbarch, x86_64_linux_frame_chain);
1015 set_gdbarch_frameless_function_invocation (gdbarch,
1016 x86_64_frameless_function_invocation);
1017 /* FIXME: kettenis/20021025: Shouldn't this be set to
1018 generic_file_frame_chain_valid? */
1019 set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
1020 /* FIXME: kettenis/20021026: These two are GNU/Linux-specific and
1021 should be moved elsewhere. */
1022 set_gdbarch_frame_saved_pc (gdbarch, x86_64_linux_frame_saved_pc);
1023 set_gdbarch_saved_pc_after_call (gdbarch, x86_64_linux_saved_pc_after_call);
1024 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1025 /* FIXME: kettenis/20021026: This one is GNU/Linux-specific too. */
1026 set_gdbarch_pc_in_sigtramp (gdbarch, x86_64_linux_in_sigtramp);
1028 /* Build call frame information (CFI) from DWARF2 frame debug info. */
1029 set_gdbarch_dwarf2_build_frame_info (gdbarch, dwarf2_build_frame_info);
1031 /* Initialization of per-frame CFI. */
1032 set_gdbarch_init_extra_frame_info (gdbarch, cfi_init_extra_frame_info);
1034 /* Frame PC initialization is handled by using CFI. */
1035 set_gdbarch_deprecated_init_frame_pc (gdbarch, x86_64_init_frame_pc);
1037 /* Cons up virtual frame pointer for trace. */
1038 set_gdbarch_virtual_frame_pointer (gdbarch, cfi_virtual_frame_pointer);
1040 /* FIXME: kettenis/20021026: This is ELF-specific. Fine for now,
1041 since all supported x86-64 targets are ELF, but that might change
1043 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1046 static struct gdbarch *
1047 x86_64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1049 struct gdbarch_tdep *tdep;
1050 struct gdbarch *gdbarch;
1051 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
1053 /* Try to determine the OS ABI of the object we're loading. */
1054 if (info.abfd != NULL)
1055 osabi = gdbarch_lookup_osabi (info.abfd);
1057 /* Find a candidate among extant architectures. */
1058 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1060 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1062 /* Make sure the OS ABI selection matches. */
1063 tdep = gdbarch_tdep (arches->gdbarch);
1064 if (tdep && tdep->osabi == osabi)
1065 return arches->gdbarch;
1068 /* Allocate space for the new architecture. */
1069 tdep = XMALLOC (struct gdbarch_tdep);
1070 gdbarch = gdbarch_alloc (&info, tdep);
1072 tdep->osabi = osabi;
1074 /* FIXME: kettenis/20021025: The following calls are going to
1075 disappear when we integrate the x86_64 target into the i386
1078 set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
1080 set_gdbarch_max_register_raw_size (gdbarch, 16);
1081 set_gdbarch_max_register_virtual_size (gdbarch, 16);
1083 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1085 set_gdbarch_breakpoint_from_pc (gdbarch, x86_64_breakpoint_from_pc);
1086 set_gdbarch_decr_pc_after_break (gdbarch, 1);
1087 set_gdbarch_function_start_offset (gdbarch, 0);
1089 set_gdbarch_frame_args_skip (gdbarch, 8);
1091 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1092 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1093 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1094 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1095 set_gdbarch_call_dummy_length (gdbarch, 0);
1096 set_gdbarch_call_dummy_p (gdbarch, 1);
1097 set_gdbarch_call_dummy_words (gdbarch, NULL);
1098 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1099 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1100 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1102 set_gdbarch_deprecated_pc_in_call_dummy (gdbarch, deprecated_pc_in_call_dummy_at_entry_point);
1104 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1106 /* FIXME: kettenis/20021025: These already are the default. */
1108 set_gdbarch_register_virtual_size (gdbarch, generic_register_size);
1109 set_gdbarch_deprecated_extract_struct_value_address (gdbarch, 0);
1111 x86_64_init_abi (info, gdbarch);
1117 _initialize_x86_64_tdep (void)
1119 register_gdbarch_init (bfd_arch_i386, x86_64_gdbarch_init);
1121 /* Initialize the table saying where each register starts in the
1127 for (i = 0; i < X86_64_NUM_REGS; i++)
1129 x86_64_register_byte_table[i] = offset;
1130 offset += x86_64_register_info_table[i].size;
1134 tm_print_insn = gdb_print_insn_x86_64;
1135 tm_print_insn_info.mach = bfd_mach_x86_64;
1137 /* Add the variable that controls the disassembly flavour. */
1139 struct cmd_list_element *new_cmd;
1141 new_cmd = add_set_enum_cmd ("disassembly-flavour", no_class,
1142 valid_flavours, &disassembly_flavour, "\
1143 Set the disassembly flavour, the valid values are \"att\" and \"intel\", \
1144 and the default value is \"att\".", &setlist);
1145 add_show_from_set (new_cmd, &showlist);