2003-04-28 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / x86-64-tdep.c
1 /* Target-dependent code for the x86-64 for GDB, the GNU debugger.
2
3 Copyright 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Jiri Smid, SuSE Labs.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 #include "defs.h"
24 #include "inferior.h"
25 #include "gdbcore.h"
26 #include "gdbcmd.h"
27 #include "arch-utils.h"
28 #include "regcache.h"
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "x86-64-tdep.h"
32 #include "dwarf2cfi.h"
33 #include "gdb_assert.h"
34 #include "block.h"
35
36 /* Register numbers of various important registers. */
37 #define RAX_REGNUM 0
38 #define RDX_REGNUM 3
39 #define RDI_REGNUM 5
40 #define EFLAGS_REGNUM 17
41 #define ST0_REGNUM 22
42 #define XMM1_REGNUM 39
43
44 struct register_info
45 {
46 int size;
47 char *name;
48 struct type **type;
49 };
50
51 /* x86_64_register_raw_size_table[i] is the number of bytes of storage in
52 GDB's register array occupied by register i. */
53 static struct register_info x86_64_register_info_table[] = {
54 /* 0 */ {8, "rax", &builtin_type_int64},
55 /* 1 */ {8, "rbx", &builtin_type_int64},
56 /* 2 */ {8, "rcx", &builtin_type_int64},
57 /* 3 */ {8, "rdx", &builtin_type_int64},
58 /* 4 */ {8, "rsi", &builtin_type_int64},
59 /* 5 */ {8, "rdi", &builtin_type_int64},
60 /* 6 */ {8, "rbp", &builtin_type_void_func_ptr},
61 /* 7 */ {8, "rsp", &builtin_type_void_func_ptr},
62 /* 8 */ {8, "r8", &builtin_type_int64},
63 /* 9 */ {8, "r9", &builtin_type_int64},
64 /* 10 */ {8, "r10", &builtin_type_int64},
65 /* 11 */ {8, "r11", &builtin_type_int64},
66 /* 12 */ {8, "r12", &builtin_type_int64},
67 /* 13 */ {8, "r13", &builtin_type_int64},
68 /* 14 */ {8, "r14", &builtin_type_int64},
69 /* 15 */ {8, "r15", &builtin_type_int64},
70 /* 16 */ {8, "rip", &builtin_type_void_func_ptr},
71 /* 17 */ {4, "eflags", &builtin_type_int32},
72 /* 18 */ {4, "ds", &builtin_type_int32},
73 /* 19 */ {4, "es", &builtin_type_int32},
74 /* 20 */ {4, "fs", &builtin_type_int32},
75 /* 21 */ {4, "gs", &builtin_type_int32},
76 /* 22 */ {10, "st0", &builtin_type_i387_ext},
77 /* 23 */ {10, "st1", &builtin_type_i387_ext},
78 /* 24 */ {10, "st2", &builtin_type_i387_ext},
79 /* 25 */ {10, "st3", &builtin_type_i387_ext},
80 /* 26 */ {10, "st4", &builtin_type_i387_ext},
81 /* 27 */ {10, "st5", &builtin_type_i387_ext},
82 /* 28 */ {10, "st6", &builtin_type_i387_ext},
83 /* 29 */ {10, "st7", &builtin_type_i387_ext},
84 /* 30 */ {4, "fctrl", &builtin_type_int32},
85 /* 31 */ {4, "fstat", &builtin_type_int32},
86 /* 32 */ {4, "ftag", &builtin_type_int32},
87 /* 33 */ {4, "fiseg", &builtin_type_int32},
88 /* 34 */ {4, "fioff", &builtin_type_int32},
89 /* 35 */ {4, "foseg", &builtin_type_int32},
90 /* 36 */ {4, "fooff", &builtin_type_int32},
91 /* 37 */ {4, "fop", &builtin_type_int32},
92 /* 38 */ {16, "xmm0", &builtin_type_v4sf},
93 /* 39 */ {16, "xmm1", &builtin_type_v4sf},
94 /* 40 */ {16, "xmm2", &builtin_type_v4sf},
95 /* 41 */ {16, "xmm3", &builtin_type_v4sf},
96 /* 42 */ {16, "xmm4", &builtin_type_v4sf},
97 /* 43 */ {16, "xmm5", &builtin_type_v4sf},
98 /* 44 */ {16, "xmm6", &builtin_type_v4sf},
99 /* 45 */ {16, "xmm7", &builtin_type_v4sf},
100 /* 46 */ {16, "xmm8", &builtin_type_v4sf},
101 /* 47 */ {16, "xmm9", &builtin_type_v4sf},
102 /* 48 */ {16, "xmm10", &builtin_type_v4sf},
103 /* 49 */ {16, "xmm11", &builtin_type_v4sf},
104 /* 50 */ {16, "xmm12", &builtin_type_v4sf},
105 /* 51 */ {16, "xmm13", &builtin_type_v4sf},
106 /* 52 */ {16, "xmm14", &builtin_type_v4sf},
107 /* 53 */ {16, "xmm15", &builtin_type_v4sf},
108 /* 54 */ {4, "mxcsr", &builtin_type_int32}
109 };
110
111 /* This array is a mapping from Dwarf-2 register
112 numbering to GDB's one. Dwarf-2 numbering is
113 defined in x86-64 ABI, section 3.6. */
114 static int x86_64_dwarf2gdb_regno_map[] = {
115 0, 1, 2, 3, /* RAX - RDX */
116 4, 5, 6, 7, /* RSI, RDI, RBP, RSP */
117 8, 9, 10, 11, /* R8 - R11 */
118 12, 13, 14, 15, /* R12 - R15 */
119 -1, /* RA - not mapped */
120 XMM1_REGNUM - 1, XMM1_REGNUM, /* XMM0 ... */
121 XMM1_REGNUM + 1, XMM1_REGNUM + 2,
122 XMM1_REGNUM + 3, XMM1_REGNUM + 4,
123 XMM1_REGNUM + 5, XMM1_REGNUM + 6,
124 XMM1_REGNUM + 7, XMM1_REGNUM + 8,
125 XMM1_REGNUM + 9, XMM1_REGNUM + 10,
126 XMM1_REGNUM + 11, XMM1_REGNUM + 12,
127 XMM1_REGNUM + 13, XMM1_REGNUM + 14, /* ... XMM15 */
128 ST0_REGNUM + 0, ST0_REGNUM + 1, /* ST0 ... */
129 ST0_REGNUM + 2, ST0_REGNUM + 3,
130 ST0_REGNUM + 4, ST0_REGNUM + 5,
131 ST0_REGNUM + 6, ST0_REGNUM + 7 /* ... ST7 */
132 };
133
134 static int x86_64_dwarf2gdb_regno_map_length =
135 sizeof (x86_64_dwarf2gdb_regno_map) /
136 sizeof (x86_64_dwarf2gdb_regno_map[0]);
137
138 /* Number of all registers */
139 #define X86_64_NUM_REGS (sizeof (x86_64_register_info_table) / \
140 sizeof (x86_64_register_info_table[0]))
141
142 /* Number of general registers. */
143 #define X86_64_NUM_GREGS (22)
144
145 int x86_64_num_regs = X86_64_NUM_REGS;
146 int x86_64_num_gregs = X86_64_NUM_GREGS;
147
148 /* Did we already print a note about frame pointer? */
149 int omit_fp_note_printed = 0;
150
151 /* Number of bytes of storage in the actual machine representation for
152 register REGNO. */
153 int
154 x86_64_register_raw_size (int regno)
155 {
156 return x86_64_register_info_table[regno].size;
157 }
158
159 /* x86_64_register_byte_table[i] is the offset into the register file of the
160 start of register number i. We initialize this from
161 x86_64_register_info_table. */
162 int x86_64_register_byte_table[X86_64_NUM_REGS];
163
164 /* Index within `registers' of the first byte of the space for register REGNO. */
165 int
166 x86_64_register_byte (int regno)
167 {
168 return x86_64_register_byte_table[regno];
169 }
170
171 /* Return the GDB type object for the "standard" data type of data in
172 register N. */
173 static struct type *
174 x86_64_register_virtual_type (int regno)
175 {
176 return *x86_64_register_info_table[regno].type;
177 }
178
179 /* x86_64_register_convertible is true if register N's virtual format is
180 different from its raw format. Note that this definition assumes
181 that the host supports IEEE 32-bit floats, since it doesn't say
182 that SSE registers need conversion. Even if we can't find a
183 counterexample, this is still sloppy. */
184 int
185 x86_64_register_convertible (int regno)
186 {
187 return IS_FP_REGNUM (regno);
188 }
189
190 /* Convert data from raw format for register REGNUM in buffer FROM to
191 virtual format with type TYPE in buffer TO. In principle both
192 formats are identical except that the virtual format has two extra
193 bytes appended that aren't used. We set these to zero. */
194 void
195 x86_64_register_convert_to_virtual (int regnum, struct type *type,
196 char *from, char *to)
197 {
198 char buf[12];
199
200 /* We only support floating-point values. */
201 if (TYPE_CODE (type) != TYPE_CODE_FLT)
202 {
203 warning ("Cannot convert floating-point register value "
204 "to non-floating-point type.");
205 memset (to, 0, TYPE_LENGTH (type));
206 return;
207 }
208 /* First add the necessary padding. */
209 memcpy (buf, from, FPU_REG_RAW_SIZE);
210 memset (buf + FPU_REG_RAW_SIZE, 0, sizeof buf - FPU_REG_RAW_SIZE);
211 /* Convert to TYPE. This should be a no-op, if TYPE is equivalent
212 to the extended floating-point format used by the FPU. */
213 convert_typed_floating (to, type, buf,
214 x86_64_register_virtual_type (regnum));
215 }
216
217 /* Convert data from virtual format with type TYPE in buffer FROM to
218 raw format for register REGNUM in buffer TO. Simply omit the two
219 unused bytes. */
220
221 void
222 x86_64_register_convert_to_raw (struct type *type, int regnum,
223 char *from, char *to)
224 {
225 gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12);
226 /* Simply omit the two unused bytes. */
227 memcpy (to, from, FPU_REG_RAW_SIZE);
228 }
229
230 /* Dwarf-2 <-> GDB register numbers mapping. */
231 int
232 x86_64_dwarf2_reg_to_regnum (int dw_reg)
233 {
234 if (dw_reg < 0 || dw_reg > x86_64_dwarf2gdb_regno_map_length)
235 {
236 warning ("Dwarf-2 uses unmapped register #%d\n", dw_reg);
237 return dw_reg;
238 }
239
240 return x86_64_dwarf2gdb_regno_map[dw_reg];
241 }
242
243 /* Push the return address (pointing to the call dummy) onto the stack
244 and return the new value for the stack pointer. */
245
246 static CORE_ADDR
247 x86_64_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
248 {
249 char buf[8];
250
251 store_unsigned_integer (buf, 8, CALL_DUMMY_ADDRESS ());
252 write_memory (sp - 8, buf, 8);
253 return sp - 8;
254 }
255
256 static void
257 x86_64_pop_frame (void)
258 {
259 generic_pop_current_frame (cfi_pop_frame);
260 }
261 \f
262
263 /* The returning of values is done according to the special algorithm.
264 Some types are returned in registers an some (big structures) in memory.
265 See ABI for details.
266 */
267
268 #define MAX_CLASSES 4
269
270 enum x86_64_reg_class
271 {
272 X86_64_NO_CLASS,
273 X86_64_INTEGER_CLASS,
274 X86_64_INTEGERSI_CLASS,
275 X86_64_SSE_CLASS,
276 X86_64_SSESF_CLASS,
277 X86_64_SSEDF_CLASS,
278 X86_64_SSEUP_CLASS,
279 X86_64_X87_CLASS,
280 X86_64_X87UP_CLASS,
281 X86_64_MEMORY_CLASS
282 };
283
284 /* Return the union class of CLASS1 and CLASS2.
285 See the x86-64 ABI for details. */
286
287 static enum x86_64_reg_class
288 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
289 {
290 /* Rule #1: If both classes are equal, this is the resulting class. */
291 if (class1 == class2)
292 return class1;
293
294 /* Rule #2: If one of the classes is NO_CLASS, the resulting class
295 is the other class. */
296 if (class1 == X86_64_NO_CLASS)
297 return class2;
298 if (class2 == X86_64_NO_CLASS)
299 return class1;
300
301 /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */
302 if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
303 return X86_64_MEMORY_CLASS;
304
305 /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */
306 if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
307 || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
308 return X86_64_INTEGERSI_CLASS;
309 if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
310 || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
311 return X86_64_INTEGER_CLASS;
312
313 /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used. */
314 if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
315 || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
316 return X86_64_MEMORY_CLASS;
317
318 /* Rule #6: Otherwise class SSE is used. */
319 return X86_64_SSE_CLASS;
320 }
321
322 /* Classify the argument type. CLASSES will be filled by the register
323 class used to pass each word of the operand. The number of words
324 is returned. In case the parameter should be passed in memory, 0
325 is returned. As a special case for zero sized containers,
326 classes[0] will be NO_CLASS and 1 is returned.
327
328 See the x86-64 psABI for details. */
329
330 static int
331 classify_argument (struct type *type,
332 enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
333 {
334 int bytes = TYPE_LENGTH (type);
335 int words = (bytes + 8 - 1) / 8;
336
337 switch (TYPE_CODE (type))
338 {
339 case TYPE_CODE_ARRAY:
340 case TYPE_CODE_STRUCT:
341 case TYPE_CODE_UNION:
342 {
343 int i;
344 enum x86_64_reg_class subclasses[MAX_CLASSES];
345
346 /* On x86-64 we pass structures larger than 16 bytes on the stack. */
347 if (bytes > 16)
348 return 0;
349
350 for (i = 0; i < words; i++)
351 classes[i] = X86_64_NO_CLASS;
352
353 /* Zero sized arrays or structures are NO_CLASS. We return 0
354 to signalize memory class, so handle it as special case. */
355 if (!words)
356 {
357 classes[0] = X86_64_NO_CLASS;
358 return 1;
359 }
360 switch (TYPE_CODE (type))
361 {
362 case TYPE_CODE_STRUCT:
363 {
364 int j;
365 for (j = 0; j < TYPE_NFIELDS (type); ++j)
366 {
367 int num = classify_argument (TYPE_FIELDS (type)[j].type,
368 subclasses,
369 (TYPE_FIELDS (type)[j].loc.
370 bitpos + bit_offset) % 256);
371 if (!num)
372 return 0;
373 for (i = 0; i < num; i++)
374 {
375 int pos =
376 (TYPE_FIELDS (type)[j].loc.bitpos +
377 bit_offset) / 8 / 8;
378 classes[i + pos] =
379 merge_classes (subclasses[i], classes[i + pos]);
380 }
381 }
382 }
383 break;
384 case TYPE_CODE_ARRAY:
385 {
386 int num;
387
388 num = classify_argument (TYPE_TARGET_TYPE (type),
389 subclasses, bit_offset);
390 if (!num)
391 return 0;
392
393 /* The partial classes are now full classes. */
394 if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
395 subclasses[0] = X86_64_SSE_CLASS;
396 if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
397 subclasses[0] = X86_64_INTEGER_CLASS;
398
399 for (i = 0; i < words; i++)
400 classes[i] = subclasses[i % num];
401 }
402 break;
403 case TYPE_CODE_UNION:
404 {
405 int j;
406 {
407 for (j = 0; j < TYPE_NFIELDS (type); ++j)
408 {
409 int num;
410 num = classify_argument (TYPE_FIELDS (type)[j].type,
411 subclasses, bit_offset);
412 if (!num)
413 return 0;
414 for (i = 0; i < num; i++)
415 classes[i] = merge_classes (subclasses[i], classes[i]);
416 }
417 }
418 }
419 break;
420 default:
421 break;
422 }
423 /* Final merger cleanup. */
424 for (i = 0; i < words; i++)
425 {
426 /* If one class is MEMORY, everything should be passed in
427 memory. */
428 if (classes[i] == X86_64_MEMORY_CLASS)
429 return 0;
430
431 /* The X86_64_SSEUP_CLASS should be always preceeded by
432 X86_64_SSE_CLASS. */
433 if (classes[i] == X86_64_SSEUP_CLASS
434 && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
435 classes[i] = X86_64_SSE_CLASS;
436
437 /* X86_64_X87UP_CLASS should be preceeded by X86_64_X87_CLASS. */
438 if (classes[i] == X86_64_X87UP_CLASS
439 && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
440 classes[i] = X86_64_SSE_CLASS;
441 }
442 return words;
443 }
444 break;
445 case TYPE_CODE_FLT:
446 switch (bytes)
447 {
448 case 4:
449 if (!(bit_offset % 64))
450 classes[0] = X86_64_SSESF_CLASS;
451 else
452 classes[0] = X86_64_SSE_CLASS;
453 return 1;
454 case 8:
455 classes[0] = X86_64_SSEDF_CLASS;
456 return 1;
457 case 16:
458 classes[0] = X86_64_X87_CLASS;
459 classes[1] = X86_64_X87UP_CLASS;
460 return 2;
461 }
462 break;
463 case TYPE_CODE_ENUM:
464 case TYPE_CODE_REF:
465 case TYPE_CODE_INT:
466 case TYPE_CODE_PTR:
467 switch (bytes)
468 {
469 case 1:
470 case 2:
471 case 4:
472 case 8:
473 if (bytes * 8 + bit_offset <= 32)
474 classes[0] = X86_64_INTEGERSI_CLASS;
475 else
476 classes[0] = X86_64_INTEGER_CLASS;
477 return 1;
478 case 16:
479 classes[0] = classes[1] = X86_64_INTEGER_CLASS;
480 return 2;
481 default:
482 break;
483 }
484 case TYPE_CODE_VOID:
485 return 0;
486 default: /* Avoid warning. */
487 break;
488 }
489 internal_error (__FILE__, __LINE__,
490 "classify_argument: unknown argument type");
491 }
492
493 /* Examine the argument and set *INT_NREGS and *SSE_NREGS to the
494 number of registers required based on the information passed in
495 CLASSES. Return 0 if parameter should be passed in memory. */
496
497 static int
498 examine_argument (enum x86_64_reg_class classes[MAX_CLASSES],
499 int n, int *int_nregs, int *sse_nregs)
500 {
501 *int_nregs = 0;
502 *sse_nregs = 0;
503 if (!n)
504 return 0;
505 for (n--; n >= 0; n--)
506 switch (classes[n])
507 {
508 case X86_64_INTEGER_CLASS:
509 case X86_64_INTEGERSI_CLASS:
510 (*int_nregs)++;
511 break;
512 case X86_64_SSE_CLASS:
513 case X86_64_SSESF_CLASS:
514 case X86_64_SSEDF_CLASS:
515 (*sse_nregs)++;
516 break;
517 case X86_64_NO_CLASS:
518 case X86_64_SSEUP_CLASS:
519 case X86_64_X87_CLASS:
520 case X86_64_X87UP_CLASS:
521 break;
522 case X86_64_MEMORY_CLASS:
523 internal_error (__FILE__, __LINE__,
524 "examine_argument: unexpected memory class");
525 }
526 return 1;
527 }
528
529 #define RET_INT_REGS 2
530 #define RET_SSE_REGS 2
531
532 /* Check if the structure in value_type is returned in registers or in
533 memory. If this function returns 1, GDB will call
534 STORE_STRUCT_RETURN and EXTRACT_STRUCT_VALUE_ADDRESS else
535 STORE_RETURN_VALUE and EXTRACT_RETURN_VALUE will be used. */
536 int
537 x86_64_use_struct_convention (int gcc_p, struct type *value_type)
538 {
539 enum x86_64_reg_class class[MAX_CLASSES];
540 int n = classify_argument (value_type, class, 0);
541 int needed_intregs;
542 int needed_sseregs;
543
544 return (!n ||
545 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
546 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS);
547 }
548
549 /* Extract from an array REGBUF containing the (raw) register state, a
550 function return value of TYPE, and copy that, in virtual format,
551 into VALBUF. */
552
553 void
554 x86_64_extract_return_value (struct type *type, struct regcache *regcache,
555 void *valbuf)
556 {
557 enum x86_64_reg_class class[MAX_CLASSES];
558 int n = classify_argument (type, class, 0);
559 int needed_intregs;
560 int needed_sseregs;
561 int intreg = 0;
562 int ssereg = 0;
563 int offset = 0;
564 int ret_int_r[RET_INT_REGS] = { RAX_REGNUM, RDX_REGNUM };
565 int ret_sse_r[RET_SSE_REGS] = { XMM0_REGNUM, XMM1_REGNUM };
566
567 if (!n ||
568 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
569 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS)
570 { /* memory class */
571 CORE_ADDR addr;
572 regcache_cooked_read (regcache, RAX_REGNUM, &addr);
573 read_memory (addr, valbuf, TYPE_LENGTH (type));
574 return;
575 }
576 else
577 {
578 int i;
579 for (i = 0; i < n; i++)
580 {
581 switch (class[i])
582 {
583 case X86_64_NO_CLASS:
584 break;
585 case X86_64_INTEGER_CLASS:
586 regcache_cooked_read (regcache, ret_int_r[(intreg + 1) / 2],
587 (char *) valbuf + offset);
588 offset += 8;
589 intreg += 2;
590 break;
591 case X86_64_INTEGERSI_CLASS:
592 regcache_cooked_read_part (regcache, ret_int_r[intreg / 2],
593 0, 4, (char *) valbuf + offset);
594 offset += 8;
595 intreg++;
596 break;
597 case X86_64_SSEDF_CLASS:
598 case X86_64_SSESF_CLASS:
599 case X86_64_SSE_CLASS:
600 regcache_cooked_read_part (regcache,
601 ret_sse_r[(ssereg + 1) / 2], 0, 8,
602 (char *) valbuf + offset);
603 offset += 8;
604 ssereg += 2;
605 break;
606 case X86_64_SSEUP_CLASS:
607 regcache_cooked_read_part (regcache, ret_sse_r[ssereg / 2],
608 0, 8, (char *) valbuf + offset);
609 offset += 8;
610 ssereg++;
611 break;
612 case X86_64_X87_CLASS:
613 regcache_cooked_read_part (regcache, FP0_REGNUM,
614 0, 8, (char *) valbuf + offset);
615 offset += 8;
616 break;
617 case X86_64_X87UP_CLASS:
618 regcache_cooked_read_part (regcache, FP0_REGNUM,
619 8, 2, (char *) valbuf + offset);
620 offset += 8;
621 break;
622 case X86_64_MEMORY_CLASS:
623 default:
624 internal_error (__FILE__, __LINE__,
625 "Unexpected argument class");
626 }
627 }
628 }
629 }
630
631 static void
632 x86_64_frame_init_saved_regs (struct frame_info *fi)
633 {
634 /* Do nothing. Everything is handled by the stack unwinding code. */
635 }
636
637 #define INT_REGS 6
638 #define SSE_REGS 16
639
640 CORE_ADDR
641 x86_64_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
642 int struct_return, CORE_ADDR struct_addr)
643 {
644 int intreg = 0;
645 int ssereg = 0;
646 int i;
647 static int int_parameter_registers[INT_REGS] = {
648 5 /* RDI */ , 4 /* RSI */ ,
649 3 /* RDX */ , 2 /* RCX */ ,
650 8 /* R8 */ , 9 /* R9 */
651 };
652 /* XMM0 - XMM15 */
653 static int sse_parameter_registers[SSE_REGS] = {
654 XMM1_REGNUM - 1, XMM1_REGNUM, XMM1_REGNUM + 1, XMM1_REGNUM + 2,
655 XMM1_REGNUM + 3, XMM1_REGNUM + 4, XMM1_REGNUM + 5, XMM1_REGNUM + 6,
656 XMM1_REGNUM + 7, XMM1_REGNUM + 8, XMM1_REGNUM + 9, XMM1_REGNUM + 10,
657 XMM1_REGNUM + 11, XMM1_REGNUM + 12, XMM1_REGNUM + 13, XMM1_REGNUM + 14
658 };
659 int stack_values_count = 0;
660 int *stack_values;
661 stack_values = alloca (nargs * sizeof (int));
662 for (i = 0; i < nargs; i++)
663 {
664 enum x86_64_reg_class class[MAX_CLASSES];
665 int n = classify_argument (args[i]->type, class, 0);
666 int needed_intregs;
667 int needed_sseregs;
668
669 if (!n ||
670 !examine_argument (class, n, &needed_intregs, &needed_sseregs)
671 || intreg / 2 + needed_intregs > INT_REGS
672 || ssereg / 2 + needed_sseregs > SSE_REGS)
673 { /* memory class */
674 stack_values[stack_values_count++] = i;
675 }
676 else
677 {
678 int j;
679 for (j = 0; j < n; j++)
680 {
681 int offset = 0;
682 switch (class[j])
683 {
684 case X86_64_NO_CLASS:
685 break;
686 case X86_64_INTEGER_CLASS:
687 deprecated_write_register_gen (int_parameter_registers
688 [(intreg + 1) / 2],
689 VALUE_CONTENTS_ALL (args[i]) + offset);
690 offset += 8;
691 intreg += 2;
692 break;
693 case X86_64_INTEGERSI_CLASS:
694 {
695 LONGEST num
696 = extract_signed_integer (VALUE_CONTENTS_ALL (args[i])
697 + offset, 4);
698 regcache_raw_write_signed (current_regcache,
699 int_parameter_registers[intreg / 2], num);
700
701 offset += 8;
702 intreg++;
703 break;
704 }
705 case X86_64_SSEDF_CLASS:
706 case X86_64_SSESF_CLASS:
707 case X86_64_SSE_CLASS:
708 deprecated_write_register_gen (sse_parameter_registers
709 [(ssereg + 1) / 2],
710 VALUE_CONTENTS_ALL (args[i]) + offset);
711 offset += 8;
712 ssereg += 2;
713 break;
714 case X86_64_SSEUP_CLASS:
715 deprecated_write_register_gen (sse_parameter_registers[ssereg / 2],
716 VALUE_CONTENTS_ALL (args[i]) + offset);
717 offset += 8;
718 ssereg++;
719 break;
720 case X86_64_X87_CLASS:
721 case X86_64_MEMORY_CLASS:
722 stack_values[stack_values_count++] = i;
723 break;
724 case X86_64_X87UP_CLASS:
725 break;
726 default:
727 internal_error (__FILE__, __LINE__,
728 "Unexpected argument class");
729 }
730 intreg += intreg % 2;
731 ssereg += ssereg % 2;
732 }
733 }
734 }
735 while (--stack_values_count >= 0)
736 {
737 struct value *arg = args[stack_values[stack_values_count]];
738 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
739 len += 7;
740 len -= len % 8;
741 sp -= len;
742 write_memory (sp, VALUE_CONTENTS_ALL (arg), len);
743 }
744 return sp;
745 }
746
747 /* Write into the appropriate registers a function return value stored
748 in VALBUF of type TYPE, given in virtual format. */
749 void
750 x86_64_store_return_value (struct type *type, struct regcache *regcache,
751 const void *valbuf)
752 {
753 int len = TYPE_LENGTH (type);
754
755 if (TYPE_CODE_FLT == TYPE_CODE (type))
756 {
757 /* Floating-point return values can be found in %st(0). */
758 if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT
759 && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext)
760 {
761 /* Copy straight over. */
762 regcache_cooked_write (regcache, FP0_REGNUM, valbuf);
763 }
764 else
765 {
766 char buf[FPU_REG_RAW_SIZE];
767 DOUBLEST val;
768
769 /* Convert the value found in VALBUF to the extended
770 floating point format used by the FPU. This is probably
771 not exactly how it would happen on the target itself, but
772 it is the best we can do. */
773 val = deprecated_extract_floating (valbuf, TYPE_LENGTH (type));
774 floatformat_from_doublest (&floatformat_i387_ext, &val, buf);
775 regcache_cooked_write_part (regcache, FP0_REGNUM,
776 0, FPU_REG_RAW_SIZE, buf);
777 }
778 }
779 else
780 {
781 int low_size = REGISTER_RAW_SIZE (0);
782 int high_size = REGISTER_RAW_SIZE (1);
783
784 if (len <= low_size)
785 regcache_cooked_write_part (regcache, 0, 0, len, valbuf);
786 else if (len <= (low_size + high_size))
787 {
788 regcache_cooked_write_part (regcache, 0, 0, low_size, valbuf);
789 regcache_cooked_write_part (regcache, 1, 0,
790 len - low_size,
791 (const char *) valbuf + low_size);
792 }
793 else
794 internal_error (__FILE__, __LINE__,
795 "Cannot store return value of %d bytes long.", len);
796 }
797 }
798 \f
799
800 const char *
801 x86_64_register_name (int reg_nr)
802 {
803 if (reg_nr < 0 || reg_nr >= X86_64_NUM_REGS)
804 return NULL;
805 return x86_64_register_info_table[reg_nr].name;
806 }
807
808 int
809 x86_64_register_number (const char *name)
810 {
811 int reg_nr;
812
813 for (reg_nr = 0; reg_nr < X86_64_NUM_REGS; reg_nr++)
814 if (strcmp (name, x86_64_register_info_table[reg_nr].name) == 0)
815 return reg_nr;
816 return -1;
817 }
818 \f
819
820 /* Store the address of the place in which to copy the structure the
821 subroutine will return. This is called from call_function. */
822 void
823 x86_64_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
824 {
825 write_register (RDI_REGNUM, addr);
826 }
827
828 int
829 x86_64_frameless_function_invocation (struct frame_info *frame)
830 {
831 return 0;
832 }
833
834 /* We will handle only functions beginning with:
835 55 pushq %rbp
836 48 89 e5 movq %rsp,%rbp
837 Any function that doesn't start with this sequence
838 will be assumed to have no prologue and thus no valid
839 frame pointer in %rbp. */
840 #define PROLOG_BUFSIZE 4
841 int
842 x86_64_function_has_prologue (CORE_ADDR pc)
843 {
844 int i;
845 unsigned char prolog_expect[PROLOG_BUFSIZE] = { 0x55, 0x48, 0x89, 0xe5 },
846 prolog_buf[PROLOG_BUFSIZE];
847
848 read_memory (pc, (char *) prolog_buf, PROLOG_BUFSIZE);
849
850 /* First check, whether pc points to pushq %rbp, movq %rsp,%rbp. */
851 for (i = 0; i < PROLOG_BUFSIZE; i++)
852 if (prolog_expect[i] != prolog_buf[i])
853 return 0; /* ... no, it doesn't. Nothing to skip. */
854
855 return 1;
856 }
857
858 /* If a function with debugging information and known beginning
859 is detected, we will return pc of the next line in the source
860 code. With this approach we effectively skip the prolog. */
861
862 CORE_ADDR
863 x86_64_skip_prologue (CORE_ADDR pc)
864 {
865 int i;
866 struct symtab_and_line v_sal;
867 struct symbol *v_function;
868 CORE_ADDR endaddr;
869
870 if (! x86_64_function_has_prologue (pc))
871 return pc;
872
873 /* OK, we have found the prologue and want PC of the first
874 non-prologue instruction. */
875 pc += PROLOG_BUFSIZE;
876
877 v_function = find_pc_function (pc);
878 v_sal = find_pc_line (pc, 0);
879
880 /* If pc doesn't point to a function with debuginfo, some of the
881 following may be NULL. */
882 if (!v_function || !v_function->ginfo.value.block || !v_sal.symtab)
883 return pc;
884
885 endaddr = BLOCK_END (SYMBOL_BLOCK_VALUE (v_function));
886
887 for (i = 0; i < v_sal.symtab->linetable->nitems; i++)
888 if (v_sal.symtab->linetable->item[i].pc >= pc
889 && v_sal.symtab->linetable->item[i].pc < endaddr)
890 {
891 pc = v_sal.symtab->linetable->item[i].pc;
892 break;
893 }
894
895 return pc;
896 }
897
898 static void
899 x86_64_save_dummy_frame_tos (CORE_ADDR sp)
900 {
901 /* We must add the size of the return address that is already
902 put on the stack. */
903 generic_save_dummy_frame_tos (sp +
904 TYPE_LENGTH (builtin_type_void_func_ptr));
905 }
906
907 static struct frame_id
908 x86_64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *frame)
909 {
910 CORE_ADDR base;
911 frame_unwind_unsigned_register (frame, SP_REGNUM, &base);
912 return frame_id_build (base, frame_pc_unwind (frame));
913 }
914
915 void
916 x86_64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
917 {
918 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
919 int i, sum;
920
921 /* The x86-64 has 16 SSE registers. */
922 tdep->num_xmm_regs = 16;
923
924 /* This is what all the fuss is about. */
925 set_gdbarch_long_bit (gdbarch, 64);
926 set_gdbarch_long_long_bit (gdbarch, 64);
927 set_gdbarch_ptr_bit (gdbarch, 64);
928
929 /* In contrast to the i386, on the x86-64 a `long double' actually
930 takes up 128 bits, even though it's still based on the i387
931 extended floating-point format which has only 80 significant bits. */
932 set_gdbarch_long_double_bit (gdbarch, 128);
933
934 set_gdbarch_num_regs (gdbarch, X86_64_NUM_REGS);
935
936 /* Register numbers of various important registers. */
937 set_gdbarch_sp_regnum (gdbarch, 7); /* %rsp */
938 set_gdbarch_deprecated_fp_regnum (gdbarch, 6); /* %rbp */
939 set_gdbarch_pc_regnum (gdbarch, 16); /* %rip */
940 set_gdbarch_ps_regnum (gdbarch, 17); /* %eflags */
941 set_gdbarch_fp0_regnum (gdbarch, X86_64_NUM_GREGS); /* %st(0) */
942
943 /* The "default" register numbering scheme for the x86-64 is
944 referred to as the "DWARF register number mapping" in the psABI.
945 The preferred debugging format for all known x86-64 targets is
946 actually DWARF2, and GCC doesn't seem to support DWARF (that is
947 DWARF-1), but we provide the same mapping just in case. This
948 mapping is also used for stabs, which GCC does support. */
949 set_gdbarch_stab_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
950 set_gdbarch_dwarf_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
951 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, x86_64_dwarf2_reg_to_regnum);
952
953 /* We don't override SDB_REG_RO_REGNUM, sice COFF doesn't seem to be
954 in use on any of the supported x86-64 targets. */
955
956 set_gdbarch_register_name (gdbarch, x86_64_register_name);
957 set_gdbarch_register_size (gdbarch, 8);
958
959 /* Total amount of space needed to store our copies of the machine's
960 register (SIZEOF_GREGS + SIZEOF_FPU_REGS + SIZEOF_FPU_CTRL_REGS +
961 SIZEOF_SSE_REGS) */
962 for (i = 0, sum = 0; i < X86_64_NUM_REGS; i++)
963 sum += x86_64_register_info_table[i].size;
964 set_gdbarch_register_bytes (gdbarch, sum);
965
966 set_gdbarch_register_raw_size (gdbarch, x86_64_register_raw_size);
967 set_gdbarch_register_byte (gdbarch, x86_64_register_byte);
968 set_gdbarch_register_virtual_type (gdbarch, x86_64_register_virtual_type);
969
970 set_gdbarch_register_convertible (gdbarch, x86_64_register_convertible);
971 set_gdbarch_register_convert_to_virtual (gdbarch,
972 x86_64_register_convert_to_virtual);
973 set_gdbarch_register_convert_to_raw (gdbarch,
974 x86_64_register_convert_to_raw);
975
976 /* Getting saved registers is handled by unwind information. */
977 set_gdbarch_deprecated_get_saved_register (gdbarch, cfi_get_saved_register);
978
979 /* FIXME: kettenis/20021026: Should we set parm_boundary to 64 here? */
980 set_gdbarch_deprecated_target_read_fp (gdbarch, cfi_read_fp);
981
982 set_gdbarch_extract_return_value (gdbarch, x86_64_extract_return_value);
983
984 set_gdbarch_deprecated_push_arguments (gdbarch, x86_64_push_arguments);
985 set_gdbarch_deprecated_push_return_address (gdbarch, x86_64_push_return_address);
986 set_gdbarch_deprecated_pop_frame (gdbarch, x86_64_pop_frame);
987 set_gdbarch_deprecated_store_struct_return (gdbarch, x86_64_store_struct_return);
988 set_gdbarch_store_return_value (gdbarch, x86_64_store_return_value);
989 /* Override, since this is handled by x86_64_extract_return_value. */
990 set_gdbarch_extract_struct_value_address (gdbarch, NULL);
991 set_gdbarch_use_struct_convention (gdbarch, x86_64_use_struct_convention);
992
993 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, x86_64_frame_init_saved_regs);
994 set_gdbarch_skip_prologue (gdbarch, x86_64_skip_prologue);
995
996 set_gdbarch_deprecated_frame_chain (gdbarch, x86_64_linux_frame_chain);
997 set_gdbarch_frameless_function_invocation (gdbarch,
998 x86_64_frameless_function_invocation);
999 /* FIXME: kettenis/20021026: These two are GNU/Linux-specific and
1000 should be moved elsewhere. */
1001 set_gdbarch_deprecated_frame_saved_pc (gdbarch, x86_64_linux_frame_saved_pc);
1002 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, x86_64_linux_saved_pc_after_call);
1003 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1004 /* FIXME: kettenis/20021026: This one is GNU/Linux-specific too. */
1005 set_gdbarch_pc_in_sigtramp (gdbarch, x86_64_linux_in_sigtramp);
1006
1007 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1008
1009 /* Build call frame information (CFI) from DWARF2 frame debug info. */
1010 set_gdbarch_dwarf2_build_frame_info (gdbarch, dwarf2_build_frame_info);
1011
1012 /* Initialization of per-frame CFI. */
1013 set_gdbarch_deprecated_init_extra_frame_info (gdbarch, cfi_init_extra_frame_info);
1014
1015 /* Frame PC initialization is handled by using CFI. */
1016 set_gdbarch_deprecated_init_frame_pc (gdbarch, x86_64_init_frame_pc);
1017
1018 /* Cons up virtual frame pointer for trace. */
1019 set_gdbarch_virtual_frame_pointer (gdbarch, cfi_virtual_frame_pointer);
1020
1021 /* FIXME: kettenis/20021026: This is ELF-specific. Fine for now,
1022 since all supported x86-64 targets are ELF, but that might change
1023 in the future. */
1024 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1025
1026 /* Dummy frame helper functions. */
1027 set_gdbarch_save_dummy_frame_tos (gdbarch, x86_64_save_dummy_frame_tos);
1028 set_gdbarch_unwind_dummy_id (gdbarch, x86_64_unwind_dummy_id);
1029 }
1030
1031 void
1032 _initialize_x86_64_tdep (void)
1033 {
1034 /* Initialize the table saying where each register starts in the
1035 register file. */
1036 {
1037 int i, offset;
1038
1039 offset = 0;
1040 for (i = 0; i < X86_64_NUM_REGS; i++)
1041 {
1042 x86_64_register_byte_table[i] = offset;
1043 offset += x86_64_register_info_table[i].size;
1044 }
1045 }
1046 }
This page took 0.070874 seconds and 5 git commands to generate.