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