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