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