Commit | Line | Data |
---|---|---|
53e95fcf JS |
1 | /* Target-dependent code for the x86-64 for GDB, the GNU debugger. |
2 | Copyright 2001 | |
3 | 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 "x86-64-tdep.h" | |
31 | #include "dwarf2cfi.h" | |
fd0407d6 | 32 | #include "value.h" |
53e95fcf JS |
33 | |
34 | ||
35 | /* Register numbers of various important registers. */ | |
36 | #define RAX_REGNUM 0 | |
37 | #define RDX_REGNUM 1 | |
38 | #define RDI_REGNUM 5 | |
39 | #define EFLAGS_REGNUM 17 | |
40 | #define XMM1_REGNUM 35 | |
41 | ||
42 | /* x86_64_register_raw_size_table[i] is the number of bytes of storage in | |
43 | GDB's register array occupied by register i. */ | |
44 | int x86_64_register_raw_size_table[X86_64_NUM_REGS] = { | |
45 | 8, 8, 8, 8, | |
46 | 8, 8, 8, 8, | |
47 | 8, 8, 8, 8, | |
48 | 8, 8, 8, 8, | |
49 | 8, 4, | |
50 | 10, 10, 10, 10, | |
51 | 10, 10, 10, 10, | |
52 | 4, 4, 4, 4, | |
53 | 4, 4, 4, 4, | |
54 | 16, 16, 16, 16, | |
55 | 16, 16, 16, 16, | |
56 | 16, 16, 16, 16, | |
57 | 16, 16, 16, 16, | |
58 | 4 | |
59 | }; | |
60 | ||
61 | /* Number of bytes of storage in the actual machine representation for | |
62 | register REGNO. */ | |
63 | int | |
64 | x86_64_register_raw_size (int regno) | |
65 | { | |
66 | return x86_64_register_raw_size_table[regno]; | |
67 | } | |
68 | ||
69 | /* x86_64_register_byte_table[i] is the offset into the register file of the | |
70 | start of register number i. We initialize this from | |
71 | x86_64_register_raw_size_table. */ | |
72 | int x86_64_register_byte_table[X86_64_NUM_REGS]; | |
73 | ||
74 | /* Index within `registers' of the first byte of the space for register REGNO. */ | |
75 | int | |
76 | x86_64_register_byte (int regno) | |
77 | { | |
78 | return x86_64_register_byte_table[regno]; | |
79 | } | |
80 | ||
81 | /* Return the GDB type object for the "standard" data type of data in | |
82 | register N. */ | |
83 | static struct type * | |
84 | x86_64_register_virtual_type (int regno) | |
85 | { | |
86 | if (regno == PC_REGNUM || regno == SP_REGNUM) | |
87 | return lookup_pointer_type (builtin_type_void); | |
88 | if (IS_FP_REGNUM (regno)) | |
89 | return builtin_type_long_double; | |
90 | if (IS_SSE_REGNUM (regno)) | |
91 | return builtin_type_v4sf; | |
92 | if (IS_FPU_CTRL_REGNUM (regno) || regno == MXCSR_REGNUM | |
93 | || regno == EFLAGS_REGNUM) | |
94 | return builtin_type_int; | |
95 | return builtin_type_long; | |
96 | } | |
97 | ||
98 | /* Number of bytes of storage in the program's representation | |
99 | for register REGNO. */ | |
100 | int | |
101 | x86_64_register_virtual_size (int regno) | |
102 | { | |
103 | return (TYPE_LENGTH (x86_64_register_virtual_type (regno))); | |
104 | } | |
105 | ||
106 | /* x86_64_register_convertible is true if register N's virtual format is | |
107 | different from its raw format. Note that this definition assumes | |
108 | that the host supports IEEE 32-bit floats, since it doesn't say | |
109 | that SSE registers need conversion. Even if we can't find a | |
110 | counterexample, this is still sloppy. */ | |
111 | int | |
112 | x86_64_register_convertible (int regno) | |
113 | { | |
114 | return IS_FP_REGNUM (regno); | |
115 | } | |
116 | ||
117 | /* Convert data from raw format for register REGNUM in buffer FROM to | |
118 | virtual format with type TYPE in buffer TO. In principle both | |
119 | formats are identical except that the virtual format has two extra | |
120 | bytes appended that aren't used. We set these to zero. */ | |
121 | void | |
122 | x86_64_register_convert_to_virtual (int regnum, struct type *type, | |
123 | char *from, char *to) | |
124 | { | |
125 | /* Copy straight over, but take care of the padding. */ | |
126 | memcpy (to, from, FPU_REG_RAW_SIZE); | |
127 | memset (to + FPU_REG_RAW_SIZE, 0, TYPE_LENGTH (type) - FPU_REG_RAW_SIZE); | |
128 | } | |
129 | ||
130 | /* Convert data from virtual format with type TYPE in buffer FROM to | |
131 | raw format for register REGNUM in buffer TO. Simply omit the two | |
132 | unused bytes. */ | |
133 | ||
134 | void | |
135 | x86_64_register_convert_to_raw (struct type *type, int regnum, | |
136 | char *from, char *to) | |
137 | { | |
138 | memcpy (to, from, FPU_REG_RAW_SIZE); | |
139 | } | |
140 | \f | |
141 | ||
142 | /* This is the variable that is set with "set disassembly-flavour", and | |
143 | its legitimate values. */ | |
144 | static const char att_flavour[] = "att"; | |
145 | static const char intel_flavour[] = "intel"; | |
146 | static const char *valid_flavours[] = { | |
147 | att_flavour, | |
148 | intel_flavour, | |
149 | NULL | |
150 | }; | |
151 | static const char *disassembly_flavour = att_flavour; | |
152 | ||
153 | static CORE_ADDR | |
154 | x86_64_push_return_address (CORE_ADDR pc, CORE_ADDR sp) | |
155 | { | |
156 | char buf[8]; | |
157 | ||
158 | store_unsigned_integer (buf, 8, CALL_DUMMY_ADDRESS ()); | |
159 | ||
160 | write_memory (sp - 8, buf, 8); | |
161 | return sp - 8; | |
162 | } | |
163 | ||
164 | void | |
165 | x86_64_pop_frame (void) | |
166 | { | |
167 | generic_pop_current_frame (cfi_pop_frame); | |
168 | } | |
169 | \f | |
170 | ||
171 | /* The returning of values is done according to the special algorithm. | |
172 | Some types are returned in registers an some (big structures) in memory. | |
173 | See ABI for details. | |
174 | */ | |
175 | ||
176 | #define MAX_CLASSES 4 | |
177 | ||
178 | enum x86_64_reg_class | |
179 | { | |
180 | X86_64_NO_CLASS, | |
181 | X86_64_INTEGER_CLASS, | |
182 | X86_64_INTEGERSI_CLASS, | |
183 | X86_64_SSE_CLASS, | |
184 | X86_64_SSESF_CLASS, | |
185 | X86_64_SSEDF_CLASS, | |
186 | X86_64_SSEUP_CLASS, | |
187 | X86_64_X87_CLASS, | |
188 | X86_64_X87UP_CLASS, | |
189 | X86_64_MEMORY_CLASS | |
190 | }; | |
191 | ||
192 | /* Return the union class of CLASS1 and CLASS2. | |
193 | See the x86-64 ABI for details. */ | |
194 | ||
195 | static enum x86_64_reg_class | |
196 | merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2) | |
197 | { | |
198 | /* Rule #1: If both classes are equal, this is the resulting class. */ | |
199 | if (class1 == class2) | |
200 | return class1; | |
201 | ||
202 | /* Rule #2: If one of the classes is NO_CLASS, the resulting class is | |
203 | the other class. */ | |
204 | if (class1 == X86_64_NO_CLASS) | |
205 | return class2; | |
206 | if (class2 == X86_64_NO_CLASS) | |
207 | return class1; | |
208 | ||
209 | /* Rule #3: If one of the classes is MEMORY, the result is MEMORY. */ | |
210 | if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS) | |
211 | return X86_64_MEMORY_CLASS; | |
212 | ||
213 | /* Rule #4: If one of the classes is INTEGER, the result is INTEGER. */ | |
214 | if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS) | |
215 | || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS)) | |
216 | return X86_64_INTEGERSI_CLASS; | |
217 | if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS | |
218 | || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS) | |
219 | return X86_64_INTEGER_CLASS; | |
220 | ||
221 | /* Rule #5: If one of the classes is X87 or X87UP class, MEMORY is used. */ | |
222 | if (class1 == X86_64_X87_CLASS || class1 == X86_64_X87UP_CLASS | |
223 | || class2 == X86_64_X87_CLASS || class2 == X86_64_X87UP_CLASS) | |
224 | return X86_64_MEMORY_CLASS; | |
225 | ||
226 | /* Rule #6: Otherwise class SSE is used. */ | |
227 | return X86_64_SSE_CLASS; | |
228 | } | |
229 | ||
230 | ||
231 | /* Classify the argument type. | |
232 | CLASSES will be filled by the register class used to pass each word | |
233 | of the operand. The number of words is returned. In case the parameter | |
234 | should be passed in memory, 0 is returned. As a special case for zero | |
235 | sized containers, classes[0] will be NO_CLASS and 1 is returned. | |
236 | ||
237 | See the x86-64 PS ABI for details. | |
238 | */ | |
239 | ||
240 | static int | |
241 | classify_argument (struct type *type, | |
242 | enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset) | |
243 | { | |
244 | int bytes = TYPE_LENGTH (type); | |
245 | int words = (bytes + 8 - 1) / 8; | |
246 | ||
247 | switch (TYPE_CODE (type)) | |
248 | { | |
249 | case TYPE_CODE_ARRAY: | |
250 | case TYPE_CODE_STRUCT: | |
251 | case TYPE_CODE_UNION: | |
252 | { | |
253 | int i; | |
254 | enum x86_64_reg_class subclasses[MAX_CLASSES]; | |
255 | ||
256 | /* On x86-64 we pass structures larger than 16 bytes on the stack. */ | |
257 | if (bytes > 16) | |
258 | return 0; | |
259 | ||
260 | for (i = 0; i < words; i++) | |
261 | classes[i] = X86_64_NO_CLASS; | |
262 | ||
263 | /* Zero sized arrays or structures are NO_CLASS. We return 0 to | |
264 | signalize memory class, so handle it as special case. */ | |
265 | if (!words) | |
266 | { | |
267 | classes[0] = X86_64_NO_CLASS; | |
268 | return 1; | |
269 | } | |
270 | switch (TYPE_CODE (type)) | |
271 | { | |
272 | case TYPE_CODE_STRUCT: | |
273 | { | |
274 | int j; | |
275 | for (j = 0; j < type->nfields; ++j) | |
276 | { | |
277 | int num = classify_argument (type->fields[j].type, | |
278 | subclasses, | |
279 | (type->fields[j].loc.bitpos | |
280 | + bit_offset) % 256); | |
281 | if (!num) | |
282 | return 0; | |
283 | for (i = 0; i < num; i++) | |
284 | { | |
285 | int pos = | |
286 | (type->fields[j].loc.bitpos + bit_offset) / 8 / 8; | |
287 | classes[i + pos] = | |
288 | merge_classes (subclasses[i], classes[i + pos]); | |
289 | } | |
290 | } | |
291 | } | |
292 | break; | |
293 | case TYPE_CODE_ARRAY: | |
294 | { | |
295 | int num; | |
296 | ||
297 | num = classify_argument (type->target_type, | |
298 | subclasses, bit_offset); | |
299 | if (!num) | |
300 | return 0; | |
301 | ||
302 | /* The partial classes are now full classes. */ | |
303 | if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4) | |
304 | subclasses[0] = X86_64_SSE_CLASS; | |
305 | if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4) | |
306 | subclasses[0] = X86_64_INTEGER_CLASS; | |
307 | ||
308 | for (i = 0; i < words; i++) | |
309 | classes[i] = subclasses[i % num]; | |
310 | } | |
311 | break; | |
312 | case TYPE_CODE_UNION: | |
313 | { | |
314 | int j; | |
315 | { | |
316 | for (j = 0; j < type->nfields; ++j) | |
317 | { | |
318 | int num; | |
319 | num = classify_argument (type->fields[j].type, | |
320 | subclasses, bit_offset); | |
321 | if (!num) | |
322 | return 0; | |
323 | for (i = 0; i < num; i++) | |
324 | classes[i] = merge_classes (subclasses[i], classes[i]); | |
325 | } | |
326 | } | |
327 | } | |
328 | break; | |
329 | } | |
330 | /* Final merger cleanup. */ | |
331 | for (i = 0; i < words; i++) | |
332 | { | |
333 | /* If one class is MEMORY, everything should be passed in | |
334 | memory. */ | |
335 | if (classes[i] == X86_64_MEMORY_CLASS) | |
336 | return 0; | |
337 | ||
338 | /* The X86_64_SSEUP_CLASS should be always preceeded by | |
339 | X86_64_SSE_CLASS. */ | |
340 | if (classes[i] == X86_64_SSEUP_CLASS | |
341 | && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS)) | |
342 | classes[i] = X86_64_SSE_CLASS; | |
343 | ||
344 | /* X86_64_X87UP_CLASS should be preceeded by X86_64_X87_CLASS. */ | |
345 | if (classes[i] == X86_64_X87UP_CLASS | |
346 | && (i == 0 || classes[i - 1] != X86_64_X87_CLASS)) | |
347 | classes[i] = X86_64_SSE_CLASS; | |
348 | } | |
349 | return words; | |
350 | } | |
351 | break; | |
352 | case TYPE_CODE_FLT: | |
353 | switch (bytes) | |
354 | { | |
355 | case 4: | |
356 | if (!(bit_offset % 64)) | |
357 | classes[0] = X86_64_SSESF_CLASS; | |
358 | else | |
359 | classes[0] = X86_64_SSE_CLASS; | |
360 | return 1; | |
361 | case 8: | |
362 | classes[0] = X86_64_SSEDF_CLASS; | |
363 | return 1; | |
364 | case 16: | |
365 | classes[0] = X86_64_X87_CLASS; | |
366 | classes[1] = X86_64_X87UP_CLASS; | |
367 | return 2; | |
368 | } | |
369 | break; | |
370 | case TYPE_CODE_INT: | |
371 | case TYPE_CODE_PTR: | |
372 | switch (bytes) | |
373 | { | |
374 | case 1: | |
375 | case 2: | |
376 | case 4: | |
377 | case 8: | |
378 | if (bytes * 8 + bit_offset <= 32) | |
379 | classes[0] = X86_64_INTEGERSI_CLASS; | |
380 | else | |
381 | classes[0] = X86_64_INTEGER_CLASS; | |
382 | return 1; | |
383 | case 16: | |
384 | classes[0] = classes[1] = X86_64_INTEGER_CLASS; | |
385 | return 2; | |
386 | default: | |
387 | break; | |
388 | } | |
389 | case TYPE_CODE_VOID: | |
390 | return 0; | |
391 | } | |
392 | internal_error (__FILE__, __LINE__, "classify_argument: unknown argument type"); | |
393 | } | |
394 | ||
395 | /* Examine the argument and return set number of register required in each | |
396 | class. Return 0 ifif parameter should be passed in memory. */ | |
397 | ||
398 | static int | |
399 | examine_argument (enum x86_64_reg_class classes[MAX_CLASSES], | |
400 | int n, int *int_nregs, int *sse_nregs) | |
401 | { | |
402 | *int_nregs = 0; | |
403 | *sse_nregs = 0; | |
404 | if (!n) | |
405 | return 0; | |
406 | for (n--; n >= 0; n--) | |
407 | switch (classes[n]) | |
408 | { | |
409 | case X86_64_INTEGER_CLASS: | |
410 | case X86_64_INTEGERSI_CLASS: | |
411 | (*int_nregs)++; | |
412 | break; | |
413 | case X86_64_SSE_CLASS: | |
414 | case X86_64_SSESF_CLASS: | |
415 | case X86_64_SSEDF_CLASS: | |
416 | (*sse_nregs)++; | |
417 | break; | |
418 | case X86_64_NO_CLASS: | |
419 | case X86_64_SSEUP_CLASS: | |
420 | case X86_64_X87_CLASS: | |
421 | case X86_64_X87UP_CLASS: | |
422 | break; | |
423 | case X86_64_MEMORY_CLASS: | |
424 | internal_error (__FILE__, __LINE__, "examine_argument: unexpected memory class"); | |
425 | } | |
426 | return 1; | |
427 | } | |
428 | ||
429 | #define RET_INT_REGS 2 | |
430 | #define RET_SSE_REGS 2 | |
431 | ||
432 | /* Check if the structure in value_type is returned in registers or in | |
433 | memory. If this function returns 1, gdb will call STORE_STRUCT_RETURN and | |
434 | EXTRACT_STRUCT_VALUE_ADDRESS else STORE_RETURN_VALUE and EXTRACT_RETURN_VALUE | |
435 | will be used. */ | |
436 | int | |
437 | x86_64_use_struct_convention (int gcc_p, struct type *value_type) | |
438 | { | |
439 | enum x86_64_reg_class class[MAX_CLASSES]; | |
440 | int n = classify_argument (value_type, class, 0); | |
441 | int needed_intregs; | |
442 | int needed_sseregs; | |
443 | ||
444 | return (!n || | |
445 | !examine_argument (class, n, &needed_intregs, &needed_sseregs) || | |
446 | needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS); | |
447 | } | |
448 | ||
449 | ||
450 | /* Extract from an array REGBUF containing the (raw) register state, a | |
451 | function return value of TYPE, and copy that, in virtual format, | |
452 | into VALBUF. */ | |
453 | ||
454 | void | |
455 | x86_64_extract_return_value (struct type *type, char *regbuf, char *valbuf) | |
456 | { | |
457 | enum x86_64_reg_class class[MAX_CLASSES]; | |
458 | int n = classify_argument (type, class, 0); | |
459 | int needed_intregs; | |
460 | int needed_sseregs; | |
461 | int intreg = 0; | |
462 | int ssereg = 0; | |
463 | int offset = 0; | |
464 | int ret_int_r[RET_INT_REGS] = { RAX_REGNUM, RDX_REGNUM }; | |
465 | int ret_sse_r[RET_SSE_REGS] = { XMM0_REGNUM, XMM1_REGNUM }; | |
466 | ||
467 | if (!n || | |
468 | !examine_argument (class, n, &needed_intregs, &needed_sseregs) || | |
469 | needed_intregs > RET_INT_REGS || needed_sseregs > RET_SSE_REGS) | |
470 | { /* memory class */ | |
471 | CORE_ADDR addr; | |
472 | memcpy (&addr, regbuf, REGISTER_RAW_SIZE (RAX_REGNUM)); | |
473 | read_memory (addr, valbuf, TYPE_LENGTH (type)); | |
474 | return; | |
475 | } | |
476 | else | |
477 | { | |
478 | int i; | |
479 | for (i = 0; i < n; i++) | |
480 | { | |
481 | switch (class[i]) | |
482 | { | |
483 | case X86_64_NO_CLASS: | |
484 | break; | |
485 | case X86_64_INTEGER_CLASS: | |
486 | memcpy (valbuf + offset, | |
487 | regbuf + REGISTER_BYTE (ret_int_r[(intreg + 1) / 2]), | |
488 | 8); | |
489 | offset += 8; | |
490 | intreg += 2; | |
491 | break; | |
492 | case X86_64_INTEGERSI_CLASS: | |
493 | memcpy (valbuf + offset, | |
494 | regbuf + REGISTER_BYTE (ret_int_r[intreg / 2]), 4); | |
495 | offset += 8; | |
496 | intreg++; | |
497 | break; | |
498 | case X86_64_SSEDF_CLASS: | |
499 | case X86_64_SSESF_CLASS: | |
500 | case X86_64_SSE_CLASS: | |
501 | memcpy (valbuf + offset, | |
502 | regbuf + REGISTER_BYTE (ret_sse_r[(ssereg + 1) / 2]), | |
503 | 8); | |
504 | offset += 8; | |
505 | ssereg += 2; | |
506 | break; | |
507 | case X86_64_SSEUP_CLASS: | |
508 | memcpy (valbuf + offset + 8, | |
509 | regbuf + REGISTER_BYTE (ret_sse_r[ssereg / 2]), 8); | |
510 | offset += 8; | |
511 | ssereg++; | |
512 | break; | |
513 | case X86_64_X87_CLASS: | |
514 | memcpy (valbuf + offset, regbuf + REGISTER_BYTE (FP0_REGNUM), | |
515 | 8); | |
516 | offset += 8; | |
517 | break; | |
518 | case X86_64_X87UP_CLASS: | |
519 | memcpy (valbuf + offset, | |
520 | regbuf + REGISTER_BYTE (FP0_REGNUM) + 8, 8); | |
521 | offset += 8; | |
522 | break; | |
523 | case X86_64_MEMORY_CLASS: | |
524 | default: | |
525 | internal_error (__FILE__, __LINE__, | |
526 | "Unexpected argument class"); | |
527 | } | |
528 | } | |
529 | } | |
530 | } | |
531 | ||
532 | /* Handled by unwind informations. */ | |
533 | static void | |
534 | x86_64_frame_init_saved_regs (struct frame_info *fi) | |
535 | { | |
536 | } | |
537 | ||
538 | #define INT_REGS 6 | |
539 | #define SSE_REGS 16 | |
540 | ||
541 | /* Push onto the stack the specified value VALUE. Pad it correctly for | |
542 | it to be an argument to a function. */ | |
543 | ||
544 | static CORE_ADDR | |
d45fc520 | 545 | value_push (register CORE_ADDR sp, struct value *arg) |
53e95fcf JS |
546 | { |
547 | register int len = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (arg)); | |
548 | register int container_len = len; | |
549 | ||
550 | /* How big is the container we're going to put this value in? */ | |
551 | if (PARM_BOUNDARY) | |
552 | container_len = ((len + PARM_BOUNDARY / TARGET_CHAR_BIT - 1) | |
553 | & ~(PARM_BOUNDARY / TARGET_CHAR_BIT - 1)); | |
554 | ||
555 | sp -= container_len; | |
556 | write_memory (sp, VALUE_CONTENTS_ALL (arg), len); | |
557 | ||
558 | return sp; | |
559 | } | |
560 | ||
561 | CORE_ADDR | |
d45fc520 | 562 | x86_64_push_arguments (int nargs, struct value **args, CORE_ADDR sp, |
53e95fcf JS |
563 | int struct_return, CORE_ADDR struct_addr) |
564 | { | |
565 | int intreg = 0; | |
566 | int ssereg = 0; | |
567 | int i; | |
568 | static int int_parameter_registers[INT_REGS] = {5 /*RDI*/, 4 /*RSI*/, | |
569 | 1 /*RDX*/, 2 /*RCX*/, | |
570 | 8 /*R8 */, 9 /*R9 */}; | |
571 | /* XMM0 - XMM15 */ | |
572 | static int sse_parameter_registers[SSE_REGS] = {34, 35, 36, 37, | |
573 | 38, 39, 40, 41, | |
574 | 42, 43, 44, 45, | |
575 | 46, 47, 48, 49}; | |
576 | for (i = 0; i < nargs; i++) | |
577 | { | |
578 | enum x86_64_reg_class class[MAX_CLASSES]; | |
579 | int n = classify_argument (args[i]->type, class, 0); | |
580 | int needed_intregs; | |
581 | int needed_sseregs; | |
582 | ||
583 | if (!n || | |
584 | !examine_argument (class, n, &needed_intregs, &needed_sseregs) | |
585 | || intreg + needed_intregs > INT_REGS | |
586 | || ssereg + needed_sseregs > SSE_REGS) | |
587 | { /* memory class */ | |
588 | sp = value_push (sp, args[i]); | |
589 | } | |
590 | else | |
591 | { | |
592 | int j; | |
593 | for (j = 0; j < n; j++) | |
594 | { | |
595 | int offset = 0; | |
596 | switch (class[j]) | |
597 | { | |
598 | case X86_64_NO_CLASS: | |
599 | break; | |
600 | case X86_64_INTEGER_CLASS: | |
601 | write_register_gen (int_parameter_registers[(intreg + 1) / 2], | |
602 | VALUE_CONTENTS_ALL (args[i]) + offset); | |
603 | offset += 8; | |
604 | intreg += 2; | |
605 | break; | |
606 | case X86_64_INTEGERSI_CLASS: | |
607 | write_register_gen (int_parameter_registers[intreg / 2], | |
608 | VALUE_CONTENTS_ALL (args[i]) + offset); | |
609 | offset += 8; | |
610 | intreg++; | |
611 | break; | |
612 | case X86_64_SSEDF_CLASS: | |
613 | case X86_64_SSESF_CLASS: | |
614 | case X86_64_SSE_CLASS: | |
615 | write_register_gen (sse_parameter_registers[(ssereg + 1) / 2], | |
616 | VALUE_CONTENTS_ALL (args[i]) + offset); | |
617 | offset += 8; | |
618 | ssereg += 2; | |
619 | break; | |
620 | case X86_64_SSEUP_CLASS: | |
621 | write_register_gen (sse_parameter_registers[ssereg / 2], | |
622 | VALUE_CONTENTS_ALL (args[i]) + offset); | |
623 | offset += 8; | |
624 | ssereg++; | |
625 | break; | |
626 | case X86_64_X87_CLASS: | |
627 | case X86_64_X87UP_CLASS: | |
628 | case X86_64_MEMORY_CLASS: | |
629 | sp = value_push (sp, args[i]); | |
630 | break; | |
631 | default: | |
632 | internal_error (__FILE__, __LINE__, | |
633 | "Unexpected argument class"); | |
634 | } | |
635 | intreg += intreg % 2; | |
636 | ssereg += ssereg % 2; | |
637 | } | |
638 | } | |
639 | } | |
640 | return sp; | |
641 | } | |
642 | ||
643 | /* Write into the appropriate registers a function return value stored | |
644 | in VALBUF of type TYPE, given in virtual format. */ | |
645 | void | |
646 | x86_64_store_return_value (struct type *type, char *valbuf) | |
647 | { | |
648 | int len = TYPE_LENGTH (type); | |
649 | ||
650 | if (TYPE_CODE_FLT == TYPE_CODE (type)) | |
651 | { | |
652 | /* Floating-point return values can be found in %st(0). */ | |
653 | if (len == TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT | |
654 | && TARGET_LONG_DOUBLE_FORMAT == &floatformat_i387_ext) | |
655 | { | |
656 | /* Copy straight over. */ | |
657 | write_register_bytes (REGISTER_BYTE (FP0_REGNUM), valbuf, | |
658 | FPU_REG_RAW_SIZE); | |
659 | } | |
660 | else | |
661 | { | |
662 | char buf[FPU_REG_RAW_SIZE]; | |
663 | DOUBLEST val; | |
664 | ||
665 | /* Convert the value found in VALBUF to the extended | |
666 | floating point format used by the FPU. This is probably | |
667 | not exactly how it would happen on the target itself, but | |
668 | it is the best we can do. */ | |
669 | val = extract_floating (valbuf, TYPE_LENGTH (type)); | |
670 | floatformat_from_doublest (&floatformat_i387_ext, &val, buf); | |
671 | write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf, | |
672 | FPU_REG_RAW_SIZE); | |
673 | } | |
674 | } | |
675 | else | |
676 | { | |
677 | int low_size = REGISTER_RAW_SIZE (0); | |
678 | int high_size = REGISTER_RAW_SIZE (1); | |
679 | ||
680 | if (len <= low_size) | |
681 | write_register_bytes (REGISTER_BYTE (0), valbuf, len); | |
682 | else if (len <= (low_size + high_size)) | |
683 | { | |
684 | write_register_bytes (REGISTER_BYTE (0), valbuf, low_size); | |
685 | write_register_bytes (REGISTER_BYTE (1), | |
686 | valbuf + low_size, len - low_size); | |
687 | } | |
688 | else | |
689 | internal_error (__FILE__, __LINE__, | |
690 | "Cannot store return value of %d bytes long.", len); | |
691 | } | |
692 | } | |
693 | \f | |
694 | ||
695 | static char * | |
696 | x86_64_register_name (int reg_nr) | |
697 | { | |
698 | static char *register_names[] = { | |
699 | "rax", "rdx", "rcx", "rbx", | |
700 | "rsi", "rdi", "rbp", "rsp", | |
701 | "r8", "r9", "r10", "r11", | |
702 | "r12", "r13", "r14", "r15", | |
703 | "rip", "eflags", | |
704 | "st0", "st1", "st2", "st3", | |
705 | "st4", "st5", "st6", "st7", | |
706 | "fctrl", "fstat", "ftag", "fiseg", | |
707 | "fioff", "foseg", "fooff", "fop", | |
708 | "xmm0", "xmm1", "xmm2", "xmm3", | |
709 | "xmm4", "xmm5", "xmm6", "xmm7", | |
710 | "xmm8", "xmm9", "xmm10", "xmm11", | |
711 | "xmm12", "xmm13", "xmm14", "xmm15", | |
712 | "mxcsr" | |
713 | }; | |
714 | if (reg_nr < 0) | |
715 | return NULL; | |
716 | if (reg_nr >= (sizeof (register_names) / sizeof (*register_names))) | |
717 | return NULL; | |
718 | return register_names[reg_nr]; | |
719 | } | |
720 | \f | |
721 | ||
722 | ||
723 | /* We have two flavours of disassembly. The machinery on this page | |
724 | deals with switching between those. */ | |
725 | ||
726 | static int | |
727 | gdb_print_insn_x86_64 (bfd_vma memaddr, disassemble_info * info) | |
728 | { | |
729 | if (disassembly_flavour == att_flavour) | |
730 | return print_insn_i386_att (memaddr, info); | |
731 | else if (disassembly_flavour == intel_flavour) | |
732 | return print_insn_i386_intel (memaddr, info); | |
733 | /* Never reached -- disassembly_flavour is always either att_flavour | |
734 | or intel_flavour. */ | |
735 | internal_error (__FILE__, __LINE__, "failed internal consistency check"); | |
736 | } | |
737 | \f | |
738 | ||
739 | /* Store the address of the place in which to copy the structure the | |
740 | subroutine will return. This is called from call_function. */ | |
741 | void | |
742 | x86_64_store_struct_return (CORE_ADDR addr, CORE_ADDR sp) | |
743 | { | |
744 | write_register (RDI_REGNUM, addr); | |
745 | } | |
746 | ||
747 | int | |
748 | x86_64_frameless_function_invocation (struct frame_info *frame) | |
749 | { | |
750 | return 0; | |
751 | } | |
752 | ||
753 | /* On x86_64 there are no reasonable prologs. */ | |
754 | CORE_ADDR | |
755 | x86_64_skip_prologue (CORE_ADDR pc) | |
756 | { | |
757 | return pc; | |
758 | } | |
759 | ||
760 | /* Sequence of bytes for breakpoint instruction. */ | |
761 | static unsigned char * | |
762 | x86_64_breakpoint_from_pc (CORE_ADDR *pc, int *lenptr) | |
763 | { | |
764 | static unsigned char breakpoint[] = { 0xcc }; | |
765 | *lenptr = 1; | |
766 | return breakpoint; | |
767 | } | |
768 | ||
769 | static struct gdbarch * | |
770 | i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) | |
771 | { | |
772 | struct gdbarch *gdbarch; | |
773 | struct gdbarch_tdep *tdep; | |
774 | ||
775 | /* Find a candidate among the list of pre-declared architectures. */ | |
776 | for (arches = gdbarch_list_lookup_by_info (arches, &info); | |
777 | arches != NULL; | |
778 | arches = gdbarch_list_lookup_by_info (arches->next, &info)) | |
779 | { | |
780 | switch (info.bfd_arch_info->mach) | |
781 | { | |
782 | case bfd_mach_x86_64: | |
783 | case bfd_mach_x86_64_intel_syntax: | |
784 | switch (gdbarch_bfd_arch_info (arches->gdbarch)->mach) | |
785 | { | |
786 | case bfd_mach_x86_64: | |
787 | case bfd_mach_x86_64_intel_syntax: | |
788 | return arches->gdbarch; | |
789 | case bfd_mach_i386_i386: | |
790 | case bfd_mach_i386_i8086: | |
791 | case bfd_mach_i386_i386_intel_syntax: | |
792 | break; | |
793 | default: | |
794 | internal_error (__FILE__, __LINE__, | |
795 | "i386_gdbarch_init: unknown machine type"); | |
796 | } | |
797 | break; | |
798 | case bfd_mach_i386_i386: | |
799 | case bfd_mach_i386_i8086: | |
800 | case bfd_mach_i386_i386_intel_syntax: | |
801 | switch (gdbarch_bfd_arch_info (arches->gdbarch)->mach) | |
802 | { | |
803 | case bfd_mach_x86_64: | |
804 | case bfd_mach_x86_64_intel_syntax: | |
805 | break; | |
806 | case bfd_mach_i386_i386: | |
807 | case bfd_mach_i386_i8086: | |
808 | case bfd_mach_i386_i386_intel_syntax: | |
809 | return arches->gdbarch; | |
810 | default: | |
811 | internal_error (__FILE__, __LINE__, | |
812 | "i386_gdbarch_init: unknown machine type"); | |
813 | } | |
814 | break; | |
815 | default: | |
816 | internal_error (__FILE__, __LINE__, | |
817 | "i386_gdbarch_init: unknown machine type"); | |
818 | } | |
819 | } | |
820 | ||
821 | tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep)); | |
822 | gdbarch = gdbarch_alloc (&info, tdep); | |
823 | ||
824 | switch (info.bfd_arch_info->mach) | |
825 | { | |
826 | case bfd_mach_x86_64: | |
827 | case bfd_mach_x86_64_intel_syntax: | |
828 | tdep->last_fpu_regnum = 25; | |
829 | tdep->first_xmm_regnum = 34; | |
830 | tdep->last_xmm_regnum = 49; | |
831 | tdep->mxcsr_regnum = 50; | |
832 | tdep->first_fpu_ctrl_regnum = 26; | |
833 | break; | |
834 | case bfd_mach_i386_i386: | |
835 | case bfd_mach_i386_i8086: | |
836 | case bfd_mach_i386_i386_intel_syntax: | |
837 | /* This is place for definition of i386 target vector. */ | |
838 | break; | |
839 | default: | |
840 | internal_error (__FILE__, __LINE__, | |
841 | "i386_gdbarch_init: unknown machine type"); | |
842 | } | |
843 | ||
844 | set_gdbarch_long_bit (gdbarch, 64); | |
845 | set_gdbarch_long_long_bit (gdbarch, 64); | |
846 | set_gdbarch_ptr_bit (gdbarch, 64); | |
847 | ||
848 | set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext); | |
849 | set_gdbarch_ieee_float (gdbarch, 1); | |
850 | ||
851 | ||
852 | set_gdbarch_num_regs (gdbarch, X86_64_NUM_REGS); | |
853 | set_gdbarch_register_name (gdbarch, x86_64_register_name); | |
854 | set_gdbarch_register_size (gdbarch, 8); | |
855 | set_gdbarch_register_raw_size (gdbarch, x86_64_register_raw_size); | |
856 | set_gdbarch_max_register_raw_size (gdbarch, 16); | |
857 | set_gdbarch_register_byte (gdbarch, x86_64_register_byte); | |
858 | /* Total amount of space needed to store our copies of the machine's register | |
859 | (SIZEOF_GREGS + SIZEOF_FPU_REGS + SIZEOF_FPU_CTRL_REGS + SIZEOF_SSE_REGS) */ | |
860 | set_gdbarch_register_bytes (gdbarch, | |
861 | (18 * 8) + (8 * 10) + (8 * 4) + (8 * 16 + 4)); | |
862 | set_gdbarch_register_virtual_size (gdbarch, x86_64_register_virtual_size); | |
863 | set_gdbarch_max_register_virtual_size (gdbarch, 16); | |
864 | ||
865 | set_gdbarch_register_virtual_type (gdbarch, x86_64_register_virtual_type); | |
866 | ||
867 | set_gdbarch_register_convertible (gdbarch, x86_64_register_convertible); | |
868 | set_gdbarch_register_convert_to_virtual (gdbarch, | |
869 | x86_64_register_convert_to_virtual); | |
870 | set_gdbarch_register_convert_to_raw (gdbarch, | |
871 | x86_64_register_convert_to_raw); | |
872 | ||
873 | /* Register numbers of various important registers. */ | |
874 | set_gdbarch_sp_regnum (gdbarch, 7); /* (rsp) Contains address of top of stack. */ | |
875 | set_gdbarch_fp_regnum (gdbarch, 6); /* (rbp) */ | |
876 | set_gdbarch_pc_regnum (gdbarch, 16); /* (rip) Contains program counter. */ | |
877 | ||
878 | set_gdbarch_fp0_regnum (gdbarch, 18); /* First FPU floating-point register. */ | |
879 | ||
880 | set_gdbarch_read_fp (gdbarch, cfi_read_fp); | |
881 | set_gdbarch_write_fp (gdbarch, cfi_write_fp); | |
882 | ||
883 | /* Discard from the stack the innermost frame, restoring all registers. */ | |
884 | set_gdbarch_pop_frame (gdbarch, x86_64_pop_frame); | |
885 | ||
886 | /* FRAME_CHAIN takes a frame's nominal address and produces the frame's | |
887 | chain-pointer. */ | |
888 | set_gdbarch_frame_chain (gdbarch, cfi_frame_chain); | |
889 | ||
890 | set_gdbarch_frameless_function_invocation (gdbarch, | |
891 | x86_64_frameless_function_invocation); | |
892 | set_gdbarch_frame_saved_pc (gdbarch, x86_64_linux_frame_saved_pc); | |
893 | ||
894 | set_gdbarch_frame_args_address (gdbarch, default_frame_address); | |
895 | set_gdbarch_frame_locals_address (gdbarch, default_frame_address); | |
896 | ||
897 | /* Return number of bytes at start of arglist that are not really args. */ | |
898 | set_gdbarch_frame_args_skip (gdbarch, 8); | |
899 | ||
900 | set_gdbarch_frame_init_saved_regs (gdbarch, x86_64_frame_init_saved_regs); | |
901 | ||
902 | /* Frame pc initialization is handled by unwind informations. */ | |
903 | set_gdbarch_init_frame_pc (gdbarch, cfi_init_frame_pc); | |
904 | ||
905 | /* Initialization of unwind informations. */ | |
906 | set_gdbarch_init_extra_frame_info (gdbarch, cfi_init_extra_frame_info); | |
907 | ||
908 | /* Getting saved registers is handled by unwind informations. */ | |
909 | set_gdbarch_get_saved_register (gdbarch, cfi_get_saved_register); | |
910 | ||
911 | set_gdbarch_frame_init_saved_regs (gdbarch, x86_64_frame_init_saved_regs); | |
912 | ||
913 | /* Cons up virtual frame pointer for trace */ | |
914 | set_gdbarch_virtual_frame_pointer (gdbarch, cfi_virtual_frame_pointer); | |
915 | ||
916 | ||
917 | set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid); | |
918 | ||
919 | set_gdbarch_use_generic_dummy_frames (gdbarch, 1); | |
920 | set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT); | |
921 | set_gdbarch_call_dummy_address (gdbarch, entry_point_address); | |
922 | set_gdbarch_call_dummy_length (gdbarch, 0); | |
923 | set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0); | |
924 | set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1); | |
925 | set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point); | |
926 | set_gdbarch_call_dummy_words (gdbarch, 0); | |
927 | set_gdbarch_sizeof_call_dummy_words (gdbarch, 0); | |
928 | set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0); | |
929 | set_gdbarch_call_dummy_p (gdbarch, 1); | |
930 | set_gdbarch_call_dummy_start_offset (gdbarch, 0); | |
931 | set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame); | |
932 | set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy); | |
933 | set_gdbarch_push_return_address (gdbarch, x86_64_push_return_address); | |
934 | set_gdbarch_push_arguments (gdbarch, x86_64_push_arguments); | |
935 | ||
936 | /* Return number of args passed to a frame, no way to tell. */ | |
937 | set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown); | |
938 | /* Don't use default structure extract routine */ | |
939 | set_gdbarch_extract_struct_value_address (gdbarch, 0); | |
940 | ||
941 | /* If USE_STRUCT_CONVENTION retruns 0, then gdb uses STORE_RETURN_VALUE | |
942 | and EXTRACT_RETURN_VALUE to store/fetch the functions return value. It is | |
943 | the case when structure is returned in registers. */ | |
944 | set_gdbarch_use_struct_convention (gdbarch, x86_64_use_struct_convention); | |
945 | ||
946 | /* Store the address of the place in which to copy the structure the | |
947 | subroutine will return. This is called from call_function. */ | |
948 | set_gdbarch_store_struct_return (gdbarch, x86_64_store_struct_return); | |
949 | ||
950 | /* Extract from an array REGBUF containing the (raw) register state | |
951 | a function return value of type TYPE, and copy that, in virtual format, | |
952 | into VALBUF. */ | |
953 | set_gdbarch_extract_return_value (gdbarch, x86_64_extract_return_value); | |
954 | ||
955 | ||
956 | /* Write into the appropriate registers a function return value stored | |
957 | in VALBUF of type TYPE, given in virtual format. */ | |
958 | set_gdbarch_store_return_value (gdbarch, x86_64_store_return_value); | |
959 | \f | |
960 | ||
961 | /* Offset from address of function to start of its code. */ | |
962 | set_gdbarch_function_start_offset (gdbarch, 0); | |
963 | ||
964 | set_gdbarch_skip_prologue (gdbarch, x86_64_skip_prologue); | |
965 | ||
966 | set_gdbarch_saved_pc_after_call (gdbarch, x86_64_linux_saved_pc_after_call); | |
967 | ||
968 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
969 | ||
970 | set_gdbarch_breakpoint_from_pc (gdbarch, x86_64_breakpoint_from_pc); | |
971 | ||
972 | ||
973 | /* Amount PC must be decremented by after a breakpoint. This is often the | |
974 | number of bytes in BREAKPOINT but not always. */ | |
975 | set_gdbarch_decr_pc_after_break (gdbarch, 1); | |
976 | ||
977 | return gdbarch; | |
978 | } | |
979 | ||
980 | void | |
981 | _initialize_x86_64_tdep (void) | |
982 | { | |
983 | register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init); | |
984 | ||
985 | /* Initialize the table saying where each register starts in the | |
986 | register file. */ | |
987 | { | |
988 | int i, offset; | |
989 | ||
990 | offset = 0; | |
991 | for (i = 0; i < X86_64_NUM_REGS; i++) | |
992 | { | |
993 | x86_64_register_byte_table[i] = offset; | |
994 | offset += x86_64_register_raw_size_table[i]; | |
995 | } | |
996 | } | |
997 | ||
998 | tm_print_insn = gdb_print_insn_x86_64; | |
999 | tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 3)->mach; | |
1000 | ||
1001 | /* Add the variable that controls the disassembly flavour. */ | |
1002 | { | |
1003 | struct cmd_list_element *new_cmd; | |
1004 | ||
1005 | new_cmd = add_set_enum_cmd ("disassembly-flavour", no_class, | |
1006 | valid_flavours, &disassembly_flavour, "\ | |
1007 | Set the disassembly flavour, the valid values are \"att\" and \"intel\", \ | |
1008 | and the default value is \"att\".", &setlist); | |
1009 | add_show_from_set (new_cmd, &showlist); | |
1010 | } | |
1011 | } |