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