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