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