1 /* Intel 386 target-dependent stuff.
3 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
4 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
24 #include "gdb_string.h"
29 #include "floatformat.h"
34 #include "arch-utils.h"
38 #include "gdb_assert.h"
40 #include "i386-tdep.h"
42 /* Names of the registers. The first 10 registers match the register
43 numbering scheme used by GCC for stabs and DWARF. */
44 static char *i386_register_names[] =
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",
59 /* i386_register_offset[i] is the offset into the register file of the
60 start of register number i. We initialize this from
61 i386_register_size. */
62 static int i386_register_offset[I386_SSE_NUM_REGS];
64 /* i386_register_size[i] is the number of bytes of storage in GDB's
65 register array occupied by register i. */
66 static int i386_register_size[I386_SSE_NUM_REGS] = {
80 /* Return the name of register REG. */
83 i386_register_name (int reg)
87 if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
90 return i386_register_names[reg];
93 /* Return the offset into the register array of the start of register
96 i386_register_byte (int reg)
98 return i386_register_offset[reg];
101 /* Return the number of bytes of storage in GDB's register array
102 occupied by register REG. */
105 i386_register_raw_size (int reg)
107 return i386_register_size[reg];
110 /* Convert stabs register number REG to the appropriate register
111 number used by GDB. */
114 i386_stab_reg_to_regnum (int reg)
116 /* This implements what GCC calls the "default" register map. */
117 if (reg >= 0 && reg <= 7)
119 /* General registers. */
122 else if (reg >= 12 && reg <= 19)
124 /* Floating-point registers. */
125 return reg - 12 + FP0_REGNUM;
127 else if (reg >= 21 && reg <= 28)
130 return reg - 21 + XMM0_REGNUM;
132 else if (reg >= 29 && reg <= 36)
135 /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
136 as pseudo-registers? */
137 return reg - 29 + FP0_REGNUM;
140 /* This will hopefully provoke a warning. */
141 return NUM_REGS + NUM_PSEUDO_REGS;
144 /* Convert DWARF register number REG to the appropriate register
145 number used by GDB. */
148 i386_dwarf_reg_to_regnum (int reg)
150 /* The DWARF register numbering includes %eip and %eflags, and
151 numbers the floating point registers differently. */
152 if (reg >= 0 && reg <= 9)
154 /* General registers. */
157 else if (reg >= 11 && reg <= 18)
159 /* Floating-point registers. */
160 return reg - 11 + FP0_REGNUM;
164 /* The SSE and MMX registers have identical numbers as in stabs. */
165 return i386_stab_reg_to_regnum (reg);
168 /* This will hopefully provoke a warning. */
169 return NUM_REGS + NUM_PSEUDO_REGS;
173 /* This is the variable that is set with "set disassembly-flavor", and
174 its legitimate values. */
175 static const char att_flavor[] = "att";
176 static const char intel_flavor[] = "intel";
177 static const char *valid_flavors[] =
183 static const char *disassembly_flavor = att_flavor;
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) */
203 #define CODESTREAM_BUFSIZ 1 /* Was sizeof(int), see note above. */
204 static CORE_ADDR codestream_next_addr;
205 static CORE_ADDR codestream_addr;
206 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
207 static int codestream_off;
208 static int codestream_cnt;
210 #define codestream_tell() (codestream_addr + codestream_off)
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++])
219 codestream_fill (int peek_flag)
221 codestream_addr = codestream_next_addr;
222 codestream_next_addr += CODESTREAM_BUFSIZ;
224 codestream_cnt = CODESTREAM_BUFSIZ;
225 read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
228 return (codestream_peek ());
230 return (codestream_get ());
234 codestream_seek (CORE_ADDR place)
236 codestream_next_addr = place / CODESTREAM_BUFSIZ;
237 codestream_next_addr *= CODESTREAM_BUFSIZ;
240 while (codestream_tell () != place)
245 codestream_read (unsigned char *buf, int count)
250 for (i = 0; i < count; i++)
251 *p++ = codestream_get ();
255 /* If the next instruction is a jump, move to its target. */
258 i386_follow_jump (void)
260 unsigned char buf[4];
266 pos = codestream_tell ();
269 if (codestream_peek () == 0x66)
275 switch (codestream_get ())
278 /* Relative jump: if data16 == 0, disp32, else disp16. */
281 codestream_read (buf, 2);
282 delta = extract_signed_integer (buf, 2);
284 /* Include the size of the jmp instruction (including the
290 codestream_read (buf, 4);
291 delta = extract_signed_integer (buf, 4);
297 /* Relative jump, disp8 (ignore data16). */
298 codestream_read (buf, 1);
299 /* Sign-extend it. */
300 delta = extract_signed_integer (buf, 1);
305 codestream_seek (pos);
308 /* Find & return the amount a local space allocated, and advance the
309 codestream to the first register push (if any).
311 If the entry sequence doesn't make sense, return -1, and leave
312 codestream pointer at a random spot. */
315 i386_get_frame_setup (CORE_ADDR pc)
319 codestream_seek (pc);
323 op = codestream_get ();
325 if (op == 0x58) /* popl %eax */
327 /* This function must start with
330 xchgl %eax, (%esp) 0x87 0x04 0x24
331 or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
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
339 unsigned char buf[4];
340 static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
341 static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
343 pos = codestream_tell ();
344 codestream_read (buf, 4);
345 if (memcmp (buf, proto1, 3) == 0)
347 else if (memcmp (buf, proto2, 4) == 0)
350 codestream_seek (pos);
351 op = codestream_get (); /* Update next opcode. */
354 if (op == 0x68 || op == 0x6a)
356 /* This function may start with
368 unsigned char buf[8];
370 /* Skip past the `pushl' instruction; it has either a one-byte
371 or a four-byte operand, depending on the opcode. */
372 pos = codestream_tell ();
377 codestream_seek (pos);
379 /* Read the following 8 bytes, which should be "call _probe" (6
380 bytes) followed by "addl $4,%esp" (2 bytes). */
381 codestream_read (buf, sizeof (buf));
382 if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
384 codestream_seek (pos);
385 op = codestream_get (); /* Update next opcode. */
388 if (op == 0x55) /* pushl %ebp */
390 /* Check for "movl %esp, %ebp" -- can be written in two ways. */
391 switch (codestream_get ())
394 if (codestream_get () != 0xec)
398 if (codestream_get () != 0xe5)
404 /* Check for stack adjustment
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. */
410 op = codestream_peek ();
413 /* `subl' with 8 bit immediate. */
415 if (codestream_get () != 0xec)
416 /* Some instruction starting with 0x83 other than `subl'. */
418 codestream_seek (codestream_tell () - 2);
421 /* `subl' with signed byte immediate (though it wouldn't
422 make sense to be negative). */
423 return (codestream_get ());
428 /* Maybe it is `subl' with a 32 bit immedediate. */
430 if (codestream_get () != 0xec)
431 /* Some instruction starting with 0x81 other than `subl'. */
433 codestream_seek (codestream_tell () - 2);
436 /* It is `subl' with a 32 bit immediate. */
437 codestream_read ((unsigned char *) buf, 4);
438 return extract_signed_integer (buf, 4);
448 /* `enter' with 16 bit unsigned immediate. */
449 codestream_read ((unsigned char *) buf, 2);
450 codestream_get (); /* Flush final byte of enter instruction. */
451 return extract_unsigned_integer (buf, 2);
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
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
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
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
481 /* Return non-zero if we're dealing with a frameless signal, that is,
482 a signal trampoline invoked from a frameless function. */
485 i386_frameless_signal_p (struct frame_info *frame)
487 return (frame->next && frame->next->signal_handler_caller
488 && (frameless_look_for_prologue (frame)
489 || frame->pc == get_pc_function_start (frame->pc)));
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. */
497 i386_frame_chain (struct frame_info *frame)
499 if (PC_IN_CALL_DUMMY (frame->pc, 0, 0))
502 if (frame->signal_handler_caller
503 || i386_frameless_signal_p (frame))
506 if (! inside_entry_file (frame->pc))
507 return read_memory_unsigned_integer (frame->frame, 4);
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. */
517 i386_frameless_function_invocation (struct frame_info *frame)
519 if (frame->signal_handler_caller)
522 return frameless_look_for_prologue (frame);
525 /* Assuming FRAME is for a sigtramp routine, return the saved program
529 i386_sigtramp_saved_pc (struct frame_info *frame)
531 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
534 addr = tdep->sigcontext_addr (frame);
535 return read_memory_unsigned_integer (addr + tdep->sc_pc_offset, 4);
538 /* Assuming FRAME is for a sigtramp routine, return the saved stack
542 i386_sigtramp_saved_sp (struct frame_info *frame)
544 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
547 addr = tdep->sigcontext_addr (frame);
548 return read_memory_unsigned_integer (addr + tdep->sc_sp_offset, 4);
551 /* Return the saved program counter for FRAME. */
554 i386_frame_saved_pc (struct frame_info *frame)
556 if (PC_IN_CALL_DUMMY (frame->pc, 0, 0))
557 return generic_read_register_dummy (frame->pc, frame->frame,
560 if (frame->signal_handler_caller)
561 return i386_sigtramp_saved_pc (frame);
563 if (i386_frameless_signal_p (frame))
565 CORE_ADDR sp = i386_sigtramp_saved_sp (frame->next);
566 return read_memory_unsigned_integer (sp, 4);
569 return read_memory_unsigned_integer (frame->frame + 4, 4);
572 /* Immediately after a function call, return the saved pc. */
575 i386_saved_pc_after_call (struct frame_info *frame)
577 if (frame->signal_handler_caller)
578 return i386_sigtramp_saved_pc (frame);
580 return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
583 /* Return number of args passed to a frame.
584 Can return -1, meaning no way to tell. */
587 i386_frame_num_args (struct frame_info *fi)
592 /* This loses because not only might the compiler not be popping the
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. */
599 struct frame_info *pfi;
601 /* On the i386, the instruction following the call could be:
603 addl $imm, %esp - imm/4 args; imm may be 8 or 32 bits
604 anything else - zero args. */
608 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
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. */
615 pfi = get_prev_frame (fi);
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
628 op = read_memory_integer (retpc, 1);
629 if (op == 0x59) /* pop %ecx */
633 op = read_memory_integer (retpc + 1, 1);
635 /* addl $<signed imm 8 bits>, %esp */
636 return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
640 else if (op == 0x81) /* `add' with 32 bit immediate. */
642 op = read_memory_integer (retpc + 1, 1);
644 /* addl $<imm 32>, %esp */
645 return read_memory_integer (retpc + 2, 4) / 4;
657 /* Parse the first few instructions the function to see what registers
660 We handle these cases:
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.
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).
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.
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.
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. */
684 i386_frame_init_saved_regs (struct frame_info *fip)
695 frame_saved_regs_zalloc (fip);
697 pc = get_pc_function_start (fip->pc);
699 locals = i386_get_frame_setup (pc);
703 addr = fip->frame - 4 - locals;
704 for (i = 0; i < 8; i++)
706 op = codestream_get ();
707 if (op < 0x50 || op > 0x57)
709 #ifdef I386_REGNO_TO_SYMMETRY
710 /* Dynix uses different internal numbering. Ick. */
711 fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
713 fip->saved_regs[op - 0x50] = addr;
719 fip->saved_regs[PC_REGNUM] = fip->frame + 4;
720 fip->saved_regs[FP_REGNUM] = fip->frame;
723 /* Return PC of first real instruction. */
726 i386_skip_prologue (CORE_ADDR pc)
730 static unsigned char pic_pat[6] =
731 { 0xe8, 0, 0, 0, 0, /* call 0x0 */
732 0x5b, /* popl %ebx */
736 if (i386_get_frame_setup (pc) < 0)
739 /* Found valid frame setup -- codestream now points to start of push
740 instructions for saving registers. */
742 /* Skip over register saves. */
743 for (i = 0; i < 8; i++)
745 op = codestream_peek ();
746 /* Break if not `pushl' instrunction. */
747 if (op < 0x50 || op > 0x57)
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
758 movl %ebx,x(%ebp) (optional)
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. */
765 pos = codestream_tell ();
766 for (i = 0; i < 6; i++)
768 op = codestream_get ();
769 if (pic_pat[i] != op)
774 unsigned char buf[4];
777 op = codestream_get ();
778 if (op == 0x89) /* movl %ebx, x(%ebp) */
780 op = codestream_get ();
781 if (op == 0x5d) /* One byte offset from %ebp. */
784 codestream_read (buf, 1);
786 else if (op == 0x9d) /* Four byte offset from %ebp. */
789 codestream_read (buf, 4);
791 else /* Unexpected instruction. */
793 op = codestream_get ();
796 if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
801 codestream_seek (pos);
805 return (codestream_tell ());
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.
814 On the i386 we have a single breakpoint that fits in a single byte
815 and can be inserted anywhere. */
817 static const unsigned char *
818 i386_breakpoint_from_pc (CORE_ADDR *pc, int *len)
820 static unsigned char break_insn[] = { 0xcc }; /* int 3 */
822 *len = sizeof (break_insn);
826 /* Push the return address (pointing to the call dummy) onto the stack
827 and return the new value for the stack pointer. */
830 i386_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
834 store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
835 write_memory (sp - 4, buf, 4);
840 i386_do_pop_frame (struct frame_info *frame)
844 char regbuf[MAX_REGISTER_RAW_SIZE];
846 fp = FRAME_FP (frame);
847 i386_frame_init_saved_regs (frame);
849 for (regnum = 0; regnum < NUM_REGS; regnum++)
852 addr = frame->saved_regs[regnum];
855 read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
856 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
857 REGISTER_RAW_SIZE (regnum));
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 ();
867 i386_pop_frame (void)
869 generic_pop_current_frame (i386_do_pop_frame);
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
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
880 i386_get_longjmp_target (CORE_ADDR *pc)
883 CORE_ADDR sp, jb_addr;
884 int jb_pc_offset = gdbarch_tdep (current_gdbarch)->jb_pc_offset;
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)
891 sp = read_register (SP_REGNUM);
892 if (target_read_memory (sp + 4, buf, 4))
895 jb_addr = extract_address (buf, 4);
896 if (target_read_memory (jb_addr + jb_pc_offset, buf, 4))
899 *pc = extract_address (buf, 4);
905 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
906 int struct_return, CORE_ADDR struct_addr)
908 sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
915 store_address (buf, 4, struct_addr);
916 write_memory (sp, buf, 4);
923 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
925 /* Do nothing. Everything was already done by i386_push_arguments. */
928 /* These registers are used for returning integers (and on some
929 targets also for returning `struct' and `union' values when their
930 size and alignment match an integer type). */
931 #define LOW_RETURN_REGNUM 0 /* %eax */
932 #define HIGH_RETURN_REGNUM 2 /* %edx */
934 /* Extract from an array REGBUF containing the (raw) register state, a
935 function return value of TYPE, and copy that, in virtual format,
939 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
941 int len = TYPE_LENGTH (type);
943 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
944 && TYPE_NFIELDS (type) == 1)
946 i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
950 if (TYPE_CODE (type) == TYPE_CODE_FLT)
954 warning ("Cannot find floating-point return value.");
955 memset (valbuf, 0, len);
959 /* Floating-point return values can be found in %st(0). Convert
960 its contents to the desired type. This is probably not
961 exactly how it would happen on the target itself, but it is
962 the best we can do. */
963 convert_typed_floating (®buf[REGISTER_BYTE (FP0_REGNUM)],
964 builtin_type_i387_ext, valbuf, type);
968 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
969 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
972 memcpy (valbuf, ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
973 else if (len <= (low_size + high_size))
976 ®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
977 memcpy (valbuf + low_size,
978 ®buf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
981 internal_error (__FILE__, __LINE__,
982 "Cannot extract return value of %d bytes long.", len);
986 /* Write into the appropriate registers a function return value stored
987 in VALBUF of type TYPE, given in virtual format. */
990 i386_store_return_value (struct type *type, char *valbuf)
992 int len = TYPE_LENGTH (type);
994 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
995 && TYPE_NFIELDS (type) == 1)
997 i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
1001 if (TYPE_CODE (type) == TYPE_CODE_FLT)
1004 char buf[FPU_REG_RAW_SIZE];
1006 if (FP0_REGNUM == 0)
1008 warning ("Cannot set floating-point return value.");
1012 /* Returning floating-point values is a bit tricky. Apart from
1013 storing the return value in %st(0), we have to simulate the
1014 state of the FPU at function return point. */
1016 /* Convert the value found in VALBUF to the extended
1017 floating-point format used by the FPU. This is probably
1018 not exactly how it would happen on the target itself, but
1019 it is the best we can do. */
1020 convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
1021 write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
1024 /* Set the top of the floating-point register stack to 7. The
1025 actual value doesn't really matter, but 7 is what a normal
1026 function return would end up with if the program started out
1027 with a freshly initialized FPU. */
1028 fstat = read_register (FSTAT_REGNUM);
1030 write_register (FSTAT_REGNUM, fstat);
1032 /* Mark %st(1) through %st(7) as empty. Since we set the top of
1033 the floating-point register stack to 7, the appropriate value
1034 for the tag word is 0x3fff. */
1035 write_register (FTAG_REGNUM, 0x3fff);
1039 int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1040 int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1042 if (len <= low_size)
1043 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1044 else if (len <= (low_size + high_size))
1046 write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1048 write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1049 valbuf + low_size, len - low_size);
1052 internal_error (__FILE__, __LINE__,
1053 "Cannot store return value of %d bytes long.", len);
1057 /* Extract from an array REGBUF containing the (raw) register state
1058 the address in which a function should return its structure value,
1062 i386_extract_struct_value_address (char *regbuf)
1064 return extract_address (®buf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1065 REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1069 /* This is the variable that is set with "set struct-convention", and
1070 its legitimate values. */
1071 static const char default_struct_convention[] = "default";
1072 static const char pcc_struct_convention[] = "pcc";
1073 static const char reg_struct_convention[] = "reg";
1074 static const char *valid_conventions[] =
1076 default_struct_convention,
1077 pcc_struct_convention,
1078 reg_struct_convention,
1081 static const char *struct_convention = default_struct_convention;
1084 i386_use_struct_convention (int gcc_p, struct type *type)
1086 enum struct_return struct_return;
1088 if (struct_convention == default_struct_convention)
1089 struct_return = gdbarch_tdep (current_gdbarch)->struct_return;
1090 else if (struct_convention == pcc_struct_convention)
1091 struct_return = pcc_struct_return;
1093 struct_return = reg_struct_return;
1095 return generic_use_struct_convention (struct_return == reg_struct_return,
1100 /* Return the GDB type object for the "standard" data type of data in
1101 register REGNUM. Perhaps %esi and %edi should go here, but
1102 potentially they could be used for things other than address. */
1104 static struct type *
1105 i386_register_virtual_type (int regnum)
1107 if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1108 return lookup_pointer_type (builtin_type_void);
1110 if (IS_FP_REGNUM (regnum))
1111 return builtin_type_i387_ext;
1113 if (IS_SSE_REGNUM (regnum))
1114 return builtin_type_vec128i;
1116 return builtin_type_int;
1119 /* Return true iff register REGNUM's virtual format is different from
1120 its raw format. Note that this definition assumes that the host
1121 supports IEEE 32-bit floats, since it doesn't say that SSE
1122 registers need conversion. Even if we can't find a counterexample,
1123 this is still sloppy. */
1126 i386_register_convertible (int regnum)
1128 return IS_FP_REGNUM (regnum);
1131 /* Convert data from raw format for register REGNUM in buffer FROM to
1132 virtual format with type TYPE in buffer TO. */
1135 i386_register_convert_to_virtual (int regnum, struct type *type,
1136 char *from, char *to)
1138 gdb_assert (IS_FP_REGNUM (regnum));
1140 /* We only support floating-point values. */
1141 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1143 warning ("Cannot convert floating-point register value "
1144 "to non-floating-point type.");
1145 memset (to, 0, TYPE_LENGTH (type));
1149 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
1150 the extended floating-point format used by the FPU. */
1151 convert_typed_floating (from, builtin_type_i387_ext, to, type);
1154 /* Convert data from virtual format with type TYPE in buffer FROM to
1155 raw format for register REGNUM in buffer TO. */
1158 i386_register_convert_to_raw (struct type *type, int regnum,
1159 char *from, char *to)
1161 gdb_assert (IS_FP_REGNUM (regnum));
1163 /* We only support floating-point values. */
1164 if (TYPE_CODE (type) != TYPE_CODE_FLT)
1166 warning ("Cannot convert non-floating-point type "
1167 "to floating-point register value.");
1168 memset (to, 0, TYPE_LENGTH (type));
1172 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
1173 to the extended floating-point format used by the FPU. */
1174 convert_typed_floating (from, type, to, builtin_type_i387_ext);
1178 #ifdef STATIC_TRANSFORM_NAME
1179 /* SunPRO encodes the static variables. This is not related to C++
1180 mangling, it is done for C too. */
1183 sunpro_static_transform_name (char *name)
1186 if (IS_STATIC_TRANSFORM_NAME (name))
1188 /* For file-local statics there will be a period, a bunch of
1189 junk (the contents of which match a string given in the
1190 N_OPT), a period and the name. For function-local statics
1191 there will be a bunch of junk (which seems to change the
1192 second character from 'A' to 'B'), a period, the name of the
1193 function, and the name. So just skip everything before the
1195 p = strrchr (name, '.');
1201 #endif /* STATIC_TRANSFORM_NAME */
1204 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
1207 skip_trampoline_code (CORE_ADDR pc, char *name)
1209 if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1211 unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1212 struct minimal_symbol *indsym =
1213 indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1214 char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1218 if (strncmp (symname, "__imp_", 6) == 0
1219 || strncmp (symname, "_imp_", 5) == 0)
1220 return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1223 return 0; /* Not a trampoline. */
1227 /* Return non-zero if PC and NAME show that we are in a signal
1231 i386_pc_in_sigtramp (CORE_ADDR pc, char *name)
1233 return (name && strcmp ("_sigtramp", name) == 0);
1237 /* We have two flavours of disassembly. The machinery on this page
1238 deals with switching between those. */
1241 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1243 if (disassembly_flavor == att_flavor)
1244 return print_insn_i386_att (memaddr, info);
1245 else if (disassembly_flavor == intel_flavor)
1246 return print_insn_i386_intel (memaddr, info);
1247 /* Never reached -- disassembly_flavour is always either att_flavor
1249 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1253 /* There are a few i386 architecture variants that differ only
1254 slightly from the generic i386 target. For now, we don't give them
1255 their own source file, but include them here. As a consequence,
1256 they'll always be included. */
1258 /* System V Release 4 (SVR4). */
1261 i386_svr4_pc_in_sigtramp (CORE_ADDR pc, char *name)
1263 return (name && (strcmp ("_sigreturn", name) == 0
1264 || strcmp ("_sigacthandler", name) == 0
1265 || strcmp ("sigvechandler", name) == 0));
1268 /* Get address of the pushed ucontext (sigcontext) on the stack for
1269 all three variants of SVR4 sigtramps. */
1272 i386_svr4_sigcontext_addr (struct frame_info *frame)
1274 int sigcontext_offset = -1;
1277 find_pc_partial_function (frame->pc, &name, NULL, NULL);
1280 if (strcmp (name, "_sigreturn") == 0)
1281 sigcontext_offset = 132;
1282 else if (strcmp (name, "_sigacthandler") == 0)
1283 sigcontext_offset = 80;
1284 else if (strcmp (name, "sigvechandler") == 0)
1285 sigcontext_offset = 120;
1288 gdb_assert (sigcontext_offset != -1);
1291 return frame->next->frame + sigcontext_offset;
1292 return read_register (SP_REGNUM) + sigcontext_offset;
1299 i386_go32_pc_in_sigtramp (CORE_ADDR pc, char *name)
1301 /* DJGPP doesn't have any special frames for signal handlers. */
1309 i386_elf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1311 /* We typically use stabs-in-ELF with the DWARF register numbering. */
1312 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1315 /* System V Release 4 (SVR4). */
1318 i386_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1320 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1322 /* System V Release 4 uses ELF. */
1323 i386_elf_init_abi (info, gdbarch);
1325 /* FIXME: kettenis/20020511: Why do we override this function here? */
1326 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1328 set_gdbarch_pc_in_sigtramp (gdbarch, i386_svr4_pc_in_sigtramp);
1329 tdep->sigcontext_addr = i386_svr4_sigcontext_addr;
1330 tdep->sc_pc_offset = 14 * 4;
1331 tdep->sc_sp_offset = 7 * 4;
1333 tdep->jb_pc_offset = 20;
1339 i386_go32_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1341 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1343 set_gdbarch_pc_in_sigtramp (gdbarch, i386_go32_pc_in_sigtramp);
1345 tdep->jb_pc_offset = 36;
1351 i386_nw_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1353 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1355 /* FIXME: kettenis/20020511: Why do we override this function here? */
1356 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1358 tdep->jb_pc_offset = 24;
1362 static struct gdbarch *
1363 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1365 struct gdbarch_tdep *tdep;
1366 struct gdbarch *gdbarch;
1367 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
1369 /* Try to determine the OS ABI of the object we're loading. */
1370 if (info.abfd != NULL)
1371 osabi = gdbarch_lookup_osabi (info.abfd);
1373 /* Find a candidate among extant architectures. */
1374 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1376 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1378 /* Make sure the OS ABI selection matches. */
1379 tdep = gdbarch_tdep (arches->gdbarch);
1380 if (tdep && tdep->osabi == osabi)
1381 return arches->gdbarch;
1384 /* Allocate space for the new architecture. */
1385 tdep = XMALLOC (struct gdbarch_tdep);
1386 gdbarch = gdbarch_alloc (&info, tdep);
1388 tdep->osabi = osabi;
1390 /* The i386 default settings don't include the SSE registers.
1391 FIXME: kettenis/20020614: They do include the FPU registers for
1392 now, which probably is not quite right. */
1393 tdep->num_xmm_regs = 0;
1395 tdep->jb_pc_offset = -1;
1396 tdep->struct_return = pcc_struct_return;
1397 tdep->sigtramp_start = 0;
1398 tdep->sigtramp_end = 0;
1399 tdep->sigcontext_addr = NULL;
1400 tdep->sc_pc_offset = -1;
1401 tdep->sc_sp_offset = -1;
1403 /* The format used for `long double' on almost all i386 targets is
1404 the i387 extended floating-point format. In fact, of all targets
1405 in the GCC 2.95 tree, only OSF/1 does it different, and insists
1406 on having a `long double' that's not `long' at all. */
1407 set_gdbarch_long_double_format (gdbarch, &floatformat_i387_ext);
1409 /* Although the i386 extended floating-point has only 80 significant
1410 bits, a `long double' actually takes up 96, probably to enforce
1412 set_gdbarch_long_double_bit (gdbarch, 96);
1414 /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-ptx.h,
1415 tm-symmetry.h currently override this. Sigh. */
1416 set_gdbarch_num_regs (gdbarch, I386_NUM_GREGS + I386_NUM_FREGS);
1418 set_gdbarch_sp_regnum (gdbarch, 4);
1419 set_gdbarch_fp_regnum (gdbarch, 5);
1420 set_gdbarch_pc_regnum (gdbarch, 8);
1421 set_gdbarch_ps_regnum (gdbarch, 9);
1422 set_gdbarch_fp0_regnum (gdbarch, 16);
1424 /* Use the "default" register numbering scheme for stabs and COFF. */
1425 set_gdbarch_stab_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1426 set_gdbarch_sdb_reg_to_regnum (gdbarch, i386_stab_reg_to_regnum);
1428 /* Use the DWARF register numbering scheme for DWARF and DWARF 2. */
1429 set_gdbarch_dwarf_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1430 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, i386_dwarf_reg_to_regnum);
1432 /* We don't define ECOFF_REG_TO_REGNUM, since ECOFF doesn't seem to
1433 be in use on any of the supported i386 targets. */
1435 set_gdbarch_register_name (gdbarch, i386_register_name);
1436 set_gdbarch_register_size (gdbarch, 4);
1437 set_gdbarch_register_bytes (gdbarch, I386_SIZEOF_GREGS + I386_SIZEOF_FREGS);
1438 set_gdbarch_register_byte (gdbarch, i386_register_byte);
1439 set_gdbarch_register_raw_size (gdbarch, i386_register_raw_size);
1440 set_gdbarch_max_register_raw_size (gdbarch, 16);
1441 set_gdbarch_max_register_virtual_size (gdbarch, 16);
1442 set_gdbarch_register_virtual_type (gdbarch, i386_register_virtual_type);
1444 set_gdbarch_get_longjmp_target (gdbarch, i386_get_longjmp_target);
1446 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1448 /* Call dummy code. */
1449 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1450 set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1451 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1452 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1453 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1454 set_gdbarch_call_dummy_length (gdbarch, 0);
1455 set_gdbarch_call_dummy_p (gdbarch, 1);
1456 set_gdbarch_call_dummy_words (gdbarch, NULL);
1457 set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1458 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1459 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1461 set_gdbarch_register_convertible (gdbarch, i386_register_convertible);
1462 set_gdbarch_register_convert_to_virtual (gdbarch,
1463 i386_register_convert_to_virtual);
1464 set_gdbarch_register_convert_to_raw (gdbarch, i386_register_convert_to_raw);
1466 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1467 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1469 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1471 /* "An argument's size is increased, if necessary, to make it a
1472 multiple of [32-bit] words. This may require tail padding,
1473 depending on the size of the argument" -- from the x86 ABI. */
1474 set_gdbarch_parm_boundary (gdbarch, 32);
1476 set_gdbarch_deprecated_extract_return_value (gdbarch,
1477 i386_extract_return_value);
1478 set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1479 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1480 set_gdbarch_push_return_address (gdbarch, i386_push_return_address);
1481 set_gdbarch_pop_frame (gdbarch, i386_pop_frame);
1482 set_gdbarch_store_struct_return (gdbarch, i386_store_struct_return);
1483 set_gdbarch_store_return_value (gdbarch, i386_store_return_value);
1484 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
1485 i386_extract_struct_value_address);
1486 set_gdbarch_use_struct_convention (gdbarch, i386_use_struct_convention);
1488 set_gdbarch_frame_init_saved_regs (gdbarch, i386_frame_init_saved_regs);
1489 set_gdbarch_skip_prologue (gdbarch, i386_skip_prologue);
1491 /* Stack grows downward. */
1492 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1494 set_gdbarch_breakpoint_from_pc (gdbarch, i386_breakpoint_from_pc);
1495 set_gdbarch_decr_pc_after_break (gdbarch, 1);
1496 set_gdbarch_function_start_offset (gdbarch, 0);
1498 /* The following redefines make backtracing through sigtramp work.
1499 They manufacture a fake sigtramp frame and obtain the saved pc in
1500 sigtramp from the sigcontext structure which is pushed by the
1501 kernel on the user stack, along with a pointer to it. */
1503 set_gdbarch_frame_args_skip (gdbarch, 8);
1504 set_gdbarch_frameless_function_invocation (gdbarch,
1505 i386_frameless_function_invocation);
1506 set_gdbarch_frame_chain (gdbarch, i386_frame_chain);
1507 set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid);
1508 set_gdbarch_frame_saved_pc (gdbarch, i386_frame_saved_pc);
1509 set_gdbarch_frame_args_address (gdbarch, default_frame_address);
1510 set_gdbarch_frame_locals_address (gdbarch, default_frame_address);
1511 set_gdbarch_saved_pc_after_call (gdbarch, i386_saved_pc_after_call);
1512 set_gdbarch_frame_num_args (gdbarch, i386_frame_num_args);
1513 set_gdbarch_pc_in_sigtramp (gdbarch, i386_pc_in_sigtramp);
1515 /* Hook in ABI-specific overrides, if they have been registered. */
1516 gdbarch_init_osabi (info, gdbarch, osabi);
1521 static enum gdb_osabi
1522 i386_coff_osabi_sniffer (bfd *abfd)
1524 if (strcmp (bfd_get_target (abfd), "coff-go32-exe") == 0
1525 || strcmp (bfd_get_target (abfd), "coff-go32") == 0)
1526 return GDB_OSABI_GO32;
1528 return GDB_OSABI_UNKNOWN;
1531 static enum gdb_osabi
1532 i386_nlm_osabi_sniffer (bfd *abfd)
1534 return GDB_OSABI_NETWARE;
1538 /* Provide a prototype to silence -Wmissing-prototypes. */
1539 void _initialize_i386_tdep (void);
1542 _initialize_i386_tdep (void)
1544 register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1546 /* Initialize the table saying where each register starts in the
1552 for (i = 0; i < I386_SSE_NUM_REGS; i++)
1554 i386_register_offset[i] = offset;
1555 offset += i386_register_size[i];
1559 tm_print_insn = gdb_print_insn_i386;
1560 tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1562 /* Add the variable that controls the disassembly flavor. */
1564 struct cmd_list_element *new_cmd;
1566 new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1568 &disassembly_flavor,
1570 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1571 and the default value is \"att\".",
1573 add_show_from_set (new_cmd, &showlist);
1576 /* Add the variable that controls the convention for returning
1579 struct cmd_list_element *new_cmd;
1581 new_cmd = add_set_enum_cmd ("struct-convention", no_class,
1583 &struct_convention, "\
1584 Set the convention for returning small structs, valid values \
1585 are \"default\", \"pcc\" and \"reg\", and the default value is \"default\".",
1587 add_show_from_set (new_cmd, &showlist);
1590 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_coff_flavour,
1591 i386_coff_osabi_sniffer);
1592 gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_nlm_flavour,
1593 i386_nlm_osabi_sniffer);
1595 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_SVR4,
1596 i386_svr4_init_abi);
1597 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_GO32,
1598 i386_go32_init_abi);
1599 gdbarch_register_osabi (bfd_arch_i386, GDB_OSABI_NETWARE,