* config/djgpp/fnchange.lst: Add alphanbsd-nat.c and
[platform/upstream/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 "symtab.h"
31 #include "gdbcmd.h"
32 #include "command.h"
33 #include "arch-utils.h"
34 #include "regcache.h"
35 #include "doublest.h"
36 #include "value.h"
37 #include "gdb_assert.h"
38
39 #include "elf-bfd.h"
40
41 #include "i386-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 /* i386_register_offset[i] is the offset into the register file of the
61    start of register number i.  We initialize this from
62    i386_register_size.  */
63 static int i386_register_offset[MAX_NUM_REGS];
64
65 /* i386_register_size[i] is the number of bytes of storage in GDB's
66    register array occupied by register i.  */
67 static int i386_register_size[MAX_NUM_REGS] = {
68    4,  4,  4,  4,
69    4,  4,  4,  4,
70    4,  4,  4,  4,
71    4,  4,  4,  4,
72   10, 10, 10, 10,
73   10, 10, 10, 10,
74    4,  4,  4,  4,
75    4,  4,  4,  4,
76   16, 16, 16, 16,
77   16, 16, 16, 16,
78    4
79 };
80
81 /* Return the name of register REG.  */
82
83 char *
84 i386_register_name (int reg)
85 {
86   if (reg < 0)
87     return NULL;
88   if (reg >= sizeof (i386_register_names) / sizeof (*i386_register_names))
89     return NULL;
90
91   return i386_register_names[reg];
92 }
93
94 /* Return the offset into the register array of the start of register
95    number REG.  */
96 int
97 i386_register_byte (int reg)
98 {
99   return i386_register_offset[reg];
100 }
101
102 /* Return the number of bytes of storage in GDB's register array
103    occupied by register REG.  */
104
105 int
106 i386_register_raw_size (int reg)
107 {
108   return i386_register_size[reg];
109 }
110
111 /* Return the size in bytes of the virtual type of register REG.  */
112
113 int
114 i386_register_virtual_size (int reg)
115 {
116   return TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (reg));
117 }
118
119 /* Convert stabs register number REG to the appropriate register
120    number used by GDB.  */
121
122 int
123 i386_stab_reg_to_regnum (int reg)
124 {
125   /* This implements what GCC calls the "default" register map.  */
126   if (reg >= 0 && reg <= 7)
127     {
128       /* General registers.  */
129       return reg;
130     }
131   else if (reg >= 12 && reg <= 19)
132     {
133       /* Floating-point registers.  */
134       return reg - 12 + FP0_REGNUM;
135     }
136   else if (reg >= 21 && reg <= 28)
137     {
138       /* SSE registers.  */
139       return reg - 21 + XMM0_REGNUM;
140     }
141   else if (reg >= 29 && reg <= 36)
142     {
143       /* MMX registers.  */
144       /* FIXME: kettenis/2001-07-28: Should we have the MMX registers
145          as pseudo-registers?  */
146       return reg - 29 + FP0_REGNUM;
147     }
148
149   /* This will hopefully provoke a warning.  */
150   return NUM_REGS + NUM_PSEUDO_REGS;
151 }
152
153 /* Convert Dwarf register number REG to the appropriate register
154    number used by GDB.  */
155
156 int
157 i386_dwarf_reg_to_regnum (int reg)
158 {
159   /* The DWARF register numbering includes %eip and %eflags, and
160      numbers the floating point registers differently.  */
161   if (reg >= 0 && reg <= 9)
162     {
163       /* General registers.  */
164       return reg;
165     }
166   else if (reg >= 11 && reg <= 18)
167     {
168       /* Floating-point registers.  */
169       return reg - 11 + FP0_REGNUM;
170     }
171   else if (reg >= 21)
172     {
173       /* The SSE and MMX registers have identical numbers as in stabs.  */
174       return i386_stab_reg_to_regnum (reg);
175     }
176
177   /* This will hopefully provoke a warning.  */
178   return NUM_REGS + NUM_PSEUDO_REGS;
179 }
180 \f
181
182 /* This is the variable that is set with "set disassembly-flavor", and
183    its legitimate values.  */
184 static const char att_flavor[] = "att";
185 static const char intel_flavor[] = "intel";
186 static const char *valid_flavors[] =
187 {
188   att_flavor,
189   intel_flavor,
190   NULL
191 };
192 static const char *disassembly_flavor = att_flavor;
193
194 /* Stdio style buffering was used to minimize calls to ptrace, but
195    this buffering did not take into account that the code section
196    being accessed may not be an even number of buffers long (even if
197    the buffer is only sizeof(int) long).  In cases where the code
198    section size happened to be a non-integral number of buffers long,
199    attempting to read the last buffer would fail.  Simply using
200    target_read_memory and ignoring errors, rather than read_memory, is
201    not the correct solution, since legitimate access errors would then
202    be totally ignored.  To properly handle this situation and continue
203    to use buffering would require that this code be able to determine
204    the minimum code section size granularity (not the alignment of the
205    section itself, since the actual failing case that pointed out this
206    problem had a section alignment of 4 but was not a multiple of 4
207    bytes long), on a target by target basis, and then adjust it's
208    buffer size accordingly.  This is messy, but potentially feasible.
209    It probably needs the bfd library's help and support.  For now, the
210    buffer size is set to 1.  (FIXME -fnf) */
211
212 #define CODESTREAM_BUFSIZ 1     /* Was sizeof(int), see note above.  */
213 static CORE_ADDR codestream_next_addr;
214 static CORE_ADDR codestream_addr;
215 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
216 static int codestream_off;
217 static int codestream_cnt;
218
219 #define codestream_tell() (codestream_addr + codestream_off)
220 #define codestream_peek() \
221   (codestream_cnt == 0 ? \
222    codestream_fill(1) : codestream_buf[codestream_off])
223 #define codestream_get() \
224   (codestream_cnt-- == 0 ? \
225    codestream_fill(0) : codestream_buf[codestream_off++])
226
227 static unsigned char
228 codestream_fill (int peek_flag)
229 {
230   codestream_addr = codestream_next_addr;
231   codestream_next_addr += CODESTREAM_BUFSIZ;
232   codestream_off = 0;
233   codestream_cnt = CODESTREAM_BUFSIZ;
234   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
235
236   if (peek_flag)
237     return (codestream_peek ());
238   else
239     return (codestream_get ());
240 }
241
242 static void
243 codestream_seek (CORE_ADDR place)
244 {
245   codestream_next_addr = place / CODESTREAM_BUFSIZ;
246   codestream_next_addr *= CODESTREAM_BUFSIZ;
247   codestream_cnt = 0;
248   codestream_fill (1);
249   while (codestream_tell () != place)
250     codestream_get ();
251 }
252
253 static void
254 codestream_read (unsigned char *buf, int count)
255 {
256   unsigned char *p;
257   int i;
258   p = buf;
259   for (i = 0; i < count; i++)
260     *p++ = codestream_get ();
261 }
262 \f
263
264 /* If the next instruction is a jump, move to its target.  */
265
266 static void
267 i386_follow_jump (void)
268 {
269   unsigned char buf[4];
270   long delta;
271
272   int data16;
273   CORE_ADDR pos;
274
275   pos = codestream_tell ();
276
277   data16 = 0;
278   if (codestream_peek () == 0x66)
279     {
280       codestream_get ();
281       data16 = 1;
282     }
283
284   switch (codestream_get ())
285     {
286     case 0xe9:
287       /* Relative jump: if data16 == 0, disp32, else disp16.  */
288       if (data16)
289         {
290           codestream_read (buf, 2);
291           delta = extract_signed_integer (buf, 2);
292
293           /* Include the size of the jmp instruction (including the
294              0x66 prefix).  */
295           pos += delta + 4;
296         }
297       else
298         {
299           codestream_read (buf, 4);
300           delta = extract_signed_integer (buf, 4);
301
302           pos += delta + 5;
303         }
304       break;
305     case 0xeb:
306       /* Relative jump, disp8 (ignore data16).  */
307       codestream_read (buf, 1);
308       /* Sign-extend it.  */
309       delta = extract_signed_integer (buf, 1);
310
311       pos += delta + 2;
312       break;
313     }
314   codestream_seek (pos);
315 }
316
317 /* Find & return the amount a local space allocated, and advance the
318    codestream to the first register push (if any).
319
320    If the entry sequence doesn't make sense, return -1, and leave
321    codestream pointer at a random spot.  */
322
323 static long
324 i386_get_frame_setup (CORE_ADDR pc)
325 {
326   unsigned char op;
327
328   codestream_seek (pc);
329
330   i386_follow_jump ();
331
332   op = codestream_get ();
333
334   if (op == 0x58)               /* popl %eax */
335     {
336       /* This function must start with
337
338             popl %eax             0x58
339             xchgl %eax, (%esp)    0x87 0x04 0x24
340          or xchgl %eax, 0(%esp)   0x87 0x44 0x24 0x00
341
342          (the System V compiler puts out the second `xchg'
343          instruction, and the assembler doesn't try to optimize it, so
344          the 'sib' form gets generated).  This sequence is used to get
345          the address of the return buffer for a function that returns
346          a structure.  */
347       int pos;
348       unsigned char buf[4];
349       static unsigned char proto1[3] = { 0x87, 0x04, 0x24 };
350       static unsigned char proto2[4] = { 0x87, 0x44, 0x24, 0x00 };
351
352       pos = codestream_tell ();
353       codestream_read (buf, 4);
354       if (memcmp (buf, proto1, 3) == 0)
355         pos += 3;
356       else if (memcmp (buf, proto2, 4) == 0)
357         pos += 4;
358
359       codestream_seek (pos);
360       op = codestream_get ();   /* Update next opcode.  */
361     }
362
363   if (op == 0x68 || op == 0x6a)
364     {
365       /* This function may start with
366
367             pushl constant
368             call _probe
369             addl $4, %esp
370            
371          followed by
372
373             pushl %ebp
374
375          etc.  */
376       int pos;
377       unsigned char buf[8];
378
379       /* Skip past the `pushl' instruction; it has either a one-byte 
380          or a four-byte operand, depending on the opcode.  */
381       pos = codestream_tell ();
382       if (op == 0x68)
383         pos += 4;
384       else
385         pos += 1;
386       codestream_seek (pos);
387
388       /* Read the following 8 bytes, which should be "call _probe" (6
389          bytes) followed by "addl $4,%esp" (2 bytes).  */
390       codestream_read (buf, sizeof (buf));
391       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
392         pos += sizeof (buf);
393       codestream_seek (pos);
394       op = codestream_get ();   /* Update next opcode.  */
395     }
396
397   if (op == 0x55)               /* pushl %ebp */
398     {
399       /* Check for "movl %esp, %ebp" -- can be written in two ways.  */
400       switch (codestream_get ())
401         {
402         case 0x8b:
403           if (codestream_get () != 0xec)
404             return -1;
405           break;
406         case 0x89:
407           if (codestream_get () != 0xe5)
408             return -1;
409           break;
410         default:
411           return -1;
412         }
413       /* Check for stack adjustment 
414
415            subl $XXX, %esp
416
417          NOTE: You can't subtract a 16 bit immediate from a 32 bit
418          reg, so we don't have to worry about a data16 prefix.  */
419       op = codestream_peek ();
420       if (op == 0x83)
421         {
422           /* `subl' with 8 bit immediate.  */
423           codestream_get ();
424           if (codestream_get () != 0xec)
425             /* Some instruction starting with 0x83 other than `subl'.  */
426             {
427               codestream_seek (codestream_tell () - 2);
428               return 0;
429             }
430           /* `subl' with signed byte immediate (though it wouldn't
431              make sense to be negative).  */
432           return (codestream_get ());
433         }
434       else if (op == 0x81)
435         {
436           char buf[4];
437           /* Maybe it is `subl' with a 32 bit immedediate.  */
438           codestream_get ();
439           if (codestream_get () != 0xec)
440             /* Some instruction starting with 0x81 other than `subl'.  */
441             {
442               codestream_seek (codestream_tell () - 2);
443               return 0;
444             }
445           /* It is `subl' with a 32 bit immediate.  */
446           codestream_read ((unsigned char *) buf, 4);
447           return extract_signed_integer (buf, 4);
448         }
449       else
450         {
451           return 0;
452         }
453     }
454   else if (op == 0xc8)
455     {
456       char buf[2];
457       /* `enter' with 16 bit unsigned immediate.  */
458       codestream_read ((unsigned char *) buf, 2);
459       codestream_get ();        /* Flush final byte of enter instruction.  */
460       return extract_unsigned_integer (buf, 2);
461     }
462   return (-1);
463 }
464
465 /* Return the chain-pointer for FRAME.  In the case of the i386, the
466    frame's nominal address is the address of a 4-byte word containing
467    the calling frame's address.  */
468
469 CORE_ADDR
470 i386_frame_chain (struct frame_info *frame)
471 {
472   if (frame->signal_handler_caller)
473     return frame->frame;
474
475   if (! inside_entry_file (frame->pc))
476     return read_memory_unsigned_integer (frame->frame, 4);
477
478   return 0;
479 }
480
481 /* Determine whether the function invocation represented by FRAME does
482    not have a from on the stack associated with it.  If it does not,
483    return non-zero, otherwise return zero.  */
484
485 int
486 i386_frameless_function_invocation (struct frame_info *frame)
487 {
488   if (frame->signal_handler_caller)
489     return 0;
490
491   return frameless_look_for_prologue (frame);
492 }
493
494 /* Return the saved program counter for FRAME.  */
495
496 CORE_ADDR
497 i386_frame_saved_pc (struct frame_info *frame)
498 {
499   /* FIXME: kettenis/2001-05-09: Conditionalizing the next bit of code
500      on SIGCONTEXT_PC_OFFSET and I386V4_SIGTRAMP_SAVED_PC should be
501      considered a temporary hack.  I plan to come up with something
502      better when we go multi-arch.  */
503 #if defined (SIGCONTEXT_PC_OFFSET) || defined (I386V4_SIGTRAMP_SAVED_PC)
504   if (frame->signal_handler_caller)
505     return sigtramp_saved_pc (frame);
506 #endif
507
508   return read_memory_unsigned_integer (frame->frame + 4, 4);
509 }
510
511 CORE_ADDR
512 i386go32_frame_saved_pc (struct frame_info *frame)
513 {
514   return read_memory_integer (frame->frame + 4, 4);
515 }
516
517 /* Immediately after a function call, return the saved pc.  */
518
519 CORE_ADDR
520 i386_saved_pc_after_call (struct frame_info *frame)
521 {
522   return read_memory_unsigned_integer (read_register (SP_REGNUM), 4);
523 }
524
525 /* Return number of args passed to a frame.
526    Can return -1, meaning no way to tell.  */
527
528 int
529 i386_frame_num_args (struct frame_info *fi)
530 {
531 #if 1
532   return -1;
533 #else
534   /* This loses because not only might the compiler not be popping the
535      args right after the function call, it might be popping args from
536      both this call and a previous one, and we would say there are
537      more args than there really are.  */
538
539   int retpc;
540   unsigned char op;
541   struct frame_info *pfi;
542
543   /* On the i386, the instruction following the call could be:
544      popl %ecx        -  one arg
545      addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
546      anything else    -  zero args.  */
547
548   int frameless;
549
550   frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
551   if (frameless)
552     /* In the absence of a frame pointer, GDB doesn't get correct
553        values for nameless arguments.  Return -1, so it doesn't print
554        any nameless arguments.  */
555     return -1;
556
557   pfi = get_prev_frame (fi);
558   if (pfi == 0)
559     {
560       /* NOTE: This can happen if we are looking at the frame for
561          main, because FRAME_CHAIN_VALID won't let us go into start.
562          If we have debugging symbols, that's not really a big deal;
563          it just means it will only show as many arguments to main as
564          are declared.  */
565       return -1;
566     }
567   else
568     {
569       retpc = pfi->pc;
570       op = read_memory_integer (retpc, 1);
571       if (op == 0x59)           /* pop %ecx */
572         return 1;
573       else if (op == 0x83)
574         {
575           op = read_memory_integer (retpc + 1, 1);
576           if (op == 0xc4)
577             /* addl $<signed imm 8 bits>, %esp */
578             return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
579           else
580             return 0;
581         }
582       else if (op == 0x81)      /* `add' with 32 bit immediate.  */
583         {
584           op = read_memory_integer (retpc + 1, 1);
585           if (op == 0xc4)
586             /* addl $<imm 32>, %esp */
587             return read_memory_integer (retpc + 2, 4) / 4;
588           else
589             return 0;
590         }
591       else
592         {
593           return 0;
594         }
595     }
596 #endif
597 }
598
599 /* Parse the first few instructions the function to see what registers
600    were stored.
601    
602    We handle these cases:
603
604    The startup sequence can be at the start of the function, or the
605    function can start with a branch to startup code at the end.
606
607    %ebp can be set up with either the 'enter' instruction, or "pushl
608    %ebp, movl %esp, %ebp" (`enter' is too slow to be useful, but was
609    once used in the System V compiler).
610
611    Local space is allocated just below the saved %ebp by either the
612    'enter' instruction, or by "subl $<size>, %esp".  'enter' has a 16
613    bit unsigned argument for space to allocate, and the 'addl'
614    instruction could have either a signed byte, or 32 bit immediate.
615
616    Next, the registers used by this function are pushed.  With the
617    System V compiler they will always be in the order: %edi, %esi,
618    %ebx (and sometimes a harmless bug causes it to also save but not
619    restore %eax); however, the code below is willing to see the pushes
620    in any order, and will handle up to 8 of them.
621  
622    If the setup sequence is at the end of the function, then the next
623    instruction will be a branch back to the start.  */
624
625 void
626 i386_frame_init_saved_regs (struct frame_info *fip)
627 {
628   long locals = -1;
629   unsigned char op;
630   CORE_ADDR dummy_bottom;
631   CORE_ADDR addr;
632   CORE_ADDR pc;
633   int i;
634
635   if (fip->saved_regs)
636     return;
637
638   frame_saved_regs_zalloc (fip);
639
640   /* If the frame is the end of a dummy, compute where the beginning
641      would be.  */
642   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
643
644   /* Check if the PC points in the stack, in a dummy frame.  */
645   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
646     {
647       /* All registers were saved by push_call_dummy.  */
648       addr = fip->frame;
649       for (i = 0; i < NUM_REGS; i++)
650         {
651           addr -= REGISTER_RAW_SIZE (i);
652           fip->saved_regs[i] = addr;
653         }
654       return;
655     }
656
657   pc = get_pc_function_start (fip->pc);
658   if (pc != 0)
659     locals = i386_get_frame_setup (pc);
660
661   if (locals >= 0)
662     {
663       addr = fip->frame - 4 - locals;
664       for (i = 0; i < 8; i++)
665         {
666           op = codestream_get ();
667           if (op < 0x50 || op > 0x57)
668             break;
669 #ifdef I386_REGNO_TO_SYMMETRY
670           /* Dynix uses different internal numbering.  Ick.  */
671           fip->saved_regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = addr;
672 #else
673           fip->saved_regs[op - 0x50] = addr;
674 #endif
675           addr -= 4;
676         }
677     }
678
679   fip->saved_regs[PC_REGNUM] = fip->frame + 4;
680   fip->saved_regs[FP_REGNUM] = fip->frame;
681 }
682
683 /* Return PC of first real instruction.  */
684
685 int
686 i386_skip_prologue (int pc)
687 {
688   unsigned char op;
689   int i;
690   static unsigned char pic_pat[6] =
691   { 0xe8, 0, 0, 0, 0,           /* call   0x0 */
692     0x5b,                       /* popl   %ebx */
693   };
694   CORE_ADDR pos;
695
696   if (i386_get_frame_setup (pc) < 0)
697     return (pc);
698
699   /* Found valid frame setup -- codestream now points to start of push
700      instructions for saving registers.  */
701
702   /* Skip over register saves.  */
703   for (i = 0; i < 8; i++)
704     {
705       op = codestream_peek ();
706       /* Break if not `pushl' instrunction.  */
707       if (op < 0x50 || op > 0x57)
708         break;
709       codestream_get ();
710     }
711
712   /* The native cc on SVR4 in -K PIC mode inserts the following code
713      to get the address of the global offset table (GOT) into register
714      %ebx
715      
716         call    0x0
717         popl    %ebx
718         movl    %ebx,x(%ebp)    (optional)
719         addl    y,%ebx
720
721      This code is with the rest of the prologue (at the end of the
722      function), so we have to skip it to get to the first real
723      instruction at the start of the function.  */
724
725   pos = codestream_tell ();
726   for (i = 0; i < 6; i++)
727     {
728       op = codestream_get ();
729       if (pic_pat[i] != op)
730         break;
731     }
732   if (i == 6)
733     {
734       unsigned char buf[4];
735       long delta = 6;
736
737       op = codestream_get ();
738       if (op == 0x89)           /* movl %ebx, x(%ebp) */
739         {
740           op = codestream_get ();
741           if (op == 0x5d)       /* One byte offset from %ebp.  */
742             {
743               delta += 3;
744               codestream_read (buf, 1);
745             }
746           else if (op == 0x9d)  /* Four byte offset from %ebp.  */
747             {
748               delta += 6;
749               codestream_read (buf, 4);
750             }
751           else                  /* Unexpected instruction.  */
752             delta = -1;
753           op = codestream_get ();
754         }
755       /* addl y,%ebx */
756       if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
757         {
758           pos += delta + 6;
759         }
760     }
761   codestream_seek (pos);
762
763   i386_follow_jump ();
764
765   return (codestream_tell ());
766 }
767
768 void
769 i386_push_dummy_frame (void)
770 {
771   CORE_ADDR sp = read_register (SP_REGNUM);
772   CORE_ADDR fp;
773   int regnum;
774   char regbuf[MAX_REGISTER_RAW_SIZE];
775
776   sp = push_word (sp, read_register (PC_REGNUM));
777   sp = push_word (sp, read_register (FP_REGNUM));
778   fp = sp;
779   for (regnum = 0; regnum < NUM_REGS; regnum++)
780     {
781       read_register_gen (regnum, regbuf);
782       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
783     }
784   write_register (SP_REGNUM, sp);
785   write_register (FP_REGNUM, fp);
786 }
787
788 /* Insert the (relative) function address into the call sequence
789    stored at DYMMY.  */
790
791 void
792 i386_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
793                      struct value **args, struct type *type, int gcc_p)
794 {
795   int from, to, delta, loc;
796
797   loc = (int)(read_register (SP_REGNUM) - CALL_DUMMY_LENGTH);
798   from = loc + 5;
799   to = (int)(fun);
800   delta = to - from;
801
802   *((char *)(dummy) + 1) = (delta & 0xff);
803   *((char *)(dummy) + 2) = ((delta >> 8) & 0xff);
804   *((char *)(dummy) + 3) = ((delta >> 16) & 0xff);
805   *((char *)(dummy) + 4) = ((delta >> 24) & 0xff);
806 }
807
808 void
809 i386_pop_frame (void)
810 {
811   struct frame_info *frame = get_current_frame ();
812   CORE_ADDR fp;
813   int regnum;
814   char regbuf[MAX_REGISTER_RAW_SIZE];
815
816   fp = FRAME_FP (frame);
817   i386_frame_init_saved_regs (frame);
818
819   for (regnum = 0; regnum < NUM_REGS; regnum++)
820     {
821       CORE_ADDR addr;
822       addr = frame->saved_regs[regnum];
823       if (addr)
824         {
825           read_memory (addr, regbuf, REGISTER_RAW_SIZE (regnum));
826           write_register_bytes (REGISTER_BYTE (regnum), regbuf,
827                                 REGISTER_RAW_SIZE (regnum));
828         }
829     }
830   write_register (FP_REGNUM, read_memory_integer (fp, 4));
831   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
832   write_register (SP_REGNUM, fp + 8);
833   flush_cached_frames ();
834 }
835 \f
836
837 #ifdef GET_LONGJMP_TARGET
838
839 /* FIXME: Multi-arching does not set JB_PC and JB_ELEMENT_SIZE yet.  
840    Fill in with dummy value to enable compilation.  */
841 #ifndef JB_PC
842 #define JB_PC 0
843 #endif /* JB_PC */
844
845 #ifndef JB_ELEMENT_SIZE
846 #define JB_ELEMENT_SIZE 4
847 #endif /* JB_ELEMENT_SIZE */
848
849 /* Figure out where the longjmp will land.  Slurp the args out of the
850    stack.  We expect the first arg to be a pointer to the jmp_buf
851    structure from which we extract the pc (JB_PC) that we will land
852    at.  The pc is copied into PC.  This routine returns true on
853    success.  */
854
855 int
856 get_longjmp_target (CORE_ADDR *pc)
857 {
858   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
859   CORE_ADDR sp, jb_addr;
860
861   sp = read_register (SP_REGNUM);
862
863   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack.  */
864                           buf,
865                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
866     return 0;
867
868   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
869
870   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
871                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
872     return 0;
873
874   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
875
876   return 1;
877 }
878
879 #endif /* GET_LONGJMP_TARGET */
880 \f
881
882 CORE_ADDR
883 i386_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
884                      int struct_return, CORE_ADDR struct_addr)
885 {
886   sp = default_push_arguments (nargs, args, sp, struct_return, struct_addr);
887   
888   if (struct_return)
889     {
890       char buf[4];
891
892       sp -= 4;
893       store_address (buf, 4, struct_addr);
894       write_memory (sp, buf, 4);
895     }
896
897   return sp;
898 }
899
900 void
901 i386_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
902 {
903   /* Do nothing.  Everything was already done by i386_push_arguments.  */
904 }
905
906 /* These registers are used for returning integers (and on some
907    targets also for returning `struct' and `union' values when their
908    size and alignment match an integer type).  */
909 #define LOW_RETURN_REGNUM 0     /* %eax */
910 #define HIGH_RETURN_REGNUM 2    /* %edx */
911
912 /* Extract from an array REGBUF containing the (raw) register state, a
913    function return value of TYPE, and copy that, in virtual format,
914    into VALBUF.  */
915
916 void
917 i386_extract_return_value (struct type *type, char *regbuf, char *valbuf)
918 {
919   int len = TYPE_LENGTH (type);
920
921   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
922       && TYPE_NFIELDS (type) == 1)
923     {
924       i386_extract_return_value (TYPE_FIELD_TYPE (type, 0), regbuf, valbuf);
925       return;
926     }
927
928   if (TYPE_CODE (type) == TYPE_CODE_FLT)
929     {
930       if (NUM_FREGS == 0)
931         {
932           warning ("Cannot find floating-point return value.");
933           memset (valbuf, 0, len);
934           return;
935         }
936
937       /* Floating-point return values can be found in %st(0).  Convert
938          its contents to the desired type.  This is probably not
939          exactly how it would happen on the target itself, but it is
940          the best we can do.  */
941       convert_typed_floating (&regbuf[REGISTER_BYTE (FP0_REGNUM)],
942                               builtin_type_i387_ext, valbuf, type);
943     }
944   else
945     {
946       int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
947       int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
948
949       if (len <= low_size)
950         memcpy (valbuf, &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], len);
951       else if (len <= (low_size + high_size))
952         {
953           memcpy (valbuf,
954                   &regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)], low_size);
955           memcpy (valbuf + low_size,
956                   &regbuf[REGISTER_BYTE (HIGH_RETURN_REGNUM)], len - low_size);
957         }
958       else
959         internal_error (__FILE__, __LINE__,
960                         "Cannot extract return value of %d bytes long.", len);
961     }
962 }
963
964 /* Write into the appropriate registers a function return value stored
965    in VALBUF of type TYPE, given in virtual format.  */
966
967 void
968 i386_store_return_value (struct type *type, char *valbuf)
969 {
970   int len = TYPE_LENGTH (type);
971
972   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
973       && TYPE_NFIELDS (type) == 1)
974     {
975       i386_store_return_value (TYPE_FIELD_TYPE (type, 0), valbuf);
976       return;
977     }
978
979   if (TYPE_CODE (type) == TYPE_CODE_FLT)
980     {
981       unsigned int fstat;
982       char buf[FPU_REG_RAW_SIZE];
983
984       if (NUM_FREGS == 0)
985         {
986           warning ("Cannot set floating-point return value.");
987           return;
988         }
989
990       /* Returning floating-point values is a bit tricky.  Apart from
991          storing the return value in %st(0), we have to simulate the
992          state of the FPU at function return point.  */
993
994       /* Convert the value found in VALBUF to the extended
995          floating-point format used by the FPU.  This is probably
996          not exactly how it would happen on the target itself, but
997          it is the best we can do.  */
998       convert_typed_floating (valbuf, type, buf, builtin_type_i387_ext);
999       write_register_bytes (REGISTER_BYTE (FP0_REGNUM), buf,
1000                             FPU_REG_RAW_SIZE);
1001
1002       /* Set the top of the floating-point register stack to 7.  The
1003          actual value doesn't really matter, but 7 is what a normal
1004          function return would end up with if the program started out
1005          with a freshly initialized FPU.  */
1006       fstat = read_register (FSTAT_REGNUM);
1007       fstat |= (7 << 11);
1008       write_register (FSTAT_REGNUM, fstat);
1009
1010       /* Mark %st(1) through %st(7) as empty.  Since we set the top of
1011          the floating-point register stack to 7, the appropriate value
1012          for the tag word is 0x3fff.  */
1013       write_register (FTAG_REGNUM, 0x3fff);
1014     }
1015   else
1016     {
1017       int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
1018       int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
1019
1020       if (len <= low_size)
1021         write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM), valbuf, len);
1022       else if (len <= (low_size + high_size))
1023         {
1024           write_register_bytes (REGISTER_BYTE (LOW_RETURN_REGNUM),
1025                                 valbuf, low_size);
1026           write_register_bytes (REGISTER_BYTE (HIGH_RETURN_REGNUM),
1027                                 valbuf + low_size, len - low_size);
1028         }
1029       else
1030         internal_error (__FILE__, __LINE__,
1031                         "Cannot store return value of %d bytes long.", len);
1032     }
1033 }
1034
1035 /* Extract from an array REGBUF containing the (raw) register state
1036    the address in which a function should return its structure value,
1037    as a CORE_ADDR.  */
1038
1039 CORE_ADDR
1040 i386_extract_struct_value_address (char *regbuf)
1041 {
1042   return extract_address (&regbuf[REGISTER_BYTE (LOW_RETURN_REGNUM)],
1043                           REGISTER_RAW_SIZE (LOW_RETURN_REGNUM));
1044 }
1045 \f
1046
1047 /* Return the GDB type object for the "standard" data type of data in
1048    register REGNUM.  Perhaps %esi and %edi should go here, but
1049    potentially they could be used for things other than address.  */
1050
1051 struct type *
1052 i386_register_virtual_type (int regnum)
1053 {
1054   if (regnum == PC_REGNUM || regnum == FP_REGNUM || regnum == SP_REGNUM)
1055     return lookup_pointer_type (builtin_type_void);
1056
1057   if (IS_FP_REGNUM (regnum))
1058     return builtin_type_i387_ext;
1059
1060   if (IS_SSE_REGNUM (regnum))
1061     return builtin_type_v4sf;
1062
1063   return builtin_type_int;
1064 }
1065
1066 /* Return true iff register REGNUM's virtual format is different from
1067    its raw format.  Note that this definition assumes that the host
1068    supports IEEE 32-bit floats, since it doesn't say that SSE
1069    registers need conversion.  Even if we can't find a counterexample,
1070    this is still sloppy.  */
1071
1072 int
1073 i386_register_convertible (int regnum)
1074 {
1075   return IS_FP_REGNUM (regnum);
1076 }
1077
1078 /* Convert data from raw format for register REGNUM in buffer FROM to
1079    virtual format with type TYPE in buffer TO.  */
1080
1081 void
1082 i386_register_convert_to_virtual (int regnum, struct type *type,
1083                                   char *from, char *to)
1084 {
1085   gdb_assert (IS_FP_REGNUM (regnum));
1086
1087   /* We only support floating-point values.  */
1088   if (TYPE_CODE (type) != TYPE_CODE_FLT)
1089     {
1090       warning ("Cannot convert floating-point register value "
1091                "to non-floating-point type.");
1092       memset (to, 0, TYPE_LENGTH (type));
1093       return;
1094     }
1095
1096   /* Convert to TYPE.  This should be a no-op if TYPE is equivalent to
1097      the extended floating-point format used by the FPU.  */
1098   convert_typed_floating (from, builtin_type_i387_ext, to, type);
1099 }
1100
1101 /* Convert data from virtual format with type TYPE in buffer FROM to
1102    raw format for register REGNUM in buffer TO.  */
1103
1104 void
1105 i386_register_convert_to_raw (struct type *type, int regnum,
1106                               char *from, char *to)
1107 {
1108   gdb_assert (IS_FP_REGNUM (regnum));
1109
1110   /* We only support floating-point values.  */
1111   if (TYPE_CODE (type) != TYPE_CODE_FLT)
1112     {
1113       warning ("Cannot convert non-floating-point type "
1114                "to floating-point register value.");
1115       memset (to, 0, TYPE_LENGTH (type));
1116       return;
1117     }
1118
1119   /* Convert from TYPE.  This should be a no-op if TYPE is equivalent
1120      to the extended floating-point format used by the FPU.  */
1121   convert_typed_floating (from, type, to, builtin_type_i387_ext);
1122 }
1123 \f     
1124
1125 #ifdef I386V4_SIGTRAMP_SAVED_PC
1126 /* Get saved user PC for sigtramp from the pushed ucontext on the
1127    stack for all three variants of SVR4 sigtramps.  */
1128
1129 CORE_ADDR
1130 i386v4_sigtramp_saved_pc (struct frame_info *frame)
1131 {
1132   CORE_ADDR saved_pc_offset = 4;
1133   char *name = NULL;
1134
1135   find_pc_partial_function (frame->pc, &name, NULL, NULL);
1136   if (name)
1137     {
1138       if (STREQ (name, "_sigreturn"))
1139         saved_pc_offset = 132 + 14 * 4;
1140       else if (STREQ (name, "_sigacthandler"))
1141         saved_pc_offset = 80 + 14 * 4;
1142       else if (STREQ (name, "sigvechandler"))
1143         saved_pc_offset = 120 + 14 * 4;
1144     }
1145
1146   if (frame->next)
1147     return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
1148   return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
1149 }
1150 #endif /* I386V4_SIGTRAMP_SAVED_PC */
1151 \f
1152
1153 #ifdef STATIC_TRANSFORM_NAME
1154 /* SunPRO encodes the static variables.  This is not related to C++
1155    mangling, it is done for C too.  */
1156
1157 char *
1158 sunpro_static_transform_name (char *name)
1159 {
1160   char *p;
1161   if (IS_STATIC_TRANSFORM_NAME (name))
1162     {
1163       /* For file-local statics there will be a period, a bunch of
1164          junk (the contents of which match a string given in the
1165          N_OPT), a period and the name.  For function-local statics
1166          there will be a bunch of junk (which seems to change the
1167          second character from 'A' to 'B'), a period, the name of the
1168          function, and the name.  So just skip everything before the
1169          last period.  */
1170       p = strrchr (name, '.');
1171       if (p != NULL)
1172         name = p + 1;
1173     }
1174   return name;
1175 }
1176 #endif /* STATIC_TRANSFORM_NAME */
1177 \f
1178
1179 /* Stuff for WIN32 PE style DLL's but is pretty generic really.  */
1180
1181 CORE_ADDR
1182 skip_trampoline_code (CORE_ADDR pc, char *name)
1183 {
1184   if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff) /* jmp *(dest) */
1185     {
1186       unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
1187       struct minimal_symbol *indsym =
1188         indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
1189       char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
1190
1191       if (symname)
1192         {
1193           if (strncmp (symname, "__imp_", 6) == 0
1194               || strncmp (symname, "_imp_", 5) == 0)
1195             return name ? 1 : read_memory_unsigned_integer (indirect, 4);
1196         }
1197     }
1198   return 0;                     /* Not a trampoline.  */
1199 }
1200 \f
1201
1202 /* We have two flavours of disassembly.  The machinery on this page
1203    deals with switching between those.  */
1204
1205 static int
1206 gdb_print_insn_i386 (bfd_vma memaddr, disassemble_info *info)
1207 {
1208   if (disassembly_flavor == att_flavor)
1209     return print_insn_i386_att (memaddr, info);
1210   else if (disassembly_flavor == intel_flavor)
1211     return print_insn_i386_intel (memaddr, info);
1212   /* Never reached -- disassembly_flavour is always either att_flavor
1213      or intel_flavor.  */
1214   internal_error (__FILE__, __LINE__, "failed internal consistency check");
1215 }
1216
1217 \f
1218 static void
1219 process_note_abi_tag_sections (bfd *abfd, asection *sect, void *obj)
1220 {
1221   int *os_ident_ptr = obj;
1222   const char *name;
1223   unsigned int sect_size;
1224
1225   name = bfd_get_section_name (abfd, sect);
1226   sect_size = bfd_section_size (abfd, sect);
1227   if (strcmp (name, ".note.ABI-tag") == 0 && sect_size > 0)
1228     {
1229       unsigned int name_length, data_length, note_type;
1230       char *note = alloca (sect_size);
1231
1232       bfd_get_section_contents (abfd, sect, note,
1233                                 (file_ptr) 0, (bfd_size_type) sect_size);
1234
1235       name_length = bfd_h_get_32 (abfd, note);
1236       data_length = bfd_h_get_32 (abfd, note + 4);
1237       note_type = bfd_h_get_32 (abfd, note + 8);
1238
1239       if (name_length == 4 && data_length == 16 && note_type == 1
1240           && strcmp (note + 12, "GNU") == 0)
1241         {
1242           int os_number = bfd_h_get_32 (abfd, note + 16);
1243
1244           /* The case numbers are from abi-tags in glibc.  */
1245           switch (os_number)
1246             {
1247             case 0:
1248               *os_ident_ptr = ELFOSABI_LINUX;
1249               break;
1250             case 1:
1251               *os_ident_ptr = ELFOSABI_HURD;
1252               break;
1253             case 2:
1254               *os_ident_ptr = ELFOSABI_SOLARIS;
1255               break;
1256             default:
1257               internal_error (__FILE__, __LINE__,
1258                               "process_note_abi_sections: "
1259                               "unknown OS number %d", os_number);
1260               break;
1261             }
1262         }
1263     }
1264 }
1265
1266 struct gdbarch *
1267 i386_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1268 {
1269   struct gdbarch_tdep *tdep;
1270   struct gdbarch *gdbarch;
1271   int os_ident;
1272
1273   if (info.abfd != NULL
1274       && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1275     {
1276       os_ident = elf_elfheader (info.abfd)->e_ident[EI_OSABI];
1277
1278       /* If os_ident is 0, it is not necessarily the case that we're
1279          on a SYSV system.  (ELFOSABI_NONE is defined to be 0.)
1280          GNU/Linux uses a note section to record OS/ABI info, but
1281          leaves e_ident[EI_OSABI] zero.  So we have to check for note
1282          sections too.  */
1283       if (os_ident == ELFOSABI_NONE)
1284         bfd_map_over_sections (info.abfd,
1285                                process_note_abi_tag_sections,
1286                                &os_ident);
1287           
1288       /* If that didn't help us, revert to some non-standard checks.  */
1289       if (os_ident == ELFOSABI_NONE)
1290         {
1291           /* FreeBSD folks are naughty; they stored the string
1292              "FreeBSD" in the padding of the e_ident field of the ELF
1293              header.  */
1294           if (strcmp (&elf_elfheader (info.abfd)->e_ident[8], "FreeBSD") == 0)
1295             os_ident = ELFOSABI_FREEBSD;
1296         }
1297     }
1298   else
1299     os_ident = -1;
1300
1301   for (arches = gdbarch_list_lookup_by_info (arches, &info);
1302        arches != NULL;
1303        arches = gdbarch_list_lookup_by_info (arches->next, &info))
1304     {
1305       tdep = gdbarch_tdep (arches->gdbarch);
1306       if (tdep && tdep->os_ident == os_ident)
1307         return arches->gdbarch;
1308     }
1309
1310   /* Allocate space for the new architecture.  */
1311   tdep = XMALLOC (struct gdbarch_tdep);
1312   gdbarch = gdbarch_alloc (&info, tdep);
1313
1314   tdep->os_ident = os_ident;
1315
1316   /* FIXME: kettenis/2001-11-24: Although not all IA-32 processors
1317      have the SSE registers, it's easier to set the default to 8.  */
1318   tdep->num_xmm_regs = 8;
1319
1320   set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
1321
1322   /* Call dummy code.  */
1323   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
1324   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 5);
1325   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1326   set_gdbarch_call_dummy_p (gdbarch, 1);
1327   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1328
1329   set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1330   set_gdbarch_push_arguments (gdbarch, i386_push_arguments);
1331
1332   set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
1333
1334   /* NOTE: tm-i386nw.h and tm-i386v4.h override this.  */
1335   set_gdbarch_frame_chain_valid (gdbarch, file_frame_chain_valid);
1336
1337   /* NOTE: tm-i386aix.h, tm-i386bsd.h, tm-i386os9k.h, tm-linux.h,
1338      tm-ptx.h, tm-symmetry.h currently override this.  Sigh.  */
1339   set_gdbarch_num_regs (gdbarch, NUM_GREGS + NUM_FREGS + NUM_SSE_REGS);
1340
1341   return gdbarch;
1342 }
1343
1344 /* Provide a prototype to silence -Wmissing-prototypes.  */
1345 void _initialize_i386_tdep (void);
1346
1347 void
1348 _initialize_i386_tdep (void)
1349 {
1350   register_gdbarch_init (bfd_arch_i386, i386_gdbarch_init);
1351
1352   /* Initialize the table saying where each register starts in the
1353      register file.  */
1354   {
1355     int i, offset;
1356
1357     offset = 0;
1358     for (i = 0; i < MAX_NUM_REGS; i++)
1359       {
1360         i386_register_offset[i] = offset;
1361         offset += i386_register_size[i];
1362       }
1363   }
1364
1365   tm_print_insn = gdb_print_insn_i386;
1366   tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1367
1368   /* Add the variable that controls the disassembly flavor.  */
1369   {
1370     struct cmd_list_element *new_cmd;
1371
1372     new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1373                                 valid_flavors,
1374                                 &disassembly_flavor,
1375                                 "\
1376 Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1377 and the default value is \"att\".",
1378                                 &setlist);
1379     add_show_from_set (new_cmd, &showlist);
1380   }
1381 }