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