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