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