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