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