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