]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/i386-tdep.c
* i386-tdep.c (i386_extract_return_value,
[thirdparty/binutils-gdb.git] / gdb / i386-tdep.c
CommitLineData
c906108c 1/* Intel 386 target-dependent stuff.
349c5d5f
AC
2
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4be87837 4 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "gdb_string.h"
25#include "frame.h"
26#include "inferior.h"
27#include "gdbcore.h"
dfe01d39 28#include "objfiles.h"
c906108c
SS
29#include "target.h"
30#include "floatformat.h"
c0d1d883 31#include "symfile.h"
c906108c
SS
32#include "symtab.h"
33#include "gdbcmd.h"
34#include "command.h"
b4a20239 35#include "arch-utils.h"
4e052eda 36#include "regcache.h"
d16aafd8 37#include "doublest.h"
fd0407d6 38#include "value.h"
3d261580 39#include "gdb_assert.h"
38c968cf 40#include "reggroups.h"
5a4d6ff4 41#include "dummy-frame.h"
4be87837 42#include "osabi.h"
3d261580 43
d2a7c97a 44#include "i386-tdep.h"
61113f8b 45#include "i387-tdep.h"
d2a7c97a 46
fc633446
MK
47/* Names of the registers. The first 10 registers match the register
48 numbering scheme used by GCC for stabs and DWARF. */
49static char *i386_register_names[] =
50{
51 "eax", "ecx", "edx", "ebx",
52 "esp", "ebp", "esi", "edi",
53 "eip", "eflags", "cs", "ss",
54 "ds", "es", "fs", "gs",
55 "st0", "st1", "st2", "st3",
56 "st4", "st5", "st6", "st7",
57 "fctrl", "fstat", "ftag", "fiseg",
58 "fioff", "foseg", "fooff", "fop",
59 "xmm0", "xmm1", "xmm2", "xmm3",
60 "xmm4", "xmm5", "xmm6", "xmm7",
61 "mxcsr"
62};
63
28fc6740
AC
64/* MMX registers. */
65
66static char *i386_mmx_names[] =
67{
68 "mm0", "mm1", "mm2", "mm3",
69 "mm4", "mm5", "mm6", "mm7"
70};
71static const int mmx_num_regs = (sizeof (i386_mmx_names)
72 / sizeof (i386_mmx_names[0]));
73#define MM0_REGNUM (NUM_REGS)
74
75static int
23a34459 76i386_mmx_regnum_p (int reg)
28fc6740
AC
77{
78 return (reg >= MM0_REGNUM && reg < MM0_REGNUM + mmx_num_regs);
79}
80
23a34459
AC
81/* FP register? */
82
83int
84i386_fp_regnum_p (int regnum)
85{
86 return (regnum < NUM_REGS
87 && (FP0_REGNUM && FP0_REGNUM <= (regnum) && (regnum) < FPC_REGNUM));
88}
89
90int
91i386_fpc_regnum_p (int regnum)
92{
93 return (regnum < NUM_REGS
94 && (FPC_REGNUM <= (regnum) && (regnum) < XMM0_REGNUM));
95}
96
97/* SSE register? */
98
99int
100i386_sse_regnum_p (int regnum)
101{
102 return (regnum < NUM_REGS
103 && (XMM0_REGNUM <= (regnum) && (regnum) < MXCSR_REGNUM));
104}
105
106int
107i386_mxcsr_regnum_p (int regnum)
108{
109 return (regnum < NUM_REGS
110 && (regnum == MXCSR_REGNUM));
111}
112
fc633446
MK
113/* Return the name of register REG. */
114
fa88f677 115const char *
fc633446
MK
116i386_register_name (int reg)
117{
118 if (reg < 0)
119 return NULL;
23a34459 120 if (i386_mmx_regnum_p (reg))
28fc6740 121 return i386_mmx_names[reg - MM0_REGNUM];
fc633446
MK
122 if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
123 return NULL;
124
125 return i386_register_names[reg];
126}
127
85540d8c
MK
128/* Convert stabs register number REG to the appropriate register
129 number used by GDB. */
130
8201327c 131static int
85540d8c
MK
132i386_stab_reg_to_regnum (int reg)
133{
134 /* This implements what GCC calls the "default" register map. */
135 if (reg >= 0 && reg <= 7)
136 {
137 /* General registers. */
138 return reg;
139 }
140 else if (reg >= 12 && reg <= 19)
141 {
142 /* Floating-point registers. */
143 return reg - 12 + FP0_REGNUM;
144 }
145 else if (reg >= 21 && reg <= 28)
146 {
147 /* SSE registers. */
148 return reg - 21 + XMM0_REGNUM;
149 }
150 else if (reg >= 29 && reg <= 36)
151 {
152 /* MMX registers. */
7d12f766 153 return reg - 29 + MM0_REGNUM;
85540d8c
MK
154 }
155
156 /* This will hopefully provoke a warning. */
157 return NUM_REGS + NUM_PSEUDO_REGS;
158}
159
8201327c 160/* Convert DWARF register number REG to the appropriate register
85540d8c
MK
161 number used by GDB. */
162
8201327c 163static int
85540d8c
MK
164i386_dwarf_reg_to_regnum (int reg)
165{
166 /* The DWARF register numbering includes %eip and %eflags, and
167 numbers the floating point registers differently. */
168 if (reg >= 0 && reg <= 9)
169 {
170 /* General registers. */
171 return reg;
172 }
173 else if (reg >= 11 && reg <= 18)
174 {
175 /* Floating-point registers. */
176 return reg - 11 + FP0_REGNUM;
177 }
178 else if (reg >= 21)
179 {
180 /* The SSE and MMX registers have identical numbers as in stabs. */
181 return i386_stab_reg_to_regnum (reg);
182 }
183
184 /* This will hopefully provoke a warning. */
185 return NUM_REGS + NUM_PSEUDO_REGS;
186}
fc338970 187\f
917317f4 188
fc338970
MK
189/* This is the variable that is set with "set disassembly-flavor", and
190 its legitimate values. */
53904c9e
AC
191static const char att_flavor[] = "att";
192static const char intel_flavor[] = "intel";
193static const char *valid_flavors[] =
c5aa993b 194{
c906108c
SS
195 att_flavor,
196 intel_flavor,
197 NULL
198};
53904c9e 199static const char *disassembly_flavor = att_flavor;
c906108c 200
fc338970
MK
201/* Stdio style buffering was used to minimize calls to ptrace, but
202 this buffering did not take into account that the code section
203 being accessed may not be an even number of buffers long (even if
204 the buffer is only sizeof(int) long). In cases where the code
205 section size happened to be a non-integral number of buffers long,
206 attempting to read the last buffer would fail. Simply using
207 target_read_memory and ignoring errors, rather than read_memory, is
208 not the correct solution, since legitimate access errors would then
209 be totally ignored. To properly handle this situation and continue
210 to use buffering would require that this code be able to determine
211 the minimum code section size granularity (not the alignment of the
212 section itself, since the actual failing case that pointed out this
213 problem had a section alignment of 4 but was not a multiple of 4
214 bytes long), on a target by target basis, and then adjust it's
215 buffer size accordingly. This is messy, but potentially feasible.
216 It probably needs the bfd library's help and support. For now, the
217 buffer size is set to 1. (FIXME -fnf) */
218
219#define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
c906108c
SS
220static CORE_ADDR codestream_next_addr;
221static CORE_ADDR codestream_addr;
222static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
223static int codestream_off;
224static int codestream_cnt;
225
226#define codestream_tell() (codestream_addr + codestream_off)
fc338970
MK
227#define codestream_peek() \
228 (codestream_cnt == 0 ? \
229 codestream_fill(1) : codestream_buf[codestream_off])
230#define codestream_get() \
231 (codestream_cnt-- == 0 ? \
232 codestream_fill(0) : codestream_buf[codestream_off++])
c906108c 233
c5aa993b 234static unsigned char
fba45db2 235codestream_fill (int peek_flag)
c906108c
SS
236{
237 codestream_addr = codestream_next_addr;
238 codestream_next_addr += CODESTREAM_BUFSIZ;
239 codestream_off = 0;
240 codestream_cnt = CODESTREAM_BUFSIZ;
241 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
c5aa993b 242
c906108c 243 if (peek_flag)
c5aa993b 244 return (codestream_peek ());
c906108c 245 else
c5aa993b 246 return (codestream_get ());
c906108c
SS
247}
248
249static void
fba45db2 250codestream_seek (CORE_ADDR place)
c906108c
SS
251{
252 codestream_next_addr = place / CODESTREAM_BUFSIZ;
253 codestream_next_addr *= CODESTREAM_BUFSIZ;
254 codestream_cnt = 0;
255 codestream_fill (1);
c5aa993b 256 while (codestream_tell () != place)
c906108c
SS
257 codestream_get ();
258}
259
260static void
fba45db2 261codestream_read (unsigned char *buf, int count)
c906108c
SS
262{
263 unsigned char *p;
264 int i;
265 p = buf;
266 for (i = 0; i < count; i++)
267 *p++ = codestream_get ();
268}
fc338970 269\f
c906108c 270
fc338970 271/* If the next instruction is a jump, move to its target. */
c906108c
SS
272
273static void
fba45db2 274i386_follow_jump (void)
c906108c
SS
275{
276 unsigned char buf[4];
277 long delta;
278
279 int data16;
280 CORE_ADDR pos;
281
282 pos = codestream_tell ();
283
284 data16 = 0;
285 if (codestream_peek () == 0x66)
286 {
287 codestream_get ();
288 data16 = 1;
289 }
290
291 switch (codestream_get ())
292 {
293 case 0xe9:
fc338970 294 /* Relative jump: if data16 == 0, disp32, else disp16. */
c906108c
SS
295 if (data16)
296 {
297 codestream_read (buf, 2);
298 delta = extract_signed_integer (buf, 2);
299
fc338970
MK
300 /* Include the size of the jmp instruction (including the
301 0x66 prefix). */
c5aa993b 302 pos += delta + 4;
c906108c
SS
303 }
304 else
305 {
306 codestream_read (buf, 4);
307 delta = extract_signed_integer (buf, 4);
308
309 pos += delta + 5;
310 }
311 break;
312 case 0xeb:
fc338970 313 /* Relative jump, disp8 (ignore data16). */
c906108c
SS
314 codestream_read (buf, 1);
315 /* Sign-extend it. */
316 delta = extract_signed_integer (buf, 1);
317
318 pos += delta + 2;
319 break;
320 }
321 codestream_seek (pos);
322}
323
fc338970
MK
324/* Find & return the amount a local space allocated, and advance the
325 codestream to the first register push (if any).
326
327 If the entry sequence doesn't make sense, return -1, and leave
328 codestream pointer at a random spot. */
c906108c
SS
329
330static long
fba45db2 331i386_get_frame_setup (CORE_ADDR pc)
c906108c
SS
332{
333 unsigned char op;
334
335 codestream_seek (pc);
336
337 i386_follow_jump ();
338
339 op = codestream_get ();
340
341 if (op == 0x58) /* popl %eax */
342 {
fc338970
MK
343 /* This function must start with
344
345 popl %eax 0x58
346 xchgl %eax, (%esp) 0x87 0x04 0x24
347 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
348
349 (the System V compiler puts out the second `xchg'
350 instruction, and the assembler doesn't try to optimize it, so
351 the 'sib' form gets generated). This sequence is used to get
352 the address of the return buffer for a function that returns
353 a structure. */
c906108c
SS
354 int pos;
355 unsigned char buf[4];
fc338970
MK
356 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
357 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
358
c906108c
SS
359 pos = codestream_tell ();
360 codestream_read (buf, 4);
361 if (memcmp (buf, proto1, 3) == 0)
362 pos += 3;
363 else if (memcmp (buf, proto2, 4) == 0)
364 pos += 4;
365
366 codestream_seek (pos);
fc338970 367 op = codestream_get (); /* Update next opcode. */
c906108c
SS
368 }
369
370 if (op == 0x68 || op == 0x6a)
371 {
fc338970
MK
372 /* This function may start with
373
374 pushl constant
375 call _probe
376 addl $4, %esp
377
378 followed by
379
380 pushl %ebp
381
382 etc. */
c906108c
SS
383 int pos;
384 unsigned char buf[8];
385
fc338970 386 /* Skip past the `pushl' instruction; it has either a one-byte
c906108c
SS
387 or a four-byte operand, depending on the opcode. */
388 pos = codestream_tell ();
389 if (op == 0x68)
390 pos += 4;
391 else
392 pos += 1;
393 codestream_seek (pos);
394
fc338970
MK
395 /* Read the following 8 bytes, which should be "call _probe" (6
396 bytes) followed by "addl $4,%esp" (2 bytes). */
c906108c
SS
397 codestream_read (buf, sizeof (buf));
398 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
399 pos += sizeof (buf);
400 codestream_seek (pos);
fc338970 401 op = codestream_get (); /* Update next opcode. */
c906108c
SS
402 }
403
404 if (op == 0x55) /* pushl %ebp */
c5aa993b 405 {
fc338970 406 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
c906108c
SS
407 switch (codestream_get ())
408 {
409 case 0x8b:
410 if (codestream_get () != 0xec)
fc338970 411 return -1;
c906108c
SS
412 break;
413 case 0x89:
414 if (codestream_get () != 0xe5)
fc338970 415 return -1;
c906108c
SS
416 break;
417 default:
fc338970 418 return -1;
c906108c 419 }
fc338970
MK
420 /* Check for stack adjustment
421
422 subl $XXX, %esp
423
424 NOTE: You can't subtract a 16 bit immediate from a 32 bit
425 reg, so we don't have to worry about a data16 prefix. */
c906108c
SS
426 op = codestream_peek ();
427 if (op == 0x83)
428 {
fc338970 429 /* `subl' with 8 bit immediate. */
c906108c
SS
430 codestream_get ();
431 if (codestream_get () != 0xec)
fc338970 432 /* Some instruction starting with 0x83 other than `subl'. */
c906108c
SS
433 {
434 codestream_seek (codestream_tell () - 2);
435 return 0;
436 }
fc338970
MK
437 /* `subl' with signed byte immediate (though it wouldn't
438 make sense to be negative). */
c5aa993b 439 return (codestream_get ());
c906108c
SS
440 }
441 else if (op == 0x81)
442 {
443 char buf[4];
fc338970 444 /* Maybe it is `subl' with a 32 bit immedediate. */
c5aa993b 445 codestream_get ();
c906108c 446 if (codestream_get () != 0xec)
fc338970 447 /* Some instruction starting with 0x81 other than `subl'. */
c906108c
SS
448 {
449 codestream_seek (codestream_tell () - 2);
450 return 0;
451 }
fc338970 452 /* It is `subl' with a 32 bit immediate. */
c5aa993b 453 codestream_read ((unsigned char *) buf, 4);
c906108c
SS
454 return extract_signed_integer (buf, 4);
455 }
456 else
457 {
fc338970 458 return 0;
c906108c
SS
459 }
460 }
461 else if (op == 0xc8)
462 {
463 char buf[2];
fc338970 464 /* `enter' with 16 bit unsigned immediate. */
c5aa993b 465 codestream_read ((unsigned char *) buf, 2);
fc338970 466 codestream_get (); /* Flush final byte of enter instruction. */
c906108c
SS
467 return extract_unsigned_integer (buf, 2);
468 }
469 return (-1);
470}
471
6bff26de
MK
472/* Signal trampolines don't have a meaningful frame. The frame
473 pointer value we use is actually the frame pointer of the calling
474 frame -- that is, the frame which was in progress when the signal
475 trampoline was entered. GDB mostly treats this frame pointer value
476 as a magic cookie. We detect the case of a signal trampoline by
5a203e44
AC
477 testing for get_frame_type() == SIGTRAMP_FRAME, which is set based
478 on PC_IN_SIGTRAMP.
6bff26de
MK
479
480 When a signal trampoline is invoked from a frameless function, we
481 essentially have two frameless functions in a row. In this case,
482 we use the same magic cookie for three frames in a row. We detect
5a203e44
AC
483 this case by seeing whether the next frame is a SIGTRAMP_FRAME,
484 and, if it does, checking whether the current frame is actually
485 frameless. In this case, we need to get the PC by looking at the
486 SP register value stored in the signal context.
6bff26de
MK
487
488 This should work in most cases except in horrible situations where
489 a signal occurs just as we enter a function but before the frame
c0d1d883
MK
490 has been set up. Incidentally, that's just what happens when we
491 call a function from GDB with a signal pending (there's a test in
492 the testsuite that makes this happen). Therefore we pretend that
493 we have a frameless function if we're stopped at the start of a
494 function. */
6bff26de
MK
495
496/* Return non-zero if we're dealing with a frameless signal, that is,
497 a signal trampoline invoked from a frameless function. */
498
5512c44a 499int
6bff26de
MK
500i386_frameless_signal_p (struct frame_info *frame)
501{
11c02a10
AC
502 return (get_next_frame (frame)
503 && get_frame_type (get_next_frame (frame)) == SIGTRAMP_FRAME
c0d1d883 504 && (frameless_look_for_prologue (frame)
be41e9f4 505 || get_frame_pc (frame) == get_frame_func (frame)));
6bff26de
MK
506}
507
c833a37e
MK
508/* Return the chain-pointer for FRAME. In the case of the i386, the
509 frame's nominal address is the address of a 4-byte word containing
510 the calling frame's address. */
511
8201327c 512static CORE_ADDR
c833a37e
MK
513i386_frame_chain (struct frame_info *frame)
514{
50abf9e5 515 if (pc_in_dummy_frame (get_frame_pc (frame)))
1e2330ba 516 return get_frame_base (frame);
c0d1d883 517
5a203e44 518 if (get_frame_type (frame) == SIGTRAMP_FRAME
6bff26de 519 || i386_frameless_signal_p (frame))
1e2330ba 520 return get_frame_base (frame);
c833a37e 521
50abf9e5 522 if (! inside_entry_file (get_frame_pc (frame)))
1e2330ba 523 return read_memory_unsigned_integer (get_frame_base (frame), 4);
c833a37e
MK
524
525 return 0;
526}
527
539ffe0b
MK
528/* Determine whether the function invocation represented by FRAME does
529 not have a from on the stack associated with it. If it does not,
530 return non-zero, otherwise return zero. */
531
3a1e71e3 532static int
539ffe0b
MK
533i386_frameless_function_invocation (struct frame_info *frame)
534{
5a203e44 535 if (get_frame_type (frame) == SIGTRAMP_FRAME)
539ffe0b
MK
536 return 0;
537
538 return frameless_look_for_prologue (frame);
539}
540
21d0e8a4
MK
541/* Assuming FRAME is for a sigtramp routine, return the saved program
542 counter. */
543
544static CORE_ADDR
545i386_sigtramp_saved_pc (struct frame_info *frame)
546{
547 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
548 CORE_ADDR addr;
549
550 addr = tdep->sigcontext_addr (frame);
551 return read_memory_unsigned_integer (addr + tdep->sc_pc_offset, 4);
552}
553
6bff26de
MK
554/* Assuming FRAME is for a sigtramp routine, return the saved stack
555 pointer. */
556
557static CORE_ADDR
558i386_sigtramp_saved_sp (struct frame_info *frame)
559{
560 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
561 CORE_ADDR addr;
562
563 addr = tdep->sigcontext_addr (frame);
564 return read_memory_unsigned_integer (addr + tdep->sc_sp_offset, 4);
565}
566
0d17c81d
MK
567/* Return the saved program counter for FRAME. */
568
8201327c 569static CORE_ADDR
0d17c81d
MK
570i386_frame_saved_pc (struct frame_info *frame)
571{
50abf9e5 572 if (pc_in_dummy_frame (get_frame_pc (frame)))
267bf4bb
MK
573 {
574 ULONGEST pc;
575
576 frame_unwind_unsigned_register (frame, PC_REGNUM, &pc);
577 return pc;
578 }
c0d1d883 579
5a203e44 580 if (get_frame_type (frame) == SIGTRAMP_FRAME)
21d0e8a4 581 return i386_sigtramp_saved_pc (frame);
0d17c81d 582
6bff26de
MK
583 if (i386_frameless_signal_p (frame))
584 {
11c02a10 585 CORE_ADDR sp = i386_sigtramp_saved_sp (get_next_frame (frame));
6bff26de
MK
586 return read_memory_unsigned_integer (sp, 4);
587 }
588
1e2330ba 589 return read_memory_unsigned_integer (get_frame_base (frame) + 4, 4);
22797942
AC
590}
591
ed84f6c1
MK
592/* Immediately after a function call, return the saved pc. */
593
8201327c 594static CORE_ADDR
ed84f6c1
MK
595i386_saved_pc_after_call (struct frame_info *frame)
596{
5a203e44 597 if (get_frame_type (frame) == SIGTRAMP_FRAME)
6bff26de
MK
598 return i386_sigtramp_saved_pc (frame);
599
ed84f6c1
MK
600 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
601}
602
fc338970
MK
603/* Parse the first few instructions the function to see what registers
604 were stored.
605
606 We handle these cases:
607
608 The startup sequence can be at the start of the function, or the
609 function can start with a branch to startup code at the end.
610
611 %ebp can be set up with either the 'enter' instruction, or "pushl
612 %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
613 once used in the System V compiler).
614
615 Local space is allocated just below the saved %ebp by either the
616 'enter' instruction, or by "subl $<size>, %esp". 'enter' has a 16
617 bit unsigned argument for space to allocate, and the 'addl'
618 instruction could have either a signed byte, or 32 bit immediate.
619
620 Next, the registers used by this function are pushed. With the
621 System V compiler they will always be in the order: %edi, %esi,
622 %ebx (and sometimes a harmless bug causes it to also save but not
623 restore %eax); however, the code below is willing to see the pushes
624 in any order, and will handle up to 8 of them.
625
626 If the setup sequence is at the end of the function, then the next
627 instruction will be a branch back to the start. */
c906108c 628
3a1e71e3 629static void
fba45db2 630i386_frame_init_saved_regs (struct frame_info *fip)
c906108c
SS
631{
632 long locals = -1;
633 unsigned char op;
fc338970 634 CORE_ADDR addr;
c906108c
SS
635 CORE_ADDR pc;
636 int i;
c5aa993b 637
b2fb4676 638 if (get_frame_saved_regs (fip))
1211c4e4
AC
639 return;
640
641 frame_saved_regs_zalloc (fip);
c5aa993b 642
be41e9f4 643 pc = get_frame_func (fip);
c906108c
SS
644 if (pc != 0)
645 locals = i386_get_frame_setup (pc);
c5aa993b
JM
646
647 if (locals >= 0)
c906108c 648 {
1e2330ba 649 addr = get_frame_base (fip) - 4 - locals;
c5aa993b 650 for (i = 0; i < 8; i++)
c906108c
SS
651 {
652 op = codestream_get ();
653 if (op < 0x50 || op > 0x57)
654 break;
655#ifdef I386_REGNO_TO_SYMMETRY
656 /* Dynix uses different internal numbering. Ick. */
b2fb4676 657 get_frame_saved_regs (fip)[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
c906108c 658#else
b2fb4676 659 get_frame_saved_regs (fip)[op - 0x50] = addr;
c906108c 660#endif
fc338970 661 addr -= 4;
c906108c
SS
662 }
663 }
c5aa993b 664
1e2330ba
AC
665 get_frame_saved_regs (fip)[PC_REGNUM] = get_frame_base (fip) + 4;
666 get_frame_saved_regs (fip)[FP_REGNUM] = get_frame_base (fip);
c906108c
SS
667}
668
fc338970 669/* Return PC of first real instruction. */
c906108c 670
3a1e71e3 671static CORE_ADDR
93924b6b 672i386_skip_prologue (CORE_ADDR pc)
c906108c
SS
673{
674 unsigned char op;
675 int i;
c5aa993b 676 static unsigned char pic_pat[6] =
fc338970
MK
677 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
678 0x5b, /* popl %ebx */
c5aa993b 679 };
c906108c 680 CORE_ADDR pos;
c5aa993b 681
c906108c
SS
682 if (i386_get_frame_setup (pc) < 0)
683 return (pc);
c5aa993b 684
fc338970
MK
685 /* Found valid frame setup -- codestream now points to start of push
686 instructions for saving registers. */
c5aa993b 687
fc338970 688 /* Skip over register saves. */
c906108c
SS
689 for (i = 0; i < 8; i++)
690 {
691 op = codestream_peek ();
fc338970 692 /* Break if not `pushl' instrunction. */
c5aa993b 693 if (op < 0x50 || op > 0x57)
c906108c
SS
694 break;
695 codestream_get ();
696 }
697
fc338970
MK
698 /* The native cc on SVR4 in -K PIC mode inserts the following code
699 to get the address of the global offset table (GOT) into register
700 %ebx
701
702 call 0x0
703 popl %ebx
704 movl %ebx,x(%ebp) (optional)
705 addl y,%ebx
706
c906108c
SS
707 This code is with the rest of the prologue (at the end of the
708 function), so we have to skip it to get to the first real
709 instruction at the start of the function. */
c5aa993b 710
c906108c
SS
711 pos = codestream_tell ();
712 for (i = 0; i < 6; i++)
713 {
714 op = codestream_get ();
c5aa993b 715 if (pic_pat[i] != op)
c906108c
SS
716 break;
717 }
718 if (i == 6)
719 {
720 unsigned char buf[4];
721 long delta = 6;
722
723 op = codestream_get ();
c5aa993b 724 if (op == 0x89) /* movl %ebx, x(%ebp) */
c906108c
SS
725 {
726 op = codestream_get ();
fc338970 727 if (op == 0x5d) /* One byte offset from %ebp. */
c906108c
SS
728 {
729 delta += 3;
730 codestream_read (buf, 1);
731 }
fc338970 732 else if (op == 0x9d) /* Four byte offset from %ebp. */
c906108c
SS
733 {
734 delta += 6;
735 codestream_read (buf, 4);
736 }
fc338970 737 else /* Unexpected instruction. */
c5aa993b
JM
738 delta = -1;
739 op = codestream_get ();
c906108c 740 }
c5aa993b
JM
741 /* addl y,%ebx */
742 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
c906108c 743 {
c5aa993b 744 pos += delta + 6;
c906108c
SS
745 }
746 }
747 codestream_seek (pos);
c5aa993b 748
c906108c 749 i386_follow_jump ();
c5aa993b 750
c906108c
SS
751 return (codestream_tell ());
752}
753
93924b6b
MK
754/* Use the program counter to determine the contents and size of a
755 breakpoint instruction. Return a pointer to a string of bytes that
756 encode a breakpoint instruction, store the length of the string in
757 *LEN and optionally adjust *PC to point to the correct memory
758 location for inserting the breakpoint.
759
760 On the i386 we have a single breakpoint that fits in a single byte
761 and can be inserted anywhere. */
762
763static const unsigned char *
764i386_breakpoint_from_pc (CORE_ADDR *pc, int *len)
765{
766 static unsigned char break_insn[] = { 0xcc }; /* int 3 */
767
768 *len = sizeof (break_insn);
769 return break_insn;
770}
771
c0d1d883
MK
772/* Push the return address (pointing to the call dummy) onto the stack
773 and return the new value for the stack pointer. */
c5aa993b 774
c0d1d883
MK
775static CORE_ADDR
776i386_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
a7769679 777{
c0d1d883 778 char buf[4];
a7769679 779
c0d1d883
MK
780 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
781 write_memory (sp - 4, buf, 4);
782 return sp - 4;
a7769679
MK
783}
784
3a1e71e3 785static void
c0d1d883 786i386_do_pop_frame (struct frame_info *frame)
c906108c 787{
c906108c
SS
788 CORE_ADDR fp;
789 int regnum;
00f8375e 790 char regbuf[I386_MAX_REGISTER_SIZE];
c5aa993b 791
c193f6ac 792 fp = get_frame_base (frame);
1211c4e4
AC
793 i386_frame_init_saved_regs (frame);
794
c5aa993b 795 for (regnum = 0; regnum < NUM_REGS; regnum++)
c906108c 796 {
fc338970 797 CORE_ADDR addr;
b2fb4676 798 addr = get_frame_saved_regs (frame)[regnum];
fc338970 799 if (addr)
c906108c 800 {
fc338970 801 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
4caf0990 802 deprecated_write_register_gen (regnum, regbuf);
c906108c
SS
803 }
804 }
805 write_register (FP_REGNUM, read_memory_integer (fp, 4));
806 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
807 write_register (SP_REGNUM, fp + 8);
808 flush_cached_frames ();
809}
c0d1d883
MK
810
811static void
812i386_pop_frame (void)
813{
814 generic_pop_current_frame (i386_do_pop_frame);
815}
fc338970 816\f
c906108c 817
fc338970
MK
818/* Figure out where the longjmp will land. Slurp the args out of the
819 stack. We expect the first arg to be a pointer to the jmp_buf
8201327c 820 structure from which we extract the address that we will land at.
28bcfd30 821 This address is copied into PC. This routine returns non-zero on
fc338970 822 success. */
c906108c 823
8201327c
MK
824static int
825i386_get_longjmp_target (CORE_ADDR *pc)
c906108c 826{
28bcfd30 827 char buf[8];
c906108c 828 CORE_ADDR sp, jb_addr;
8201327c 829 int jb_pc_offset = gdbarch_tdep (current_gdbarch)->jb_pc_offset;
f9d3c2a8 830 int len = TYPE_LENGTH (builtin_type_void_func_ptr);
c906108c 831
8201327c
MK
832 /* If JB_PC_OFFSET is -1, we have no way to find out where the
833 longjmp will land. */
834 if (jb_pc_offset == -1)
c906108c
SS
835 return 0;
836
8201327c 837 sp = read_register (SP_REGNUM);
28bcfd30 838 if (target_read_memory (sp + len, buf, len))
c906108c
SS
839 return 0;
840
f9d3c2a8 841 jb_addr = extract_typed_address (buf, builtin_type_void_func_ptr);
28bcfd30 842 if (target_read_memory (jb_addr + jb_pc_offset, buf, len))
8201327c 843 return 0;
c906108c 844
f9d3c2a8 845 *pc = extract_typed_address (buf, builtin_type_void_func_ptr);
c906108c
SS
846 return 1;
847}
fc338970 848\f
c906108c 849
3a1e71e3 850static CORE_ADDR
ea7c478f 851i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
22f8ba57
MK
852 int struct_return, CORE_ADDR struct_addr)
853{
b81774d8 854 sp = legacy_push_arguments (nargs, args, sp, struct_return, struct_addr);
22f8ba57
MK
855
856 if (struct_return)
857 {
858 char buf[4];
859
860 sp -= 4;
861 store_address (buf, 4, struct_addr);
862 write_memory (sp, buf, 4);
863 }
864
865 return sp;
866}
867
1a309862
MK
868/* These registers are used for returning integers (and on some
869 targets also for returning `struct' and `union' values when their
ef9dff19 870 size and alignment match an integer type). */
1a309862
MK
871#define LOW_RETURN_REGNUM 0 /* %eax */
872#define HIGH_RETURN_REGNUM 2 /* %edx */
873
874/* Extract from an array REGBUF containing the (raw) register state, a
875 function return value of TYPE, and copy that, in virtual format,
876 into VALBUF. */
877
3a1e71e3 878static void
00f8375e 879i386_extract_return_value (struct type *type, struct regcache *regcache,
ebba8386 880 void *dst)
c906108c 881{
ebba8386 882 bfd_byte *valbuf = dst;
1a309862 883 int len = TYPE_LENGTH (type);
00f8375e 884 char buf[I386_MAX_REGISTER_SIZE];
1a309862 885
1e8d0a7b
MK
886 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
887 && TYPE_NFIELDS (type) == 1)
3df1b9b4 888 {
00f8375e 889 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
3df1b9b4
MK
890 return;
891 }
1e8d0a7b
MK
892
893 if (TYPE_CODE (type) == TYPE_CODE_FLT)
c906108c 894 {
94ea66b3 895 if (FP0_REGNUM < 0)
1a309862
MK
896 {
897 warning ("Cannot find floating-point return value.");
898 memset (valbuf, 0, len);
ef9dff19 899 return;
1a309862
MK
900 }
901
c6ba6f0d
MK
902 /* Floating-point return values can be found in %st(0). Convert
903 its contents to the desired type. This is probably not
904 exactly how it would happen on the target itself, but it is
905 the best we can do. */
0818c12a 906 regcache_raw_read (regcache, FP0_REGNUM, buf);
00f8375e 907 convert_typed_floating (buf, builtin_type_i387_ext, valbuf, type);
c906108c
SS
908 }
909 else
c5aa993b 910 {
d4f3574e
SS
911 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
912 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
913
914 if (len <= low_size)
00f8375e 915 {
0818c12a 916 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e
MK
917 memcpy (valbuf, buf, len);
918 }
d4f3574e
SS
919 else if (len <= (low_size + high_size))
920 {
0818c12a 921 regcache_raw_read (regcache, LOW_RETURN_REGNUM, buf);
00f8375e 922 memcpy (valbuf, buf, low_size);
0818c12a 923 regcache_raw_read (regcache, HIGH_RETURN_REGNUM, buf);
00f8375e 924 memcpy (valbuf + low_size, buf, len - low_size);
d4f3574e
SS
925 }
926 else
8e65ff28
AC
927 internal_error (__FILE__, __LINE__,
928 "Cannot extract return value of %d bytes long.", len);
c906108c
SS
929 }
930}
931
ef9dff19
MK
932/* Write into the appropriate registers a function return value stored
933 in VALBUF of type TYPE, given in virtual format. */
934
3a1e71e3 935static void
3d7f4f49
MK
936i386_store_return_value (struct type *type, struct regcache *regcache,
937 const void *valbuf)
ef9dff19
MK
938{
939 int len = TYPE_LENGTH (type);
940
1e8d0a7b
MK
941 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
942 && TYPE_NFIELDS (type) == 1)
3df1b9b4 943 {
3d7f4f49 944 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), regcache, valbuf);
3df1b9b4
MK
945 return;
946 }
1e8d0a7b
MK
947
948 if (TYPE_CODE (type) == TYPE_CODE_FLT)
ef9dff19 949 {
3d7f4f49 950 ULONGEST fstat;
c6ba6f0d 951 char buf[FPU_REG_RAW_SIZE];
ccb945b8 952
94ea66b3 953 if (FP0_REGNUM < 0)
ef9dff19
MK
954 {
955 warning ("Cannot set floating-point return value.");
956 return;
957 }
958
635b0cc1
MK
959 /* Returning floating-point values is a bit tricky. Apart from
960 storing the return value in %st(0), we have to simulate the
961 state of the FPU at function return point. */
962
c6ba6f0d
MK
963 /* Convert the value found in VALBUF to the extended
964 floating-point format used by the FPU. This is probably
965 not exactly how it would happen on the target itself, but
966 it is the best we can do. */
967 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
3d7f4f49 968 regcache_raw_write (regcache, FP0_REGNUM, buf);
ccb945b8 969
635b0cc1
MK
970 /* Set the top of the floating-point register stack to 7. The
971 actual value doesn't really matter, but 7 is what a normal
972 function return would end up with if the program started out
973 with a freshly initialized FPU. */
3d7f4f49 974 regcache_raw_read_unsigned (regcache, FSTAT_REGNUM, &fstat);
ccb945b8 975 fstat |= (7 << 11);
3d7f4f49 976 regcache_raw_write_unsigned (regcache, FSTAT_REGNUM, fstat);
ccb945b8 977
635b0cc1
MK
978 /* Mark %st(1) through %st(7) as empty. Since we set the top of
979 the floating-point register stack to 7, the appropriate value
980 for the tag word is 0x3fff. */
3d7f4f49 981 regcache_raw_write_unsigned (regcache, FTAG_REGNUM, 0x3fff);
ef9dff19
MK
982 }
983 else
984 {
985 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
986 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
987
988 if (len <= low_size)
3d7f4f49 989 regcache_raw_write_part (regcache, LOW_RETURN_REGNUM, 0, len, valbuf);
ef9dff19
MK
990 else if (len <= (low_size + high_size))
991 {
3d7f4f49
MK
992 regcache_raw_write (regcache, LOW_RETURN_REGNUM, valbuf);
993 regcache_raw_write_part (regcache, HIGH_RETURN_REGNUM, 0,
994 len - low_size, (char *) valbuf + low_size);
ef9dff19
MK
995 }
996 else
8e65ff28
AC
997 internal_error (__FILE__, __LINE__,
998 "Cannot store return value of %d bytes long.", len);
ef9dff19
MK
999 }
1000}
f7af9647 1001
751f1375
MK
1002/* Extract from REGCACHE, which contains the (raw) register state, the
1003 address in which a function should return its structure value, as a
1004 CORE_ADDR. */
f7af9647 1005
3a1e71e3 1006static CORE_ADDR
00f8375e 1007i386_extract_struct_value_address (struct regcache *regcache)
f7af9647 1008{
751f1375
MK
1009 ULONGEST addr;
1010
1011 regcache_raw_read_unsigned (regcache, LOW_RETURN_REGNUM, &addr);
1012 return addr;
f7af9647 1013}
fc338970 1014\f
ef9dff19 1015
8201327c
MK
1016/* This is the variable that is set with "set struct-convention", and
1017 its legitimate values. */
1018static const char default_struct_convention[] = "default";
1019static const char pcc_struct_convention[] = "pcc";
1020static const char reg_struct_convention[] = "reg";
1021static const char *valid_conventions[] =
1022{
1023 default_struct_convention,
1024 pcc_struct_convention,
1025 reg_struct_convention,
1026 NULL
1027};
1028static const char *struct_convention = default_struct_convention;
1029
1030static int
1031i386_use_struct_convention (int gcc_p, struct type *type)
1032{
1033 enum struct_return struct_return;
1034
1035 if (struct_convention == default_struct_convention)
1036 struct_return = gdbarch_tdep (current_gdbarch)->struct_return;
1037 else if (struct_convention == pcc_struct_convention)
1038 struct_return = pcc_struct_return;
1039 else
1040 struct_return = reg_struct_return;
1041
1042 return generic_use_struct_convention (struct_return == reg_struct_return,
1043 type);
1044}
1045\f
1046
d7a0d72c
MK
1047/* Return the GDB type object for the "standard" data type of data in
1048 register REGNUM. Perhaps %esi and %edi should go here, but
1049 potentially they could be used for things other than address. */
1050
3a1e71e3 1051static struct type *
4e259f09 1052i386_register_type (struct gdbarch *gdbarch, int regnum)
d7a0d72c
MK
1053{
1054 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1055 return lookup_pointer_type (builtin_type_void);
1056
23a34459 1057 if (i386_fp_regnum_p (regnum))
c6ba6f0d 1058 return builtin_type_i387_ext;
d7a0d72c 1059
23a34459 1060 if (i386_sse_regnum_p (regnum))
3139facc 1061 return builtin_type_vec128i;
d7a0d72c 1062
23a34459 1063 if (i386_mmx_regnum_p (regnum))
28fc6740
AC
1064 return builtin_type_vec64i;
1065
d7a0d72c
MK
1066 return builtin_type_int;
1067}
1068
28fc6740 1069/* Map a cooked register onto a raw register or memory. For the i386,
c86c27af 1070 the MMX registers need to be mapped onto floating-point registers. */
28fc6740
AC
1071
1072static int
c86c27af 1073i386_mmx_regnum_to_fp_regnum (struct regcache *regcache, int regnum)
28fc6740
AC
1074{
1075 int mmxi;
1076 ULONGEST fstat;
1077 int tos;
1078 int fpi;
c86c27af 1079
28fc6740
AC
1080 mmxi = regnum - MM0_REGNUM;
1081 regcache_raw_read_unsigned (regcache, FSTAT_REGNUM, &fstat);
1082 tos = (fstat >> 11) & 0x7;
1083 fpi = (mmxi + tos) % 8;
c86c27af 1084
28fc6740
AC
1085 return (FP0_REGNUM + fpi);
1086}
1087
1088static void
1089i386_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1090 int regnum, void *buf)
1091{
23a34459 1092 if (i386_mmx_regnum_p (regnum))
28fc6740
AC
1093 {
1094 char *mmx_buf = alloca (MAX_REGISTER_RAW_SIZE);
c86c27af
MK
1095 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
1096
28fc6740 1097 /* Extract (always little endian). */
c86c27af 1098 regcache_raw_read (regcache, fpnum, mmx_buf);
28fc6740
AC
1099 memcpy (buf, mmx_buf, REGISTER_RAW_SIZE (regnum));
1100 }
1101 else
1102 regcache_raw_read (regcache, regnum, buf);
1103}
1104
1105static void
1106i386_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1107 int regnum, const void *buf)
1108{
23a34459 1109 if (i386_mmx_regnum_p (regnum))
28fc6740
AC
1110 {
1111 char *mmx_buf = alloca (MAX_REGISTER_RAW_SIZE);
c86c27af
MK
1112 int fpnum = i386_mmx_regnum_to_fp_regnum (regcache, regnum);
1113
28fc6740
AC
1114 /* Read ... */
1115 regcache_raw_read (regcache, fpnum, mmx_buf);
1116 /* ... Modify ... (always little endian). */
1117 memcpy (mmx_buf, buf, REGISTER_RAW_SIZE (regnum));
1118 /* ... Write. */
1119 regcache_raw_write (regcache, fpnum, mmx_buf);
1120 }
1121 else
1122 regcache_raw_write (regcache, regnum, buf);
1123}
1124
d7a0d72c
MK
1125/* Return true iff register REGNUM's virtual format is different from
1126 its raw format. Note that this definition assumes that the host
1127 supports IEEE 32-bit floats, since it doesn't say that SSE
1128 registers need conversion. Even if we can't find a counterexample,
1129 this is still sloppy. */
1130
3a1e71e3 1131static int
d7a0d72c
MK
1132i386_register_convertible (int regnum)
1133{
23a34459 1134 return i386_fp_regnum_p (regnum);
d7a0d72c
MK
1135}
1136
ac27f131 1137/* Convert data from raw format for register REGNUM in buffer FROM to
3d261580 1138 virtual format with type TYPE in buffer TO. */
ac27f131 1139
3a1e71e3 1140static void
ac27f131
MK
1141i386_register_convert_to_virtual (int regnum, struct type *type,
1142 char *from, char *to)
1143{
23a34459 1144 gdb_assert (i386_fp_regnum_p (regnum));
3d261580
MK
1145
1146 /* We only support floating-point values. */
8d7f6b4a
MK
1147 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1148 {
1149 warning ("Cannot convert floating-point register value "
1150 "to non-floating-point type.");
1151 memset (to, 0, TYPE_LENGTH (type));
1152 return;
1153 }
3d261580 1154
c6ba6f0d
MK
1155 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1156 the extended floating-point format used by the FPU. */
1157 convert_typed_floating (from, builtin_type_i387_ext, to, type);
ac27f131
MK
1158}
1159
1160/* Convert data from virtual format with type TYPE in buffer FROM to
3d261580 1161 raw format for register REGNUM in buffer TO. */
ac27f131 1162
3a1e71e3 1163static void
ac27f131
MK
1164i386_register_convert_to_raw (struct type *type, int regnum,
1165 char *from, char *to)
1166{
23a34459 1167 gdb_assert (i386_fp_regnum_p (regnum));
c6ba6f0d
MK
1168
1169 /* We only support floating-point values. */
1170 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1171 {
1172 warning ("Cannot convert non-floating-point type "
1173 "to floating-point register value.");
1174 memset (to, 0, TYPE_LENGTH (type));
1175 return;
1176 }
3d261580 1177
c6ba6f0d
MK
1178 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1179 to the extended floating-point format used by the FPU. */
1180 convert_typed_floating (from, type, to, builtin_type_i387_ext);
ac27f131 1181}
ac27f131 1182\f
fc338970 1183
c906108c 1184#ifdef STATIC_TRANSFORM_NAME
fc338970
MK
1185/* SunPRO encodes the static variables. This is not related to C++
1186 mangling, it is done for C too. */
c906108c
SS
1187
1188char *
fba45db2 1189sunpro_static_transform_name (char *name)
c906108c
SS
1190{
1191 char *p;
1192 if (IS_STATIC_TRANSFORM_NAME (name))
1193 {
fc338970
MK
1194 /* For file-local statics there will be a period, a bunch of
1195 junk (the contents of which match a string given in the
c5aa993b
JM
1196 N_OPT), a period and the name. For function-local statics
1197 there will be a bunch of junk (which seems to change the
1198 second character from 'A' to 'B'), a period, the name of the
1199 function, and the name. So just skip everything before the
1200 last period. */
c906108c
SS
1201 p = strrchr (name, '.');
1202 if (p != NULL)
1203 name = p + 1;
1204 }
1205 return name;
1206}
1207#endif /* STATIC_TRANSFORM_NAME */
fc338970 1208\f
c906108c 1209
fc338970 1210/* Stuff for WIN32 PE style DLL's but is pretty generic really. */
c906108c
SS
1211
1212CORE_ADDR
1cce71eb 1213i386_pe_skip_trampoline_code (CORE_ADDR pc, char *name)
c906108c 1214{
fc338970 1215 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
c906108c 1216 {
c5aa993b 1217 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
c906108c 1218 struct minimal_symbol *indsym =
fc338970 1219 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
645dd519 1220 char *symname = indsym ? SYMBOL_LINKAGE_NAME (indsym) : 0;
c906108c 1221
c5aa993b 1222 if (symname)
c906108c 1223 {
c5aa993b
JM
1224 if (strncmp (symname, "__imp_", 6) == 0
1225 || strncmp (symname, "_imp_", 5) == 0)
c906108c
SS
1226 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1227 }
1228 }
fc338970 1229 return 0; /* Not a trampoline. */
c906108c 1230}
fc338970
MK
1231\f
1232
8201327c
MK
1233/* Return non-zero if PC and NAME show that we are in a signal
1234 trampoline. */
1235
1236static int
1237i386_pc_in_sigtramp (CORE_ADDR pc, char *name)
1238{
1239 return (name && strcmp ("_sigtramp", name) == 0);
1240}
1241\f
1242
fc338970
MK
1243/* We have two flavours of disassembly. The machinery on this page
1244 deals with switching between those. */
c906108c
SS
1245
1246static int
5e3397bb 1247i386_print_insn (bfd_vma pc, disassemble_info *info)
c906108c 1248{
5e3397bb
MK
1249 gdb_assert (disassembly_flavor == att_flavor
1250 || disassembly_flavor == intel_flavor);
1251
1252 /* FIXME: kettenis/20020915: Until disassembler_options is properly
1253 constified, cast to prevent a compiler warning. */
1254 info->disassembler_options = (char *) disassembly_flavor;
1255 info->mach = gdbarch_bfd_arch_info (current_gdbarch)->mach;
1256
1257 return print_insn_i386 (pc, info);
7a292a7a 1258}
fc338970 1259\f
3ce1502b 1260
8201327c
MK
1261/* There are a few i386 architecture variants that differ only
1262 slightly from the generic i386 target. For now, we don't give them
1263 their own source file, but include them here. As a consequence,
1264 they'll always be included. */
3ce1502b 1265
8201327c 1266/* System V Release 4 (SVR4). */
3ce1502b 1267
8201327c
MK
1268static int
1269i386_svr4_pc_in_sigtramp (CORE_ADDR pc, char *name)
d2a7c97a 1270{
8201327c
MK
1271 return (name && (strcmp ("_sigreturn", name) == 0
1272 || strcmp ("_sigacthandler", name) == 0
1273 || strcmp ("sigvechandler", name) == 0));
1274}
d2a7c97a 1275
21d0e8a4
MK
1276/* Get address of the pushed ucontext (sigcontext) on the stack for
1277 all three variants of SVR4 sigtramps. */
3ce1502b 1278
3a1e71e3 1279static CORE_ADDR
21d0e8a4 1280i386_svr4_sigcontext_addr (struct frame_info *frame)
8201327c 1281{
21d0e8a4 1282 int sigcontext_offset = -1;
8201327c
MK
1283 char *name = NULL;
1284
50abf9e5 1285 find_pc_partial_function (get_frame_pc (frame), &name, NULL, NULL);
8201327c 1286 if (name)
d2a7c97a 1287 {
8201327c 1288 if (strcmp (name, "_sigreturn") == 0)
21d0e8a4 1289 sigcontext_offset = 132;
8201327c 1290 else if (strcmp (name, "_sigacthandler") == 0)
21d0e8a4 1291 sigcontext_offset = 80;
8201327c 1292 else if (strcmp (name, "sigvechandler") == 0)
21d0e8a4 1293 sigcontext_offset = 120;
8201327c 1294 }
3ce1502b 1295
21d0e8a4
MK
1296 gdb_assert (sigcontext_offset != -1);
1297
11c02a10
AC
1298 if (get_next_frame (frame))
1299 return get_frame_base (get_next_frame (frame)) + sigcontext_offset;
21d0e8a4 1300 return read_register (SP_REGNUM) + sigcontext_offset;
8201327c
MK
1301}
1302\f
3ce1502b 1303
8201327c 1304/* DJGPP. */
d2a7c97a 1305
8201327c
MK
1306static int
1307i386_go32_pc_in_sigtramp (CORE_ADDR pc, char *name)
1308{
1309 /* DJGPP doesn't have any special frames for signal handlers. */
1310 return 0;
1311}
1312\f
d2a7c97a 1313
8201327c 1314/* Generic ELF. */
d2a7c97a 1315
8201327c
MK
1316void
1317i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1318{
1319 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1320 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1321}
3ce1502b 1322
8201327c 1323/* System V Release 4 (SVR4). */
3ce1502b 1324
8201327c
MK
1325void
1326i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1327{
1328 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 1329
8201327c
MK
1330 /* System V Release 4 uses ELF. */
1331 i386_elf_init_abi (info, gdbarch);
3ce1502b 1332
dfe01d39
MK
1333 /* System V Release 4 has shared libraries. */
1334 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
1335 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
1336
8201327c 1337 set_gdbarch_pc_in_sigtramp (gdbarch, i386_svr4_pc_in_sigtramp);
21d0e8a4
MK
1338 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
1339 tdep->sc_pc_offset = 14 * 4;
1340 tdep->sc_sp_offset = 7 * 4;
3ce1502b 1341
8201327c 1342 tdep->jb_pc_offset = 20;
3ce1502b
MK
1343}
1344
8201327c 1345/* DJGPP. */
3ce1502b 1346
3a1e71e3 1347static void
8201327c 1348i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
3ce1502b 1349{
8201327c 1350 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 1351
8201327c 1352 set_gdbarch_pc_in_sigtramp (gdbarch, i386_go32_pc_in_sigtramp);
3ce1502b 1353
8201327c 1354 tdep->jb_pc_offset = 36;
3ce1502b
MK
1355}
1356
8201327c 1357/* NetWare. */
3ce1502b 1358
3a1e71e3 1359static void
8201327c 1360i386_nw_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
3ce1502b 1361{
8201327c 1362 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
3ce1502b 1363
8201327c 1364 tdep->jb_pc_offset = 24;
d2a7c97a 1365}
8201327c 1366\f
2acceee2 1367
38c968cf
AC
1368/* i386 register groups. In addition to the normal groups, add "mmx"
1369 and "sse". */
1370
1371static struct reggroup *i386_sse_reggroup;
1372static struct reggroup *i386_mmx_reggroup;
1373
1374static void
1375i386_init_reggroups (void)
1376{
1377 i386_sse_reggroup = reggroup_new ("sse", USER_REGGROUP);
1378 i386_mmx_reggroup = reggroup_new ("mmx", USER_REGGROUP);
1379}
1380
1381static void
1382i386_add_reggroups (struct gdbarch *gdbarch)
1383{
1384 reggroup_add (gdbarch, i386_sse_reggroup);
1385 reggroup_add (gdbarch, i386_mmx_reggroup);
1386 reggroup_add (gdbarch, general_reggroup);
1387 reggroup_add (gdbarch, float_reggroup);
1388 reggroup_add (gdbarch, all_reggroup);
1389 reggroup_add (gdbarch, save_reggroup);
1390 reggroup_add (gdbarch, restore_reggroup);
1391 reggroup_add (gdbarch, vector_reggroup);
1392 reggroup_add (gdbarch, system_reggroup);
1393}
1394
1395int
1396i386_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1397 struct reggroup *group)
1398{
1399 int sse_regnum_p = (i386_sse_regnum_p (regnum)
1400 || i386_mxcsr_regnum_p (regnum));
1401 int fp_regnum_p = (i386_fp_regnum_p (regnum)
1402 || i386_fpc_regnum_p (regnum));
1403 int mmx_regnum_p = (i386_mmx_regnum_p (regnum));
1404 if (group == i386_mmx_reggroup)
1405 return mmx_regnum_p;
1406 if (group == i386_sse_reggroup)
1407 return sse_regnum_p;
1408 if (group == vector_reggroup)
1409 return (mmx_regnum_p || sse_regnum_p);
1410 if (group == float_reggroup)
1411 return fp_regnum_p;
1412 if (group == general_reggroup)
1413 return (!fp_regnum_p && !mmx_regnum_p && !sse_regnum_p);
1414 return default_register_reggroup_p (gdbarch, regnum, group);
1415}
1416
1417\f
3a1e71e3 1418static struct gdbarch *
a62cc96e
AC
1419i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1420{
cd3c07fc 1421 struct gdbarch_tdep *tdep;
a62cc96e
AC
1422 struct gdbarch *gdbarch;
1423
4be87837
DJ
1424 /* If there is already a candidate, use it. */
1425 arches = gdbarch_list_lookup_by_info (arches, &info);
1426 if (arches != NULL)
1427 return arches->gdbarch;
a62cc96e
AC
1428
1429 /* Allocate space for the new architecture. */
1430 tdep = XMALLOC (struct gdbarch_tdep);
1431 gdbarch = gdbarch_alloc (&info, tdep);
1432
a5afb99f
AC
1433 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1434 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1435 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1436
8201327c 1437 /* The i386 default settings don't include the SSE registers.
356a6b3e
MK
1438 FIXME: kettenis/20020614: They do include the FPU registers for
1439 now, which probably is not quite right. */
8201327c 1440 tdep->num_xmm_regs = 0;
d2a7c97a 1441
8201327c
MK
1442 tdep->jb_pc_offset = -1;
1443 tdep->struct_return = pcc_struct_return;
8201327c
MK
1444 tdep->sigtramp_start = 0;
1445 tdep->sigtramp_end = 0;
21d0e8a4 1446 tdep->sigcontext_addr = NULL;
8201327c 1447 tdep->sc_pc_offset = -1;
21d0e8a4 1448 tdep->sc_sp_offset = -1;
8201327c 1449
896fb97d
MK
1450 /* The format used for `long double' on almost all i386 targets is
1451 the i387 extended floating-point format. In fact, of all targets
1452 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1453 on having a `long double' that's not `long' at all. */
1454 set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
21d0e8a4 1455
66da5fd8 1456 /* Although the i387 extended floating-point has only 80 significant
896fb97d
MK
1457 bits, a `long double' actually takes up 96, probably to enforce
1458 alignment. */
1459 set_gdbarch_long_double_bit (gdbarch, 96);
1460
356a6b3e
MK
1461 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1462 tm-symmetry.h currently override this. Sigh. */
1463 set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
21d0e8a4 1464
66da5fd8
MK
1465 set_gdbarch_sp_regnum (gdbarch, 4); /* %esp */
1466 set_gdbarch_fp_regnum (gdbarch, 5); /* %ebp */
1467 set_gdbarch_pc_regnum (gdbarch, 8); /* %eip */
1468 set_gdbarch_ps_regnum (gdbarch, 9); /* %eflags */
1469 set_gdbarch_fp0_regnum (gdbarch, 16); /* %st(0) */
356a6b3e
MK
1470
1471 /* Use the "default" register numbering scheme for stabs and COFF. */
1472 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1473 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1474
1475 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1476 set_gdbarch_dwarf_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1477 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1478
1479 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1480 be in use on any of the supported i386 targets. */
1481
1482 set_gdbarch_register_name (gdbarch, i386_register_name);
1483 set_gdbarch_register_size (gdbarch, 4);
1484 set_gdbarch_register_bytes (gdbarch, I386_SIZEOF_GREGS + I386_SIZEOF_FREGS);
4e259f09 1485 set_gdbarch_register_type (gdbarch, i386_register_type);
356a6b3e 1486
61113f8b
MK
1487 set_gdbarch_print_float_info (gdbarch, i387_print_float_info);
1488
8201327c 1489 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
96297dab 1490
a62cc96e 1491 /* Call dummy code. */
c0d1d883
MK
1492 set_gdbarch_call_dummy_words (gdbarch, NULL);
1493 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
a62cc96e 1494
b6197528
MK
1495 set_gdbarch_register_convertible (gdbarch, i386_register_convertible);
1496 set_gdbarch_register_convert_to_virtual (gdbarch,
1497 i386_register_convert_to_virtual);
1498 set_gdbarch_register_convert_to_raw (gdbarch, i386_register_convert_to_raw);
1499
8758dec1
MK
1500 /* "An argument's size is increased, if necessary, to make it a
1501 multiple of [32-bit] words. This may require tail padding,
1502 depending on the size of the argument" -- from the x86 ABI. */
1503 set_gdbarch_parm_boundary (gdbarch, 32);
1504
00f8375e 1505 set_gdbarch_extract_return_value (gdbarch, i386_extract_return_value);
b81774d8 1506 set_gdbarch_deprecated_push_arguments (gdbarch, i386_push_arguments);
28f617b3 1507 set_gdbarch_deprecated_push_return_address (gdbarch, i386_push_return_address);
749b82f6 1508 set_gdbarch_deprecated_pop_frame (gdbarch, i386_pop_frame);
3d7f4f49 1509 set_gdbarch_store_return_value (gdbarch, i386_store_return_value);
00f8375e 1510 set_gdbarch_extract_struct_value_address (gdbarch,
fc08ec52 1511 i386_extract_struct_value_address);
8201327c
MK
1512 set_gdbarch_use_struct_convention (gdbarch, i386_use_struct_convention);
1513
f30ee0bc 1514 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, i386_frame_init_saved_regs);
93924b6b
MK
1515 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
1516
1517 /* Stack grows downward. */
1518 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1519
1520 set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
1521 set_gdbarch_decr_pc_after_break (gdbarch, 1);
1522 set_gdbarch_function_start_offset (gdbarch, 0);
42fdc8df 1523
8201327c
MK
1524 /* The following redefines make backtracing through sigtramp work.
1525 They manufacture a fake sigtramp frame and obtain the saved pc in
1526 sigtramp from the sigcontext structure which is pushed by the
1527 kernel on the user stack, along with a pointer to it. */
1528
42fdc8df
MK
1529 set_gdbarch_frame_args_skip (gdbarch, 8);
1530 set_gdbarch_frameless_function_invocation (gdbarch,
1531 i386_frameless_function_invocation);
618ce49f 1532 set_gdbarch_deprecated_frame_chain (gdbarch, i386_frame_chain);
8bedc050 1533 set_gdbarch_deprecated_frame_saved_pc (gdbarch, i386_frame_saved_pc);
6913c89a 1534 set_gdbarch_deprecated_saved_pc_after_call (gdbarch, i386_saved_pc_after_call);
54299a1d 1535 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
8201327c
MK
1536 set_gdbarch_pc_in_sigtramp (gdbarch, i386_pc_in_sigtramp);
1537
28fc6740
AC
1538 /* Wire in the MMX registers. */
1539 set_gdbarch_num_pseudo_regs (gdbarch, mmx_num_regs);
1540 set_gdbarch_pseudo_register_read (gdbarch, i386_pseudo_register_read);
1541 set_gdbarch_pseudo_register_write (gdbarch, i386_pseudo_register_write);
1542
5e3397bb
MK
1543 set_gdbarch_print_insn (gdbarch, i386_print_insn);
1544
38c968cf
AC
1545 /* Add the i386 register groups. */
1546 i386_add_reggroups (gdbarch);
1547 set_gdbarch_register_reggroup_p (gdbarch, i386_register_reggroup_p);
1548
6c0e89ed
AC
1549 /* Should be using push_dummy_call. */
1550 set_gdbarch_deprecated_dummy_write_sp (gdbarch, generic_target_write_sp);
1551
3ce1502b 1552 /* Hook in ABI-specific overrides, if they have been registered. */
4be87837 1553 gdbarch_init_osabi (info, gdbarch);
3ce1502b 1554
a62cc96e
AC
1555 return gdbarch;
1556}
1557
8201327c
MK
1558static enum gdb_osabi
1559i386_coff_osabi_sniffer (bfd *abfd)
1560{
762c5349
MK
1561 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
1562 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
8201327c
MK
1563 return GDB_OSABI_GO32;
1564
1565 return GDB_OSABI_UNKNOWN;
1566}
1567
1568static enum gdb_osabi
1569i386_nlm_osabi_sniffer (bfd *abfd)
1570{
1571 return GDB_OSABI_NETWARE;
1572}
1573\f
1574
28e9e0f0
MK
1575/* Provide a prototype to silence -Wmissing-prototypes. */
1576void _initialize_i386_tdep (void);
1577
c906108c 1578void
fba45db2 1579_initialize_i386_tdep (void)
c906108c 1580{
a62cc96e
AC
1581 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1582
fc338970 1583 /* Add the variable that controls the disassembly flavor. */
917317f4
JM
1584 {
1585 struct cmd_list_element *new_cmd;
7a292a7a 1586
917317f4
JM
1587 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1588 valid_flavors,
1ed2a135 1589 &disassembly_flavor,
fc338970
MK
1590 "\
1591Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
c906108c 1592and the default value is \"att\".",
917317f4 1593 &setlist);
917317f4
JM
1594 add_show_from_set (new_cmd, &showlist);
1595 }
8201327c
MK
1596
1597 /* Add the variable that controls the convention for returning
1598 structs. */
1599 {
1600 struct cmd_list_element *new_cmd;
1601
1602 new_cmd = add_set_enum_cmd ("struct-convention", no_class,
5e3397bb 1603 valid_conventions,
8201327c
MK
1604 &struct_convention, "\
1605Set the convention for returning small structs, valid values \
1606are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1607 &setlist);
1608 add_show_from_set (new_cmd, &showlist);
1609 }
1610
1611 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
1612 i386_coff_osabi_sniffer);
1613 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_nlm_flavour,
1614 i386_nlm_osabi_sniffer);
1615
05816f70 1616 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_SVR4,
8201327c 1617 i386_svr4_init_abi);
05816f70 1618 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_GO32,
8201327c 1619 i386_go32_init_abi);
05816f70 1620 gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETWARE,
8201327c 1621 i386_nw_init_abi);
38c968cf
AC
1622
1623 /* Initialize the i386 specific register groups. */
1624 i386_init_reggroups ();
c906108c 1625}