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