531217436c3c996ad8c123e5d43cfcad0b24940b
[binutils-gdb.git] / gdb / amd64-tdep.c
1 /* Target-dependent code for AMD64.
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 Contributed by Jiri Smid, SuSE Labs.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
24
25 #include "defs.h"
26 #include "arch-utils.h"
27 #include "block.h"
28 #include "dummy-frame.h"
29 #include "frame.h"
30 #include "frame-base.h"
31 #include "frame-unwind.h"
32 #include "inferior.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "objfiles.h"
36 #include "regcache.h"
37 #include "regset.h"
38 #include "symfile.h"
39
40 #include "gdb_assert.h"
41
42 #include "amd64-tdep.h"
43 #include "i387-tdep.h"
44
45 /* Note that the AMD64 architecture was previously known as x86-64.
46 The latter is (forever) engraved into the canonical system name as
47 returned by config.guess, and used as the name for the AMD64 port
48 of GNU/Linux. The BSD's have renamed their ports to amd64; they
49 don't like to shout. For GDB we prefer the amd64_-prefix over the
50 x86_64_-prefix since it's so much easier to type. */
51
52 /* Register information. */
53
54 static const char *amd64_register_names[] =
55 {
56 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
57
58 /* %r8 is indeed register number 8. */
59 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
60 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
61
62 /* %st0 is register number 24. */
63 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
64 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
65
66 /* %xmm0 is register number 40. */
67 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
68 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
69 "mxcsr",
70 };
71
72 /* Total number of registers. */
73 #define AMD64_NUM_REGS ARRAY_SIZE (amd64_register_names)
74
75 /* Return the name of register REGNUM. */
76
77 const char *
78 amd64_register_name (int regnum)
79 {
80 if (regnum >= 0 && regnum < AMD64_NUM_REGS)
81 return amd64_register_names[regnum];
82
83 return NULL;
84 }
85
86 /* Return the GDB type object for the "standard" data type of data in
87 register REGNUM. */
88
89 struct type *
90 amd64_register_type (struct gdbarch *gdbarch, int regnum)
91 {
92 if (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_RDI_REGNUM)
93 return builtin_type_int64;
94 if (regnum == AMD64_RBP_REGNUM || regnum == AMD64_RSP_REGNUM)
95 return builtin_type_void_data_ptr;
96 if (regnum >= AMD64_R8_REGNUM && regnum <= AMD64_R15_REGNUM)
97 return builtin_type_int64;
98 if (regnum == AMD64_RIP_REGNUM)
99 return builtin_type_void_func_ptr;
100 if (regnum == AMD64_EFLAGS_REGNUM)
101 return i386_eflags_type;
102 if (regnum >= AMD64_CS_REGNUM && regnum <= AMD64_GS_REGNUM)
103 return builtin_type_int32;
104 if (regnum >= AMD64_ST0_REGNUM && regnum <= AMD64_ST0_REGNUM + 7)
105 return builtin_type_i387_ext;
106 if (regnum >= AMD64_FCTRL_REGNUM && regnum <= AMD64_FCTRL_REGNUM + 7)
107 return builtin_type_int32;
108 if (regnum >= AMD64_XMM0_REGNUM && regnum <= AMD64_XMM0_REGNUM + 15)
109 return i386_sse_type (gdbarch);
110 if (regnum == AMD64_MXCSR_REGNUM)
111 return i386_mxcsr_type;
112
113 internal_error (__FILE__, __LINE__, _("invalid regnum"));
114 }
115
116 /* DWARF Register Number Mapping as defined in the System V psABI,
117 section 3.6. */
118
119 static int amd64_dwarf_regmap[] =
120 {
121 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
122 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
123 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
124 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
125
126 /* Frame Pointer Register RBP. */
127 AMD64_RBP_REGNUM,
128
129 /* Stack Pointer Register RSP. */
130 AMD64_RSP_REGNUM,
131
132 /* Extended Integer Registers 8 - 15. */
133 8, 9, 10, 11, 12, 13, 14, 15,
134
135 /* Return Address RA. Mapped to RIP. */
136 AMD64_RIP_REGNUM,
137
138 /* SSE Registers 0 - 7. */
139 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
140 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
141 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
142 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
143
144 /* Extended SSE Registers 8 - 15. */
145 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
146 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
147 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
148 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
149
150 /* Floating Point Registers 0-7. */
151 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
152 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
153 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
154 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
155
156 /* Control and Status Flags Register. */
157 AMD64_EFLAGS_REGNUM,
158
159 /* Selector Registers. */
160 AMD64_ES_REGNUM,
161 AMD64_CS_REGNUM,
162 AMD64_SS_REGNUM,
163 AMD64_DS_REGNUM,
164 AMD64_FS_REGNUM,
165 AMD64_GS_REGNUM,
166 -1,
167 -1,
168
169 /* Segment Base Address Registers. */
170 -1,
171 -1,
172 -1,
173 -1,
174
175 /* Special Selector Registers. */
176 -1,
177 -1,
178
179 /* Floating Point Control Registers. */
180 AMD64_MXCSR_REGNUM,
181 AMD64_FCTRL_REGNUM,
182 AMD64_FSTAT_REGNUM
183 };
184
185 static const int amd64_dwarf_regmap_len =
186 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
187
188 /* Convert DWARF register number REG to the appropriate register
189 number used by GDB. */
190
191 static int
192 amd64_dwarf_reg_to_regnum (int reg)
193 {
194 int regnum = -1;
195
196 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
197 regnum = amd64_dwarf_regmap[reg];
198
199 if (regnum == -1)
200 warning (_("Unmapped DWARF Register #%d encountered."), reg);
201
202 return regnum;
203 }
204
205 /* Return nonzero if a value of type TYPE stored in register REGNUM
206 needs any special handling. */
207
208 static int
209 amd64_convert_register_p (int regnum, struct type *type)
210 {
211 return i386_fp_regnum_p (regnum);
212 }
213 \f
214
215 /* Register classes as defined in the psABI. */
216
217 enum amd64_reg_class
218 {
219 AMD64_INTEGER,
220 AMD64_SSE,
221 AMD64_SSEUP,
222 AMD64_X87,
223 AMD64_X87UP,
224 AMD64_COMPLEX_X87,
225 AMD64_NO_CLASS,
226 AMD64_MEMORY
227 };
228
229 /* Return the union class of CLASS1 and CLASS2. See the psABI for
230 details. */
231
232 static enum amd64_reg_class
233 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
234 {
235 /* Rule (a): If both classes are equal, this is the resulting class. */
236 if (class1 == class2)
237 return class1;
238
239 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
240 is the other class. */
241 if (class1 == AMD64_NO_CLASS)
242 return class2;
243 if (class2 == AMD64_NO_CLASS)
244 return class1;
245
246 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
247 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
248 return AMD64_MEMORY;
249
250 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
251 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
252 return AMD64_INTEGER;
253
254 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
255 MEMORY is used as class. */
256 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
257 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
258 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
259 return AMD64_MEMORY;
260
261 /* Rule (f): Otherwise class SSE is used. */
262 return AMD64_SSE;
263 }
264
265 static void amd64_classify (struct type *type, enum amd64_reg_class class[2]);
266
267 /* Return non-zero if TYPE is a non-POD structure or union type. */
268
269 static int
270 amd64_non_pod_p (struct type *type)
271 {
272 /* ??? A class with a base class certainly isn't POD, but does this
273 catch all non-POD structure types? */
274 if (TYPE_CODE (type) == TYPE_CODE_STRUCT && TYPE_N_BASECLASSES (type) > 0)
275 return 1;
276
277 return 0;
278 }
279
280 /* Classify TYPE according to the rules for aggregate (structures and
281 arrays) and union types, and store the result in CLASS. */
282
283 static void
284 amd64_classify_aggregate (struct type *type, enum amd64_reg_class class[2])
285 {
286 int len = TYPE_LENGTH (type);
287
288 /* 1. If the size of an object is larger than two eightbytes, or in
289 C++, is a non-POD structure or union type, or contains
290 unaligned fields, it has class memory. */
291 if (len > 16 || amd64_non_pod_p (type))
292 {
293 class[0] = class[1] = AMD64_MEMORY;
294 return;
295 }
296
297 /* 2. Both eightbytes get initialized to class NO_CLASS. */
298 class[0] = class[1] = AMD64_NO_CLASS;
299
300 /* 3. Each field of an object is classified recursively so that
301 always two fields are considered. The resulting class is
302 calculated according to the classes of the fields in the
303 eightbyte: */
304
305 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
306 {
307 struct type *subtype = check_typedef (TYPE_TARGET_TYPE (type));
308
309 /* All fields in an array have the same type. */
310 amd64_classify (subtype, class);
311 if (len > 8 && class[1] == AMD64_NO_CLASS)
312 class[1] = class[0];
313 }
314 else
315 {
316 int i;
317
318 /* Structure or union. */
319 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
320 || TYPE_CODE (type) == TYPE_CODE_UNION);
321
322 for (i = 0; i < TYPE_NFIELDS (type); i++)
323 {
324 struct type *subtype = check_typedef (TYPE_FIELD_TYPE (type, i));
325 int pos = TYPE_FIELD_BITPOS (type, i) / 64;
326 enum amd64_reg_class subclass[2];
327
328 /* Ignore static fields. */
329 if (TYPE_FIELD_STATIC (type, i))
330 continue;
331
332 gdb_assert (pos == 0 || pos == 1);
333
334 amd64_classify (subtype, subclass);
335 class[pos] = amd64_merge_classes (class[pos], subclass[0]);
336 if (pos == 0)
337 class[1] = amd64_merge_classes (class[1], subclass[1]);
338 }
339 }
340
341 /* 4. Then a post merger cleanup is done: */
342
343 /* Rule (a): If one of the classes is MEMORY, the whole argument is
344 passed in memory. */
345 if (class[0] == AMD64_MEMORY || class[1] == AMD64_MEMORY)
346 class[0] = class[1] = AMD64_MEMORY;
347
348 /* Rule (b): If SSEUP is not preceeded by SSE, it is converted to
349 SSE. */
350 if (class[0] == AMD64_SSEUP)
351 class[0] = AMD64_SSE;
352 if (class[1] == AMD64_SSEUP && class[0] != AMD64_SSE)
353 class[1] = AMD64_SSE;
354 }
355
356 /* Classify TYPE, and store the result in CLASS. */
357
358 static void
359 amd64_classify (struct type *type, enum amd64_reg_class class[2])
360 {
361 enum type_code code = TYPE_CODE (type);
362 int len = TYPE_LENGTH (type);
363
364 class[0] = class[1] = AMD64_NO_CLASS;
365
366 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
367 long, long long, and pointers are in the INTEGER class. Similarly,
368 range types, used by languages such as Ada, are also in the INTEGER
369 class. */
370 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
371 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
372 || code == TYPE_CODE_PTR || code == TYPE_CODE_REF)
373 && (len == 1 || len == 2 || len == 4 || len == 8))
374 class[0] = AMD64_INTEGER;
375
376 /* Arguments of types float, double and __m64 are in class SSE. */
377 else if (code == TYPE_CODE_FLT && (len == 4 || len == 8))
378 /* FIXME: __m64 . */
379 class[0] = AMD64_SSE;
380
381 /* Arguments of types __float128 and __m128 are split into two
382 halves. The least significant ones belong to class SSE, the most
383 significant one to class SSEUP. */
384 /* FIXME: __float128, __m128. */
385
386 /* The 64-bit mantissa of arguments of type long double belongs to
387 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
388 class X87UP. */
389 else if (code == TYPE_CODE_FLT && len == 16)
390 /* Class X87 and X87UP. */
391 class[0] = AMD64_X87, class[1] = AMD64_X87UP;
392
393 /* Aggregates. */
394 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
395 || code == TYPE_CODE_UNION)
396 amd64_classify_aggregate (type, class);
397 }
398
399 static enum return_value_convention
400 amd64_return_value (struct gdbarch *gdbarch, struct type *type,
401 struct regcache *regcache,
402 gdb_byte *readbuf, const gdb_byte *writebuf)
403 {
404 enum amd64_reg_class class[2];
405 int len = TYPE_LENGTH (type);
406 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
407 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
408 int integer_reg = 0;
409 int sse_reg = 0;
410 int i;
411
412 gdb_assert (!(readbuf && writebuf));
413
414 /* 1. Classify the return type with the classification algorithm. */
415 amd64_classify (type, class);
416
417 /* 2. If the type has class MEMORY, then the caller provides space
418 for the return value and passes the address of this storage in
419 %rdi as if it were the first argument to the function. In effect,
420 this address becomes a hidden first argument.
421
422 On return %rax will contain the address that has been passed in
423 by the caller in %rdi. */
424 if (class[0] == AMD64_MEMORY)
425 {
426 /* As indicated by the comment above, the ABI guarantees that we
427 can always find the return value just after the function has
428 returned. */
429
430 if (readbuf)
431 {
432 ULONGEST addr;
433
434 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
435 read_memory (addr, readbuf, TYPE_LENGTH (type));
436 }
437
438 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
439 }
440
441 gdb_assert (class[1] != AMD64_MEMORY);
442 gdb_assert (len <= 16);
443
444 for (i = 0; len > 0; i++, len -= 8)
445 {
446 int regnum = -1;
447 int offset = 0;
448
449 switch (class[i])
450 {
451 case AMD64_INTEGER:
452 /* 3. If the class is INTEGER, the next available register
453 of the sequence %rax, %rdx is used. */
454 regnum = integer_regnum[integer_reg++];
455 break;
456
457 case AMD64_SSE:
458 /* 4. If the class is SSE, the next available SSE register
459 of the sequence %xmm0, %xmm1 is used. */
460 regnum = sse_regnum[sse_reg++];
461 break;
462
463 case AMD64_SSEUP:
464 /* 5. If the class is SSEUP, the eightbyte is passed in the
465 upper half of the last used SSE register. */
466 gdb_assert (sse_reg > 0);
467 regnum = sse_regnum[sse_reg - 1];
468 offset = 8;
469 break;
470
471 case AMD64_X87:
472 /* 6. If the class is X87, the value is returned on the X87
473 stack in %st0 as 80-bit x87 number. */
474 regnum = AMD64_ST0_REGNUM;
475 if (writebuf)
476 i387_return_value (gdbarch, regcache);
477 break;
478
479 case AMD64_X87UP:
480 /* 7. If the class is X87UP, the value is returned together
481 with the previous X87 value in %st0. */
482 gdb_assert (i > 0 && class[0] == AMD64_X87);
483 regnum = AMD64_ST0_REGNUM;
484 offset = 8;
485 len = 2;
486 break;
487
488 case AMD64_NO_CLASS:
489 continue;
490
491 default:
492 gdb_assert (!"Unexpected register class.");
493 }
494
495 gdb_assert (regnum != -1);
496
497 if (readbuf)
498 regcache_raw_read_part (regcache, regnum, offset, min (len, 8),
499 readbuf + i * 8);
500 if (writebuf)
501 regcache_raw_write_part (regcache, regnum, offset, min (len, 8),
502 writebuf + i * 8);
503 }
504
505 return RETURN_VALUE_REGISTER_CONVENTION;
506 }
507 \f
508
509 static CORE_ADDR
510 amd64_push_arguments (struct regcache *regcache, int nargs,
511 struct value **args, CORE_ADDR sp, int struct_return)
512 {
513 static int integer_regnum[] =
514 {
515 AMD64_RDI_REGNUM, /* %rdi */
516 AMD64_RSI_REGNUM, /* %rsi */
517 AMD64_RDX_REGNUM, /* %rdx */
518 AMD64_RCX_REGNUM, /* %rcx */
519 8, /* %r8 */
520 9 /* %r9 */
521 };
522 static int sse_regnum[] =
523 {
524 /* %xmm0 ... %xmm7 */
525 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
526 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
527 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
528 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
529 };
530 struct value **stack_args = alloca (nargs * sizeof (struct value *));
531 int num_stack_args = 0;
532 int num_elements = 0;
533 int element = 0;
534 int integer_reg = 0;
535 int sse_reg = 0;
536 int i;
537
538 /* Reserve a register for the "hidden" argument. */
539 if (struct_return)
540 integer_reg++;
541
542 for (i = 0; i < nargs; i++)
543 {
544 struct type *type = value_type (args[i]);
545 int len = TYPE_LENGTH (type);
546 enum amd64_reg_class class[2];
547 int needed_integer_regs = 0;
548 int needed_sse_regs = 0;
549 int j;
550
551 /* Classify argument. */
552 amd64_classify (type, class);
553
554 /* Calculate the number of integer and SSE registers needed for
555 this argument. */
556 for (j = 0; j < 2; j++)
557 {
558 if (class[j] == AMD64_INTEGER)
559 needed_integer_regs++;
560 else if (class[j] == AMD64_SSE)
561 needed_sse_regs++;
562 }
563
564 /* Check whether enough registers are available, and if the
565 argument should be passed in registers at all. */
566 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
567 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
568 || (needed_integer_regs == 0 && needed_sse_regs == 0))
569 {
570 /* The argument will be passed on the stack. */
571 num_elements += ((len + 7) / 8);
572 stack_args[num_stack_args++] = args[i];
573 }
574 else
575 {
576 /* The argument will be passed in registers. */
577 const gdb_byte *valbuf = value_contents (args[i]);
578 gdb_byte buf[8];
579
580 gdb_assert (len <= 16);
581
582 for (j = 0; len > 0; j++, len -= 8)
583 {
584 int regnum = -1;
585 int offset = 0;
586
587 switch (class[j])
588 {
589 case AMD64_INTEGER:
590 regnum = integer_regnum[integer_reg++];
591 break;
592
593 case AMD64_SSE:
594 regnum = sse_regnum[sse_reg++];
595 break;
596
597 case AMD64_SSEUP:
598 gdb_assert (sse_reg > 0);
599 regnum = sse_regnum[sse_reg - 1];
600 offset = 8;
601 break;
602
603 default:
604 gdb_assert (!"Unexpected register class.");
605 }
606
607 gdb_assert (regnum != -1);
608 memset (buf, 0, sizeof buf);
609 memcpy (buf, valbuf + j * 8, min (len, 8));
610 regcache_raw_write_part (regcache, regnum, offset, 8, buf);
611 }
612 }
613 }
614
615 /* Allocate space for the arguments on the stack. */
616 sp -= num_elements * 8;
617
618 /* The psABI says that "The end of the input argument area shall be
619 aligned on a 16 byte boundary." */
620 sp &= ~0xf;
621
622 /* Write out the arguments to the stack. */
623 for (i = 0; i < num_stack_args; i++)
624 {
625 struct type *type = value_type (stack_args[i]);
626 const gdb_byte *valbuf = value_contents (stack_args[i]);
627 int len = TYPE_LENGTH (type);
628
629 write_memory (sp + element * 8, valbuf, len);
630 element += ((len + 7) / 8);
631 }
632
633 /* The psABI says that "For calls that may call functions that use
634 varargs or stdargs (prototype-less calls or calls to functions
635 containing ellipsis (...) in the declaration) %al is used as
636 hidden argument to specify the number of SSE registers used. */
637 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
638 return sp;
639 }
640
641 static CORE_ADDR
642 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
643 struct regcache *regcache, CORE_ADDR bp_addr,
644 int nargs, struct value **args, CORE_ADDR sp,
645 int struct_return, CORE_ADDR struct_addr)
646 {
647 gdb_byte buf[8];
648
649 /* Pass arguments. */
650 sp = amd64_push_arguments (regcache, nargs, args, sp, struct_return);
651
652 /* Pass "hidden" argument". */
653 if (struct_return)
654 {
655 store_unsigned_integer (buf, 8, struct_addr);
656 regcache_cooked_write (regcache, AMD64_RDI_REGNUM, buf);
657 }
658
659 /* Store return address. */
660 sp -= 8;
661 store_unsigned_integer (buf, 8, bp_addr);
662 write_memory (sp, buf, 8);
663
664 /* Finally, update the stack pointer... */
665 store_unsigned_integer (buf, 8, sp);
666 regcache_cooked_write (regcache, AMD64_RSP_REGNUM, buf);
667
668 /* ...and fake a frame pointer. */
669 regcache_cooked_write (regcache, AMD64_RBP_REGNUM, buf);
670
671 return sp + 16;
672 }
673 \f
674
675 /* The maximum number of saved registers. This should include %rip. */
676 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
677
678 struct amd64_frame_cache
679 {
680 /* Base address. */
681 CORE_ADDR base;
682 CORE_ADDR sp_offset;
683 CORE_ADDR pc;
684
685 /* Saved registers. */
686 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
687 CORE_ADDR saved_sp;
688
689 /* Do we have a frame? */
690 int frameless_p;
691 };
692
693 /* Initialize a frame cache. */
694
695 static void
696 amd64_init_frame_cache (struct amd64_frame_cache *cache)
697 {
698 int i;
699
700 /* Base address. */
701 cache->base = 0;
702 cache->sp_offset = -8;
703 cache->pc = 0;
704
705 /* Saved registers. We initialize these to -1 since zero is a valid
706 offset (that's where %rbp is supposed to be stored). */
707 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
708 cache->saved_regs[i] = -1;
709 cache->saved_sp = 0;
710
711 /* Frameless until proven otherwise. */
712 cache->frameless_p = 1;
713 }
714
715 /* Allocate and initialize a frame cache. */
716
717 static struct amd64_frame_cache *
718 amd64_alloc_frame_cache (void)
719 {
720 struct amd64_frame_cache *cache;
721
722 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
723 amd64_init_frame_cache (cache);
724 return cache;
725 }
726
727 /* Do a limited analysis of the prologue at PC and update CACHE
728 accordingly. Bail out early if CURRENT_PC is reached. Return the
729 address where the analysis stopped.
730
731 We will handle only functions beginning with:
732
733 pushq %rbp 0x55
734 movq %rsp, %rbp 0x48 0x89 0xe5
735
736 Any function that doesn't start with this sequence will be assumed
737 to have no prologue and thus no valid frame pointer in %rbp. */
738
739 static CORE_ADDR
740 amd64_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
741 struct amd64_frame_cache *cache)
742 {
743 static gdb_byte proto[3] = { 0x48, 0x89, 0xe5 }; /* movq %rsp, %rbp */
744 gdb_byte buf[3];
745 gdb_byte op;
746
747 if (current_pc <= pc)
748 return current_pc;
749
750 op = read_memory_unsigned_integer (pc, 1);
751
752 if (op == 0x55) /* pushq %rbp */
753 {
754 /* Take into account that we've executed the `pushq %rbp' that
755 starts this instruction sequence. */
756 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
757 cache->sp_offset += 8;
758
759 /* If that's all, return now. */
760 if (current_pc <= pc + 1)
761 return current_pc;
762
763 /* Check for `movq %rsp, %rbp'. */
764 read_memory (pc + 1, buf, 3);
765 if (memcmp (buf, proto, 3) != 0)
766 return pc + 1;
767
768 /* OK, we actually have a frame. */
769 cache->frameless_p = 0;
770 return pc + 4;
771 }
772
773 return pc;
774 }
775
776 /* Return PC of first real instruction. */
777
778 static CORE_ADDR
779 amd64_skip_prologue (CORE_ADDR start_pc)
780 {
781 struct amd64_frame_cache cache;
782 CORE_ADDR pc;
783
784 amd64_init_frame_cache (&cache);
785 pc = amd64_analyze_prologue (start_pc, 0xffffffffffffffffLL, &cache);
786 if (cache.frameless_p)
787 return start_pc;
788
789 return pc;
790 }
791 \f
792
793 /* Normal frames. */
794
795 static struct amd64_frame_cache *
796 amd64_frame_cache (struct frame_info *next_frame, void **this_cache)
797 {
798 struct amd64_frame_cache *cache;
799 gdb_byte buf[8];
800 int i;
801
802 if (*this_cache)
803 return *this_cache;
804
805 cache = amd64_alloc_frame_cache ();
806 *this_cache = cache;
807
808 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
809 if (cache->pc != 0)
810 amd64_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
811
812 if (cache->frameless_p)
813 {
814 /* We didn't find a valid frame. If we're at the start of a
815 function, or somewhere half-way its prologue, the function's
816 frame probably hasn't been fully setup yet. Try to
817 reconstruct the base address for the stack frame by looking
818 at the stack pointer. For truly "frameless" functions this
819 might work too. */
820
821 frame_unwind_register (next_frame, AMD64_RSP_REGNUM, buf);
822 cache->base = extract_unsigned_integer (buf, 8) + cache->sp_offset;
823 }
824 else
825 {
826 frame_unwind_register (next_frame, AMD64_RBP_REGNUM, buf);
827 cache->base = extract_unsigned_integer (buf, 8);
828 }
829
830 /* Now that we have the base address for the stack frame we can
831 calculate the value of %rsp in the calling frame. */
832 cache->saved_sp = cache->base + 16;
833
834 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
835 frame we find it at the same offset from the reconstructed base
836 address. */
837 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
838
839 /* Adjust all the saved registers such that they contain addresses
840 instead of offsets. */
841 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
842 if (cache->saved_regs[i] != -1)
843 cache->saved_regs[i] += cache->base;
844
845 return cache;
846 }
847
848 static void
849 amd64_frame_this_id (struct frame_info *next_frame, void **this_cache,
850 struct frame_id *this_id)
851 {
852 struct amd64_frame_cache *cache =
853 amd64_frame_cache (next_frame, this_cache);
854
855 /* This marks the outermost frame. */
856 if (cache->base == 0)
857 return;
858
859 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
860 }
861
862 static void
863 amd64_frame_prev_register (struct frame_info *next_frame, void **this_cache,
864 int regnum, int *optimizedp,
865 enum lval_type *lvalp, CORE_ADDR *addrp,
866 int *realnump, gdb_byte *valuep)
867 {
868 struct amd64_frame_cache *cache =
869 amd64_frame_cache (next_frame, this_cache);
870
871 gdb_assert (regnum >= 0);
872
873 if (regnum == gdbarch_sp_regnum (current_gdbarch) && cache->saved_sp)
874 {
875 *optimizedp = 0;
876 *lvalp = not_lval;
877 *addrp = 0;
878 *realnump = -1;
879 if (valuep)
880 {
881 /* Store the value. */
882 store_unsigned_integer (valuep, 8, cache->saved_sp);
883 }
884 return;
885 }
886
887 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
888 {
889 *optimizedp = 0;
890 *lvalp = lval_memory;
891 *addrp = cache->saved_regs[regnum];
892 *realnump = -1;
893 if (valuep)
894 {
895 /* Read the value in from memory. */
896 read_memory (*addrp, valuep,
897 register_size (current_gdbarch, regnum));
898 }
899 return;
900 }
901
902 *optimizedp = 0;
903 *lvalp = lval_register;
904 *addrp = 0;
905 *realnump = regnum;
906 if (valuep)
907 frame_unwind_register (next_frame, (*realnump), valuep);
908 }
909
910 static const struct frame_unwind amd64_frame_unwind =
911 {
912 NORMAL_FRAME,
913 amd64_frame_this_id,
914 amd64_frame_prev_register
915 };
916
917 static const struct frame_unwind *
918 amd64_frame_sniffer (struct frame_info *next_frame)
919 {
920 return &amd64_frame_unwind;
921 }
922 \f
923
924 /* Signal trampolines. */
925
926 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
927 64-bit variants. This would require using identical frame caches
928 on both platforms. */
929
930 static struct amd64_frame_cache *
931 amd64_sigtramp_frame_cache (struct frame_info *next_frame, void **this_cache)
932 {
933 struct amd64_frame_cache *cache;
934 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
935 CORE_ADDR addr;
936 gdb_byte buf[8];
937 int i;
938
939 if (*this_cache)
940 return *this_cache;
941
942 cache = amd64_alloc_frame_cache ();
943
944 frame_unwind_register (next_frame, AMD64_RSP_REGNUM, buf);
945 cache->base = extract_unsigned_integer (buf, 8) - 8;
946
947 addr = tdep->sigcontext_addr (next_frame);
948 gdb_assert (tdep->sc_reg_offset);
949 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
950 for (i = 0; i < tdep->sc_num_regs; i++)
951 if (tdep->sc_reg_offset[i] != -1)
952 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
953
954 *this_cache = cache;
955 return cache;
956 }
957
958 static void
959 amd64_sigtramp_frame_this_id (struct frame_info *next_frame,
960 void **this_cache, struct frame_id *this_id)
961 {
962 struct amd64_frame_cache *cache =
963 amd64_sigtramp_frame_cache (next_frame, this_cache);
964
965 (*this_id) = frame_id_build (cache->base + 16, frame_pc_unwind (next_frame));
966 }
967
968 static void
969 amd64_sigtramp_frame_prev_register (struct frame_info *next_frame,
970 void **this_cache,
971 int regnum, int *optimizedp,
972 enum lval_type *lvalp, CORE_ADDR *addrp,
973 int *realnump, gdb_byte *valuep)
974 {
975 /* Make sure we've initialized the cache. */
976 amd64_sigtramp_frame_cache (next_frame, this_cache);
977
978 amd64_frame_prev_register (next_frame, this_cache, regnum,
979 optimizedp, lvalp, addrp, realnump, valuep);
980 }
981
982 static const struct frame_unwind amd64_sigtramp_frame_unwind =
983 {
984 SIGTRAMP_FRAME,
985 amd64_sigtramp_frame_this_id,
986 amd64_sigtramp_frame_prev_register
987 };
988
989 static const struct frame_unwind *
990 amd64_sigtramp_frame_sniffer (struct frame_info *next_frame)
991 {
992 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (next_frame));
993
994 /* We shouldn't even bother if we don't have a sigcontext_addr
995 handler. */
996 if (tdep->sigcontext_addr == NULL)
997 return NULL;
998
999 if (tdep->sigtramp_p != NULL)
1000 {
1001 if (tdep->sigtramp_p (next_frame))
1002 return &amd64_sigtramp_frame_unwind;
1003 }
1004
1005 if (tdep->sigtramp_start != 0)
1006 {
1007 CORE_ADDR pc = frame_pc_unwind (next_frame);
1008
1009 gdb_assert (tdep->sigtramp_end != 0);
1010 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
1011 return &amd64_sigtramp_frame_unwind;
1012 }
1013
1014 return NULL;
1015 }
1016 \f
1017
1018 static CORE_ADDR
1019 amd64_frame_base_address (struct frame_info *next_frame, void **this_cache)
1020 {
1021 struct amd64_frame_cache *cache =
1022 amd64_frame_cache (next_frame, this_cache);
1023
1024 return cache->base;
1025 }
1026
1027 static const struct frame_base amd64_frame_base =
1028 {
1029 &amd64_frame_unwind,
1030 amd64_frame_base_address,
1031 amd64_frame_base_address,
1032 amd64_frame_base_address
1033 };
1034
1035 static struct frame_id
1036 amd64_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1037 {
1038 gdb_byte buf[8];
1039 CORE_ADDR fp;
1040
1041 frame_unwind_register (next_frame, AMD64_RBP_REGNUM, buf);
1042 fp = extract_unsigned_integer (buf, 8);
1043
1044 return frame_id_build (fp + 16, frame_pc_unwind (next_frame));
1045 }
1046
1047 /* 16 byte align the SP per frame requirements. */
1048
1049 static CORE_ADDR
1050 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
1051 {
1052 return sp & -(CORE_ADDR)16;
1053 }
1054 \f
1055
1056 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
1057 in the floating-point register set REGSET to register cache
1058 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
1059
1060 static void
1061 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
1062 int regnum, const void *fpregs, size_t len)
1063 {
1064 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
1065
1066 gdb_assert (len == tdep->sizeof_fpregset);
1067 amd64_supply_fxsave (regcache, regnum, fpregs);
1068 }
1069
1070 /* Collect register REGNUM from the register cache REGCACHE and store
1071 it in the buffer specified by FPREGS and LEN as described by the
1072 floating-point register set REGSET. If REGNUM is -1, do this for
1073 all registers in REGSET. */
1074
1075 static void
1076 amd64_collect_fpregset (const struct regset *regset,
1077 const struct regcache *regcache,
1078 int regnum, void *fpregs, size_t len)
1079 {
1080 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
1081
1082 gdb_assert (len == tdep->sizeof_fpregset);
1083 amd64_collect_fxsave (regcache, regnum, fpregs);
1084 }
1085
1086 /* Return the appropriate register set for the core section identified
1087 by SECT_NAME and SECT_SIZE. */
1088
1089 static const struct regset *
1090 amd64_regset_from_core_section (struct gdbarch *gdbarch,
1091 const char *sect_name, size_t sect_size)
1092 {
1093 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1094
1095 if (strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
1096 {
1097 if (tdep->fpregset == NULL)
1098 tdep->fpregset = regset_alloc (gdbarch, amd64_supply_fpregset,
1099 amd64_collect_fpregset);
1100
1101 return tdep->fpregset;
1102 }
1103
1104 return i386_regset_from_core_section (gdbarch, sect_name, sect_size);
1105 }
1106 \f
1107
1108 void
1109 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1110 {
1111 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1112
1113 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
1114 floating-point registers. */
1115 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
1116
1117 /* AMD64 has an FPU and 16 SSE registers. */
1118 tdep->st0_regnum = AMD64_ST0_REGNUM;
1119 tdep->num_xmm_regs = 16;
1120
1121 /* This is what all the fuss is about. */
1122 set_gdbarch_long_bit (gdbarch, 64);
1123 set_gdbarch_long_long_bit (gdbarch, 64);
1124 set_gdbarch_ptr_bit (gdbarch, 64);
1125
1126 /* In contrast to the i386, on AMD64 a `long double' actually takes
1127 up 128 bits, even though it's still based on the i387 extended
1128 floating-point format which has only 80 significant bits. */
1129 set_gdbarch_long_double_bit (gdbarch, 128);
1130
1131 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
1132 set_gdbarch_register_name (gdbarch, amd64_register_name);
1133 set_gdbarch_register_type (gdbarch, amd64_register_type);
1134
1135 /* Register numbers of various important registers. */
1136 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
1137 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
1138 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
1139 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
1140
1141 /* The "default" register numbering scheme for AMD64 is referred to
1142 as the "DWARF Register Number Mapping" in the System V psABI.
1143 The preferred debugging format for all known AMD64 targets is
1144 actually DWARF2, and GCC doesn't seem to support DWARF (that is
1145 DWARF-1), but we provide the same mapping just in case. This
1146 mapping is also used for stabs, which GCC does support. */
1147 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1148 set_gdbarch_dwarf_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1149 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
1150
1151 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
1152 be in use on any of the supported AMD64 targets. */
1153
1154 /* Call dummy code. */
1155 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
1156 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
1157 set_gdbarch_frame_red_zone_size (gdbarch, 128);
1158
1159 set_gdbarch_convert_register_p (gdbarch, amd64_convert_register_p);
1160 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
1161 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
1162
1163 set_gdbarch_return_value (gdbarch, amd64_return_value);
1164
1165 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
1166
1167 /* Avoid wiring in the MMX registers for now. */
1168 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1169 tdep->mm0_regnum = -1;
1170
1171 set_gdbarch_unwind_dummy_id (gdbarch, amd64_unwind_dummy_id);
1172
1173 frame_unwind_append_sniffer (gdbarch, amd64_sigtramp_frame_sniffer);
1174 frame_unwind_append_sniffer (gdbarch, amd64_frame_sniffer);
1175 frame_base_set_default (gdbarch, &amd64_frame_base);
1176
1177 /* If we have a register mapping, enable the generic core file support. */
1178 if (tdep->gregset_reg_offset)
1179 set_gdbarch_regset_from_core_section (gdbarch,
1180 amd64_regset_from_core_section);
1181 }
1182 \f
1183
1184 #define I387_ST0_REGNUM AMD64_ST0_REGNUM
1185
1186 /* The 64-bit FXSAVE format differs from the 32-bit format in the
1187 sense that the instruction pointer and data pointer are simply
1188 64-bit offsets into the code segment and the data segment instead
1189 of a selector offset pair. The functions below store the upper 32
1190 bits of these pointers (instead of just the 16-bits of the segment
1191 selector). */
1192
1193 /* Fill register REGNUM in REGCACHE with the appropriate
1194 floating-point or SSE register value from *FXSAVE. If REGNUM is
1195 -1, do this for all registers. This function masks off any of the
1196 reserved bits in *FXSAVE. */
1197
1198 void
1199 amd64_supply_fxsave (struct regcache *regcache, int regnum,
1200 const void *fxsave)
1201 {
1202 i387_supply_fxsave (regcache, regnum, fxsave);
1203
1204 if (fxsave && gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1205 {
1206 const gdb_byte *regs = fxsave;
1207
1208 if (regnum == -1 || regnum == I387_FISEG_REGNUM)
1209 regcache_raw_supply (regcache, I387_FISEG_REGNUM, regs + 12);
1210 if (regnum == -1 || regnum == I387_FOSEG_REGNUM)
1211 regcache_raw_supply (regcache, I387_FOSEG_REGNUM, regs + 20);
1212 }
1213 }
1214
1215 /* Fill register REGNUM (if it is a floating-point or SSE register) in
1216 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
1217 all registers. This function doesn't touch any of the reserved
1218 bits in *FXSAVE. */
1219
1220 void
1221 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
1222 void *fxsave)
1223 {
1224 gdb_byte *regs = fxsave;
1225
1226 i387_collect_fxsave (regcache, regnum, fxsave);
1227
1228 if (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 64)
1229 {
1230 if (regnum == -1 || regnum == I387_FISEG_REGNUM)
1231 regcache_raw_collect (regcache, I387_FISEG_REGNUM, regs + 12);
1232 if (regnum == -1 || regnum == I387_FOSEG_REGNUM)
1233 regcache_raw_collect (regcache, I387_FOSEG_REGNUM, regs + 20);
1234 }
1235 }