* gdbtypes.h (builtin_type_int0, builtin_type_int8, builtin_type_uint8,
[deliverable/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
c906108c 1/* Intel 386 target-dependent stuff.
349c5d5f 2
6aba47ca 3 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
0fb0cc75 4 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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"
1903f0e6 23#include "opcode/i386.h"
acd5c798
MK
24#include "arch-utils.h"
25#include "command.h"
26#include "dummy-frame.h"
6405b0a6 27#include "dwarf2-frame.h"
acd5c798 28#include "doublest.h"
c906108c 29#include "frame.h"
acd5c798
MK
30#include "frame-base.h"
31#include "frame-unwind.h"
c906108c 32#include "inferior.h"
acd5c798 33#include "gdbcmd.h"
c906108c 34#include "gdbcore.h"
e6bb342a 35#include "gdbtypes.h"
dfe01d39 36#include "objfiles.h"
acd5c798
MK
37#include "osabi.h"
38#include "regcache.h"
39#include "reggroups.h"
473f17b0 40#include "regset.h"
c0d1d883 41#include "symfile.h"
c906108c 42#include "symtab.h"
acd5c798 43#include "target.h"
fd0407d6 44#include "value.h"
a89aa300 45#include "dis-asm.h"
acd5c798 46
3d261580 47#include "gdb_assert.h"
acd5c798 48#include "gdb_string.h"
3d261580 49
d2a7c97a 50#include "i386-tdep.h"
61113f8b 51#include "i387-tdep.h"
d2a7c97a 52
7ad10968
HZ
53#include "record.h"
54#include <stdint.h>
55
c4fc7f1b 56/* Register names. */
c40e1eab 57
fc633446
MK
58static char *i386_register_names[] =
59{
60 "eax", "ecx", "edx", "ebx",
61 "esp", "ebp", "esi", "edi",
62 "eip", "eflags", "cs", "ss",
63 "ds", "es", "fs", "gs",
64 "st0", "st1", "st2", "st3",
65 "st4", "st5", "st6", "st7",
66 "fctrl", "fstat", "ftag", "fiseg",
67 "fioff", "foseg", "fooff", "fop",
68 "xmm0", "xmm1", "xmm2", "xmm3",
69 "xmm4", "xmm5", "xmm6", "xmm7",
70 "mxcsr"
71};
72
1cb97e17 73static const int i386_num_register_names = ARRAY_SIZE (i386_register_names);
c40e1eab 74
c4fc7f1b 75/* Register names for MMX pseudo-registers. */
28fc6740
AC
76
77static char *i386_mmx_names[] =
78{
79 "mm0", "mm1", "mm2", "mm3",
80 "mm4", "mm5", "mm6", "mm7"
81};
c40e1eab 82
1cb97e17 83static const int i386_num_mmx_regs = ARRAY_SIZE (i386_mmx_names);
c40e1eab 84
28fc6740 85static int
5716833c 86i386_mmx_regnum_p (struct gdbarch *gdbarch, int regnum)
28fc6740 87{
5716833c
MK
88 int mm0_regnum = gdbarch_tdep (gdbarch)->mm0_regnum;
89
90 if (mm0_regnum < 0)
91 return 0;
92
93 return (regnum >= mm0_regnum && regnum < mm0_regnum + i386_num_mmx_regs);
28fc6740
AC
94}
95
5716833c 96/* SSE register? */
23a34459 97
5716833c
MK
98static int
99i386_sse_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 100{
5716833c
MK
101 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
102
20a6ec49 103 if (I387_NUM_XMM_REGS (tdep) == 0)
5716833c
MK
104 return 0;
105
20a6ec49
MD
106 return (I387_XMM0_REGNUM (tdep) <= regnum
107 && regnum < I387_MXCSR_REGNUM (tdep));
23a34459
AC
108}
109
5716833c
MK
110static int
111i386_mxcsr_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 112{
5716833c
MK
113 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
114
20a6ec49 115 if (I387_NUM_XMM_REGS (tdep) == 0)
5716833c
MK
116 return 0;
117
20a6ec49 118 return (regnum == I387_MXCSR_REGNUM (tdep));
23a34459
AC
119}
120
5716833c 121/* FP register? */
23a34459
AC
122
123int
20a6ec49 124i386_fp_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 125{
20a6ec49
MD
126 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
127
128 if (I387_ST0_REGNUM (tdep) < 0)
5716833c
MK
129 return 0;
130
20a6ec49
MD
131 return (I387_ST0_REGNUM (tdep) <= regnum
132 && regnum < I387_FCTRL_REGNUM (tdep));
23a34459
AC
133}
134
135int
20a6ec49 136i386_fpc_regnum_p (struct gdbarch *gdbarch, int regnum)
23a34459 137{
20a6ec49
MD
138 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
139
140 if (I387_ST0_REGNUM (tdep) < 0)
5716833c
MK
141 return 0;
142
20a6ec49
MD
143 return (I387_FCTRL_REGNUM (tdep) <= regnum
144 && regnum < I387_XMM0_REGNUM (tdep));
23a34459
AC
145}
146
30b0e2d8 147/* Return the name of register REGNUM. */
fc633446 148
fa88f677 149const char *
d93859e2 150i386_register_name (struct gdbarch *gdbarch, int regnum)
fc633446 151{
d93859e2 152 if (i386_mmx_regnum_p (gdbarch, regnum))
20a6ec49 153 return i386_mmx_names[regnum - I387_MM0_REGNUM (gdbarch_tdep (gdbarch))];
fc633446 154
30b0e2d8
MK
155 if (regnum >= 0 && regnum < i386_num_register_names)
156 return i386_register_names[regnum];
70913449 157
c40e1eab 158 return NULL;
fc633446
MK
159}
160
c4fc7f1b 161/* Convert a dbx register number REG to the appropriate register
85540d8c
MK
162 number used by GDB. */
163
8201327c 164static int
d3f73121 165i386_dbx_reg_to_regnum (struct gdbarch *gdbarch, int reg)
85540d8c 166{
20a6ec49
MD
167 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
168
c4fc7f1b
MK
169 /* This implements what GCC calls the "default" register map
170 (dbx_register_map[]). */
171
85540d8c
MK
172 if (reg >= 0 && reg <= 7)
173 {
9872ad24
JB
174 /* General-purpose registers. The debug info calls %ebp
175 register 4, and %esp register 5. */
176 if (reg == 4)
177 return 5;
178 else if (reg == 5)
179 return 4;
180 else return reg;
85540d8c
MK
181 }
182 else if (reg >= 12 && reg <= 19)
183 {
184 /* Floating-point registers. */
20a6ec49 185 return reg - 12 + I387_ST0_REGNUM (tdep);
85540d8c
MK
186 }
187 else if (reg >= 21 && reg <= 28)
188 {
189 /* SSE registers. */
20a6ec49 190 return reg - 21 + I387_XMM0_REGNUM (tdep);
85540d8c
MK
191 }
192 else if (reg >= 29 && reg <= 36)
193 {
194 /* MMX registers. */
20a6ec49 195 return reg - 29 + I387_MM0_REGNUM (tdep);
85540d8c
MK
196 }
197
198 /* This will hopefully provoke a warning. */
d3f73121 199 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
85540d8c
MK
200}
201
c4fc7f1b
MK
202/* Convert SVR4 register number REG to the appropriate register number
203 used by GDB. */
85540d8c 204
8201327c 205static int
d3f73121 206i386_svr4_reg_to_regnum (struct gdbarch *gdbarch, int reg)
85540d8c 207{
20a6ec49
MD
208 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
209
c4fc7f1b
MK
210 /* This implements the GCC register map that tries to be compatible
211 with the SVR4 C compiler for DWARF (svr4_dbx_register_map[]). */
212
213 /* The SVR4 register numbering includes %eip and %eflags, and
85540d8c
MK
214 numbers the floating point registers differently. */
215 if (reg >= 0 && reg <= 9)
216 {
acd5c798 217 /* General-purpose registers. */
85540d8c
MK
218 return reg;
219 }
220 else if (reg >= 11 && reg <= 18)
221 {
222 /* Floating-point registers. */
20a6ec49 223 return reg - 11 + I387_ST0_REGNUM (tdep);
85540d8c 224 }
c6f4c129 225 else if (reg >= 21 && reg <= 36)
85540d8c 226 {
c4fc7f1b 227 /* The SSE and MMX registers have the same numbers as with dbx. */
d3f73121 228 return i386_dbx_reg_to_regnum (gdbarch, reg);
85540d8c
MK
229 }
230
c6f4c129
JB
231 switch (reg)
232 {
20a6ec49
MD
233 case 37: return I387_FCTRL_REGNUM (tdep);
234 case 38: return I387_FSTAT_REGNUM (tdep);
235 case 39: return I387_MXCSR_REGNUM (tdep);
c6f4c129
JB
236 case 40: return I386_ES_REGNUM;
237 case 41: return I386_CS_REGNUM;
238 case 42: return I386_SS_REGNUM;
239 case 43: return I386_DS_REGNUM;
240 case 44: return I386_FS_REGNUM;
241 case 45: return I386_GS_REGNUM;
242 }
243
85540d8c 244 /* This will hopefully provoke a warning. */
d3f73121 245 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
85540d8c 246}
5716833c 247
fc338970 248\f
917317f4 249
fc338970
MK
250/* This is the variable that is set with "set disassembly-flavor", and
251 its legitimate values. */
53904c9e
AC
252static const char att_flavor[] = "att";
253static const char intel_flavor[] = "intel";
254static const char *valid_flavors[] =
c5aa993b 255{
c906108c
SS
256 att_flavor,
257 intel_flavor,
258 NULL
259};
53904c9e 260static const char *disassembly_flavor = att_flavor;
acd5c798 261\f
c906108c 262
acd5c798
MK
263/* Use the program counter to determine the contents and size of a
264 breakpoint instruction. Return a pointer to a string of bytes that
265 encode a breakpoint instruction, store the length of the string in
266 *LEN and optionally adjust *PC to point to the correct memory
267 location for inserting the breakpoint.
c906108c 268
acd5c798
MK
269 On the i386 we have a single breakpoint that fits in a single byte
270 and can be inserted anywhere.
c906108c 271
acd5c798 272 This function is 64-bit safe. */
63c0089f
MK
273
274static const gdb_byte *
67d57894 275i386_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pc, int *len)
c906108c 276{
63c0089f
MK
277 static gdb_byte break_insn[] = { 0xcc }; /* int 3 */
278
acd5c798
MK
279 *len = sizeof (break_insn);
280 return break_insn;
c906108c 281}
237fc4c9
PA
282\f
283/* Displaced instruction handling. */
284
1903f0e6
DE
285/* Skip the legacy instruction prefixes in INSN.
286 Not all prefixes are valid for any particular insn
287 but we needn't care, the insn will fault if it's invalid.
288 The result is a pointer to the first opcode byte,
289 or NULL if we run off the end of the buffer. */
290
291static gdb_byte *
292i386_skip_prefixes (gdb_byte *insn, size_t max_len)
293{
294 gdb_byte *end = insn + max_len;
295
296 while (insn < end)
297 {
298 switch (*insn)
299 {
300 case DATA_PREFIX_OPCODE:
301 case ADDR_PREFIX_OPCODE:
302 case CS_PREFIX_OPCODE:
303 case DS_PREFIX_OPCODE:
304 case ES_PREFIX_OPCODE:
305 case FS_PREFIX_OPCODE:
306 case GS_PREFIX_OPCODE:
307 case SS_PREFIX_OPCODE:
308 case LOCK_PREFIX_OPCODE:
309 case REPE_PREFIX_OPCODE:
310 case REPNE_PREFIX_OPCODE:
311 ++insn;
312 continue;
313 default:
314 return insn;
315 }
316 }
317
318 return NULL;
319}
237fc4c9
PA
320
321static int
1903f0e6 322i386_absolute_jmp_p (const gdb_byte *insn)
237fc4c9
PA
323{
324 /* jmp far (absolute address in operand) */
325 if (insn[0] == 0xea)
326 return 1;
327
328 if (insn[0] == 0xff)
329 {
330 /* jump near, absolute indirect (/4) */
331 if ((insn[1] & 0x38) == 0x20)
332 return 1;
333
334 /* jump far, absolute indirect (/5) */
335 if ((insn[1] & 0x38) == 0x28)
336 return 1;
337 }
338
339 return 0;
340}
341
342static int
1903f0e6 343i386_absolute_call_p (const gdb_byte *insn)
237fc4c9
PA
344{
345 /* call far, absolute */
346 if (insn[0] == 0x9a)
347 return 1;
348
349 if (insn[0] == 0xff)
350 {
351 /* Call near, absolute indirect (/2) */
352 if ((insn[1] & 0x38) == 0x10)
353 return 1;
354
355 /* Call far, absolute indirect (/3) */
356 if ((insn[1] & 0x38) == 0x18)
357 return 1;
358 }
359
360 return 0;
361}
362
363static int
1903f0e6 364i386_ret_p (const gdb_byte *insn)
237fc4c9
PA
365{
366 switch (insn[0])
367 {
368 case 0xc2: /* ret near, pop N bytes */
369 case 0xc3: /* ret near */
370 case 0xca: /* ret far, pop N bytes */
371 case 0xcb: /* ret far */
372 case 0xcf: /* iret */
373 return 1;
374
375 default:
376 return 0;
377 }
378}
379
380static int
1903f0e6 381i386_call_p (const gdb_byte *insn)
237fc4c9
PA
382{
383 if (i386_absolute_call_p (insn))
384 return 1;
385
386 /* call near, relative */
387 if (insn[0] == 0xe8)
388 return 1;
389
390 return 0;
391}
392
237fc4c9
PA
393/* Return non-zero if INSN is a system call, and set *LENGTHP to its
394 length in bytes. Otherwise, return zero. */
1903f0e6 395
237fc4c9 396static int
1903f0e6 397i386_syscall_p (const gdb_byte *insn, ULONGEST *lengthp)
237fc4c9
PA
398{
399 if (insn[0] == 0xcd)
400 {
401 *lengthp = 2;
402 return 1;
403 }
404
405 return 0;
406}
407
408/* Fix up the state of registers and memory after having single-stepped
409 a displaced instruction. */
1903f0e6 410
237fc4c9
PA
411void
412i386_displaced_step_fixup (struct gdbarch *gdbarch,
413 struct displaced_step_closure *closure,
414 CORE_ADDR from, CORE_ADDR to,
415 struct regcache *regs)
416{
417 /* The offset we applied to the instruction's address.
418 This could well be negative (when viewed as a signed 32-bit
419 value), but ULONGEST won't reflect that, so take care when
420 applying it. */
421 ULONGEST insn_offset = to - from;
422
423 /* Since we use simple_displaced_step_copy_insn, our closure is a
424 copy of the instruction. */
425 gdb_byte *insn = (gdb_byte *) closure;
1903f0e6
DE
426 /* The start of the insn, needed in case we see some prefixes. */
427 gdb_byte *insn_start = insn;
237fc4c9
PA
428
429 if (debug_displaced)
430 fprintf_unfiltered (gdb_stdlog,
431 "displaced: fixup (0x%s, 0x%s), "
432 "insn = 0x%02x 0x%02x ...\n",
433 paddr_nz (from), paddr_nz (to), insn[0], insn[1]);
434
435 /* The list of issues to contend with here is taken from
436 resume_execution in arch/i386/kernel/kprobes.c, Linux 2.6.20.
437 Yay for Free Software! */
438
439 /* Relocate the %eip, if necessary. */
440
1903f0e6
DE
441 /* The instruction recognizers we use assume any leading prefixes
442 have been skipped. */
443 {
444 /* This is the size of the buffer in closure. */
445 size_t max_insn_len = gdbarch_max_insn_length (gdbarch);
446 gdb_byte *opcode = i386_skip_prefixes (insn, max_insn_len);
447 /* If there are too many prefixes, just ignore the insn.
448 It will fault when run. */
449 if (opcode != NULL)
450 insn = opcode;
451 }
452
237fc4c9
PA
453 /* Except in the case of absolute or indirect jump or call
454 instructions, or a return instruction, the new eip is relative to
455 the displaced instruction; make it relative. Well, signal
456 handler returns don't need relocation either, but we use the
457 value of %eip to recognize those; see below. */
458 if (! i386_absolute_jmp_p (insn)
459 && ! i386_absolute_call_p (insn)
460 && ! i386_ret_p (insn))
461 {
462 ULONGEST orig_eip;
463 ULONGEST insn_len;
464
465 regcache_cooked_read_unsigned (regs, I386_EIP_REGNUM, &orig_eip);
466
467 /* A signal trampoline system call changes the %eip, resuming
468 execution of the main program after the signal handler has
469 returned. That makes them like 'return' instructions; we
470 shouldn't relocate %eip.
471
472 But most system calls don't, and we do need to relocate %eip.
473
474 Our heuristic for distinguishing these cases: if stepping
475 over the system call instruction left control directly after
476 the instruction, the we relocate --- control almost certainly
477 doesn't belong in the displaced copy. Otherwise, we assume
478 the instruction has put control where it belongs, and leave
479 it unrelocated. Goodness help us if there are PC-relative
480 system calls. */
481 if (i386_syscall_p (insn, &insn_len)
1903f0e6 482 && orig_eip != to + (insn - insn_start) + insn_len)
237fc4c9
PA
483 {
484 if (debug_displaced)
485 fprintf_unfiltered (gdb_stdlog,
486 "displaced: syscall changed %%eip; "
487 "not relocating\n");
488 }
489 else
490 {
491 ULONGEST eip = (orig_eip - insn_offset) & 0xffffffffUL;
492
1903f0e6
DE
493 /* If we just stepped over a breakpoint insn, we don't backup
494 the pc on purpose; this is to match behaviour without
495 stepping. */
237fc4c9
PA
496
497 regcache_cooked_write_unsigned (regs, I386_EIP_REGNUM, eip);
498
499 if (debug_displaced)
500 fprintf_unfiltered (gdb_stdlog,
501 "displaced: "
502 "relocated %%eip from 0x%s to 0x%s\n",
503 paddr_nz (orig_eip), paddr_nz (eip));
504 }
505 }
506
507 /* If the instruction was PUSHFL, then the TF bit will be set in the
508 pushed value, and should be cleared. We'll leave this for later,
509 since GDB already messes up the TF flag when stepping over a
510 pushfl. */
511
512 /* If the instruction was a call, the return address now atop the
513 stack is the address following the copied instruction. We need
514 to make it the address following the original instruction. */
515 if (i386_call_p (insn))
516 {
517 ULONGEST esp;
518 ULONGEST retaddr;
519 const ULONGEST retaddr_len = 4;
520
521 regcache_cooked_read_unsigned (regs, I386_ESP_REGNUM, &esp);
522 retaddr = read_memory_unsigned_integer (esp, retaddr_len);
523 retaddr = (retaddr - insn_offset) & 0xffffffffUL;
524 write_memory_unsigned_integer (esp, retaddr_len, retaddr);
525
526 if (debug_displaced)
527 fprintf_unfiltered (gdb_stdlog,
528 "displaced: relocated return addr at 0x%s "
529 "to 0x%s\n",
530 paddr_nz (esp),
531 paddr_nz (retaddr));
532 }
533}
fc338970 534\f
acd5c798
MK
535#ifdef I386_REGNO_TO_SYMMETRY
536#error "The Sequent Symmetry is no longer supported."
537#endif
c906108c 538
acd5c798
MK
539/* According to the System V ABI, the registers %ebp, %ebx, %edi, %esi
540 and %esp "belong" to the calling function. Therefore these
541 registers should be saved if they're going to be modified. */
c906108c 542
acd5c798
MK
543/* The maximum number of saved registers. This should include all
544 registers mentioned above, and %eip. */
a3386186 545#define I386_NUM_SAVED_REGS I386_NUM_GREGS
acd5c798
MK
546
547struct i386_frame_cache
c906108c 548{
acd5c798
MK
549 /* Base address. */
550 CORE_ADDR base;
772562f8 551 LONGEST sp_offset;
acd5c798
MK
552 CORE_ADDR pc;
553
fd13a04a
AC
554 /* Saved registers. */
555 CORE_ADDR saved_regs[I386_NUM_SAVED_REGS];
acd5c798 556 CORE_ADDR saved_sp;
e0c62198 557 int saved_sp_reg;
acd5c798
MK
558 int pc_in_eax;
559
560 /* Stack space reserved for local variables. */
561 long locals;
562};
563
564/* Allocate and initialize a frame cache. */
565
566static struct i386_frame_cache *
fd13a04a 567i386_alloc_frame_cache (void)
acd5c798
MK
568{
569 struct i386_frame_cache *cache;
570 int i;
571
572 cache = FRAME_OBSTACK_ZALLOC (struct i386_frame_cache);
573
574 /* Base address. */
575 cache->base = 0;
576 cache->sp_offset = -4;
577 cache->pc = 0;
578
fd13a04a
AC
579 /* Saved registers. We initialize these to -1 since zero is a valid
580 offset (that's where %ebp is supposed to be stored). */
581 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
582 cache->saved_regs[i] = -1;
acd5c798 583 cache->saved_sp = 0;
e0c62198 584 cache->saved_sp_reg = -1;
acd5c798
MK
585 cache->pc_in_eax = 0;
586
587 /* Frameless until proven otherwise. */
588 cache->locals = -1;
589
590 return cache;
591}
c906108c 592
acd5c798
MK
593/* If the instruction at PC is a jump, return the address of its
594 target. Otherwise, return PC. */
c906108c 595
acd5c798
MK
596static CORE_ADDR
597i386_follow_jump (CORE_ADDR pc)
598{
63c0089f 599 gdb_byte op;
acd5c798
MK
600 long delta = 0;
601 int data16 = 0;
c906108c 602
8defab1a 603 target_read_memory (pc, &op, 1);
acd5c798 604 if (op == 0x66)
c906108c 605 {
c906108c 606 data16 = 1;
acd5c798 607 op = read_memory_unsigned_integer (pc + 1, 1);
c906108c
SS
608 }
609
acd5c798 610 switch (op)
c906108c
SS
611 {
612 case 0xe9:
fc338970 613 /* Relative jump: if data16 == 0, disp32, else disp16. */
c906108c
SS
614 if (data16)
615 {
acd5c798 616 delta = read_memory_integer (pc + 2, 2);
c906108c 617
fc338970
MK
618 /* Include the size of the jmp instruction (including the
619 0x66 prefix). */
acd5c798 620 delta += 4;
c906108c
SS
621 }
622 else
623 {
acd5c798 624 delta = read_memory_integer (pc + 1, 4);
c906108c 625
acd5c798
MK
626 /* Include the size of the jmp instruction. */
627 delta += 5;
c906108c
SS
628 }
629 break;
630 case 0xeb:
fc338970 631 /* Relative jump, disp8 (ignore data16). */
acd5c798 632 delta = read_memory_integer (pc + data16 + 1, 1);
c906108c 633
acd5c798 634 delta += data16 + 2;
c906108c
SS
635 break;
636 }
c906108c 637
acd5c798
MK
638 return pc + delta;
639}
fc338970 640
acd5c798
MK
641/* Check whether PC points at a prologue for a function returning a
642 structure or union. If so, it updates CACHE and returns the
643 address of the first instruction after the code sequence that
644 removes the "hidden" argument from the stack or CURRENT_PC,
645 whichever is smaller. Otherwise, return PC. */
c906108c 646
acd5c798
MK
647static CORE_ADDR
648i386_analyze_struct_return (CORE_ADDR pc, CORE_ADDR current_pc,
649 struct i386_frame_cache *cache)
c906108c 650{
acd5c798
MK
651 /* Functions that return a structure or union start with:
652
653 popl %eax 0x58
654 xchgl %eax, (%esp) 0x87 0x04 0x24
655 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
656
657 (the System V compiler puts out the second `xchg' instruction,
658 and the assembler doesn't try to optimize it, so the 'sib' form
659 gets generated). This sequence is used to get the address of the
660 return buffer for a function that returns a structure. */
63c0089f
MK
661 static gdb_byte proto1[3] = { 0x87, 0x04, 0x24 };
662 static gdb_byte proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
663 gdb_byte buf[4];
664 gdb_byte op;
c906108c 665
acd5c798
MK
666 if (current_pc <= pc)
667 return pc;
668
8defab1a 669 target_read_memory (pc, &op, 1);
c906108c 670
acd5c798
MK
671 if (op != 0x58) /* popl %eax */
672 return pc;
c906108c 673
8defab1a 674 target_read_memory (pc + 1, buf, 4);
acd5c798
MK
675 if (memcmp (buf, proto1, 3) != 0 && memcmp (buf, proto2, 4) != 0)
676 return pc;
c906108c 677
acd5c798 678 if (current_pc == pc)
c906108c 679 {
acd5c798
MK
680 cache->sp_offset += 4;
681 return current_pc;
c906108c
SS
682 }
683
acd5c798 684 if (current_pc == pc + 1)
c906108c 685 {
acd5c798
MK
686 cache->pc_in_eax = 1;
687 return current_pc;
688 }
689
690 if (buf[1] == proto1[1])
691 return pc + 4;
692 else
693 return pc + 5;
694}
695
696static CORE_ADDR
697i386_skip_probe (CORE_ADDR pc)
698{
699 /* A function may start with
fc338970 700
acd5c798
MK
701 pushl constant
702 call _probe
703 addl $4, %esp
fc338970 704
acd5c798
MK
705 followed by
706
707 pushl %ebp
fc338970 708
acd5c798 709 etc. */
63c0089f
MK
710 gdb_byte buf[8];
711 gdb_byte op;
fc338970 712
8defab1a 713 target_read_memory (pc, &op, 1);
acd5c798
MK
714
715 if (op == 0x68 || op == 0x6a)
716 {
717 int delta;
c906108c 718
acd5c798
MK
719 /* Skip past the `pushl' instruction; it has either a one-byte or a
720 four-byte operand, depending on the opcode. */
c906108c 721 if (op == 0x68)
acd5c798 722 delta = 5;
c906108c 723 else
acd5c798 724 delta = 2;
c906108c 725
acd5c798
MK
726 /* Read the following 8 bytes, which should be `call _probe' (6
727 bytes) followed by `addl $4,%esp' (2 bytes). */
728 read_memory (pc + delta, buf, sizeof (buf));
c906108c 729 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
acd5c798 730 pc += delta + sizeof (buf);
c906108c
SS
731 }
732
acd5c798
MK
733 return pc;
734}
735
92dd43fa
MK
736/* GCC 4.1 and later, can put code in the prologue to realign the
737 stack pointer. Check whether PC points to such code, and update
738 CACHE accordingly. Return the first instruction after the code
739 sequence or CURRENT_PC, whichever is smaller. If we don't
740 recognize the code, return PC. */
741
742static CORE_ADDR
743i386_analyze_stack_align (CORE_ADDR pc, CORE_ADDR current_pc,
744 struct i386_frame_cache *cache)
745{
e0c62198
L
746 /* There are 2 code sequences to re-align stack before the frame
747 gets set up:
748
749 1. Use a caller-saved saved register:
750
751 leal 4(%esp), %reg
752 andl $-XXX, %esp
753 pushl -4(%reg)
754
755 2. Use a callee-saved saved register:
756
757 pushl %reg
758 leal 8(%esp), %reg
759 andl $-XXX, %esp
760 pushl -4(%reg)
761
762 "andl $-XXX, %esp" can be either 3 bytes or 6 bytes:
763
764 0x83 0xe4 0xf0 andl $-16, %esp
765 0x81 0xe4 0x00 0xff 0xff 0xff andl $-256, %esp
766 */
767
768 gdb_byte buf[14];
769 int reg;
770 int offset, offset_and;
771 static int regnums[8] = {
772 I386_EAX_REGNUM, /* %eax */
773 I386_ECX_REGNUM, /* %ecx */
774 I386_EDX_REGNUM, /* %edx */
775 I386_EBX_REGNUM, /* %ebx */
776 I386_ESP_REGNUM, /* %esp */
777 I386_EBP_REGNUM, /* %ebp */
778 I386_ESI_REGNUM, /* %esi */
779 I386_EDI_REGNUM /* %edi */
92dd43fa 780 };
92dd43fa 781
e0c62198
L
782 if (target_read_memory (pc, buf, sizeof buf))
783 return pc;
784
785 /* Check caller-saved saved register. The first instruction has
786 to be "leal 4(%esp), %reg". */
787 if (buf[0] == 0x8d && buf[2] == 0x24 && buf[3] == 0x4)
788 {
789 /* MOD must be binary 10 and R/M must be binary 100. */
790 if ((buf[1] & 0xc7) != 0x44)
791 return pc;
792
793 /* REG has register number. */
794 reg = (buf[1] >> 3) & 7;
795 offset = 4;
796 }
797 else
798 {
799 /* Check callee-saved saved register. The first instruction
800 has to be "pushl %reg". */
801 if ((buf[0] & 0xf8) != 0x50)
802 return pc;
803
804 /* Get register. */
805 reg = buf[0] & 0x7;
806
807 /* The next instruction has to be "leal 8(%esp), %reg". */
808 if (buf[1] != 0x8d || buf[3] != 0x24 || buf[4] != 0x8)
809 return pc;
810
811 /* MOD must be binary 10 and R/M must be binary 100. */
812 if ((buf[2] & 0xc7) != 0x44)
813 return pc;
814
815 /* REG has register number. Registers in pushl and leal have to
816 be the same. */
817 if (reg != ((buf[2] >> 3) & 7))
818 return pc;
819
820 offset = 5;
821 }
822
823 /* Rigister can't be %esp nor %ebp. */
824 if (reg == 4 || reg == 5)
825 return pc;
826
827 /* The next instruction has to be "andl $-XXX, %esp". */
828 if (buf[offset + 1] != 0xe4
829 || (buf[offset] != 0x81 && buf[offset] != 0x83))
830 return pc;
831
832 offset_and = offset;
833 offset += buf[offset] == 0x81 ? 6 : 3;
834
835 /* The next instruction has to be "pushl -4(%reg)". 8bit -4 is
836 0xfc. REG must be binary 110 and MOD must be binary 01. */
837 if (buf[offset] != 0xff
838 || buf[offset + 2] != 0xfc
839 || (buf[offset + 1] & 0xf8) != 0x70)
840 return pc;
841
842 /* R/M has register. Registers in leal and pushl have to be the
843 same. */
844 if (reg != (buf[offset + 1] & 7))
92dd43fa
MK
845 return pc;
846
e0c62198
L
847 if (current_pc > pc + offset_and)
848 cache->saved_sp_reg = regnums[reg];
92dd43fa 849
e0c62198 850 return min (pc + offset + 3, current_pc);
92dd43fa
MK
851}
852
37bdc87e 853/* Maximum instruction length we need to handle. */
237fc4c9 854#define I386_MAX_MATCHED_INSN_LEN 6
37bdc87e
MK
855
856/* Instruction description. */
857struct i386_insn
858{
859 size_t len;
237fc4c9
PA
860 gdb_byte insn[I386_MAX_MATCHED_INSN_LEN];
861 gdb_byte mask[I386_MAX_MATCHED_INSN_LEN];
37bdc87e
MK
862};
863
864/* Search for the instruction at PC in the list SKIP_INSNS. Return
865 the first instruction description that matches. Otherwise, return
866 NULL. */
867
868static struct i386_insn *
869i386_match_insn (CORE_ADDR pc, struct i386_insn *skip_insns)
870{
871 struct i386_insn *insn;
63c0089f 872 gdb_byte op;
37bdc87e 873
8defab1a 874 target_read_memory (pc, &op, 1);
37bdc87e
MK
875
876 for (insn = skip_insns; insn->len > 0; insn++)
877 {
878 if ((op & insn->mask[0]) == insn->insn[0])
879 {
237fc4c9 880 gdb_byte buf[I386_MAX_MATCHED_INSN_LEN - 1];
613e8135 881 int insn_matched = 1;
37bdc87e
MK
882 size_t i;
883
884 gdb_assert (insn->len > 1);
237fc4c9 885 gdb_assert (insn->len <= I386_MAX_MATCHED_INSN_LEN);
37bdc87e 886
8defab1a 887 target_read_memory (pc + 1, buf, insn->len - 1);
37bdc87e
MK
888 for (i = 1; i < insn->len; i++)
889 {
890 if ((buf[i - 1] & insn->mask[i]) != insn->insn[i])
613e8135 891 insn_matched = 0;
37bdc87e 892 }
613e8135
MK
893
894 if (insn_matched)
895 return insn;
37bdc87e
MK
896 }
897 }
898
899 return NULL;
900}
901
902/* Some special instructions that might be migrated by GCC into the
903 part of the prologue that sets up the new stack frame. Because the
904 stack frame hasn't been setup yet, no registers have been saved
905 yet, and only the scratch registers %eax, %ecx and %edx can be
906 touched. */
907
908struct i386_insn i386_frame_setup_skip_insns[] =
909{
910 /* Check for `movb imm8, r' and `movl imm32, r'.
911
912 ??? Should we handle 16-bit operand-sizes here? */
913
914 /* `movb imm8, %al' and `movb imm8, %ah' */
915 /* `movb imm8, %cl' and `movb imm8, %ch' */
916 { 2, { 0xb0, 0x00 }, { 0xfa, 0x00 } },
917 /* `movb imm8, %dl' and `movb imm8, %dh' */
918 { 2, { 0xb2, 0x00 }, { 0xfb, 0x00 } },
919 /* `movl imm32, %eax' and `movl imm32, %ecx' */
920 { 5, { 0xb8 }, { 0xfe } },
921 /* `movl imm32, %edx' */
922 { 5, { 0xba }, { 0xff } },
923
924 /* Check for `mov imm32, r32'. Note that there is an alternative
925 encoding for `mov m32, %eax'.
926
927 ??? Should we handle SIB adressing here?
928 ??? Should we handle 16-bit operand-sizes here? */
929
930 /* `movl m32, %eax' */
931 { 5, { 0xa1 }, { 0xff } },
932 /* `movl m32, %eax' and `mov; m32, %ecx' */
933 { 6, { 0x89, 0x05 }, {0xff, 0xf7 } },
934 /* `movl m32, %edx' */
935 { 6, { 0x89, 0x15 }, {0xff, 0xff } },
936
937 /* Check for `xorl r32, r32' and the equivalent `subl r32, r32'.
938 Because of the symmetry, there are actually two ways to encode
939 these instructions; opcode bytes 0x29 and 0x2b for `subl' and
940 opcode bytes 0x31 and 0x33 for `xorl'. */
941
942 /* `subl %eax, %eax' */
943 { 2, { 0x29, 0xc0 }, { 0xfd, 0xff } },
944 /* `subl %ecx, %ecx' */
945 { 2, { 0x29, 0xc9 }, { 0xfd, 0xff } },
946 /* `subl %edx, %edx' */
947 { 2, { 0x29, 0xd2 }, { 0xfd, 0xff } },
948 /* `xorl %eax, %eax' */
949 { 2, { 0x31, 0xc0 }, { 0xfd, 0xff } },
950 /* `xorl %ecx, %ecx' */
951 { 2, { 0x31, 0xc9 }, { 0xfd, 0xff } },
952 /* `xorl %edx, %edx' */
953 { 2, { 0x31, 0xd2 }, { 0xfd, 0xff } },
954 { 0 }
955};
956
e11481da
PM
957
958/* Check whether PC points to a no-op instruction. */
959static CORE_ADDR
960i386_skip_noop (CORE_ADDR pc)
961{
962 gdb_byte op;
963 int check = 1;
964
8defab1a 965 target_read_memory (pc, &op, 1);
e11481da
PM
966
967 while (check)
968 {
969 check = 0;
970 /* Ignore `nop' instruction. */
971 if (op == 0x90)
972 {
973 pc += 1;
8defab1a 974 target_read_memory (pc, &op, 1);
e11481da
PM
975 check = 1;
976 }
977 /* Ignore no-op instruction `mov %edi, %edi'.
978 Microsoft system dlls often start with
979 a `mov %edi,%edi' instruction.
980 The 5 bytes before the function start are
981 filled with `nop' instructions.
982 This pattern can be used for hot-patching:
983 The `mov %edi, %edi' instruction can be replaced by a
984 near jump to the location of the 5 `nop' instructions
985 which can be replaced by a 32-bit jump to anywhere
986 in the 32-bit address space. */
987
988 else if (op == 0x8b)
989 {
8defab1a 990 target_read_memory (pc + 1, &op, 1);
e11481da
PM
991 if (op == 0xff)
992 {
993 pc += 2;
8defab1a 994 target_read_memory (pc, &op, 1);
e11481da
PM
995 check = 1;
996 }
997 }
998 }
999 return pc;
1000}
1001
acd5c798
MK
1002/* Check whether PC points at a code that sets up a new stack frame.
1003 If so, it updates CACHE and returns the address of the first
37bdc87e
MK
1004 instruction after the sequence that sets up the frame or LIMIT,
1005 whichever is smaller. If we don't recognize the code, return PC. */
acd5c798
MK
1006
1007static CORE_ADDR
37bdc87e 1008i386_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR limit,
acd5c798
MK
1009 struct i386_frame_cache *cache)
1010{
37bdc87e 1011 struct i386_insn *insn;
63c0089f 1012 gdb_byte op;
26604a34 1013 int skip = 0;
acd5c798 1014
37bdc87e
MK
1015 if (limit <= pc)
1016 return limit;
acd5c798 1017
8defab1a 1018 target_read_memory (pc, &op, 1);
acd5c798 1019
c906108c 1020 if (op == 0x55) /* pushl %ebp */
c5aa993b 1021 {
acd5c798
MK
1022 /* Take into account that we've executed the `pushl %ebp' that
1023 starts this instruction sequence. */
fd13a04a 1024 cache->saved_regs[I386_EBP_REGNUM] = 0;
acd5c798 1025 cache->sp_offset += 4;
37bdc87e 1026 pc++;
acd5c798
MK
1027
1028 /* If that's all, return now. */
37bdc87e
MK
1029 if (limit <= pc)
1030 return limit;
26604a34 1031
b4632131 1032 /* Check for some special instructions that might be migrated by
37bdc87e
MK
1033 GCC into the prologue and skip them. At this point in the
1034 prologue, code should only touch the scratch registers %eax,
1035 %ecx and %edx, so while the number of posibilities is sheer,
1036 it is limited.
5daa5b4e 1037
26604a34
MK
1038 Make sure we only skip these instructions if we later see the
1039 `movl %esp, %ebp' that actually sets up the frame. */
37bdc87e 1040 while (pc + skip < limit)
26604a34 1041 {
37bdc87e
MK
1042 insn = i386_match_insn (pc + skip, i386_frame_setup_skip_insns);
1043 if (insn == NULL)
1044 break;
b4632131 1045
37bdc87e 1046 skip += insn->len;
26604a34
MK
1047 }
1048
37bdc87e
MK
1049 /* If that's all, return now. */
1050 if (limit <= pc + skip)
1051 return limit;
1052
8defab1a 1053 target_read_memory (pc + skip, &op, 1);
37bdc87e 1054
26604a34 1055 /* Check for `movl %esp, %ebp' -- can be written in two ways. */
acd5c798 1056 switch (op)
c906108c
SS
1057 {
1058 case 0x8b:
37bdc87e
MK
1059 if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xec)
1060 return pc;
c906108c
SS
1061 break;
1062 case 0x89:
37bdc87e
MK
1063 if (read_memory_unsigned_integer (pc + skip + 1, 1) != 0xe5)
1064 return pc;
c906108c
SS
1065 break;
1066 default:
37bdc87e 1067 return pc;
c906108c 1068 }
acd5c798 1069
26604a34
MK
1070 /* OK, we actually have a frame. We just don't know how large
1071 it is yet. Set its size to zero. We'll adjust it if
1072 necessary. We also now commit to skipping the special
1073 instructions mentioned before. */
acd5c798 1074 cache->locals = 0;
37bdc87e 1075 pc += (skip + 2);
acd5c798
MK
1076
1077 /* If that's all, return now. */
37bdc87e
MK
1078 if (limit <= pc)
1079 return limit;
acd5c798 1080
fc338970
MK
1081 /* Check for stack adjustment
1082
acd5c798 1083 subl $XXX, %esp
fc338970 1084
fd35795f 1085 NOTE: You can't subtract a 16-bit immediate from a 32-bit
fc338970 1086 reg, so we don't have to worry about a data16 prefix. */
8defab1a 1087 target_read_memory (pc, &op, 1);
c906108c
SS
1088 if (op == 0x83)
1089 {
fd35795f 1090 /* `subl' with 8-bit immediate. */
37bdc87e 1091 if (read_memory_unsigned_integer (pc + 1, 1) != 0xec)
fc338970 1092 /* Some instruction starting with 0x83 other than `subl'. */
37bdc87e 1093 return pc;
acd5c798 1094
37bdc87e
MK
1095 /* `subl' with signed 8-bit immediate (though it wouldn't
1096 make sense to be negative). */
1097 cache->locals = read_memory_integer (pc + 2, 1);
1098 return pc + 3;
c906108c
SS
1099 }
1100 else if (op == 0x81)
1101 {
fd35795f 1102 /* Maybe it is `subl' with a 32-bit immediate. */
37bdc87e 1103 if (read_memory_unsigned_integer (pc + 1, 1) != 0xec)
fc338970 1104 /* Some instruction starting with 0x81 other than `subl'. */
37bdc87e 1105 return pc;
acd5c798 1106
fd35795f 1107 /* It is `subl' with a 32-bit immediate. */
37bdc87e
MK
1108 cache->locals = read_memory_integer (pc + 2, 4);
1109 return pc + 6;
c906108c
SS
1110 }
1111 else
1112 {
acd5c798 1113 /* Some instruction other than `subl'. */
37bdc87e 1114 return pc;
c906108c
SS
1115 }
1116 }
37bdc87e 1117 else if (op == 0xc8) /* enter */
c906108c 1118 {
acd5c798
MK
1119 cache->locals = read_memory_unsigned_integer (pc + 1, 2);
1120 return pc + 4;
c906108c 1121 }
21d0e8a4 1122
acd5c798 1123 return pc;
21d0e8a4
MK
1124}
1125
acd5c798
MK
1126/* Check whether PC points at code that saves registers on the stack.
1127 If so, it updates CACHE and returns the address of the first
1128 instruction after the register saves or CURRENT_PC, whichever is
1129 smaller. Otherwise, return PC. */
6bff26de
MK
1130
1131static CORE_ADDR
acd5c798
MK
1132i386_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
1133 struct i386_frame_cache *cache)
6bff26de 1134{
99ab4326 1135 CORE_ADDR offset = 0;
63c0089f 1136 gdb_byte op;
99ab4326 1137 int i;
c0d1d883 1138
99ab4326
MK
1139 if (cache->locals > 0)
1140 offset -= cache->locals;
1141 for (i = 0; i < 8 && pc < current_pc; i++)
1142 {
8defab1a 1143 target_read_memory (pc, &op, 1);
99ab4326
MK
1144 if (op < 0x50 || op > 0x57)
1145 break;
0d17c81d 1146
99ab4326
MK
1147 offset -= 4;
1148 cache->saved_regs[op - 0x50] = offset;
1149 cache->sp_offset += 4;
1150 pc++;
6bff26de
MK
1151 }
1152
acd5c798 1153 return pc;
22797942
AC
1154}
1155
acd5c798
MK
1156/* Do a full analysis of the prologue at PC and update CACHE
1157 accordingly. Bail out early if CURRENT_PC is reached. Return the
1158 address where the analysis stopped.
ed84f6c1 1159
fc338970
MK
1160 We handle these cases:
1161
1162 The startup sequence can be at the start of the function, or the
1163 function can start with a branch to startup code at the end.
1164
1165 %ebp can be set up with either the 'enter' instruction, or "pushl
1166 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
1167 once used in the System V compiler).
1168
1169 Local space is allocated just below the saved %ebp by either the
fd35795f
MK
1170 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a
1171 16-bit unsigned argument for space to allocate, and the 'addl'
1172 instruction could have either a signed byte, or 32-bit immediate.
fc338970
MK
1173
1174 Next, the registers used by this function are pushed. With the
1175 System V compiler they will always be in the order: %edi, %esi,
1176 %ebx (and sometimes a harmless bug causes it to also save but not
1177 restore %eax); however, the code below is willing to see the pushes
1178 in any order, and will handle up to 8 of them.
1179
1180 If the setup sequence is at the end of the function, then the next
1181 instruction will be a branch back to the start. */
c906108c 1182
acd5c798
MK
1183static CORE_ADDR
1184i386_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
1185 struct i386_frame_cache *cache)
c906108c 1186{
e11481da 1187 pc = i386_skip_noop (pc);
acd5c798
MK
1188 pc = i386_follow_jump (pc);
1189 pc = i386_analyze_struct_return (pc, current_pc, cache);
1190 pc = i386_skip_probe (pc);
92dd43fa 1191 pc = i386_analyze_stack_align (pc, current_pc, cache);
acd5c798
MK
1192 pc = i386_analyze_frame_setup (pc, current_pc, cache);
1193 return i386_analyze_register_saves (pc, current_pc, cache);
c906108c
SS
1194}
1195
fc338970 1196/* Return PC of first real instruction. */
c906108c 1197
3a1e71e3 1198static CORE_ADDR
6093d2eb 1199i386_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
c906108c 1200{
63c0089f 1201 static gdb_byte pic_pat[6] =
acd5c798
MK
1202 {
1203 0xe8, 0, 0, 0, 0, /* call 0x0 */
1204 0x5b, /* popl %ebx */
c5aa993b 1205 };
acd5c798
MK
1206 struct i386_frame_cache cache;
1207 CORE_ADDR pc;
63c0089f 1208 gdb_byte op;
acd5c798 1209 int i;
c5aa993b 1210
acd5c798
MK
1211 cache.locals = -1;
1212 pc = i386_analyze_prologue (start_pc, 0xffffffff, &cache);
1213 if (cache.locals < 0)
1214 return start_pc;
c5aa993b 1215
acd5c798 1216 /* Found valid frame setup. */
c906108c 1217
fc338970
MK
1218 /* The native cc on SVR4 in -K PIC mode inserts the following code
1219 to get the address of the global offset table (GOT) into register
acd5c798
MK
1220 %ebx:
1221
fc338970
MK
1222 call 0x0
1223 popl %ebx
1224 movl %ebx,x(%ebp) (optional)
1225 addl y,%ebx
1226
c906108c
SS
1227 This code is with the rest of the prologue (at the end of the
1228 function), so we have to skip it to get to the first real
1229 instruction at the start of the function. */
c5aa993b 1230
c906108c
SS
1231 for (i = 0; i < 6; i++)
1232 {
8defab1a 1233 target_read_memory (pc + i, &op, 1);
c5aa993b 1234 if (pic_pat[i] != op)
c906108c
SS
1235 break;
1236 }
1237 if (i == 6)
1238 {
acd5c798
MK
1239 int delta = 6;
1240
8defab1a 1241 target_read_memory (pc + delta, &op, 1);
c906108c 1242
c5aa993b 1243 if (op == 0x89) /* movl %ebx, x(%ebp) */
c906108c 1244 {
acd5c798
MK
1245 op = read_memory_unsigned_integer (pc + delta + 1, 1);
1246
fc338970 1247 if (op == 0x5d) /* One byte offset from %ebp. */
acd5c798 1248 delta += 3;
fc338970 1249 else if (op == 0x9d) /* Four byte offset from %ebp. */
acd5c798 1250 delta += 6;
fc338970 1251 else /* Unexpected instruction. */
acd5c798
MK
1252 delta = 0;
1253
8defab1a 1254 target_read_memory (pc + delta, &op, 1);
c906108c 1255 }
acd5c798 1256
c5aa993b 1257 /* addl y,%ebx */
acd5c798 1258 if (delta > 0 && op == 0x81
d5d6fca5 1259 && read_memory_unsigned_integer (pc + delta + 1, 1) == 0xc3)
c906108c 1260 {
acd5c798 1261 pc += delta + 6;
c906108c
SS
1262 }
1263 }
c5aa993b 1264
e63bbc88
MK
1265 /* If the function starts with a branch (to startup code at the end)
1266 the last instruction should bring us back to the first
1267 instruction of the real code. */
1268 if (i386_follow_jump (start_pc) != start_pc)
1269 pc = i386_follow_jump (pc);
1270
1271 return pc;
c906108c
SS
1272}
1273
4309257c
PM
1274/* Check that the code pointed to by PC corresponds to a call to
1275 __main, skip it if so. Return PC otherwise. */
1276
1277CORE_ADDR
1278i386_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
1279{
1280 gdb_byte op;
1281
1282 target_read_memory (pc, &op, 1);
1283 if (op == 0xe8)
1284 {
1285 gdb_byte buf[4];
1286
1287 if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
1288 {
1289 /* Make sure address is computed correctly as a 32bit
1290 integer even if CORE_ADDR is 64 bit wide. */
1291 struct minimal_symbol *s;
1292 CORE_ADDR call_dest = pc + 5 + extract_signed_integer (buf, 4);
1293
1294 call_dest = call_dest & 0xffffffffU;
1295 s = lookup_minimal_symbol_by_pc (call_dest);
1296 if (s != NULL
1297 && SYMBOL_LINKAGE_NAME (s) != NULL
1298 && strcmp (SYMBOL_LINKAGE_NAME (s), "__main") == 0)
1299 pc += 5;
1300 }
1301 }
1302
1303 return pc;
1304}
1305
acd5c798 1306/* This function is 64-bit safe. */
93924b6b 1307
acd5c798
MK
1308static CORE_ADDR
1309i386_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
93924b6b 1310{
63c0089f 1311 gdb_byte buf[8];
acd5c798 1312
875f8d0e 1313 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
0dfff4cb 1314 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
93924b6b 1315}
acd5c798 1316\f
93924b6b 1317
acd5c798 1318/* Normal frames. */
c5aa993b 1319
acd5c798 1320static struct i386_frame_cache *
10458914 1321i386_frame_cache (struct frame_info *this_frame, void **this_cache)
a7769679 1322{
acd5c798 1323 struct i386_frame_cache *cache;
63c0089f 1324 gdb_byte buf[4];
acd5c798
MK
1325 int i;
1326
1327 if (*this_cache)
1328 return *this_cache;
1329
fd13a04a 1330 cache = i386_alloc_frame_cache ();
acd5c798
MK
1331 *this_cache = cache;
1332
1333 /* In principle, for normal frames, %ebp holds the frame pointer,
1334 which holds the base address for the current stack frame.
1335 However, for functions that don't need it, the frame pointer is
1336 optional. For these "frameless" functions the frame pointer is
1337 actually the frame pointer of the calling frame. Signal
1338 trampolines are just a special case of a "frameless" function.
1339 They (usually) share their frame pointer with the frame that was
1340 in progress when the signal occurred. */
1341
10458914 1342 get_frame_register (this_frame, I386_EBP_REGNUM, buf);
acd5c798
MK
1343 cache->base = extract_unsigned_integer (buf, 4);
1344 if (cache->base == 0)
1345 return cache;
1346
1347 /* For normal frames, %eip is stored at 4(%ebp). */
fd13a04a 1348 cache->saved_regs[I386_EIP_REGNUM] = 4;
acd5c798 1349
10458914 1350 cache->pc = get_frame_func (this_frame);
acd5c798 1351 if (cache->pc != 0)
10458914 1352 i386_analyze_prologue (cache->pc, get_frame_pc (this_frame), cache);
acd5c798 1353
e0c62198 1354 if (cache->saved_sp_reg != -1)
92dd43fa 1355 {
e0c62198
L
1356 /* Saved stack pointer has been saved. */
1357 get_frame_register (this_frame, cache->saved_sp_reg, buf);
92dd43fa
MK
1358 cache->saved_sp = extract_unsigned_integer(buf, 4);
1359 }
1360
acd5c798
MK
1361 if (cache->locals < 0)
1362 {
1363 /* We didn't find a valid frame, which means that CACHE->base
1364 currently holds the frame pointer for our calling frame. If
1365 we're at the start of a function, or somewhere half-way its
1366 prologue, the function's frame probably hasn't been fully
1367 setup yet. Try to reconstruct the base address for the stack
1368 frame by looking at the stack pointer. For truly "frameless"
1369 functions this might work too. */
1370
e0c62198 1371 if (cache->saved_sp_reg != -1)
92dd43fa
MK
1372 {
1373 /* We're halfway aligning the stack. */
1374 cache->base = ((cache->saved_sp - 4) & 0xfffffff0) - 4;
1375 cache->saved_regs[I386_EIP_REGNUM] = cache->saved_sp - 4;
1376
1377 /* This will be added back below. */
1378 cache->saved_regs[I386_EIP_REGNUM] -= cache->base;
1379 }
1380 else
1381 {
10458914 1382 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
92dd43fa
MK
1383 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
1384 }
acd5c798
MK
1385 }
1386
1387 /* Now that we have the base address for the stack frame we can
1388 calculate the value of %esp in the calling frame. */
92dd43fa
MK
1389 if (cache->saved_sp == 0)
1390 cache->saved_sp = cache->base + 8;
a7769679 1391
acd5c798
MK
1392 /* Adjust all the saved registers such that they contain addresses
1393 instead of offsets. */
1394 for (i = 0; i < I386_NUM_SAVED_REGS; i++)
fd13a04a
AC
1395 if (cache->saved_regs[i] != -1)
1396 cache->saved_regs[i] += cache->base;
acd5c798
MK
1397
1398 return cache;
a7769679
MK
1399}
1400
3a1e71e3 1401static void
10458914 1402i386_frame_this_id (struct frame_info *this_frame, void **this_cache,
acd5c798 1403 struct frame_id *this_id)
c906108c 1404{
10458914 1405 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798
MK
1406
1407 /* This marks the outermost frame. */
1408 if (cache->base == 0)
1409 return;
1410
3e210248 1411 /* See the end of i386_push_dummy_call. */
acd5c798
MK
1412 (*this_id) = frame_id_build (cache->base + 8, cache->pc);
1413}
1414
10458914
DJ
1415static struct value *
1416i386_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1417 int regnum)
acd5c798 1418{
10458914 1419 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798
MK
1420
1421 gdb_assert (regnum >= 0);
1422
1423 /* The System V ABI says that:
1424
1425 "The flags register contains the system flags, such as the
1426 direction flag and the carry flag. The direction flag must be
1427 set to the forward (that is, zero) direction before entry and
1428 upon exit from a function. Other user flags have no specified
1429 role in the standard calling sequence and are not preserved."
1430
1431 To guarantee the "upon exit" part of that statement we fake a
1432 saved flags register that has its direction flag cleared.
1433
1434 Note that GCC doesn't seem to rely on the fact that the direction
1435 flag is cleared after a function return; it always explicitly
1436 clears the flag before operations where it matters.
1437
1438 FIXME: kettenis/20030316: I'm not quite sure whether this is the
1439 right thing to do. The way we fake the flags register here makes
1440 it impossible to change it. */
1441
1442 if (regnum == I386_EFLAGS_REGNUM)
1443 {
10458914 1444 ULONGEST val;
c5aa993b 1445
10458914
DJ
1446 val = get_frame_register_unsigned (this_frame, regnum);
1447 val &= ~(1 << 10);
1448 return frame_unwind_got_constant (this_frame, regnum, val);
acd5c798 1449 }
1211c4e4 1450
acd5c798 1451 if (regnum == I386_EIP_REGNUM && cache->pc_in_eax)
10458914 1452 return frame_unwind_got_register (this_frame, regnum, I386_EAX_REGNUM);
acd5c798
MK
1453
1454 if (regnum == I386_ESP_REGNUM && cache->saved_sp)
10458914 1455 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
acd5c798 1456
fd13a04a 1457 if (regnum < I386_NUM_SAVED_REGS && cache->saved_regs[regnum] != -1)
10458914
DJ
1458 return frame_unwind_got_memory (this_frame, regnum,
1459 cache->saved_regs[regnum]);
fd13a04a 1460
10458914 1461 return frame_unwind_got_register (this_frame, regnum, regnum);
acd5c798
MK
1462}
1463
1464static const struct frame_unwind i386_frame_unwind =
1465{
1466 NORMAL_FRAME,
1467 i386_frame_this_id,
10458914
DJ
1468 i386_frame_prev_register,
1469 NULL,
1470 default_frame_sniffer
acd5c798 1471};
acd5c798
MK
1472\f
1473
1474/* Signal trampolines. */
1475
1476static struct i386_frame_cache *
10458914 1477i386_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
acd5c798
MK
1478{
1479 struct i386_frame_cache *cache;
10458914 1480 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
acd5c798 1481 CORE_ADDR addr;
63c0089f 1482 gdb_byte buf[4];
acd5c798
MK
1483
1484 if (*this_cache)
1485 return *this_cache;
1486
fd13a04a 1487 cache = i386_alloc_frame_cache ();
acd5c798 1488
10458914 1489 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
acd5c798
MK
1490 cache->base = extract_unsigned_integer (buf, 4) - 4;
1491
10458914 1492 addr = tdep->sigcontext_addr (this_frame);
a3386186
MK
1493 if (tdep->sc_reg_offset)
1494 {
1495 int i;
1496
1497 gdb_assert (tdep->sc_num_regs <= I386_NUM_SAVED_REGS);
1498
1499 for (i = 0; i < tdep->sc_num_regs; i++)
1500 if (tdep->sc_reg_offset[i] != -1)
fd13a04a 1501 cache->saved_regs[i] = addr + tdep->sc_reg_offset[i];
a3386186
MK
1502 }
1503 else
1504 {
fd13a04a
AC
1505 cache->saved_regs[I386_EIP_REGNUM] = addr + tdep->sc_pc_offset;
1506 cache->saved_regs[I386_ESP_REGNUM] = addr + tdep->sc_sp_offset;
a3386186 1507 }
acd5c798
MK
1508
1509 *this_cache = cache;
1510 return cache;
1511}
1512
1513static void
10458914 1514i386_sigtramp_frame_this_id (struct frame_info *this_frame, void **this_cache,
acd5c798
MK
1515 struct frame_id *this_id)
1516{
1517 struct i386_frame_cache *cache =
10458914 1518 i386_sigtramp_frame_cache (this_frame, this_cache);
acd5c798 1519
3e210248 1520 /* See the end of i386_push_dummy_call. */
10458914 1521 (*this_id) = frame_id_build (cache->base + 8, get_frame_pc (this_frame));
acd5c798
MK
1522}
1523
10458914
DJ
1524static struct value *
1525i386_sigtramp_frame_prev_register (struct frame_info *this_frame,
1526 void **this_cache, int regnum)
acd5c798
MK
1527{
1528 /* Make sure we've initialized the cache. */
10458914 1529 i386_sigtramp_frame_cache (this_frame, this_cache);
acd5c798 1530
10458914 1531 return i386_frame_prev_register (this_frame, this_cache, regnum);
c906108c 1532}
c0d1d883 1533
10458914
DJ
1534static int
1535i386_sigtramp_frame_sniffer (const struct frame_unwind *self,
1536 struct frame_info *this_frame,
1537 void **this_prologue_cache)
acd5c798 1538{
10458914 1539 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
acd5c798 1540
911bc6ee
MK
1541 /* We shouldn't even bother if we don't have a sigcontext_addr
1542 handler. */
1543 if (tdep->sigcontext_addr == NULL)
10458914 1544 return 0;
1c3545ae 1545
911bc6ee
MK
1546 if (tdep->sigtramp_p != NULL)
1547 {
10458914
DJ
1548 if (tdep->sigtramp_p (this_frame))
1549 return 1;
911bc6ee
MK
1550 }
1551
1552 if (tdep->sigtramp_start != 0)
1553 {
10458914 1554 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
1555
1556 gdb_assert (tdep->sigtramp_end != 0);
1557 if (pc >= tdep->sigtramp_start && pc < tdep->sigtramp_end)
10458914 1558 return 1;
911bc6ee 1559 }
acd5c798 1560
10458914 1561 return 0;
acd5c798 1562}
10458914
DJ
1563
1564static const struct frame_unwind i386_sigtramp_frame_unwind =
1565{
1566 SIGTRAMP_FRAME,
1567 i386_sigtramp_frame_this_id,
1568 i386_sigtramp_frame_prev_register,
1569 NULL,
1570 i386_sigtramp_frame_sniffer
1571};
acd5c798
MK
1572\f
1573
1574static CORE_ADDR
10458914 1575i386_frame_base_address (struct frame_info *this_frame, void **this_cache)
acd5c798 1576{
10458914 1577 struct i386_frame_cache *cache = i386_frame_cache (this_frame, this_cache);
acd5c798
MK
1578
1579 return cache->base;
1580}
1581
1582static const struct frame_base i386_frame_base =
1583{
1584 &i386_frame_unwind,
1585 i386_frame_base_address,
1586 i386_frame_base_address,
1587 i386_frame_base_address
1588};
1589
acd5c798 1590static struct frame_id
10458914 1591i386_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
acd5c798 1592{
acd5c798
MK
1593 CORE_ADDR fp;
1594
10458914 1595 fp = get_frame_register_unsigned (this_frame, I386_EBP_REGNUM);
acd5c798 1596
3e210248 1597 /* See the end of i386_push_dummy_call. */
10458914 1598 return frame_id_build (fp + 8, get_frame_pc (this_frame));
c0d1d883 1599}
fc338970 1600\f
c906108c 1601
fc338970
MK
1602/* Figure out where the longjmp will land. Slurp the args out of the
1603 stack. We expect the first arg to be a pointer to the jmp_buf
8201327c 1604 structure from which we extract the address that we will land at.
28bcfd30 1605 This address is copied into PC. This routine returns non-zero on
436675d3 1606 success. */
c906108c 1607
8201327c 1608static int
60ade65d 1609i386_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
c906108c 1610{
436675d3 1611 gdb_byte buf[4];
c906108c 1612 CORE_ADDR sp, jb_addr;
20a6ec49
MD
1613 struct gdbarch *gdbarch = get_frame_arch (frame);
1614 int jb_pc_offset = gdbarch_tdep (gdbarch)->jb_pc_offset;
c906108c 1615
8201327c
MK
1616 /* If JB_PC_OFFSET is -1, we have no way to find out where the
1617 longjmp will land. */
1618 if (jb_pc_offset == -1)
c906108c
SS
1619 return 0;
1620
436675d3
PA
1621 get_frame_register (frame, I386_ESP_REGNUM, buf);
1622 sp = extract_unsigned_integer (buf, 4);
1623 if (target_read_memory (sp + 4, buf, 4))
c906108c
SS
1624 return 0;
1625
436675d3
PA
1626 jb_addr = extract_unsigned_integer (buf, 4);
1627 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
8201327c 1628 return 0;
c906108c 1629
436675d3 1630 *pc = extract_unsigned_integer (buf, 4);
c906108c
SS
1631 return 1;
1632}
fc338970 1633\f
c906108c 1634
7ccc1c74
JM
1635/* Check whether TYPE must be 16-byte-aligned when passed as a
1636 function argument. 16-byte vectors, _Decimal128 and structures or
1637 unions containing such types must be 16-byte-aligned; other
1638 arguments are 4-byte-aligned. */
1639
1640static int
1641i386_16_byte_align_p (struct type *type)
1642{
1643 type = check_typedef (type);
1644 if ((TYPE_CODE (type) == TYPE_CODE_DECFLOAT
1645 || (TYPE_CODE (type) == TYPE_CODE_ARRAY && TYPE_VECTOR (type)))
1646 && TYPE_LENGTH (type) == 16)
1647 return 1;
1648 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
1649 return i386_16_byte_align_p (TYPE_TARGET_TYPE (type));
1650 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
1651 || TYPE_CODE (type) == TYPE_CODE_UNION)
1652 {
1653 int i;
1654 for (i = 0; i < TYPE_NFIELDS (type); i++)
1655 {
1656 if (i386_16_byte_align_p (TYPE_FIELD_TYPE (type, i)))
1657 return 1;
1658 }
1659 }
1660 return 0;
1661}
1662
3a1e71e3 1663static CORE_ADDR
7d9b040b 1664i386_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
6a65450a
AC
1665 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
1666 struct value **args, CORE_ADDR sp, int struct_return,
1667 CORE_ADDR struct_addr)
22f8ba57 1668{
63c0089f 1669 gdb_byte buf[4];
acd5c798 1670 int i;
7ccc1c74
JM
1671 int write_pass;
1672 int args_space = 0;
acd5c798 1673
7ccc1c74
JM
1674 /* Determine the total space required for arguments and struct
1675 return address in a first pass (allowing for 16-byte-aligned
1676 arguments), then push arguments in a second pass. */
1677
1678 for (write_pass = 0; write_pass < 2; write_pass++)
22f8ba57 1679 {
7ccc1c74
JM
1680 int args_space_used = 0;
1681 int have_16_byte_aligned_arg = 0;
1682
1683 if (struct_return)
1684 {
1685 if (write_pass)
1686 {
1687 /* Push value address. */
1688 store_unsigned_integer (buf, 4, struct_addr);
1689 write_memory (sp, buf, 4);
1690 args_space_used += 4;
1691 }
1692 else
1693 args_space += 4;
1694 }
1695
1696 for (i = 0; i < nargs; i++)
1697 {
1698 int len = TYPE_LENGTH (value_enclosing_type (args[i]));
acd5c798 1699
7ccc1c74
JM
1700 if (write_pass)
1701 {
1702 if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1703 args_space_used = align_up (args_space_used, 16);
acd5c798 1704
7ccc1c74
JM
1705 write_memory (sp + args_space_used,
1706 value_contents_all (args[i]), len);
1707 /* The System V ABI says that:
acd5c798 1708
7ccc1c74
JM
1709 "An argument's size is increased, if necessary, to make it a
1710 multiple of [32-bit] words. This may require tail padding,
1711 depending on the size of the argument."
22f8ba57 1712
7ccc1c74
JM
1713 This makes sure the stack stays word-aligned. */
1714 args_space_used += align_up (len, 4);
1715 }
1716 else
1717 {
1718 if (i386_16_byte_align_p (value_enclosing_type (args[i])))
1719 {
1720 args_space = align_up (args_space, 16);
1721 have_16_byte_aligned_arg = 1;
1722 }
1723 args_space += align_up (len, 4);
1724 }
1725 }
1726
1727 if (!write_pass)
1728 {
1729 if (have_16_byte_aligned_arg)
1730 args_space = align_up (args_space, 16);
1731 sp -= args_space;
1732 }
22f8ba57
MK
1733 }
1734
acd5c798
MK
1735 /* Store return address. */
1736 sp -= 4;
6a65450a 1737 store_unsigned_integer (buf, 4, bp_addr);
acd5c798
MK
1738 write_memory (sp, buf, 4);
1739
1740 /* Finally, update the stack pointer... */
1741 store_unsigned_integer (buf, 4, sp);
1742 regcache_cooked_write (regcache, I386_ESP_REGNUM, buf);
1743
1744 /* ...and fake a frame pointer. */
1745 regcache_cooked_write (regcache, I386_EBP_REGNUM, buf);
1746
3e210248
AC
1747 /* MarkK wrote: This "+ 8" is all over the place:
1748 (i386_frame_this_id, i386_sigtramp_frame_this_id,
10458914 1749 i386_dummy_id). It's there, since all frame unwinders for
3e210248 1750 a given target have to agree (within a certain margin) on the
a45ae3ed
UW
1751 definition of the stack address of a frame. Otherwise frame id
1752 comparison might not work correctly. Since DWARF2/GCC uses the
3e210248
AC
1753 stack address *before* the function call as a frame's CFA. On
1754 the i386, when %ebp is used as a frame pointer, the offset
1755 between the contents %ebp and the CFA as defined by GCC. */
1756 return sp + 8;
22f8ba57
MK
1757}
1758
1a309862
MK
1759/* These registers are used for returning integers (and on some
1760 targets also for returning `struct' and `union' values when their
ef9dff19 1761 size and alignment match an integer type). */
acd5c798
MK
1762#define LOW_RETURN_REGNUM I386_EAX_REGNUM /* %eax */
1763#define HIGH_RETURN_REGNUM I386_EDX_REGNUM /* %edx */
1a309862 1764
c5e656c1
MK
1765/* Read, for architecture GDBARCH, a function return value of TYPE
1766 from REGCACHE, and copy that into VALBUF. */
1a309862 1767
3a1e71e3 1768static void
c5e656c1 1769i386_extract_return_value (struct gdbarch *gdbarch, struct type *type,
63c0089f 1770 struct regcache *regcache, gdb_byte *valbuf)
c906108c 1771{
c5e656c1 1772 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1a309862 1773 int len = TYPE_LENGTH (type);
63c0089f 1774 gdb_byte buf[I386_MAX_REGISTER_SIZE];
1a309862 1775
1e8d0a7b 1776 if (TYPE_CODE (type) == TYPE_CODE_FLT)
c906108c 1777 {
5716833c 1778 if (tdep->st0_regnum < 0)
1a309862 1779 {
8a3fe4f8 1780 warning (_("Cannot find floating-point return value."));
1a309862 1781 memset (valbuf, 0, len);
ef9dff19 1782 return;
1a309862
MK
1783 }
1784
c6ba6f0d
MK
1785 /* Floating-point return values can be found in %st(0). Convert
1786 its contents to the desired type. This is probably not
1787 exactly how it would happen on the target itself, but it is
1788 the best we can do. */
acd5c798 1789 regcache_raw_read (regcache, I386_ST0_REGNUM, buf);
00f8375e 1790 convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
c906108c
SS
1791 }
1792 else
c5aa993b 1793 {
875f8d0e
UW
1794 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1795 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
d4f3574e
SS
1796
1797 if (len <= low_size)
00f8375e 1798 {
0818c12a 1799 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e
MK
1800 memcpy (valbuf, buf, len);
1801 }
d4f3574e
SS
1802 else if (len <= (low_size + high_size))
1803 {
0818c12a 1804 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e 1805 memcpy (valbuf, buf, low_size);
0818c12a 1806 regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
63c0089f 1807 memcpy (valbuf + low_size, buf, len - low_size);
d4f3574e
SS
1808 }
1809 else
8e65ff28 1810 internal_error (__FILE__, __LINE__,
e2e0b3e5 1811 _("Cannot extract return value of %d bytes long."), len);
c906108c
SS
1812 }
1813}
1814
c5e656c1
MK
1815/* Write, for architecture GDBARCH, a function return value of TYPE
1816 from VALBUF into REGCACHE. */
ef9dff19 1817
3a1e71e3 1818static void
c5e656c1 1819i386_store_return_value (struct gdbarch *gdbarch, struct type *type,
63c0089f 1820 struct regcache *regcache, const gdb_byte *valbuf)
ef9dff19 1821{
c5e656c1 1822 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
ef9dff19
MK
1823 int len = TYPE_LENGTH (type);
1824
1e8d0a7b 1825 if (TYPE_CODE (type) == TYPE_CODE_FLT)
ef9dff19 1826 {
3d7f4f49 1827 ULONGEST fstat;
63c0089f 1828 gdb_byte buf[I386_MAX_REGISTER_SIZE];
ccb945b8 1829
5716833c 1830 if (tdep->st0_regnum < 0)
ef9dff19 1831 {
8a3fe4f8 1832 warning (_("Cannot set floating-point return value."));
ef9dff19
MK
1833 return;
1834 }
1835
635b0cc1
MK
1836 /* Returning floating-point values is a bit tricky. Apart from
1837 storing the return value in %st(0), we have to simulate the
1838 state of the FPU at function return point. */
1839
c6ba6f0d
MK
1840 /* Convert the value found in VALBUF to the extended
1841 floating-point format used by the FPU. This is probably
1842 not exactly how it would happen on the target itself, but
1843 it is the best we can do. */
1844 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
acd5c798 1845 regcache_raw_write (regcache, I386_ST0_REGNUM, buf);
ccb945b8 1846
635b0cc1
MK
1847 /* Set the top of the floating-point register stack to 7. The
1848 actual value doesn't really matter, but 7 is what a normal
1849 function return would end up with if the program started out
1850 with a freshly initialized FPU. */
20a6ec49 1851 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
ccb945b8 1852 fstat |= (7 << 11);
20a6ec49 1853 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
ccb945b8 1854
635b0cc1
MK
1855 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1856 the floating-point register stack to 7, the appropriate value
1857 for the tag word is 0x3fff. */
20a6ec49 1858 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
ef9dff19
MK
1859 }
1860 else
1861 {
875f8d0e
UW
1862 int low_size = register_size (gdbarch, LOW_RETURN_REGNUM);
1863 int high_size = register_size (gdbarch, HIGH_RETURN_REGNUM);
ef9dff19
MK
1864
1865 if (len <= low_size)
3d7f4f49 1866 regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
ef9dff19
MK
1867 else if (len <= (low_size + high_size))
1868 {
3d7f4f49
MK
1869 regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
1870 regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
63c0089f 1871 len - low_size, valbuf + low_size);
ef9dff19
MK
1872 }
1873 else
8e65ff28 1874 internal_error (__FILE__, __LINE__,
e2e0b3e5 1875 _("Cannot store return value of %d bytes long."), len);
ef9dff19
MK
1876 }
1877}
fc338970 1878\f
ef9dff19 1879
8201327c
MK
1880/* This is the variable that is set with "set struct-convention", and
1881 its legitimate values. */
1882static const char default_struct_convention[] = "default";
1883static const char pcc_struct_convention[] = "pcc";
1884static const char reg_struct_convention[] = "reg";
1885static const char *valid_conventions[] =
1886{
1887 default_struct_convention,
1888 pcc_struct_convention,
1889 reg_struct_convention,
1890 NULL
1891};
1892static const char *struct_convention = default_struct_convention;
1893
0e4377e1
JB
1894/* Return non-zero if TYPE, which is assumed to be a structure,
1895 a union type, or an array type, should be returned in registers
1896 for architecture GDBARCH. */
c5e656c1 1897
8201327c 1898static int
c5e656c1 1899i386_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
8201327c 1900{
c5e656c1
MK
1901 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1902 enum type_code code = TYPE_CODE (type);
1903 int len = TYPE_LENGTH (type);
8201327c 1904
0e4377e1
JB
1905 gdb_assert (code == TYPE_CODE_STRUCT
1906 || code == TYPE_CODE_UNION
1907 || code == TYPE_CODE_ARRAY);
c5e656c1
MK
1908
1909 if (struct_convention == pcc_struct_convention
1910 || (struct_convention == default_struct_convention
1911 && tdep->struct_return == pcc_struct_return))
1912 return 0;
1913
9edde48e
MK
1914 /* Structures consisting of a single `float', `double' or 'long
1915 double' member are returned in %st(0). */
1916 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1917 {
1918 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
1919 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1920 return (len == 4 || len == 8 || len == 12);
1921 }
1922
c5e656c1
MK
1923 return (len == 1 || len == 2 || len == 4 || len == 8);
1924}
1925
1926/* Determine, for architecture GDBARCH, how a return value of TYPE
1927 should be returned. If it is supposed to be returned in registers,
1928 and READBUF is non-zero, read the appropriate value from REGCACHE,
1929 and copy it into READBUF. If WRITEBUF is non-zero, write the value
1930 from WRITEBUF into REGCACHE. */
1931
1932static enum return_value_convention
c055b101
CV
1933i386_return_value (struct gdbarch *gdbarch, struct type *func_type,
1934 struct type *type, struct regcache *regcache,
1935 gdb_byte *readbuf, const gdb_byte *writebuf)
c5e656c1
MK
1936{
1937 enum type_code code = TYPE_CODE (type);
1938
5daa78cc
TJB
1939 if (((code == TYPE_CODE_STRUCT
1940 || code == TYPE_CODE_UNION
1941 || code == TYPE_CODE_ARRAY)
1942 && !i386_reg_struct_return_p (gdbarch, type))
1943 /* 128-bit decimal float uses the struct return convention. */
1944 || (code == TYPE_CODE_DECFLOAT && TYPE_LENGTH (type) == 16))
31db7b6c
MK
1945 {
1946 /* The System V ABI says that:
1947
1948 "A function that returns a structure or union also sets %eax
1949 to the value of the original address of the caller's area
1950 before it returns. Thus when the caller receives control
1951 again, the address of the returned object resides in register
1952 %eax and can be used to access the object."
1953
1954 So the ABI guarantees that we can always find the return
1955 value just after the function has returned. */
1956
0e4377e1
JB
1957 /* Note that the ABI doesn't mention functions returning arrays,
1958 which is something possible in certain languages such as Ada.
1959 In this case, the value is returned as if it was wrapped in
1960 a record, so the convention applied to records also applies
1961 to arrays. */
1962
31db7b6c
MK
1963 if (readbuf)
1964 {
1965 ULONGEST addr;
1966
1967 regcache_raw_read_unsigned (regcache, I386_EAX_REGNUM, &addr);
1968 read_memory (addr, readbuf, TYPE_LENGTH (type));
1969 }
1970
1971 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
1972 }
c5e656c1
MK
1973
1974 /* This special case is for structures consisting of a single
9edde48e
MK
1975 `float', `double' or 'long double' member. These structures are
1976 returned in %st(0). For these structures, we call ourselves
1977 recursively, changing TYPE into the type of the first member of
1978 the structure. Since that should work for all structures that
1979 have only one member, we don't bother to check the member's type
1980 here. */
c5e656c1
MK
1981 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
1982 {
1983 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
c055b101
CV
1984 return i386_return_value (gdbarch, func_type, type, regcache,
1985 readbuf, writebuf);
c5e656c1
MK
1986 }
1987
1988 if (readbuf)
1989 i386_extract_return_value (gdbarch, type, regcache, readbuf);
1990 if (writebuf)
1991 i386_store_return_value (gdbarch, type, regcache, writebuf);
8201327c 1992
c5e656c1 1993 return RETURN_VALUE_REGISTER_CONVENTION;
8201327c
MK
1994}
1995\f
1996
5ae96ec1
MK
1997/* Type for %eflags. */
1998struct type *i386_eflags_type;
1999
794ac428 2000/* Type for %mxcsr. */
878d9193 2001struct type *i386_mxcsr_type;
5ae96ec1
MK
2002
2003/* Construct types for ISA-specific registers. */
2004static void
2005i386_init_types (void)
2006{
2007 struct type *type;
2008
2009 type = init_flags_type ("builtin_type_i386_eflags", 4);
2010 append_flags_type_flag (type, 0, "CF");
2011 append_flags_type_flag (type, 1, NULL);
2012 append_flags_type_flag (type, 2, "PF");
2013 append_flags_type_flag (type, 4, "AF");
2014 append_flags_type_flag (type, 6, "ZF");
2015 append_flags_type_flag (type, 7, "SF");
2016 append_flags_type_flag (type, 8, "TF");
2017 append_flags_type_flag (type, 9, "IF");
2018 append_flags_type_flag (type, 10, "DF");
2019 append_flags_type_flag (type, 11, "OF");
2020 append_flags_type_flag (type, 14, "NT");
2021 append_flags_type_flag (type, 16, "RF");
2022 append_flags_type_flag (type, 17, "VM");
2023 append_flags_type_flag (type, 18, "AC");
2024 append_flags_type_flag (type, 19, "VIF");
2025 append_flags_type_flag (type, 20, "VIP");
2026 append_flags_type_flag (type, 21, "ID");
2027 i386_eflags_type = type;
21b4b2f2 2028
878d9193
MK
2029 type = init_flags_type ("builtin_type_i386_mxcsr", 4);
2030 append_flags_type_flag (type, 0, "IE");
2031 append_flags_type_flag (type, 1, "DE");
2032 append_flags_type_flag (type, 2, "ZE");
2033 append_flags_type_flag (type, 3, "OE");
2034 append_flags_type_flag (type, 4, "UE");
2035 append_flags_type_flag (type, 5, "PE");
2036 append_flags_type_flag (type, 6, "DAZ");
2037 append_flags_type_flag (type, 7, "IM");
2038 append_flags_type_flag (type, 8, "DM");
2039 append_flags_type_flag (type, 9, "ZM");
2040 append_flags_type_flag (type, 10, "OM");
2041 append_flags_type_flag (type, 11, "UM");
2042 append_flags_type_flag (type, 12, "PM");
2043 append_flags_type_flag (type, 15, "FZ");
2044 i386_mxcsr_type = type;
21b4b2f2
JB
2045}
2046
794ac428
UW
2047/* Construct vector type for MMX registers. */
2048struct type *
2049i386_mmx_type (struct gdbarch *gdbarch)
2050{
2051 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2052
2053 if (!tdep->i386_mmx_type)
2054 {
df4df182
UW
2055 const struct builtin_type *bt = builtin_type (gdbarch);
2056
794ac428
UW
2057 /* The type we're building is this: */
2058#if 0
2059 union __gdb_builtin_type_vec64i
2060 {
2061 int64_t uint64;
2062 int32_t v2_int32[2];
2063 int16_t v4_int16[4];
2064 int8_t v8_int8[8];
2065 };
2066#endif
2067
2068 struct type *t;
2069
2070 t = init_composite_type ("__gdb_builtin_type_vec64i", TYPE_CODE_UNION);
df4df182
UW
2071
2072 append_composite_type_field (t, "uint64", bt->builtin_int64);
794ac428 2073 append_composite_type_field (t, "v2_int32",
df4df182 2074 init_vector_type (bt->builtin_int32, 2));
794ac428 2075 append_composite_type_field (t, "v4_int16",
df4df182 2076 init_vector_type (bt->builtin_int16, 4));
794ac428 2077 append_composite_type_field (t, "v8_int8",
df4df182 2078 init_vector_type (bt->builtin_int8, 8));
794ac428 2079
876cecd0 2080 TYPE_VECTOR (t) = 1;
794ac428
UW
2081 TYPE_NAME (t) = "builtin_type_vec64i";
2082 tdep->i386_mmx_type = t;
2083 }
2084
2085 return tdep->i386_mmx_type;
2086}
2087
2088struct type *
2089i386_sse_type (struct gdbarch *gdbarch)
2090{
2091 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2092
2093 if (!tdep->i386_sse_type)
2094 {
df4df182
UW
2095 const struct builtin_type *bt = builtin_type (gdbarch);
2096
794ac428
UW
2097 /* The type we're building is this: */
2098#if 0
2099 union __gdb_builtin_type_vec128i
2100 {
2101 int128_t uint128;
2102 int64_t v2_int64[2];
2103 int32_t v4_int32[4];
2104 int16_t v8_int16[8];
2105 int8_t v16_int8[16];
2106 double v2_double[2];
2107 float v4_float[4];
2108 };
2109#endif
2110
2111 struct type *t;
2112
2113 t = init_composite_type ("__gdb_builtin_type_vec128i", TYPE_CODE_UNION);
2114 append_composite_type_field (t, "v4_float",
df4df182 2115 init_vector_type (bt->builtin_float, 4));
794ac428 2116 append_composite_type_field (t, "v2_double",
df4df182 2117 init_vector_type (bt->builtin_double, 2));
794ac428 2118 append_composite_type_field (t, "v16_int8",
df4df182 2119 init_vector_type (bt->builtin_int8, 16));
794ac428 2120 append_composite_type_field (t, "v8_int16",
df4df182 2121 init_vector_type (bt->builtin_int16, 8));
794ac428 2122 append_composite_type_field (t, "v4_int32",
df4df182 2123 init_vector_type (bt->builtin_int32, 4));
794ac428 2124 append_composite_type_field (t, "v2_int64",
df4df182
UW
2125 init_vector_type (bt->builtin_int64, 2));
2126 append_composite_type_field (t, "uint128", bt->builtin_int128);
794ac428 2127
876cecd0 2128 TYPE_VECTOR (t) = 1;
794ac428
UW
2129 TYPE_NAME (t) = "builtin_type_vec128i";
2130 tdep->i386_sse_type = t;
2131 }
2132
2133 return tdep->i386_sse_type;
2134}
2135
d7a0d72c
MK
2136/* Return the GDB type object for the "standard" data type of data in
2137 register REGNUM. Perhaps %esi and %edi should go here, but
2138 potentially they could be used for things other than address. */
2139
3a1e71e3 2140static struct type *
4e259f09 2141i386_register_type (struct gdbarch *gdbarch, int regnum)
d7a0d72c 2142{
ab533587 2143 if (regnum == I386_EIP_REGNUM)
0dfff4cb 2144 return builtin_type (gdbarch)->builtin_func_ptr;
ab533587 2145
5ae96ec1
MK
2146 if (regnum == I386_EFLAGS_REGNUM)
2147 return i386_eflags_type;
2148
ab533587 2149 if (regnum == I386_EBP_REGNUM || regnum == I386_ESP_REGNUM)
0dfff4cb 2150 return builtin_type (gdbarch)->builtin_data_ptr;
d7a0d72c 2151
20a6ec49 2152 if (i386_fp_regnum_p (gdbarch, regnum))
c6ba6f0d 2153 return builtin_type_i387_ext;
d7a0d72c 2154
878d9193 2155 if (i386_mmx_regnum_p (gdbarch, regnum))
794ac428 2156 return i386_mmx_type (gdbarch);
878d9193 2157
5716833c 2158 if (i386_sse_regnum_p (gdbarch, regnum))
794ac428 2159 return i386_sse_type (gdbarch);
d7a0d72c 2160
20a6ec49 2161 if (regnum == I387_MXCSR_REGNUM (gdbarch_tdep (gdbarch)))
878d9193
MK
2162 return i386_mxcsr_type;
2163
0dfff4cb 2164 return builtin_type (gdbarch)->builtin_int;
d7a0d72c
MK
2165}
2166
28fc6740 2167/* Map a cooked register onto a raw register or memory. For the i386,
acd5c798 2168 the MMX registers need to be mapped onto floating point registers. */
28fc6740
AC
2169
2170static int
c86c27af 2171i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
28fc6740 2172{
5716833c
MK
2173 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
2174 int mmxreg, fpreg;
28fc6740
AC
2175 ULONGEST fstat;
2176 int tos;
c86c27af 2177
5716833c 2178 mmxreg = regnum - tdep->mm0_regnum;
20a6ec49 2179 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
28fc6740 2180 tos = (fstat >> 11) & 0x7;
5716833c
MK
2181 fpreg = (mmxreg + tos) % 8;
2182
20a6ec49 2183 return (I387_ST0_REGNUM (tdep) + fpreg);
28fc6740
AC
2184}
2185
2186static void
2187i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
42835c2b 2188 int regnum, gdb_byte *buf)
28fc6740 2189{
5716833c 2190 if (i386_mmx_regnum_p (gdbarch, regnum))
28fc6740 2191 {
63c0089f 2192 gdb_byte mmx_buf[MAX_REGISTER_SIZE];
c86c27af
MK
2193 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2194
28fc6740 2195 /* Extract (always little endian). */
c86c27af 2196 regcache_raw_read (regcache, fpnum, mmx_buf);
f837910f 2197 memcpy (buf, mmx_buf, register_size (gdbarch, regnum));
28fc6740
AC
2198 }
2199 else
2200 regcache_raw_read (regcache, regnum, buf);
2201}
2202
2203static void
2204i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
42835c2b 2205 int regnum, const gdb_byte *buf)
28fc6740 2206{
5716833c 2207 if (i386_mmx_regnum_p (gdbarch, regnum))
28fc6740 2208 {
63c0089f 2209 gdb_byte mmx_buf[MAX_REGISTER_SIZE];
c86c27af
MK
2210 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
2211
28fc6740
AC
2212 /* Read ... */
2213 regcache_raw_read (regcache, fpnum, mmx_buf);
2214 /* ... Modify ... (always little endian). */
f837910f 2215 memcpy (mmx_buf, buf, register_size (gdbarch, regnum));
28fc6740
AC
2216 /* ... Write. */
2217 regcache_raw_write (regcache, fpnum, mmx_buf);
2218 }
2219 else
2220 regcache_raw_write (regcache, regnum, buf);
2221}
ff2e87ac
AC
2222\f
2223
ff2e87ac
AC
2224/* Return the register number of the register allocated by GCC after
2225 REGNUM, or -1 if there is no such register. */
2226
2227static int
2228i386_next_regnum (int regnum)
2229{
2230 /* GCC allocates the registers in the order:
2231
2232 %eax, %edx, %ecx, %ebx, %esi, %edi, %ebp, %esp, ...
2233
2234 Since storing a variable in %esp doesn't make any sense we return
2235 -1 for %ebp and for %esp itself. */
2236 static int next_regnum[] =
2237 {
2238 I386_EDX_REGNUM, /* Slot for %eax. */
2239 I386_EBX_REGNUM, /* Slot for %ecx. */
2240 I386_ECX_REGNUM, /* Slot for %edx. */
2241 I386_ESI_REGNUM, /* Slot for %ebx. */
2242 -1, -1, /* Slots for %esp and %ebp. */
2243 I386_EDI_REGNUM, /* Slot for %esi. */
2244 I386_EBP_REGNUM /* Slot for %edi. */
2245 };
2246
de5b9bb9 2247 if (regnum >= 0 && regnum < sizeof (next_regnum) / sizeof (next_regnum[0]))
ff2e87ac 2248 return next_regnum[regnum];
28fc6740 2249
ff2e87ac
AC
2250 return -1;
2251}
2252
2253/* Return nonzero if a value of type TYPE stored in register REGNUM
2254 needs any special handling. */
d7a0d72c 2255
3a1e71e3 2256static int
0abe36f5 2257i386_convert_register_p (struct gdbarch *gdbarch, int regnum, struct type *type)
d7a0d72c 2258{
de5b9bb9
MK
2259 int len = TYPE_LENGTH (type);
2260
ff2e87ac
AC
2261 /* Values may be spread across multiple registers. Most debugging
2262 formats aren't expressive enough to specify the locations, so
2263 some heuristics is involved. Right now we only handle types that
de5b9bb9
MK
2264 have a length that is a multiple of the word size, since GCC
2265 doesn't seem to put any other types into registers. */
2266 if (len > 4 && len % 4 == 0)
2267 {
2268 int last_regnum = regnum;
2269
2270 while (len > 4)
2271 {
2272 last_regnum = i386_next_regnum (last_regnum);
2273 len -= 4;
2274 }
2275
2276 if (last_regnum != -1)
2277 return 1;
2278 }
ff2e87ac 2279
0abe36f5 2280 return i387_convert_register_p (gdbarch, regnum, type);
d7a0d72c
MK
2281}
2282
ff2e87ac
AC
2283/* Read a value of type TYPE from register REGNUM in frame FRAME, and
2284 return its contents in TO. */
ac27f131 2285
3a1e71e3 2286static void
ff2e87ac 2287i386_register_to_value (struct frame_info *frame, int regnum,
42835c2b 2288 struct type *type, gdb_byte *to)
ac27f131 2289{
20a6ec49 2290 struct gdbarch *gdbarch = get_frame_arch (frame);
de5b9bb9 2291 int len = TYPE_LENGTH (type);
de5b9bb9 2292
ff2e87ac
AC
2293 /* FIXME: kettenis/20030609: What should we do if REGNUM isn't
2294 available in FRAME (i.e. if it wasn't saved)? */
3d261580 2295
20a6ec49 2296 if (i386_fp_regnum_p (gdbarch, regnum))
8d7f6b4a 2297 {
d532c08f
MK
2298 i387_register_to_value (frame, regnum, type, to);
2299 return;
8d7f6b4a 2300 }
ff2e87ac 2301
fd35795f 2302 /* Read a value spread across multiple registers. */
de5b9bb9
MK
2303
2304 gdb_assert (len > 4 && len % 4 == 0);
3d261580 2305
de5b9bb9
MK
2306 while (len > 0)
2307 {
2308 gdb_assert (regnum != -1);
20a6ec49 2309 gdb_assert (register_size (gdbarch, regnum) == 4);
d532c08f 2310
42835c2b 2311 get_frame_register (frame, regnum, to);
de5b9bb9
MK
2312 regnum = i386_next_regnum (regnum);
2313 len -= 4;
42835c2b 2314 to += 4;
de5b9bb9 2315 }
ac27f131
MK
2316}
2317
ff2e87ac
AC
2318/* Write the contents FROM of a value of type TYPE into register
2319 REGNUM in frame FRAME. */
ac27f131 2320
3a1e71e3 2321static void
ff2e87ac 2322i386_value_to_register (struct frame_info *frame, int regnum,
42835c2b 2323 struct type *type, const gdb_byte *from)
ac27f131 2324{
de5b9bb9 2325 int len = TYPE_LENGTH (type);
de5b9bb9 2326
20a6ec49 2327 if (i386_fp_regnum_p (get_frame_arch (frame), regnum))
c6ba6f0d 2328 {
d532c08f
MK
2329 i387_value_to_register (frame, regnum, type, from);
2330 return;
2331 }
3d261580 2332
fd35795f 2333 /* Write a value spread across multiple registers. */
de5b9bb9
MK
2334
2335 gdb_assert (len > 4 && len % 4 == 0);
ff2e87ac 2336
de5b9bb9
MK
2337 while (len > 0)
2338 {
2339 gdb_assert (regnum != -1);
875f8d0e 2340 gdb_assert (register_size (get_frame_arch (frame), regnum) == 4);
d532c08f 2341
42835c2b 2342 put_frame_register (frame, regnum, from);
de5b9bb9
MK
2343 regnum = i386_next_regnum (regnum);
2344 len -= 4;
42835c2b 2345 from += 4;
de5b9bb9 2346 }
ac27f131 2347}
ff2e87ac 2348\f
7fdafb5a
MK
2349/* Supply register REGNUM from the buffer specified by GREGS and LEN
2350 in the general-purpose register set REGSET to register cache
2351 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
ff2e87ac 2352
20187ed5 2353void
473f17b0
MK
2354i386_supply_gregset (const struct regset *regset, struct regcache *regcache,
2355 int regnum, const void *gregs, size_t len)
2356{
9ea75c57 2357 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
156cdbee 2358 const gdb_byte *regs = gregs;
473f17b0
MK
2359 int i;
2360
2361 gdb_assert (len == tdep->sizeof_gregset);
2362
2363 for (i = 0; i < tdep->gregset_num_regs; i++)
2364 {
2365 if ((regnum == i || regnum == -1)
2366 && tdep->gregset_reg_offset[i] != -1)
2367 regcache_raw_supply (regcache, i, regs + tdep->gregset_reg_offset[i]);
2368 }
2369}
2370
7fdafb5a
MK
2371/* Collect register REGNUM from the register cache REGCACHE and store
2372 it in the buffer specified by GREGS and LEN as described by the
2373 general-purpose register set REGSET. If REGNUM is -1, do this for
2374 all registers in REGSET. */
2375
2376void
2377i386_collect_gregset (const struct regset *regset,
2378 const struct regcache *regcache,
2379 int regnum, void *gregs, size_t len)
2380{
2381 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
156cdbee 2382 gdb_byte *regs = gregs;
7fdafb5a
MK
2383 int i;
2384
2385 gdb_assert (len == tdep->sizeof_gregset);
2386
2387 for (i = 0; i < tdep->gregset_num_regs; i++)
2388 {
2389 if ((regnum == i || regnum == -1)
2390 && tdep->gregset_reg_offset[i] != -1)
2391 regcache_raw_collect (regcache, i, regs + tdep->gregset_reg_offset[i]);
2392 }
2393}
2394
2395/* Supply register REGNUM from the buffer specified by FPREGS and LEN
2396 in the floating-point register set REGSET to register cache
2397 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
473f17b0
MK
2398
2399static void
2400i386_supply_fpregset (const struct regset *regset, struct regcache *regcache,
2401 int regnum, const void *fpregs, size_t len)
2402{
9ea75c57 2403 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
473f17b0 2404
66a72d25
MK
2405 if (len == I387_SIZEOF_FXSAVE)
2406 {
2407 i387_supply_fxsave (regcache, regnum, fpregs);
2408 return;
2409 }
2410
473f17b0
MK
2411 gdb_assert (len == tdep->sizeof_fpregset);
2412 i387_supply_fsave (regcache, regnum, fpregs);
2413}
8446b36a 2414
2f305df1
MK
2415/* Collect register REGNUM from the register cache REGCACHE and store
2416 it in the buffer specified by FPREGS and LEN as described by the
2417 floating-point register set REGSET. If REGNUM is -1, do this for
2418 all registers in REGSET. */
7fdafb5a
MK
2419
2420static void
2421i386_collect_fpregset (const struct regset *regset,
2422 const struct regcache *regcache,
2423 int regnum, void *fpregs, size_t len)
2424{
2425 const struct gdbarch_tdep *tdep = gdbarch_tdep (regset->arch);
2426
2427 if (len == I387_SIZEOF_FXSAVE)
2428 {
2429 i387_collect_fxsave (regcache, regnum, fpregs);
2430 return;
2431 }
2432
2433 gdb_assert (len == tdep->sizeof_fpregset);
2434 i387_collect_fsave (regcache, regnum, fpregs);
2435}
2436
8446b36a
MK
2437/* Return the appropriate register set for the core section identified
2438 by SECT_NAME and SECT_SIZE. */
2439
2440const struct regset *
2441i386_regset_from_core_section (struct gdbarch *gdbarch,
2442 const char *sect_name, size_t sect_size)
2443{
2444 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2445
2446 if (strcmp (sect_name, ".reg") == 0 && sect_size == tdep->sizeof_gregset)
2447 {
2448 if (tdep->gregset == NULL)
7fdafb5a
MK
2449 tdep->gregset = regset_alloc (gdbarch, i386_supply_gregset,
2450 i386_collect_gregset);
8446b36a
MK
2451 return tdep->gregset;
2452 }
2453
66a72d25
MK
2454 if ((strcmp (sect_name, ".reg2") == 0 && sect_size == tdep->sizeof_fpregset)
2455 || (strcmp (sect_name, ".reg-xfp") == 0
2456 && sect_size == I387_SIZEOF_FXSAVE))
8446b36a
MK
2457 {
2458 if (tdep->fpregset == NULL)
7fdafb5a
MK
2459 tdep->fpregset = regset_alloc (gdbarch, i386_supply_fpregset,
2460 i386_collect_fpregset);
8446b36a
MK
2461 return tdep->fpregset;
2462 }
2463
2464 return NULL;
2465}
473f17b0 2466\f
fc338970 2467
fc338970 2468/* Stuff for WIN32 PE style DLL's but is pretty generic really. */
c906108c
SS
2469
2470CORE_ADDR
1cce71eb 2471i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name)
c906108c 2472{
fc338970 2473 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
c906108c 2474 {
c5aa993b 2475 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
c906108c 2476 struct minimal_symbol *indsym =
fc338970 2477 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
645dd519 2478 char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
c906108c 2479
c5aa993b 2480 if (symname)
c906108c 2481 {
c5aa993b
JM
2482 if (strncmp (symname, "__imp_", 6) == 0
2483 || strncmp (symname, "_imp_", 5) == 0)
c906108c
SS
2484 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
2485 }
2486 }
fc338970 2487 return 0; /* Not a trampoline. */
c906108c 2488}
fc338970
MK
2489\f
2490
10458914
DJ
2491/* Return whether the THIS_FRAME corresponds to a sigtramp
2492 routine. */
8201327c 2493
4bd207ef 2494int
10458914 2495i386_sigtramp_p (struct frame_info *this_frame)
8201327c 2496{
10458914 2497 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
2498 char *name;
2499
2500 find_pc_partial_function (pc, &name, NULL, NULL);
8201327c
MK
2501 return (name && strcmp ("_sigtramp", name) == 0);
2502}
2503\f
2504
fc338970
MK
2505/* We have two flavours of disassembly. The machinery on this page
2506 deals with switching between those. */
c906108c
SS
2507
2508static int
a89aa300 2509i386_print_insn (bfd_vma pc, struct disassemble_info *info)
c906108c 2510{
5e3397bb
MK
2511 gdb_assert (disassembly_flavor == att_flavor
2512 || disassembly_flavor == intel_flavor);
2513
2514 /* FIXME: kettenis/20020915: Until disassembler_options is properly
2515 constified, cast to prevent a compiler warning. */
2516 info->disassembler_options = (char *) disassembly_flavor;
5e3397bb
MK
2517
2518 return print_insn_i386 (pc, info);
7a292a7a 2519}
fc338970 2520\f
3ce1502b 2521
8201327c
MK
2522/* There are a few i386 architecture variants that differ only
2523 slightly from the generic i386 target. For now, we don't give them
2524 their own source file, but include them here. As a consequence,
2525 they'll always be included. */
3ce1502b 2526
8201327c 2527/* System V Release 4 (SVR4). */
3ce1502b 2528
10458914
DJ
2529/* Return whether THIS_FRAME corresponds to a SVR4 sigtramp
2530 routine. */
911bc6ee 2531
8201327c 2532static int
10458914 2533i386_svr4_sigtramp_p (struct frame_info *this_frame)
d2a7c97a 2534{
10458914 2535 CORE_ADDR pc = get_frame_pc (this_frame);
911bc6ee
MK
2536 char *name;
2537
acd5c798
MK
2538 /* UnixWare uses _sigacthandler. The origin of the other symbols is
2539 currently unknown. */
911bc6ee 2540 find_pc_partial_function (pc, &name, NULL, NULL);
8201327c
MK
2541 return (name && (strcmp ("_sigreturn", name) == 0
2542 || strcmp ("_sigacthandler", name) == 0
2543 || strcmp ("sigvechandler", name) == 0));
2544}
d2a7c97a 2545
10458914
DJ
2546/* Assuming THIS_FRAME is for a SVR4 sigtramp routine, return the
2547 address of the associated sigcontext (ucontext) structure. */
3ce1502b 2548
3a1e71e3 2549static CORE_ADDR
10458914 2550i386_svr4_sigcontext_addr (struct frame_info *this_frame)
8201327c 2551{
63c0089f 2552 gdb_byte buf[4];
acd5c798 2553 CORE_ADDR sp;
3ce1502b 2554
10458914 2555 get_frame_register (this_frame, I386_ESP_REGNUM, buf);
acd5c798 2556 sp = extract_unsigned_integer (buf, 4);
21d0e8a4 2557
acd5c798 2558 return read_memory_unsigned_integer (sp + 8, 4);
8201327c
MK
2559}
2560\f
3ce1502b 2561
8201327c 2562/* Generic ELF. */
d2a7c97a 2563
8201327c
MK
2564void
2565i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2566{
c4fc7f1b
MK
2567 /* We typically use stabs-in-ELF with the SVR4 register numbering. */
2568 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
8201327c 2569}
3ce1502b 2570
8201327c 2571/* System V Release 4 (SVR4). */
3ce1502b 2572
8201327c
MK
2573void
2574i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
2575{
2576 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 2577
8201327c
MK
2578 /* System V Release 4 uses ELF. */
2579 i386_elf_init_abi (info, gdbarch);
3ce1502b 2580
dfe01d39 2581 /* System V Release 4 has shared libraries. */
dfe01d39
MK
2582 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
2583
911bc6ee 2584 tdep->sigtramp_p = i386_svr4_sigtramp_p;
21d0e8a4 2585 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
acd5c798
MK
2586 tdep->sc_pc_offset = 36 + 14 * 4;
2587 tdep->sc_sp_offset = 36 + 17 * 4;
3ce1502b 2588
8201327c 2589 tdep->jb_pc_offset = 20;
3ce1502b
MK
2590}
2591
8201327c 2592/* DJGPP. */
3ce1502b 2593
3a1e71e3 2594static void
8201327c 2595i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
3ce1502b 2596{
8201327c 2597 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 2598
911bc6ee
MK
2599 /* DJGPP doesn't have any special frames for signal handlers. */
2600 tdep->sigtramp_p = NULL;
3ce1502b 2601
8201327c 2602 tdep->jb_pc_offset = 36;
15430fc0
EZ
2603
2604 /* DJGPP does not support the SSE registers. */
2605 tdep->num_xmm_regs = 0;
2606 set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
3d22076f
EZ
2607
2608 /* Native compiler is GCC, which uses the SVR4 register numbering
2609 even in COFF and STABS. See the comment in i386_gdbarch_init,
2610 before the calls to set_gdbarch_stab_reg_to_regnum and
2611 set_gdbarch_sdb_reg_to_regnum. */
2612 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
2613 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
3ce1502b 2614}
8201327c 2615\f
2acceee2 2616
38c968cf
AC
2617/* i386 register groups. In addition to the normal groups, add "mmx"
2618 and "sse". */
2619
2620static struct reggroup *i386_sse_reggroup;
2621static struct reggroup *i386_mmx_reggroup;
2622
2623static void
2624i386_init_reggroups (void)
2625{
2626 i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
2627 i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
2628}
2629
2630static void
2631i386_add_reggroups (struct gdbarch *gdbarch)
2632{
2633 reggroup_add (gdbarch, i386_sse_reggroup);
2634 reggroup_add (gdbarch, i386_mmx_reggroup);
2635 reggroup_add (gdbarch, general_reggroup);
2636 reggroup_add (gdbarch, float_reggroup);
2637 reggroup_add (gdbarch, all_reggroup);
2638 reggroup_add (gdbarch, save_reggroup);
2639 reggroup_add (gdbarch, restore_reggroup);
2640 reggroup_add (gdbarch, vector_reggroup);
2641 reggroup_add (gdbarch, system_reggroup);
2642}
2643
2644int
2645i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
2646 struct reggroup *group)
2647{
5716833c
MK
2648 int sse_regnum_p = (i386_sse_regnum_p (gdbarch, regnum)
2649 || i386_mxcsr_regnum_p (gdbarch, regnum));
20a6ec49
MD
2650 int fp_regnum_p = (i386_fp_regnum_p (gdbarch, regnum)
2651 || i386_fpc_regnum_p (gdbarch, regnum));
5716833c 2652 int mmx_regnum_p = (i386_mmx_regnum_p (gdbarch, regnum));
acd5c798 2653
38c968cf
AC
2654 if (group == i386_mmx_reggroup)
2655 return mmx_regnum_p;
2656 if (group == i386_sse_reggroup)
2657 return sse_regnum_p;
2658 if (group == vector_reggroup)
2659 return (mmx_regnum_p || sse_regnum_p);
2660 if (group == float_reggroup)
2661 return fp_regnum_p;
2662 if (group == general_reggroup)
2663 return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
acd5c798 2664
38c968cf
AC
2665 return default_register_reggroup_p (gdbarch, regnum, group);
2666}
38c968cf 2667\f
acd5c798 2668
f837910f
MK
2669/* Get the ARGIth function argument for the current function. */
2670
42c466d7 2671static CORE_ADDR
143985b7
AF
2672i386_fetch_pointer_argument (struct frame_info *frame, int argi,
2673 struct type *type)
2674{
f837910f
MK
2675 CORE_ADDR sp = get_frame_register_unsigned (frame, I386_ESP_REGNUM);
2676 return read_memory_unsigned_integer (sp + (4 * (argi + 1)), 4);
143985b7
AF
2677}
2678
514f746b
AR
2679static void
2680i386_skip_permanent_breakpoint (struct regcache *regcache)
2681{
2682 CORE_ADDR current_pc = regcache_read_pc (regcache);
2683
2684 /* On i386, breakpoint is exactly 1 byte long, so we just
2685 adjust the PC in the regcache. */
2686 current_pc += 1;
2687 regcache_write_pc (regcache, current_pc);
2688}
2689
2690
7ad10968
HZ
2691#define PREFIX_REPZ 0x01
2692#define PREFIX_REPNZ 0x02
2693#define PREFIX_LOCK 0x04
2694#define PREFIX_DATA 0x08
2695#define PREFIX_ADDR 0x10
473f17b0 2696
7ad10968
HZ
2697/* operand size */
2698enum
2699{
2700 OT_BYTE = 0,
2701 OT_WORD,
2702 OT_LONG,
2703};
473f17b0 2704
7ad10968
HZ
2705/* i386 arith/logic operations */
2706enum
2707{
2708 OP_ADDL,
2709 OP_ORL,
2710 OP_ADCL,
2711 OP_SBBL,
2712 OP_ANDL,
2713 OP_SUBL,
2714 OP_XORL,
2715 OP_CMPL,
2716};
5716833c 2717
7ad10968
HZ
2718struct i386_record_s
2719{
2720 struct regcache *regcache;
2721 CORE_ADDR addr;
2722 int aflag;
2723 int dflag;
2724 int override;
2725 uint8_t modrm;
2726 uint8_t mod, reg, rm;
2727 int ot;
2728};
5716833c 2729
7ad10968
HZ
2730/* Parse "modrm" part in current memory address that irp->addr point to
2731 Return -1 if something wrong. */
5716833c 2732
7ad10968
HZ
2733static int
2734i386_record_modrm (struct i386_record_s *irp)
2735{
2736 if (target_read_memory (irp->addr, &irp->modrm, 1))
2737 {
2738 if (record_debug)
2739 printf_unfiltered (_("Process record: error reading memory at "
2740 "addr 0x%s len = 1.\n"),
2741 paddr_nz (irp->addr));
2742 return -1;
2743 }
2744 irp->addr++;
2745 irp->mod = (irp->modrm >> 6) & 3;
2746 irp->reg = (irp->modrm >> 3) & 7;
2747 irp->rm = irp->modrm & 7;
5716833c 2748
7ad10968
HZ
2749 return 0;
2750}
d2a7c97a 2751
7ad10968
HZ
2752/* Get the memory address that current instruction write to and set it to
2753 the argument "addr".
2754 Return -1 if something wrong. */
8201327c 2755
7ad10968
HZ
2756static int
2757i386_record_lea_modrm_addr (struct i386_record_s *irp, uint32_t * addr)
2758{
2759 uint8_t tmpu8;
2760 uint16_t tmpu16;
2761 uint32_t tmpu32;
21d0e8a4 2762
7ad10968
HZ
2763 *addr = 0;
2764 if (irp->aflag)
2765 {
2766 /* 32 bits */
2767 int havesib = 0;
2768 uint8_t scale = 0;
2769 uint8_t index = 0;
2770 uint8_t base = irp->rm;
896fb97d 2771
7ad10968
HZ
2772 if (base == 4)
2773 {
2774 havesib = 1;
2775 if (target_read_memory (irp->addr, &tmpu8, 1))
2776 {
2777 if (record_debug)
2778 printf_unfiltered (_("Process record: error reading memory "
2779 "at addr 0x%s len = 1.\n"),
2780 paddr_nz (irp->addr));
2781 return -1;
2782 }
2783 irp->addr++;
2784 scale = (tmpu8 >> 6) & 3;
2785 index = ((tmpu8 >> 3) & 7);
2786 base = (tmpu8 & 7);
2787 }
21d0e8a4 2788
7ad10968
HZ
2789 switch (irp->mod)
2790 {
2791 case 0:
2792 if ((base & 7) == 5)
2793 {
2794 base = 0xff;
2795 if (target_read_memory (irp->addr, (gdb_byte *) addr, 4))
2796 {
2797 if (record_debug)
2798 printf_unfiltered (_("Process record: error reading "
2799 "memory at addr 0x%s len = 4.\n"),
2800 paddr_nz (irp->addr));
2801 return -1;
2802 }
2803 irp->addr += 4;
2804 }
2805 else
2806 {
2807 *addr = 0;
2808 }
2809 break;
2810 case 1:
2811 if (target_read_memory (irp->addr, &tmpu8, 1))
2812 {
2813 if (record_debug)
2814 printf_unfiltered (_("Process record: error reading memory "
2815 "at addr 0x%s len = 1.\n"),
2816 paddr_nz (irp->addr));
2817 return -1;
2818 }
2819 irp->addr++;
2820 *addr = (int8_t) tmpu8;
2821 break;
2822 case 2:
2823 if (target_read_memory (irp->addr, (gdb_byte *) addr, 4))
2824 {
2825 if (record_debug)
2826 printf_unfiltered (_("Process record: error reading memory "
2827 "at addr 0x%s len = 4.\n"),
2828 paddr_nz (irp->addr));
2829 return -1;
2830 }
2831 irp->addr += 4;
2832 break;
2833 }
356a6b3e 2834
7ad10968
HZ
2835 if (base != 0xff)
2836 {
2837 regcache_raw_read (irp->regcache, base, (gdb_byte *) & tmpu32);
2838 *addr += tmpu32;
2839 }
c4fc7f1b 2840
7ad10968
HZ
2841 /* XXX: index == 4 is always invalid */
2842 if (havesib && (index != 4 || scale != 0))
2843 {
2844 regcache_raw_read (irp->regcache, index, (gdb_byte *) & tmpu32);
2845 *addr += tmpu32 << scale;
2846 }
2847 }
2848 else
2849 {
2850 /* 16 bits */
2851 switch (irp->mod)
2852 {
2853 case 0:
2854 if (irp->rm == 6)
2855 {
2856 if (target_read_memory
2857 (irp->addr, (gdb_byte *) & tmpu16, 2))
2858 {
2859 if (record_debug)
2860 printf_unfiltered (_("Process record: error reading "
2861 "memory at addr 0x%s len = 2.\n"),
2862 paddr_nz (irp->addr));
2863 return -1;
2864 }
2865 irp->addr += 2;
2866 *addr = (int16_t) tmpu16;
2867 irp->rm = 0;
2868 goto no_rm;
2869 }
2870 else
2871 {
2872 *addr = 0;
2873 }
2874 break;
2875 case 1:
2876 if (target_read_memory (irp->addr, &tmpu8, 1))
2877 {
2878 if (record_debug)
2879 printf_unfiltered (_("Process record: error reading memory "
2880 "at addr 0x%s len = 1.\n"),
2881 paddr_nz (irp->addr));
2882 return -1;
2883 }
2884 irp->addr++;
2885 *addr = (int8_t) tmpu8;
2886 break;
2887 case 2:
2888 if (target_read_memory (irp->addr, (gdb_byte *) & tmpu16, 2))
2889 {
2890 if (record_debug)
2891 printf_unfiltered (_("Process record: error reading memory "
2892 "at addr 0x%s len = 2.\n"),
2893 paddr_nz (irp->addr));
2894 return -1;
2895 }
2896 irp->addr += 2;
2897 *addr = (int16_t) tmpu16;
2898 break;
2899 }
c4fc7f1b 2900
7ad10968
HZ
2901 switch (irp->rm)
2902 {
2903 case 0:
2904 regcache_raw_read (irp->regcache, I386_EBX_REGNUM,
2905 (gdb_byte *) & tmpu32);
2906 *addr += tmpu32;
2907 regcache_raw_read (irp->regcache, I386_ESI_REGNUM,
2908 (gdb_byte *) & tmpu32);
2909 *addr += tmpu32;
2910 break;
2911 case 1:
2912 regcache_raw_read (irp->regcache, I386_EBX_REGNUM,
2913 (gdb_byte *) & tmpu32);
2914 *addr += tmpu32;
2915 regcache_raw_read (irp->regcache, I386_EDI_REGNUM,
2916 (gdb_byte *) & tmpu32);
2917 *addr += tmpu32;
2918 break;
2919 case 2:
2920 regcache_raw_read (irp->regcache, I386_EBP_REGNUM,
2921 (gdb_byte *) & tmpu32);
2922 *addr += tmpu32;
2923 regcache_raw_read (irp->regcache, I386_ESI_REGNUM,
2924 (gdb_byte *) & tmpu32);
2925 *addr += tmpu32;
2926 break;
2927 case 3:
2928 regcache_raw_read (irp->regcache, I386_EBP_REGNUM,
2929 (gdb_byte *) & tmpu32);
2930 *addr += tmpu32;
2931 regcache_raw_read (irp->regcache, I386_EDI_REGNUM,
2932 (gdb_byte *) & tmpu32);
2933 *addr += tmpu32;
2934 break;
2935 case 4:
2936 regcache_raw_read (irp->regcache, I386_ESI_REGNUM,
2937 (gdb_byte *) & tmpu32);
2938 *addr += tmpu32;
2939 break;
2940 case 5:
2941 regcache_raw_read (irp->regcache, I386_EDI_REGNUM,
2942 (gdb_byte *) & tmpu32);
2943 *addr += tmpu32;
2944 break;
2945 case 6:
2946 regcache_raw_read (irp->regcache, I386_EBP_REGNUM,
2947 (gdb_byte *) & tmpu32);
2948 *addr += tmpu32;
2949 break;
2950 case 7:
2951 regcache_raw_read (irp->regcache, I386_EBX_REGNUM,
2952 (gdb_byte *) & tmpu32);
2953 *addr += tmpu32;
2954 break;
2955 }
2956 *addr &= 0xffff;
2957 }
c4fc7f1b 2958
7ad10968
HZ
2959no_rm:
2960 return 0;
2961}
c4fc7f1b 2962
7ad10968
HZ
2963/* Record the value of the memory that willbe changed in current instruction
2964 to "record_arch_list".
2965 Return -1 if something wrong. */
356a6b3e 2966
7ad10968
HZ
2967static int
2968i386_record_lea_modrm (struct i386_record_s *irp)
2969{
2970 uint32_t addr;
356a6b3e 2971
7ad10968
HZ
2972 if (irp->override)
2973 {
2974 if (record_debug)
2975 printf_unfiltered (_("Process record ignores the memory change "
2976 "of instruction at address 0x%s because it "
2977 "can't get the value of the segment register.\n"),
2978 paddr_nz (irp->addr));
2979 return 0;
2980 }
61113f8b 2981
7ad10968
HZ
2982 if (i386_record_lea_modrm_addr (irp, &addr))
2983 return -1;
96297dab 2984
7ad10968
HZ
2985 if (record_arch_list_add_mem (addr, 1 << irp->ot))
2986 return -1;
a62cc96e 2987
7ad10968
HZ
2988 return 0;
2989}
b6197528 2990
7ad10968
HZ
2991/* Parse the current instruction and record the values of the registers and
2992 memory that will be changed in current instruction to "record_arch_list".
2993 Return -1 if something wrong. */
8201327c 2994
a6b808b4 2995int
7ad10968
HZ
2996i386_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
2997 CORE_ADDR addr)
2998{
2999 int prefixes = 0;
3000 uint8_t tmpu8;
3001 uint16_t tmpu16;
3002 uint32_t tmpu32;
3003 uint32_t opcode;
3004 struct i386_record_s ir;
3005
8408d274 3006 memset (&ir, 0, sizeof (struct i386_record_s));
7ad10968
HZ
3007 ir.regcache = regcache;
3008 ir.addr = addr;
3009 ir.aflag = 1;
3010 ir.dflag = 1;
3011
3012 if (record_debug > 1)
3013 fprintf_unfiltered (gdb_stdlog, "Process record: i386_process_record "
3014 "addr = 0x%s\n",
3015 paddr_nz (ir.addr));
3016
3017 /* prefixes */
3018 while (1)
3019 {
3020 if (target_read_memory (ir.addr, &tmpu8, 1))
3021 {
3022 if (record_debug)
3023 printf_unfiltered (_("Process record: error reading memory at "
3024 "addr 0x%s len = 1.\n"),
3025 paddr_nz (ir.addr));
3026 return -1;
3027 }
3028 ir.addr++;
3029 switch (tmpu8)
3030 {
3031 case 0xf3:
3032 prefixes |= PREFIX_REPZ;
3033 break;
3034 case 0xf2:
3035 prefixes |= PREFIX_REPNZ;
3036 break;
3037 case 0xf0:
3038 prefixes |= PREFIX_LOCK;
3039 break;
3040 case 0x2e:
3041 ir.override = I386_CS_REGNUM;
3042 break;
3043 case 0x36:
3044 ir.override = I386_SS_REGNUM;
3045 break;
3046 case 0x3e:
3047 ir.override = I386_DS_REGNUM;
3048 break;
3049 case 0x26:
3050 ir.override = I386_ES_REGNUM;
3051 break;
3052 case 0x64:
3053 ir.override = I386_FS_REGNUM;
3054 break;
3055 case 0x65:
3056 ir.override = I386_GS_REGNUM;
3057 break;
3058 case 0x66:
3059 prefixes |= PREFIX_DATA;
3060 break;
3061 case 0x67:
3062 prefixes |= PREFIX_ADDR;
3063 break;
3064 default:
3065 goto out_prefixes;
3066 break;
3067 }
3068 }
3069out_prefixes:
3070 if (prefixes & PREFIX_DATA)
3071 ir.dflag ^= 1;
3072 if (prefixes & PREFIX_ADDR)
3073 ir.aflag ^= 1;
3074
3075 /* now check op code */
3076 opcode = (uint32_t) tmpu8;
3077reswitch:
3078 switch (opcode)
3079 {
3080 case 0x0f:
3081 if (target_read_memory (ir.addr, &tmpu8, 1))
3082 {
3083 if (record_debug)
3084 printf_unfiltered (_("Process record: error reading memory at "
3085 "addr 0x%s len = 1.\n"),
3086 paddr_nz (ir.addr));
3087 return -1;
3088 }
3089 ir.addr++;
3090 opcode = (uint16_t) tmpu8 | 0x0f00;
3091 goto reswitch;
3092 break;
93924b6b 3093
7ad10968
HZ
3094 /* arith & logic */
3095 case 0x00:
3096 case 0x01:
3097 case 0x02:
3098 case 0x03:
3099 case 0x04:
3100 case 0x05:
3101 case 0x08:
3102 case 0x09:
3103 case 0x0a:
3104 case 0x0b:
3105 case 0x0c:
3106 case 0x0d:
3107 case 0x10:
3108 case 0x11:
3109 case 0x12:
3110 case 0x13:
3111 case 0x14:
3112 case 0x15:
3113 case 0x18:
3114 case 0x19:
3115 case 0x1a:
3116 case 0x1b:
3117 case 0x1c:
3118 case 0x1d:
3119 case 0x20:
3120 case 0x21:
3121 case 0x22:
3122 case 0x23:
3123 case 0x24:
3124 case 0x25:
3125 case 0x28:
3126 case 0x29:
3127 case 0x2a:
3128 case 0x2b:
3129 case 0x2c:
3130 case 0x2d:
3131 case 0x30:
3132 case 0x31:
3133 case 0x32:
3134 case 0x33:
3135 case 0x34:
3136 case 0x35:
3137 case 0x38:
3138 case 0x39:
3139 case 0x3a:
3140 case 0x3b:
3141 case 0x3c:
3142 case 0x3d:
3143 if (((opcode >> 3) & 7) != OP_CMPL)
3144 {
3145 if ((opcode & 1) == 0)
3146 ir.ot = OT_BYTE;
3147 else
3148 ir.ot = ir.dflag + OT_WORD;
93924b6b 3149
7ad10968
HZ
3150 switch ((opcode >> 1) & 3)
3151 {
3152 /* OP Ev, Gv */
3153 case 0:
3154 if (i386_record_modrm (&ir))
3155 return -1;
3156 if (ir.mod != 3)
3157 {
3158 if (i386_record_lea_modrm (&ir))
3159 return -1;
3160 }
3161 else
3162 {
3163 if (ir.ot == OT_BYTE)
3164 ir.rm &= 0x3;
3165 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3166 return -1;
3167 }
3168 break;
3169 /* OP Gv, Ev */
3170 case 1:
3171 if (i386_record_modrm (&ir))
3172 return -1;
3173 if (ir.ot == OT_BYTE)
3174 ir.reg &= 0x3;
3175 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3176 return -1;
3177 break;
3178 /* OP A, Iv */
3179 case 2:
3180 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3181 return -1;
3182 break;
3183 }
3184 }
3185 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3186 return -1;
3187 break;
42fdc8df 3188
7ad10968
HZ
3189 /* GRP1 */
3190 case 0x80:
3191 case 0x81:
3192 case 0x82:
3193 case 0x83:
3194 if (i386_record_modrm (&ir))
3195 return -1;
8201327c 3196
7ad10968
HZ
3197 if (ir.reg != OP_CMPL)
3198 {
3199 if ((opcode & 1) == 0)
3200 ir.ot = OT_BYTE;
3201 else
3202 ir.ot = ir.dflag + OT_WORD;
28fc6740 3203
7ad10968
HZ
3204 if (ir.mod != 3)
3205 {
3206 if (i386_record_lea_modrm (&ir))
3207 return -1;
3208 }
3209 else
3210 {
3211 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3212 return -1;
3213 }
3214 }
3215 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3216 return -1;
3217 break;
5e3397bb 3218
7ad10968
HZ
3219 /* inv */
3220 case 0x40:
3221 case 0x41:
3222 case 0x42:
3223 case 0x43:
3224 case 0x44:
3225 case 0x45:
3226 case 0x46:
3227 case 0x47:
3228 /* dec */
3229 case 0x48:
3230 case 0x49:
3231 case 0x4a:
3232 case 0x4b:
3233 case 0x4c:
3234 case 0x4d:
3235 case 0x4e:
3236 case 0x4f:
3237 if (record_arch_list_add_reg (ir.regcache, opcode & 7))
3238 return -1;
3239 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3240 return -1;
3241 break;
acd5c798 3242
7ad10968
HZ
3243 /* GRP3 */
3244 case 0xf6:
3245 case 0xf7:
3246 if ((opcode & 1) == 0)
3247 ir.ot = OT_BYTE;
3248 else
3249 ir.ot = ir.dflag + OT_WORD;
3250 if (i386_record_modrm (&ir))
3251 return -1;
acd5c798 3252
7ad10968
HZ
3253 switch (ir.reg)
3254 {
3255 /* test */
3256 case 0:
3257 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3258 return -1;
3259 break;
3260 /* not */
3261 case 2:
3262 if (ir.mod != 3)
3263 {
3264 if (i386_record_lea_modrm (&ir))
3265 return -1;
3266 }
3267 else
3268 {
3269 if (ir.ot == OT_BYTE)
3270 ir.rm &= 0x3;
3271 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3272 return -1;
3273 }
3274 break;
3275 /* neg */
3276 case 3:
3277 if (ir.mod != 3)
3278 {
3279 if (i386_record_lea_modrm (&ir))
3280 return -1;
3281 }
3282 else
3283 {
3284 if (ir.ot == OT_BYTE)
3285 ir.rm &= 0x3;
3286 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3287 return -1;
3288 }
3289 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3290 return -1;
3291 break;
3292 /* mul */
3293 case 4:
3294 /* imul */
3295 case 5:
3296 /* div */
3297 case 6:
3298 /* idiv */
3299 case 7:
3300 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3301 return -1;
3302 if (ir.ot != OT_BYTE)
3303 {
3304 if (record_arch_list_add_reg (ir.regcache, I386_EDX_REGNUM))
3305 return -1;
3306 }
3307 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3308 return -1;
3309 break;
3310 default:
3311 ir.addr -= 2;
3312 opcode = opcode << 8 | ir.modrm;
3313 goto no_support;
3314 break;
3315 }
3316 break;
3317
3318 /* GRP4 */
3319 case 0xfe:
3320 /* GRP5 */
3321 case 0xff:
3322 if ((opcode & 1) == 0)
3323 ir.ot = OT_BYTE;
3324 else
3325 ir.ot = ir.dflag + OT_WORD;
3326 if (i386_record_modrm (&ir))
3327 return -1;
3328 if (ir.reg >= 2 && opcode == 0xfe)
3329 {
3330 ir.addr -= 2;
3331 opcode = opcode << 8 | ir.modrm;
3332 goto no_support;
3333 }
3334
3335 switch (ir.reg)
3336 {
3337 /* inc */
3338 case 0:
3339 /* dec */
3340 case 1:
3341 if (ir.mod != 3)
3342 {
3343 if (i386_record_lea_modrm (&ir))
3344 return -1;
3345 }
3346 else
3347 {
3348 if (ir.ot == OT_BYTE)
3349 ir.rm &= 0x3;
3350 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3351 return -1;
3352 }
3353 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3354 return -1;
3355 break;
3356 /* call */
3357 case 2:
3358 /* push */
3359 case 6:
3360 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3361 return -1;
3362 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
3363 (gdb_byte *) & tmpu32);
3364 if (record_arch_list_add_mem
3365 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 1)), (1 << (ir.dflag + 1))))
3366 return -1;
3367 break;
3368 /* lcall */
3369 case 3:
3370 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3371 return -1;
3372 if (record_arch_list_add_reg (ir.regcache, I386_CS_REGNUM))
3373 return -1;
3374 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
3375 (gdb_byte *) & tmpu32);
3376 if (record_arch_list_add_mem
3377 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 2)), (1 << (ir.dflag + 2))))
3378 return -1;
3379 break;
3380 /* jmp */
3381 case 4:
3382 /* ljmp */
3383 case 5:
3384 break;
3385 default:
3386 ir.addr -= 2;
3387 opcode = opcode << 8 | ir.modrm;
3388 goto no_support;
3389 break;
3390 }
3391 break;
3392
3393 /* test */
3394 case 0x84:
3395 case 0x85:
3396 case 0xa8:
3397 case 0xa9:
3398 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3399 return -1;
3400 break;
3401
3402 /* CWDE/CBW */
3403 case 0x98:
3404 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3405 return -1;
3406 break;
3407
3408 /* CDQ/CWD */
3409 case 0x99:
3410 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3411 return -1;
3412 if (record_arch_list_add_reg (ir.regcache, I386_EDX_REGNUM))
3413 return -1;
3414 break;
3415
3416 /* imul */
3417 case 0x0faf:
3418 case 0x69:
3419 case 0x6b:
3420 ir.ot = ir.dflag + OT_WORD;
3421 if (i386_record_modrm (&ir))
3422 return -1;
3423 if (ir.ot == OT_BYTE)
3424 ir.reg &= 0x3;
3425 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3426 return -1;
3427 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3428 return -1;
3429 break;
3430
3431 /* xadd */
3432 case 0x0fc0:
3433 case 0x0fc1:
3434 if ((opcode & 1) == 0)
3435 ir.ot = OT_BYTE;
3436 else
3437 ir.ot = ir.dflag + OT_WORD;
3438 if (i386_record_modrm (&ir))
3439 return -1;
3440 if (ir.mod == 3)
3441 {
3442 if (ir.ot == OT_BYTE)
3443 ir.reg &= 0x3;
3444 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3445 return -1;
3446 if (ir.ot == OT_BYTE)
3447 ir.rm &= 0x3;
3448 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3449 return -1;
3450 }
3451 else
3452 {
3453 if (i386_record_lea_modrm (&ir))
3454 return -1;
3455 if (ir.ot == OT_BYTE)
3456 ir.reg &= 0x3;
3457 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3458 return -1;
3459 }
3460 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3461 return -1;
3462 break;
3463
3464 /* cmpxchg */
3465 case 0x0fb0:
3466 case 0x0fb1:
3467 if ((opcode & 1) == 0)
3468 ir.ot = OT_BYTE;
3469 else
3470 ir.ot = ir.dflag + OT_WORD;
3471 if (i386_record_modrm (&ir))
3472 return -1;
3473 if (ir.mod == 3)
3474 {
3475 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3476 return -1;
3477 if (ir.ot == OT_BYTE)
3478 ir.reg &= 0x3;
3479 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3480 return -1;
3481 }
3482 else
3483 {
3484 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3485 return -1;
3486 if (i386_record_lea_modrm (&ir))
3487 return -1;
3488 }
3489 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3490 return -1;
3491 break;
3492
3493 /* cmpxchg8b */
3494 case 0x0fc7:
3495 if (i386_record_modrm (&ir))
3496 return -1;
3497 if (ir.mod == 3)
3498 {
3499 ir.addr -= 2;
3500 opcode = opcode << 8 | ir.modrm;
3501 goto no_support;
3502 }
3503 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3504 return -1;
3505 if (record_arch_list_add_reg (ir.regcache, I386_EDX_REGNUM))
3506 return -1;
3507 if (i386_record_lea_modrm (&ir))
3508 return -1;
3509 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3510 return -1;
3511 break;
3512
3513 /* push */
3514 case 0x50:
3515 case 0x51:
3516 case 0x52:
3517 case 0x53:
3518 case 0x54:
3519 case 0x55:
3520 case 0x56:
3521 case 0x57:
3522 case 0x68:
3523 case 0x6a:
3524 /* push es */
3525 case 0x06:
3526 /* push cs */
3527 case 0x0e:
3528 /* push ss */
3529 case 0x16:
3530 /* push ds */
3531 case 0x1e:
3532 /* push fs */
3533 case 0x0fa0:
3534 /* push gs */
3535 case 0x0fa8:
3536 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3537 return -1;
3538 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
3539 (gdb_byte *) & tmpu32);
3540 if (record_arch_list_add_mem
3541 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 1)), (1 << (ir.dflag + 1))))
3542 return -1;
3543 break;
3544
3545 /* pop */
3546 case 0x58:
3547 case 0x59:
3548 case 0x5a:
3549 case 0x5b:
3550 case 0x5c:
3551 case 0x5d:
3552 case 0x5e:
3553 case 0x5f:
3554 ir.ot = ir.dflag + OT_WORD;
3555 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3556 return -1;
3557 if (ir.ot == OT_BYTE)
3558 opcode &= 0x3;
3559 if (record_arch_list_add_reg (ir.regcache, opcode & 0x7))
3560 return -1;
3561 break;
3562
3563 /* pusha */
3564 case 0x60:
3565 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3566 return -1;
3567 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
3568 (gdb_byte *) & tmpu32);
3569 if (record_arch_list_add_mem
3570 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 4)), (1 << (ir.dflag + 4))))
3571 return -1;
3572 break;
3573
3574 /* popa */
3575 case 0x61:
3576 for (tmpu8 = I386_EAX_REGNUM; tmpu8 <= I386_EDI_REGNUM; tmpu8++)
3577 {
3578 if (record_arch_list_add_reg (ir.regcache, tmpu8))
3579 return -1;
3580 }
3581 break;
3582
3583 /* pop */
3584 case 0x8f:
3585 ir.ot = ir.dflag + OT_WORD;
3586 if (i386_record_modrm (&ir))
3587 return -1;
3588 if (ir.mod == 3)
3589 {
3590 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3591 return -1;
3592 }
3593 else
3594 {
3595 if (i386_record_lea_modrm (&ir))
3596 return -1;
3597 }
3598 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3599 return -1;
3600 break;
3601
3602 /* enter */
3603 case 0xc8:
3604 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3605 return -1;
3606 if (record_arch_list_add_reg (ir.regcache, I386_EBP_REGNUM))
3607 return -1;
3608 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
3609 (gdb_byte *) & tmpu32);
3610 if (record_arch_list_add_mem
3611 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 1)), (1 << (ir.dflag + 1))))
3612 return -1;
3613 break;
3614
3615 /* leave */
3616 case 0xc9:
3617 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3618 return -1;
3619 if (record_arch_list_add_reg (ir.regcache, I386_EBP_REGNUM))
3620 return -1;
3621 break;
3622
3623 /* pop es */
3624 case 0x07:
3625 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3626 return -1;
3627 if (record_arch_list_add_reg (ir.regcache, I386_ES_REGNUM))
3628 return -1;
3629 break;
3630
3631 /* pop ss */
3632 case 0x17:
3633 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3634 return -1;
3635 if (record_arch_list_add_reg (ir.regcache, I386_SS_REGNUM))
3636 return -1;
3637 break;
3638
3639 /* pop ds */
3640 case 0x1f:
3641 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3642 return -1;
3643 if (record_arch_list_add_reg (ir.regcache, I386_DS_REGNUM))
3644 return -1;
3645 break;
3646
3647 /* pop fs */
3648 case 0x0fa1:
3649 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3650 return -1;
3651 if (record_arch_list_add_reg (ir.regcache, I386_FS_REGNUM))
3652 return -1;
3653 break;
3654
3655 /* pop gs */
3656 case 0x0fa9:
3657 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
3658 return -1;
3659 if (record_arch_list_add_reg (ir.regcache, I386_GS_REGNUM))
3660 return -1;
3661 break;
3662
3663 /* mov */
3664 case 0x88:
3665 case 0x89:
3666 case 0xc6:
3667 case 0xc7:
3668 if ((opcode & 1) == 0)
3669 ir.ot = OT_BYTE;
3670 else
3671 ir.ot = ir.dflag + OT_WORD;
3672
3673 if (i386_record_modrm (&ir))
3674 return -1;
3675
3676 if (ir.mod != 3)
3677 {
3678 if (i386_record_lea_modrm (&ir))
3679 return -1;
3680 }
3681 else
3682 {
3683 if (ir.ot == OT_BYTE)
3684 ir.rm &= 0x3;
3685 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3686 return -1;
3687 }
3688 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3689 return -1;
3690 break;
3691 /* mov */
3692 case 0x8a:
3693 case 0x8b:
3694 if ((opcode & 1) == 0)
3695 ir.ot = OT_BYTE;
3696 else
3697 ir.ot = ir.dflag + OT_WORD;
3698
3699 if (i386_record_modrm (&ir))
3700 return -1;
3701
3702 if (ir.ot == OT_BYTE)
3703 ir.reg &= 0x3;
3704 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3705 return -1;
3706
3707 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3708 return -1;
3709 break;
3710
3711 /* mov seg */
3712 case 0x8e:
3713 if (i386_record_modrm (&ir))
3714 return -1;
3715
3716 switch (ir.reg)
3717 {
3718 case 0:
3719 tmpu8 = I386_ES_REGNUM;
3720 break;
3721 case 2:
3722 tmpu8 = I386_SS_REGNUM;
3723 break;
3724 case 3:
3725 tmpu8 = I386_DS_REGNUM;
3726 break;
3727 case 4:
3728 tmpu8 = I386_FS_REGNUM;
3729 break;
3730 case 5:
3731 tmpu8 = I386_GS_REGNUM;
3732 break;
3733 default:
3734 ir.addr -= 2;
3735 opcode = opcode << 8 | ir.modrm;
3736 goto no_support;
3737 break;
3738 }
3739 if (record_arch_list_add_reg (ir.regcache, tmpu8))
3740 return -1;
3741
3742 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3743 return -1;
3744 break;
3745
3746 /* mov seg */
3747 case 0x8c:
3748 if (i386_record_modrm (&ir))
3749 return -1;
3750 if (ir.reg > 5)
3751 {
3752 ir.addr -= 2;
3753 opcode = opcode << 8 | ir.modrm;
3754 goto no_support;
3755 }
3756
3757 if (ir.mod == 3)
3758 {
3759 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3760 return -1;
3761 }
3762 else
3763 {
3764 ir.ot = OT_WORD;
3765 if (i386_record_lea_modrm (&ir))
3766 return -1;
3767 }
3768
3769 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
3770 return -1;
3771 break;
3772
3773 /* movzbS */
3774 case 0x0fb6:
3775 /* movzwS */
3776 case 0x0fb7:
3777 /* movsbS */
3778 case 0x0fbe:
3779 /* movswS */
3780 case 0x0fbf:
3781 if (i386_record_modrm (&ir))
3782 return -1;
3783 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3784 return -1;
3785 break;
3786
3787 /* lea */
3788 case 0x8d:
3789 if (i386_record_modrm (&ir))
3790 return -1;
3791 if (ir.mod == 3)
3792 {
3793 ir.addr -= 2;
3794 opcode = opcode << 8 | ir.modrm;
3795 goto no_support;
3796 }
3797
3798 ir.ot = ir.dflag;
3799 if (ir.ot == OT_BYTE)
3800 ir.reg &= 0x3;
3801 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3802 return -1;
3803 break;
3804
3805 /* mov EAX */
3806 case 0xa0:
3807 case 0xa1:
3808 /* xlat */
3809 case 0xd7:
3810 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3811 return -1;
3812 break;
3813
3814 /* mov EAX */
3815 case 0xa2:
3816 case 0xa3:
3817 {
3818 uint32_t addr;
3819
3820 if (ir.override)
3821 {
3822 if (record_debug)
3823 printf_unfiltered (_("Process record ignores the memory change "
3824 "of instruction at address 0x%s because "
3825 "it can't get the value of the segment "
3826 "register.\n"),
3827 paddr_nz (ir.addr));
3828 }
3829 else
3830 {
3831 if ((opcode & 1) == 0)
3832 ir.ot = OT_BYTE;
3833 else
3834 ir.ot = ir.dflag + OT_WORD;
3835 if (ir.aflag)
3836 {
3837 if (target_read_memory
3838 (ir.addr, (gdb_byte *) & addr, 4))
3839 {
3840 if (record_debug)
3841 printf_unfiltered (_("Process record: error reading "
3842 "memory at addr 0x%s len = 4.\n"),
3843 paddr_nz (ir.addr));
3844 return -1;
3845 }
3846 ir.addr += 4;
3847 }
3848 else
3849 {
3850 if (target_read_memory
3851 (ir.addr, (gdb_byte *) & tmpu16, 4))
3852 {
3853 if (record_debug)
3854 printf_unfiltered (_("Process record: error reading "
3855 "memory at addr 0x%s len = 4.\n"),
3856 paddr_nz (ir.addr));
3857 return -1;
3858 }
3859 ir.addr += 2;
3860 addr = tmpu16;
3861 }
3862 if (record_arch_list_add_mem (addr, 1 << ir.ot))
3863 return -1;
3864 }
3865 }
3866 break;
3867
3868 /* mov R, Ib */
3869 case 0xb0:
3870 case 0xb1:
3871 case 0xb2:
3872 case 0xb3:
3873 case 0xb4:
3874 case 0xb5:
3875 case 0xb6:
3876 case 0xb7:
3877 if (record_arch_list_add_reg (ir.regcache, (opcode & 0x7) & 0x3))
3878 return -1;
3879 break;
3880
3881 /* mov R, Iv */
3882 case 0xb8:
3883 case 0xb9:
3884 case 0xba:
3885 case 0xbb:
3886 case 0xbc:
3887 case 0xbd:
3888 case 0xbe:
3889 case 0xbf:
3890 if (record_arch_list_add_reg (ir.regcache, opcode & 0x7))
3891 return -1;
3892 break;
3893
3894 /* xchg R, EAX */
3895 case 0x91:
3896 case 0x92:
3897 case 0x93:
3898 case 0x94:
3899 case 0x95:
3900 case 0x96:
3901 case 0x97:
3902 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
3903 return -1;
3904 if (record_arch_list_add_reg (ir.regcache, opcode & 0x7))
3905 return -1;
3906 break;
3907
3908 /* xchg Ev, Gv */
3909 case 0x86:
3910 case 0x87:
3911 if ((opcode & 1) == 0)
3912 ir.ot = OT_BYTE;
3913 else
3914 ir.ot = ir.dflag + OT_WORD;
3915
3916 if (i386_record_modrm (&ir))
3917 return -1;
3918
3919 if (ir.mod == 3)
3920 {
3921 if (ir.ot == OT_BYTE)
3922 ir.rm &= 0x3;
3923 if (record_arch_list_add_reg (ir.regcache, ir.rm))
3924 return -1;
3925 }
3926 else
3927 {
3928 if (i386_record_lea_modrm (&ir))
3929 return -1;
3930 }
3931
3932 if (ir.ot == OT_BYTE)
3933 ir.reg &= 0x3;
3934 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3935 return -1;
3936 break;
3937
3938 /* les Gv */
3939 case 0xc4:
3940 /* lds Gv */
3941 case 0xc5:
3942 /* lss Gv */
3943 case 0x0fb2:
3944 /* lfs Gv */
3945 case 0x0fb4:
3946 /* lgs Gv */
3947 case 0x0fb5:
3948 if (i386_record_modrm (&ir))
3949 return -1;
3950 if (ir.mod == 3)
3951 {
3952 if (opcode > 0xff)
3953 ir.addr -= 3;
3954 else
3955 ir.addr -= 2;
3956 opcode = opcode << 8 | ir.modrm;
3957 goto no_support;
3958 }
3959
3960 switch (opcode)
3961 {
3962 /* les Gv */
3963 case 0xc4:
3964 tmpu8 = I386_ES_REGNUM;
3965 break;
3966 /* lds Gv */
3967 case 0xc5:
3968 tmpu8 = I386_DS_REGNUM;
3969 break;
3970 /* lss Gv */
3971 case 0x0fb2:
3972 tmpu8 = I386_SS_REGNUM;
3973 break;
3974 /* lfs Gv */
3975 case 0x0fb4:
3976 tmpu8 = I386_FS_REGNUM;
3977 break;
3978 /* lgs Gv */
3979 case 0x0fb5:
3980 tmpu8 = I386_GS_REGNUM;
3981 break;
3982 }
3983 if (record_arch_list_add_reg (ir.regcache, tmpu8))
3984 return -1;
3985
3986 if (record_arch_list_add_reg (ir.regcache, ir.reg))
3987 return -1;
3988 break;
3989
3990 /* shifts */
3991 case 0xc0:
3992 case 0xc1:
3993 case 0xd0:
3994 case 0xd1:
3995 case 0xd2:
3996 case 0xd3:
3997 if ((opcode & 1) == 0)
3998 ir.ot = OT_BYTE;
3999 else
4000 ir.ot = ir.dflag + OT_WORD;
4001
4002 if (i386_record_modrm (&ir))
4003 return -1;
4004
4005 if (ir.mod != 3 && (opcode == 0xd2 || opcode == 0xd3))
4006 {
4007 if (i386_record_lea_modrm (&ir))
4008 return -1;
4009 }
4010 else
4011 {
4012 if (ir.ot == OT_BYTE)
4013 ir.rm &= 0x3;
4014 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4015 return -1;
4016 }
4017
4018 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4019 return -1;
4020 break;
4021
4022 case 0x0fa4:
4023 case 0x0fa5:
4024 case 0x0fac:
4025 case 0x0fad:
4026 if (i386_record_modrm (&ir))
4027 return -1;
4028 if (ir.mod == 3)
4029 {
4030 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4031 return -1;
4032 }
4033 else
4034 {
4035 if (i386_record_lea_modrm (&ir))
4036 return -1;
4037 }
4038 break;
4039
4040 /* floats */
4041 /* It just record the memory change of instrcution. */
4042 case 0xd8:
4043 case 0xd9:
4044 case 0xda:
4045 case 0xdb:
4046 case 0xdc:
4047 case 0xdd:
4048 case 0xde:
4049 case 0xdf:
4050 if (i386_record_modrm (&ir))
4051 return -1;
4052 ir.reg |= ((opcode & 7) << 3);
4053 if (ir.mod != 3)
4054 {
4055 /* memory */
4056 uint32_t addr;
4057
4058 if (i386_record_lea_modrm_addr (&ir, &addr))
4059 return -1;
4060 switch (ir.reg)
4061 {
4062 case 0x00:
4063 case 0x01:
4064 case 0x02:
4065 case 0x03:
4066 case 0x04:
4067 case 0x05:
4068 case 0x06:
4069 case 0x07:
4070 case 0x10:
4071 case 0x11:
4072 case 0x12:
4073 case 0x13:
4074 case 0x14:
4075 case 0x15:
4076 case 0x16:
4077 case 0x17:
4078 case 0x20:
4079 case 0x21:
4080 case 0x22:
4081 case 0x23:
4082 case 0x24:
4083 case 0x25:
4084 case 0x26:
4085 case 0x27:
4086 case 0x30:
4087 case 0x31:
4088 case 0x32:
4089 case 0x33:
4090 case 0x34:
4091 case 0x35:
4092 case 0x36:
4093 case 0x37:
4094 break;
4095 case 0x08:
4096 case 0x0a:
4097 case 0x0b:
4098 case 0x18:
4099 case 0x19:
4100 case 0x1a:
4101 case 0x1b:
4102 case 0x28:
4103 case 0x29:
4104 case 0x2a:
4105 case 0x2b:
4106 case 0x38:
4107 case 0x39:
4108 case 0x3a:
4109 case 0x3b:
4110 switch (ir.reg & 7)
4111 {
4112 case 0:
4113 break;
4114 case 1:
4115 switch (ir.reg >> 4)
4116 {
4117 case 0:
4118 if (record_arch_list_add_mem (addr, 4))
4119 return -1;
4120 break;
4121 case 2:
4122 if (record_arch_list_add_mem (addr, 8))
4123 return -1;
4124 break;
4125 case 3:
4126 default:
4127 if (record_arch_list_add_mem (addr, 2))
4128 return -1;
4129 break;
4130 }
4131 break;
4132 default:
4133 switch (ir.reg >> 4)
4134 {
4135 case 0:
4136 case 1:
4137 if (record_arch_list_add_mem (addr, 4))
4138 return -1;
4139 break;
4140 case 2:
4141 if (record_arch_list_add_mem (addr, 8))
4142 return -1;
4143 break;
4144 case 3:
4145 default:
4146 if (record_arch_list_add_mem (addr, 2))
4147 return -1;
4148 break;
4149 }
4150 break;
4151 }
4152 break;
4153 case 0x0c:
4154 case 0x0d:
4155 case 0x1d:
4156 case 0x2c:
4157 case 0x3c:
4158 case 0x3d:
4159 break;
4160 case 0x0e:
4161 if (ir.dflag)
4162 {
4163 if (record_arch_list_add_mem (addr, 28))
4164 return -1;
4165 }
4166 else
4167 {
4168 if (record_arch_list_add_mem (addr, 14))
4169 return -1;
4170 }
4171 break;
4172 case 0x0f:
4173 case 0x2f:
4174 if (record_arch_list_add_mem (addr, 2))
4175 return -1;
4176 break;
4177 case 0x1f:
4178 case 0x3e:
4179 if (record_arch_list_add_mem (addr, 10))
4180 return -1;
4181 break;
4182 case 0x2e:
4183 if (ir.dflag)
4184 {
4185 if (record_arch_list_add_mem (addr, 28))
4186 return -1;
4187 addr += 28;
4188 }
4189 else
4190 {
4191 if (record_arch_list_add_mem (addr, 14))
4192 return -1;
4193 addr += 14;
4194 }
4195 if (record_arch_list_add_mem (addr, 80))
4196 return -1;
4197 break;
4198 case 0x3f:
4199 if (record_arch_list_add_mem (addr, 8))
4200 return -1;
4201 break;
4202 default:
4203 ir.addr -= 2;
4204 opcode = opcode << 8 | ir.modrm;
4205 goto no_support;
4206 break;
4207 }
4208 }
4209 break;
4210
4211 /* string ops */
4212 /* movsS */
4213 case 0xa4:
4214 case 0xa5:
4215 /* stosS */
4216 case 0xaa:
4217 case 0xab:
4218 /* insS */
4219 case 0x6c:
4220 case 0x6d:
4221 {
4222 uint32_t addr;
4223
4224 if ((opcode & 1) == 0)
4225 ir.ot = OT_BYTE;
4226 else
4227 ir.ot = ir.dflag + OT_WORD;
4228 if (opcode == 0xa4 || opcode == 0xa5)
4229 {
4230 if (record_arch_list_add_reg (ir.regcache, I386_ESI_REGNUM))
4231 return -1;
4232 }
4233 if (record_arch_list_add_reg (ir.regcache, I386_EDI_REGNUM))
4234 return -1;
4235
4236 regcache_raw_read (ir.regcache, I386_EDI_REGNUM,
4237 (gdb_byte *) & addr);
4238 if (!ir.aflag)
4239 {
4240 addr &= 0xffff;
4241 /* addr += ((uint32_t)read_register (I386_ES_REGNUM)) << 4; */
4242 if (record_debug)
4243 printf_unfiltered (_("Process record ignores the memory change "
4244 "of instruction at address 0x%s because "
4245 "it can't get the value of the segment "
4246 "register.\n"),
4247 paddr_nz (ir.addr));
4248 }
4249
4250 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4251 {
4252 uint32_t count;
4253
4254 regcache_raw_read (ir.regcache, I386_ECX_REGNUM,
4255 (gdb_byte *) & count);
4256 if (!ir.aflag)
4257 count &= 0xffff;
4258
4259 regcache_raw_read (ir.regcache, I386_EFLAGS_REGNUM,
4260 (gdb_byte *) & tmpu32);
4261 if ((tmpu32 >> 10) & 0x1)
4262 addr -= (count - 1) * (1 << ir.ot);
4263
4264 if (ir.aflag)
4265 {
4266 if (record_arch_list_add_mem (addr, count * (1 << ir.ot)))
4267 return -1;
4268 }
4269
4270 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4271 return -1;
4272 }
4273 else
4274 {
4275 if (ir.aflag)
4276 {
4277 if (record_arch_list_add_mem (addr, 1 << ir.ot))
4278 return -1;
4279 }
4280 }
4281 }
4282 break;
4283
4284 /* lodsS */
4285 case 0xac:
4286 case 0xad:
4287 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4288 return -1;
4289 if (record_arch_list_add_reg (ir.regcache, I386_ESI_REGNUM))
4290 return -1;
4291 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4292 {
4293 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4294 return -1;
4295 }
4296 break;
4297
4298 /* outsS */
4299 case 0x6e:
4300 case 0x6f:
4301 if (record_arch_list_add_reg (ir.regcache, I386_ESI_REGNUM))
4302 return -1;
4303 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4304 {
4305 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4306 return -1;
4307 }
4308 break;
4309
4310 /* scasS */
4311 case 0xae:
4312 case 0xaf:
4313 if (record_arch_list_add_reg (ir.regcache, I386_EDI_REGNUM))
4314 return -1;
4315 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4316 {
4317 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4318 return -1;
4319 }
4320 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4321 return -1;
4322 break;
4323
4324 /* cmpsS */
4325 case 0xa6:
4326 case 0xa7:
4327 if (record_arch_list_add_reg (ir.regcache, I386_EDI_REGNUM))
4328 return -1;
4329 if (record_arch_list_add_reg (ir.regcache, I386_ESI_REGNUM))
4330 return -1;
4331 if (prefixes & (PREFIX_REPZ | PREFIX_REPNZ))
4332 {
4333 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4334 return -1;
4335 }
4336 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4337 return -1;
4338 break;
4339
4340 /* port I/O */
4341 case 0xe4:
4342 case 0xe5:
4343 case 0xec:
4344 case 0xed:
4345 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4346 return -1;
4347 break;
4348
4349 case 0xe6:
4350 case 0xe7:
4351 case 0xee:
4352 case 0xef:
4353 break;
4354
4355 /* control */
4356 /* ret im */
4357 case 0xc2:
4358 /* ret */
4359 case 0xc3:
4360 /* lret im */
4361 case 0xca:
4362 /* lret */
4363 case 0xcb:
4364 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4365 return -1;
4366 if (record_arch_list_add_reg (ir.regcache, I386_CS_REGNUM))
4367 return -1;
4368 break;
4369
4370 /* iret */
4371 case 0xcf:
4372 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4373 return -1;
4374 if (record_arch_list_add_reg (ir.regcache, I386_CS_REGNUM))
4375 return -1;
4376 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4377 return -1;
4378 break;
4379
4380 /* call im */
4381 case 0xe8:
4382 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4383 return -1;
4384 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
4385 (gdb_byte *) & tmpu32);
4386 if (record_arch_list_add_mem
4387 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 1)), (1 << (ir.dflag + 1))))
4388 return -1;
4389 break;
4390
4391 /* lcall im */
4392 case 0x9a:
4393 if (record_arch_list_add_reg (ir.regcache, I386_CS_REGNUM))
4394 return -1;
4395 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4396 return -1;
4397 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
4398 (gdb_byte *) & tmpu32);
4399 if (record_arch_list_add_mem
4400 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 2)), (1 << (ir.dflag + 2))))
4401 return -1;
4402 break;
4403
4404 /* jmp im */
4405 case 0xe9:
4406 /* ljmp im */
4407 case 0xea:
4408 /* jmp Jb */
4409 case 0xeb:
4410 /* jcc Jb */
4411 case 0x70:
4412 case 0x71:
4413 case 0x72:
4414 case 0x73:
4415 case 0x74:
4416 case 0x75:
4417 case 0x76:
4418 case 0x77:
4419 case 0x78:
4420 case 0x79:
4421 case 0x7a:
4422 case 0x7b:
4423 case 0x7c:
4424 case 0x7d:
4425 case 0x7e:
4426 case 0x7f:
4427 /* jcc Jv */
4428 case 0x0f80:
4429 case 0x0f81:
4430 case 0x0f82:
4431 case 0x0f83:
4432 case 0x0f84:
4433 case 0x0f85:
4434 case 0x0f86:
4435 case 0x0f87:
4436 case 0x0f88:
4437 case 0x0f89:
4438 case 0x0f8a:
4439 case 0x0f8b:
4440 case 0x0f8c:
4441 case 0x0f8d:
4442 case 0x0f8e:
4443 case 0x0f8f:
4444 break;
4445
4446 /* setcc Gv */
4447 case 0x0f90:
4448 case 0x0f91:
4449 case 0x0f92:
4450 case 0x0f93:
4451 case 0x0f94:
4452 case 0x0f95:
4453 case 0x0f96:
4454 case 0x0f97:
4455 case 0x0f98:
4456 case 0x0f99:
4457 case 0x0f9a:
4458 case 0x0f9b:
4459 case 0x0f9c:
4460 case 0x0f9d:
4461 case 0x0f9e:
4462 case 0x0f9f:
4463 ir.ot = OT_BYTE;
4464 if (i386_record_modrm (&ir))
4465 return -1;
4466 if (ir.mod == 3)
4467 {
4468 if (record_arch_list_add_reg (ir.regcache, ir.rm & 0x3))
4469 return -1;
4470 }
4471 else
4472 {
4473 if (i386_record_lea_modrm (&ir))
4474 return -1;
4475 }
4476 break;
4477
4478 /* cmov Gv, Ev */
4479 case 0x0f40:
4480 case 0x0f41:
4481 case 0x0f42:
4482 case 0x0f43:
4483 case 0x0f44:
4484 case 0x0f45:
4485 case 0x0f46:
4486 case 0x0f47:
4487 case 0x0f48:
4488 case 0x0f49:
4489 case 0x0f4a:
4490 case 0x0f4b:
4491 case 0x0f4c:
4492 case 0x0f4d:
4493 case 0x0f4e:
4494 case 0x0f4f:
4495 if (i386_record_modrm (&ir))
4496 return -1;
4497 if (ir.dflag == OT_BYTE)
4498 ir.reg &= 0x3;
4499 if (record_arch_list_add_reg (ir.regcache, ir.reg & 0x3))
4500 return -1;
4501 break;
4502
4503 /* flags */
4504 /* pushf */
4505 case 0x9c:
4506 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4507 return -1;
4508 regcache_raw_read (ir.regcache, I386_ESP_REGNUM,
4509 (gdb_byte *) & tmpu32);
4510 if (record_arch_list_add_mem
4511 ((CORE_ADDR) tmpu32 - (1 << (ir.dflag + 1)), (1 << (ir.dflag + 1))))
4512 return -1;
4513 break;
4514
4515 /* popf */
4516 case 0x9d:
4517 if (record_arch_list_add_reg (ir.regcache, I386_ESP_REGNUM))
4518 return -1;
4519 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4520 return -1;
4521 break;
4522
4523 /* sahf */
4524 case 0x9e:
4525 /* cmc */
4526 case 0xf5:
4527 /* clc */
4528 case 0xf8:
4529 /* stc */
4530 case 0xf9:
4531 /* cld */
4532 case 0xfc:
4533 /* std */
4534 case 0xfd:
4535 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4536 return -1;
4537 break;
4538
4539 /* lahf */
4540 case 0x9f:
4541 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4542 return -1;
4543 break;
4544
4545 /* bit operations */
4546 /* bt/bts/btr/btc Gv, im */
4547 case 0x0fba:
4548 /* bts */
4549 case 0x0fab:
4550 /* btr */
4551 case 0x0fb3:
4552 /* btc */
4553 case 0x0fbb:
4554 ir.ot = ir.dflag + OT_WORD;
4555 if (i386_record_modrm (&ir))
4556 return -1;
4557 if (ir.reg < 4)
4558 {
4559 ir.addr -= 3;
4560 opcode = opcode << 8 | ir.modrm;
4561 goto no_support;
4562 }
4563 ir.reg -= 4;
4564 if (ir.reg != 0)
4565 {
4566 if (ir.mod != 3)
4567 {
4568 if (i386_record_lea_modrm (&ir))
4569 return -1;
4570 }
4571 else
4572 {
4573 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4574 return -1;
4575 }
4576 }
4577 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4578 return -1;
4579 break;
4580
4581 /* bt Gv, Ev */
4582 case 0x0fa3:
4583 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4584 return -1;
4585 break;
4586
4587 /* bsf */
4588 case 0x0fbc:
4589 /* bsr */
4590 case 0x0fbd:
4591 if (record_arch_list_add_reg (ir.regcache, ir.reg))
4592 return -1;
4593 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4594 return -1;
4595 break;
4596
4597 /* bcd */
4598 /* daa */
4599 case 0x27:
4600 /* das */
4601 case 0x2f:
4602 /* aaa */
4603 case 0x37:
4604 /* aas */
4605 case 0x3f:
4606 /* aam */
4607 case 0xd4:
4608 /* aad */
4609 case 0xd5:
4610 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4611 return -1;
4612 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4613 return -1;
4614 break;
4615
4616 /* misc */
4617 /* nop */
4618 case 0x90:
4619 if (prefixes & PREFIX_LOCK)
4620 {
4621 ir.addr -= 1;
4622 goto no_support;
4623 }
4624 break;
4625
4626 /* fwait */
4627 /* XXX */
4628 case 0x9b:
4629 printf_unfiltered (_("Process record doesn't support instruction "
4630 "fwait.\n"));
4631 ir.addr -= 1;
4632 goto no_support;
4633 break;
4634
4635 /* int3 */
4636 /* XXX */
4637 case 0xcc:
4638 printf_unfiltered (_("Process record doesn't support instruction "
4639 "int3.\n"));
4640 ir.addr -= 1;
4641 goto no_support;
4642 break;
4643
4644 /* int */
4645 /* XXX */
4646 case 0xcd:
4647 {
4648 int ret;
4649 if (target_read_memory (ir.addr, &tmpu8, 1))
4650 {
4651 if (record_debug)
4652 printf_unfiltered (_("Process record: error reading memory "
4653 "at addr 0x%s len = 1.\n"),
4654 paddr_nz (ir.addr));
4655 return -1;
4656 }
4657 ir.addr++;
4658 if (tmpu8 != 0x80
4659 || gdbarch_tdep (gdbarch)->i386_intx80_record == NULL)
4660 {
4661 printf_unfiltered (_("Process record doesn't support "
4662 "instruction int 0x%02x.\n"),
4663 tmpu8);
4664 ir.addr -= 2;
4665 goto no_support;
4666 }
4667 ret = gdbarch_tdep (gdbarch)->i386_intx80_record (ir.regcache);
4668 if (ret)
4669 return ret;
4670 }
4671 break;
4672
4673 /* into */
4674 /* XXX */
4675 case 0xce:
4676 printf_unfiltered (_("Process record doesn't support "
4677 "instruction into.\n"));
4678 ir.addr -= 1;
4679 goto no_support;
4680 break;
4681
4682 /* cli */
4683 case 0xfa:
4684 /* sti */
4685 case 0xfb:
4686 break;
4687
4688 /* bound */
4689 case 0x62:
4690 printf_unfiltered (_("Process record doesn't support "
4691 "instruction bound.\n"));
4692 ir.addr -= 1;
4693 goto no_support;
4694 break;
4695
4696 /* bswap reg */
4697 case 0x0fc8:
4698 case 0x0fc9:
4699 case 0x0fca:
4700 case 0x0fcb:
4701 case 0x0fcc:
4702 case 0x0fcd:
4703 case 0x0fce:
4704 case 0x0fcf:
4705 if (record_arch_list_add_reg (ir.regcache, opcode & 7))
4706 return -1;
4707 break;
4708
4709 /* salc */
4710 case 0xd6:
4711 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4712 return -1;
4713 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4714 return -1;
4715 break;
4716
4717 /* loopnz */
4718 case 0xe0:
4719 /* loopz */
4720 case 0xe1:
4721 /* loop */
4722 case 0xe2:
4723 /* jecxz */
4724 case 0xe3:
4725 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4726 return -1;
4727 break;
4728
4729 /* wrmsr */
4730 case 0x0f30:
4731 printf_unfiltered (_("Process record doesn't support "
4732 "instruction wrmsr.\n"));
4733 ir.addr -= 2;
4734 goto no_support;
4735 break;
4736
4737 /* rdmsr */
4738 case 0x0f32:
4739 printf_unfiltered (_("Process record doesn't support "
4740 "instruction rdmsr.\n"));
4741 ir.addr -= 2;
4742 goto no_support;
4743 break;
4744
4745 /* rdtsc */
4746 case 0x0f31:
4747 printf_unfiltered (_("Process record doesn't support "
4748 "instruction rdtsc.\n"));
4749 ir.addr -= 2;
4750 goto no_support;
4751 break;
4752
4753 /* sysenter */
4754 case 0x0f34:
4755 {
4756 int ret;
4757 if (gdbarch_tdep (gdbarch)->i386_sysenter_record == NULL)
4758 {
4759 printf_unfiltered (_("Process record doesn't support "
4760 "instruction sysenter.\n"));
4761 ir.addr -= 2;
4762 goto no_support;
4763 }
4764 ret = gdbarch_tdep (gdbarch)->i386_sysenter_record (ir.regcache);
4765 if (ret)
4766 return ret;
4767 }
4768 break;
4769
4770 /* sysexit */
4771 case 0x0f35:
4772 printf_unfiltered (_("Process record doesn't support "
4773 "instruction sysexit.\n"));
4774 ir.addr -= 2;
4775 goto no_support;
4776 break;
4777
4778 /* cpuid */
4779 case 0x0fa2:
4780 if (record_arch_list_add_reg (ir.regcache, I386_EAX_REGNUM))
4781 return -1;
4782 if (record_arch_list_add_reg (ir.regcache, I386_ECX_REGNUM))
4783 return -1;
4784 if (record_arch_list_add_reg (ir.regcache, I386_EDX_REGNUM))
4785 return -1;
4786 if (record_arch_list_add_reg (ir.regcache, I386_EBX_REGNUM))
4787 return -1;
4788 break;
4789
4790 /* hlt */
4791 case 0xf4:
4792 printf_unfiltered (_("Process record doesn't support "
4793 "instruction hlt.\n"));
4794 ir.addr -= 1;
4795 goto no_support;
4796 break;
4797
4798 case 0x0f00:
4799 if (i386_record_modrm (&ir))
4800 return -1;
4801 switch (ir.reg)
4802 {
4803 /* sldt */
4804 case 0:
4805 /* str */
4806 case 1:
4807 if (ir.mod == 3)
4808 {
4809 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4810 return -1;
4811 }
4812 else
4813 {
4814 ir.ot = OT_WORD;
4815 if (i386_record_lea_modrm (&ir))
4816 return -1;
4817 }
4818 break;
4819 /* lldt */
4820 case 2:
4821 /* ltr */
4822 case 3:
4823 break;
4824 /* verr */
4825 case 4:
4826 /* verw */
4827 case 5:
4828 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4829 return -1;
4830 break;
4831 default:
4832 ir.addr -= 3;
4833 opcode = opcode << 8 | ir.modrm;
4834 goto no_support;
4835 break;
4836 }
4837 break;
4838
4839 case 0x0f01:
4840 if (i386_record_modrm (&ir))
4841 return -1;
4842 switch (ir.reg)
4843 {
4844 /* sgdt */
4845 case 0:
4846 {
4847 uint32_t addr;
4848
4849 if (ir.mod == 3)
4850 {
4851 ir.addr -= 3;
4852 opcode = opcode << 8 | ir.modrm;
4853 goto no_support;
4854 }
4855
4856 if (ir.override)
4857 {
4858 if (record_debug)
4859 printf_unfiltered (_("Process record ignores the memory "
4860 "change of instruction at "
4861 "address 0x%s because it can't get "
4862 "the value of the segment "
4863 "register.\n"),
4864 paddr_nz (ir.addr));
4865 }
4866 else
4867 {
4868 if (i386_record_lea_modrm_addr (&ir, &addr))
4869 return -1;
4870 if (record_arch_list_add_mem (addr, 2))
4871 return -1;
4872 addr += 2;
4873 if (record_arch_list_add_mem (addr, 4))
4874 return -1;
4875 }
4876 }
4877 break;
4878 case 1:
4879 if (ir.mod == 3)
4880 {
4881 switch (ir.rm)
4882 {
4883 /* monitor */
4884 case 0:
4885 break;
4886 /* mwait */
4887 case 1:
4888 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4889 return -1;
4890 break;
4891 default:
4892 ir.addr -= 3;
4893 opcode = opcode << 8 | ir.modrm;
4894 goto no_support;
4895 break;
4896 }
4897 }
4898 else
4899 {
4900 /* sidt */
4901 if (ir.override)
4902 {
4903 if (record_debug)
4904 printf_unfiltered (_("Process record ignores the memory "
4905 "change of instruction at "
4906 "address 0x%s because it can't get "
4907 "the value of the segment "
4908 "register.\n"),
4909 paddr_nz (ir.addr));
4910 }
4911 else
4912 {
4913 uint32_t addr;
4914
4915 if (i386_record_lea_modrm_addr (&ir, &addr))
4916 return -1;
4917 if (record_arch_list_add_mem (addr, 2))
4918 return -1;
4919 addr += 2;
4920 if (record_arch_list_add_mem (addr, 4))
4921 return -1;
4922 }
4923 }
4924 break;
4925 /* lgdt */
4926 case 2:
4927 /* lidt */
4928 case 3:
4929 /* invlpg */
4930 case 7:
4931 default:
4932 if (ir.mod == 3)
4933 {
4934 ir.addr -= 3;
4935 opcode = opcode << 8 | ir.modrm;
4936 goto no_support;
4937 }
4938 break;
4939 /* smsw */
4940 case 4:
4941 if (ir.mod == 3)
4942 {
4943 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4944 return -1;
4945 }
4946 else
4947 {
4948 ir.ot = OT_WORD;
4949 if (i386_record_lea_modrm (&ir))
4950 return -1;
4951 }
4952 break;
4953 /* lmsw */
4954 case 6:
4955 break;
4956 }
4957 break;
4958
4959 /* invd */
4960 case 0x0f08:
4961 /* wbinvd */
4962 case 0x0f09:
4963 break;
4964
4965 /* arpl */
4966 case 0x63:
4967 ir.ot = ir.dflag ? OT_LONG : OT_WORD;
4968 if (i386_record_modrm (&ir))
4969 return -1;
4970 if (ir.mod != 3)
4971 {
4972 if (i386_record_lea_modrm (&ir))
4973 return -1;
4974 }
4975 else
4976 {
4977 if (record_arch_list_add_reg (ir.regcache, ir.rm))
4978 return -1;
4979 }
4980 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4981 return -1;
4982 break;
4983
4984 /* lar */
4985 case 0x0f02:
4986 /* lsl */
4987 case 0x0f03:
4988 if (i386_record_modrm (&ir))
4989 return -1;
4990 if (record_arch_list_add_reg (ir.regcache, ir.reg))
4991 return -1;
4992 if (record_arch_list_add_reg (ir.regcache, I386_EFLAGS_REGNUM))
4993 return -1;
4994 break;
4995
4996 case 0x0f18:
4997 break;
4998
4999 /* nop (multi byte) */
5000 case 0x0f19:
5001 case 0x0f1a:
5002 case 0x0f1b:
5003 case 0x0f1c:
5004 case 0x0f1d:
5005 case 0x0f1e:
5006 case 0x0f1f:
5007 break;
5008
5009 /* mov reg, crN */
5010 case 0x0f20:
5011 /* mov crN, reg */
5012 case 0x0f22:
5013 if (i386_record_modrm (&ir))
5014 return -1;
5015 if ((ir.modrm & 0xc0) != 0xc0)
5016 {
5017 ir.addr -= 2;
5018 opcode = opcode << 8 | ir.modrm;
5019 goto no_support;
5020 }
5021 switch (ir.reg)
5022 {
5023 case 0:
5024 case 2:
5025 case 3:
5026 case 4:
5027 case 8:
5028 if (opcode & 2)
5029 {
5030 }
5031 else
5032 {
5033 if (record_arch_list_add_reg (ir.regcache, ir.rm))
5034 return -1;
5035 }
5036 break;
5037 default:
5038 ir.addr -= 2;
5039 opcode = opcode << 8 | ir.modrm;
5040 goto no_support;
5041 break;
5042 }
5043 break;
5044
5045 /* mov reg, drN */
5046 case 0x0f21:
5047 /* mov drN, reg */
5048 case 0x0f23:
5049 if (i386_record_modrm (&ir))
5050 return -1;
5051 if ((ir.modrm & 0xc0) != 0xc0 || ir.reg == 4
5052 || ir.reg == 5 || ir.reg >= 8)
5053 {
5054 ir.addr -= 2;
5055 opcode = opcode << 8 | ir.modrm;
5056 goto no_support;
5057 }
5058 if (opcode & 2)
5059 {
5060 }
5061 else
5062 {
5063 if (record_arch_list_add_reg (ir.regcache, ir.rm))
5064 return -1;
5065 }
5066 break;
5067
5068 /* clts */
5069 case 0x0f06:
5070 break;
5071
5072 /* MMX/SSE/SSE2/PNI support */
5073 /* XXX */
5074
5075 default:
5076 if (opcode > 0xff)
5077 ir.addr -= 2;
5078 else
5079 ir.addr -= 1;
5080 goto no_support;
5081 break;
5082 }
5083
5084/* In the future, Maybe still need to deal with need_dasm */
5085 if (record_arch_list_add_reg (ir.regcache, I386_EIP_REGNUM))
5086 return -1;
5087 if (record_arch_list_add_end ())
5088 return -1;
5089
5090 return 0;
5091
5092no_support:
5093 printf_unfiltered (_("Process record doesn't support instruction 0x%02x "
5094 "at address 0x%s.\n"),
5095 (unsigned int) (opcode), paddr_nz (ir.addr));
5096 return -1;
5097}
5098
5099\f
5100static struct gdbarch *
5101i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
5102{
5103 struct gdbarch_tdep *tdep;
5104 struct gdbarch *gdbarch;
5105
5106 /* If there is already a candidate, use it. */
5107 arches = gdbarch_list_lookup_by_info (arches, &info);
5108 if (arches != NULL)
5109 return arches->gdbarch;
5110
5111 /* Allocate space for the new architecture. */
5112 tdep = XCALLOC (1, struct gdbarch_tdep);
5113 gdbarch = gdbarch_alloc (&info, tdep);
5114
5115 /* General-purpose registers. */
5116 tdep->gregset = NULL;
5117 tdep->gregset_reg_offset = NULL;
5118 tdep->gregset_num_regs = I386_NUM_GREGS;
5119 tdep->sizeof_gregset = 0;
5120
5121 /* Floating-point registers. */
5122 tdep->fpregset = NULL;
5123 tdep->sizeof_fpregset = I387_SIZEOF_FSAVE;
5124
5125 /* The default settings include the FPU registers, the MMX registers
5126 and the SSE registers. This can be overridden for a specific ABI
5127 by adjusting the members `st0_regnum', `mm0_regnum' and
5128 `num_xmm_regs' of `struct gdbarch_tdep', otherwise the registers
5129 will show up in the output of "info all-registers". Ideally we
5130 should try to autodetect whether they are available, such that we
5131 can prevent "info all-registers" from displaying registers that
5132 aren't available.
5133
5134 NOTE: kevinb/2003-07-13: ... if it's a choice between printing
5135 [the SSE registers] always (even when they don't exist) or never
5136 showing them to the user (even when they do exist), I prefer the
5137 former over the latter. */
5138
5139 tdep->st0_regnum = I386_ST0_REGNUM;
5140
5141 /* The MMX registers are implemented as pseudo-registers. Put off
5142 calculating the register number for %mm0 until we know the number
5143 of raw registers. */
5144 tdep->mm0_regnum = 0;
5145
5146 /* I386_NUM_XREGS includes %mxcsr, so substract one. */
5147 tdep->num_xmm_regs = I386_NUM_XREGS - 1;
5148
5149 tdep->jb_pc_offset = -1;
5150 tdep->struct_return = pcc_struct_return;
5151 tdep->sigtramp_start = 0;
5152 tdep->sigtramp_end = 0;
5153 tdep->sigtramp_p = i386_sigtramp_p;
5154 tdep->sigcontext_addr = NULL;
5155 tdep->sc_reg_offset = NULL;
5156 tdep->sc_pc_offset = -1;
5157 tdep->sc_sp_offset = -1;
5158
5159 /* The format used for `long double' on almost all i386 targets is
5160 the i387 extended floating-point format. In fact, of all targets
5161 in the GCC 2.95 tree, only OSF/1 does it different, and insists
5162 on having a `long double' that's not `long' at all. */
5163 set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
5164
5165 /* Although the i387 extended floating-point has only 80 significant
5166 bits, a `long double' actually takes up 96, probably to enforce
5167 alignment. */
5168 set_gdbarch_long_double_bit (gdbarch, 96);
5169
5170 /* The default ABI includes general-purpose registers,
5171 floating-point registers, and the SSE registers. */
5172 set_gdbarch_num_regs (gdbarch, I386_SSE_NUM_REGS);
5173 set_gdbarch_register_name (gdbarch, i386_register_name);
5174 set_gdbarch_register_type (gdbarch, i386_register_type);
5175
5176 /* Register numbers of various important registers. */
5177 set_gdbarch_sp_regnum (gdbarch, I386_ESP_REGNUM); /* %esp */
5178 set_gdbarch_pc_regnum (gdbarch, I386_EIP_REGNUM); /* %eip */
5179 set_gdbarch_ps_regnum (gdbarch, I386_EFLAGS_REGNUM); /* %eflags */
5180 set_gdbarch_fp0_regnum (gdbarch, I386_ST0_REGNUM); /* %st(0) */
5181
5182 /* NOTE: kettenis/20040418: GCC does have two possible register
5183 numbering schemes on the i386: dbx and SVR4. These schemes
5184 differ in how they number %ebp, %esp, %eflags, and the
5185 floating-point registers, and are implemented by the arrays
5186 dbx_register_map[] and svr4_dbx_register_map in
5187 gcc/config/i386.c. GCC also defines a third numbering scheme in
5188 gcc/config/i386.c, which it designates as the "default" register
5189 map used in 64bit mode. This last register numbering scheme is
5190 implemented in dbx64_register_map, and is used for AMD64; see
5191 amd64-tdep.c.
5192
5193 Currently, each GCC i386 target always uses the same register
5194 numbering scheme across all its supported debugging formats
5195 i.e. SDB (COFF), stabs and DWARF 2. This is because
5196 gcc/sdbout.c, gcc/dbxout.c and gcc/dwarf2out.c all use the
5197 DBX_REGISTER_NUMBER macro which is defined by each target's
5198 respective config header in a manner independent of the requested
5199 output debugging format.
5200
5201 This does not match the arrangement below, which presumes that
5202 the SDB and stabs numbering schemes differ from the DWARF and
5203 DWARF 2 ones. The reason for this arrangement is that it is
5204 likely to get the numbering scheme for the target's
5205 default/native debug format right. For targets where GCC is the
5206 native compiler (FreeBSD, NetBSD, OpenBSD, GNU/Linux) or for
5207 targets where the native toolchain uses a different numbering
5208 scheme for a particular debug format (stabs-in-ELF on Solaris)
5209 the defaults below will have to be overridden, like
5210 i386_elf_init_abi() does. */
5211
5212 /* Use the dbx register numbering scheme for stabs and COFF. */
5213 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
5214 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_dbx_reg_to_regnum);
5215
5216 /* Use the SVR4 register numbering scheme for DWARF 2. */
5217 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_svr4_reg_to_regnum);
5218
5219 /* We don't set gdbarch_stab_reg_to_regnum, since ECOFF doesn't seem to
5220 be in use on any of the supported i386 targets. */
5221
5222 set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
5223
5224 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
5225
5226 /* Call dummy code. */
5227 set_gdbarch_push_dummy_call (gdbarch, i386_push_dummy_call);
5228
5229 set_gdbarch_convert_register_p (gdbarch, i386_convert_register_p);
5230 set_gdbarch_register_to_value (gdbarch, i386_register_to_value);
5231 set_gdbarch_value_to_register (gdbarch, i386_value_to_register);
5232
5233 set_gdbarch_return_value (gdbarch, i386_return_value);
5234
5235 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
5236
5237 /* Stack grows downward. */
5238 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
5239
5240 set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
5241 set_gdbarch_decr_pc_after_break (gdbarch, 1);
5242 set_gdbarch_max_insn_length (gdbarch, I386_MAX_INSN_LEN);
5243
5244 set_gdbarch_frame_args_skip (gdbarch, 8);
5245
5246 /* Wire in the MMX registers. */
5247 set_gdbarch_num_pseudo_regs (gdbarch, i386_num_mmx_regs);
5248 set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
5249 set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
5250
5251 set_gdbarch_print_insn (gdbarch, i386_print_insn);
5252
5253 set_gdbarch_dummy_id (gdbarch, i386_dummy_id);
5254
5255 set_gdbarch_unwind_pc (gdbarch, i386_unwind_pc);
5256
5257 /* Add the i386 register groups. */
5258 i386_add_reggroups (gdbarch);
5259 set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
38c968cf 5260
143985b7
AF
5261 /* Helper for function argument information. */
5262 set_gdbarch_fetch_pointer_argument (gdbarch, i386_fetch_pointer_argument);
5263
6405b0a6 5264 /* Hook in the DWARF CFI frame unwinder. */
10458914 5265 dwarf2_append_unwinders (gdbarch);
6405b0a6 5266
acd5c798 5267 frame_base_set_default (gdbarch, &i386_frame_base);
6c0e89ed 5268
3ce1502b 5269 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 5270 gdbarch_init_osabi (info, gdbarch);
3ce1502b 5271
10458914
DJ
5272 frame_unwind_append_unwinder (gdbarch, &i386_sigtramp_frame_unwind);
5273 frame_unwind_append_unwinder (gdbarch, &i386_frame_unwind);
acd5c798 5274
8446b36a
MK
5275 /* If we have a register mapping, enable the generic core file
5276 support, unless it has already been enabled. */
5277 if (tdep->gregset_reg_offset
5278 && !gdbarch_regset_from_core_section_p (gdbarch))
5279 set_gdbarch_regset_from_core_section (gdbarch,
5280 i386_regset_from_core_section);
5281
5716833c
MK
5282 /* Unless support for MMX has been disabled, make %mm0 the first
5283 pseudo-register. */
5284 if (tdep->mm0_regnum == 0)
5285 tdep->mm0_regnum = gdbarch_num_regs (gdbarch);
5286
514f746b
AR
5287 set_gdbarch_skip_permanent_breakpoint (gdbarch,
5288 i386_skip_permanent_breakpoint);
5289
a62cc96e
AC
5290 return gdbarch;
5291}
5292
8201327c
MK
5293static enum gdb_osabi
5294i386_coff_osabi_sniffer (bfd *abfd)
5295{
762c5349
MK
5296 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
5297 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
8201327c
MK
5298 return GDB_OSABI_GO32;
5299
5300 return GDB_OSABI_UNKNOWN;
5301}
8201327c
MK
5302\f
5303
28e9e0f0
MK
5304/* Provide a prototype to silence -Wmissing-prototypes. */
5305void _initialize_i386_tdep (void);
5306
c906108c 5307void
fba45db2 5308_initialize_i386_tdep (void)
c906108c 5309{
a62cc96e
AC
5310 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
5311
fc338970 5312 /* Add the variable that controls the disassembly flavor. */
7ab04401
AC
5313 add_setshow_enum_cmd ("disassembly-flavor", no_class, valid_flavors,
5314 &disassembly_flavor, _("\
5315Set the disassembly flavor."), _("\
5316Show the disassembly flavor."), _("\
5317The valid values are \"att\" and \"intel\", and the default value is \"att\"."),
5318 NULL,
5319 NULL, /* FIXME: i18n: */
5320 &setlist, &showlist);
8201327c
MK
5321
5322 /* Add the variable that controls the convention for returning
5323 structs. */
7ab04401
AC
5324 add_setshow_enum_cmd ("struct-convention", no_class, valid_conventions,
5325 &struct_convention, _("\
5326Set the convention for returning small structs."), _("\
5327Show the convention for returning small structs."), _("\
5328Valid values are \"default\", \"pcc\" and \"reg\", and the default value\n\
5329is \"default\"."),
5330 NULL,
5331 NULL, /* FIXME: i18n: */
5332 &setlist, &showlist);
8201327c
MK
5333
5334 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
5335 i386_coff_osabi_sniffer);
8201327c 5336
05816f70 5337 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
8201327c 5338 i386_svr4_init_abi);
05816f70 5339 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
8201327c 5340 i386_go32_init_abi);
38c968cf 5341
5ae96ec1 5342 /* Initialize the i386-specific register groups & types. */
38c968cf 5343 i386_init_reggroups ();
5ae96ec1 5344 i386_init_types();
c906108c 5345}
This page took 1.244418 seconds and 4 git commands to generate.