1 /* Target-dependent code for AMD64.
2
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4
5 Contributed by Jiri Smid, SuSE Labs.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "extract-store-integer.h"
23 #include "language.h"
24 #include "opcode/i386.h"
25 #include "dis-asm.h"
26 #include "arch-utils.h"
27 #include "dummy-frame.h"
28 #include "frame.h"
29 #include "frame-base.h"
30 #include "frame-unwind.h"
31 #include "inferior.h"
32 #include "infrun.h"
33 #include "cli/cli-cmds.h"
34 #include "gdbcore.h"
35 #include "objfiles.h"
36 #include "regcache.h"
37 #include "regset.h"
38 #include "symfile.h"
39 #include "disasm.h"
40 #include "amd64-tdep.h"
41 #include "i387-tdep.h"
42 #include "gdbsupport/x86-xstate.h"
43 #include <algorithm>
44 #include "target-descriptions.h"
45 #include "arch/amd64.h"
46 #include "producer.h"
47 #include "ax.h"
48 #include "ax-gdb.h"
49 #include "gdbsupport/byte-vector.h"
50 #include "osabi.h"
51 #include "x86-tdep.h"
52 #include "amd64-ravenscar-thread.h"
53
54 /* Note that the AMD64 architecture was previously known as x86-64.
55 The latter is (forever) engraved into the canonical system name as
56 returned by config.guess, and used as the name for the AMD64 port
57 of GNU/Linux. The BSD's have renamed their ports to amd64; they
58 don't like to shout. For GDB we prefer the amd64_-prefix over the
59 x86_64_-prefix since it's so much easier to type. */
60
61 /* Register information. */
62
63 static const char * const amd64_register_names[] =
64 {
65 "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp",
66
67 /* %r8 is indeed register number 8. */
68 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
69 "rip", "eflags", "cs", "ss", "ds", "es", "fs", "gs",
70
71 /* %st0 is register number 24. */
72 "st0", "st1", "st2", "st3", "st4", "st5", "st6", "st7",
73 "fctrl", "fstat", "ftag", "fiseg", "fioff", "foseg", "fooff", "fop",
74
75 /* %xmm0 is register number 40. */
76 "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
77 "xmm8", "xmm9", "xmm10", "xmm11", "xmm12", "xmm13", "xmm14", "xmm15",
78 "mxcsr",
79 };
80
81 static const char * const amd64_ymm_names[] =
82 {
83 "ymm0", "ymm1", "ymm2", "ymm3",
84 "ymm4", "ymm5", "ymm6", "ymm7",
85 "ymm8", "ymm9", "ymm10", "ymm11",
86 "ymm12", "ymm13", "ymm14", "ymm15"
87 };
88
89 static const char * const amd64_ymm_avx512_names[] =
90 {
91 "ymm16", "ymm17", "ymm18", "ymm19",
92 "ymm20", "ymm21", "ymm22", "ymm23",
93 "ymm24", "ymm25", "ymm26", "ymm27",
94 "ymm28", "ymm29", "ymm30", "ymm31"
95 };
96
97 static const char * const amd64_ymmh_names[] =
98 {
99 "ymm0h", "ymm1h", "ymm2h", "ymm3h",
100 "ymm4h", "ymm5h", "ymm6h", "ymm7h",
101 "ymm8h", "ymm9h", "ymm10h", "ymm11h",
102 "ymm12h", "ymm13h", "ymm14h", "ymm15h"
103 };
104
105 static const char * const amd64_ymmh_avx512_names[] =
106 {
107 "ymm16h", "ymm17h", "ymm18h", "ymm19h",
108 "ymm20h", "ymm21h", "ymm22h", "ymm23h",
109 "ymm24h", "ymm25h", "ymm26h", "ymm27h",
110 "ymm28h", "ymm29h", "ymm30h", "ymm31h"
111 };
112
113 static const char * const amd64_mpx_names[] =
114 {
115 "bnd0raw", "bnd1raw", "bnd2raw", "bnd3raw", "bndcfgu", "bndstatus"
116 };
117
118 static const char * const amd64_k_names[] =
119 {
120 "k0", "k1", "k2", "k3",
121 "k4", "k5", "k6", "k7"
122 };
123
124 static const char * const amd64_zmmh_names[] =
125 {
126 "zmm0h", "zmm1h", "zmm2h", "zmm3h",
127 "zmm4h", "zmm5h", "zmm6h", "zmm7h",
128 "zmm8h", "zmm9h", "zmm10h", "zmm11h",
129 "zmm12h", "zmm13h", "zmm14h", "zmm15h",
130 "zmm16h", "zmm17h", "zmm18h", "zmm19h",
131 "zmm20h", "zmm21h", "zmm22h", "zmm23h",
132 "zmm24h", "zmm25h", "zmm26h", "zmm27h",
133 "zmm28h", "zmm29h", "zmm30h", "zmm31h"
134 };
135
136 static const char * const amd64_zmm_names[] =
137 {
138 "zmm0", "zmm1", "zmm2", "zmm3",
139 "zmm4", "zmm5", "zmm6", "zmm7",
140 "zmm8", "zmm9", "zmm10", "zmm11",
141 "zmm12", "zmm13", "zmm14", "zmm15",
142 "zmm16", "zmm17", "zmm18", "zmm19",
143 "zmm20", "zmm21", "zmm22", "zmm23",
144 "zmm24", "zmm25", "zmm26", "zmm27",
145 "zmm28", "zmm29", "zmm30", "zmm31"
146 };
147
148 static const char * const amd64_xmm_avx512_names[] = {
149 "xmm16", "xmm17", "xmm18", "xmm19",
150 "xmm20", "xmm21", "xmm22", "xmm23",
151 "xmm24", "xmm25", "xmm26", "xmm27",
152 "xmm28", "xmm29", "xmm30", "xmm31"
153 };
154
155 static const char * const amd64_pkeys_names[] = {
156 "pkru"
157 };
158
159 /* DWARF Register Number Mapping as defined in the System V psABI,
160 section 3.6. */
161
162 static int amd64_dwarf_regmap[] =
163 {
164 /* General Purpose Registers RAX, RDX, RCX, RBX, RSI, RDI. */
165 AMD64_RAX_REGNUM, AMD64_RDX_REGNUM,
166 AMD64_RCX_REGNUM, AMD64_RBX_REGNUM,
167 AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
168
169 /* Frame Pointer Register RBP. */
170 AMD64_RBP_REGNUM,
171
172 /* Stack Pointer Register RSP. */
173 AMD64_RSP_REGNUM,
174
175 /* Extended Integer Registers 8 - 15. */
176 AMD64_R8_REGNUM, /* %r8 */
177 AMD64_R9_REGNUM, /* %r9 */
178 AMD64_R10_REGNUM, /* %r10 */
179 AMD64_R11_REGNUM, /* %r11 */
180 AMD64_R12_REGNUM, /* %r12 */
181 AMD64_R13_REGNUM, /* %r13 */
182 AMD64_R14_REGNUM, /* %r14 */
183 AMD64_R15_REGNUM, /* %r15 */
184
185 /* Return Address RA. Mapped to RIP. */
186 AMD64_RIP_REGNUM,
187
188 /* SSE Registers 0 - 7. */
189 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
190 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
191 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
192 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
193
194 /* Extended SSE Registers 8 - 15. */
195 AMD64_XMM0_REGNUM + 8, AMD64_XMM0_REGNUM + 9,
196 AMD64_XMM0_REGNUM + 10, AMD64_XMM0_REGNUM + 11,
197 AMD64_XMM0_REGNUM + 12, AMD64_XMM0_REGNUM + 13,
198 AMD64_XMM0_REGNUM + 14, AMD64_XMM0_REGNUM + 15,
199
200 /* Floating Point Registers 0-7. */
201 AMD64_ST0_REGNUM + 0, AMD64_ST0_REGNUM + 1,
202 AMD64_ST0_REGNUM + 2, AMD64_ST0_REGNUM + 3,
203 AMD64_ST0_REGNUM + 4, AMD64_ST0_REGNUM + 5,
204 AMD64_ST0_REGNUM + 6, AMD64_ST0_REGNUM + 7,
205
206 /* MMX Registers 0 - 7.
207 We have to handle those registers specifically, as their register
208 number within GDB depends on the target (or they may even not be
209 available at all). */
210 -1, -1, -1, -1, -1, -1, -1, -1,
211
212 /* Control and Status Flags Register. */
213 AMD64_EFLAGS_REGNUM,
214
215 /* Selector Registers. */
216 AMD64_ES_REGNUM,
217 AMD64_CS_REGNUM,
218 AMD64_SS_REGNUM,
219 AMD64_DS_REGNUM,
220 AMD64_FS_REGNUM,
221 AMD64_GS_REGNUM,
222 -1,
223 -1,
224
225 /* Segment Base Address Registers. */
226 -1,
227 -1,
228 -1,
229 -1,
230
231 /* Special Selector Registers. */
232 -1,
233 -1,
234
235 /* Floating Point Control Registers. */
236 AMD64_MXCSR_REGNUM,
237 AMD64_FCTRL_REGNUM,
238 AMD64_FSTAT_REGNUM,
239
240 /* XMM16-XMM31. */
241 AMD64_XMM16_REGNUM + 0, AMD64_XMM16_REGNUM + 1,
242 AMD64_XMM16_REGNUM + 2, AMD64_XMM16_REGNUM + 3,
243 AMD64_XMM16_REGNUM + 4, AMD64_XMM16_REGNUM + 5,
244 AMD64_XMM16_REGNUM + 6, AMD64_XMM16_REGNUM + 7,
245 AMD64_XMM16_REGNUM + 8, AMD64_XMM16_REGNUM + 9,
246 AMD64_XMM16_REGNUM + 10, AMD64_XMM16_REGNUM + 11,
247 AMD64_XMM16_REGNUM + 12, AMD64_XMM16_REGNUM + 13,
248 AMD64_XMM16_REGNUM + 14, AMD64_XMM16_REGNUM + 15,
249
250 /* Reserved. */
251 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
252 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
253 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
254
255 /* Mask Registers. */
256 AMD64_K0_REGNUM + 0, AMD64_K0_REGNUM + 1,
257 AMD64_K0_REGNUM + 2, AMD64_K0_REGNUM + 3,
258 AMD64_K0_REGNUM + 4, AMD64_K0_REGNUM + 5,
259 AMD64_K0_REGNUM + 6, AMD64_K0_REGNUM + 7
260 };
261
262 static const int amd64_dwarf_regmap_len =
263 (sizeof (amd64_dwarf_regmap) / sizeof (amd64_dwarf_regmap[0]));
264
265 /* Convert DWARF register number REG to the appropriate register
266 number used by GDB. */
267
268 static int
amd64_dwarf_reg_to_regnum(struct gdbarch * gdbarch,int reg)269 amd64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
270 {
271 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
272 int ymm0_regnum = tdep->ymm0_regnum;
273 int regnum = -1;
274
275 if (reg >= 0 && reg < amd64_dwarf_regmap_len)
276 regnum = amd64_dwarf_regmap[reg];
277
278 if (ymm0_regnum >= 0 && i386_xmm_regnum_p (gdbarch, regnum))
279 regnum += ymm0_regnum - I387_XMM0_REGNUM (tdep);
280
281 return regnum;
282 }
283
284 /* Map architectural register numbers to gdb register numbers. */
285
286 static const int amd64_arch_regmap[16] =
287 {
288 AMD64_RAX_REGNUM, /* %rax */
289 AMD64_RCX_REGNUM, /* %rcx */
290 AMD64_RDX_REGNUM, /* %rdx */
291 AMD64_RBX_REGNUM, /* %rbx */
292 AMD64_RSP_REGNUM, /* %rsp */
293 AMD64_RBP_REGNUM, /* %rbp */
294 AMD64_RSI_REGNUM, /* %rsi */
295 AMD64_RDI_REGNUM, /* %rdi */
296 AMD64_R8_REGNUM, /* %r8 */
297 AMD64_R9_REGNUM, /* %r9 */
298 AMD64_R10_REGNUM, /* %r10 */
299 AMD64_R11_REGNUM, /* %r11 */
300 AMD64_R12_REGNUM, /* %r12 */
301 AMD64_R13_REGNUM, /* %r13 */
302 AMD64_R14_REGNUM, /* %r14 */
303 AMD64_R15_REGNUM /* %r15 */
304 };
305
306 static const int amd64_arch_regmap_len =
307 (sizeof (amd64_arch_regmap) / sizeof (amd64_arch_regmap[0]));
308
309 /* Convert architectural register number REG to the appropriate register
310 number used by GDB. */
311
312 static int
amd64_arch_reg_to_regnum(int reg)313 amd64_arch_reg_to_regnum (int reg)
314 {
315 gdb_assert (reg >= 0 && reg < amd64_arch_regmap_len);
316
317 return amd64_arch_regmap[reg];
318 }
319
320 /* Register names for byte pseudo-registers. */
321
322 static const char * const amd64_byte_names[] =
323 {
324 "al", "bl", "cl", "dl", "sil", "dil", "bpl", "spl",
325 "r8l", "r9l", "r10l", "r11l", "r12l", "r13l", "r14l", "r15l",
326 "ah", "bh", "ch", "dh"
327 };
328
329 /* Number of lower byte registers. */
330 #define AMD64_NUM_LOWER_BYTE_REGS 16
331
332 /* Register names for word pseudo-registers. */
333
334 static const char * const amd64_word_names[] =
335 {
336 "ax", "bx", "cx", "dx", "si", "di", "bp", "",
337 "r8w", "r9w", "r10w", "r11w", "r12w", "r13w", "r14w", "r15w"
338 };
339
340 /* Register names for dword pseudo-registers. */
341
342 static const char * const amd64_dword_names[] =
343 {
344 "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp", "esp",
345 "r8d", "r9d", "r10d", "r11d", "r12d", "r13d", "r14d", "r15d",
346 "eip"
347 };
348
349 /* Return the name of register REGNUM. */
350
351 static const char *
amd64_pseudo_register_name(struct gdbarch * gdbarch,int regnum)352 amd64_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
353 {
354 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
355 if (i386_byte_regnum_p (gdbarch, regnum))
356 return amd64_byte_names[regnum - tdep->al_regnum];
357 else if (i386_zmm_regnum_p (gdbarch, regnum))
358 return amd64_zmm_names[regnum - tdep->zmm0_regnum];
359 else if (i386_ymm_regnum_p (gdbarch, regnum))
360 return amd64_ymm_names[regnum - tdep->ymm0_regnum];
361 else if (i386_ymm_avx512_regnum_p (gdbarch, regnum))
362 return amd64_ymm_avx512_names[regnum - tdep->ymm16_regnum];
363 else if (i386_word_regnum_p (gdbarch, regnum))
364 return amd64_word_names[regnum - tdep->ax_regnum];
365 else if (i386_dword_regnum_p (gdbarch, regnum))
366 return amd64_dword_names[regnum - tdep->eax_regnum];
367 else
368 return i386_pseudo_register_name (gdbarch, regnum);
369 }
370
371 static value *
amd64_pseudo_register_read_value(gdbarch * gdbarch,const frame_info_ptr & next_frame,int regnum)372 amd64_pseudo_register_read_value (gdbarch *gdbarch, const frame_info_ptr &next_frame,
373 int regnum)
374 {
375 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
376
377 if (i386_byte_regnum_p (gdbarch, regnum))
378 {
379 int gpnum = regnum - tdep->al_regnum;
380
381 /* Extract (always little endian). */
382 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
383 {
384 gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
385
386 /* Special handling for AH, BH, CH, DH. */
387 return pseudo_from_raw_part (next_frame, regnum, gpnum, 1);
388 }
389 else
390 return pseudo_from_raw_part (next_frame, regnum, gpnum, 0);
391 }
392 else if (i386_dword_regnum_p (gdbarch, regnum))
393 {
394 int gpnum = regnum - tdep->eax_regnum;
395
396 return pseudo_from_raw_part (next_frame, regnum, gpnum, 0);
397 }
398 else
399 return i386_pseudo_register_read_value (gdbarch, next_frame, regnum);
400 }
401
402 static void
amd64_pseudo_register_write(gdbarch * gdbarch,const frame_info_ptr & next_frame,int regnum,gdb::array_view<const gdb_byte> buf)403 amd64_pseudo_register_write (gdbarch *gdbarch, const frame_info_ptr &next_frame,
404 int regnum, gdb::array_view<const gdb_byte> buf)
405 {
406 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
407
408 if (i386_byte_regnum_p (gdbarch, regnum))
409 {
410 int gpnum = regnum - tdep->al_regnum;
411
412 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
413 {
414 gpnum -= AMD64_NUM_LOWER_BYTE_REGS;
415 pseudo_to_raw_part (next_frame, buf, gpnum, 1);
416 }
417 else
418 pseudo_to_raw_part (next_frame, buf, gpnum, 0);
419 }
420 else if (i386_dword_regnum_p (gdbarch, regnum))
421 {
422 int gpnum = regnum - tdep->eax_regnum;
423 pseudo_to_raw_part (next_frame, buf, gpnum, 0);
424 }
425 else
426 i386_pseudo_register_write (gdbarch, next_frame, regnum, buf);
427 }
428
429 /* Implement the 'ax_pseudo_register_collect' gdbarch method. */
430
431 static int
amd64_ax_pseudo_register_collect(struct gdbarch * gdbarch,struct agent_expr * ax,int regnum)432 amd64_ax_pseudo_register_collect (struct gdbarch *gdbarch,
433 struct agent_expr *ax, int regnum)
434 {
435 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
436
437 if (i386_byte_regnum_p (gdbarch, regnum))
438 {
439 int gpnum = regnum - tdep->al_regnum;
440
441 if (gpnum >= AMD64_NUM_LOWER_BYTE_REGS)
442 ax_reg_mask (ax, gpnum - AMD64_NUM_LOWER_BYTE_REGS);
443 else
444 ax_reg_mask (ax, gpnum);
445 return 0;
446 }
447 else if (i386_dword_regnum_p (gdbarch, regnum))
448 {
449 int gpnum = regnum - tdep->eax_regnum;
450
451 ax_reg_mask (ax, gpnum);
452 return 0;
453 }
454 else
455 return i386_ax_pseudo_register_collect (gdbarch, ax, regnum);
456 }
457
458
459
460 /* Register classes as defined in the psABI. */
461
462 enum amd64_reg_class
463 {
464 AMD64_INTEGER,
465 AMD64_SSE,
466 AMD64_SSEUP,
467 AMD64_X87,
468 AMD64_X87UP,
469 AMD64_COMPLEX_X87,
470 AMD64_NO_CLASS,
471 AMD64_MEMORY
472 };
473
474 /* Return the union class of CLASS1 and CLASS2. See the psABI for
475 details. */
476
477 static enum amd64_reg_class
amd64_merge_classes(enum amd64_reg_class class1,enum amd64_reg_class class2)478 amd64_merge_classes (enum amd64_reg_class class1, enum amd64_reg_class class2)
479 {
480 /* Rule (a): If both classes are equal, this is the resulting class. */
481 if (class1 == class2)
482 return class1;
483
484 /* Rule (b): If one of the classes is NO_CLASS, the resulting class
485 is the other class. */
486 if (class1 == AMD64_NO_CLASS)
487 return class2;
488 if (class2 == AMD64_NO_CLASS)
489 return class1;
490
491 /* Rule (c): If one of the classes is MEMORY, the result is MEMORY. */
492 if (class1 == AMD64_MEMORY || class2 == AMD64_MEMORY)
493 return AMD64_MEMORY;
494
495 /* Rule (d): If one of the classes is INTEGER, the result is INTEGER. */
496 if (class1 == AMD64_INTEGER || class2 == AMD64_INTEGER)
497 return AMD64_INTEGER;
498
499 /* Rule (e): If one of the classes is X87, X87UP, COMPLEX_X87 class,
500 MEMORY is used as class. */
501 if (class1 == AMD64_X87 || class1 == AMD64_X87UP
502 || class1 == AMD64_COMPLEX_X87 || class2 == AMD64_X87
503 || class2 == AMD64_X87UP || class2 == AMD64_COMPLEX_X87)
504 return AMD64_MEMORY;
505
506 /* Rule (f): Otherwise class SSE is used. */
507 return AMD64_SSE;
508 }
509
510 static void amd64_classify (struct type *type, enum amd64_reg_class theclass[2]);
511
512 /* Return true if TYPE is a structure or union with unaligned fields. */
513
514 static bool
amd64_has_unaligned_fields(struct type * type)515 amd64_has_unaligned_fields (struct type *type)
516 {
517 if (type->code () == TYPE_CODE_STRUCT
518 || type->code () == TYPE_CODE_UNION)
519 {
520 for (int i = 0; i < type->num_fields (); i++)
521 {
522 struct type *subtype = check_typedef (type->field (i).type ());
523
524 /* Ignore static fields, empty fields (for example nested
525 empty structures), and bitfields (these are handled by
526 the caller). */
527 if (type->field (i).is_static ()
528 || (type->field (i).bitsize () == 0
529 && subtype->length () == 0)
530 || type->field (i).is_packed ())
531 continue;
532
533 int bitpos = type->field (i).loc_bitpos ();
534
535 if (bitpos % 8 != 0)
536 return true;
537
538 int align = type_align (subtype);
539 if (align == 0)
540 error (_("could not determine alignment of type"));
541
542 int bytepos = bitpos / 8;
543 if (bytepos % align != 0)
544 return true;
545
546 if (amd64_has_unaligned_fields (subtype))
547 return true;
548 }
549 }
550
551 return false;
552 }
553
554 /* Classify field I of TYPE starting at BITOFFSET according to the rules for
555 structures and union types, and store the result in THECLASS. */
556
557 static void
amd64_classify_aggregate_field(struct type * type,int i,enum amd64_reg_class theclass[2],unsigned int bitoffset)558 amd64_classify_aggregate_field (struct type *type, int i,
559 enum amd64_reg_class theclass[2],
560 unsigned int bitoffset)
561 {
562 struct type *subtype = check_typedef (type->field (i).type ());
563 enum amd64_reg_class subclass[2];
564 int bitsize = type->field (i).bitsize ();
565
566 if (bitsize == 0)
567 bitsize = subtype->length () * 8;
568
569 /* Ignore static fields, or empty fields, for example nested
570 empty structures.*/
571 if (type->field (i).is_static () || bitsize == 0)
572 return;
573
574 int bitpos = bitoffset + type->field (i).loc_bitpos ();
575 int pos = bitpos / 64;
576 int endpos = (bitpos + bitsize - 1) / 64;
577
578 if (subtype->code () == TYPE_CODE_STRUCT
579 || subtype->code () == TYPE_CODE_UNION)
580 {
581 /* Each field of an object is classified recursively. */
582 int j;
583 for (j = 0; j < subtype->num_fields (); j++)
584 amd64_classify_aggregate_field (subtype, j, theclass, bitpos);
585 return;
586 }
587
588 gdb_assert (pos == 0 || pos == 1);
589
590 amd64_classify (subtype, subclass);
591 theclass[pos] = amd64_merge_classes (theclass[pos], subclass[0]);
592 if (bitsize <= 64 && pos == 0 && endpos == 1)
593 /* This is a bit of an odd case: We have a field that would
594 normally fit in one of the two eightbytes, except that
595 it is placed in a way that this field straddles them.
596 This has been seen with a structure containing an array.
597
598 The ABI is a bit unclear in this case, but we assume that
599 this field's class (stored in subclass[0]) must also be merged
600 into class[1]. In other words, our field has a piece stored
601 in the second eight-byte, and thus its class applies to
602 the second eight-byte as well.
603
604 In the case where the field length exceeds 8 bytes,
605 it should not be necessary to merge the field class
606 into class[1]. As LEN > 8, subclass[1] is necessarily
607 different from AMD64_NO_CLASS. If subclass[1] is equal
608 to subclass[0], then the normal class[1]/subclass[1]
609 merging will take care of everything. For subclass[1]
610 to be different from subclass[0], I can only see the case
611 where we have a SSE/SSEUP or X87/X87UP pair, which both
612 use up all 16 bytes of the aggregate, and are already
613 handled just fine (because each portion sits on its own
614 8-byte). */
615 theclass[1] = amd64_merge_classes (theclass[1], subclass[0]);
616 if (pos == 0)
617 theclass[1] = amd64_merge_classes (theclass[1], subclass[1]);
618 }
619
620 /* Classify TYPE according to the rules for aggregate (structures and
621 arrays) and union types, and store the result in CLASS. */
622
623 static void
amd64_classify_aggregate(struct type * type,enum amd64_reg_class theclass[2])624 amd64_classify_aggregate (struct type *type, enum amd64_reg_class theclass[2])
625 {
626 /* 1. If the size of an object is larger than two times eight bytes, or
627 it is a non-trivial C++ object, or it has unaligned fields, then it
628 has class memory.
629
630 It is important that the trivially_copyable check is before the
631 unaligned fields check, as C++ classes with virtual base classes
632 will have fields (for the virtual base classes) with non-constant
633 loc_bitpos attributes, which will cause an assert to trigger within
634 the unaligned field check. As classes with virtual bases are not
635 trivially copyable, checking that first avoids this problem. */
636 if (TYPE_HAS_DYNAMIC_LENGTH (type)
637 || type->length () > 16
638 || !language_pass_by_reference (type).trivially_copyable
639 || amd64_has_unaligned_fields (type))
640 {
641 theclass[0] = theclass[1] = AMD64_MEMORY;
642 return;
643 }
644
645 /* 2. Both eightbytes get initialized to class NO_CLASS. */
646 theclass[0] = theclass[1] = AMD64_NO_CLASS;
647
648 /* 3. Each field of an object is classified recursively so that
649 always two fields are considered. The resulting class is
650 calculated according to the classes of the fields in the
651 eightbyte: */
652
653 if (type->code () == TYPE_CODE_ARRAY)
654 {
655 struct type *subtype = check_typedef (type->target_type ());
656
657 /* All fields in an array have the same type. */
658 amd64_classify (subtype, theclass);
659 if (type->length () > 8 && theclass[1] == AMD64_NO_CLASS)
660 theclass[1] = theclass[0];
661 }
662 else
663 {
664 int i;
665
666 /* Structure or union. */
667 gdb_assert (type->code () == TYPE_CODE_STRUCT
668 || type->code () == TYPE_CODE_UNION);
669
670 for (i = 0; i < type->num_fields (); i++)
671 amd64_classify_aggregate_field (type, i, theclass, 0);
672 }
673
674 /* 4. Then a post merger cleanup is done: */
675
676 /* Rule (a): If one of the classes is MEMORY, the whole argument is
677 passed in memory. */
678 if (theclass[0] == AMD64_MEMORY || theclass[1] == AMD64_MEMORY)
679 theclass[0] = theclass[1] = AMD64_MEMORY;
680
681 /* Rule (b): If SSEUP is not preceded by SSE, it is converted to
682 SSE. */
683 if (theclass[0] == AMD64_SSEUP)
684 theclass[0] = AMD64_SSE;
685 if (theclass[1] == AMD64_SSEUP && theclass[0] != AMD64_SSE)
686 theclass[1] = AMD64_SSE;
687 }
688
689 /* Classify TYPE, and store the result in CLASS. */
690
691 static void
amd64_classify(struct type * type,enum amd64_reg_class theclass[2])692 amd64_classify (struct type *type, enum amd64_reg_class theclass[2])
693 {
694 enum type_code code = type->code ();
695 int len = type->length ();
696
697 theclass[0] = theclass[1] = AMD64_NO_CLASS;
698
699 /* Arguments of types (signed and unsigned) _Bool, char, short, int,
700 long, long long, and pointers are in the INTEGER class. Similarly,
701 range types, used by languages such as Ada, are also in the INTEGER
702 class. */
703 if ((code == TYPE_CODE_INT || code == TYPE_CODE_ENUM
704 || code == TYPE_CODE_BOOL || code == TYPE_CODE_RANGE
705 || code == TYPE_CODE_CHAR
706 || code == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type))
707 && (len == 1 || len == 2 || len == 4 || len == 8))
708 theclass[0] = AMD64_INTEGER;
709
710 /* Arguments of types _Float16, float, double, _Decimal32, _Decimal64 and
711 __m64 are in class SSE. */
712 else if ((code == TYPE_CODE_FLT || code == TYPE_CODE_DECFLOAT)
713 && (len == 2 || len == 4 || len == 8))
714 /* FIXME: __m64 . */
715 theclass[0] = AMD64_SSE;
716
717 /* Arguments of types __float128, _Decimal128 and __m128 are split into
718 two halves. The least significant ones belong to class SSE, the most
719 significant one to class SSEUP. */
720 else if (code == TYPE_CODE_DECFLOAT && len == 16)
721 /* FIXME: __float128, __m128. */
722 theclass[0] = AMD64_SSE, theclass[1] = AMD64_SSEUP;
723
724 /* The 64-bit mantissa of arguments of type long double belongs to
725 class X87, the 16-bit exponent plus 6 bytes of padding belongs to
726 class X87UP. */
727 else if (code == TYPE_CODE_FLT && len == 16)
728 /* Class X87 and X87UP. */
729 theclass[0] = AMD64_X87, theclass[1] = AMD64_X87UP;
730
731 /* Arguments of complex T - where T is one of the types _Float16, float or
732 double - get treated as if they are implemented as:
733
734 struct complexT {
735 T real;
736 T imag;
737 };
738
739 */
740 else if (code == TYPE_CODE_COMPLEX && (len == 8 || len == 4))
741 theclass[0] = AMD64_SSE;
742 else if (code == TYPE_CODE_COMPLEX && len == 16)
743 theclass[0] = theclass[1] = AMD64_SSE;
744
745 /* A variable of type complex long double is classified as type
746 COMPLEX_X87. */
747 else if (code == TYPE_CODE_COMPLEX && len == 32)
748 theclass[0] = AMD64_COMPLEX_X87;
749
750 /* Aggregates. */
751 else if (code == TYPE_CODE_ARRAY || code == TYPE_CODE_STRUCT
752 || code == TYPE_CODE_UNION)
753 amd64_classify_aggregate (type, theclass);
754 }
755
756 static enum return_value_convention
amd64_return_value(struct gdbarch * gdbarch,struct value * function,struct type * type,struct regcache * regcache,struct value ** read_value,const gdb_byte * writebuf)757 amd64_return_value (struct gdbarch *gdbarch, struct value *function,
758 struct type *type, struct regcache *regcache,
759 struct value **read_value, const gdb_byte *writebuf)
760 {
761 enum amd64_reg_class theclass[2];
762 int len = type->length ();
763 static int integer_regnum[] = { AMD64_RAX_REGNUM, AMD64_RDX_REGNUM };
764 static int sse_regnum[] = { AMD64_XMM0_REGNUM, AMD64_XMM1_REGNUM };
765 int integer_reg = 0;
766 int sse_reg = 0;
767 int i;
768
769 gdb_assert (!(read_value && writebuf));
770
771 /* 1. Classify the return type with the classification algorithm. */
772 amd64_classify (type, theclass);
773
774 /* 2. If the type has class MEMORY, then the caller provides space
775 for the return value and passes the address of this storage in
776 %rdi as if it were the first argument to the function. In effect,
777 this address becomes a hidden first argument.
778
779 On return %rax will contain the address that has been passed in
780 by the caller in %rdi. */
781 if (theclass[0] == AMD64_MEMORY)
782 {
783 /* As indicated by the comment above, the ABI guarantees that we
784 can always find the return value just after the function has
785 returned. */
786
787 if (read_value != nullptr)
788 {
789 ULONGEST addr;
790
791 regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
792 *read_value = value_at_non_lval (type, addr);
793 }
794
795 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
796 }
797
798 gdb_byte *readbuf = nullptr;
799 if (read_value != nullptr)
800 {
801 *read_value = value::allocate (type);
802 readbuf = (*read_value)->contents_raw ().data ();
803 }
804
805 /* 8. If the class is COMPLEX_X87, the real part of the value is
806 returned in %st0 and the imaginary part in %st1. */
807 if (theclass[0] == AMD64_COMPLEX_X87)
808 {
809 if (readbuf)
810 {
811 regcache->raw_read (AMD64_ST0_REGNUM, readbuf);
812 regcache->raw_read (AMD64_ST1_REGNUM, readbuf + 16);
813 }
814
815 if (writebuf)
816 {
817 i387_return_value (gdbarch, regcache);
818 regcache->raw_write (AMD64_ST0_REGNUM, writebuf);
819 regcache->raw_write (AMD64_ST1_REGNUM, writebuf + 16);
820
821 /* Fix up the tag word such that both %st(0) and %st(1) are
822 marked as valid. */
823 regcache_raw_write_unsigned (regcache, AMD64_FTAG_REGNUM, 0xfff);
824 }
825
826 return RETURN_VALUE_REGISTER_CONVENTION;
827 }
828
829 gdb_assert (theclass[1] != AMD64_MEMORY);
830 gdb_assert (len <= 16);
831
832 for (i = 0; len > 0; i++, len -= 8)
833 {
834 int regnum = -1;
835 int offset = 0;
836
837 switch (theclass[i])
838 {
839 case AMD64_INTEGER:
840 /* 3. If the class is INTEGER, the next available register
841 of the sequence %rax, %rdx is used. */
842 regnum = integer_regnum[integer_reg++];
843 break;
844
845 case AMD64_SSE:
846 /* 4. If the class is SSE, the next available SSE register
847 of the sequence %xmm0, %xmm1 is used. */
848 regnum = sse_regnum[sse_reg++];
849 break;
850
851 case AMD64_SSEUP:
852 /* 5. If the class is SSEUP, the eightbyte is passed in the
853 upper half of the last used SSE register. */
854 gdb_assert (sse_reg > 0);
855 regnum = sse_regnum[sse_reg - 1];
856 offset = 8;
857 break;
858
859 case AMD64_X87:
860 /* 6. If the class is X87, the value is returned on the X87
861 stack in %st0 as 80-bit x87 number. */
862 regnum = AMD64_ST0_REGNUM;
863 if (writebuf)
864 i387_return_value (gdbarch, regcache);
865 break;
866
867 case AMD64_X87UP:
868 /* 7. If the class is X87UP, the value is returned together
869 with the previous X87 value in %st0. */
870 gdb_assert (i > 0 && theclass[0] == AMD64_X87);
871 regnum = AMD64_ST0_REGNUM;
872 offset = 8;
873 len = 2;
874 break;
875
876 case AMD64_NO_CLASS:
877 continue;
878
879 default:
880 gdb_assert (!"Unexpected register class.");
881 }
882
883 gdb_assert (regnum != -1);
884
885 if (readbuf)
886 regcache->raw_read_part (regnum, offset, std::min (len, 8),
887 readbuf + i * 8);
888 if (writebuf)
889 regcache->raw_write_part (regnum, offset, std::min (len, 8),
890 writebuf + i * 8);
891 }
892
893 return RETURN_VALUE_REGISTER_CONVENTION;
894 }
895
896
897 static CORE_ADDR
amd64_push_arguments(struct regcache * regcache,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method)898 amd64_push_arguments (struct regcache *regcache, int nargs, struct value **args,
899 CORE_ADDR sp, function_call_return_method return_method)
900 {
901 static int integer_regnum[] =
902 {
903 AMD64_RDI_REGNUM, /* %rdi */
904 AMD64_RSI_REGNUM, /* %rsi */
905 AMD64_RDX_REGNUM, /* %rdx */
906 AMD64_RCX_REGNUM, /* %rcx */
907 AMD64_R8_REGNUM, /* %r8 */
908 AMD64_R9_REGNUM /* %r9 */
909 };
910 static int sse_regnum[] =
911 {
912 /* %xmm0 ... %xmm7 */
913 AMD64_XMM0_REGNUM + 0, AMD64_XMM1_REGNUM,
914 AMD64_XMM0_REGNUM + 2, AMD64_XMM0_REGNUM + 3,
915 AMD64_XMM0_REGNUM + 4, AMD64_XMM0_REGNUM + 5,
916 AMD64_XMM0_REGNUM + 6, AMD64_XMM0_REGNUM + 7,
917 };
918 struct value **stack_args = XALLOCAVEC (struct value *, nargs);
919 int num_stack_args = 0;
920 int num_elements = 0;
921 int element = 0;
922 int integer_reg = 0;
923 int sse_reg = 0;
924 int i;
925
926 /* Reserve a register for the "hidden" argument. */
927 if (return_method == return_method_struct)
928 integer_reg++;
929
930 for (i = 0; i < nargs; i++)
931 {
932 struct type *type = args[i]->type ();
933 int len = type->length ();
934 enum amd64_reg_class theclass[2];
935 int needed_integer_regs = 0;
936 int needed_sse_regs = 0;
937 int j;
938
939 /* Classify argument. */
940 amd64_classify (type, theclass);
941
942 /* Calculate the number of integer and SSE registers needed for
943 this argument. */
944 for (j = 0; j < 2; j++)
945 {
946 if (theclass[j] == AMD64_INTEGER)
947 needed_integer_regs++;
948 else if (theclass[j] == AMD64_SSE)
949 needed_sse_regs++;
950 }
951
952 /* Check whether enough registers are available, and if the
953 argument should be passed in registers at all. */
954 if (integer_reg + needed_integer_regs > ARRAY_SIZE (integer_regnum)
955 || sse_reg + needed_sse_regs > ARRAY_SIZE (sse_regnum)
956 || (needed_integer_regs == 0 && needed_sse_regs == 0))
957 {
958 /* The argument will be passed on the stack. */
959 num_elements += ((len + 7) / 8);
960 stack_args[num_stack_args++] = args[i];
961 }
962 else
963 {
964 /* The argument will be passed in registers. */
965 const gdb_byte *valbuf = args[i]->contents ().data ();
966 gdb_byte buf[8];
967
968 gdb_assert (len <= 16);
969
970 for (j = 0; len > 0; j++, len -= 8)
971 {
972 int regnum = -1;
973 int offset = 0;
974
975 switch (theclass[j])
976 {
977 case AMD64_INTEGER:
978 regnum = integer_regnum[integer_reg++];
979 break;
980
981 case AMD64_SSE:
982 regnum = sse_regnum[sse_reg++];
983 break;
984
985 case AMD64_SSEUP:
986 gdb_assert (sse_reg > 0);
987 regnum = sse_regnum[sse_reg - 1];
988 offset = 8;
989 break;
990
991 case AMD64_NO_CLASS:
992 continue;
993
994 default:
995 gdb_assert (!"Unexpected register class.");
996 }
997
998 gdb_assert (regnum != -1);
999 memset (buf, 0, sizeof buf);
1000 memcpy (buf, valbuf + j * 8, std::min (len, 8));
1001 regcache->raw_write_part (regnum, offset, 8, buf);
1002 }
1003 }
1004 }
1005
1006 /* Allocate space for the arguments on the stack. */
1007 sp -= num_elements * 8;
1008
1009 /* The psABI says that "The end of the input argument area shall be
1010 aligned on a 16 byte boundary." */
1011 sp &= ~0xf;
1012
1013 /* Write out the arguments to the stack. */
1014 for (i = 0; i < num_stack_args; i++)
1015 {
1016 struct type *type = stack_args[i]->type ();
1017 const gdb_byte *valbuf = stack_args[i]->contents ().data ();
1018 int len = type->length ();
1019
1020 write_memory (sp + element * 8, valbuf, len);
1021 element += ((len + 7) / 8);
1022 }
1023
1024 /* The psABI says that "For calls that may call functions that use
1025 varargs or stdargs (prototype-less calls or calls to functions
1026 containing ellipsis (...) in the declaration) %al is used as
1027 hidden argument to specify the number of SSE registers used. */
1028 regcache_raw_write_unsigned (regcache, AMD64_RAX_REGNUM, sse_reg);
1029 return sp;
1030 }
1031
1032 static CORE_ADDR
amd64_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)1033 amd64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
1034 struct regcache *regcache, CORE_ADDR bp_addr,
1035 int nargs, struct value **args, CORE_ADDR sp,
1036 function_call_return_method return_method,
1037 CORE_ADDR struct_addr)
1038 {
1039 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1040 gdb_byte buf[8];
1041
1042 /* BND registers can be in arbitrary values at the moment of the
1043 inferior call. This can cause boundary violations that are not
1044 due to a real bug or even desired by the user. The best to be done
1045 is set the BND registers to allow access to the whole memory, INIT
1046 state, before pushing the inferior call. */
1047 i387_reset_bnd_regs (gdbarch, regcache);
1048
1049 /* Pass arguments. */
1050 sp = amd64_push_arguments (regcache, nargs, args, sp, return_method);
1051
1052 /* Pass "hidden" argument". */
1053 if (return_method == return_method_struct)
1054 {
1055 store_unsigned_integer (buf, 8, byte_order, struct_addr);
1056 regcache->cooked_write (AMD64_RDI_REGNUM, buf);
1057 }
1058
1059 /* Store return address. */
1060 sp -= 8;
1061 store_unsigned_integer (buf, 8, byte_order, bp_addr);
1062 write_memory (sp, buf, 8);
1063
1064 /* Finally, update the stack pointer... */
1065 store_unsigned_integer (buf, 8, byte_order, sp);
1066 regcache->cooked_write (AMD64_RSP_REGNUM, buf);
1067
1068 /* ...and fake a frame pointer. */
1069 regcache->cooked_write (AMD64_RBP_REGNUM, buf);
1070
1071 return sp + 16;
1072 }
1073
1074 /* Displaced instruction handling. */
1075
1076 /* A partially decoded instruction.
1077 This contains enough details for displaced stepping purposes. */
1078
1079 struct amd64_insn
1080 {
1081 /* The number of opcode bytes. */
1082 int opcode_len;
1083 /* The offset of the REX/VEX instruction encoding prefix or -1 if
1084 not present. */
1085 int enc_prefix_offset;
1086 /* The offset to the first opcode byte. */
1087 int opcode_offset;
1088 /* The offset to the modrm byte or -1 if not present. */
1089 int modrm_offset;
1090
1091 /* The raw instruction. */
1092 gdb_byte *raw_insn;
1093 };
1094
1095 struct amd64_displaced_step_copy_insn_closure
1096 : public displaced_step_copy_insn_closure
1097 {
amd64_displaced_step_copy_insn_closureamd64_displaced_step_copy_insn_closure1098 amd64_displaced_step_copy_insn_closure (int insn_buf_len)
1099 : insn_buf (insn_buf_len, 0)
1100 {}
1101
1102 /* For rip-relative insns, saved copy of the reg we use instead of %rip. */
1103 int tmp_used = 0;
1104 int tmp_regno;
1105 ULONGEST tmp_save;
1106
1107 /* Details of the instruction. */
1108 struct amd64_insn insn_details;
1109
1110 /* The possibly modified insn. */
1111 gdb::byte_vector insn_buf;
1112 };
1113
1114 /* WARNING: Keep onebyte_has_modrm, twobyte_has_modrm in sync with
1115 ../opcodes/i386-dis.c (until libopcodes exports them, or an alternative,
1116 at which point delete these in favor of libopcodes' versions). */
1117
1118 static const unsigned char onebyte_has_modrm[256] = {
1119 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1120 /* ------------------------------- */
1121 /* 00 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 00 */
1122 /* 10 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 10 */
1123 /* 20 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 20 */
1124 /* 30 */ 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0, /* 30 */
1125 /* 40 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 40 */
1126 /* 50 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 50 */
1127 /* 60 */ 0,0,1,1,0,0,0,0,0,1,0,1,0,0,0,0, /* 60 */
1128 /* 70 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 70 */
1129 /* 80 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 80 */
1130 /* 90 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 90 */
1131 /* a0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* a0 */
1132 /* b0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* b0 */
1133 /* c0 */ 1,1,0,0,1,1,1,1,0,0,0,0,0,0,0,0, /* c0 */
1134 /* d0 */ 1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1, /* d0 */
1135 /* e0 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* e0 */
1136 /* f0 */ 0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1 /* f0 */
1137 /* ------------------------------- */
1138 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1139 };
1140
1141 static const unsigned char twobyte_has_modrm[256] = {
1142 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1143 /* ------------------------------- */
1144 /* 00 */ 1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,1, /* 0f */
1145 /* 10 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 1f */
1146 /* 20 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 2f */
1147 /* 30 */ 0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0, /* 3f */
1148 /* 40 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 4f */
1149 /* 50 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 5f */
1150 /* 60 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 6f */
1151 /* 70 */ 1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1, /* 7f */
1152 /* 80 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 8f */
1153 /* 90 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 9f */
1154 /* a0 */ 0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1, /* af */
1155 /* b0 */ 1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1, /* bf */
1156 /* c0 */ 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0, /* cf */
1157 /* d0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* df */
1158 /* e0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* ef */
1159 /* f0 */ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0 /* ff */
1160 /* ------------------------------- */
1161 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
1162 };
1163
1164 static int amd64_syscall_p (const struct amd64_insn *insn, int *lengthp);
1165
1166 static int
rex_prefix_p(gdb_byte pfx)1167 rex_prefix_p (gdb_byte pfx)
1168 {
1169 return REX_PREFIX_P (pfx);
1170 }
1171
1172 /* True if PFX is the start of the 2-byte VEX prefix. */
1173
1174 static bool
vex2_prefix_p(gdb_byte pfx)1175 vex2_prefix_p (gdb_byte pfx)
1176 {
1177 return pfx == 0xc5;
1178 }
1179
1180 /* True if PFX is the start of the 3-byte VEX prefix. */
1181
1182 static bool
vex3_prefix_p(gdb_byte pfx)1183 vex3_prefix_p (gdb_byte pfx)
1184 {
1185 return pfx == 0xc4;
1186 }
1187
1188 /* Skip the legacy instruction prefixes in INSN.
1189 We assume INSN is properly sentineled so we don't have to worry
1190 about falling off the end of the buffer. */
1191
1192 static gdb_byte *
amd64_skip_prefixes(gdb_byte * insn)1193 amd64_skip_prefixes (gdb_byte *insn)
1194 {
1195 while (1)
1196 {
1197 switch (*insn)
1198 {
1199 case DATA_PREFIX_OPCODE:
1200 case ADDR_PREFIX_OPCODE:
1201 case CS_PREFIX_OPCODE:
1202 case DS_PREFIX_OPCODE:
1203 case ES_PREFIX_OPCODE:
1204 case FS_PREFIX_OPCODE:
1205 case GS_PREFIX_OPCODE:
1206 case SS_PREFIX_OPCODE:
1207 case LOCK_PREFIX_OPCODE:
1208 case REPE_PREFIX_OPCODE:
1209 case REPNE_PREFIX_OPCODE:
1210 ++insn;
1211 continue;
1212 default:
1213 break;
1214 }
1215 break;
1216 }
1217
1218 return insn;
1219 }
1220
1221 /* Return an integer register (other than RSP) that is unused as an input
1222 operand in INSN.
1223 In order to not require adding a rex prefix if the insn doesn't already
1224 have one, the result is restricted to RAX ... RDI, sans RSP.
1225 The register numbering of the result follows architecture ordering,
1226 e.g. RDI = 7. */
1227
1228 static int
amd64_get_unused_input_int_reg(const struct amd64_insn * details)1229 amd64_get_unused_input_int_reg (const struct amd64_insn *details)
1230 {
1231 /* 1 bit for each reg */
1232 int used_regs_mask = 0;
1233
1234 /* There can be at most 3 int regs used as inputs in an insn, and we have
1235 7 to choose from (RAX ... RDI, sans RSP).
1236 This allows us to take a conservative approach and keep things simple.
1237 E.g. By avoiding RAX, we don't have to specifically watch for opcodes
1238 that implicitly specify RAX. */
1239
1240 /* Avoid RAX. */
1241 used_regs_mask |= 1 << EAX_REG_NUM;
1242 /* Similarily avoid RDX, implicit operand in divides. */
1243 used_regs_mask |= 1 << EDX_REG_NUM;
1244 /* Avoid RSP. */
1245 used_regs_mask |= 1 << ESP_REG_NUM;
1246
1247 /* If the opcode is one byte long and there's no ModRM byte,
1248 assume the opcode specifies a register. */
1249 if (details->opcode_len == 1 && details->modrm_offset == -1)
1250 used_regs_mask |= 1 << (details->raw_insn[details->opcode_offset] & 7);
1251
1252 /* Mark used regs in the modrm/sib bytes. */
1253 if (details->modrm_offset != -1)
1254 {
1255 int modrm = details->raw_insn[details->modrm_offset];
1256 int mod = MODRM_MOD_FIELD (modrm);
1257 int reg = MODRM_REG_FIELD (modrm);
1258 int rm = MODRM_RM_FIELD (modrm);
1259 int have_sib = mod != 3 && rm == 4;
1260
1261 /* Assume the reg field of the modrm byte specifies a register. */
1262 used_regs_mask |= 1 << reg;
1263
1264 if (have_sib)
1265 {
1266 int base = SIB_BASE_FIELD (details->raw_insn[details->modrm_offset + 1]);
1267 int idx = SIB_INDEX_FIELD (details->raw_insn[details->modrm_offset + 1]);
1268 used_regs_mask |= 1 << base;
1269 used_regs_mask |= 1 << idx;
1270 }
1271 else
1272 {
1273 used_regs_mask |= 1 << rm;
1274 }
1275 }
1276
1277 gdb_assert (used_regs_mask < 256);
1278 gdb_assert (used_regs_mask != 255);
1279
1280 /* Finally, find a free reg. */
1281 {
1282 int i;
1283
1284 for (i = 0; i < 8; ++i)
1285 {
1286 if (! (used_regs_mask & (1 << i)))
1287 return i;
1288 }
1289
1290 /* We shouldn't get here. */
1291 internal_error (_("unable to find free reg"));
1292 }
1293 }
1294
1295 /* Extract the details of INSN that we need. */
1296
1297 static void
amd64_get_insn_details(gdb_byte * insn,struct amd64_insn * details)1298 amd64_get_insn_details (gdb_byte *insn, struct amd64_insn *details)
1299 {
1300 gdb_byte *start = insn;
1301 int need_modrm;
1302
1303 details->raw_insn = insn;
1304
1305 details->opcode_len = -1;
1306 details->enc_prefix_offset = -1;
1307 details->opcode_offset = -1;
1308 details->modrm_offset = -1;
1309
1310 /* Skip legacy instruction prefixes. */
1311 insn = amd64_skip_prefixes (insn);
1312
1313 /* Skip REX/VEX instruction encoding prefixes. */
1314 if (rex_prefix_p (*insn))
1315 {
1316 details->enc_prefix_offset = insn - start;
1317 ++insn;
1318 }
1319 else if (vex2_prefix_p (*insn))
1320 {
1321 /* Don't record the offset in this case because this prefix has
1322 no REX.B equivalent. */
1323 insn += 2;
1324 }
1325 else if (vex3_prefix_p (*insn))
1326 {
1327 details->enc_prefix_offset = insn - start;
1328 insn += 3;
1329 }
1330
1331 details->opcode_offset = insn - start;
1332
1333 if (*insn == TWO_BYTE_OPCODE_ESCAPE)
1334 {
1335 /* Two or three-byte opcode. */
1336 ++insn;
1337 need_modrm = twobyte_has_modrm[*insn];
1338
1339 /* Check for three-byte opcode. */
1340 switch (*insn)
1341 {
1342 case 0x24:
1343 case 0x25:
1344 case 0x38:
1345 case 0x3a:
1346 case 0x7a:
1347 case 0x7b:
1348 ++insn;
1349 details->opcode_len = 3;
1350 break;
1351 default:
1352 details->opcode_len = 2;
1353 break;
1354 }
1355 }
1356 else
1357 {
1358 /* One-byte opcode. */
1359 need_modrm = onebyte_has_modrm[*insn];
1360 details->opcode_len = 1;
1361 }
1362
1363 if (need_modrm)
1364 {
1365 ++insn;
1366 details->modrm_offset = insn - start;
1367 }
1368 }
1369
1370 /* Update %rip-relative addressing in INSN.
1371
1372 %rip-relative addressing only uses a 32-bit displacement.
1373 32 bits is not enough to be guaranteed to cover the distance between where
1374 the real instruction is and where its copy is.
1375 Convert the insn to use base+disp addressing.
1376 We set base = pc + insn_length so we can leave disp unchanged. */
1377
1378 static void
fixup_riprel(struct gdbarch * gdbarch,amd64_displaced_step_copy_insn_closure * dsc,CORE_ADDR from,CORE_ADDR to,struct regcache * regs)1379 fixup_riprel (struct gdbarch *gdbarch,
1380 amd64_displaced_step_copy_insn_closure *dsc,
1381 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1382 {
1383 const struct amd64_insn *insn_details = &dsc->insn_details;
1384 int modrm_offset = insn_details->modrm_offset;
1385 CORE_ADDR rip_base;
1386 int insn_length;
1387 int arch_tmp_regno, tmp_regno;
1388 ULONGEST orig_value;
1389
1390 /* Compute the rip-relative address. */
1391 insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf.data (),
1392 dsc->insn_buf.size (), from);
1393 rip_base = from + insn_length;
1394
1395 /* We need a register to hold the address.
1396 Pick one not used in the insn.
1397 NOTE: arch_tmp_regno uses architecture ordering, e.g. RDI = 7. */
1398 arch_tmp_regno = amd64_get_unused_input_int_reg (insn_details);
1399 tmp_regno = amd64_arch_reg_to_regnum (arch_tmp_regno);
1400
1401 /* Position of the not-B bit in the 3-byte VEX prefix (in byte 1). */
1402 static constexpr gdb_byte VEX3_NOT_B = 0x20;
1403
1404 /* REX.B should be unset (VEX.!B set) as we were using rip-relative
1405 addressing, but ensure it's unset (set for VEX) anyway, tmp_regno
1406 is not r8-r15. */
1407 if (insn_details->enc_prefix_offset != -1)
1408 {
1409 gdb_byte *pfx = &dsc->insn_buf[insn_details->enc_prefix_offset];
1410 if (rex_prefix_p (pfx[0]))
1411 pfx[0] &= ~REX_B;
1412 else if (vex3_prefix_p (pfx[0]))
1413 pfx[1] |= VEX3_NOT_B;
1414 else
1415 gdb_assert_not_reached ("unhandled prefix");
1416 }
1417
1418 regcache_cooked_read_unsigned (regs, tmp_regno, &orig_value);
1419 dsc->tmp_regno = tmp_regno;
1420 dsc->tmp_save = orig_value;
1421 dsc->tmp_used = 1;
1422
1423 /* Convert the ModRM field to be base+disp. */
1424 dsc->insn_buf[modrm_offset] &= ~0xc7;
1425 dsc->insn_buf[modrm_offset] |= 0x80 + arch_tmp_regno;
1426
1427 regcache_cooked_write_unsigned (regs, tmp_regno, rip_base);
1428
1429 displaced_debug_printf ("%%rip-relative addressing used.");
1430 displaced_debug_printf ("using temp reg %d, old value %s, new value %s",
1431 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save),
1432 paddress (gdbarch, rip_base));
1433 }
1434
1435 static void
fixup_displaced_copy(struct gdbarch * gdbarch,amd64_displaced_step_copy_insn_closure * dsc,CORE_ADDR from,CORE_ADDR to,struct regcache * regs)1436 fixup_displaced_copy (struct gdbarch *gdbarch,
1437 amd64_displaced_step_copy_insn_closure *dsc,
1438 CORE_ADDR from, CORE_ADDR to, struct regcache *regs)
1439 {
1440 const struct amd64_insn *details = &dsc->insn_details;
1441
1442 if (details->modrm_offset != -1)
1443 {
1444 gdb_byte modrm = details->raw_insn[details->modrm_offset];
1445
1446 if ((modrm & 0xc7) == 0x05)
1447 {
1448 /* The insn uses rip-relative addressing.
1449 Deal with it. */
1450 fixup_riprel (gdbarch, dsc, from, to, regs);
1451 }
1452 }
1453 }
1454
1455 displaced_step_copy_insn_closure_up
amd64_displaced_step_copy_insn(struct gdbarch * gdbarch,CORE_ADDR from,CORE_ADDR to,struct regcache * regs)1456 amd64_displaced_step_copy_insn (struct gdbarch *gdbarch,
1457 CORE_ADDR from, CORE_ADDR to,
1458 struct regcache *regs)
1459 {
1460 int len = gdbarch_max_insn_length (gdbarch);
1461 /* Extra space for sentinels so fixup_{riprel,displaced_copy} don't have to
1462 continually watch for running off the end of the buffer. */
1463 int fixup_sentinel_space = len;
1464 std::unique_ptr<amd64_displaced_step_copy_insn_closure> dsc
1465 (new amd64_displaced_step_copy_insn_closure (len + fixup_sentinel_space));
1466 gdb_byte *buf = &dsc->insn_buf[0];
1467 struct amd64_insn *details = &dsc->insn_details;
1468
1469 read_memory (from, buf, len);
1470
1471 /* Set up the sentinel space so we don't have to worry about running
1472 off the end of the buffer. An excessive number of leading prefixes
1473 could otherwise cause this. */
1474 memset (buf + len, 0, fixup_sentinel_space);
1475
1476 amd64_get_insn_details (buf, details);
1477
1478 /* GDB may get control back after the insn after the syscall.
1479 Presumably this is a kernel bug.
1480 If this is a syscall, make sure there's a nop afterwards. */
1481 {
1482 int syscall_length;
1483
1484 if (amd64_syscall_p (details, &syscall_length))
1485 buf[details->opcode_offset + syscall_length] = NOP_OPCODE;
1486 }
1487
1488 /* Modify the insn to cope with the address where it will be executed from.
1489 In particular, handle any rip-relative addressing. */
1490 fixup_displaced_copy (gdbarch, dsc.get (), from, to, regs);
1491
1492 write_memory (to, buf, len);
1493
1494 displaced_debug_printf ("copy %s->%s: %s",
1495 paddress (gdbarch, from), paddress (gdbarch, to),
1496 bytes_to_string (buf, len).c_str ());
1497
1498 /* This is a work around for a problem with g++ 4.8. */
1499 return displaced_step_copy_insn_closure_up (dsc.release ());
1500 }
1501
1502 static int
amd64_absolute_jmp_p(const struct amd64_insn * details)1503 amd64_absolute_jmp_p (const struct amd64_insn *details)
1504 {
1505 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1506
1507 if (insn[0] == 0xff)
1508 {
1509 /* jump near, absolute indirect (/4) */
1510 if ((insn[1] & 0x38) == 0x20)
1511 return 1;
1512
1513 /* jump far, absolute indirect (/5) */
1514 if ((insn[1] & 0x38) == 0x28)
1515 return 1;
1516 }
1517
1518 return 0;
1519 }
1520
1521 /* Return non-zero if the instruction DETAILS is a jump, zero otherwise. */
1522
1523 static int
amd64_jmp_p(const struct amd64_insn * details)1524 amd64_jmp_p (const struct amd64_insn *details)
1525 {
1526 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1527
1528 /* jump short, relative. */
1529 if (insn[0] == 0xeb)
1530 return 1;
1531
1532 /* jump near, relative. */
1533 if (insn[0] == 0xe9)
1534 return 1;
1535
1536 return amd64_absolute_jmp_p (details);
1537 }
1538
1539 static int
amd64_absolute_call_p(const struct amd64_insn * details)1540 amd64_absolute_call_p (const struct amd64_insn *details)
1541 {
1542 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1543
1544 if (insn[0] == 0xff)
1545 {
1546 /* Call near, absolute indirect (/2) */
1547 if ((insn[1] & 0x38) == 0x10)
1548 return 1;
1549
1550 /* Call far, absolute indirect (/3) */
1551 if ((insn[1] & 0x38) == 0x18)
1552 return 1;
1553 }
1554
1555 return 0;
1556 }
1557
1558 static int
amd64_ret_p(const struct amd64_insn * details)1559 amd64_ret_p (const struct amd64_insn *details)
1560 {
1561 /* NOTE: gcc can emit "repz ; ret". */
1562 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1563
1564 switch (insn[0])
1565 {
1566 case 0xc2: /* ret near, pop N bytes */
1567 case 0xc3: /* ret near */
1568 case 0xca: /* ret far, pop N bytes */
1569 case 0xcb: /* ret far */
1570 case 0xcf: /* iret */
1571 return 1;
1572
1573 default:
1574 return 0;
1575 }
1576 }
1577
1578 static int
amd64_call_p(const struct amd64_insn * details)1579 amd64_call_p (const struct amd64_insn *details)
1580 {
1581 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1582
1583 if (amd64_absolute_call_p (details))
1584 return 1;
1585
1586 /* call near, relative */
1587 if (insn[0] == 0xe8)
1588 return 1;
1589
1590 return 0;
1591 }
1592
1593 /* Return non-zero if INSN is a system call, and set *LENGTHP to its
1594 length in bytes. Otherwise, return zero. */
1595
1596 static int
amd64_syscall_p(const struct amd64_insn * details,int * lengthp)1597 amd64_syscall_p (const struct amd64_insn *details, int *lengthp)
1598 {
1599 const gdb_byte *insn = &details->raw_insn[details->opcode_offset];
1600
1601 if (insn[0] == 0x0f && insn[1] == 0x05)
1602 {
1603 *lengthp = 2;
1604 return 1;
1605 }
1606
1607 return 0;
1608 }
1609
1610 /* Classify the instruction at ADDR using PRED.
1611 Throw an error if the memory can't be read. */
1612
1613 static int
amd64_classify_insn_at(struct gdbarch * gdbarch,CORE_ADDR addr,int (* pred)(const struct amd64_insn *))1614 amd64_classify_insn_at (struct gdbarch *gdbarch, CORE_ADDR addr,
1615 int (*pred) (const struct amd64_insn *))
1616 {
1617 struct amd64_insn details;
1618
1619 gdb::byte_vector buf (gdbarch_max_insn_length (gdbarch));
1620
1621 read_code (addr, buf.data (), buf.size ());
1622 amd64_get_insn_details (buf.data (), &details);
1623
1624 int classification = pred (&details);
1625
1626 return classification;
1627 }
1628
1629 /* The gdbarch insn_is_call method. */
1630
1631 static int
amd64_insn_is_call(struct gdbarch * gdbarch,CORE_ADDR addr)1632 amd64_insn_is_call (struct gdbarch *gdbarch, CORE_ADDR addr)
1633 {
1634 return amd64_classify_insn_at (gdbarch, addr, amd64_call_p);
1635 }
1636
1637 /* The gdbarch insn_is_ret method. */
1638
1639 static int
amd64_insn_is_ret(struct gdbarch * gdbarch,CORE_ADDR addr)1640 amd64_insn_is_ret (struct gdbarch *gdbarch, CORE_ADDR addr)
1641 {
1642 return amd64_classify_insn_at (gdbarch, addr, amd64_ret_p);
1643 }
1644
1645 /* The gdbarch insn_is_jump method. */
1646
1647 static int
amd64_insn_is_jump(struct gdbarch * gdbarch,CORE_ADDR addr)1648 amd64_insn_is_jump (struct gdbarch *gdbarch, CORE_ADDR addr)
1649 {
1650 return amd64_classify_insn_at (gdbarch, addr, amd64_jmp_p);
1651 }
1652
1653 /* Fix up the state of registers and memory after having single-stepped
1654 a displaced instruction. */
1655
1656 void
amd64_displaced_step_fixup(struct gdbarch * gdbarch,struct displaced_step_copy_insn_closure * dsc_,CORE_ADDR from,CORE_ADDR to,struct regcache * regs,bool completed_p)1657 amd64_displaced_step_fixup (struct gdbarch *gdbarch,
1658 struct displaced_step_copy_insn_closure *dsc_,
1659 CORE_ADDR from, CORE_ADDR to,
1660 struct regcache *regs, bool completed_p)
1661 {
1662 amd64_displaced_step_copy_insn_closure *dsc
1663 = (amd64_displaced_step_copy_insn_closure *) dsc_;
1664 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1665 /* The offset we applied to the instruction's address. */
1666 ULONGEST insn_offset = to - from;
1667 gdb_byte *insn = dsc->insn_buf.data ();
1668 const struct amd64_insn *insn_details = &dsc->insn_details;
1669
1670 displaced_debug_printf ("fixup (%s, %s), insn = 0x%02x 0x%02x ...",
1671 paddress (gdbarch, from), paddress (gdbarch, to),
1672 insn[0], insn[1]);
1673
1674 /* If we used a tmp reg, restore it. */
1675
1676 if (dsc->tmp_used)
1677 {
1678 displaced_debug_printf ("restoring reg %d to %s",
1679 dsc->tmp_regno, paddress (gdbarch, dsc->tmp_save));
1680 regcache_cooked_write_unsigned (regs, dsc->tmp_regno, dsc->tmp_save);
1681 }
1682
1683 /* The list of issues to contend with here is taken from
1684 resume_execution in arch/x86/kernel/kprobes.c, Linux 2.6.28.
1685 Yay for Free Software! */
1686
1687 /* Relocate the %rip back to the program's instruction stream,
1688 if necessary. */
1689
1690 /* Except in the case of absolute or indirect jump or call
1691 instructions, or a return instruction, the new rip is relative to
1692 the displaced instruction; make it relative to the original insn.
1693 Well, signal handler returns don't need relocation either, but we use the
1694 value of %rip to recognize those; see below. */
1695 if (!completed_p
1696 || (!amd64_absolute_jmp_p (insn_details)
1697 && !amd64_absolute_call_p (insn_details)
1698 && !amd64_ret_p (insn_details)))
1699 {
1700 int insn_len;
1701
1702 CORE_ADDR pc = regcache_read_pc (regs);
1703
1704 /* A signal trampoline system call changes the %rip, resuming
1705 execution of the main program after the signal handler has
1706 returned. That makes them like 'return' instructions; we
1707 shouldn't relocate %rip.
1708
1709 But most system calls don't, and we do need to relocate %rip.
1710
1711 Our heuristic for distinguishing these cases: if stepping
1712 over the system call instruction left control directly after
1713 the instruction, the we relocate --- control almost certainly
1714 doesn't belong in the displaced copy. Otherwise, we assume
1715 the instruction has put control where it belongs, and leave
1716 it unrelocated. Goodness help us if there are PC-relative
1717 system calls. */
1718 if (amd64_syscall_p (insn_details, &insn_len)
1719 /* GDB can get control back after the insn after the syscall.
1720 Presumably this is a kernel bug. Fixup ensures it's a nop, we
1721 add one to the length for it. */
1722 && (pc < to || pc > (to + insn_len + 1)))
1723 displaced_debug_printf ("syscall changed %%rip; not relocating");
1724 else
1725 {
1726 CORE_ADDR rip = pc - insn_offset;
1727
1728 /* If we just stepped over a breakpoint insn, we don't backup
1729 the pc on purpose; this is to match behaviour without
1730 stepping. */
1731
1732 regcache_write_pc (regs, rip);
1733
1734 displaced_debug_printf ("relocated %%rip from %s to %s",
1735 paddress (gdbarch, pc),
1736 paddress (gdbarch, rip));
1737 }
1738 }
1739
1740 /* If the instruction was PUSHFL, then the TF bit will be set in the
1741 pushed value, and should be cleared. We'll leave this for later,
1742 since GDB already messes up the TF flag when stepping over a
1743 pushfl. */
1744
1745 /* If the instruction was a call, the return address now atop the
1746 stack is the address following the copied instruction. We need
1747 to make it the address following the original instruction. */
1748 if (completed_p && amd64_call_p (insn_details))
1749 {
1750 ULONGEST rsp;
1751 ULONGEST retaddr;
1752 const ULONGEST retaddr_len = 8;
1753
1754 regcache_cooked_read_unsigned (regs, AMD64_RSP_REGNUM, &rsp);
1755 retaddr = read_memory_unsigned_integer (rsp, retaddr_len, byte_order);
1756 retaddr = (retaddr - insn_offset) & 0xffffffffffffffffULL;
1757 write_memory_unsigned_integer (rsp, retaddr_len, byte_order, retaddr);
1758
1759 displaced_debug_printf ("relocated return addr at %s to %s",
1760 paddress (gdbarch, rsp),
1761 paddress (gdbarch, retaddr));
1762 }
1763 }
1764
1765 /* If the instruction INSN uses RIP-relative addressing, return the
1766 offset into the raw INSN where the displacement to be adjusted is
1767 found. Returns 0 if the instruction doesn't use RIP-relative
1768 addressing. */
1769
1770 static int
rip_relative_offset(struct amd64_insn * insn)1771 rip_relative_offset (struct amd64_insn *insn)
1772 {
1773 if (insn->modrm_offset != -1)
1774 {
1775 gdb_byte modrm = insn->raw_insn[insn->modrm_offset];
1776
1777 if ((modrm & 0xc7) == 0x05)
1778 {
1779 /* The displacement is found right after the ModRM byte. */
1780 return insn->modrm_offset + 1;
1781 }
1782 }
1783
1784 return 0;
1785 }
1786
1787 static void
append_insns(CORE_ADDR * to,ULONGEST len,const gdb_byte * buf)1788 append_insns (CORE_ADDR *to, ULONGEST len, const gdb_byte *buf)
1789 {
1790 target_write_memory (*to, buf, len);
1791 *to += len;
1792 }
1793
1794 static void
amd64_relocate_instruction(struct gdbarch * gdbarch,CORE_ADDR * to,CORE_ADDR oldloc)1795 amd64_relocate_instruction (struct gdbarch *gdbarch,
1796 CORE_ADDR *to, CORE_ADDR oldloc)
1797 {
1798 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1799 int len = gdbarch_max_insn_length (gdbarch);
1800 /* Extra space for sentinels. */
1801 int fixup_sentinel_space = len;
1802 gdb::byte_vector buf (len + fixup_sentinel_space);
1803 struct amd64_insn insn_details;
1804 int offset = 0;
1805 LONGEST rel32, newrel;
1806 gdb_byte *insn;
1807 int insn_length;
1808
1809 read_memory (oldloc, buf.data (), len);
1810
1811 /* Set up the sentinel space so we don't have to worry about running
1812 off the end of the buffer. An excessive number of leading prefixes
1813 could otherwise cause this. */
1814 memset (buf.data () + len, 0, fixup_sentinel_space);
1815
1816 insn = buf.data ();
1817 amd64_get_insn_details (insn, &insn_details);
1818
1819 insn_length = gdb_buffered_insn_length (gdbarch, insn, len, oldloc);
1820
1821 /* Skip legacy instruction prefixes. */
1822 insn = amd64_skip_prefixes (insn);
1823
1824 /* Adjust calls with 32-bit relative addresses as push/jump, with
1825 the address pushed being the location where the original call in
1826 the user program would return to. */
1827 if (insn[0] == 0xe8)
1828 {
1829 gdb_byte push_buf[32];
1830 CORE_ADDR ret_addr;
1831 int i = 0;
1832
1833 /* Where "ret" in the original code will return to. */
1834 ret_addr = oldloc + insn_length;
1835
1836 /* If pushing an address higher than or equal to 0x80000000,
1837 avoid 'pushq', as that sign extends its 32-bit operand, which
1838 would be incorrect. */
1839 if (ret_addr <= 0x7fffffff)
1840 {
1841 push_buf[0] = 0x68; /* pushq $... */
1842 store_unsigned_integer (&push_buf[1], 4, byte_order, ret_addr);
1843 i = 5;
1844 }
1845 else
1846 {
1847 push_buf[i++] = 0x48; /* sub $0x8,%rsp */
1848 push_buf[i++] = 0x83;
1849 push_buf[i++] = 0xec;
1850 push_buf[i++] = 0x08;
1851
1852 push_buf[i++] = 0xc7; /* movl $imm,(%rsp) */
1853 push_buf[i++] = 0x04;
1854 push_buf[i++] = 0x24;
1855 store_unsigned_integer (&push_buf[i], 4, byte_order,
1856 ret_addr & 0xffffffff);
1857 i += 4;
1858
1859 push_buf[i++] = 0xc7; /* movl $imm,4(%rsp) */
1860 push_buf[i++] = 0x44;
1861 push_buf[i++] = 0x24;
1862 push_buf[i++] = 0x04;
1863 store_unsigned_integer (&push_buf[i], 4, byte_order,
1864 ret_addr >> 32);
1865 i += 4;
1866 }
1867 gdb_assert (i <= sizeof (push_buf));
1868 /* Push the push. */
1869 append_insns (to, i, push_buf);
1870
1871 /* Convert the relative call to a relative jump. */
1872 insn[0] = 0xe9;
1873
1874 /* Adjust the destination offset. */
1875 rel32 = extract_signed_integer (insn + 1, 4, byte_order);
1876 newrel = (oldloc - *to) + rel32;
1877 store_signed_integer (insn + 1, 4, byte_order, newrel);
1878
1879 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
1880 hex_string (rel32), paddress (gdbarch, oldloc),
1881 hex_string (newrel), paddress (gdbarch, *to));
1882
1883 /* Write the adjusted jump into its displaced location. */
1884 append_insns (to, 5, insn);
1885 return;
1886 }
1887
1888 offset = rip_relative_offset (&insn_details);
1889 if (!offset)
1890 {
1891 /* Adjust jumps with 32-bit relative addresses. Calls are
1892 already handled above. */
1893 if (insn[0] == 0xe9)
1894 offset = 1;
1895 /* Adjust conditional jumps. */
1896 else if (insn[0] == 0x0f && (insn[1] & 0xf0) == 0x80)
1897 offset = 2;
1898 }
1899
1900 if (offset)
1901 {
1902 rel32 = extract_signed_integer (insn + offset, 4, byte_order);
1903 newrel = (oldloc - *to) + rel32;
1904 store_signed_integer (insn + offset, 4, byte_order, newrel);
1905 displaced_debug_printf ("adjusted insn rel32=%s at %s to rel32=%s at %s",
1906 hex_string (rel32), paddress (gdbarch, oldloc),
1907 hex_string (newrel), paddress (gdbarch, *to));
1908 }
1909
1910 /* Write the adjusted instruction into its displaced location. */
1911 append_insns (to, insn_length, buf.data ());
1912 }
1913
1914
1915 /* The maximum number of saved registers. This should include %rip. */
1916 #define AMD64_NUM_SAVED_REGS AMD64_NUM_GREGS
1917
1918 struct amd64_frame_cache
1919 {
1920 /* Base address. */
1921 CORE_ADDR base;
1922 int base_p;
1923 CORE_ADDR sp_offset;
1924 CORE_ADDR pc;
1925
1926 /* Saved registers. */
1927 CORE_ADDR saved_regs[AMD64_NUM_SAVED_REGS];
1928 CORE_ADDR saved_sp;
1929 int saved_sp_reg;
1930
1931 /* Do we have a frame? */
1932 int frameless_p;
1933 };
1934
1935 /* Initialize a frame cache. */
1936
1937 static void
amd64_init_frame_cache(struct amd64_frame_cache * cache)1938 amd64_init_frame_cache (struct amd64_frame_cache *cache)
1939 {
1940 int i;
1941
1942 /* Base address. */
1943 cache->base = 0;
1944 cache->base_p = 0;
1945 cache->sp_offset = -8;
1946 cache->pc = 0;
1947
1948 /* Saved registers. We initialize these to -1 since zero is a valid
1949 offset (that's where %rbp is supposed to be stored).
1950 The values start out as being offsets, and are later converted to
1951 addresses (at which point -1 is interpreted as an address, still meaning
1952 "invalid"). */
1953 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
1954 cache->saved_regs[i] = -1;
1955 cache->saved_sp = 0;
1956 cache->saved_sp_reg = -1;
1957
1958 /* Frameless until proven otherwise. */
1959 cache->frameless_p = 1;
1960 }
1961
1962 /* Allocate and initialize a frame cache. */
1963
1964 static struct amd64_frame_cache *
amd64_alloc_frame_cache(void)1965 amd64_alloc_frame_cache (void)
1966 {
1967 struct amd64_frame_cache *cache;
1968
1969 cache = FRAME_OBSTACK_ZALLOC (struct amd64_frame_cache);
1970 amd64_init_frame_cache (cache);
1971 return cache;
1972 }
1973
1974 /* GCC 4.4 and later, can put code in the prologue to realign the
1975 stack pointer. Check whether PC points to such code, and update
1976 CACHE accordingly. Return the first instruction after the code
1977 sequence or CURRENT_PC, whichever is smaller. If we don't
1978 recognize the code, return PC. */
1979
1980 static CORE_ADDR
amd64_analyze_stack_align(CORE_ADDR pc,CORE_ADDR current_pc,struct amd64_frame_cache * cache)1981 amd64_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
1982 struct amd64_frame_cache *cache)
1983 {
1984 /* There are 2 code sequences to re-align stack before the frame
1985 gets set up:
1986
1987 1. Use a caller-saved saved register:
1988
1989 leaq 8(%rsp), %reg
1990 andq $-XXX, %rsp
1991 pushq -8(%reg)
1992
1993 2. Use a callee-saved saved register:
1994
1995 pushq %reg
1996 leaq 16(%rsp), %reg
1997 andq $-XXX, %rsp
1998 pushq -8(%reg)
1999
2000 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
2001
2002 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
2003 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
2004 */
2005
2006 gdb_byte buf[18];
2007 int reg, r;
2008 int offset, offset_and;
2009
2010 if (target_read_code (pc, buf, sizeof buf))
2011 return pc;
2012
2013 /* Check caller-saved saved register. The first instruction has
2014 to be "leaq 8(%rsp), %reg". */
2015 if ((buf[0] & 0xfb) == 0x48
2016 && buf[1] == 0x8d
2017 && buf[3] == 0x24
2018 && buf[4] == 0x8)
2019 {
2020 /* MOD must be binary 10 and R/M must be binary 100. */
2021 if ((buf[2] & 0xc7) != 0x44)
2022 return pc;
2023
2024 /* REG has register number. */
2025 reg = (buf[2] >> 3) & 7;
2026
2027 /* Check the REX.R bit. */
2028 if (buf[0] == 0x4c)
2029 reg += 8;
2030
2031 offset = 5;
2032 }
2033 else
2034 {
2035 /* Check callee-saved saved register. The first instruction
2036 has to be "pushq %reg". */
2037 reg = 0;
2038 if ((buf[0] & 0xf8) == 0x50)
2039 offset = 0;
2040 else if ((buf[0] & 0xf6) == 0x40
2041 && (buf[1] & 0xf8) == 0x50)
2042 {
2043 /* Check the REX.B bit. */
2044 if ((buf[0] & 1) != 0)
2045 reg = 8;
2046
2047 offset = 1;
2048 }
2049 else
2050 return pc;
2051
2052 /* Get register. */
2053 reg += buf[offset] & 0x7;
2054
2055 offset++;
2056
2057 /* The next instruction has to be "leaq 16(%rsp), %reg". */
2058 if ((buf[offset] & 0xfb) != 0x48
2059 || buf[offset + 1] != 0x8d
2060 || buf[offset + 3] != 0x24
2061 || buf[offset + 4] != 0x10)
2062 return pc;
2063
2064 /* MOD must be binary 10 and R/M must be binary 100. */
2065 if ((buf[offset + 2] & 0xc7) != 0x44)
2066 return pc;
2067
2068 /* REG has register number. */
2069 r = (buf[offset + 2] >> 3) & 7;
2070
2071 /* Check the REX.R bit. */
2072 if (buf[offset] == 0x4c)
2073 r += 8;
2074
2075 /* Registers in pushq and leaq have to be the same. */
2076 if (reg != r)
2077 return pc;
2078
2079 offset += 5;
2080 }
2081
2082 /* Rigister can't be %rsp nor %rbp. */
2083 if (reg == 4 || reg == 5)
2084 return pc;
2085
2086 /* The next instruction has to be "andq $-XXX, %rsp". */
2087 if (buf[offset] != 0x48
2088 || buf[offset + 2] != 0xe4
2089 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2090 return pc;
2091
2092 offset_and = offset;
2093 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2094
2095 /* The next instruction has to be "pushq -8(%reg)". */
2096 r = 0;
2097 if (buf[offset] == 0xff)
2098 offset++;
2099 else if ((buf[offset] & 0xf6) == 0x40
2100 && buf[offset + 1] == 0xff)
2101 {
2102 /* Check the REX.B bit. */
2103 if ((buf[offset] & 0x1) != 0)
2104 r = 8;
2105 offset += 2;
2106 }
2107 else
2108 return pc;
2109
2110 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2111 01. */
2112 if (buf[offset + 1] != 0xf8
2113 || (buf[offset] & 0xf8) != 0x70)
2114 return pc;
2115
2116 /* R/M has register. */
2117 r += buf[offset] & 7;
2118
2119 /* Registers in leaq and pushq have to be the same. */
2120 if (reg != r)
2121 return pc;
2122
2123 if (current_pc > pc + offset_and)
2124 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2125
2126 return std::min (pc + offset + 2, current_pc);
2127 }
2128
2129 /* Similar to amd64_analyze_stack_align for x32. */
2130
2131 static CORE_ADDR
amd64_x32_analyze_stack_align(CORE_ADDR pc,CORE_ADDR current_pc,struct amd64_frame_cache * cache)2132 amd64_x32_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
2133 struct amd64_frame_cache *cache)
2134 {
2135 /* There are 2 code sequences to re-align stack before the frame
2136 gets set up:
2137
2138 1. Use a caller-saved saved register:
2139
2140 leaq 8(%rsp), %reg
2141 andq $-XXX, %rsp
2142 pushq -8(%reg)
2143
2144 or
2145
2146 [addr32] leal 8(%rsp), %reg
2147 andl $-XXX, %esp
2148 [addr32] pushq -8(%reg)
2149
2150 2. Use a callee-saved saved register:
2151
2152 pushq %reg
2153 leaq 16(%rsp), %reg
2154 andq $-XXX, %rsp
2155 pushq -8(%reg)
2156
2157 or
2158
2159 pushq %reg
2160 [addr32] leal 16(%rsp), %reg
2161 andl $-XXX, %esp
2162 [addr32] pushq -8(%reg)
2163
2164 "andq $-XXX, %rsp" can be either 4 bytes or 7 bytes:
2165
2166 0x48 0x83 0xe4 0xf0 andq $-16, %rsp
2167 0x48 0x81 0xe4 0x00 0xff 0xff 0xff andq $-256, %rsp
2168
2169 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
2170
2171 0x83 0xe4 0xf0 andl $-16, %esp
2172 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
2173 */
2174
2175 gdb_byte buf[19];
2176 int reg, r;
2177 int offset, offset_and;
2178
2179 if (target_read_memory (pc, buf, sizeof buf))
2180 return pc;
2181
2182 /* Skip optional addr32 prefix. */
2183 offset = buf[0] == 0x67 ? 1 : 0;
2184
2185 /* Check caller-saved saved register. The first instruction has
2186 to be "leaq 8(%rsp), %reg" or "leal 8(%rsp), %reg". */
2187 if (((buf[offset] & 0xfb) == 0x48 || (buf[offset] & 0xfb) == 0x40)
2188 && buf[offset + 1] == 0x8d
2189 && buf[offset + 3] == 0x24
2190 && buf[offset + 4] == 0x8)
2191 {
2192 /* MOD must be binary 10 and R/M must be binary 100. */
2193 if ((buf[offset + 2] & 0xc7) != 0x44)
2194 return pc;
2195
2196 /* REG has register number. */
2197 reg = (buf[offset + 2] >> 3) & 7;
2198
2199 /* Check the REX.R bit. */
2200 if ((buf[offset] & 0x4) != 0)
2201 reg += 8;
2202
2203 offset += 5;
2204 }
2205 else
2206 {
2207 /* Check callee-saved saved register. The first instruction
2208 has to be "pushq %reg". */
2209 reg = 0;
2210 if ((buf[offset] & 0xf6) == 0x40
2211 && (buf[offset + 1] & 0xf8) == 0x50)
2212 {
2213 /* Check the REX.B bit. */
2214 if ((buf[offset] & 1) != 0)
2215 reg = 8;
2216
2217 offset += 1;
2218 }
2219 else if ((buf[offset] & 0xf8) != 0x50)
2220 return pc;
2221
2222 /* Get register. */
2223 reg += buf[offset] & 0x7;
2224
2225 offset++;
2226
2227 /* Skip optional addr32 prefix. */
2228 if (buf[offset] == 0x67)
2229 offset++;
2230
2231 /* The next instruction has to be "leaq 16(%rsp), %reg" or
2232 "leal 16(%rsp), %reg". */
2233 if (((buf[offset] & 0xfb) != 0x48 && (buf[offset] & 0xfb) != 0x40)
2234 || buf[offset + 1] != 0x8d
2235 || buf[offset + 3] != 0x24
2236 || buf[offset + 4] != 0x10)
2237 return pc;
2238
2239 /* MOD must be binary 10 and R/M must be binary 100. */
2240 if ((buf[offset + 2] & 0xc7) != 0x44)
2241 return pc;
2242
2243 /* REG has register number. */
2244 r = (buf[offset + 2] >> 3) & 7;
2245
2246 /* Check the REX.R bit. */
2247 if ((buf[offset] & 0x4) != 0)
2248 r += 8;
2249
2250 /* Registers in pushq and leaq have to be the same. */
2251 if (reg != r)
2252 return pc;
2253
2254 offset += 5;
2255 }
2256
2257 /* Rigister can't be %rsp nor %rbp. */
2258 if (reg == 4 || reg == 5)
2259 return pc;
2260
2261 /* The next instruction may be "andq $-XXX, %rsp" or
2262 "andl $-XXX, %esp". */
2263 if (buf[offset] != 0x48)
2264 offset--;
2265
2266 if (buf[offset + 2] != 0xe4
2267 || (buf[offset + 1] != 0x81 && buf[offset + 1] != 0x83))
2268 return pc;
2269
2270 offset_and = offset;
2271 offset += buf[offset + 1] == 0x81 ? 7 : 4;
2272
2273 /* Skip optional addr32 prefix. */
2274 if (buf[offset] == 0x67)
2275 offset++;
2276
2277 /* The next instruction has to be "pushq -8(%reg)". */
2278 r = 0;
2279 if (buf[offset] == 0xff)
2280 offset++;
2281 else if ((buf[offset] & 0xf6) == 0x40
2282 && buf[offset + 1] == 0xff)
2283 {
2284 /* Check the REX.B bit. */
2285 if ((buf[offset] & 0x1) != 0)
2286 r = 8;
2287 offset += 2;
2288 }
2289 else
2290 return pc;
2291
2292 /* 8bit -8 is 0xf8. REG must be binary 110 and MOD must be binary
2293 01. */
2294 if (buf[offset + 1] != 0xf8
2295 || (buf[offset] & 0xf8) != 0x70)
2296 return pc;
2297
2298 /* R/M has register. */
2299 r += buf[offset] & 7;
2300
2301 /* Registers in leaq and pushq have to be the same. */
2302 if (reg != r)
2303 return pc;
2304
2305 if (current_pc > pc + offset_and)
2306 cache->saved_sp_reg = amd64_arch_reg_to_regnum (reg);
2307
2308 return std::min (pc + offset + 2, current_pc);
2309 }
2310
2311 /* Do a limited analysis of the prologue at PC and update CACHE
2312 accordingly. Bail out early if CURRENT_PC is reached. Return the
2313 address where the analysis stopped.
2314
2315 We will handle only functions beginning with:
2316
2317 pushq %rbp 0x55
2318 movq %rsp, %rbp 0x48 0x89 0xe5 (or 0x48 0x8b 0xec)
2319
2320 or (for the X32 ABI):
2321
2322 pushq %rbp 0x55
2323 movl %esp, %ebp 0x89 0xe5 (or 0x8b 0xec)
2324
2325 The `endbr64` instruction can be found before these sequences, and will be
2326 skipped if found.
2327
2328 Any function that doesn't start with one of these sequences will be
2329 assumed to have no prologue and thus no valid frame pointer in
2330 %rbp. */
2331
2332 static CORE_ADDR
amd64_analyze_prologue(struct gdbarch * gdbarch,CORE_ADDR pc,CORE_ADDR current_pc,struct amd64_frame_cache * cache)2333 amd64_analyze_prologue (struct gdbarch *gdbarch,
2334 CORE_ADDR pc, CORE_ADDR current_pc,
2335 struct amd64_frame_cache *cache)
2336 {
2337 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2338 /* The `endbr64` instruction. */
2339 static const gdb_byte endbr64[4] = { 0xf3, 0x0f, 0x1e, 0xfa };
2340 /* There are two variations of movq %rsp, %rbp. */
2341 static const gdb_byte mov_rsp_rbp_1[3] = { 0x48, 0x89, 0xe5 };
2342 static const gdb_byte mov_rsp_rbp_2[3] = { 0x48, 0x8b, 0xec };
2343 /* Ditto for movl %esp, %ebp. */
2344 static const gdb_byte mov_esp_ebp_1[2] = { 0x89, 0xe5 };
2345 static const gdb_byte mov_esp_ebp_2[2] = { 0x8b, 0xec };
2346
2347 gdb_byte buf[3];
2348 gdb_byte op;
2349
2350 if (current_pc <= pc)
2351 return current_pc;
2352
2353 if (gdbarch_ptr_bit (gdbarch) == 32)
2354 pc = amd64_x32_analyze_stack_align (pc, current_pc, cache);
2355 else
2356 pc = amd64_analyze_stack_align (pc, current_pc, cache);
2357
2358 op = read_code_unsigned_integer (pc, 1, byte_order);
2359
2360 /* Check for the `endbr64` instruction, skip it if found. */
2361 if (op == endbr64[0])
2362 {
2363 read_code (pc + 1, buf, 3);
2364
2365 if (memcmp (buf, &endbr64[1], 3) == 0)
2366 pc += 4;
2367
2368 op = read_code_unsigned_integer (pc, 1, byte_order);
2369 }
2370
2371 if (current_pc <= pc)
2372 return current_pc;
2373
2374 if (op == 0x55) /* pushq %rbp */
2375 {
2376 /* Take into account that we've executed the `pushq %rbp' that
2377 starts this instruction sequence. */
2378 cache->saved_regs[AMD64_RBP_REGNUM] = 0;
2379 cache->sp_offset += 8;
2380
2381 /* If that's all, return now. */
2382 if (current_pc <= pc + 1)
2383 return current_pc;
2384
2385 read_code (pc + 1, buf, 3);
2386
2387 /* Check for `movq %rsp, %rbp'. */
2388 if (memcmp (buf, mov_rsp_rbp_1, 3) == 0
2389 || memcmp (buf, mov_rsp_rbp_2, 3) == 0)
2390 {
2391 /* OK, we actually have a frame. */
2392 cache->frameless_p = 0;
2393 return pc + 4;
2394 }
2395
2396 /* For X32, also check for `movl %esp, %ebp'. */
2397 if (gdbarch_ptr_bit (gdbarch) == 32)
2398 {
2399 if (memcmp (buf, mov_esp_ebp_1, 2) == 0
2400 || memcmp (buf, mov_esp_ebp_2, 2) == 0)
2401 {
2402 /* OK, we actually have a frame. */
2403 cache->frameless_p = 0;
2404 return pc + 3;
2405 }
2406 }
2407
2408 return pc + 1;
2409 }
2410
2411 return pc;
2412 }
2413
2414 /* Work around false termination of prologue - GCC PR debug/48827.
2415
2416 START_PC is the first instruction of a function, PC is its minimal already
2417 determined advanced address. Function returns PC if it has nothing to do.
2418
2419 84 c0 test %al,%al
2420 74 23 je after
2421 <-- here is 0 lines advance - the false prologue end marker.
2422 0f 29 85 70 ff ff ff movaps %xmm0,-0x90(%rbp)
2423 0f 29 4d 80 movaps %xmm1,-0x80(%rbp)
2424 0f 29 55 90 movaps %xmm2,-0x70(%rbp)
2425 0f 29 5d a0 movaps %xmm3,-0x60(%rbp)
2426 0f 29 65 b0 movaps %xmm4,-0x50(%rbp)
2427 0f 29 6d c0 movaps %xmm5,-0x40(%rbp)
2428 0f 29 75 d0 movaps %xmm6,-0x30(%rbp)
2429 0f 29 7d e0 movaps %xmm7,-0x20(%rbp)
2430 after: */
2431
2432 static CORE_ADDR
amd64_skip_xmm_prologue(CORE_ADDR pc,CORE_ADDR start_pc)2433 amd64_skip_xmm_prologue (CORE_ADDR pc, CORE_ADDR start_pc)
2434 {
2435 struct symtab_and_line start_pc_sal, next_sal;
2436 gdb_byte buf[4 + 8 * 7];
2437 int offset, xmmreg;
2438
2439 if (pc == start_pc)
2440 return pc;
2441
2442 start_pc_sal = find_pc_sect_line (start_pc, NULL, 0);
2443 if (start_pc_sal.symtab == NULL
2444 || producer_is_gcc_ge_4 (start_pc_sal.symtab->compunit ()
2445 ->producer ()) < 6
2446 || start_pc_sal.pc != start_pc || pc >= start_pc_sal.end)
2447 return pc;
2448
2449 next_sal = find_pc_sect_line (start_pc_sal.end, NULL, 0);
2450 if (next_sal.line != start_pc_sal.line)
2451 return pc;
2452
2453 /* START_PC can be from overlayed memory, ignored here. */
2454 if (target_read_code (next_sal.pc - 4, buf, sizeof (buf)) != 0)
2455 return pc;
2456
2457 /* test %al,%al */
2458 if (buf[0] != 0x84 || buf[1] != 0xc0)
2459 return pc;
2460 /* je AFTER */
2461 if (buf[2] != 0x74)
2462 return pc;
2463
2464 offset = 4;
2465 for (xmmreg = 0; xmmreg < 8; xmmreg++)
2466 {
2467 /* 0x0f 0x29 0b??000101 movaps %xmmreg?,-0x??(%rbp) */
2468 if (buf[offset] != 0x0f || buf[offset + 1] != 0x29
2469 || (buf[offset + 2] & 0x3f) != (xmmreg << 3 | 0x5))
2470 return pc;
2471
2472 /* 0b01?????? */
2473 if ((buf[offset + 2] & 0xc0) == 0x40)
2474 {
2475 /* 8-bit displacement. */
2476 offset += 4;
2477 }
2478 /* 0b10?????? */
2479 else if ((buf[offset + 2] & 0xc0) == 0x80)
2480 {
2481 /* 32-bit displacement. */
2482 offset += 7;
2483 }
2484 else
2485 return pc;
2486 }
2487
2488 /* je AFTER */
2489 if (offset - 4 != buf[3])
2490 return pc;
2491
2492 return next_sal.end;
2493 }
2494
2495 /* Return PC of first real instruction. */
2496
2497 static CORE_ADDR
amd64_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR start_pc)2498 amd64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
2499 {
2500 struct amd64_frame_cache cache;
2501 CORE_ADDR pc;
2502 CORE_ADDR func_addr;
2503
2504 if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
2505 {
2506 CORE_ADDR post_prologue_pc
2507 = skip_prologue_using_sal (gdbarch, func_addr);
2508 struct compunit_symtab *cust = find_pc_compunit_symtab (func_addr);
2509
2510 /* LLVM backend (Clang/Flang) always emits a line note before the
2511 prologue and another one after. We trust clang and newer Intel
2512 compilers to emit usable line notes. */
2513 if (post_prologue_pc
2514 && (cust != NULL
2515 && cust->producer () != nullptr
2516 && (producer_is_llvm (cust->producer ())
2517 || producer_is_icc_ge_19 (cust->producer ()))))
2518 return std::max (start_pc, post_prologue_pc);
2519 }
2520
2521 amd64_init_frame_cache (&cache);
2522 pc = amd64_analyze_prologue (gdbarch, start_pc, 0xffffffffffffffffLL,
2523 &cache);
2524 if (cache.frameless_p)
2525 return start_pc;
2526
2527 return amd64_skip_xmm_prologue (pc, start_pc);
2528 }
2529
2530
2531 /* Normal frames. */
2532
2533 static void
amd64_frame_cache_1(const frame_info_ptr & this_frame,struct amd64_frame_cache * cache)2534 amd64_frame_cache_1 (const frame_info_ptr &this_frame,
2535 struct amd64_frame_cache *cache)
2536 {
2537 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2538 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2539 gdb_byte buf[8];
2540 int i;
2541
2542 cache->pc = get_frame_func (this_frame);
2543 if (cache->pc != 0)
2544 amd64_analyze_prologue (gdbarch, cache->pc, get_frame_pc (this_frame),
2545 cache);
2546
2547 if (cache->frameless_p)
2548 {
2549 /* We didn't find a valid frame. If we're at the start of a
2550 function, or somewhere half-way its prologue, the function's
2551 frame probably hasn't been fully setup yet. Try to
2552 reconstruct the base address for the stack frame by looking
2553 at the stack pointer. For truly "frameless" functions this
2554 might work too. */
2555
2556 if (cache->saved_sp_reg != -1)
2557 {
2558 /* Stack pointer has been saved. */
2559 get_frame_register (this_frame, cache->saved_sp_reg, buf);
2560 cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
2561
2562 /* We're halfway aligning the stack. */
2563 cache->base = ((cache->saved_sp - 8) & 0xfffffffffffffff0LL) - 8;
2564 cache->saved_regs[AMD64_RIP_REGNUM] = cache->saved_sp - 8;
2565
2566 /* This will be added back below. */
2567 cache->saved_regs[AMD64_RIP_REGNUM] -= cache->base;
2568 }
2569 else
2570 {
2571 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2572 cache->base = extract_unsigned_integer (buf, 8, byte_order)
2573 + cache->sp_offset;
2574 }
2575 }
2576 else
2577 {
2578 get_frame_register (this_frame, AMD64_RBP_REGNUM, buf);
2579 cache->base = extract_unsigned_integer (buf, 8, byte_order);
2580 }
2581
2582 /* Now that we have the base address for the stack frame we can
2583 calculate the value of %rsp in the calling frame. */
2584 cache->saved_sp = cache->base + 16;
2585
2586 /* For normal frames, %rip is stored at 8(%rbp). If we don't have a
2587 frame we find it at the same offset from the reconstructed base
2588 address. If we're halfway aligning the stack, %rip is handled
2589 differently (see above). */
2590 if (!cache->frameless_p || cache->saved_sp_reg == -1)
2591 cache->saved_regs[AMD64_RIP_REGNUM] = 8;
2592
2593 /* Adjust all the saved registers such that they contain addresses
2594 instead of offsets. */
2595 for (i = 0; i < AMD64_NUM_SAVED_REGS; i++)
2596 if (cache->saved_regs[i] != -1)
2597 cache->saved_regs[i] += cache->base;
2598
2599 cache->base_p = 1;
2600 }
2601
2602 static struct amd64_frame_cache *
amd64_frame_cache(const frame_info_ptr & this_frame,void ** this_cache)2603 amd64_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2604 {
2605 struct amd64_frame_cache *cache;
2606
2607 if (*this_cache)
2608 return (struct amd64_frame_cache *) *this_cache;
2609
2610 cache = amd64_alloc_frame_cache ();
2611 *this_cache = cache;
2612
2613 try
2614 {
2615 amd64_frame_cache_1 (this_frame, cache);
2616 }
2617 catch (const gdb_exception_error &ex)
2618 {
2619 if (ex.error != NOT_AVAILABLE_ERROR)
2620 throw;
2621 }
2622
2623 return cache;
2624 }
2625
2626 static enum unwind_stop_reason
amd64_frame_unwind_stop_reason(const frame_info_ptr & this_frame,void ** this_cache)2627 amd64_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2628 void **this_cache)
2629 {
2630 struct amd64_frame_cache *cache =
2631 amd64_frame_cache (this_frame, this_cache);
2632
2633 if (!cache->base_p)
2634 return UNWIND_UNAVAILABLE;
2635
2636 /* This marks the outermost frame. */
2637 if (cache->base == 0)
2638 return UNWIND_OUTERMOST;
2639
2640 return UNWIND_NO_REASON;
2641 }
2642
2643 static void
amd64_frame_this_id(const frame_info_ptr & this_frame,void ** this_cache,struct frame_id * this_id)2644 amd64_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
2645 struct frame_id *this_id)
2646 {
2647 struct amd64_frame_cache *cache =
2648 amd64_frame_cache (this_frame, this_cache);
2649
2650 if (!cache->base_p)
2651 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
2652 else if (cache->base == 0)
2653 {
2654 /* This marks the outermost frame. */
2655 return;
2656 }
2657 else
2658 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
2659 }
2660
2661 static struct value *
amd64_frame_prev_register(const frame_info_ptr & this_frame,void ** this_cache,int regnum)2662 amd64_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
2663 int regnum)
2664 {
2665 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2666 struct amd64_frame_cache *cache =
2667 amd64_frame_cache (this_frame, this_cache);
2668
2669 gdb_assert (regnum >= 0);
2670
2671 if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2672 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2673
2674 if (regnum < AMD64_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
2675 return frame_unwind_got_memory (this_frame, regnum,
2676 cache->saved_regs[regnum]);
2677
2678 return frame_unwind_got_register (this_frame, regnum, regnum);
2679 }
2680
2681 static const struct frame_unwind amd64_frame_unwind =
2682 {
2683 "amd64 prologue",
2684 NORMAL_FRAME,
2685 amd64_frame_unwind_stop_reason,
2686 amd64_frame_this_id,
2687 amd64_frame_prev_register,
2688 NULL,
2689 default_frame_sniffer
2690 };
2691
2692 /* Generate a bytecode expression to get the value of the saved PC. */
2693
2694 static void
amd64_gen_return_address(struct gdbarch * gdbarch,struct agent_expr * ax,struct axs_value * value,CORE_ADDR scope)2695 amd64_gen_return_address (struct gdbarch *gdbarch,
2696 struct agent_expr *ax, struct axs_value *value,
2697 CORE_ADDR scope)
2698 {
2699 /* The following sequence assumes the traditional use of the base
2700 register. */
2701 ax_reg (ax, AMD64_RBP_REGNUM);
2702 ax_const_l (ax, 8);
2703 ax_simple (ax, aop_add);
2704 value->type = register_type (gdbarch, AMD64_RIP_REGNUM);
2705 value->kind = axs_lvalue_memory;
2706 }
2707
2708
2709 /* Signal trampolines. */
2710
2711 /* FIXME: kettenis/20030419: Perhaps, we can unify the 32-bit and
2712 64-bit variants. This would require using identical frame caches
2713 on both platforms. */
2714
2715 static struct amd64_frame_cache *
amd64_sigtramp_frame_cache(const frame_info_ptr & this_frame,void ** this_cache)2716 amd64_sigtramp_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2717 {
2718 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2719 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
2720 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2721 struct amd64_frame_cache *cache;
2722 CORE_ADDR addr;
2723 gdb_byte buf[8];
2724 int i;
2725
2726 if (*this_cache)
2727 return (struct amd64_frame_cache *) *this_cache;
2728
2729 cache = amd64_alloc_frame_cache ();
2730
2731 try
2732 {
2733 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2734 cache->base = extract_unsigned_integer (buf, 8, byte_order) - 8;
2735
2736 addr = tdep->sigcontext_addr (this_frame);
2737 gdb_assert (tdep->sc_reg_offset);
2738 gdb_assert (tdep->sc_num_regs <= AMD64_NUM_SAVED_REGS);
2739 for (i = 0; i < tdep->sc_num_regs; i++)
2740 if (tdep->sc_reg_offset[i] != -1)
2741 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
2742
2743 cache->base_p = 1;
2744 }
2745 catch (const gdb_exception_error &ex)
2746 {
2747 if (ex.error != NOT_AVAILABLE_ERROR)
2748 throw;
2749 }
2750
2751 *this_cache = cache;
2752 return cache;
2753 }
2754
2755 static enum unwind_stop_reason
amd64_sigtramp_frame_unwind_stop_reason(const frame_info_ptr & this_frame,void ** this_cache)2756 amd64_sigtramp_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2757 void **this_cache)
2758 {
2759 struct amd64_frame_cache *cache =
2760 amd64_sigtramp_frame_cache (this_frame, this_cache);
2761
2762 if (!cache->base_p)
2763 return UNWIND_UNAVAILABLE;
2764
2765 return UNWIND_NO_REASON;
2766 }
2767
2768 static void
amd64_sigtramp_frame_this_id(const frame_info_ptr & this_frame,void ** this_cache,struct frame_id * this_id)2769 amd64_sigtramp_frame_this_id (const frame_info_ptr &this_frame,
2770 void **this_cache, struct frame_id *this_id)
2771 {
2772 struct amd64_frame_cache *cache =
2773 amd64_sigtramp_frame_cache (this_frame, this_cache);
2774
2775 if (!cache->base_p)
2776 (*this_id) = frame_id_build_unavailable_stack (get_frame_pc (this_frame));
2777 else if (cache->base == 0)
2778 {
2779 /* This marks the outermost frame. */
2780 return;
2781 }
2782 else
2783 (*this_id) = frame_id_build (cache->base + 16, get_frame_pc (this_frame));
2784 }
2785
2786 static struct value *
amd64_sigtramp_frame_prev_register(const frame_info_ptr & this_frame,void ** this_cache,int regnum)2787 amd64_sigtramp_frame_prev_register (const frame_info_ptr &this_frame,
2788 void **this_cache, int regnum)
2789 {
2790 /* Make sure we've initialized the cache. */
2791 amd64_sigtramp_frame_cache (this_frame, this_cache);
2792
2793 return amd64_frame_prev_register (this_frame, this_cache, regnum);
2794 }
2795
2796 static int
amd64_sigtramp_frame_sniffer(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_cache)2797 amd64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2798 const frame_info_ptr &this_frame,
2799 void **this_cache)
2800 {
2801 gdbarch *arch = get_frame_arch (this_frame);
2802 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
2803
2804 /* We shouldn't even bother if we don't have a sigcontext_addr
2805 handler. */
2806 if (tdep->sigcontext_addr == NULL)
2807 return 0;
2808
2809 if (tdep->sigtramp_p != NULL)
2810 {
2811 if (tdep->sigtramp_p (this_frame))
2812 return 1;
2813 }
2814
2815 if (tdep->sigtramp_start != 0)
2816 {
2817 CORE_ADDR pc = get_frame_pc (this_frame);
2818
2819 gdb_assert (tdep->sigtramp_end != 0);
2820 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
2821 return 1;
2822 }
2823
2824 return 0;
2825 }
2826
2827 static const struct frame_unwind amd64_sigtramp_frame_unwind =
2828 {
2829 "amd64 sigtramp",
2830 SIGTRAMP_FRAME,
2831 amd64_sigtramp_frame_unwind_stop_reason,
2832 amd64_sigtramp_frame_this_id,
2833 amd64_sigtramp_frame_prev_register,
2834 NULL,
2835 amd64_sigtramp_frame_sniffer
2836 };
2837
2838
2839 static CORE_ADDR
amd64_frame_base_address(const frame_info_ptr & this_frame,void ** this_cache)2840 amd64_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
2841 {
2842 struct amd64_frame_cache *cache =
2843 amd64_frame_cache (this_frame, this_cache);
2844
2845 return cache->base;
2846 }
2847
2848 static const struct frame_base amd64_frame_base =
2849 {
2850 &amd64_frame_unwind,
2851 amd64_frame_base_address,
2852 amd64_frame_base_address,
2853 amd64_frame_base_address
2854 };
2855
2856 /* Implement core of the stack_frame_destroyed_p gdbarch method. */
2857
2858 static int
amd64_stack_frame_destroyed_p_1(struct gdbarch * gdbarch,CORE_ADDR pc)2859 amd64_stack_frame_destroyed_p_1 (struct gdbarch *gdbarch, CORE_ADDR pc)
2860 {
2861 gdb_byte insn;
2862
2863 std::optional<CORE_ADDR> epilogue = find_epilogue_using_linetable (pc);
2864
2865 /* PC is pointing at the next instruction to be executed. If it is
2866 equal to the epilogue start, it means we're right before it starts,
2867 so the stack is still valid. */
2868 if (epilogue)
2869 return pc > epilogue;
2870
2871 if (target_read_memory (pc, &insn, 1))
2872 return 0; /* Can't read memory at pc. */
2873
2874 if (insn != 0xc3) /* 'ret' instruction. */
2875 return 0;
2876
2877 return 1;
2878 }
2879
2880 /* Normal frames, but in a function epilogue. */
2881
2882 /* Implement the stack_frame_destroyed_p gdbarch method.
2883
2884 The epilogue is defined here as the 'ret' instruction, which will
2885 follow any instruction such as 'leave' or 'pop %ebp' that destroys
2886 the function's stack frame. */
2887
2888 static int
amd64_stack_frame_destroyed_p(struct gdbarch * gdbarch,CORE_ADDR pc)2889 amd64_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2890 {
2891 struct compunit_symtab *cust = find_pc_compunit_symtab (pc);
2892
2893 if (cust != nullptr && cust->producer () != nullptr
2894 && producer_is_llvm (cust->producer ()))
2895 return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
2896
2897 return 0;
2898 }
2899
2900 static int
amd64_epilogue_frame_sniffer_1(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_prologue_cache,bool override_p)2901 amd64_epilogue_frame_sniffer_1 (const struct frame_unwind *self,
2902 const frame_info_ptr &this_frame,
2903 void **this_prologue_cache, bool override_p)
2904 {
2905 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2906 CORE_ADDR pc = get_frame_pc (this_frame);
2907
2908 if (frame_relative_level (this_frame) != 0)
2909 /* We're not in the inner frame, so assume we're not in an epilogue. */
2910 return 0;
2911
2912 bool unwind_valid_p
2913 = compunit_epilogue_unwind_valid (find_pc_compunit_symtab (pc));
2914 if (override_p)
2915 {
2916 if (unwind_valid_p)
2917 /* Don't override the symtab unwinders, skip
2918 "amd64 epilogue override". */
2919 return 0;
2920 }
2921 else
2922 {
2923 if (!unwind_valid_p)
2924 /* "amd64 epilogue override" unwinder already ran, skip
2925 "amd64 epilogue". */
2926 return 0;
2927 }
2928
2929 /* Check whether we're in an epilogue. */
2930 return amd64_stack_frame_destroyed_p_1 (gdbarch, pc);
2931 }
2932
2933 static int
amd64_epilogue_override_frame_sniffer(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_prologue_cache)2934 amd64_epilogue_override_frame_sniffer (const struct frame_unwind *self,
2935 const frame_info_ptr &this_frame,
2936 void **this_prologue_cache)
2937 {
2938 return amd64_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2939 true);
2940 }
2941
2942 static int
amd64_epilogue_frame_sniffer(const struct frame_unwind * self,const frame_info_ptr & this_frame,void ** this_prologue_cache)2943 amd64_epilogue_frame_sniffer (const struct frame_unwind *self,
2944 const frame_info_ptr &this_frame,
2945 void **this_prologue_cache)
2946 {
2947 return amd64_epilogue_frame_sniffer_1 (self, this_frame, this_prologue_cache,
2948 false);
2949 }
2950
2951 static struct amd64_frame_cache *
amd64_epilogue_frame_cache(const frame_info_ptr & this_frame,void ** this_cache)2952 amd64_epilogue_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
2953 {
2954 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2955 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2956 struct amd64_frame_cache *cache;
2957 gdb_byte buf[8];
2958
2959 if (*this_cache)
2960 return (struct amd64_frame_cache *) *this_cache;
2961
2962 cache = amd64_alloc_frame_cache ();
2963 *this_cache = cache;
2964
2965 try
2966 {
2967 /* Cache base will be %rsp plus cache->sp_offset (-8). */
2968 get_frame_register (this_frame, AMD64_RSP_REGNUM, buf);
2969 cache->base = extract_unsigned_integer (buf, 8,
2970 byte_order) + cache->sp_offset;
2971
2972 /* Cache pc will be the frame func. */
2973 cache->pc = get_frame_func (this_frame);
2974
2975 /* The previous value of %rsp is cache->base plus 16. */
2976 cache->saved_sp = cache->base + 16;
2977
2978 /* The saved %rip will be at cache->base plus 8. */
2979 cache->saved_regs[AMD64_RIP_REGNUM] = cache->base + 8;
2980
2981 cache->base_p = 1;
2982 }
2983 catch (const gdb_exception_error &ex)
2984 {
2985 if (ex.error != NOT_AVAILABLE_ERROR)
2986 throw;
2987 }
2988
2989 return cache;
2990 }
2991
2992 static enum unwind_stop_reason
amd64_epilogue_frame_unwind_stop_reason(const frame_info_ptr & this_frame,void ** this_cache)2993 amd64_epilogue_frame_unwind_stop_reason (const frame_info_ptr &this_frame,
2994 void **this_cache)
2995 {
2996 struct amd64_frame_cache *cache
2997 = amd64_epilogue_frame_cache (this_frame, this_cache);
2998
2999 if (!cache->base_p)
3000 return UNWIND_UNAVAILABLE;
3001
3002 return UNWIND_NO_REASON;
3003 }
3004
3005 static void
amd64_epilogue_frame_this_id(const frame_info_ptr & this_frame,void ** this_cache,struct frame_id * this_id)3006 amd64_epilogue_frame_this_id (const frame_info_ptr &this_frame,
3007 void **this_cache,
3008 struct frame_id *this_id)
3009 {
3010 struct amd64_frame_cache *cache = amd64_epilogue_frame_cache (this_frame,
3011 this_cache);
3012
3013 if (!cache->base_p)
3014 (*this_id) = frame_id_build_unavailable_stack (cache->pc);
3015 else
3016 (*this_id) = frame_id_build (cache->base + 16, cache->pc);
3017 }
3018
3019 static const struct frame_unwind amd64_epilogue_override_frame_unwind =
3020 {
3021 "amd64 epilogue override",
3022 NORMAL_FRAME,
3023 amd64_epilogue_frame_unwind_stop_reason,
3024 amd64_epilogue_frame_this_id,
3025 amd64_frame_prev_register,
3026 NULL,
3027 amd64_epilogue_override_frame_sniffer
3028 };
3029
3030 static const struct frame_unwind amd64_epilogue_frame_unwind =
3031 {
3032 "amd64 epilogue",
3033 NORMAL_FRAME,
3034 amd64_epilogue_frame_unwind_stop_reason,
3035 amd64_epilogue_frame_this_id,
3036 amd64_frame_prev_register,
3037 NULL,
3038 amd64_epilogue_frame_sniffer
3039 };
3040
3041 static struct frame_id
amd64_dummy_id(struct gdbarch * gdbarch,const frame_info_ptr & this_frame)3042 amd64_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
3043 {
3044 CORE_ADDR fp;
3045
3046 fp = get_frame_register_unsigned (this_frame, AMD64_RBP_REGNUM);
3047
3048 return frame_id_build (fp + 16, get_frame_pc (this_frame));
3049 }
3050
3051 /* 16 byte align the SP per frame requirements. */
3052
3053 static CORE_ADDR
amd64_frame_align(struct gdbarch * gdbarch,CORE_ADDR sp)3054 amd64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3055 {
3056 return sp & -(CORE_ADDR)16;
3057 }
3058
3059
3060 /* Supply register REGNUM from the buffer specified by FPREGS and LEN
3061 in the floating-point register set REGSET to register cache
3062 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
3063
3064 static void
amd64_supply_fpregset(const struct regset * regset,struct regcache * regcache,int regnum,const void * fpregs,size_t len)3065 amd64_supply_fpregset (const struct regset *regset, struct regcache *regcache,
3066 int regnum, const void *fpregs, size_t len)
3067 {
3068 struct gdbarch *gdbarch = regcache->arch ();
3069 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3070
3071 gdb_assert (len >= tdep->sizeof_fpregset);
3072 amd64_supply_fxsave (regcache, regnum, fpregs);
3073 }
3074
3075 /* Collect register REGNUM from the register cache REGCACHE and store
3076 it in the buffer specified by FPREGS and LEN as described by the
3077 floating-point register set REGSET. If REGNUM is -1, do this for
3078 all registers in REGSET. */
3079
3080 static void
amd64_collect_fpregset(const struct regset * regset,const struct regcache * regcache,int regnum,void * fpregs,size_t len)3081 amd64_collect_fpregset (const struct regset *regset,
3082 const struct regcache *regcache,
3083 int regnum, void *fpregs, size_t len)
3084 {
3085 struct gdbarch *gdbarch = regcache->arch ();
3086 const i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3087
3088 gdb_assert (len >= tdep->sizeof_fpregset);
3089 amd64_collect_fxsave (regcache, regnum, fpregs);
3090 }
3091
3092 const struct regset amd64_fpregset =
3093 {
3094 NULL, amd64_supply_fpregset, amd64_collect_fpregset
3095 };
3096
3097
3098 /* Figure out where the longjmp will land. Slurp the jmp_buf out of
3099 %rdi. We expect its value to be a pointer to the jmp_buf structure
3100 from which we extract the address that we will land at. This
3101 address is copied into PC. This routine returns non-zero on
3102 success. */
3103
3104 static int
amd64_get_longjmp_target(const frame_info_ptr & frame,CORE_ADDR * pc)3105 amd64_get_longjmp_target (const frame_info_ptr &frame, CORE_ADDR *pc)
3106 {
3107 gdb_byte buf[8];
3108 CORE_ADDR jb_addr;
3109 struct gdbarch *gdbarch = get_frame_arch (frame);
3110 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3111 int jb_pc_offset = tdep->jb_pc_offset;
3112 int len = builtin_type (gdbarch)->builtin_func_ptr->length ();
3113
3114 /* If JB_PC_OFFSET is -1, we have no way to find out where the
3115 longjmp will land. */
3116 if (jb_pc_offset == -1)
3117 return 0;
3118
3119 get_frame_register (frame, AMD64_RDI_REGNUM, buf);
3120 jb_addr= extract_typed_address
3121 (buf, builtin_type (gdbarch)->builtin_data_ptr);
3122 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
3123 return 0;
3124
3125 *pc = extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
3126
3127 return 1;
3128 }
3129
3130 static const int amd64_record_regmap[] =
3131 {
3132 AMD64_RAX_REGNUM, AMD64_RCX_REGNUM, AMD64_RDX_REGNUM, AMD64_RBX_REGNUM,
3133 AMD64_RSP_REGNUM, AMD64_RBP_REGNUM, AMD64_RSI_REGNUM, AMD64_RDI_REGNUM,
3134 AMD64_R8_REGNUM, AMD64_R9_REGNUM, AMD64_R10_REGNUM, AMD64_R11_REGNUM,
3135 AMD64_R12_REGNUM, AMD64_R13_REGNUM, AMD64_R14_REGNUM, AMD64_R15_REGNUM,
3136 AMD64_RIP_REGNUM, AMD64_EFLAGS_REGNUM, AMD64_CS_REGNUM, AMD64_SS_REGNUM,
3137 AMD64_DS_REGNUM, AMD64_ES_REGNUM, AMD64_FS_REGNUM, AMD64_GS_REGNUM
3138 };
3139
3140 /* Implement the "in_indirect_branch_thunk" gdbarch function. */
3141
3142 static bool
amd64_in_indirect_branch_thunk(struct gdbarch * gdbarch,CORE_ADDR pc)3143 amd64_in_indirect_branch_thunk (struct gdbarch *gdbarch, CORE_ADDR pc)
3144 {
3145 return x86_in_indirect_branch_thunk (pc, amd64_register_names,
3146 AMD64_RAX_REGNUM,
3147 AMD64_RIP_REGNUM);
3148 }
3149
3150 void
amd64_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch,const target_desc * default_tdesc)3151 amd64_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3152 const target_desc *default_tdesc)
3153 {
3154 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3155 const struct target_desc *tdesc = info.target_desc;
3156 static const char *const stap_integer_prefixes[] = { "$", NULL };
3157 static const char *const stap_register_prefixes[] = { "%", NULL };
3158 static const char *const stap_register_indirection_prefixes[] = { "(",
3159 NULL };
3160 static const char *const stap_register_indirection_suffixes[] = { ")",
3161 NULL };
3162
3163 /* AMD64 generally uses `fxsave' instead of `fsave' for saving its
3164 floating-point registers. */
3165 tdep->sizeof_fpregset = I387_SIZEOF_FXSAVE;
3166 tdep->fpregset = &amd64_fpregset;
3167
3168 if (! tdesc_has_registers (tdesc))
3169 tdesc = default_tdesc;
3170 tdep->tdesc = tdesc;
3171
3172 tdep->num_core_regs = AMD64_NUM_GREGS + I387_NUM_REGS;
3173 tdep->register_names = amd64_register_names;
3174
3175 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx512") != NULL)
3176 {
3177 tdep->zmmh_register_names = amd64_zmmh_names;
3178 tdep->k_register_names = amd64_k_names;
3179 tdep->xmm_avx512_register_names = amd64_xmm_avx512_names;
3180 tdep->ymm16h_register_names = amd64_ymmh_avx512_names;
3181
3182 tdep->num_zmm_regs = 32;
3183 tdep->num_xmm_avx512_regs = 16;
3184 tdep->num_ymm_avx512_regs = 16;
3185
3186 tdep->zmm0h_regnum = AMD64_ZMM0H_REGNUM;
3187 tdep->k0_regnum = AMD64_K0_REGNUM;
3188 tdep->xmm16_regnum = AMD64_XMM16_REGNUM;
3189 tdep->ymm16h_regnum = AMD64_YMM16H_REGNUM;
3190 }
3191
3192 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.avx") != NULL)
3193 {
3194 tdep->ymmh_register_names = amd64_ymmh_names;
3195 tdep->num_ymm_regs = 16;
3196 tdep->ymm0h_regnum = AMD64_YMM0H_REGNUM;
3197 }
3198
3199 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.mpx") != NULL)
3200 {
3201 tdep->mpx_register_names = amd64_mpx_names;
3202 tdep->bndcfgu_regnum = AMD64_BNDCFGU_REGNUM;
3203 tdep->bnd0r_regnum = AMD64_BND0R_REGNUM;
3204 }
3205
3206 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.segments") != NULL)
3207 {
3208 tdep->fsbase_regnum = AMD64_FSBASE_REGNUM;
3209 }
3210
3211 if (tdesc_find_feature (tdesc, "org.gnu.gdb.i386.pkeys") != NULL)
3212 {
3213 tdep->pkeys_register_names = amd64_pkeys_names;
3214 tdep->pkru_regnum = AMD64_PKRU_REGNUM;
3215 tdep->num_pkeys_regs = 1;
3216 }
3217
3218 tdep->num_byte_regs = 20;
3219 tdep->num_word_regs = 16;
3220 tdep->num_dword_regs = 16;
3221 /* Avoid wiring in the MMX registers for now. */
3222 tdep->num_mmx_regs = 0;
3223
3224 set_gdbarch_pseudo_register_read_value (gdbarch,
3225 amd64_pseudo_register_read_value);
3226 set_gdbarch_pseudo_register_write (gdbarch, amd64_pseudo_register_write);
3227 set_gdbarch_ax_pseudo_register_collect (gdbarch,
3228 amd64_ax_pseudo_register_collect);
3229
3230 set_tdesc_pseudo_register_name (gdbarch, amd64_pseudo_register_name);
3231
3232 /* AMD64 has an FPU and 16 SSE registers. */
3233 tdep->st0_regnum = AMD64_ST0_REGNUM;
3234 tdep->num_xmm_regs = 16;
3235
3236 /* This is what all the fuss is about. */
3237 set_gdbarch_long_bit (gdbarch, 64);
3238 set_gdbarch_long_long_bit (gdbarch, 64);
3239 set_gdbarch_ptr_bit (gdbarch, 64);
3240
3241 /* In contrast to the i386, on AMD64 a `long double' actually takes
3242 up 128 bits, even though it's still based on the i387 extended
3243 floating-point format which has only 80 significant bits. */
3244 set_gdbarch_long_double_bit (gdbarch, 128);
3245
3246 set_gdbarch_num_regs (gdbarch, AMD64_NUM_REGS);
3247
3248 /* Register numbers of various important registers. */
3249 set_gdbarch_sp_regnum (gdbarch, AMD64_RSP_REGNUM); /* %rsp */
3250 set_gdbarch_pc_regnum (gdbarch, AMD64_RIP_REGNUM); /* %rip */
3251 set_gdbarch_ps_regnum (gdbarch, AMD64_EFLAGS_REGNUM); /* %eflags */
3252 set_gdbarch_fp0_regnum (gdbarch, AMD64_ST0_REGNUM); /* %st(0) */
3253
3254 /* The "default" register numbering scheme for AMD64 is referred to
3255 as the "DWARF Register Number Mapping" in the System V psABI.
3256 The preferred debugging format for all known AMD64 targets is
3257 actually DWARF2, and GCC doesn't seem to support DWARF (that is
3258 DWARF-1), but we provide the same mapping just in case. This
3259 mapping is also used for stabs, which GCC does support. */
3260 set_gdbarch_stab_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3261 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, amd64_dwarf_reg_to_regnum);
3262
3263 /* We don't override SDB_REG_RO_REGNUM, since COFF doesn't seem to
3264 be in use on any of the supported AMD64 targets. */
3265
3266 /* Call dummy code. */
3267 set_gdbarch_push_dummy_call (gdbarch, amd64_push_dummy_call);
3268 set_gdbarch_frame_align (gdbarch, amd64_frame_align);
3269 set_gdbarch_frame_red_zone_size (gdbarch, 128);
3270
3271 set_gdbarch_convert_register_p (gdbarch, i387_convert_register_p);
3272 set_gdbarch_register_to_value (gdbarch, i387_register_to_value);
3273 set_gdbarch_value_to_register (gdbarch, i387_value_to_register);
3274
3275 set_gdbarch_return_value_as_value (gdbarch, amd64_return_value);
3276
3277 set_gdbarch_skip_prologue (gdbarch, amd64_skip_prologue);
3278
3279 tdep->record_regmap = amd64_record_regmap;
3280
3281 set_gdbarch_dummy_id (gdbarch, amd64_dummy_id);
3282
3283 /* Hook the function epilogue frame unwinder. This unwinder is
3284 appended to the list first, so that it supersedes the other
3285 unwinders in function epilogues. */
3286 frame_unwind_prepend_unwinder (gdbarch, &amd64_epilogue_override_frame_unwind);
3287
3288 frame_unwind_append_unwinder (gdbarch, &amd64_epilogue_frame_unwind);
3289
3290 /* Hook the prologue-based frame unwinders. */
3291 frame_unwind_append_unwinder (gdbarch, &amd64_sigtramp_frame_unwind);
3292 frame_unwind_append_unwinder (gdbarch, &amd64_frame_unwind);
3293 frame_base_set_default (gdbarch, &amd64_frame_base);
3294
3295 set_gdbarch_get_longjmp_target (gdbarch, amd64_get_longjmp_target);
3296
3297 set_gdbarch_relocate_instruction (gdbarch, amd64_relocate_instruction);
3298
3299 set_gdbarch_gen_return_address (gdbarch, amd64_gen_return_address);
3300
3301 set_gdbarch_stack_frame_destroyed_p (gdbarch, amd64_stack_frame_destroyed_p);
3302
3303 /* SystemTap variables and functions. */
3304 set_gdbarch_stap_integer_prefixes (gdbarch, stap_integer_prefixes);
3305 set_gdbarch_stap_register_prefixes (gdbarch, stap_register_prefixes);
3306 set_gdbarch_stap_register_indirection_prefixes (gdbarch,
3307 stap_register_indirection_prefixes);
3308 set_gdbarch_stap_register_indirection_suffixes (gdbarch,
3309 stap_register_indirection_suffixes);
3310 set_gdbarch_stap_is_single_operand (gdbarch,
3311 i386_stap_is_single_operand);
3312 set_gdbarch_stap_parse_special_token (gdbarch,
3313 i386_stap_parse_special_token);
3314 set_gdbarch_insn_is_call (gdbarch, amd64_insn_is_call);
3315 set_gdbarch_insn_is_ret (gdbarch, amd64_insn_is_ret);
3316 set_gdbarch_insn_is_jump (gdbarch, amd64_insn_is_jump);
3317
3318 set_gdbarch_in_indirect_branch_thunk (gdbarch,
3319 amd64_in_indirect_branch_thunk);
3320
3321 register_amd64_ravenscar_ops (gdbarch);
3322 }
3323
3324 /* Initialize ARCH for x86-64, no osabi. */
3325
3326 static void
amd64_none_init_abi(gdbarch_info info,gdbarch * arch)3327 amd64_none_init_abi (gdbarch_info info, gdbarch *arch)
3328 {
3329 amd64_init_abi (info, arch, amd64_target_description (X86_XSTATE_SSE_MASK,
3330 true));
3331 }
3332
3333 static struct type *
amd64_x32_pseudo_register_type(struct gdbarch * gdbarch,int regnum)3334 amd64_x32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
3335 {
3336 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3337
3338 switch (regnum - tdep->eax_regnum)
3339 {
3340 case AMD64_RBP_REGNUM: /* %ebp */
3341 case AMD64_RSP_REGNUM: /* %esp */
3342 return builtin_type (gdbarch)->builtin_data_ptr;
3343 case AMD64_RIP_REGNUM: /* %eip */
3344 return builtin_type (gdbarch)->builtin_func_ptr;
3345 }
3346
3347 return i386_pseudo_register_type (gdbarch, regnum);
3348 }
3349
3350 void
amd64_x32_init_abi(struct gdbarch_info info,struct gdbarch * gdbarch,const target_desc * default_tdesc)3351 amd64_x32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch,
3352 const target_desc *default_tdesc)
3353 {
3354 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3355
3356 amd64_init_abi (info, gdbarch, default_tdesc);
3357
3358 tdep->num_dword_regs = 17;
3359 set_tdesc_pseudo_register_type (gdbarch, amd64_x32_pseudo_register_type);
3360
3361 set_gdbarch_long_bit (gdbarch, 32);
3362 set_gdbarch_ptr_bit (gdbarch, 32);
3363 }
3364
3365 /* Initialize ARCH for x64-32, no osabi. */
3366
3367 static void
amd64_x32_none_init_abi(gdbarch_info info,gdbarch * arch)3368 amd64_x32_none_init_abi (gdbarch_info info, gdbarch *arch)
3369 {
3370 amd64_x32_init_abi (info, arch,
3371 amd64_target_description (X86_XSTATE_SSE_MASK, true));
3372 }
3373
3374 /* Return the target description for a specified XSAVE feature mask. */
3375
3376 const struct target_desc *
amd64_target_description(uint64_t xcr0,bool segments)3377 amd64_target_description (uint64_t xcr0, bool segments)
3378 {
3379 static target_desc *amd64_tdescs \
3380 [2/*AVX*/][2/*MPX*/][2/*AVX512*/][2/*PKRU*/][2/*segments*/] = {};
3381 target_desc **tdesc;
3382
3383 tdesc = &amd64_tdescs[(xcr0 & X86_XSTATE_AVX) ? 1 : 0]
3384 [(xcr0 & X86_XSTATE_MPX) ? 1 : 0]
3385 [(xcr0 & X86_XSTATE_AVX512) ? 1 : 0]
3386 [(xcr0 & X86_XSTATE_PKRU) ? 1 : 0]
3387 [segments ? 1 : 0];
3388
3389 if (*tdesc == NULL)
3390 *tdesc = amd64_create_target_description (xcr0, false, false,
3391 segments);
3392
3393 return *tdesc;
3394 }
3395
3396 void _initialize_amd64_tdep ();
3397 void
_initialize_amd64_tdep()3398 _initialize_amd64_tdep ()
3399 {
3400 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_NONE,
3401 amd64_none_init_abi);
3402 gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x64_32, GDB_OSABI_NONE,
3403 amd64_x32_none_init_abi);
3404 }
3405
3406
3407 /* The 64-bit FXSAVE format differs from the 32-bit format in the
3408 sense that the instruction pointer and data pointer are simply
3409 64-bit offsets into the code segment and the data segment instead
3410 of a selector offset pair. The functions below store the upper 32
3411 bits of these pointers (instead of just the 16-bits of the segment
3412 selector). */
3413
3414 /* Fill register REGNUM in REGCACHE with the appropriate
3415 floating-point or SSE register value from *FXSAVE. If REGNUM is
3416 -1, do this for all registers. This function masks off any of the
3417 reserved bits in *FXSAVE. */
3418
3419 void
amd64_supply_fxsave(struct regcache * regcache,int regnum,const void * fxsave)3420 amd64_supply_fxsave (struct regcache *regcache, int regnum,
3421 const void *fxsave)
3422 {
3423 struct gdbarch *gdbarch = regcache->arch ();
3424 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3425
3426 i387_supply_fxsave (regcache, regnum, fxsave);
3427
3428 if (fxsave
3429 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3430 {
3431 const gdb_byte *regs = (const gdb_byte *) fxsave;
3432
3433 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3434 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3435 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3436 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3437 }
3438 }
3439
3440 /* Similar to amd64_supply_fxsave, but use XSAVE extended state. */
3441
3442 void
amd64_supply_xsave(struct regcache * regcache,int regnum,const void * xsave)3443 amd64_supply_xsave (struct regcache *regcache, int regnum,
3444 const void *xsave)
3445 {
3446 struct gdbarch *gdbarch = regcache->arch ();
3447 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3448
3449 i387_supply_xsave (regcache, regnum, xsave);
3450
3451 if (xsave
3452 && gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3453 {
3454 const gdb_byte *regs = (const gdb_byte *) xsave;
3455 ULONGEST clear_bv;
3456
3457 clear_bv = i387_xsave_get_clear_bv (gdbarch, xsave);
3458
3459 /* If the FISEG and FOSEG registers have not been initialised yet
3460 (their CLEAR_BV bit is set) then their default values of zero will
3461 have already been setup by I387_SUPPLY_XSAVE. */
3462 if (!(clear_bv & X86_XSTATE_X87))
3463 {
3464 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3465 regcache->raw_supply (I387_FISEG_REGNUM (tdep), regs + 12);
3466 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3467 regcache->raw_supply (I387_FOSEG_REGNUM (tdep), regs + 20);
3468 }
3469 }
3470 }
3471
3472 /* Fill register REGNUM (if it is a floating-point or SSE register) in
3473 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
3474 all registers. This function doesn't touch any of the reserved
3475 bits in *FXSAVE. */
3476
3477 void
amd64_collect_fxsave(const struct regcache * regcache,int regnum,void * fxsave)3478 amd64_collect_fxsave (const struct regcache *regcache, int regnum,
3479 void *fxsave)
3480 {
3481 struct gdbarch *gdbarch = regcache->arch ();
3482 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3483 gdb_byte *regs = (gdb_byte *) fxsave;
3484
3485 i387_collect_fxsave (regcache, regnum, fxsave);
3486
3487 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3488 {
3489 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3490 regcache->raw_collect (I387_FISEG_REGNUM (tdep), regs + 12);
3491 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3492 regcache->raw_collect (I387_FOSEG_REGNUM (tdep), regs + 20);
3493 }
3494 }
3495
3496 /* Similar to amd64_collect_fxsave, but use XSAVE extended state. */
3497
3498 void
amd64_collect_xsave(const struct regcache * regcache,int regnum,void * xsave,int gcore)3499 amd64_collect_xsave (const struct regcache *regcache, int regnum,
3500 void *xsave, int gcore)
3501 {
3502 struct gdbarch *gdbarch = regcache->arch ();
3503 i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
3504 gdb_byte *regs = (gdb_byte *) xsave;
3505
3506 i387_collect_xsave (regcache, regnum, xsave, gcore);
3507
3508 if (gdbarch_bfd_arch_info (gdbarch)->bits_per_word == 64)
3509 {
3510 if (regnum == -1 || regnum == I387_FISEG_REGNUM (tdep))
3511 regcache->raw_collect (I387_FISEG_REGNUM (tdep),
3512 regs + 12);
3513 if (regnum == -1 || regnum == I387_FOSEG_REGNUM (tdep))
3514 regcache->raw_collect (I387_FOSEG_REGNUM (tdep),
3515 regs + 20);
3516 }
3517 }
3518