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