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