]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Intel 386 target-dependent stuff. |
349c5d5f | 2 | |
6aba47ca | 3 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, |
9b254dd1 | 4 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
5ae96ec1 | 5 | Free Software Foundation, Inc. |
c906108c | 6 | |
c5aa993b | 7 | This file is part of GDB. |
c906108c | 8 | |
c5aa993b JM |
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 |
c5aa993b | 12 | (at your option) any later version. |
c906108c | 13 | |
c5aa993b JM |
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. | |
c906108c | 18 | |
c5aa993b | 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/>. */ |
c906108c SS |
21 | |
22 | #include "defs.h" | |
acd5c798 MK |
23 | #include "arch-utils.h" |
24 | #include "command.h" | |
25 | #include "dummy-frame.h" | |
6405b0a6 | 26 | #include "dwarf2-frame.h" |
acd5c798 | 27 | #include "doublest.h" |
c906108c | 28 | #include "frame.h" |
acd5c798 MK |
29 | #include "frame-base.h" |
30 | #include "frame-unwind.h" | |
c906108c | 31 | #include "inferior.h" |
acd5c798 | 32 | #include "gdbcmd.h" |
c906108c | 33 | #include "gdbcore.h" |
e6bb342a | 34 | #include "gdbtypes.h" |
dfe01d39 | 35 | #include "objfiles.h" |
acd5c798 MK |
36 | #include "osabi.h" |
37 | #include "regcache.h" | |
38 | #include "reggroups.h" | |
473f17b0 | 39 | #include "regset.h" |
c0d1d883 | 40 | #include "symfile.h" |
c906108c | 41 | #include "symtab.h" |
acd5c798 | 42 | #include "target.h" |
fd0407d6 | 43 | #include "value.h" |
a89aa300 | 44 | #include "dis-asm.h" |
acd5c798 | 45 | |
3d261580 | 46 | #include "gdb_assert.h" |
acd5c798 | 47 | #include "gdb_string.h" |
3d261580 | 48 | |
d2a7c97a | 49 | #include "i386-tdep.h" |
61113f8b | 50 | #include "i387-tdep.h" |
d2a7c97a | 51 | |
c4fc7f1b | 52 | /* Register names. */ |
c40e1eab | 53 | |
fc633446 MK |
54 | static char *i386_register_names[] = |
55 | { | |
56 | "eax", "ecx", "edx", "ebx", | |
57 | "esp", "ebp", "esi", "edi", | |
58 | "eip", "eflags", "cs", "ss", | |
59 | "ds", "es", "fs", "gs", | |
60 | "st0", "st1", "st2", "st3", | |
61 | "st4", "st5", "st6", "st7", | |
62 | "fctrl", "fstat", "ftag", "fiseg", | |
63 | "fioff", "foseg", "fooff", "fop", | |
64 | "xmm0", "xmm1", "xmm2", "xmm3", | |
65 | "xmm4", "xmm5", "xmm6", "xmm7", | |
66 | "mxcsr" | |
67 | }; | |
68 | ||
1cb97e17 | 69 | static const int i386_num_register_names = ARRAY_SIZE (i386_register_names); |
c40e1eab | 70 | |
c4fc7f1b | 71 | /* Register names for MMX pseudo-registers. */ |
28fc6740 AC |
72 | |
73 | static char *i386_mmx_names[] = | |
74 | { | |
75 | "mm0", "mm1", "mm2", "mm3", | |
76 | "mm4", "mm5", "mm6", "mm7" | |
77 | }; | |
c40e1eab | 78 | |
1cb97e17 | 79 | static const int i386_num_mmx_regs = ARRAY_SIZE (i386_mmx_names); |
c40e1eab | 80 | |
28fc6740 | 81 | static int |
5716833c | 82 | i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum) |
28fc6740 | 83 | { |
5716833c MK |
84 | int mm0_regnum = gdbarch_tdep (gdbarch)->mm0_regnum; |
85 | ||
86 | if (mm0_regnum < 0) | |
87 | return 0; | |
88 | ||
89 | return (regnum >= mm0_regnum && regnum < mm0_regnum + i386_num_mmx_regs); | |
28fc6740 AC |
90 | } |
91 | ||
5716833c | 92 | /* SSE register? */ |
23a34459 | 93 | |
5716833c MK |
94 | static int |
95 | i386_sse_regnum_p (struct gdbarch *gdbarch, int regnum) | |
23a34459 | 96 | { |
5716833c MK |
97 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
98 | ||
20a6ec49 | 99 | if (I387_NUM_XMM_REGS (tdep) == 0) |
5716833c MK |
100 | return 0; |
101 | ||
20a6ec49 MD |
102 | return (I387_XMM0_REGNUM (tdep) <= regnum |
103 | && regnum < I387_MXCSR_REGNUM (tdep)); | |
23a34459 AC |
104 | } |
105 | ||
5716833c MK |
106 | static int |
107 | i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum) | |
23a34459 | 108 | { |
5716833c MK |
109 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
110 | ||
20a6ec49 | 111 | if (I387_NUM_XMM_REGS (tdep) == 0) |
5716833c MK |
112 | return 0; |
113 | ||
20a6ec49 | 114 | return (regnum == I387_MXCSR_REGNUM (tdep)); |
23a34459 AC |
115 | } |
116 | ||
5716833c | 117 | /* FP register? */ |
23a34459 AC |
118 | |
119 | int | |
20a6ec49 | 120 | i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum) |
23a34459 | 121 | { |
20a6ec49 MD |
122 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
123 | ||
124 | if (I387_ST0_REGNUM (tdep) < 0) | |
5716833c MK |
125 | return 0; |
126 | ||
20a6ec49 MD |
127 | return (I387_ST0_REGNUM (tdep) <= regnum |
128 | && regnum < I387_FCTRL_REGNUM (tdep)); | |
23a34459 AC |
129 | } |
130 | ||
131 | int | |
20a6ec49 | 132 | i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum) |
23a34459 | 133 | { |
20a6ec49 MD |
134 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
135 | ||
136 | if (I387_ST0_REGNUM (tdep) < 0) | |
5716833c MK |
137 | return 0; |
138 | ||
20a6ec49 MD |
139 | return (I387_FCTRL_REGNUM (tdep) <= regnum |
140 | && regnum < I387_XMM0_REGNUM (tdep)); | |
23a34459 AC |
141 | } |
142 | ||
30b0e2d8 | 143 | /* Return the name of register REGNUM. */ |
fc633446 | 144 | |
fa88f677 | 145 | const char * |
d93859e2 | 146 | i386_register_name (struct gdbarch *gdbarch, int regnum) |
fc633446 | 147 | { |
d93859e2 | 148 | if (i386_mmx_regnum_p (gdbarch, regnum)) |
20a6ec49 | 149 | return i386_mmx_names[regnum - I387_MM0_REGNUM (gdbarch_tdep (gdbarch))]; |
fc633446 | 150 | |
30b0e2d8 MK |
151 | if (regnum >= 0 && regnum < i386_num_register_names) |
152 | return i386_register_names[regnum]; | |
70913449 | 153 | |
c40e1eab | 154 | return NULL; |
fc633446 MK |
155 | } |
156 | ||
c4fc7f1b | 157 | /* Convert a dbx register number REG to the appropriate register |
85540d8c MK |
158 | number used by GDB. */ |
159 | ||
8201327c | 160 | static int |
d3f73121 | 161 | i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg) |
85540d8c | 162 | { |
20a6ec49 MD |
163 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
164 | ||
c4fc7f1b MK |
165 | /* This implements what GCC calls the "default" register map |
166 | (dbx_register_map[]). */ | |
167 | ||
85540d8c MK |
168 | if (reg >= 0 && reg <= 7) |
169 | { | |
9872ad24 JB |
170 | /* General-purpose registers. The debug info calls %ebp |
171 | register 4, and %esp register 5. */ | |
172 | if (reg == 4) | |
173 | return 5; | |
174 | else if (reg == 5) | |
175 | return 4; | |
176 | else return reg; | |
85540d8c MK |
177 | } |
178 | else if (reg >= 12 && reg <= 19) | |
179 | { | |
180 | /* Floating-point registers. */ | |
20a6ec49 | 181 | return reg - 12 + I387_ST0_REGNUM (tdep); |
85540d8c MK |
182 | } |
183 | else if (reg >= 21 && reg <= 28) | |
184 | { | |
185 | /* SSE registers. */ | |
20a6ec49 | 186 | return reg - 21 + I387_XMM0_REGNUM (tdep); |
85540d8c MK |
187 | } |
188 | else if (reg >= 29 && reg <= 36) | |
189 | { | |
190 | /* MMX registers. */ | |
20a6ec49 | 191 | return reg - 29 + I387_MM0_REGNUM (tdep); |
85540d8c MK |
192 | } |
193 | ||
194 | /* This will hopefully provoke a warning. */ | |
d3f73121 | 195 | return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); |
85540d8c MK |
196 | } |
197 | ||
c4fc7f1b MK |
198 | /* Convert SVR4 register number REG to the appropriate register number |
199 | used by GDB. */ | |
85540d8c | 200 | |
8201327c | 201 | static int |
d3f73121 | 202 | i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg) |
85540d8c | 203 | { |
20a6ec49 MD |
204 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
205 | ||
c4fc7f1b MK |
206 | /* This implements the GCC register map that tries to be compatible |
207 | with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]). */ | |
208 | ||
209 | /* The SVR4 register numbering includes %eip and %eflags, and | |
85540d8c MK |
210 | numbers the floating point registers differently. */ |
211 | if (reg >= 0 && reg <= 9) | |
212 | { | |
acd5c798 | 213 | /* General-purpose registers. */ |
85540d8c MK |
214 | return reg; |
215 | } | |
216 | else if (reg >= 11 && reg <= 18) | |
217 | { | |
218 | /* Floating-point registers. */ | |
20a6ec49 | 219 | return reg - 11 + I387_ST0_REGNUM (tdep); |
85540d8c | 220 | } |
c6f4c129 | 221 | else if (reg >= 21 && reg <= 36) |
85540d8c | 222 | { |
c4fc7f1b | 223 | /* The SSE and MMX registers have the same numbers as with dbx. */ |
d3f73121 | 224 | return i386_dbx_reg_to_regnum (gdbarch, reg); |
85540d8c MK |
225 | } |
226 | ||
c6f4c129 JB |
227 | switch (reg) |
228 | { | |
20a6ec49 MD |
229 | case 37: return I387_FCTRL_REGNUM (tdep); |
230 | case 38: return I387_FSTAT_REGNUM (tdep); | |
231 | case 39: return I387_MXCSR_REGNUM (tdep); | |
c6f4c129 JB |
232 | case 40: return I386_ES_REGNUM; |
233 | case 41: return I386_CS_REGNUM; | |
234 | case 42: return I386_SS_REGNUM; | |
235 | case 43: return I386_DS_REGNUM; | |
236 | case 44: return I386_FS_REGNUM; | |
237 | case 45: return I386_GS_REGNUM; | |
238 | } | |
239 | ||
85540d8c | 240 | /* This will hopefully provoke a warning. */ |
d3f73121 | 241 | return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); |
85540d8c | 242 | } |
5716833c | 243 | |
fc338970 | 244 | \f |
917317f4 | 245 | |
fc338970 MK |
246 | /* This is the variable that is set with "set disassembly-flavor", and |
247 | its legitimate values. */ | |
53904c9e AC |
248 | static const char att_flavor[] = "att"; |
249 | static const char intel_flavor[] = "intel"; | |
250 | static const char *valid_flavors[] = | |
c5aa993b | 251 | { |
c906108c SS |
252 | att_flavor, |
253 | intel_flavor, | |
254 | NULL | |
255 | }; | |
53904c9e | 256 | static const char *disassembly_flavor = att_flavor; |
acd5c798 | 257 | \f |
c906108c | 258 | |
acd5c798 MK |
259 | /* Use the program counter to determine the contents and size of a |
260 | breakpoint instruction. Return a pointer to a string of bytes that | |
261 | encode a breakpoint instruction, store the length of the string in | |
262 | *LEN and optionally adjust *PC to point to the correct memory | |
263 | location for inserting the breakpoint. | |
c906108c | 264 | |
acd5c798 MK |
265 | On the i386 we have a single breakpoint that fits in a single byte |
266 | and can be inserted anywhere. | |
c906108c | 267 | |
acd5c798 | 268 | This function is 64-bit safe. */ |
63c0089f MK |
269 | |
270 | static const gdb_byte * | |
67d57894 | 271 | i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len) |
c906108c | 272 | { |
63c0089f MK |
273 | static gdb_byte break_insn[] = { 0xcc }; /* int 3 */ |
274 | ||
acd5c798 MK |
275 | *len = sizeof (break_insn); |
276 | return break_insn; | |
c906108c | 277 | } |
237fc4c9 PA |
278 | \f |
279 | /* Displaced instruction handling. */ | |
280 | ||
281 | ||
282 | static int | |
283 | i386_absolute_jmp_p (gdb_byte *insn) | |
284 | { | |
285 | /* jmp far (absolute address in operand) */ | |
286 | if (insn[0] == 0xea) | |
287 | return 1; | |
288 | ||
289 | if (insn[0] == 0xff) | |
290 | { | |
291 | /* jump near, absolute indirect (/4) */ | |
292 | if ((insn[1] & 0x38) == 0x20) | |
293 | return 1; | |
294 | ||
295 | /* jump far, absolute indirect (/5) */ | |
296 | if ((insn[1] & 0x38) == 0x28) | |
297 | return 1; | |
298 | } | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | static int | |
304 | i386_absolute_call_p (gdb_byte *insn) | |
305 | { | |
306 | /* call far, absolute */ | |
307 | if (insn[0] == 0x9a) | |
308 | return 1; | |
309 | ||
310 | if (insn[0] == 0xff) | |
311 | { | |
312 | /* Call near, absolute indirect (/2) */ | |
313 | if ((insn[1] & 0x38) == 0x10) | |
314 | return 1; | |
315 | ||
316 | /* Call far, absolute indirect (/3) */ | |
317 | if ((insn[1] & 0x38) == 0x18) | |
318 | return 1; | |
319 | } | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | static int | |
325 | i386_ret_p (gdb_byte *insn) | |
326 | { | |
327 | switch (insn[0]) | |
328 | { | |
329 | case 0xc2: /* ret near, pop N bytes */ | |
330 | case 0xc3: /* ret near */ | |
331 | case 0xca: /* ret far, pop N bytes */ | |
332 | case 0xcb: /* ret far */ | |
333 | case 0xcf: /* iret */ | |
334 | return 1; | |
335 | ||
336 | default: | |
337 | return 0; | |
338 | } | |
339 | } | |
340 | ||
341 | static int | |
342 | i386_call_p (gdb_byte *insn) | |
343 | { | |
344 | if (i386_absolute_call_p (insn)) | |
345 | return 1; | |
346 | ||
347 | /* call near, relative */ | |
348 | if (insn[0] == 0xe8) | |
349 | return 1; | |
350 | ||
351 | return 0; | |
352 | } | |
353 | ||
354 | static int | |
355 | i386_breakpoint_p (gdb_byte *insn) | |
356 | { | |
357 | return insn[0] == 0xcc; /* int 3 */ | |
358 | } | |
359 | ||
360 | /* Return non-zero if INSN is a system call, and set *LENGTHP to its | |
361 | length in bytes. Otherwise, return zero. */ | |
362 | static int | |
363 | i386_syscall_p (gdb_byte *insn, ULONGEST *lengthp) | |
364 | { | |
365 | if (insn[0] == 0xcd) | |
366 | { | |
367 | *lengthp = 2; | |
368 | return 1; | |
369 | } | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | /* Fix up the state of registers and memory after having single-stepped | |
375 | a displaced instruction. */ | |
376 | void | |
377 | i386_displaced_step_fixup (struct gdbarch *gdbarch, | |
378 | struct displaced_step_closure *closure, | |
379 | CORE_ADDR from, CORE_ADDR to, | |
380 | struct regcache *regs) | |
381 | { | |
382 | /* The offset we applied to the instruction's address. | |
383 | This could well be negative (when viewed as a signed 32-bit | |
384 | value), but ULONGEST won't reflect that, so take care when | |
385 | applying it. */ | |
386 | ULONGEST insn_offset = to - from; | |
387 | ||
388 | /* Since we use simple_displaced_step_copy_insn, our closure is a | |
389 | copy of the instruction. */ | |
390 | gdb_byte *insn = (gdb_byte *) closure; | |
391 | ||
392 | if (debug_displaced) | |
393 | fprintf_unfiltered (gdb_stdlog, | |
394 | "displaced: fixup (0x%s, 0x%s), " | |
395 | "insn = 0x%02x 0x%02x ...\n", | |
396 | paddr_nz (from), paddr_nz (to), insn[0], insn[1]); | |
397 | ||
398 | /* The list of issues to contend with here is taken from | |
399 | resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20. | |
400 | Yay for Free Software! */ | |
401 | ||
402 | /* Relocate the %eip, if necessary. */ | |
403 | ||
404 | /* Except in the case of absolute or indirect jump or call | |
405 | instructions, or a return instruction, the new eip is relative to | |
406 | the displaced instruction; make it relative. Well, signal | |
407 | handler returns don't need relocation either, but we use the | |
408 | value of %eip to recognize those; see below. */ | |
409 | if (! i386_absolute_jmp_p (insn) | |
410 | && ! i386_absolute_call_p (insn) | |
411 | && ! i386_ret_p (insn)) | |
412 | { | |
413 | ULONGEST orig_eip; | |
414 | ULONGEST insn_len; | |
415 | ||
416 | regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip); | |
417 | ||
418 | /* A signal trampoline system call changes the %eip, resuming | |
419 | execution of the main program after the signal handler has | |
420 | returned. That makes them like 'return' instructions; we | |
421 | shouldn't relocate %eip. | |
422 | ||
423 | But most system calls don't, and we do need to relocate %eip. | |
424 | ||
425 | Our heuristic for distinguishing these cases: if stepping | |
426 | over the system call instruction left control directly after | |
427 | the instruction, the we relocate --- control almost certainly | |
428 | doesn't belong in the displaced copy. Otherwise, we assume | |
429 | the instruction has put control where it belongs, and leave | |
430 | it unrelocated. Goodness help us if there are PC-relative | |
431 | system calls. */ | |
432 | if (i386_syscall_p (insn, &insn_len) | |
433 | && orig_eip != to + insn_len) | |
434 | { | |
435 | if (debug_displaced) | |
436 | fprintf_unfiltered (gdb_stdlog, | |
437 | "displaced: syscall changed %%eip; " | |
438 | "not relocating\n"); | |
439 | } | |
440 | else | |
441 | { | |
442 | ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL; | |
443 | ||
444 | /* If we have stepped over a breakpoint, set the %eip to | |
445 | point at the breakpoint instruction itself. | |
446 | ||
447 | (gdbarch_decr_pc_after_break was never something the core | |
448 | of GDB should have been concerned with; arch-specific | |
449 | code should be making PC values consistent before | |
450 | presenting them to GDB.) */ | |
451 | if (i386_breakpoint_p (insn)) | |
452 | { | |
453 | fprintf_unfiltered (gdb_stdlog, | |
454 | "displaced: stepped breakpoint\n"); | |
455 | eip--; | |
456 | } | |
457 | ||
458 | regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip); | |
459 | ||
460 | if (debug_displaced) | |
461 | fprintf_unfiltered (gdb_stdlog, | |
462 | "displaced: " | |
463 | "relocated %%eip from 0x%s to 0x%s\n", | |
464 | paddr_nz (orig_eip), paddr_nz (eip)); | |
465 | } | |
466 | } | |
467 | ||
468 | /* If the instruction was PUSHFL, then the TF bit will be set in the | |
469 | pushed value, and should be cleared. We'll leave this for later, | |
470 | since GDB already messes up the TF flag when stepping over a | |
471 | pushfl. */ | |
472 | ||
473 | /* If the instruction was a call, the return address now atop the | |
474 | stack is the address following the copied instruction. We need | |
475 | to make it the address following the original instruction. */ | |
476 | if (i386_call_p (insn)) | |
477 | { | |
478 | ULONGEST esp; | |
479 | ULONGEST retaddr; | |
480 | const ULONGEST retaddr_len = 4; | |
481 | ||
482 | regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp); | |
483 | retaddr = read_memory_unsigned_integer (esp, retaddr_len); | |
484 | retaddr = (retaddr - insn_offset) & 0xffffffffUL; | |
485 | write_memory_unsigned_integer (esp, retaddr_len, retaddr); | |
486 | ||
487 | if (debug_displaced) | |
488 | fprintf_unfiltered (gdb_stdlog, | |
489 | "displaced: relocated return addr at 0x%s " | |
490 | "to 0x%s\n", | |
491 | paddr_nz (esp), | |
492 | paddr_nz (retaddr)); | |
493 | } | |
494 | } | |
495 | ||
496 | ||
fc338970 | 497 | \f |
acd5c798 MK |
498 | #ifdef I386_REGNO_TO_SYMMETRY |
499 | #error "The Sequent Symmetry is no longer supported." | |
500 | #endif | |
c906108c | 501 | |
acd5c798 MK |
502 | /* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi |
503 | and %esp "belong" to the calling function. Therefore these | |
504 | registers should be saved if they're going to be modified. */ | |
c906108c | 505 | |
acd5c798 MK |
506 | /* The maximum number of saved registers. This should include all |
507 | registers mentioned above, and %eip. */ | |
a3386186 | 508 | #define I386_NUM_SAVED_REGS I386_NUM_GREGS |
acd5c798 MK |
509 | |
510 | struct i386_frame_cache | |
c906108c | 511 | { |
acd5c798 MK |
512 | /* Base address. */ |
513 | CORE_ADDR base; | |
772562f8 | 514 | LONGEST sp_offset; |
acd5c798 MK |
515 | CORE_ADDR pc; |
516 | ||
fd13a04a AC |
517 | /* Saved registers. */ |
518 | CORE_ADDR saved_regs[I386_NUM_SAVED_REGS]; | |
acd5c798 | 519 | CORE_ADDR saved_sp; |
92dd43fa | 520 | int stack_align; |
acd5c798 MK |
521 | int pc_in_eax; |
522 | ||
523 | /* Stack space reserved for local variables. */ | |
524 | long locals; | |
525 | }; | |
526 | ||
527 | /* Allocate and initialize a frame cache. */ | |
528 | ||
529 | static struct i386_frame_cache * | |
fd13a04a | 530 | i386_alloc_frame_cache (void) |
acd5c798 MK |
531 | { |
532 | struct i386_frame_cache *cache; | |
533 | int i; | |
534 | ||
535 | cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache); | |
536 | ||
537 | /* Base address. */ | |
538 | cache->base = 0; | |
539 | cache->sp_offset = -4; | |
540 | cache->pc = 0; | |
541 | ||
fd13a04a AC |
542 | /* Saved registers. We initialize these to -1 since zero is a valid |
543 | offset (that's where %ebp is supposed to be stored). */ | |
544 | for (i = 0; i < I386_NUM_SAVED_REGS; i++) | |
545 | cache->saved_regs[i] = -1; | |
acd5c798 | 546 | cache->saved_sp = 0; |
92dd43fa | 547 | cache->stack_align = 0; |
acd5c798 MK |
548 | cache->pc_in_eax = 0; |
549 | ||
550 | /* Frameless until proven otherwise. */ | |
551 | cache->locals = -1; | |
552 | ||
553 | return cache; | |
554 | } | |
c906108c | 555 | |
acd5c798 MK |
556 | /* If the instruction at PC is a jump, return the address of its |
557 | target. Otherwise, return PC. */ | |
c906108c | 558 | |
acd5c798 MK |
559 | static CORE_ADDR |
560 | i386_follow_jump (CORE_ADDR pc) | |
561 | { | |
63c0089f | 562 | gdb_byte op; |
acd5c798 MK |
563 | long delta = 0; |
564 | int data16 = 0; | |
c906108c | 565 | |
8defab1a | 566 | target_read_memory (pc, &op, 1); |
acd5c798 | 567 | if (op == 0x66) |
c906108c | 568 | { |
c906108c | 569 | data16 = 1; |
acd5c798 | 570 | op = read_memory_unsigned_integer (pc + 1, 1); |
c906108c SS |
571 | } |
572 | ||
acd5c798 | 573 | switch (op) |
c906108c SS |
574 | { |
575 | case 0xe9: | |
fc338970 | 576 | /* Relative jump: if data16 == 0, disp32, else disp16. */ |
c906108c SS |
577 | if (data16) |
578 | { | |
acd5c798 | 579 | delta = read_memory_integer (pc + 2, 2); |
c906108c | 580 | |
fc338970 MK |
581 | /* Include the size of the jmp instruction (including the |
582 | 0x66 prefix). */ | |
acd5c798 | 583 | delta += 4; |
c906108c SS |
584 | } |
585 | else | |
586 | { | |
acd5c798 | 587 | delta = read_memory_integer (pc + 1, 4); |
c906108c | 588 | |
acd5c798 MK |
589 | /* Include the size of the jmp instruction. */ |
590 | delta += 5; | |
c906108c SS |
591 | } |
592 | break; | |
593 | case 0xeb: | |
fc338970 | 594 | /* Relative jump, disp8 (ignore data16). */ |
acd5c798 | 595 | delta = read_memory_integer (pc + data16 + 1, 1); |
c906108c | 596 | |
acd5c798 | 597 | delta += data16 + 2; |
c906108c SS |
598 | break; |
599 | } | |
c906108c | 600 | |
acd5c798 MK |
601 | return pc + delta; |
602 | } | |
fc338970 | 603 | |
acd5c798 MK |
604 | /* Check whether PC points at a prologue for a function returning a |
605 | structure or union. If so, it updates CACHE and returns the | |
606 | address of the first instruction after the code sequence that | |
607 | removes the "hidden" argument from the stack or CURRENT_PC, | |
608 | whichever is smaller. Otherwise, return PC. */ | |
c906108c | 609 | |
acd5c798 MK |
610 | static CORE_ADDR |
611 | i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc, | |
612 | struct i386_frame_cache *cache) | |
c906108c | 613 | { |
acd5c798 MK |
614 | /* Functions that return a structure or union start with: |
615 | ||
616 | popl %eax 0x58 | |
617 | xchgl %eax, (%esp) 0x87 0x04 0x24 | |
618 | or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00 | |
619 | ||
620 | (the System V compiler puts out the second `xchg' instruction, | |
621 | and the assembler doesn't try to optimize it, so the 'sib' form | |
622 | gets generated). This sequence is used to get the address of the | |
623 | return buffer for a function that returns a structure. */ | |
63c0089f MK |
624 | static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 }; |
625 | static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 }; | |
626 | gdb_byte buf[4]; | |
627 | gdb_byte op; | |
c906108c | 628 | |
acd5c798 MK |
629 | if (current_pc <= pc) |
630 | return pc; | |
631 | ||
8defab1a | 632 | target_read_memory (pc, &op, 1); |
c906108c | 633 | |
acd5c798 MK |
634 | if (op != 0x58) /* popl %eax */ |
635 | return pc; | |
c906108c | 636 | |
8defab1a | 637 | target_read_memory (pc + 1, buf, 4); |
acd5c798 MK |
638 | if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0) |
639 | return pc; | |
c906108c | 640 | |
acd5c798 | 641 | if (current_pc == pc) |
c906108c | 642 | { |
acd5c798 MK |
643 | cache->sp_offset += 4; |
644 | return current_pc; | |
c906108c SS |
645 | } |
646 | ||
acd5c798 | 647 | if (current_pc == pc + 1) |
c906108c | 648 | { |
acd5c798 MK |
649 | cache->pc_in_eax = 1; |
650 | return current_pc; | |
651 | } | |
652 | ||
653 | if (buf[1] == proto1[1]) | |
654 | return pc + 4; | |
655 | else | |
656 | return pc + 5; | |
657 | } | |
658 | ||
659 | static CORE_ADDR | |
660 | i386_skip_probe (CORE_ADDR pc) | |
661 | { | |
662 | /* A function may start with | |
fc338970 | 663 | |
acd5c798 MK |
664 | pushl constant |
665 | call _probe | |
666 | addl $4, %esp | |
fc338970 | 667 | |
acd5c798 MK |
668 | followed by |
669 | ||
670 | pushl %ebp | |
fc338970 | 671 | |
acd5c798 | 672 | etc. */ |
63c0089f MK |
673 | gdb_byte buf[8]; |
674 | gdb_byte op; | |
fc338970 | 675 | |
8defab1a | 676 | target_read_memory (pc, &op, 1); |
acd5c798 MK |
677 | |
678 | if (op == 0x68 || op == 0x6a) | |
679 | { | |
680 | int delta; | |
c906108c | 681 | |
acd5c798 MK |
682 | /* Skip past the `pushl' instruction; it has either a one-byte or a |
683 | four-byte operand, depending on the opcode. */ | |
c906108c | 684 | if (op == 0x68) |
acd5c798 | 685 | delta = 5; |
c906108c | 686 | else |
acd5c798 | 687 | delta = 2; |
c906108c | 688 | |
acd5c798 MK |
689 | /* Read the following 8 bytes, which should be `call _probe' (6 |
690 | bytes) followed by `addl $4,%esp' (2 bytes). */ | |
691 | read_memory (pc + delta, buf, sizeof (buf)); | |
c906108c | 692 | if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4) |
acd5c798 | 693 | pc += delta + sizeof (buf); |
c906108c SS |
694 | } |
695 | ||
acd5c798 MK |
696 | return pc; |
697 | } | |
698 | ||
92dd43fa MK |
699 | /* GCC 4.1 and later, can put code in the prologue to realign the |
700 | stack pointer. Check whether PC points to such code, and update | |
701 | CACHE accordingly. Return the first instruction after the code | |
702 | sequence or CURRENT_PC, whichever is smaller. If we don't | |
703 | recognize the code, return PC. */ | |
704 | ||
705 | static CORE_ADDR | |
706 | i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc, | |
707 | struct i386_frame_cache *cache) | |
708 | { | |
92a56b20 JB |
709 | /* The register used by the compiler to perform the stack re-alignment |
710 | is, in order of preference, either %ecx, %edx, or %eax. GCC should | |
711 | never use %ebx as it always treats it as callee-saved, whereas | |
712 | the compiler can only use caller-saved registers. */ | |
ade52156 | 713 | static const gdb_byte insns_ecx[10] = { |
92dd43fa MK |
714 | 0x8d, 0x4c, 0x24, 0x04, /* leal 4(%esp), %ecx */ |
715 | 0x83, 0xe4, 0xf0, /* andl $-16, %esp */ | |
716 | 0xff, 0x71, 0xfc /* pushl -4(%ecx) */ | |
717 | }; | |
ade52156 JB |
718 | static const gdb_byte insns_edx[10] = { |
719 | 0x8d, 0x54, 0x24, 0x04, /* leal 4(%esp), %edx */ | |
720 | 0x83, 0xe4, 0xf0, /* andl $-16, %esp */ | |
721 | 0xff, 0x72, 0xfc /* pushl -4(%edx) */ | |
722 | }; | |
723 | static const gdb_byte insns_eax[10] = { | |
724 | 0x8d, 0x44, 0x24, 0x04, /* leal 4(%esp), %eax */ | |
725 | 0x83, 0xe4, 0xf0, /* andl $-16, %esp */ | |
726 | 0xff, 0x70, 0xfc /* pushl -4(%eax) */ | |
727 | }; | |
92dd43fa MK |
728 | gdb_byte buf[10]; |
729 | ||
730 | if (target_read_memory (pc, buf, sizeof buf) | |
ade52156 JB |
731 | || (memcmp (buf, insns_ecx, sizeof buf) != 0 |
732 | && memcmp (buf, insns_edx, sizeof buf) != 0 | |
733 | && memcmp (buf, insns_eax, sizeof buf) != 0)) | |
92dd43fa MK |
734 | return pc; |
735 | ||
736 | if (current_pc > pc + 4) | |
737 | cache->stack_align = 1; | |
738 | ||
739 | return min (pc + 10, current_pc); | |
740 | } | |
741 | ||
37bdc87e | 742 | /* Maximum instruction length we need to handle. */ |
237fc4c9 | 743 | #define I386_MAX_MATCHED_INSN_LEN 6 |
37bdc87e MK |
744 | |
745 | /* Instruction description. */ | |
746 | struct i386_insn | |
747 | { | |
748 | size_t len; | |
237fc4c9 PA |
749 | gdb_byte insn[I386_MAX_MATCHED_INSN_LEN]; |
750 | gdb_byte mask[I386_MAX_MATCHED_INSN_LEN]; | |
37bdc87e MK |
751 | }; |
752 | ||
753 | /* Search for the instruction at PC in the list SKIP_INSNS. Return | |
754 | the first instruction description that matches. Otherwise, return | |
755 | NULL. */ | |
756 | ||
757 | static struct i386_insn * | |
758 | i386_match_insn (CORE_ADDR pc, struct i386_insn *skip_insns) | |
759 | { | |
760 | struct i386_insn *insn; | |
63c0089f | 761 | gdb_byte op; |
37bdc87e | 762 | |
8defab1a | 763 | target_read_memory (pc, &op, 1); |
37bdc87e MK |
764 | |
765 | for (insn = skip_insns; insn->len > 0; insn++) | |
766 | { | |
767 | if ((op & insn->mask[0]) == insn->insn[0]) | |
768 | { | |
237fc4c9 | 769 | gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1]; |
613e8135 | 770 | int insn_matched = 1; |
37bdc87e MK |
771 | size_t i; |
772 | ||
773 | gdb_assert (insn->len > 1); | |
237fc4c9 | 774 | gdb_assert (insn->len <= I386_MAX_MATCHED_INSN_LEN); |
37bdc87e | 775 | |
8defab1a | 776 | target_read_memory (pc + 1, buf, insn->len - 1); |
37bdc87e MK |
777 | for (i = 1; i < insn->len; i++) |
778 | { | |
779 | if ((buf[i - 1] & insn->mask[i]) != insn->insn[i]) | |
613e8135 | 780 | insn_matched = 0; |
37bdc87e | 781 | } |
613e8135 MK |
782 | |
783 | if (insn_matched) | |
784 | return insn; | |
37bdc87e MK |
785 | } |
786 | } | |
787 | ||
788 | return NULL; | |
789 | } | |
790 | ||
791 | /* Some special instructions that might be migrated by GCC into the | |
792 | part of the prologue that sets up the new stack frame. Because the | |
793 | stack frame hasn't been setup yet, no registers have been saved | |
794 | yet, and only the scratch registers %eax, %ecx and %edx can be | |
795 | touched. */ | |
796 | ||
797 | struct i386_insn i386_frame_setup_skip_insns[] = | |
798 | { | |
799 | /* Check for `movb imm8, r' and `movl imm32, r'. | |
800 | ||
801 | ??? Should we handle 16-bit operand-sizes here? */ | |
802 | ||
803 | /* `movb imm8, %al' and `movb imm8, %ah' */ | |
804 | /* `movb imm8, %cl' and `movb imm8, %ch' */ | |
805 | { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } }, | |
806 | /* `movb imm8, %dl' and `movb imm8, %dh' */ | |
807 | { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } }, | |
808 | /* `movl imm32, %eax' and `movl imm32, %ecx' */ | |
809 | { 5, { 0xb8 }, { 0xfe } }, | |
810 | /* `movl imm32, %edx' */ | |
811 | { 5, { 0xba }, { 0xff } }, | |
812 | ||
813 | /* Check for `mov imm32, r32'. Note that there is an alternative | |
814 | encoding for `mov m32, %eax'. | |
815 | ||
816 | ??? Should we handle SIB adressing here? | |
817 | ??? Should we handle 16-bit operand-sizes here? */ | |
818 | ||
819 | /* `movl m32, %eax' */ | |
820 | { 5, { 0xa1 }, { 0xff } }, | |
821 | /* `movl m32, %eax' and `mov; m32, %ecx' */ | |
822 | { 6, { 0x89, 0x05 }, {0xff, 0xf7 } }, | |
823 | /* `movl m32, %edx' */ | |
824 | { 6, { 0x89, 0x15 }, {0xff, 0xff } }, | |
825 | ||
826 | /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'. | |
827 | Because of the symmetry, there are actually two ways to encode | |
828 | these instructions; opcode bytes 0x29 and 0x2b for `subl' and | |
829 | opcode bytes 0x31 and 0x33 for `xorl'. */ | |
830 | ||
831 | /* `subl %eax, %eax' */ | |
832 | { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } }, | |
833 | /* `subl %ecx, %ecx' */ | |
834 | { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } }, | |
835 | /* `subl %edx, %edx' */ | |
836 | { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } }, | |
837 | /* `xorl %eax, %eax' */ | |
838 | { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } }, | |
839 | /* `xorl %ecx, %ecx' */ | |
840 | { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } }, | |
841 | /* `xorl %edx, %edx' */ | |
842 | { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } }, | |
843 | { 0 } | |
844 | }; | |
845 | ||
e11481da PM |
846 | |
847 | /* Check whether PC points to a no-op instruction. */ | |
848 | static CORE_ADDR | |
849 | i386_skip_noop (CORE_ADDR pc) | |
850 | { | |
851 | gdb_byte op; | |
852 | int check = 1; | |
853 | ||
8defab1a | 854 | target_read_memory (pc, &op, 1); |
e11481da PM |
855 | |
856 | while (check) | |
857 | { | |
858 | check = 0; | |
859 | /* Ignore `nop' instruction. */ | |
860 | if (op == 0x90) | |
861 | { | |
862 | pc += 1; | |
8defab1a | 863 | target_read_memory (pc, &op, 1); |
e11481da PM |
864 | check = 1; |
865 | } | |
866 | /* Ignore no-op instruction `mov %edi, %edi'. | |
867 | Microsoft system dlls often start with | |
868 | a `mov %edi,%edi' instruction. | |
869 | The 5 bytes before the function start are | |
870 | filled with `nop' instructions. | |
871 | This pattern can be used for hot-patching: | |
872 | The `mov %edi, %edi' instruction can be replaced by a | |
873 | near jump to the location of the 5 `nop' instructions | |
874 | which can be replaced by a 32-bit jump to anywhere | |
875 | in the 32-bit address space. */ | |
876 | ||
877 | else if (op == 0x8b) | |
878 | { | |
8defab1a | 879 | target_read_memory (pc + 1, &op, 1); |
e11481da PM |
880 | if (op == 0xff) |
881 | { | |
882 | pc += 2; | |
8defab1a | 883 | target_read_memory (pc, &op, 1); |
e11481da PM |
884 | check = 1; |
885 | } | |
886 | } | |
887 | } | |
888 | return pc; | |
889 | } | |
890 | ||
acd5c798 MK |
891 | /* Check whether PC points at a code that sets up a new stack frame. |
892 | If so, it updates CACHE and returns the address of the first | |
37bdc87e MK |
893 | instruction after the sequence that sets up the frame or LIMIT, |
894 | whichever is smaller. If we don't recognize the code, return PC. */ | |
acd5c798 MK |
895 | |
896 | static CORE_ADDR | |
37bdc87e | 897 | i386_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR limit, |
acd5c798 MK |
898 | struct i386_frame_cache *cache) |
899 | { | |
37bdc87e | 900 | struct i386_insn *insn; |
63c0089f | 901 | gdb_byte op; |
26604a34 | 902 | int skip = 0; |
acd5c798 | 903 | |
37bdc87e MK |
904 | if (limit <= pc) |
905 | return limit; | |
acd5c798 | 906 | |
8defab1a | 907 | target_read_memory (pc, &op, 1); |
acd5c798 | 908 | |
c906108c | 909 | if (op == 0x55) /* pushl %ebp */ |
c5aa993b | 910 | { |
acd5c798 MK |
911 | /* Take into account that we've executed the `pushl %ebp' that |
912 | starts this instruction sequence. */ | |
fd13a04a | 913 | cache->saved_regs[I386_EBP_REGNUM] = 0; |
acd5c798 | 914 | cache->sp_offset += 4; |
37bdc87e | 915 | pc++; |
acd5c798 MK |
916 | |
917 | /* If that's all, return now. */ | |
37bdc87e MK |
918 | if (limit <= pc) |
919 | return limit; | |
26604a34 | 920 | |
b4632131 | 921 | /* Check for some special instructions that might be migrated by |
37bdc87e MK |
922 | GCC into the prologue and skip them. At this point in the |
923 | prologue, code should only touch the scratch registers %eax, | |
924 | %ecx and %edx, so while the number of posibilities is sheer, | |
925 | it is limited. | |
5daa5b4e | 926 | |
26604a34 MK |
927 | Make sure we only skip these instructions if we later see the |
928 | `movl %esp, %ebp' that actually sets up the frame. */ | |
37bdc87e | 929 | while (pc + skip < limit) |
26604a34 | 930 | { |
37bdc87e MK |
931 | insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns); |
932 | if (insn == NULL) | |
933 | break; | |
b4632131 | 934 | |
37bdc87e | 935 | skip += insn->len; |
26604a34 MK |
936 | } |
937 | ||
37bdc87e MK |
938 | /* If that's all, return now. */ |
939 | if (limit <= pc + skip) | |
940 | return limit; | |
941 | ||
8defab1a | 942 | target_read_memory (pc + skip, &op, 1); |
37bdc87e | 943 | |
26604a34 | 944 | /* Check for `movl %esp, %ebp' -- can be written in two ways. */ |
acd5c798 | 945 | switch (op) |
c906108c SS |
946 | { |
947 | case 0x8b: | |
37bdc87e MK |
948 | if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xec) |
949 | return pc; | |
c906108c SS |
950 | break; |
951 | case 0x89: | |
37bdc87e MK |
952 | if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xe5) |
953 | return pc; | |
c906108c SS |
954 | break; |
955 | default: | |
37bdc87e | 956 | return pc; |
c906108c | 957 | } |
acd5c798 | 958 | |
26604a34 MK |
959 | /* OK, we actually have a frame. We just don't know how large |
960 | it is yet. Set its size to zero. We'll adjust it if | |
961 | necessary. We also now commit to skipping the special | |
962 | instructions mentioned before. */ | |
acd5c798 | 963 | cache->locals = 0; |
37bdc87e | 964 | pc += (skip + 2); |
acd5c798 MK |
965 | |
966 | /* If that's all, return now. */ | |
37bdc87e MK |
967 | if (limit <= pc) |
968 | return limit; | |
acd5c798 | 969 | |
fc338970 MK |
970 | /* Check for stack adjustment |
971 | ||
acd5c798 | 972 | subl $XXX, %esp |
fc338970 | 973 | |
fd35795f | 974 | NOTE: You can't subtract a 16-bit immediate from a 32-bit |
fc338970 | 975 | reg, so we don't have to worry about a data16 prefix. */ |
8defab1a | 976 | target_read_memory (pc, &op, 1); |
c906108c SS |
977 | if (op == 0x83) |
978 | { | |
fd35795f | 979 | /* `subl' with 8-bit immediate. */ |
37bdc87e | 980 | if (read_memory_unsigned_integer (pc + 1, 1) != 0xec) |
fc338970 | 981 | /* Some instruction starting with 0x83 other than `subl'. */ |
37bdc87e | 982 | return pc; |
acd5c798 | 983 | |
37bdc87e MK |
984 | /* `subl' with signed 8-bit immediate (though it wouldn't |
985 | make sense to be negative). */ | |
986 | cache->locals = read_memory_integer (pc + 2, 1); | |
987 | return pc + 3; | |
c906108c SS |
988 | } |
989 | else if (op == 0x81) | |
990 | { | |
fd35795f | 991 | /* Maybe it is `subl' with a 32-bit immediate. */ |
37bdc87e | 992 | if (read_memory_unsigned_integer (pc + 1, 1) != 0xec) |
fc338970 | 993 | /* Some instruction starting with 0x81 other than `subl'. */ |
37bdc87e | 994 | return pc; |
acd5c798 | 995 | |
fd35795f | 996 | /* It is `subl' with a 32-bit immediate. */ |
37bdc87e MK |
997 | cache->locals = read_memory_integer (pc + 2, 4); |
998 | return pc + 6; | |
c906108c SS |
999 | } |
1000 | else | |
1001 | { | |
acd5c798 | 1002 | /* Some instruction other than `subl'. */ |
37bdc87e | 1003 | return pc; |
c906108c SS |
1004 | } |
1005 | } | |
37bdc87e | 1006 | else if (op == 0xc8) /* enter */ |
c906108c | 1007 | { |
acd5c798 MK |
1008 | cache->locals = read_memory_unsigned_integer (pc + 1, 2); |
1009 | return pc + 4; | |
c906108c | 1010 | } |
21d0e8a4 | 1011 | |
acd5c798 | 1012 | return pc; |
21d0e8a4 MK |
1013 | } |
1014 | ||
acd5c798 MK |
1015 | /* Check whether PC points at code that saves registers on the stack. |
1016 | If so, it updates CACHE and returns the address of the first | |
1017 | instruction after the register saves or CURRENT_PC, whichever is | |
1018 | smaller. Otherwise, return PC. */ | |
6bff26de MK |
1019 | |
1020 | static CORE_ADDR | |
acd5c798 MK |
1021 | i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc, |
1022 | struct i386_frame_cache *cache) | |
6bff26de | 1023 | { |
99ab4326 | 1024 | CORE_ADDR offset = 0; |
63c0089f | 1025 | gdb_byte op; |
99ab4326 | 1026 | int i; |
c0d1d883 | 1027 | |
99ab4326 MK |
1028 | if (cache->locals > 0) |
1029 | offset -= cache->locals; | |
1030 | for (i = 0; i < 8 && pc < current_pc; i++) | |
1031 | { | |
8defab1a | 1032 | target_read_memory (pc, &op, 1); |
99ab4326 MK |
1033 | if (op < 0x50 || op > 0x57) |
1034 | break; | |
0d17c81d | 1035 | |
99ab4326 MK |
1036 | offset -= 4; |
1037 | cache->saved_regs[op - 0x50] = offset; | |
1038 | cache->sp_offset += 4; | |
1039 | pc++; | |
6bff26de MK |
1040 | } |
1041 | ||
acd5c798 | 1042 | return pc; |
22797942 AC |
1043 | } |
1044 | ||
acd5c798 MK |
1045 | /* Do a full analysis of the prologue at PC and update CACHE |
1046 | accordingly. Bail out early if CURRENT_PC is reached. Return the | |
1047 | address where the analysis stopped. | |
ed84f6c1 | 1048 | |
fc338970 MK |
1049 | We handle these cases: |
1050 | ||
1051 | The startup sequence can be at the start of the function, or the | |
1052 | function can start with a branch to startup code at the end. | |
1053 | ||
1054 | %ebp can be set up with either the 'enter' instruction, or "pushl | |
1055 | %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was | |
1056 | once used in the System V compiler). | |
1057 | ||
1058 | Local space is allocated just below the saved %ebp by either the | |
fd35795f MK |
1059 | 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a |
1060 | 16-bit unsigned argument for space to allocate, and the 'addl' | |
1061 | instruction could have either a signed byte, or 32-bit immediate. | |
fc338970 MK |
1062 | |
1063 | Next, the registers used by this function are pushed. With the | |
1064 | System V compiler they will always be in the order: %edi, %esi, | |
1065 | %ebx (and sometimes a harmless bug causes it to also save but not | |
1066 | restore %eax); however, the code below is willing to see the pushes | |
1067 | in any order, and will handle up to 8 of them. | |
1068 | ||
1069 | If the setup sequence is at the end of the function, then the next | |
1070 | instruction will be a branch back to the start. */ | |
c906108c | 1071 | |
acd5c798 MK |
1072 | static CORE_ADDR |
1073 | i386_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc, | |
1074 | struct i386_frame_cache *cache) | |
c906108c | 1075 | { |
e11481da | 1076 | pc = i386_skip_noop (pc); |
acd5c798 MK |
1077 | pc = i386_follow_jump (pc); |
1078 | pc = i386_analyze_struct_return (pc, current_pc, cache); | |
1079 | pc = i386_skip_probe (pc); | |
92dd43fa | 1080 | pc = i386_analyze_stack_align (pc, current_pc, cache); |
acd5c798 MK |
1081 | pc = i386_analyze_frame_setup (pc, current_pc, cache); |
1082 | return i386_analyze_register_saves (pc, current_pc, cache); | |
c906108c SS |
1083 | } |
1084 | ||
fc338970 | 1085 | /* Return PC of first real instruction. */ |
c906108c | 1086 | |
3a1e71e3 | 1087 | static CORE_ADDR |
6093d2eb | 1088 | i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc) |
c906108c | 1089 | { |
63c0089f | 1090 | static gdb_byte pic_pat[6] = |
acd5c798 MK |
1091 | { |
1092 | 0xe8, 0, 0, 0, 0, /* call 0x0 */ | |
1093 | 0x5b, /* popl %ebx */ | |
c5aa993b | 1094 | }; |
acd5c798 MK |
1095 | struct i386_frame_cache cache; |
1096 | CORE_ADDR pc; | |
63c0089f | 1097 | gdb_byte op; |
acd5c798 | 1098 | int i; |
c5aa993b | 1099 | |
acd5c798 MK |
1100 | cache.locals = -1; |
1101 | pc = i386_analyze_prologue (start_pc, 0xffffffff, &cache); | |
1102 | if (cache.locals < 0) | |
1103 | return start_pc; | |
c5aa993b | 1104 | |
acd5c798 | 1105 | /* Found valid frame setup. */ |
c906108c | 1106 | |
fc338970 MK |
1107 | /* The native cc on SVR4 in -K PIC mode inserts the following code |
1108 | to get the address of the global offset table (GOT) into register | |
acd5c798 MK |
1109 | %ebx: |
1110 | ||
fc338970 MK |
1111 | call 0x0 |
1112 | popl %ebx | |
1113 | movl %ebx,x(%ebp) (optional) | |
1114 | addl y,%ebx | |
1115 | ||
c906108c SS |
1116 | This code is with the rest of the prologue (at the end of the |
1117 | function), so we have to skip it to get to the first real | |
1118 | instruction at the start of the function. */ | |
c5aa993b | 1119 | |
c906108c SS |
1120 | for (i = 0; i < 6; i++) |
1121 | { | |
8defab1a | 1122 | target_read_memory (pc + i, &op, 1); |
c5aa993b | 1123 | if (pic_pat[i] != op) |
c906108c SS |
1124 | break; |
1125 | } | |
1126 | if (i == 6) | |
1127 | { | |
acd5c798 MK |
1128 | int delta = 6; |
1129 | ||
8defab1a | 1130 | target_read_memory (pc + delta, &op, 1); |
c906108c | 1131 | |
c5aa993b | 1132 | if (op == 0x89) /* movl %ebx, x(%ebp) */ |
c906108c | 1133 | { |
acd5c798 MK |
1134 | op = read_memory_unsigned_integer (pc + delta + 1, 1); |
1135 | ||
fc338970 | 1136 | if (op == 0x5d) /* One byte offset from %ebp. */ |
acd5c798 | 1137 | delta += 3; |
fc338970 | 1138 | else if (op == 0x9d) /* Four byte offset from %ebp. */ |
acd5c798 | 1139 | delta += 6; |
fc338970 | 1140 | else /* Unexpected instruction. */ |
acd5c798 MK |
1141 | delta = 0; |
1142 | ||
8defab1a | 1143 | target_read_memory (pc + delta, &op, 1); |
c906108c | 1144 | } |
acd5c798 | 1145 | |
c5aa993b | 1146 | /* addl y,%ebx */ |
acd5c798 | 1147 | if (delta > 0 && op == 0x81 |
d5d6fca5 | 1148 | && read_memory_unsigned_integer (pc + delta + 1, 1) == 0xc3) |
c906108c | 1149 | { |
acd5c798 | 1150 | pc += delta + 6; |
c906108c SS |
1151 | } |
1152 | } | |
c5aa993b | 1153 | |
e63bbc88 MK |
1154 | /* If the function starts with a branch (to startup code at the end) |
1155 | the last instruction should bring us back to the first | |
1156 | instruction of the real code. */ | |
1157 | if (i386_follow_jump (start_pc) != start_pc) | |
1158 | pc = i386_follow_jump (pc); | |
1159 | ||
1160 | return pc; | |
c906108c SS |
1161 | } |
1162 | ||
4309257c PM |
1163 | /* Check that the code pointed to by PC corresponds to a call to |
1164 | __main, skip it if so. Return PC otherwise. */ | |
1165 | ||
1166 | CORE_ADDR | |
1167 | i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc) | |
1168 | { | |
1169 | gdb_byte op; | |
1170 | ||
1171 | target_read_memory (pc, &op, 1); | |
1172 | if (op == 0xe8) | |
1173 | { | |
1174 | gdb_byte buf[4]; | |
1175 | ||
1176 | if (target_read_memory (pc + 1, buf, sizeof buf) == 0) | |
1177 | { | |
1178 | /* Make sure address is computed correctly as a 32bit | |
1179 | integer even if CORE_ADDR is 64 bit wide. */ | |
1180 | struct minimal_symbol *s; | |
1181 | CORE_ADDR call_dest = pc + 5 + extract_signed_integer (buf, 4); | |
1182 | ||
1183 | call_dest = call_dest & 0xffffffffU; | |
1184 | s = lookup_minimal_symbol_by_pc (call_dest); | |
1185 | if (s != NULL | |
1186 | && SYMBOL_LINKAGE_NAME (s) != NULL | |
1187 | && strcmp (SYMBOL_LINKAGE_NAME (s), "__main") == 0) | |
1188 | pc += 5; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | return pc; | |
1193 | } | |
1194 | ||
acd5c798 | 1195 | /* This function is 64-bit safe. */ |
93924b6b | 1196 | |
acd5c798 MK |
1197 | static CORE_ADDR |
1198 | i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame) | |
93924b6b | 1199 | { |
63c0089f | 1200 | gdb_byte buf[8]; |
acd5c798 | 1201 | |
875f8d0e | 1202 | frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf); |
acd5c798 | 1203 | return extract_typed_address (buf, builtin_type_void_func_ptr); |
93924b6b | 1204 | } |
acd5c798 | 1205 | \f |
93924b6b | 1206 | |
acd5c798 | 1207 | /* Normal frames. */ |
c5aa993b | 1208 | |
acd5c798 | 1209 | static struct i386_frame_cache * |
10458914 | 1210 | i386_frame_cache (struct frame_info *this_frame, void **this_cache) |
a7769679 | 1211 | { |
acd5c798 | 1212 | struct i386_frame_cache *cache; |
63c0089f | 1213 | gdb_byte buf[4]; |
acd5c798 MK |
1214 | int i; |
1215 | ||
1216 | if (*this_cache) | |
1217 | return *this_cache; | |
1218 | ||
fd13a04a | 1219 | cache = i386_alloc_frame_cache (); |
acd5c798 MK |
1220 | *this_cache = cache; |
1221 | ||
1222 | /* In principle, for normal frames, %ebp holds the frame pointer, | |
1223 | which holds the base address for the current stack frame. | |
1224 | However, for functions that don't need it, the frame pointer is | |
1225 | optional. For these "frameless" functions the frame pointer is | |
1226 | actually the frame pointer of the calling frame. Signal | |
1227 | trampolines are just a special case of a "frameless" function. | |
1228 | They (usually) share their frame pointer with the frame that was | |
1229 | in progress when the signal occurred. */ | |
1230 | ||
10458914 | 1231 | get_frame_register (this_frame, I386_EBP_REGNUM, buf); |
acd5c798 MK |
1232 | cache->base = extract_unsigned_integer (buf, 4); |
1233 | if (cache->base == 0) | |
1234 | return cache; | |
1235 | ||
1236 | /* For normal frames, %eip is stored at 4(%ebp). */ | |
fd13a04a | 1237 | cache->saved_regs[I386_EIP_REGNUM] = 4; |
acd5c798 | 1238 | |
10458914 | 1239 | cache->pc = get_frame_func (this_frame); |
acd5c798 | 1240 | if (cache->pc != 0) |
10458914 | 1241 | i386_analyze_prologue (cache->pc, get_frame_pc (this_frame), cache); |
acd5c798 | 1242 | |
92dd43fa MK |
1243 | if (cache->stack_align) |
1244 | { | |
1245 | /* Saved stack pointer has been saved in %ecx. */ | |
10458914 | 1246 | get_frame_register (this_frame, I386_ECX_REGNUM, buf); |
92dd43fa MK |
1247 | cache->saved_sp = extract_unsigned_integer(buf, 4); |
1248 | } | |
1249 | ||
acd5c798 MK |
1250 | if (cache->locals < 0) |
1251 | { | |
1252 | /* We didn't find a valid frame, which means that CACHE->base | |
1253 | currently holds the frame pointer for our calling frame. If | |
1254 | we're at the start of a function, or somewhere half-way its | |
1255 | prologue, the function's frame probably hasn't been fully | |
1256 | setup yet. Try to reconstruct the base address for the stack | |
1257 | frame by looking at the stack pointer. For truly "frameless" | |
1258 | functions this might work too. */ | |
1259 | ||
92dd43fa MK |
1260 | if (cache->stack_align) |
1261 | { | |
1262 | /* We're halfway aligning the stack. */ | |
1263 | cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4; | |
1264 | cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4; | |
1265 | ||
1266 | /* This will be added back below. */ | |
1267 | cache->saved_regs[I386_EIP_REGNUM] -= cache->base; | |
1268 | } | |
1269 | else | |
1270 | { | |
10458914 | 1271 | get_frame_register (this_frame, I386_ESP_REGNUM, buf); |
92dd43fa MK |
1272 | cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset; |
1273 | } | |
acd5c798 MK |
1274 | } |
1275 | ||
1276 | /* Now that we have the base address for the stack frame we can | |
1277 | calculate the value of %esp in the calling frame. */ | |
92dd43fa MK |
1278 | if (cache->saved_sp == 0) |
1279 | cache->saved_sp = cache->base + 8; | |
a7769679 | 1280 | |
acd5c798 MK |
1281 | /* Adjust all the saved registers such that they contain addresses |
1282 | instead of offsets. */ | |
1283 | for (i = 0; i < I386_NUM_SAVED_REGS; i++) | |
fd13a04a AC |
1284 | if (cache->saved_regs[i] != -1) |
1285 | cache->saved_regs[i] += cache->base; | |
acd5c798 MK |
1286 | |
1287 | return cache; | |
a7769679 MK |
1288 | } |
1289 | ||
3a1e71e3 | 1290 | static void |
10458914 | 1291 | i386_frame_this_id (struct frame_info *this_frame, void **this_cache, |
acd5c798 | 1292 | struct frame_id *this_id) |
c906108c | 1293 | { |
10458914 | 1294 | struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache); |
acd5c798 MK |
1295 | |
1296 | /* This marks the outermost frame. */ | |
1297 | if (cache->base == 0) | |
1298 | return; | |
1299 | ||
3e210248 | 1300 | /* See the end of i386_push_dummy_call. */ |
acd5c798 MK |
1301 | (*this_id) = frame_id_build (cache->base + 8, cache->pc); |
1302 | } | |
1303 | ||
10458914 DJ |
1304 | static struct value * |
1305 | i386_frame_prev_register (struct frame_info *this_frame, void **this_cache, | |
1306 | int regnum) | |
acd5c798 | 1307 | { |
10458914 | 1308 | struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache); |
acd5c798 MK |
1309 | |
1310 | gdb_assert (regnum >= 0); | |
1311 | ||
1312 | /* The System V ABI says that: | |
1313 | ||
1314 | "The flags register contains the system flags, such as the | |
1315 | direction flag and the carry flag. The direction flag must be | |
1316 | set to the forward (that is, zero) direction before entry and | |
1317 | upon exit from a function. Other user flags have no specified | |
1318 | role in the standard calling sequence and are not preserved." | |
1319 | ||
1320 | To guarantee the "upon exit" part of that statement we fake a | |
1321 | saved flags register that has its direction flag cleared. | |
1322 | ||
1323 | Note that GCC doesn't seem to rely on the fact that the direction | |
1324 | flag is cleared after a function return; it always explicitly | |
1325 | clears the flag before operations where it matters. | |
1326 | ||
1327 | FIXME: kettenis/20030316: I'm not quite sure whether this is the | |
1328 | right thing to do. The way we fake the flags register here makes | |
1329 | it impossible to change it. */ | |
1330 | ||
1331 | if (regnum == I386_EFLAGS_REGNUM) | |
1332 | { | |
10458914 | 1333 | ULONGEST val; |
c5aa993b | 1334 | |
10458914 DJ |
1335 | val = get_frame_register_unsigned (this_frame, regnum); |
1336 | val &= ~(1 << 10); | |
1337 | return frame_unwind_got_constant (this_frame, regnum, val); | |
acd5c798 | 1338 | } |
1211c4e4 | 1339 | |
acd5c798 | 1340 | if (regnum == I386_EIP_REGNUM && cache->pc_in_eax) |
10458914 | 1341 | return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM); |
acd5c798 MK |
1342 | |
1343 | if (regnum == I386_ESP_REGNUM && cache->saved_sp) | |
10458914 | 1344 | return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp); |
acd5c798 | 1345 | |
fd13a04a | 1346 | if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1) |
10458914 DJ |
1347 | return frame_unwind_got_memory (this_frame, regnum, |
1348 | cache->saved_regs[regnum]); | |
fd13a04a | 1349 | |
10458914 | 1350 | return frame_unwind_got_register (this_frame, regnum, regnum); |
acd5c798 MK |
1351 | } |
1352 | ||
1353 | static const struct frame_unwind i386_frame_unwind = | |
1354 | { | |
1355 | NORMAL_FRAME, | |
1356 | i386_frame_this_id, | |
10458914 DJ |
1357 | i386_frame_prev_register, |
1358 | NULL, | |
1359 | default_frame_sniffer | |
acd5c798 | 1360 | }; |
acd5c798 MK |
1361 | \f |
1362 | ||
1363 | /* Signal trampolines. */ | |
1364 | ||
1365 | static struct i386_frame_cache * | |
10458914 | 1366 | i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache) |
acd5c798 MK |
1367 | { |
1368 | struct i386_frame_cache *cache; | |
10458914 | 1369 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame)); |
acd5c798 | 1370 | CORE_ADDR addr; |
63c0089f | 1371 | gdb_byte buf[4]; |
acd5c798 MK |
1372 | |
1373 | if (*this_cache) | |
1374 | return *this_cache; | |
1375 | ||
fd13a04a | 1376 | cache = i386_alloc_frame_cache (); |
acd5c798 | 1377 | |
10458914 | 1378 | get_frame_register (this_frame, I386_ESP_REGNUM, buf); |
acd5c798 MK |
1379 | cache->base = extract_unsigned_integer (buf, 4) - 4; |
1380 | ||
10458914 | 1381 | addr = tdep->sigcontext_addr (this_frame); |
a3386186 MK |
1382 | if (tdep->sc_reg_offset) |
1383 | { | |
1384 | int i; | |
1385 | ||
1386 | gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS); | |
1387 | ||
1388 | for (i = 0; i < tdep->sc_num_regs; i++) | |
1389 | if (tdep->sc_reg_offset[i] != -1) | |
fd13a04a | 1390 | cache->saved_regs[i] = addr + tdep->sc_reg_offset[i]; |
a3386186 MK |
1391 | } |
1392 | else | |
1393 | { | |
fd13a04a AC |
1394 | cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset; |
1395 | cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset; | |
a3386186 | 1396 | } |
acd5c798 MK |
1397 | |
1398 | *this_cache = cache; | |
1399 | return cache; | |
1400 | } | |
1401 | ||
1402 | static void | |
10458914 | 1403 | i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache, |
acd5c798 MK |
1404 | struct frame_id *this_id) |
1405 | { | |
1406 | struct i386_frame_cache *cache = | |
10458914 | 1407 | i386_sigtramp_frame_cache (this_frame, this_cache); |
acd5c798 | 1408 | |
3e210248 | 1409 | /* See the end of i386_push_dummy_call. */ |
10458914 | 1410 | (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame)); |
acd5c798 MK |
1411 | } |
1412 | ||
10458914 DJ |
1413 | static struct value * |
1414 | i386_sigtramp_frame_prev_register (struct frame_info *this_frame, | |
1415 | void **this_cache, int regnum) | |
acd5c798 MK |
1416 | { |
1417 | /* Make sure we've initialized the cache. */ | |
10458914 | 1418 | i386_sigtramp_frame_cache (this_frame, this_cache); |
acd5c798 | 1419 | |
10458914 | 1420 | return i386_frame_prev_register (this_frame, this_cache, regnum); |
c906108c | 1421 | } |
c0d1d883 | 1422 | |
10458914 DJ |
1423 | static int |
1424 | i386_sigtramp_frame_sniffer (const struct frame_unwind *self, | |
1425 | struct frame_info *this_frame, | |
1426 | void **this_prologue_cache) | |
acd5c798 | 1427 | { |
10458914 | 1428 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame)); |
acd5c798 | 1429 | |
911bc6ee MK |
1430 | /* We shouldn't even bother if we don't have a sigcontext_addr |
1431 | handler. */ | |
1432 | if (tdep->sigcontext_addr == NULL) | |
10458914 | 1433 | return 0; |
1c3545ae | 1434 | |
911bc6ee MK |
1435 | if (tdep->sigtramp_p != NULL) |
1436 | { | |
10458914 DJ |
1437 | if (tdep->sigtramp_p (this_frame)) |
1438 | return 1; | |
911bc6ee MK |
1439 | } |
1440 | ||
1441 | if (tdep->sigtramp_start != 0) | |
1442 | { | |
10458914 | 1443 | CORE_ADDR pc = get_frame_pc (this_frame); |
911bc6ee MK |
1444 | |
1445 | gdb_assert (tdep->sigtramp_end != 0); | |
1446 | if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end) | |
10458914 | 1447 | return 1; |
911bc6ee | 1448 | } |
acd5c798 | 1449 | |
10458914 | 1450 | return 0; |
acd5c798 | 1451 | } |
10458914 DJ |
1452 | |
1453 | static const struct frame_unwind i386_sigtramp_frame_unwind = | |
1454 | { | |
1455 | SIGTRAMP_FRAME, | |
1456 | i386_sigtramp_frame_this_id, | |
1457 | i386_sigtramp_frame_prev_register, | |
1458 | NULL, | |
1459 | i386_sigtramp_frame_sniffer | |
1460 | }; | |
acd5c798 MK |
1461 | \f |
1462 | ||
1463 | static CORE_ADDR | |
10458914 | 1464 | i386_frame_base_address (struct frame_info *this_frame, void **this_cache) |
acd5c798 | 1465 | { |
10458914 | 1466 | struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache); |
acd5c798 MK |
1467 | |
1468 | return cache->base; | |
1469 | } | |
1470 | ||
1471 | static const struct frame_base i386_frame_base = | |
1472 | { | |
1473 | &i386_frame_unwind, | |
1474 | i386_frame_base_address, | |
1475 | i386_frame_base_address, | |
1476 | i386_frame_base_address | |
1477 | }; | |
1478 | ||
acd5c798 | 1479 | static struct frame_id |
10458914 | 1480 | i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame) |
acd5c798 | 1481 | { |
acd5c798 MK |
1482 | CORE_ADDR fp; |
1483 | ||
10458914 | 1484 | fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM); |
acd5c798 | 1485 | |
3e210248 | 1486 | /* See the end of i386_push_dummy_call. */ |
10458914 | 1487 | return frame_id_build (fp + 8, get_frame_pc (this_frame)); |
c0d1d883 | 1488 | } |
fc338970 | 1489 | \f |
c906108c | 1490 | |
fc338970 MK |
1491 | /* Figure out where the longjmp will land. Slurp the args out of the |
1492 | stack. We expect the first arg to be a pointer to the jmp_buf | |
8201327c | 1493 | structure from which we extract the address that we will land at. |
28bcfd30 | 1494 | This address is copied into PC. This routine returns non-zero on |
436675d3 | 1495 | success. */ |
c906108c | 1496 | |
8201327c | 1497 | static int |
60ade65d | 1498 | i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc) |
c906108c | 1499 | { |
436675d3 | 1500 | gdb_byte buf[4]; |
c906108c | 1501 | CORE_ADDR sp, jb_addr; |
20a6ec49 MD |
1502 | struct gdbarch *gdbarch = get_frame_arch (frame); |
1503 | int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset; | |
c906108c | 1504 | |
8201327c MK |
1505 | /* If JB_PC_OFFSET is -1, we have no way to find out where the |
1506 | longjmp will land. */ | |
1507 | if (jb_pc_offset == -1) | |
c906108c SS |
1508 | return 0; |
1509 | ||
436675d3 PA |
1510 | get_frame_register (frame, I386_ESP_REGNUM, buf); |
1511 | sp = extract_unsigned_integer (buf, 4); | |
1512 | if (target_read_memory (sp + 4, buf, 4)) | |
c906108c SS |
1513 | return 0; |
1514 | ||
436675d3 PA |
1515 | jb_addr = extract_unsigned_integer (buf, 4); |
1516 | if (target_read_memory (jb_addr + jb_pc_offset, buf, 4)) | |
8201327c | 1517 | return 0; |
c906108c | 1518 | |
436675d3 | 1519 | *pc = extract_unsigned_integer (buf, 4); |
c906108c SS |
1520 | return 1; |
1521 | } | |
fc338970 | 1522 | \f |
c906108c | 1523 | |
7ccc1c74 JM |
1524 | /* Check whether TYPE must be 16-byte-aligned when passed as a |
1525 | function argument. 16-byte vectors, _Decimal128 and structures or | |
1526 | unions containing such types must be 16-byte-aligned; other | |
1527 | arguments are 4-byte-aligned. */ | |
1528 | ||
1529 | static int | |
1530 | i386_16_byte_align_p (struct type *type) | |
1531 | { | |
1532 | type = check_typedef (type); | |
1533 | if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT | |
1534 | || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type))) | |
1535 | && TYPE_LENGTH (type) == 16) | |
1536 | return 1; | |
1537 | if (TYPE_CODE (type) == TYPE_CODE_ARRAY) | |
1538 | return i386_16_byte_align_p (TYPE_TARGET_TYPE (type)); | |
1539 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
1540 | || TYPE_CODE (type) == TYPE_CODE_UNION) | |
1541 | { | |
1542 | int i; | |
1543 | for (i = 0; i < TYPE_NFIELDS (type); i++) | |
1544 | { | |
1545 | if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i))) | |
1546 | return 1; | |
1547 | } | |
1548 | } | |
1549 | return 0; | |
1550 | } | |
1551 | ||
3a1e71e3 | 1552 | static CORE_ADDR |
7d9b040b | 1553 | i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function, |
6a65450a AC |
1554 | struct regcache *regcache, CORE_ADDR bp_addr, int nargs, |
1555 | struct value **args, CORE_ADDR sp, int struct_return, | |
1556 | CORE_ADDR struct_addr) | |
22f8ba57 | 1557 | { |
63c0089f | 1558 | gdb_byte buf[4]; |
acd5c798 | 1559 | int i; |
7ccc1c74 JM |
1560 | int write_pass; |
1561 | int args_space = 0; | |
acd5c798 | 1562 | |
7ccc1c74 JM |
1563 | /* Determine the total space required for arguments and struct |
1564 | return address in a first pass (allowing for 16-byte-aligned | |
1565 | arguments), then push arguments in a second pass. */ | |
1566 | ||
1567 | for (write_pass = 0; write_pass < 2; write_pass++) | |
22f8ba57 | 1568 | { |
7ccc1c74 JM |
1569 | int args_space_used = 0; |
1570 | int have_16_byte_aligned_arg = 0; | |
1571 | ||
1572 | if (struct_return) | |
1573 | { | |
1574 | if (write_pass) | |
1575 | { | |
1576 | /* Push value address. */ | |
1577 | store_unsigned_integer (buf, 4, struct_addr); | |
1578 | write_memory (sp, buf, 4); | |
1579 | args_space_used += 4; | |
1580 | } | |
1581 | else | |
1582 | args_space += 4; | |
1583 | } | |
1584 | ||
1585 | for (i = 0; i < nargs; i++) | |
1586 | { | |
1587 | int len = TYPE_LENGTH (value_enclosing_type (args[i])); | |
acd5c798 | 1588 | |
7ccc1c74 JM |
1589 | if (write_pass) |
1590 | { | |
1591 | if (i386_16_byte_align_p (value_enclosing_type (args[i]))) | |
1592 | args_space_used = align_up (args_space_used, 16); | |
acd5c798 | 1593 | |
7ccc1c74 JM |
1594 | write_memory (sp + args_space_used, |
1595 | value_contents_all (args[i]), len); | |
1596 | /* The System V ABI says that: | |
acd5c798 | 1597 | |
7ccc1c74 JM |
1598 | "An argument's size is increased, if necessary, to make it a |
1599 | multiple of [32-bit] words. This may require tail padding, | |
1600 | depending on the size of the argument." | |
22f8ba57 | 1601 | |
7ccc1c74 JM |
1602 | This makes sure the stack stays word-aligned. */ |
1603 | args_space_used += align_up (len, 4); | |
1604 | } | |
1605 | else | |
1606 | { | |
1607 | if (i386_16_byte_align_p (value_enclosing_type (args[i]))) | |
1608 | { | |
1609 | args_space = align_up (args_space, 16); | |
1610 | have_16_byte_aligned_arg = 1; | |
1611 | } | |
1612 | args_space += align_up (len, 4); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | if (!write_pass) | |
1617 | { | |
1618 | if (have_16_byte_aligned_arg) | |
1619 | args_space = align_up (args_space, 16); | |
1620 | sp -= args_space; | |
1621 | } | |
22f8ba57 MK |
1622 | } |
1623 | ||
acd5c798 MK |
1624 | /* Store return address. */ |
1625 | sp -= 4; | |
6a65450a | 1626 | store_unsigned_integer (buf, 4, bp_addr); |
acd5c798 MK |
1627 | write_memory (sp, buf, 4); |
1628 | ||
1629 | /* Finally, update the stack pointer... */ | |
1630 | store_unsigned_integer (buf, 4, sp); | |
1631 | regcache_cooked_write (regcache, I386_ESP_REGNUM, buf); | |
1632 | ||
1633 | /* ...and fake a frame pointer. */ | |
1634 | regcache_cooked_write (regcache, I386_EBP_REGNUM, buf); | |
1635 | ||
3e210248 AC |
1636 | /* MarkK wrote: This "+ 8" is all over the place: |
1637 | (i386_frame_this_id, i386_sigtramp_frame_this_id, | |
10458914 | 1638 | i386_dummy_id). It's there, since all frame unwinders for |
3e210248 | 1639 | a given target have to agree (within a certain margin) on the |
fd35795f | 1640 | definition of the stack address of a frame. Otherwise |
3e210248 AC |
1641 | frame_id_inner() won't work correctly. Since DWARF2/GCC uses the |
1642 | stack address *before* the function call as a frame's CFA. On | |
1643 | the i386, when %ebp is used as a frame pointer, the offset | |
1644 | between the contents %ebp and the CFA as defined by GCC. */ | |
1645 | return sp + 8; | |
22f8ba57 MK |
1646 | } |
1647 | ||
1a309862 MK |
1648 | /* These registers are used for returning integers (and on some |
1649 | targets also for returning `struct' and `union' values when their | |
ef9dff19 | 1650 | size and alignment match an integer type). */ |
acd5c798 MK |
1651 | #define LOW_RETURN_REGNUM I386_EAX_REGNUM /* %eax */ |
1652 | #define HIGH_RETURN_REGNUM I386_EDX_REGNUM /* %edx */ | |
1a309862 | 1653 | |
c5e656c1 MK |
1654 | /* Read, for architecture GDBARCH, a function return value of TYPE |
1655 | from REGCACHE, and copy that into VALBUF. */ | |
1a309862 | 1656 | |
3a1e71e3 | 1657 | static void |
c5e656c1 | 1658 | i386_extract_return_value (struct gdbarch *gdbarch, struct type *type, |
63c0089f | 1659 | struct regcache *regcache, gdb_byte *valbuf) |
c906108c | 1660 | { |
c5e656c1 | 1661 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
1a309862 | 1662 | int len = TYPE_LENGTH (type); |
63c0089f | 1663 | gdb_byte buf[I386_MAX_REGISTER_SIZE]; |
1a309862 | 1664 | |
1e8d0a7b | 1665 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
c906108c | 1666 | { |
5716833c | 1667 | if (tdep->st0_regnum < 0) |
1a309862 | 1668 | { |
8a3fe4f8 | 1669 | warning (_("Cannot find floating-point return value.")); |
1a309862 | 1670 | memset (valbuf, 0, len); |
ef9dff19 | 1671 | return; |
1a309862 MK |
1672 | } |
1673 | ||
c6ba6f0d MK |
1674 | /* Floating-point return values can be found in %st(0). Convert |
1675 | its contents to the desired type. This is probably not | |
1676 | exactly how it would happen on the target itself, but it is | |
1677 | the best we can do. */ | |
acd5c798 | 1678 | regcache_raw_read (regcache, I386_ST0_REGNUM, buf); |
00f8375e | 1679 | convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type); |
c906108c SS |
1680 | } |
1681 | else | |
c5aa993b | 1682 | { |
875f8d0e UW |
1683 | int low_size = register_size (gdbarch, LOW_RETURN_REGNUM); |
1684 | int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM); | |
d4f3574e SS |
1685 | |
1686 | if (len <= low_size) | |
00f8375e | 1687 | { |
0818c12a | 1688 | regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf); |
00f8375e MK |
1689 | memcpy (valbuf, buf, len); |
1690 | } | |
d4f3574e SS |
1691 | else if (len <= (low_size + high_size)) |
1692 | { | |
0818c12a | 1693 | regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf); |
00f8375e | 1694 | memcpy (valbuf, buf, low_size); |
0818c12a | 1695 | regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf); |
63c0089f | 1696 | memcpy (valbuf + low_size, buf, len - low_size); |
d4f3574e SS |
1697 | } |
1698 | else | |
8e65ff28 | 1699 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1700 | _("Cannot extract return value of %d bytes long."), len); |
c906108c SS |
1701 | } |
1702 | } | |
1703 | ||
c5e656c1 MK |
1704 | /* Write, for architecture GDBARCH, a function return value of TYPE |
1705 | from VALBUF into REGCACHE. */ | |
ef9dff19 | 1706 | |
3a1e71e3 | 1707 | static void |
c5e656c1 | 1708 | i386_store_return_value (struct gdbarch *gdbarch, struct type *type, |
63c0089f | 1709 | struct regcache *regcache, const gdb_byte *valbuf) |
ef9dff19 | 1710 | { |
c5e656c1 | 1711 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
ef9dff19 MK |
1712 | int len = TYPE_LENGTH (type); |
1713 | ||
1e8d0a7b | 1714 | if (TYPE_CODE (type) == TYPE_CODE_FLT) |
ef9dff19 | 1715 | { |
3d7f4f49 | 1716 | ULONGEST fstat; |
63c0089f | 1717 | gdb_byte buf[I386_MAX_REGISTER_SIZE]; |
ccb945b8 | 1718 | |
5716833c | 1719 | if (tdep->st0_regnum < 0) |
ef9dff19 | 1720 | { |
8a3fe4f8 | 1721 | warning (_("Cannot set floating-point return value.")); |
ef9dff19 MK |
1722 | return; |
1723 | } | |
1724 | ||
635b0cc1 MK |
1725 | /* Returning floating-point values is a bit tricky. Apart from |
1726 | storing the return value in %st(0), we have to simulate the | |
1727 | state of the FPU at function return point. */ | |
1728 | ||
c6ba6f0d MK |
1729 | /* Convert the value found in VALBUF to the extended |
1730 | floating-point format used by the FPU. This is probably | |
1731 | not exactly how it would happen on the target itself, but | |
1732 | it is the best we can do. */ | |
1733 | convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext); | |
acd5c798 | 1734 | regcache_raw_write (regcache, I386_ST0_REGNUM, buf); |
ccb945b8 | 1735 | |
635b0cc1 MK |
1736 | /* Set the top of the floating-point register stack to 7. The |
1737 | actual value doesn't really matter, but 7 is what a normal | |
1738 | function return would end up with if the program started out | |
1739 | with a freshly initialized FPU. */ | |
20a6ec49 | 1740 | regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat); |
ccb945b8 | 1741 | fstat |= (7 << 11); |
20a6ec49 | 1742 | regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat); |
ccb945b8 | 1743 | |
635b0cc1 MK |
1744 | /* Mark %st(1) through %st(7) as empty. Since we set the top of |
1745 | the floating-point register stack to 7, the appropriate value | |
1746 | for the tag word is 0x3fff. */ | |
20a6ec49 | 1747 | regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff); |
ef9dff19 MK |
1748 | } |
1749 | else | |
1750 | { | |
875f8d0e UW |
1751 | int low_size = register_size (gdbarch, LOW_RETURN_REGNUM); |
1752 | int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM); | |
ef9dff19 MK |
1753 | |
1754 | if (len <= low_size) | |
3d7f4f49 | 1755 | regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf); |
ef9dff19 MK |
1756 | else if (len <= (low_size + high_size)) |
1757 | { | |
3d7f4f49 MK |
1758 | regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf); |
1759 | regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0, | |
63c0089f | 1760 | len - low_size, valbuf + low_size); |
ef9dff19 MK |
1761 | } |
1762 | else | |
8e65ff28 | 1763 | internal_error (__FILE__, __LINE__, |
e2e0b3e5 | 1764 | _("Cannot store return value of %d bytes long."), len); |
ef9dff19 MK |
1765 | } |
1766 | } | |
fc338970 | 1767 | \f |
ef9dff19 | 1768 | |
8201327c MK |
1769 | /* This is the variable that is set with "set struct-convention", and |
1770 | its legitimate values. */ | |
1771 | static const char default_struct_convention[] = "default"; | |
1772 | static const char pcc_struct_convention[] = "pcc"; | |
1773 | static const char reg_struct_convention[] = "reg"; | |
1774 | static const char *valid_conventions[] = | |
1775 | { | |
1776 | default_struct_convention, | |
1777 | pcc_struct_convention, | |
1778 | reg_struct_convention, | |
1779 | NULL | |
1780 | }; | |
1781 | static const char *struct_convention = default_struct_convention; | |
1782 | ||
0e4377e1 JB |
1783 | /* Return non-zero if TYPE, which is assumed to be a structure, |
1784 | a union type, or an array type, should be returned in registers | |
1785 | for architecture GDBARCH. */ | |
c5e656c1 | 1786 | |
8201327c | 1787 | static int |
c5e656c1 | 1788 | i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type) |
8201327c | 1789 | { |
c5e656c1 MK |
1790 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
1791 | enum type_code code = TYPE_CODE (type); | |
1792 | int len = TYPE_LENGTH (type); | |
8201327c | 1793 | |
0e4377e1 JB |
1794 | gdb_assert (code == TYPE_CODE_STRUCT |
1795 | || code == TYPE_CODE_UNION | |
1796 | || code == TYPE_CODE_ARRAY); | |
c5e656c1 MK |
1797 | |
1798 | if (struct_convention == pcc_struct_convention | |
1799 | || (struct_convention == default_struct_convention | |
1800 | && tdep->struct_return == pcc_struct_return)) | |
1801 | return 0; | |
1802 | ||
9edde48e MK |
1803 | /* Structures consisting of a single `float', `double' or 'long |
1804 | double' member are returned in %st(0). */ | |
1805 | if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1) | |
1806 | { | |
1807 | type = check_typedef (TYPE_FIELD_TYPE (type, 0)); | |
1808 | if (TYPE_CODE (type) == TYPE_CODE_FLT) | |
1809 | return (len == 4 || len == 8 || len == 12); | |
1810 | } | |
1811 | ||
c5e656c1 MK |
1812 | return (len == 1 || len == 2 || len == 4 || len == 8); |
1813 | } | |
1814 | ||
1815 | /* Determine, for architecture GDBARCH, how a return value of TYPE | |
1816 | should be returned. If it is supposed to be returned in registers, | |
1817 | and READBUF is non-zero, read the appropriate value from REGCACHE, | |
1818 | and copy it into READBUF. If WRITEBUF is non-zero, write the value | |
1819 | from WRITEBUF into REGCACHE. */ | |
1820 | ||
1821 | static enum return_value_convention | |
c055b101 CV |
1822 | i386_return_value (struct gdbarch *gdbarch, struct type *func_type, |
1823 | struct type *type, struct regcache *regcache, | |
1824 | gdb_byte *readbuf, const gdb_byte *writebuf) | |
c5e656c1 MK |
1825 | { |
1826 | enum type_code code = TYPE_CODE (type); | |
1827 | ||
5daa78cc TJB |
1828 | if (((code == TYPE_CODE_STRUCT |
1829 | || code == TYPE_CODE_UNION | |
1830 | || code == TYPE_CODE_ARRAY) | |
1831 | && !i386_reg_struct_return_p (gdbarch, type)) | |
1832 | /* 128-bit decimal float uses the struct return convention. */ | |
1833 | || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16)) | |
31db7b6c MK |
1834 | { |
1835 | /* The System V ABI says that: | |
1836 | ||
1837 | "A function that returns a structure or union also sets %eax | |
1838 | to the value of the original address of the caller's area | |
1839 | before it returns. Thus when the caller receives control | |
1840 | again, the address of the returned object resides in register | |
1841 | %eax and can be used to access the object." | |
1842 | ||
1843 | So the ABI guarantees that we can always find the return | |
1844 | value just after the function has returned. */ | |
1845 | ||
0e4377e1 JB |
1846 | /* Note that the ABI doesn't mention functions returning arrays, |
1847 | which is something possible in certain languages such as Ada. | |
1848 | In this case, the value is returned as if it was wrapped in | |
1849 | a record, so the convention applied to records also applies | |
1850 | to arrays. */ | |
1851 | ||
31db7b6c MK |
1852 | if (readbuf) |
1853 | { | |
1854 | ULONGEST addr; | |
1855 | ||
1856 | regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr); | |
1857 | read_memory (addr, readbuf, TYPE_LENGTH (type)); | |
1858 | } | |
1859 | ||
1860 | return RETURN_VALUE_ABI_RETURNS_ADDRESS; | |
1861 | } | |
c5e656c1 MK |
1862 | |
1863 | /* This special case is for structures consisting of a single | |
9edde48e MK |
1864 | `float', `double' or 'long double' member. These structures are |
1865 | returned in %st(0). For these structures, we call ourselves | |
1866 | recursively, changing TYPE into the type of the first member of | |
1867 | the structure. Since that should work for all structures that | |
1868 | have only one member, we don't bother to check the member's type | |
1869 | here. */ | |
c5e656c1 MK |
1870 | if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1) |
1871 | { | |
1872 | type = check_typedef (TYPE_FIELD_TYPE (type, 0)); | |
c055b101 CV |
1873 | return i386_return_value (gdbarch, func_type, type, regcache, |
1874 | readbuf, writebuf); | |
c5e656c1 MK |
1875 | } |
1876 | ||
1877 | if (readbuf) | |
1878 | i386_extract_return_value (gdbarch, type, regcache, readbuf); | |
1879 | if (writebuf) | |
1880 | i386_store_return_value (gdbarch, type, regcache, writebuf); | |
8201327c | 1881 | |
c5e656c1 | 1882 | return RETURN_VALUE_REGISTER_CONVENTION; |
8201327c MK |
1883 | } |
1884 | \f | |
1885 | ||
5ae96ec1 MK |
1886 | /* Type for %eflags. */ |
1887 | struct type *i386_eflags_type; | |
1888 | ||
794ac428 | 1889 | /* Type for %mxcsr. */ |
878d9193 | 1890 | struct type *i386_mxcsr_type; |
5ae96ec1 MK |
1891 | |
1892 | /* Construct types for ISA-specific registers. */ | |
1893 | static void | |
1894 | i386_init_types (void) | |
1895 | { | |
1896 | struct type *type; | |
1897 | ||
1898 | type = init_flags_type ("builtin_type_i386_eflags", 4); | |
1899 | append_flags_type_flag (type, 0, "CF"); | |
1900 | append_flags_type_flag (type, 1, NULL); | |
1901 | append_flags_type_flag (type, 2, "PF"); | |
1902 | append_flags_type_flag (type, 4, "AF"); | |
1903 | append_flags_type_flag (type, 6, "ZF"); | |
1904 | append_flags_type_flag (type, 7, "SF"); | |
1905 | append_flags_type_flag (type, 8, "TF"); | |
1906 | append_flags_type_flag (type, 9, "IF"); | |
1907 | append_flags_type_flag (type, 10, "DF"); | |
1908 | append_flags_type_flag (type, 11, "OF"); | |
1909 | append_flags_type_flag (type, 14, "NT"); | |
1910 | append_flags_type_flag (type, 16, "RF"); | |
1911 | append_flags_type_flag (type, 17, "VM"); | |
1912 | append_flags_type_flag (type, 18, "AC"); | |
1913 | append_flags_type_flag (type, 19, "VIF"); | |
1914 | append_flags_type_flag (type, 20, "VIP"); | |
1915 | append_flags_type_flag (type, 21, "ID"); | |
1916 | i386_eflags_type = type; | |
21b4b2f2 | 1917 | |
878d9193 MK |
1918 | type = init_flags_type ("builtin_type_i386_mxcsr", 4); |
1919 | append_flags_type_flag (type, 0, "IE"); | |
1920 | append_flags_type_flag (type, 1, "DE"); | |
1921 | append_flags_type_flag (type, 2, "ZE"); | |
1922 | append_flags_type_flag (type, 3, "OE"); | |
1923 | append_flags_type_flag (type, 4, "UE"); | |
1924 | append_flags_type_flag (type, 5, "PE"); | |
1925 | append_flags_type_flag (type, 6, "DAZ"); | |
1926 | append_flags_type_flag (type, 7, "IM"); | |
1927 | append_flags_type_flag (type, 8, "DM"); | |
1928 | append_flags_type_flag (type, 9, "ZM"); | |
1929 | append_flags_type_flag (type, 10, "OM"); | |
1930 | append_flags_type_flag (type, 11, "UM"); | |
1931 | append_flags_type_flag (type, 12, "PM"); | |
1932 | append_flags_type_flag (type, 15, "FZ"); | |
1933 | i386_mxcsr_type = type; | |
21b4b2f2 JB |
1934 | } |
1935 | ||
794ac428 UW |
1936 | /* Construct vector type for MMX registers. */ |
1937 | struct type * | |
1938 | i386_mmx_type (struct gdbarch *gdbarch) | |
1939 | { | |
1940 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
1941 | ||
1942 | if (!tdep->i386_mmx_type) | |
1943 | { | |
1944 | /* The type we're building is this: */ | |
1945 | #if 0 | |
1946 | union __gdb_builtin_type_vec64i | |
1947 | { | |
1948 | int64_t uint64; | |
1949 | int32_t v2_int32[2]; | |
1950 | int16_t v4_int16[4]; | |
1951 | int8_t v8_int8[8]; | |
1952 | }; | |
1953 | #endif | |
1954 | ||
1955 | struct type *t; | |
1956 | ||
1957 | t = init_composite_type ("__gdb_builtin_type_vec64i", TYPE_CODE_UNION); | |
1958 | append_composite_type_field (t, "uint64", builtin_type_int64); | |
1959 | append_composite_type_field (t, "v2_int32", | |
1960 | init_vector_type (builtin_type_int32, 2)); | |
1961 | append_composite_type_field (t, "v4_int16", | |
1962 | init_vector_type (builtin_type_int16, 4)); | |
1963 | append_composite_type_field (t, "v8_int8", | |
1964 | init_vector_type (builtin_type_int8, 8)); | |
1965 | ||
1966 | TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR; | |
1967 | TYPE_NAME (t) = "builtin_type_vec64i"; | |
1968 | tdep->i386_mmx_type = t; | |
1969 | } | |
1970 | ||
1971 | return tdep->i386_mmx_type; | |
1972 | } | |
1973 | ||
1974 | struct type * | |
1975 | i386_sse_type (struct gdbarch *gdbarch) | |
1976 | { | |
1977 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
1978 | ||
1979 | if (!tdep->i386_sse_type) | |
1980 | { | |
1981 | /* The type we're building is this: */ | |
1982 | #if 0 | |
1983 | union __gdb_builtin_type_vec128i | |
1984 | { | |
1985 | int128_t uint128; | |
1986 | int64_t v2_int64[2]; | |
1987 | int32_t v4_int32[4]; | |
1988 | int16_t v8_int16[8]; | |
1989 | int8_t v16_int8[16]; | |
1990 | double v2_double[2]; | |
1991 | float v4_float[4]; | |
1992 | }; | |
1993 | #endif | |
1994 | ||
1995 | struct type *t; | |
1996 | ||
1997 | t = init_composite_type ("__gdb_builtin_type_vec128i", TYPE_CODE_UNION); | |
1998 | append_composite_type_field (t, "v4_float", | |
1999 | init_vector_type (builtin_type_float, 4)); | |
2000 | append_composite_type_field (t, "v2_double", | |
2001 | init_vector_type (builtin_type_double, 2)); | |
2002 | append_composite_type_field (t, "v16_int8", | |
2003 | init_vector_type (builtin_type_int8, 16)); | |
2004 | append_composite_type_field (t, "v8_int16", | |
2005 | init_vector_type (builtin_type_int16, 8)); | |
2006 | append_composite_type_field (t, "v4_int32", | |
2007 | init_vector_type (builtin_type_int32, 4)); | |
2008 | append_composite_type_field (t, "v2_int64", | |
2009 | init_vector_type (builtin_type_int64, 2)); | |
2010 | append_composite_type_field (t, "uint128", builtin_type_int128); | |
2011 | ||
2012 | TYPE_FLAGS (t) |= TYPE_FLAG_VECTOR; | |
2013 | TYPE_NAME (t) = "builtin_type_vec128i"; | |
2014 | tdep->i386_sse_type = t; | |
2015 | } | |
2016 | ||
2017 | return tdep->i386_sse_type; | |
2018 | } | |
2019 | ||
d7a0d72c MK |
2020 | /* Return the GDB type object for the "standard" data type of data in |
2021 | register REGNUM. Perhaps %esi and %edi should go here, but | |
2022 | potentially they could be used for things other than address. */ | |
2023 | ||
3a1e71e3 | 2024 | static struct type * |
4e259f09 | 2025 | i386_register_type (struct gdbarch *gdbarch, int regnum) |
d7a0d72c | 2026 | { |
ab533587 MK |
2027 | if (regnum == I386_EIP_REGNUM) |
2028 | return builtin_type_void_func_ptr; | |
2029 | ||
5ae96ec1 MK |
2030 | if (regnum == I386_EFLAGS_REGNUM) |
2031 | return i386_eflags_type; | |
2032 | ||
ab533587 MK |
2033 | if (regnum == I386_EBP_REGNUM || regnum == I386_ESP_REGNUM) |
2034 | return builtin_type_void_data_ptr; | |
d7a0d72c | 2035 | |
20a6ec49 | 2036 | if (i386_fp_regnum_p (gdbarch, regnum)) |
c6ba6f0d | 2037 | return builtin_type_i387_ext; |
d7a0d72c | 2038 | |
878d9193 | 2039 | if (i386_mmx_regnum_p (gdbarch, regnum)) |
794ac428 | 2040 | return i386_mmx_type (gdbarch); |
878d9193 | 2041 | |
5716833c | 2042 | if (i386_sse_regnum_p (gdbarch, regnum)) |
794ac428 | 2043 | return i386_sse_type (gdbarch); |
d7a0d72c | 2044 | |
20a6ec49 | 2045 | if (regnum == I387_MXCSR_REGNUM (gdbarch_tdep (gdbarch))) |
878d9193 MK |
2046 | return i386_mxcsr_type; |
2047 | ||
d7a0d72c MK |
2048 | return builtin_type_int; |
2049 | } | |
2050 | ||
28fc6740 | 2051 | /* Map a cooked register onto a raw register or memory. For the i386, |
acd5c798 | 2052 | the MMX registers need to be mapped onto floating point registers. */ |
28fc6740 AC |
2053 | |
2054 | static int | |
c86c27af | 2055 | i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum) |
28fc6740 | 2056 | { |
5716833c MK |
2057 | struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache)); |
2058 | int mmxreg, fpreg; | |
28fc6740 AC |
2059 | ULONGEST fstat; |
2060 | int tos; | |
c86c27af | 2061 | |
5716833c | 2062 | mmxreg = regnum - tdep->mm0_regnum; |
20a6ec49 | 2063 | regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat); |
28fc6740 | 2064 | tos = (fstat >> 11) & 0x7; |
5716833c MK |
2065 | fpreg = (mmxreg + tos) % 8; |
2066 | ||
20a6ec49 | 2067 | return (I387_ST0_REGNUM (tdep) + fpreg); |
28fc6740 AC |
2068 | } |
2069 | ||
2070 | static void | |
2071 | i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache, | |
42835c2b | 2072 | int regnum, gdb_byte *buf) |
28fc6740 | 2073 | { |
5716833c | 2074 | if (i386_mmx_regnum_p (gdbarch, regnum)) |
28fc6740 | 2075 | { |
63c0089f | 2076 | gdb_byte mmx_buf[MAX_REGISTER_SIZE]; |
c86c27af MK |
2077 | int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum); |
2078 | ||
28fc6740 | 2079 | /* Extract (always little endian). */ |
c86c27af | 2080 | regcache_raw_read (regcache, fpnum, mmx_buf); |
f837910f | 2081 | memcpy (buf, mmx_buf, register_size (gdbarch, regnum)); |
28fc6740 AC |
2082 | } |
2083 | else | |
2084 | regcache_raw_read (regcache, regnum, buf); | |
2085 | } | |
2086 | ||
2087 | static void | |
2088 | i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache, | |
42835c2b | 2089 | int regnum, const gdb_byte *buf) |
28fc6740 | 2090 | { |
5716833c | 2091 | if (i386_mmx_regnum_p (gdbarch, regnum)) |
28fc6740 | 2092 | { |
63c0089f | 2093 | gdb_byte mmx_buf[MAX_REGISTER_SIZE]; |
c86c27af MK |
2094 | int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum); |
2095 | ||
28fc6740 AC |
2096 | /* Read ... */ |
2097 | regcache_raw_read (regcache, fpnum, mmx_buf); | |
2098 | /* ... Modify ... (always little endian). */ | |
f837910f | 2099 | memcpy (mmx_buf, buf, register_size (gdbarch, regnum)); |
28fc6740 AC |
2100 | /* ... Write. */ |
2101 | regcache_raw_write (regcache, fpnum, mmx_buf); | |
2102 | } | |
2103 | else | |
2104 | regcache_raw_write (regcache, regnum, buf); | |
2105 | } | |
ff2e87ac AC |
2106 | \f |
2107 | ||
ff2e87ac AC |
2108 | /* Return the register number of the register allocated by GCC after |
2109 | REGNUM, or -1 if there is no such register. */ | |
2110 | ||
2111 | static int | |
2112 | i386_next_regnum (int regnum) | |
2113 | { | |
2114 | /* GCC allocates the registers in the order: | |
2115 | ||
2116 | %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ... | |
2117 | ||
2118 | Since storing a variable in %esp doesn't make any sense we return | |
2119 | -1 for %ebp and for %esp itself. */ | |
2120 | static int next_regnum[] = | |
2121 | { | |
2122 | I386_EDX_REGNUM, /* Slot for %eax. */ | |
2123 | I386_EBX_REGNUM, /* Slot for %ecx. */ | |
2124 | I386_ECX_REGNUM, /* Slot for %edx. */ | |
2125 | I386_ESI_REGNUM, /* Slot for %ebx. */ | |
2126 | -1, -1, /* Slots for %esp and %ebp. */ | |
2127 | I386_EDI_REGNUM, /* Slot for %esi. */ | |
2128 | I386_EBP_REGNUM /* Slot for %edi. */ | |
2129 | }; | |
2130 | ||
de5b9bb9 | 2131 | if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0])) |
ff2e87ac | 2132 | return next_regnum[regnum]; |
28fc6740 | 2133 | |
ff2e87ac AC |
2134 | return -1; |
2135 | } | |
2136 | ||
2137 | /* Return nonzero if a value of type TYPE stored in register REGNUM | |
2138 | needs any special handling. */ | |
d7a0d72c | 2139 | |
3a1e71e3 | 2140 | static int |
0abe36f5 | 2141 | i386_convert_register_p (struct gdbarch *gdbarch, int regnum, struct type *type) |
d7a0d72c | 2142 | { |
de5b9bb9 MK |
2143 | int len = TYPE_LENGTH (type); |
2144 | ||
ff2e87ac AC |
2145 | /* Values may be spread across multiple registers. Most debugging |
2146 | formats aren't expressive enough to specify the locations, so | |
2147 | some heuristics is involved. Right now we only handle types that | |
de5b9bb9 MK |
2148 | have a length that is a multiple of the word size, since GCC |
2149 | doesn't seem to put any other types into registers. */ | |
2150 | if (len > 4 && len % 4 == 0) | |
2151 | { | |
2152 | int last_regnum = regnum; | |
2153 | ||
2154 | while (len > 4) | |
2155 | { | |
2156 | last_regnum = i386_next_regnum (last_regnum); | |
2157 | len -= 4; | |
2158 | } | |
2159 | ||
2160 | if (last_regnum != -1) | |
2161 | return 1; | |
2162 | } | |
ff2e87ac | 2163 | |
0abe36f5 | 2164 | return i387_convert_register_p (gdbarch, regnum, type); |
d7a0d72c MK |
2165 | } |
2166 | ||
ff2e87ac AC |
2167 | /* Read a value of type TYPE from register REGNUM in frame FRAME, and |
2168 | return its contents in TO. */ | |
ac27f131 | 2169 | |
3a1e71e3 | 2170 | static void |
ff2e87ac | 2171 | i386_register_to_value (struct frame_info *frame, int regnum, |
42835c2b | 2172 | struct type *type, gdb_byte *to) |
ac27f131 | 2173 | { |
20a6ec49 | 2174 | struct gdbarch *gdbarch = get_frame_arch (frame); |
de5b9bb9 | 2175 | int len = TYPE_LENGTH (type); |
de5b9bb9 | 2176 | |
ff2e87ac AC |
2177 | /* FIXME: kettenis/20030609: What should we do if REGNUM isn't |
2178 | available in FRAME (i.e. if it wasn't saved)? */ | |
3d261580 | 2179 | |
20a6ec49 | 2180 | if (i386_fp_regnum_p (gdbarch, regnum)) |
8d7f6b4a | 2181 | { |
d532c08f MK |
2182 | i387_register_to_value (frame, regnum, type, to); |
2183 | return; | |
8d7f6b4a | 2184 | } |
ff2e87ac | 2185 | |
fd35795f | 2186 | /* Read a value spread across multiple registers. */ |
de5b9bb9 MK |
2187 | |
2188 | gdb_assert (len > 4 && len % 4 == 0); | |
3d261580 | 2189 | |
de5b9bb9 MK |
2190 | while (len > 0) |
2191 | { | |
2192 | gdb_assert (regnum != -1); | |
20a6ec49 | 2193 | gdb_assert (register_size (gdbarch, regnum) == 4); |
d532c08f | 2194 | |
42835c2b | 2195 | get_frame_register (frame, regnum, to); |
de5b9bb9 MK |
2196 | regnum = i386_next_regnum (regnum); |
2197 | len -= 4; | |
42835c2b | 2198 | to += 4; |
de5b9bb9 | 2199 | } |
ac27f131 MK |
2200 | } |
2201 | ||
ff2e87ac AC |
2202 | /* Write the contents FROM of a value of type TYPE into register |
2203 | REGNUM in frame FRAME. */ | |
ac27f131 | 2204 | |
3a1e71e3 | 2205 | static void |
ff2e87ac | 2206 | i386_value_to_register (struct frame_info *frame, int regnum, |
42835c2b | 2207 | struct type *type, const gdb_byte *from) |
ac27f131 | 2208 | { |
de5b9bb9 | 2209 | int len = TYPE_LENGTH (type); |
de5b9bb9 | 2210 | |
20a6ec49 | 2211 | if (i386_fp_regnum_p (get_frame_arch (frame), regnum)) |
c6ba6f0d | 2212 | { |
d532c08f MK |
2213 | i387_value_to_register (frame, regnum, type, from); |
2214 | return; | |
2215 | } | |
3d261580 | 2216 | |
fd35795f | 2217 | /* Write a value spread across multiple registers. */ |
de5b9bb9 MK |
2218 | |
2219 | gdb_assert (len > 4 && len % 4 == 0); | |
ff2e87ac | 2220 | |
de5b9bb9 MK |
2221 | while (len > 0) |
2222 | { | |
2223 | gdb_assert (regnum != -1); | |
875f8d0e | 2224 | gdb_assert (register_size (get_frame_arch (frame), regnum) == 4); |
d532c08f | 2225 | |
42835c2b | 2226 | put_frame_register (frame, regnum, from); |
de5b9bb9 MK |
2227 | regnum = i386_next_regnum (regnum); |
2228 | len -= 4; | |
42835c2b | 2229 | from += 4; |
de5b9bb9 | 2230 | } |
ac27f131 | 2231 | } |
ff2e87ac | 2232 | \f |
7fdafb5a MK |
2233 | /* Supply register REGNUM from the buffer specified by GREGS and LEN |
2234 | in the general-purpose register set REGSET to register cache | |
2235 | REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */ | |
ff2e87ac | 2236 | |
20187ed5 | 2237 | void |
473f17b0 MK |
2238 | i386_supply_gregset (const struct regset *regset, struct regcache *regcache, |
2239 | int regnum, const void *gregs, size_t len) | |
2240 | { | |
9ea75c57 | 2241 | const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch); |
156cdbee | 2242 | const gdb_byte *regs = gregs; |
473f17b0 MK |
2243 | int i; |
2244 | ||
2245 | gdb_assert (len == tdep->sizeof_gregset); | |
2246 | ||
2247 | for (i = 0; i < tdep->gregset_num_regs; i++) | |
2248 | { | |
2249 | if ((regnum == i || regnum == -1) | |
2250 | && tdep->gregset_reg_offset[i] != -1) | |
2251 | regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]); | |
2252 | } | |
2253 | } | |
2254 | ||
7fdafb5a MK |
2255 | /* Collect register REGNUM from the register cache REGCACHE and store |
2256 | it in the buffer specified by GREGS and LEN as described by the | |
2257 | general-purpose register set REGSET. If REGNUM is -1, do this for | |
2258 | all registers in REGSET. */ | |
2259 | ||
2260 | void | |
2261 | i386_collect_gregset (const struct regset *regset, | |
2262 | const struct regcache *regcache, | |
2263 | int regnum, void *gregs, size_t len) | |
2264 | { | |
2265 | const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch); | |
156cdbee | 2266 | gdb_byte *regs = gregs; |
7fdafb5a MK |
2267 | int i; |
2268 | ||
2269 | gdb_assert (len == tdep->sizeof_gregset); | |
2270 | ||
2271 | for (i = 0; i < tdep->gregset_num_regs; i++) | |
2272 | { | |
2273 | if ((regnum == i || regnum == -1) | |
2274 | && tdep->gregset_reg_offset[i] != -1) | |
2275 | regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]); | |
2276 | } | |
2277 | } | |
2278 | ||
2279 | /* Supply register REGNUM from the buffer specified by FPREGS and LEN | |
2280 | in the floating-point register set REGSET to register cache | |
2281 | REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */ | |
473f17b0 MK |
2282 | |
2283 | static void | |
2284 | i386_supply_fpregset (const struct regset *regset, struct regcache *regcache, | |
2285 | int regnum, const void *fpregs, size_t len) | |
2286 | { | |
9ea75c57 | 2287 | const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch); |
473f17b0 | 2288 | |
66a72d25 MK |
2289 | if (len == I387_SIZEOF_FXSAVE) |
2290 | { | |
2291 | i387_supply_fxsave (regcache, regnum, fpregs); | |
2292 | return; | |
2293 | } | |
2294 | ||
473f17b0 MK |
2295 | gdb_assert (len == tdep->sizeof_fpregset); |
2296 | i387_supply_fsave (regcache, regnum, fpregs); | |
2297 | } | |
8446b36a | 2298 | |
2f305df1 MK |
2299 | /* Collect register REGNUM from the register cache REGCACHE and store |
2300 | it in the buffer specified by FPREGS and LEN as described by the | |
2301 | floating-point register set REGSET. If REGNUM is -1, do this for | |
2302 | all registers in REGSET. */ | |
7fdafb5a MK |
2303 | |
2304 | static void | |
2305 | i386_collect_fpregset (const struct regset *regset, | |
2306 | const struct regcache *regcache, | |
2307 | int regnum, void *fpregs, size_t len) | |
2308 | { | |
2309 | const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch); | |
2310 | ||
2311 | if (len == I387_SIZEOF_FXSAVE) | |
2312 | { | |
2313 | i387_collect_fxsave (regcache, regnum, fpregs); | |
2314 | return; | |
2315 | } | |
2316 | ||
2317 | gdb_assert (len == tdep->sizeof_fpregset); | |
2318 | i387_collect_fsave (regcache, regnum, fpregs); | |
2319 | } | |
2320 | ||
8446b36a MK |
2321 | /* Return the appropriate register set for the core section identified |
2322 | by SECT_NAME and SECT_SIZE. */ | |
2323 | ||
2324 | const struct regset * | |
2325 | i386_regset_from_core_section (struct gdbarch *gdbarch, | |
2326 | const char *sect_name, size_t sect_size) | |
2327 | { | |
2328 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
2329 | ||
2330 | if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset) | |
2331 | { | |
2332 | if (tdep->gregset == NULL) | |
7fdafb5a MK |
2333 | tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset, |
2334 | i386_collect_gregset); | |
8446b36a MK |
2335 | return tdep->gregset; |
2336 | } | |
2337 | ||
66a72d25 MK |
2338 | if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset) |
2339 | || (strcmp (sect_name, ".reg-xfp") == 0 | |
2340 | && sect_size == I387_SIZEOF_FXSAVE)) | |
8446b36a MK |
2341 | { |
2342 | if (tdep->fpregset == NULL) | |
7fdafb5a MK |
2343 | tdep->fpregset = regset_alloc (gdbarch, i386_supply_fpregset, |
2344 | i386_collect_fpregset); | |
8446b36a MK |
2345 | return tdep->fpregset; |
2346 | } | |
2347 | ||
2348 | return NULL; | |
2349 | } | |
473f17b0 | 2350 | \f |
fc338970 | 2351 | |
fc338970 | 2352 | /* Stuff for WIN32 PE style DLL's but is pretty generic really. */ |
c906108c SS |
2353 | |
2354 | CORE_ADDR | |
1cce71eb | 2355 | i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name) |
c906108c | 2356 | { |
fc338970 | 2357 | if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */ |
c906108c | 2358 | { |
c5aa993b | 2359 | unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4); |
c906108c | 2360 | struct minimal_symbol *indsym = |
fc338970 | 2361 | indirect ? lookup_minimal_symbol_by_pc (indirect) : 0; |
645dd519 | 2362 | char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0; |
c906108c | 2363 | |
c5aa993b | 2364 | if (symname) |
c906108c | 2365 | { |
c5aa993b JM |
2366 | if (strncmp (symname, "__imp_", 6) == 0 |
2367 | || strncmp (symname, "_imp_", 5) == 0) | |
c906108c SS |
2368 | return name ? 1 : read_memory_unsigned_integer (indirect, 4); |
2369 | } | |
2370 | } | |
fc338970 | 2371 | return 0; /* Not a trampoline. */ |
c906108c | 2372 | } |
fc338970 MK |
2373 | \f |
2374 | ||
10458914 DJ |
2375 | /* Return whether the THIS_FRAME corresponds to a sigtramp |
2376 | routine. */ | |
8201327c MK |
2377 | |
2378 | static int | |
10458914 | 2379 | i386_sigtramp_p (struct frame_info *this_frame) |
8201327c | 2380 | { |
10458914 | 2381 | CORE_ADDR pc = get_frame_pc (this_frame); |
911bc6ee MK |
2382 | char *name; |
2383 | ||
2384 | find_pc_partial_function (pc, &name, NULL, NULL); | |
8201327c MK |
2385 | return (name && strcmp ("_sigtramp", name) == 0); |
2386 | } | |
2387 | \f | |
2388 | ||
fc338970 MK |
2389 | /* We have two flavours of disassembly. The machinery on this page |
2390 | deals with switching between those. */ | |
c906108c SS |
2391 | |
2392 | static int | |
a89aa300 | 2393 | i386_print_insn (bfd_vma pc, struct disassemble_info *info) |
c906108c | 2394 | { |
5e3397bb MK |
2395 | gdb_assert (disassembly_flavor == att_flavor |
2396 | || disassembly_flavor == intel_flavor); | |
2397 | ||
2398 | /* FIXME: kettenis/20020915: Until disassembler_options is properly | |
2399 | constified, cast to prevent a compiler warning. */ | |
2400 | info->disassembler_options = (char *) disassembly_flavor; | |
5e3397bb MK |
2401 | |
2402 | return print_insn_i386 (pc, info); | |
7a292a7a | 2403 | } |
fc338970 | 2404 | \f |
3ce1502b | 2405 | |
8201327c MK |
2406 | /* There are a few i386 architecture variants that differ only |
2407 | slightly from the generic i386 target. For now, we don't give them | |
2408 | their own source file, but include them here. As a consequence, | |
2409 | they'll always be included. */ | |
3ce1502b | 2410 | |
8201327c | 2411 | /* System V Release 4 (SVR4). */ |
3ce1502b | 2412 | |
10458914 DJ |
2413 | /* Return whether THIS_FRAME corresponds to a SVR4 sigtramp |
2414 | routine. */ | |
911bc6ee | 2415 | |
8201327c | 2416 | static int |
10458914 | 2417 | i386_svr4_sigtramp_p (struct frame_info *this_frame) |
d2a7c97a | 2418 | { |
10458914 | 2419 | CORE_ADDR pc = get_frame_pc (this_frame); |
911bc6ee MK |
2420 | char *name; |
2421 | ||
acd5c798 MK |
2422 | /* UnixWare uses _sigacthandler. The origin of the other symbols is |
2423 | currently unknown. */ | |
911bc6ee | 2424 | find_pc_partial_function (pc, &name, NULL, NULL); |
8201327c MK |
2425 | return (name && (strcmp ("_sigreturn", name) == 0 |
2426 | || strcmp ("_sigacthandler", name) == 0 | |
2427 | || strcmp ("sigvechandler", name) == 0)); | |
2428 | } | |
d2a7c97a | 2429 | |
10458914 DJ |
2430 | /* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the |
2431 | address of the associated sigcontext (ucontext) structure. */ | |
3ce1502b | 2432 | |
3a1e71e3 | 2433 | static CORE_ADDR |
10458914 | 2434 | i386_svr4_sigcontext_addr (struct frame_info *this_frame) |
8201327c | 2435 | { |
63c0089f | 2436 | gdb_byte buf[4]; |
acd5c798 | 2437 | CORE_ADDR sp; |
3ce1502b | 2438 | |
10458914 | 2439 | get_frame_register (this_frame, I386_ESP_REGNUM, buf); |
acd5c798 | 2440 | sp = extract_unsigned_integer (buf, 4); |
21d0e8a4 | 2441 | |
acd5c798 | 2442 | return read_memory_unsigned_integer (sp + 8, 4); |
8201327c MK |
2443 | } |
2444 | \f | |
3ce1502b | 2445 | |
8201327c | 2446 | /* Generic ELF. */ |
d2a7c97a | 2447 | |
8201327c MK |
2448 | void |
2449 | i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
2450 | { | |
c4fc7f1b MK |
2451 | /* We typically use stabs-in-ELF with the SVR4 register numbering. */ |
2452 | set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum); | |
8201327c | 2453 | } |
3ce1502b | 2454 | |
8201327c | 2455 | /* System V Release 4 (SVR4). */ |
3ce1502b | 2456 | |
8201327c MK |
2457 | void |
2458 | i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) | |
2459 | { | |
2460 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); | |
3ce1502b | 2461 | |
8201327c MK |
2462 | /* System V Release 4 uses ELF. */ |
2463 | i386_elf_init_abi (info, gdbarch); | |
3ce1502b | 2464 | |
dfe01d39 | 2465 | /* System V Release 4 has shared libraries. */ |
dfe01d39 MK |
2466 | set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target); |
2467 | ||
911bc6ee | 2468 | tdep->sigtramp_p = i386_svr4_sigtramp_p; |
21d0e8a4 | 2469 | tdep->sigcontext_addr = i386_svr4_sigcontext_addr; |
acd5c798 MK |
2470 | tdep->sc_pc_offset = 36 + 14 * 4; |
2471 | tdep->sc_sp_offset = 36 + 17 * 4; | |
3ce1502b | 2472 | |
8201327c | 2473 | tdep->jb_pc_offset = 20; |
3ce1502b MK |
2474 | } |
2475 | ||
8201327c | 2476 | /* DJGPP. */ |
3ce1502b | 2477 | |
3a1e71e3 | 2478 | static void |
8201327c | 2479 | i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) |
3ce1502b | 2480 | { |
8201327c | 2481 | struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); |
3ce1502b | 2482 | |
911bc6ee MK |
2483 | /* DJGPP doesn't have any special frames for signal handlers. */ |
2484 | tdep->sigtramp_p = NULL; | |
3ce1502b | 2485 | |
8201327c | 2486 | tdep->jb_pc_offset = 36; |
3ce1502b | 2487 | } |
8201327c | 2488 | \f |
2acceee2 | 2489 | |
38c968cf AC |
2490 | /* i386 register groups. In addition to the normal groups, add "mmx" |
2491 | and "sse". */ | |
2492 | ||
2493 | static struct reggroup *i386_sse_reggroup; | |
2494 | static struct reggroup *i386_mmx_reggroup; | |
2495 | ||
2496 | static void | |
2497 | i386_init_reggroups (void) | |
2498 | { | |
2499 | i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP); | |
2500 | i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP); | |
2501 | } | |
2502 | ||
2503 | static void | |
2504 | i386_add_reggroups (struct gdbarch *gdbarch) | |
2505 | { | |
2506 | reggroup_add (gdbarch, i386_sse_reggroup); | |
2507 | reggroup_add (gdbarch, i386_mmx_reggroup); | |
2508 | reggroup_add (gdbarch, general_reggroup); | |
2509 | reggroup_add (gdbarch, float_reggroup); | |
2510 | reggroup_add (gdbarch, all_reggroup); | |
2511 | reggroup_add (gdbarch, save_reggroup); | |
2512 | reggroup_add (gdbarch, restore_reggroup); | |
2513 | reggroup_add (gdbarch, vector_reggroup); | |
2514 | reggroup_add (gdbarch, system_reggroup); | |
2515 | } | |
2516 | ||
2517 | int | |
2518 | i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum, | |
2519 | struct reggroup *group) | |
2520 | { | |
5716833c MK |
2521 | int sse_regnum_p = (i386_sse_regnum_p (gdbarch, regnum) |
2522 | || i386_mxcsr_regnum_p (gdbarch, regnum)); | |
20a6ec49 MD |
2523 | int fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum) |
2524 | || i386_fpc_regnum_p (gdbarch, regnum)); | |
5716833c | 2525 | int mmx_regnum_p = (i386_mmx_regnum_p (gdbarch, regnum)); |
acd5c798 | 2526 | |
38c968cf AC |
2527 | if (group == i386_mmx_reggroup) |
2528 | return mmx_regnum_p; | |
2529 | if (group == i386_sse_reggroup) | |
2530 | return sse_regnum_p; | |
2531 | if (group == vector_reggroup) | |
2532 | return (mmx_regnum_p || sse_regnum_p); | |
2533 | if (group == float_reggroup) | |
2534 | return fp_regnum_p; | |
2535 | if (group == general_reggroup) | |
2536 | return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p); | |
acd5c798 | 2537 | |
38c968cf AC |
2538 | return default_register_reggroup_p (gdbarch, regnum, group); |
2539 | } | |
38c968cf | 2540 | \f |
acd5c798 | 2541 | |
f837910f MK |
2542 | /* Get the ARGIth function argument for the current function. */ |
2543 | ||
42c466d7 | 2544 | static CORE_ADDR |
143985b7 AF |
2545 | i386_fetch_pointer_argument (struct frame_info *frame, int argi, |
2546 | struct type *type) | |
2547 | { | |
f837910f MK |
2548 | CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM); |
2549 | return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4); | |
143985b7 AF |
2550 | } |
2551 | ||
2552 | \f | |
3a1e71e3 | 2553 | static struct gdbarch * |
a62cc96e AC |
2554 | i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) |
2555 | { | |
cd3c07fc | 2556 | struct gdbarch_tdep *tdep; |
a62cc96e AC |
2557 | struct gdbarch *gdbarch; |
2558 | ||
4be87837 DJ |
2559 | /* If there is already a candidate, use it. */ |
2560 | arches = gdbarch_list_lookup_by_info (arches, &info); | |
2561 | if (arches != NULL) | |
2562 | return arches->gdbarch; | |
a62cc96e AC |
2563 | |
2564 | /* Allocate space for the new architecture. */ | |
794ac428 | 2565 | tdep = XCALLOC (1, struct gdbarch_tdep); |
a62cc96e AC |
2566 | gdbarch = gdbarch_alloc (&info, tdep); |
2567 | ||
473f17b0 MK |
2568 | /* General-purpose registers. */ |
2569 | tdep->gregset = NULL; | |
2570 | tdep->gregset_reg_offset = NULL; | |
2571 | tdep->gregset_num_regs = I386_NUM_GREGS; | |
2572 | tdep->sizeof_gregset = 0; | |
2573 | ||
2574 | /* Floating-point registers. */ | |
2575 | tdep->fpregset = NULL; | |
2576 | tdep->sizeof_fpregset = I387_SIZEOF_FSAVE; | |
2577 | ||
5716833c | 2578 | /* The default settings include the FPU registers, the MMX registers |
fd35795f | 2579 | and the SSE registers. This can be overridden for a specific ABI |
5716833c MK |
2580 | by adjusting the members `st0_regnum', `mm0_regnum' and |
2581 | `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers | |
2582 | will show up in the output of "info all-registers". Ideally we | |
2583 | should try to autodetect whether they are available, such that we | |
2584 | can prevent "info all-registers" from displaying registers that | |
2585 | aren't available. | |
2586 | ||
2587 | NOTE: kevinb/2003-07-13: ... if it's a choice between printing | |
2588 | [the SSE registers] always (even when they don't exist) or never | |
2589 | showing them to the user (even when they do exist), I prefer the | |
2590 | former over the latter. */ | |
2591 | ||
2592 | tdep->st0_regnum = I386_ST0_REGNUM; | |
2593 | ||
2594 | /* The MMX registers are implemented as pseudo-registers. Put off | |
fd35795f | 2595 | calculating the register number for %mm0 until we know the number |
5716833c MK |
2596 | of raw registers. */ |
2597 | tdep->mm0_regnum = 0; | |
2598 | ||
2599 | /* I386_NUM_XREGS includes %mxcsr, so substract one. */ | |
49ed40de | 2600 | tdep->num_xmm_regs = I386_NUM_XREGS - 1; |
d2a7c97a | 2601 | |
8201327c MK |
2602 | tdep->jb_pc_offset = -1; |
2603 | tdep->struct_return = pcc_struct_return; | |
8201327c MK |
2604 | tdep->sigtramp_start = 0; |
2605 | tdep->sigtramp_end = 0; | |
911bc6ee | 2606 | tdep->sigtramp_p = i386_sigtramp_p; |
21d0e8a4 | 2607 | tdep->sigcontext_addr = NULL; |
a3386186 | 2608 | tdep->sc_reg_offset = NULL; |
8201327c | 2609 | tdep->sc_pc_offset = -1; |
21d0e8a4 | 2610 | tdep->sc_sp_offset = -1; |
8201327c | 2611 | |
896fb97d MK |
2612 | /* The format used for `long double' on almost all i386 targets is |
2613 | the i387 extended floating-point format. In fact, of all targets | |
2614 | in the GCC 2.95 tree, only OSF/1 does it different, and insists | |
2615 | on having a `long double' that's not `long' at all. */ | |
8da61cc4 | 2616 | set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext); |
21d0e8a4 | 2617 | |
66da5fd8 | 2618 | /* Although the i387 extended floating-point has only 80 significant |
896fb97d MK |
2619 | bits, a `long double' actually takes up 96, probably to enforce |
2620 | alignment. */ | |
2621 | set_gdbarch_long_double_bit (gdbarch, 96); | |
2622 | ||
49ed40de KB |
2623 | /* The default ABI includes general-purpose registers, |
2624 | floating-point registers, and the SSE registers. */ | |
2625 | set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS); | |
acd5c798 MK |
2626 | set_gdbarch_register_name (gdbarch, i386_register_name); |
2627 | set_gdbarch_register_type (gdbarch, i386_register_type); | |
21d0e8a4 | 2628 | |
acd5c798 MK |
2629 | /* Register numbers of various important registers. */ |
2630 | set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */ | |
2631 | set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */ | |
2632 | set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */ | |
2633 | set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */ | |
356a6b3e | 2634 | |
c4fc7f1b MK |
2635 | /* NOTE: kettenis/20040418: GCC does have two possible register |
2636 | numbering schemes on the i386: dbx and SVR4. These schemes | |
2637 | differ in how they number %ebp, %esp, %eflags, and the | |
fd35795f | 2638 | floating-point registers, and are implemented by the arrays |
c4fc7f1b MK |
2639 | dbx_register_map[] and svr4_dbx_register_map in |
2640 | gcc/config/i386.c. GCC also defines a third numbering scheme in | |
2641 | gcc/config/i386.c, which it designates as the "default" register | |
2642 | map used in 64bit mode. This last register numbering scheme is | |
d4dc1a91 | 2643 | implemented in dbx64_register_map, and is used for AMD64; see |
c4fc7f1b MK |
2644 | amd64-tdep.c. |
2645 | ||
2646 | Currently, each GCC i386 target always uses the same register | |
2647 | numbering scheme across all its supported debugging formats | |
2648 | i.e. SDB (COFF), stabs and DWARF 2. This is because | |
2649 | gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the | |
2650 | DBX_REGISTER_NUMBER macro which is defined by each target's | |
2651 | respective config header in a manner independent of the requested | |
2652 | output debugging format. | |
2653 | ||
2654 | This does not match the arrangement below, which presumes that | |
2655 | the SDB and stabs numbering schemes differ from the DWARF and | |
2656 | DWARF 2 ones. The reason for this arrangement is that it is | |
2657 | likely to get the numbering scheme for the target's | |
2658 | default/native debug format right. For targets where GCC is the | |
2659 | native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for | |
2660 | targets where the native toolchain uses a different numbering | |
2661 | scheme for a particular debug format (stabs-in-ELF on Solaris) | |
d4dc1a91 BF |
2662 | the defaults below will have to be overridden, like |
2663 | i386_elf_init_abi() does. */ | |
c4fc7f1b MK |
2664 | |
2665 | /* Use the dbx register numbering scheme for stabs and COFF. */ | |
2666 | set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum); | |
2667 | set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum); | |
2668 | ||
ba2b1c56 | 2669 | /* Use the SVR4 register numbering scheme for DWARF 2. */ |
c4fc7f1b | 2670 | set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum); |
356a6b3e | 2671 | |
055d23b8 | 2672 | /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to |
356a6b3e MK |
2673 | be in use on any of the supported i386 targets. */ |
2674 | ||
61113f8b MK |
2675 | set_gdbarch_print_float_info (gdbarch, i387_print_float_info); |
2676 | ||
8201327c | 2677 | set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target); |
96297dab | 2678 | |
a62cc96e | 2679 | /* Call dummy code. */ |
acd5c798 | 2680 | set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call); |
a62cc96e | 2681 | |
ff2e87ac AC |
2682 | set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p); |
2683 | set_gdbarch_register_to_value (gdbarch, i386_register_to_value); | |
2684 | set_gdbarch_value_to_register (gdbarch, i386_value_to_register); | |
b6197528 | 2685 | |
c5e656c1 | 2686 | set_gdbarch_return_value (gdbarch, i386_return_value); |
8201327c | 2687 | |
93924b6b MK |
2688 | set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue); |
2689 | ||
2690 | /* Stack grows downward. */ | |
2691 | set_gdbarch_inner_than (gdbarch, core_addr_lessthan); | |
2692 | ||
2693 | set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc); | |
2694 | set_gdbarch_decr_pc_after_break (gdbarch, 1); | |
237fc4c9 | 2695 | set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN); |
42fdc8df | 2696 | |
42fdc8df | 2697 | set_gdbarch_frame_args_skip (gdbarch, 8); |
8201327c | 2698 | |
28fc6740 | 2699 | /* Wire in the MMX registers. */ |
0f751ff2 | 2700 | set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs); |
28fc6740 AC |
2701 | set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read); |
2702 | set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write); | |
2703 | ||
5e3397bb MK |
2704 | set_gdbarch_print_insn (gdbarch, i386_print_insn); |
2705 | ||
10458914 | 2706 | set_gdbarch_dummy_id (gdbarch, i386_dummy_id); |
acd5c798 MK |
2707 | |
2708 | set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc); | |
2709 | ||
38c968cf AC |
2710 | /* Add the i386 register groups. */ |
2711 | i386_add_reggroups (gdbarch); | |
2712 | set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p); | |
2713 | ||
143985b7 AF |
2714 | /* Helper for function argument information. */ |
2715 | set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument); | |
2716 | ||
6405b0a6 | 2717 | /* Hook in the DWARF CFI frame unwinder. */ |
10458914 | 2718 | dwarf2_append_unwinders (gdbarch); |
6405b0a6 | 2719 | |
acd5c798 | 2720 | frame_base_set_default (gdbarch, &i386_frame_base); |
6c0e89ed | 2721 | |
3ce1502b | 2722 | /* Hook in ABI-specific overrides, if they have been registered. */ |
4be87837 | 2723 | gdbarch_init_osabi (info, gdbarch); |
3ce1502b | 2724 | |
10458914 DJ |
2725 | frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind); |
2726 | frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind); | |
acd5c798 | 2727 | |
8446b36a MK |
2728 | /* If we have a register mapping, enable the generic core file |
2729 | support, unless it has already been enabled. */ | |
2730 | if (tdep->gregset_reg_offset | |
2731 | && !gdbarch_regset_from_core_section_p (gdbarch)) | |
2732 | set_gdbarch_regset_from_core_section (gdbarch, | |
2733 | i386_regset_from_core_section); | |
2734 | ||
5716833c MK |
2735 | /* Unless support for MMX has been disabled, make %mm0 the first |
2736 | pseudo-register. */ | |
2737 | if (tdep->mm0_regnum == 0) | |
2738 | tdep->mm0_regnum = gdbarch_num_regs (gdbarch); | |
2739 | ||
a62cc96e AC |
2740 | return gdbarch; |
2741 | } | |
2742 | ||
8201327c MK |
2743 | static enum gdb_osabi |
2744 | i386_coff_osabi_sniffer (bfd *abfd) | |
2745 | { | |
762c5349 MK |
2746 | if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0 |
2747 | || strcmp (bfd_get_target (abfd), "coff-go32") == 0) | |
8201327c MK |
2748 | return GDB_OSABI_GO32; |
2749 | ||
2750 | return GDB_OSABI_UNKNOWN; | |
2751 | } | |
8201327c MK |
2752 | \f |
2753 | ||
28e9e0f0 MK |
2754 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
2755 | void _initialize_i386_tdep (void); | |
2756 | ||
c906108c | 2757 | void |
fba45db2 | 2758 | _initialize_i386_tdep (void) |
c906108c | 2759 | { |
a62cc96e AC |
2760 | register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init); |
2761 | ||
fc338970 | 2762 | /* Add the variable that controls the disassembly flavor. */ |
7ab04401 AC |
2763 | add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors, |
2764 | &disassembly_flavor, _("\ | |
2765 | Set the disassembly flavor."), _("\ | |
2766 | Show the disassembly flavor."), _("\ | |
2767 | The valid values are \"att\" and \"intel\", and the default value is \"att\"."), | |
2768 | NULL, | |
2769 | NULL, /* FIXME: i18n: */ | |
2770 | &setlist, &showlist); | |
8201327c MK |
2771 | |
2772 | /* Add the variable that controls the convention for returning | |
2773 | structs. */ | |
7ab04401 AC |
2774 | add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions, |
2775 | &struct_convention, _("\ | |
2776 | Set the convention for returning small structs."), _("\ | |
2777 | Show the convention for returning small structs."), _("\ | |
2778 | Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\ | |
2779 | is \"default\"."), | |
2780 | NULL, | |
2781 | NULL, /* FIXME: i18n: */ | |
2782 | &setlist, &showlist); | |
8201327c MK |
2783 | |
2784 | gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour, | |
2785 | i386_coff_osabi_sniffer); | |
8201327c | 2786 | |
05816f70 | 2787 | gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4, |
8201327c | 2788 | i386_svr4_init_abi); |
05816f70 | 2789 | gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32, |
8201327c | 2790 | i386_go32_init_abi); |
38c968cf | 2791 | |
5ae96ec1 | 2792 | /* Initialize the i386-specific register groups & types. */ |
38c968cf | 2793 | i386_init_reggroups (); |
5ae96ec1 | 2794 | i386_init_types(); |
c906108c | 2795 | } |