* x86-64-tdep.c (X86_64_NUM_SAVED_REGS): Set to X86_64_NUM_GREGS.
[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 "arch-utils.h"
25 #include "block.h"
26 #include "dummy-frame.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "inferior.h"
31 #include "gdbcmd.h"
32 #include "gdbcore.h"
33 #include "objfiles.h"
34 #include "regcache.h"
35 #include "symfile.h"
36
37 #include "gdb_assert.h"
38
39 #include "x86-64-tdep.h"
40 #include "i387-tdep.h"
41
42 /* Register numbers of various important registers. */
43
44 #define X86_64_RAX_REGNUM 0 /* %rax */
45 #define X86_64_RDX_REGNUM 3 /* %rdx */
46 #define X86_64_RDI_REGNUM 5 /* %rdi */
47 #define X86_64_RBP_REGNUM 6 /* %rbp */
48 #define X86_64_RSP_REGNUM 7 /* %rsp */
49 #define X86_64_RIP_REGNUM 16 /* %rip */
50 #define X86_64_EFLAGS_REGNUM 17 /* %eflags */
51 #define X86_64_ST0_REGNUM 22 /* %st0 */
52 #define X86_64_XMM0_REGNUM 38 /* %xmm0 */
53 #define X86_64_XMM1_REGNUM 39 /* %xmm1 */
54
55 struct x86_64_register_info
56 {
57 char *name;
58 struct type **type;
59 };
60
61 static struct x86_64_register_info x86_64_register_info[] =
62 {
63 { "rax", &builtin_type_int64 },
64 { "rbx", &builtin_type_int64 },
65 { "rcx", &builtin_type_int64 },
66 { "rdx", &builtin_type_int64 },
67 { "rsi", &builtin_type_int64 },
68 { "rdi", &builtin_type_int64 },
69 { "rbp", &builtin_type_void_data_ptr },
70 { "rsp", &builtin_type_void_data_ptr },
71
72 /* %r8 is indeed register number 8. */
73 { "r8", &builtin_type_int64 },
74 { "r9", &builtin_type_int64 },
75 { "r10", &builtin_type_int64 },
76 { "r11", &builtin_type_int64 },
77 { "r12", &builtin_type_int64 },
78 { "r13", &builtin_type_int64 },
79 { "r14", &builtin_type_int64 },
80 { "r15", &builtin_type_int64 },
81 { "rip", &builtin_type_void_func_ptr },
82 { "eflags", &builtin_type_int32 },
83 { "ds", &builtin_type_int32 },
84 { "es", &builtin_type_int32 },
85 { "fs", &builtin_type_int32 },
86 { "gs", &builtin_type_int32 },
87
88 /* %st0 is register number 22. */
89 { "st0", &builtin_type_i387_ext },
90 { "st1", &builtin_type_i387_ext },
91 { "st2", &builtin_type_i387_ext },
92 { "st3", &builtin_type_i387_ext },
93 { "st4", &builtin_type_i387_ext },
94 { "st5", &builtin_type_i387_ext },
95 { "st6", &builtin_type_i387_ext },
96 { "st7", &builtin_type_i387_ext },
97 { "fctrl", &builtin_type_int32 },
98 { "fstat", &builtin_type_int32 },
99 { "ftag", &builtin_type_int32 },
100 { "fiseg", &builtin_type_int32 },
101 { "fioff", &builtin_type_int32 },
102 { "foseg", &builtin_type_int32 },
103 { "fooff", &builtin_type_int32 },
104 { "fop", &builtin_type_int32 },
105
106 /* %xmm0 is register number 38. */
107 { "xmm0", &builtin_type_v4sf },
108 { "xmm1", &builtin_type_v4sf },
109 { "xmm2", &builtin_type_v4sf },
110 { "xmm3", &builtin_type_v4sf },
111 { "xmm4", &builtin_type_v4sf },
112 { "xmm5", &builtin_type_v4sf },
113 { "xmm6", &builtin_type_v4sf },
114 { "xmm7", &builtin_type_v4sf },
115 { "xmm8", &builtin_type_v4sf },
116 { "xmm9", &builtin_type_v4sf },
117 { "xmm10", &builtin_type_v4sf },
118 { "xmm11", &builtin_type_v4sf },
119 { "xmm12", &builtin_type_v4sf },
120 { "xmm13", &builtin_type_v4sf },
121 { "xmm14", &builtin_type_v4sf },
122 { "xmm15", &builtin_type_v4sf },
123 { "mxcsr", &builtin_type_int32 }
124 };
125
126 /* Total number of registers. */
127 #define X86_64_NUM_REGS \
128 (sizeof (x86_64_register_info) / sizeof (x86_64_register_info[0]))
129
130 /* Return the name of register REGNUM. */
131
132 static const char *
133 x86_64_register_name (int regnum)
134 {
135 if (regnum >= 0 && regnum < X86_64_NUM_REGS)
136 return x86_64_register_info[regnum].name;
137
138 return NULL;
139 }
140
141 /* Return the GDB type object for the "standard" data type of data in
142 register REGNUM. */
143
144 static struct type *
145 x86_64_register_type (struct gdbarch *gdbarch, int regnum)
146 {
147 gdb_assert (regnum >= 0 && regnum < X86_64_NUM_REGS);
148
149 return *x86_64_register_info[regnum].type;
150 }
151
152 /* DWARF Register Number Mapping as defined in the System V psABI,
153 section 3.6. */
154
155 static int x86_64_dwarf_regmap[] =
156 {
157 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
158 X86_64_RAX_REGNUM, X86_64_RDX_REGNUM, 3, 2,
159 4, X86_64_RDI_REGNUM,
160
161 /* Frame Pointer Register RBP. */
162 X86_64_RBP_REGNUM,
163
164 /* Stack Pointer Register RSP. */
165 X86_64_RSP_REGNUM,
166
167 /* Extended Integer Registers 8 - 15. */
168 8, 9, 10, 11, 12, 13, 14, 15,
169
170 /* Return Address RA. Not mapped. */
171 -1,
172
173 /* SSE Registers 0 - 7. */
174 X86_64_XMM0_REGNUM + 0, X86_64_XMM1_REGNUM,
175 X86_64_XMM0_REGNUM + 2, X86_64_XMM0_REGNUM + 3,
176 X86_64_XMM0_REGNUM + 4, X86_64_XMM0_REGNUM + 5,
177 X86_64_XMM0_REGNUM + 6, X86_64_XMM0_REGNUM + 7,
178
179 /* Extended SSE Registers 8 - 15. */
180 X86_64_XMM0_REGNUM + 8, X86_64_XMM0_REGNUM + 9,
181 X86_64_XMM0_REGNUM + 10, X86_64_XMM0_REGNUM + 11,
182 X86_64_XMM0_REGNUM + 12, X86_64_XMM0_REGNUM + 13,
183 X86_64_XMM0_REGNUM + 14, X86_64_XMM0_REGNUM + 15,
184
185 /* Floating Point Registers 0-7. */
186 X86_64_ST0_REGNUM + 0, X86_64_ST0_REGNUM + 1,
187 X86_64_ST0_REGNUM + 2, X86_64_ST0_REGNUM + 3,
188 X86_64_ST0_REGNUM + 4, X86_64_ST0_REGNUM + 5,
189 X86_64_ST0_REGNUM + 6, X86_64_ST0_REGNUM + 7
190 };
191
192 static const int x86_64_dwarf_regmap_len =
193 (sizeof (x86_64_dwarf_regmap) / sizeof (x86_64_dwarf_regmap[0]));
194
195 /* Convert DWARF register number REG to the appropriate register
196 number used by GDB. */
197
198 static int
199 x86_64_dwarf_reg_to_regnum (int reg)
200 {
201 int regnum = -1;
202
203 if (reg >= 0 || reg < x86_64_dwarf_regmap_len)
204 regnum = x86_64_dwarf_regmap[reg];
205
206 if (regnum == -1)
207 warning ("Unmapped DWARF Register #%d encountered\n", reg);
208
209 return regnum;
210 }
211 \f
212
213 /* The returning of values is done according to the special algorithm.
214 Some types are returned in registers an some (big structures) in
215 memory. See the System V psABI for details. */
216
217 #define MAX_CLASSES 4
218
219 enum x86_64_reg_class
220 {
221 X86_64_NO_CLASS,
222 X86_64_INTEGER_CLASS,
223 X86_64_INTEGERSI_CLASS,
224 X86_64_SSE_CLASS,
225 X86_64_SSESF_CLASS,
226 X86_64_SSEDF_CLASS,
227 X86_64_SSEUP_CLASS,
228 X86_64_X87_CLASS,
229 X86_64_X87UP_CLASS,
230 X86_64_MEMORY_CLASS
231 };
232
233 /* Return the union class of CLASS1 and CLASS2.
234 See the System V psABI for details. */
235
236 static enum x86_64_reg_class
237 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
238 {
239 /* Rule (a): If both classes are equal, this is the resulting class. */
240 if (class1 == class2)
241 return class1;
242
243 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
244 is the other class. */
245 if (class1 == X86_64_NO_CLASS)
246 return class2;
247 if (class2 == X86_64_NO_CLASS)
248 return class1;
249
250 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
251 if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
252 return X86_64_MEMORY_CLASS;
253
254 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
255 if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
256 || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
257 return X86_64_INTEGERSI_CLASS;
258 if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
259 || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
260 return X86_64_INTEGER_CLASS;
261
262 /* Rule (e): If one of the classes is X87 or X87UP class, MEMORY is
263 used as class. */
264 if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS
265 || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS)
266 return X86_64_MEMORY_CLASS;
267
268 /* Rule (f): Otherwise class SSE is used. */
269 return X86_64_SSE_CLASS;
270 }
271
272 /* Classify the argument type. CLASSES will be filled by the register
273 class used to pass each word of the operand. The number of words
274 is returned. In case the parameter should be passed in memory, 0
275 is returned. As a special case for zero sized containers,
276 classes[0] will be NO_CLASS and 1 is returned.
277
278 See the System V psABI for details. */
279
280 static int
281 classify_argument (struct type *type,
282 enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
283 {
284 int bytes = TYPE_LENGTH (type);
285 int words = (bytes + 8 - 1) / 8;
286
287 switch (TYPE_CODE (type))
288 {
289 case TYPE_CODE_ARRAY:
290 case TYPE_CODE_STRUCT:
291 case TYPE_CODE_UNION:
292 {
293 int i;
294 enum x86_64_reg_class subclasses[MAX_CLASSES];
295
296 /* On x86-64 we pass structures larger than 16 bytes on the stack. */
297 if (bytes > 16)
298 return 0;
299
300 for (i = 0; i < words; i++)
301 classes[i] = X86_64_NO_CLASS;
302
303 /* Zero sized arrays or structures are NO_CLASS. We return 0
304 to signalize memory class, so handle it as special case. */
305 if (!words)
306 {
307 classes[0] = X86_64_NO_CLASS;
308 return 1;
309 }
310 switch (TYPE_CODE (type))
311 {
312 case TYPE_CODE_STRUCT:
313 {
314 int j;
315 for (j = 0; j < TYPE_NFIELDS (type); ++j)
316 {
317 int num = classify_argument (TYPE_FIELDS (type)[j].type,
318 subclasses,
319 (TYPE_FIELDS (type)[j].loc.
320 bitpos + bit_offset) % 256);
321 if (!num)
322 return 0;
323 for (i = 0; i < num; i++)
324 {
325 int pos =
326 (TYPE_FIELDS (type)[j].loc.bitpos +
327 bit_offset) / 8 / 8;
328 classes[i + pos] =
329 merge_classes (subclasses[i], classes[i + pos]);
330 }
331 }
332 }
333 break;
334 case TYPE_CODE_ARRAY:
335 {
336 int num;
337
338 num = classify_argument (TYPE_TARGET_TYPE (type),
339 subclasses, bit_offset);
340 if (!num)
341 return 0;
342
343 /* The partial classes are now full classes. */
344 if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
345 subclasses[0] = X86_64_SSE_CLASS;
346 if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
347 subclasses[0] = X86_64_INTEGER_CLASS;
348
349 for (i = 0; i < words; i++)
350 classes[i] = subclasses[i % num];
351 }
352 break;
353 case TYPE_CODE_UNION:
354 {
355 int j;
356 {
357 for (j = 0; j < TYPE_NFIELDS (type); ++j)
358 {
359 int num;
360 num = classify_argument (TYPE_FIELDS (type)[j].type,
361 subclasses, bit_offset);
362 if (!num)
363 return 0;
364 for (i = 0; i < num; i++)
365 classes[i] = merge_classes (subclasses[i], classes[i]);
366 }
367 }
368 }
369 break;
370 default:
371 break;
372 }
373 /* Final merger cleanup. */
374 for (i = 0; i < words; i++)
375 {
376 /* If one class is MEMORY, everything should be passed in
377 memory. */
378 if (classes[i] == X86_64_MEMORY_CLASS)
379 return 0;
380
381 /* The X86_64_SSEUP_CLASS should be always preceeded by
382 X86_64_SSE_CLASS. */
383 if (classes[i] == X86_64_SSEUP_CLASS
384 && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
385 classes[i] = X86_64_SSE_CLASS;
386
387 /* X86_64_X87UP_CLASS should be preceeded by X86_64_X87_CLASS. */
388 if (classes[i] == X86_64_X87UP_CLASS
389 && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
390 classes[i] = X86_64_SSE_CLASS;
391 }
392 return words;
393 }
394 break;
395 case TYPE_CODE_FLT:
396 switch (bytes)
397 {
398 case 4:
399 if (!(bit_offset % 64))
400 classes[0] = X86_64_SSESF_CLASS;
401 else
402 classes[0] = X86_64_SSE_CLASS;
403 return 1;
404 case 8:
405 classes[0] = X86_64_SSEDF_CLASS;
406 return 1;
407 case 16:
408 classes[0] = X86_64_X87_CLASS;
409 classes[1] = X86_64_X87UP_CLASS;
410 return 2;
411 }
412 break;
413 case TYPE_CODE_ENUM:
414 case TYPE_CODE_REF:
415 case TYPE_CODE_INT:
416 case TYPE_CODE_PTR:
417 switch (bytes)
418 {
419 case 1:
420 case 2:
421 case 4:
422 case 8:
423 if (bytes * 8 + bit_offset <= 32)
424 classes[0] = X86_64_INTEGERSI_CLASS;
425 else
426 classes[0] = X86_64_INTEGER_CLASS;
427 return 1;
428 case 16:
429 classes[0] = classes[1] = X86_64_INTEGER_CLASS;
430 return 2;
431 default:
432 break;
433 }
434 case TYPE_CODE_VOID:
435 return 0;
436 default: /* Avoid warning. */
437 break;
438 }
439 internal_error (__FILE__, __LINE__,
440 "classify_argument: unknown argument type");
441 }
442
443 /* Examine the argument and set *INT_NREGS and *SSE_NREGS to the
444 number of registers required based on the information passed in
445 CLASSES. Return 0 if parameter should be passed in memory. */
446
447 static int
448 examine_argument (enum x86_64_reg_class classes[MAX_CLASSES],
449 int n, int *int_nregs, int *sse_nregs)
450 {
451 *int_nregs = 0;
452 *sse_nregs = 0;
453 if (!n)
454 return 0;
455 for (n--; n >= 0; n--)
456 switch (classes[n])
457 {
458 case X86_64_INTEGER_CLASS:
459 case X86_64_INTEGERSI_CLASS:
460 (*int_nregs)++;
461 break;
462 case X86_64_SSE_CLASS:
463 case X86_64_SSESF_CLASS:
464 case X86_64_SSEDF_CLASS:
465 (*sse_nregs)++;
466 break;
467 case X86_64_NO_CLASS:
468 case X86_64_SSEUP_CLASS:
469 case X86_64_X87_CLASS:
470 case X86_64_X87UP_CLASS:
471 break;
472 case X86_64_MEMORY_CLASS:
473 internal_error (__FILE__, __LINE__,
474 "examine_argument: unexpected memory class");
475 }
476 return 1;
477 }
478
479 #define RET_INT_REGS 2
480 #define RET_SSE_REGS 2
481
482 /* Check if the structure in value_type is returned in registers or in
483 memory. If this function returns 1, GDB will call
484 STORE_STRUCT_RETURN and EXTRACT_STRUCT_VALUE_ADDRESS else
485 STORE_RETURN_VALUE and EXTRACT_RETURN_VALUE will be used. */
486
487 static int
488 x86_64_use_struct_convention (int gcc_p, struct type *value_type)
489 {
490 enum x86_64_reg_class class[MAX_CLASSES];
491 int n = classify_argument (value_type, class, 0);
492 int needed_intregs;
493 int needed_sseregs;
494
495 return (!n ||
496 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
497 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS);
498 }
499
500 /* Extract from an array REGBUF containing the (raw) register state, a
501 function return value of TYPE, and copy that, in virtual format,
502 into VALBUF. */
503
504 static void
505 x86_64_extract_return_value (struct type *type, struct regcache *regcache,
506 void *valbuf)
507 {
508 enum x86_64_reg_class class[MAX_CLASSES];
509 int n = classify_argument (type, class, 0);
510 int needed_intregs;
511 int needed_sseregs;
512 int intreg = 0;
513 int ssereg = 0;
514 int offset = 0;
515 int ret_int_r[RET_INT_REGS] = { X86_64_RAX_REGNUM, X86_64_RDX_REGNUM };
516 int ret_sse_r[RET_SSE_REGS] = { X86_64_XMM0_REGNUM, X86_64_XMM1_REGNUM };
517
518 if (!n ||
519 !examine_argument (class, n, &needed_intregs, &needed_sseregs) ||
520 needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS)
521 { /* memory class */
522 CORE_ADDR addr;
523 regcache_cooked_read (regcache, X86_64_RAX_REGNUM, &addr);
524 read_memory (addr, valbuf, TYPE_LENGTH (type));
525 return;
526 }
527 else
528 {
529 int i;
530 for (i = 0; i < n; i++)
531 {
532 switch (class[i])
533 {
534 case X86_64_NO_CLASS:
535 break;
536 case X86_64_INTEGER_CLASS:
537 regcache_cooked_read (regcache, ret_int_r[(intreg + 1) / 2],
538 (char *) valbuf + offset);
539 offset += 8;
540 intreg += 2;
541 break;
542 case X86_64_INTEGERSI_CLASS:
543 regcache_cooked_read_part (regcache, ret_int_r[intreg / 2],
544 0, 4, (char *) valbuf + offset);
545 offset += 8;
546 intreg++;
547 break;
548 case X86_64_SSEDF_CLASS:
549 case X86_64_SSESF_CLASS:
550 case X86_64_SSE_CLASS:
551 regcache_cooked_read_part (regcache,
552 ret_sse_r[(ssereg + 1) / 2], 0, 8,
553 (char *) valbuf + offset);
554 offset += 8;
555 ssereg += 2;
556 break;
557 case X86_64_SSEUP_CLASS:
558 regcache_cooked_read_part (regcache, ret_sse_r[ssereg / 2],
559 0, 8, (char *) valbuf + offset);
560 offset += 8;
561 ssereg++;
562 break;
563 case X86_64_X87_CLASS:
564 regcache_cooked_read_part (regcache, X86_64_ST0_REGNUM,
565 0, 8, (char *) valbuf + offset);
566 offset += 8;
567 break;
568 case X86_64_X87UP_CLASS:
569 regcache_cooked_read_part (regcache, X86_64_ST0_REGNUM,
570 8, 2, (char *) valbuf + offset);
571 offset += 8;
572 break;
573 case X86_64_MEMORY_CLASS:
574 default:
575 internal_error (__FILE__, __LINE__,
576 "Unexpected argument class");
577 }
578 }
579 }
580 }
581
582 #define INT_REGS 6
583 #define SSE_REGS 8
584
585 static CORE_ADDR
586 x86_64_push_arguments (struct regcache *regcache, int nargs,
587 struct value **args, CORE_ADDR sp)
588 {
589 int intreg = 0;
590 int ssereg = 0;
591 int i;
592 static int int_parameter_registers[INT_REGS] =
593 {
594 X86_64_RDI_REGNUM, 4, /* %rdi, %rsi */
595 X86_64_RDX_REGNUM, 2, /* %rdx, %rcx */
596 8, 9 /* %r8, %r9 */
597 };
598 /* %xmm0 - %xmm7 */
599 static int sse_parameter_registers[SSE_REGS] =
600 {
601 X86_64_XMM0_REGNUM + 0, X86_64_XMM1_REGNUM,
602 X86_64_XMM0_REGNUM + 2, X86_64_XMM0_REGNUM + 3,
603 X86_64_XMM0_REGNUM + 4, X86_64_XMM0_REGNUM + 5,
604 X86_64_XMM0_REGNUM + 6, X86_64_XMM0_REGNUM + 7,
605 };
606 int stack_values_count = 0;
607 int *stack_values;
608 stack_values = alloca (nargs * sizeof (int));
609 for (i = 0; i < nargs; i++)
610 {
611 enum x86_64_reg_class class[MAX_CLASSES];
612 int n = classify_argument (args[i]->type, class, 0);
613 int needed_intregs;
614 int needed_sseregs;
615
616 if (!n ||
617 !examine_argument (class, n, &needed_intregs, &needed_sseregs)
618 || intreg / 2 + needed_intregs > INT_REGS
619 || ssereg / 2 + needed_sseregs > SSE_REGS)
620 { /* memory class */
621 stack_values[stack_values_count++] = i;
622 }
623 else
624 {
625 int j;
626 for (j = 0; j < n; j++)
627 {
628 int offset = 0;
629 switch (class[j])
630 {
631 case X86_64_NO_CLASS:
632 break;
633 case X86_64_INTEGER_CLASS:
634 regcache_cooked_write
635 (regcache, int_parameter_registers[(intreg + 1) / 2],
636 VALUE_CONTENTS_ALL (args[i]) + offset);
637 offset += 8;
638 intreg += 2;
639 break;
640 case X86_64_INTEGERSI_CLASS:
641 {
642 LONGEST val = extract_signed_integer
643 (VALUE_CONTENTS_ALL (args[i]) + offset, 4);
644 regcache_cooked_write_signed
645 (regcache, int_parameter_registers[intreg / 2], val);
646
647 offset += 8;
648 intreg++;
649 break;
650 }
651 case X86_64_SSEDF_CLASS:
652 case X86_64_SSESF_CLASS:
653 case X86_64_SSE_CLASS:
654 regcache_cooked_write
655 (regcache, sse_parameter_registers[(ssereg + 1) / 2],
656 VALUE_CONTENTS_ALL (args[i]) + offset);
657 offset += 8;
658 ssereg += 2;
659 break;
660 case X86_64_SSEUP_CLASS:
661 regcache_cooked_write
662 (regcache, sse_parameter_registers[ssereg / 2],
663 VALUE_CONTENTS_ALL (args[i]) + offset);
664 offset += 8;
665 ssereg++;
666 break;
667 case X86_64_X87_CLASS:
668 case X86_64_MEMORY_CLASS:
669 stack_values[stack_values_count++] = i;
670 break;
671 case X86_64_X87UP_CLASS:
672 break;
673 default:
674 internal_error (__FILE__, __LINE__,
675 "Unexpected argument class");
676 }
677 intreg += intreg % 2;
678 ssereg += ssereg % 2;
679 }
680 }
681 }
682
683 /* Push any remaining arguments onto the stack. */
684 while (--stack_values_count >= 0)
685 {
686 struct value *arg = args[stack_values[stack_values_count]];
687 int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg));
688
689 /* Make sure the stack stays eightbyte-aligned. */
690 sp -= (len + 7) & ~7;
691 write_memory (sp, VALUE_CONTENTS_ALL (arg), len);
692 }
693
694 return sp;
695 }
696
697 /* Write into the appropriate registers a function return value stored
698 in VALBUF of type TYPE, given in virtual format. */
699
700 static void
701 x86_64_store_return_value (struct type *type, struct regcache *regcache,
702 const void *valbuf)
703 {
704 int len = TYPE_LENGTH (type);
705
706 if (TYPE_CODE_FLT == TYPE_CODE (type))
707 {
708 ULONGEST fstat;
709 char buf[FPU_REG_RAW_SIZE];
710
711 /* Returning floating-point values is a bit tricky. Apart from
712 storing the return value in %st(0), we have to simulate the
713 state of the FPU at function return point. */
714
715 /* Convert the value found in VALBUF to the extended
716 floating-point format used by the FPU. This is probably
717 not exactly how it would happen on the target itself, but
718 it is the best we can do. */
719 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
720 regcache_raw_write (regcache, X86_64_ST0_REGNUM, buf);
721
722 /* Set the top of the floating-point register stack to 7. The
723 actual value doesn't really matter, but 7 is what a normal
724 function return would end up with if the program started out
725 with a freshly initialized FPU. */
726 regcache_raw_read_unsigned (regcache, FSTAT_REGNUM, &fstat);
727 fstat |= (7 << 11);
728 regcache_raw_write_unsigned (regcache, FSTAT_REGNUM, fstat);
729
730 /* Mark %st(1) through %st(7) as empty. Since we set the top of
731 the floating-point register stack to 7, the appropriate value
732 for the tag word is 0x3fff. */
733 regcache_raw_write_unsigned (regcache, FTAG_REGNUM, 0x3fff);
734 }
735 else
736 {
737 int low_size = REGISTER_RAW_SIZE (0);
738 int high_size = REGISTER_RAW_SIZE (1);
739
740 if (len <= low_size)
741 regcache_cooked_write_part (regcache, 0, 0, len, valbuf);
742 else if (len <= (low_size + high_size))
743 {
744 regcache_cooked_write_part (regcache, 0, 0, low_size, valbuf);
745 regcache_cooked_write_part (regcache, 1, 0,
746 len - low_size,
747 (const char *) valbuf + low_size);
748 }
749 else
750 internal_error (__FILE__, __LINE__,
751 "Cannot store return value of %d bytes long.", len);
752 }
753 }
754 \f
755
756 static CORE_ADDR
757 x86_64_push_dummy_call (struct gdbarch *gdbarch, struct regcache *regcache,
758 CORE_ADDR dummy_addr, int nargs, struct value **args,
759 CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr)
760 {
761 char buf[8];
762
763 /* Pass arguments. */
764 sp = x86_64_push_arguments (regcache, nargs, args, sp);
765
766 /* Pass "hidden" argument". */
767 if (struct_return)
768 {
769 store_unsigned_integer (buf, 8, struct_addr);
770 regcache_cooked_write (regcache, X86_64_RDI_REGNUM, buf);
771 }
772
773 /* Store return address. */
774 sp -= 8;
775 store_unsigned_integer (buf, 8, dummy_addr);
776 write_memory (sp, buf, 8);
777
778 /* Finally, update the stack pointer... */
779 store_unsigned_integer (buf, 8, sp);
780 regcache_cooked_write (regcache, X86_64_RSP_REGNUM, buf);
781
782 /* ...and fake a frame pointer. */
783 regcache_cooked_write (regcache, X86_64_RBP_REGNUM, buf);
784
785 return sp;
786 }
787 \f
788
789 /* The maximum number of saved registers. This should include %rip. */
790 #define X86_64_NUM_SAVED_REGS X86_64_NUM_GREGS
791
792 struct x86_64_frame_cache
793 {
794 /* Base address. */
795 CORE_ADDR base;
796 CORE_ADDR sp_offset;
797 CORE_ADDR pc;
798
799 /* Saved registers. */
800 CORE_ADDR saved_regs[X86_64_NUM_SAVED_REGS];
801 CORE_ADDR saved_sp;
802
803 /* Do we have a frame? */
804 int frameless_p;
805 };
806
807 /* Allocate and initialize a frame cache. */
808
809 static struct x86_64_frame_cache *
810 x86_64_alloc_frame_cache (void)
811 {
812 struct x86_64_frame_cache *cache;
813 int i;
814
815 cache = FRAME_OBSTACK_ZALLOC (struct x86_64_frame_cache);
816
817 /* Base address. */
818 cache->base = 0;
819 cache->sp_offset = -8;
820 cache->pc = 0;
821
822 /* Saved registers. We initialize these to -1 since zero is a valid
823 offset (that's where %rbp is supposed to be stored). */
824 for (i = 0; i < X86_64_NUM_SAVED_REGS; i++)
825 cache->saved_regs[i] = -1;
826 cache->saved_sp = 0;
827
828 /* Frameless until proven otherwise. */
829 cache->frameless_p = 1;
830
831 return cache;
832 }
833
834 /* Do a limited analysis of the prologue at PC and update CACHE
835 accordingly. Bail out early if CURRENT_PC is reached. Return the
836 address where the analysis stopped.
837
838 We will handle only functions beginning with:
839
840 pushq %rbp 0x55
841 movq %rsp, %rbp 0x48 0x89 0xe5
842
843 Any function that doesn't start with this sequence will be assumed
844 to have no prologue and thus no valid frame pointer in %rbp. */
845
846 static CORE_ADDR
847 x86_64_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
848 struct x86_64_frame_cache *cache)
849 {
850 static unsigned char proto[3] = { 0x48, 0x89, 0xe5 };
851 unsigned char buf[3];
852 unsigned char op;
853
854 if (current_pc <= pc)
855 return current_pc;
856
857 op = read_memory_unsigned_integer (pc, 1);
858
859 if (op == 0x55) /* pushq %rbp */
860 {
861 /* Take into account that we've executed the `pushq %rbp' that
862 starts this instruction sequence. */
863 cache->saved_regs[X86_64_RBP_REGNUM] = 0;
864 cache->sp_offset += 8;
865
866 /* If that's all, return now. */
867 if (current_pc <= pc + 1)
868 return current_pc;
869
870 /* Check for `movq %rsp, %rbp'. */
871 read_memory (pc + 1, buf, 3);
872 if (memcmp (buf, proto, 3) != 0)
873 return pc + 1;
874
875 /* OK, we actually have a frame. */
876 cache->frameless_p = 0;
877 return pc + 4;
878 }
879
880 return pc;
881 }
882
883 /* Return PC of first real instruction. */
884
885 static CORE_ADDR
886 x86_64_skip_prologue (CORE_ADDR start_pc)
887 {
888 struct x86_64_frame_cache cache;
889 CORE_ADDR pc;
890
891 pc = x86_64_analyze_prologue (start_pc, 0xffffffffffffffff, &cache);
892 if (cache.frameless_p)
893 return start_pc;
894
895 return pc;
896 }
897 \f
898
899 /* Normal frames. */
900
901 static struct x86_64_frame_cache *
902 x86_64_frame_cache (struct frame_info *next_frame, void **this_cache)
903 {
904 struct x86_64_frame_cache *cache;
905 char buf[8];
906 int i;
907
908 if (*this_cache)
909 return *this_cache;
910
911 cache = x86_64_alloc_frame_cache ();
912 *this_cache = cache;
913
914 frame_unwind_register (next_frame, X86_64_RBP_REGNUM, buf);
915 cache->base = extract_unsigned_integer (buf, 8);
916 if (cache->base == 0)
917 return cache;
918
919 /* For normal frames, %rip is stored at 8(%rbp). */
920 cache->saved_regs[X86_64_RIP_REGNUM] = 8;
921
922 cache->pc = frame_func_unwind (next_frame);
923 if (cache->pc != 0)
924 x86_64_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
925
926 if (cache->frameless_p)
927 {
928 /* We didn't find a valid frame, which means that CACHE->base
929 currently holds the frame pointer for our calling frame. If
930 we're at the start of a function, or somewhere half-way its
931 prologue, the function's frame probably hasn't been fully
932 setup yet. Try to reconstruct the base address for the stack
933 frame by looking at the stack pointer. For truly "frameless"
934 functions this might work too. */
935
936 frame_unwind_register (next_frame, X86_64_RSP_REGNUM, buf);
937 cache->base = extract_unsigned_integer (buf, 8) + cache->sp_offset;
938 }
939
940 /* Now that we have the base address for the stack frame we can
941 calculate the value of %rsp in the calling frame. */
942 cache->saved_sp = cache->base + 16;
943
944 /* Adjust all the saved registers such that they contain addresses
945 instead of offsets. */
946 for (i = 0; i < X86_64_NUM_SAVED_REGS; i++)
947 if (cache->saved_regs[i] != -1)
948 cache->saved_regs[i] += cache->base;
949
950 return cache;
951 }
952
953 static void
954 x86_64_frame_this_id (struct frame_info *next_frame, void **this_cache,
955 struct frame_id *this_id)
956 {
957 struct x86_64_frame_cache *cache =
958 x86_64_frame_cache (next_frame, this_cache);
959
960 /* This marks the outermost frame. */
961 if (cache->base == 0)
962 return;
963
964 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
965 }
966
967 static void
968 x86_64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
969 int regnum, int *optimizedp,
970 enum lval_type *lvalp, CORE_ADDR *addrp,
971 int *realnump, void *valuep)
972 {
973 struct x86_64_frame_cache *cache =
974 x86_64_frame_cache (next_frame, this_cache);
975
976 gdb_assert (regnum >= 0);
977
978 if (regnum == SP_REGNUM && cache->saved_sp)
979 {
980 *optimizedp = 0;
981 *lvalp = not_lval;
982 *addrp = 0;
983 *realnump = -1;
984 if (valuep)
985 {
986 /* Store the value. */
987 store_unsigned_integer (valuep, 8, cache->saved_sp);
988 }
989 return;
990 }
991
992 if (regnum < X86_64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
993 {
994 *optimizedp = 0;
995 *lvalp = lval_memory;
996 *addrp = cache->saved_regs[regnum];
997 *realnump = -1;
998 if (valuep)
999 {
1000 /* Read the value in from memory. */
1001 read_memory (*addrp, valuep,
1002 register_size (current_gdbarch, regnum));
1003 }
1004 return;
1005 }
1006
1007 frame_register_unwind (next_frame, regnum,
1008 optimizedp, lvalp, addrp, realnump, valuep);
1009 }
1010
1011 static const struct frame_unwind x86_64_frame_unwind =
1012 {
1013 NORMAL_FRAME,
1014 x86_64_frame_this_id,
1015 x86_64_frame_prev_register
1016 };
1017
1018 static const struct frame_unwind *
1019 x86_64_frame_p (CORE_ADDR pc)
1020 {
1021 return &x86_64_frame_unwind;
1022 }
1023 \f
1024
1025 /* Signal trampolines. */
1026
1027 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
1028 64-bit variants. This would require using identical frame caches
1029 on both platforms. */
1030
1031 static struct x86_64_frame_cache *
1032 x86_64_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
1033 {
1034 struct x86_64_frame_cache *cache;
1035 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1036 CORE_ADDR addr;
1037 char buf[8];
1038 int i;
1039
1040 if (*this_cache)
1041 return *this_cache;
1042
1043 cache = x86_64_alloc_frame_cache ();
1044
1045 frame_unwind_register (next_frame, X86_64_RSP_REGNUM, buf);
1046 cache->base = extract_unsigned_integer (buf, 8) - 8;
1047
1048 addr = tdep->sigcontext_addr (next_frame);
1049 gdb_assert (tdep->sc_reg_offset);
1050 gdb_assert (tdep->sc_num_regs <= X86_64_NUM_SAVED_REGS);
1051 for (i = 0; i < tdep->sc_num_regs; i++)
1052 if (tdep->sc_reg_offset[i] != -1)
1053 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
1054
1055 *this_cache = cache;
1056 return cache;
1057 }
1058
1059 static void
1060 x86_64_sigtramp_frame_this_id (struct frame_info *next_frame,
1061 void **this_cache, struct frame_id *this_id)
1062 {
1063 struct x86_64_frame_cache *cache =
1064 x86_64_sigtramp_frame_cache (next_frame, this_cache);
1065
1066 (*this_id) = frame_id_build (cache->base + 16, frame_pc_unwind (next_frame));
1067 }
1068
1069 static void
1070 x86_64_sigtramp_frame_prev_register (struct frame_info *next_frame,
1071 void **this_cache,
1072 int regnum, int *optimizedp,
1073 enum lval_type *lvalp, CORE_ADDR *addrp,
1074 int *realnump, void *valuep)
1075 {
1076 /* Make sure we've initialized the cache. */
1077 x86_64_sigtramp_frame_cache (next_frame, this_cache);
1078
1079 x86_64_frame_prev_register (next_frame, this_cache, regnum,
1080 optimizedp, lvalp, addrp, realnump, valuep);
1081 }
1082
1083 static const struct frame_unwind x86_64_sigtramp_frame_unwind =
1084 {
1085 SIGTRAMP_FRAME,
1086 x86_64_sigtramp_frame_this_id,
1087 x86_64_sigtramp_frame_prev_register
1088 };
1089
1090 static const struct frame_unwind *
1091 x86_64_sigtramp_frame_p (CORE_ADDR pc)
1092 {
1093 char *name;
1094
1095 find_pc_partial_function (pc, &name, NULL, NULL);
1096 if (PC_IN_SIGTRAMP (pc, name))
1097 return &x86_64_sigtramp_frame_unwind;
1098
1099 return NULL;
1100 }
1101 \f
1102
1103 static CORE_ADDR
1104 x86_64_frame_base_address (struct frame_info *next_frame, void **this_cache)
1105 {
1106 struct x86_64_frame_cache *cache =
1107 x86_64_frame_cache (next_frame, this_cache);
1108
1109 return cache->base;
1110 }
1111
1112 static const struct frame_base x86_64_frame_base =
1113 {
1114 &x86_64_frame_unwind,
1115 x86_64_frame_base_address,
1116 x86_64_frame_base_address,
1117 x86_64_frame_base_address
1118 };
1119
1120 static void
1121 x86_64_save_dummy_frame_tos (CORE_ADDR sp)
1122 {
1123 generic_save_dummy_frame_tos (sp + 16);
1124 }
1125
1126 static struct frame_id
1127 x86_64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1128 {
1129 char buf[8];
1130 CORE_ADDR fp;
1131
1132 frame_unwind_register (next_frame, X86_64_RBP_REGNUM, buf);
1133 fp = extract_unsigned_integer (buf, 8);
1134
1135 return frame_id_build (fp + 16, frame_pc_unwind (next_frame));
1136 }
1137
1138 void
1139 x86_64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1140 {
1141 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1142
1143 /* The x86-64 has 16 SSE registers. */
1144 tdep->num_xmm_regs = 16;
1145
1146 /* This is what all the fuss is about. */
1147 set_gdbarch_long_bit (gdbarch, 64);
1148 set_gdbarch_long_long_bit (gdbarch, 64);
1149 set_gdbarch_ptr_bit (gdbarch, 64);
1150
1151 /* In contrast to the i386, on the x86-64 a `long double' actually
1152 takes up 128 bits, even though it's still based on the i387
1153 extended floating-point format which has only 80 significant bits. */
1154 set_gdbarch_long_double_bit (gdbarch, 128);
1155
1156 set_gdbarch_num_regs (gdbarch, X86_64_NUM_REGS);
1157 set_gdbarch_register_name (gdbarch, x86_64_register_name);
1158 set_gdbarch_register_type (gdbarch, x86_64_register_type);
1159
1160 /* Register numbers of various important registers. */
1161 set_gdbarch_sp_regnum (gdbarch, X86_64_RSP_REGNUM); /* %rsp */
1162 set_gdbarch_pc_regnum (gdbarch, X86_64_RIP_REGNUM); /* %rip */
1163 set_gdbarch_ps_regnum (gdbarch, X86_64_EFLAGS_REGNUM); /* %eflags */
1164 set_gdbarch_fp0_regnum (gdbarch, X86_64_ST0_REGNUM); /* %st(0) */
1165
1166 /* The "default" register numbering scheme for the x86-64 is
1167 referred to as the "DWARF Register Number Mapping" in the System
1168 V psABI. The preferred debugging format for all known x86-64
1169 targets is actually DWARF2, and GCC doesn't seem to support DWARF
1170 (that is DWARF-1), but we provide the same mapping just in case.
1171 This mapping is also used for stabs, which GCC does support. */
1172 set_gdbarch_stab_reg_to_regnum (gdbarch, x86_64_dwarf_reg_to_regnum);
1173 set_gdbarch_dwarf_reg_to_regnum (gdbarch, x86_64_dwarf_reg_to_regnum);
1174 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, x86_64_dwarf_reg_to_regnum);
1175
1176 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
1177 be in use on any of the supported x86-64 targets. */
1178
1179 /* Call dummy code. */
1180 set_gdbarch_push_dummy_call (gdbarch, x86_64_push_dummy_call);
1181
1182 set_gdbarch_extract_return_value (gdbarch, x86_64_extract_return_value);
1183 set_gdbarch_store_return_value (gdbarch, x86_64_store_return_value);
1184 /* Override, since this is handled by x86_64_extract_return_value. */
1185 set_gdbarch_extract_struct_value_address (gdbarch, NULL);
1186 set_gdbarch_use_struct_convention (gdbarch, x86_64_use_struct_convention);
1187
1188 set_gdbarch_skip_prologue (gdbarch, x86_64_skip_prologue);
1189
1190 /* Avoid wiring in the MMX registers for now. */
1191 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1192
1193 set_gdbarch_unwind_dummy_id (gdbarch, x86_64_unwind_dummy_id);
1194 set_gdbarch_save_dummy_frame_tos (gdbarch, x86_64_save_dummy_frame_tos);
1195
1196 /* FIXME: kettenis/20021026: This is ELF-specific. Fine for now,
1197 since all supported x86-64 targets are ELF, but that might change
1198 in the future. */
1199 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1200
1201 frame_unwind_append_predicate (gdbarch, x86_64_sigtramp_frame_p);
1202 frame_unwind_append_predicate (gdbarch, x86_64_frame_p);
1203 frame_base_set_default (gdbarch, &x86_64_frame_base);
1204 }
1205 \f
1206
1207 #define I387_FISEG_REGNUM FISEG_REGNUM
1208 #define I387_FOSEG_REGNUM FOSEG_REGNUM
1209
1210 /* The 64-bit FXSAVE format differs from the 32-bit format in the
1211 sense that the instruction pointer and data pointer are simply
1212 64-bit offsets into the code segment and the data segment instead
1213 of a selector offset pair. The functions below store the upper 32
1214 bits of these pointers (instead of just the 16-bits of the segment
1215 selector). */
1216
1217 /* Fill GDB's register array with the floating-point and SSE register
1218 values in *FXSAVE. This function masks off any of the reserved
1219 bits in *FXSAVE. */
1220
1221 void
1222 x86_64_supply_fxsave (char *fxsave)
1223 {
1224 i387_supply_fxsave (fxsave);
1225
1226 if (fxsave)
1227 {
1228 supply_register (I387_FISEG_REGNUM, fxsave + 12);
1229 supply_register (I387_FOSEG_REGNUM, fxsave + 20);
1230 }
1231 }
1232
1233 /* Fill register REGNUM (if it is a floating-point or SSE register) in
1234 *FXSAVE with the value in GDB's register array. If REGNUM is -1, do
1235 this for all registers. This function doesn't touch any of the
1236 reserved bits in *FXSAVE. */
1237
1238 void
1239 x86_64_fill_fxsave (char *fxsave, int regnum)
1240 {
1241 i387_fill_fxsave (fxsave, regnum);
1242
1243 if (regnum == -1 || regnum == I387_FISEG_REGNUM)
1244 regcache_collect (regnum, fxsave + 12);
1245 if (regnum == -1 || regnum == I387_FOSEG_REGNUM)
1246 regcache_collect (regnum, fxsave + 20);
1247 }
This page took 0.108063 seconds and 4 git commands to generate.