From Eli. Bring go32 target up-to-date with recent GDB i386 changes.
[external/binutils.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2    Copyright (C) 1988, 1989, 1991, 1994, 1995, 1996, 1998
3    Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "floatformat.h"
29 #include "symtab.h"
30 #include "gdbcmd.h"
31 #include "command.h"
32
33 static long i386_get_frame_setup PARAMS ((CORE_ADDR));
34
35 static void i386_follow_jump PARAMS ((void));
36
37 static void codestream_read PARAMS ((unsigned char *, int));
38
39 static void codestream_seek PARAMS ((CORE_ADDR));
40
41 static unsigned char codestream_fill PARAMS ((int));
42
43 CORE_ADDR skip_trampoline_code PARAMS ((CORE_ADDR, char *));
44
45 static int gdb_print_insn_i386 (bfd_vma, disassemble_info *);
46
47 void _initialize_i386_tdep PARAMS ((void));
48
49 /* i386_register_byte[i] is the offset into the register file of the
50    start of register number i.  We initialize this from
51    i386_register_raw_size.  */
52 int i386_register_byte[MAX_NUM_REGS];
53
54 /* i386_register_raw_size[i] is the number of bytes of storage in
55    GDB's register array occupied by register i.  */
56 int i386_register_raw_size[MAX_NUM_REGS] = {
57    4,  4,  4,  4,
58    4,  4,  4,  4,
59    4,  4,  4,  4,
60    4,  4,  4,  4,
61   10, 10, 10, 10,
62   10, 10, 10, 10,
63    4,  4,  4,  4,
64    4,  4,  4,  4,
65   16, 16, 16, 16,
66   16, 16, 16, 16,
67    4
68 };
69
70 /* i386_register_virtual_size[i] is the size in bytes of the virtual
71    type of register i.  */
72 int i386_register_virtual_size[MAX_NUM_REGS];
73
74
75 /* This is the variable the is set with "set disassembly-flavor",
76    and its legitimate values. */
77 static char att_flavor[] = "att";
78 static char intel_flavor[] = "intel";
79 static char *valid_flavors[] =
80 {
81   att_flavor,
82   intel_flavor,
83   NULL
84 };
85 static char *disassembly_flavor = att_flavor;
86
87 static void i386_print_register PARAMS ((char *, int, int));
88
89 /* This is used to keep the bfd arch_info in sync with the disassembly flavor.  */
90 static void set_disassembly_flavor_sfunc PARAMS ((char *, int, struct cmd_list_element *));
91 static void set_disassembly_flavor PARAMS ((void));
92
93 /* Stdio style buffering was used to minimize calls to ptrace, but this
94    buffering did not take into account that the code section being accessed
95    may not be an even number of buffers long (even if the buffer is only
96    sizeof(int) long).  In cases where the code section size happened to
97    be a non-integral number of buffers long, attempting to read the last
98    buffer would fail.  Simply using target_read_memory and ignoring errors,
99    rather than read_memory, is not the correct solution, since legitimate
100    access errors would then be totally ignored.  To properly handle this
101    situation and continue to use buffering would require that this code
102    be able to determine the minimum code section size granularity (not the
103    alignment of the section itself, since the actual failing case that
104    pointed out this problem had a section alignment of 4 but was not a
105    multiple of 4 bytes long), on a target by target basis, and then
106    adjust it's buffer size accordingly.  This is messy, but potentially
107    feasible.  It probably needs the bfd library's help and support.  For
108    now, the buffer size is set to 1.  (FIXME -fnf) */
109
110 #define CODESTREAM_BUFSIZ 1     /* Was sizeof(int), see note above. */
111 static CORE_ADDR codestream_next_addr;
112 static CORE_ADDR codestream_addr;
113 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
114 static int codestream_off;
115 static int codestream_cnt;
116
117 #define codestream_tell() (codestream_addr + codestream_off)
118 #define codestream_peek() (codestream_cnt == 0 ? \
119                            codestream_fill(1): codestream_buf[codestream_off])
120 #define codestream_get() (codestream_cnt-- == 0 ? \
121                          codestream_fill(0) : codestream_buf[codestream_off++])
122
123 static unsigned char
124 codestream_fill (peek_flag)
125      int peek_flag;
126 {
127   codestream_addr = codestream_next_addr;
128   codestream_next_addr += CODESTREAM_BUFSIZ;
129   codestream_off = 0;
130   codestream_cnt = CODESTREAM_BUFSIZ;
131   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
132
133   if (peek_flag)
134     return (codestream_peek ());
135   else
136     return (codestream_get ());
137 }
138
139 static void
140 codestream_seek (place)
141      CORE_ADDR place;
142 {
143   codestream_next_addr = place / CODESTREAM_BUFSIZ;
144   codestream_next_addr *= CODESTREAM_BUFSIZ;
145   codestream_cnt = 0;
146   codestream_fill (1);
147   while (codestream_tell () != place)
148     codestream_get ();
149 }
150
151 static void
152 codestream_read (buf, count)
153      unsigned char *buf;
154      int count;
155 {
156   unsigned char *p;
157   int i;
158   p = buf;
159   for (i = 0; i < count; i++)
160     *p++ = codestream_get ();
161 }
162
163 /* next instruction is a jump, move to target */
164
165 static void
166 i386_follow_jump ()
167 {
168   unsigned char buf[4];
169   long delta;
170
171   int data16;
172   CORE_ADDR pos;
173
174   pos = codestream_tell ();
175
176   data16 = 0;
177   if (codestream_peek () == 0x66)
178     {
179       codestream_get ();
180       data16 = 1;
181     }
182
183   switch (codestream_get ())
184     {
185     case 0xe9:
186       /* relative jump: if data16 == 0, disp32, else disp16 */
187       if (data16)
188         {
189           codestream_read (buf, 2);
190           delta = extract_signed_integer (buf, 2);
191
192           /* include size of jmp inst (including the 0x66 prefix).  */
193           pos += delta + 4;
194         }
195       else
196         {
197           codestream_read (buf, 4);
198           delta = extract_signed_integer (buf, 4);
199
200           pos += delta + 5;
201         }
202       break;
203     case 0xeb:
204       /* relative jump, disp8 (ignore data16) */
205       codestream_read (buf, 1);
206       /* Sign-extend it.  */
207       delta = extract_signed_integer (buf, 1);
208
209       pos += delta + 2;
210       break;
211     }
212   codestream_seek (pos);
213 }
214
215 /*
216  * find & return amound a local space allocated, and advance codestream to
217  * first register push (if any)
218  *
219  * if entry sequence doesn't make sense, return -1, and leave 
220  * codestream pointer random
221  */
222
223 static long
224 i386_get_frame_setup (pc)
225      CORE_ADDR pc;
226 {
227   unsigned char op;
228
229   codestream_seek (pc);
230
231   i386_follow_jump ();
232
233   op = codestream_get ();
234
235   if (op == 0x58)               /* popl %eax */
236     {
237       /*
238        * this function must start with
239        * 
240        *    popl %eax             0x58
241        *    xchgl %eax, (%esp)  0x87 0x04 0x24
242        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
243        *
244        * (the system 5 compiler puts out the second xchg
245        * inst, and the assembler doesn't try to optimize it,
246        * so the 'sib' form gets generated)
247        * 
248        * this sequence is used to get the address of the return
249        * buffer for a function that returns a structure
250        */
251       int pos;
252       unsigned char buf[4];
253       static unsigned char proto1[3] =
254       {0x87, 0x04, 0x24};
255       static unsigned char proto2[4] =
256       {0x87, 0x44, 0x24, 0x00};
257       pos = codestream_tell ();
258       codestream_read (buf, 4);
259       if (memcmp (buf, proto1, 3) == 0)
260         pos += 3;
261       else if (memcmp (buf, proto2, 4) == 0)
262         pos += 4;
263
264       codestream_seek (pos);
265       op = codestream_get ();   /* update next opcode */
266     }
267
268   if (op == 0x68 || op == 0x6a)
269     {
270       /*
271        * this function may start with
272        *
273        *   pushl constant
274        *   call _probe
275        *   addl $4, %esp
276        *      followed by 
277        *     pushl %ebp
278        *     etc.
279        */
280       int pos;
281       unsigned char buf[8];
282
283       /* Skip past the pushl instruction; it has either a one-byte 
284          or a four-byte operand, depending on the opcode.  */
285       pos = codestream_tell ();
286       if (op == 0x68)
287         pos += 4;
288       else
289         pos += 1;
290       codestream_seek (pos);
291
292       /* Read the following 8 bytes, which should be "call _probe" (6 bytes)
293          followed by "addl $4,%esp" (2 bytes).  */
294       codestream_read (buf, sizeof (buf));
295       if (buf[0] == 0xe8 && buf[6] == 0xc4 && buf[7] == 0x4)
296         pos += sizeof (buf);
297       codestream_seek (pos);
298       op = codestream_get ();   /* update next opcode */
299     }
300
301   if (op == 0x55)               /* pushl %ebp */
302     {
303       /* check for movl %esp, %ebp - can be written two ways */
304       switch (codestream_get ())
305         {
306         case 0x8b:
307           if (codestream_get () != 0xec)
308             return (-1);
309           break;
310         case 0x89:
311           if (codestream_get () != 0xe5)
312             return (-1);
313           break;
314         default:
315           return (-1);
316         }
317       /* check for stack adjustment 
318
319        *  subl $XXX, %esp
320        *
321        * note: you can't subtract a 16 bit immediate
322        * from a 32 bit reg, so we don't have to worry
323        * about a data16 prefix 
324        */
325       op = codestream_peek ();
326       if (op == 0x83)
327         {
328           /* subl with 8 bit immed */
329           codestream_get ();
330           if (codestream_get () != 0xec)
331             /* Some instruction starting with 0x83 other than subl.  */
332             {
333               codestream_seek (codestream_tell () - 2);
334               return 0;
335             }
336           /* subl with signed byte immediate 
337            * (though it wouldn't make sense to be negative)
338            */
339           return (codestream_get ());
340         }
341       else if (op == 0x81)
342         {
343           char buf[4];
344           /* Maybe it is subl with 32 bit immedediate.  */
345           codestream_get ();
346           if (codestream_get () != 0xec)
347             /* Some instruction starting with 0x81 other than subl.  */
348             {
349               codestream_seek (codestream_tell () - 2);
350               return 0;
351             }
352           /* It is subl with 32 bit immediate.  */
353           codestream_read ((unsigned char *) buf, 4);
354           return extract_signed_integer (buf, 4);
355         }
356       else
357         {
358           return (0);
359         }
360     }
361   else if (op == 0xc8)
362     {
363       char buf[2];
364       /* enter instruction: arg is 16 bit unsigned immed */
365       codestream_read ((unsigned char *) buf, 2);
366       codestream_get ();        /* flush final byte of enter instruction */
367       return extract_unsigned_integer (buf, 2);
368     }
369   return (-1);
370 }
371
372 /* Return number of args passed to a frame.
373    Can return -1, meaning no way to tell.  */
374
375 int
376 i386_frame_num_args (fi)
377      struct frame_info *fi;
378 {
379 #if 1
380   return -1;
381 #else
382   /* This loses because not only might the compiler not be popping the
383      args right after the function call, it might be popping args from both
384      this call and a previous one, and we would say there are more args
385      than there really are.  */
386
387   int retpc;
388   unsigned char op;
389   struct frame_info *pfi;
390
391   /* on the 386, the instruction following the call could be:
392      popl %ecx        -  one arg
393      addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
394      anything else    -  zero args  */
395
396   int frameless;
397
398   frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
399   if (frameless)
400     /* In the absence of a frame pointer, GDB doesn't get correct values
401        for nameless arguments.  Return -1, so it doesn't print any
402        nameless arguments.  */
403     return -1;
404
405   pfi = get_prev_frame (fi);
406   if (pfi == 0)
407     {
408       /* Note:  this can happen if we are looking at the frame for
409          main, because FRAME_CHAIN_VALID won't let us go into
410          start.  If we have debugging symbols, that's not really
411          a big deal; it just means it will only show as many arguments
412          to main as are declared.  */
413       return -1;
414     }
415   else
416     {
417       retpc = pfi->pc;
418       op = read_memory_integer (retpc, 1);
419       if (op == 0x59)
420         /* pop %ecx */
421         return 1;
422       else if (op == 0x83)
423         {
424           op = read_memory_integer (retpc + 1, 1);
425           if (op == 0xc4)
426             /* addl $<signed imm 8 bits>, %esp */
427             return (read_memory_integer (retpc + 2, 1) & 0xff) / 4;
428           else
429             return 0;
430         }
431       else if (op == 0x81)
432         {                       /* add with 32 bit immediate */
433           op = read_memory_integer (retpc + 1, 1);
434           if (op == 0xc4)
435             /* addl $<imm 32>, %esp */
436             return read_memory_integer (retpc + 2, 4) / 4;
437           else
438             return 0;
439         }
440       else
441         {
442           return 0;
443         }
444     }
445 #endif
446 }
447
448 /*
449  * parse the first few instructions of the function to see
450  * what registers were stored.
451  *
452  * We handle these cases:
453  *
454  * The startup sequence can be at the start of the function,
455  * or the function can start with a branch to startup code at the end.
456  *
457  * %ebp can be set up with either the 'enter' instruction, or 
458  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
459  * but was once used in the sys5 compiler)
460  *
461  * Local space is allocated just below the saved %ebp by either the
462  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
463  * a 16 bit unsigned argument for space to allocate, and the
464  * 'addl' instruction could have either a signed byte, or
465  * 32 bit immediate.
466  *
467  * Next, the registers used by this function are pushed.  In
468  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
469  * (and sometimes a harmless bug causes it to also save but not restore %eax);
470  * however, the code below is willing to see the pushes in any order,
471  * and will handle up to 8 of them.
472  *
473  * If the setup sequence is at the end of the function, then the
474  * next instruction will be a branch back to the start.
475  */
476
477 void
478 i386_frame_find_saved_regs (fip, fsrp)
479      struct frame_info *fip;
480      struct frame_saved_regs *fsrp;
481 {
482   long locals = -1;
483   unsigned char op;
484   CORE_ADDR dummy_bottom;
485   CORE_ADDR adr;
486   CORE_ADDR pc;
487   int i;
488
489   memset (fsrp, 0, sizeof *fsrp);
490
491   /* if frame is the end of a dummy, compute where the
492    * beginning would be
493    */
494   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
495
496   /* check if the PC is in the stack, in a dummy frame */
497   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
498     {
499       /* all regs were saved by push_call_dummy () */
500       adr = fip->frame;
501       for (i = 0; i < NUM_REGS; i++)
502         {
503           adr -= REGISTER_RAW_SIZE (i);
504           fsrp->regs[i] = adr;
505         }
506       return;
507     }
508
509   pc = get_pc_function_start (fip->pc);
510   if (pc != 0)
511     locals = i386_get_frame_setup (pc);
512
513   if (locals >= 0)
514     {
515       adr = fip->frame - 4 - locals;
516       for (i = 0; i < 8; i++)
517         {
518           op = codestream_get ();
519           if (op < 0x50 || op > 0x57)
520             break;
521 #ifdef I386_REGNO_TO_SYMMETRY
522           /* Dynix uses different internal numbering.  Ick.  */
523           fsrp->regs[I386_REGNO_TO_SYMMETRY (op - 0x50)] = adr;
524 #else
525           fsrp->regs[op - 0x50] = adr;
526 #endif
527           adr -= 4;
528         }
529     }
530
531   fsrp->regs[PC_REGNUM] = fip->frame + 4;
532   fsrp->regs[FP_REGNUM] = fip->frame;
533 }
534
535 /* return pc of first real instruction */
536
537 int
538 i386_skip_prologue (pc)
539      int pc;
540 {
541   unsigned char op;
542   int i;
543   static unsigned char pic_pat[6] =
544   {0xe8, 0, 0, 0, 0,            /* call   0x0 */
545    0x5b,                        /* popl   %ebx */
546   };
547   CORE_ADDR pos;
548
549   if (i386_get_frame_setup (pc) < 0)
550     return (pc);
551
552   /* found valid frame setup - codestream now points to 
553    * start of push instructions for saving registers
554    */
555
556   /* skip over register saves */
557   for (i = 0; i < 8; i++)
558     {
559       op = codestream_peek ();
560       /* break if not pushl inst */
561       if (op < 0x50 || op > 0x57)
562         break;
563       codestream_get ();
564     }
565
566   /* The native cc on SVR4 in -K PIC mode inserts the following code to get
567      the address of the global offset table (GOT) into register %ebx.
568      call       0x0
569      popl       %ebx
570      movl       %ebx,x(%ebp)    (optional)
571      addl       y,%ebx
572      This code is with the rest of the prologue (at the end of the
573      function), so we have to skip it to get to the first real
574      instruction at the start of the function.  */
575
576   pos = codestream_tell ();
577   for (i = 0; i < 6; i++)
578     {
579       op = codestream_get ();
580       if (pic_pat[i] != op)
581         break;
582     }
583   if (i == 6)
584     {
585       unsigned char buf[4];
586       long delta = 6;
587
588       op = codestream_get ();
589       if (op == 0x89)           /* movl %ebx, x(%ebp) */
590         {
591           op = codestream_get ();
592           if (op == 0x5d)       /* one byte offset from %ebp */
593             {
594               delta += 3;
595               codestream_read (buf, 1);
596             }
597           else if (op == 0x9d)  /* four byte offset from %ebp */
598             {
599               delta += 6;
600               codestream_read (buf, 4);
601             }
602           else                  /* unexpected instruction */
603             delta = -1;
604           op = codestream_get ();
605         }
606       /* addl y,%ebx */
607       if (delta > 0 && op == 0x81 && codestream_get () == 0xc3)
608         {
609           pos += delta + 6;
610         }
611     }
612   codestream_seek (pos);
613
614   i386_follow_jump ();
615
616   return (codestream_tell ());
617 }
618
619 void
620 i386_push_dummy_frame ()
621 {
622   CORE_ADDR sp = read_register (SP_REGNUM);
623   int regnum;
624   char regbuf[MAX_REGISTER_RAW_SIZE];
625
626   sp = push_word (sp, read_register (PC_REGNUM));
627   sp = push_word (sp, read_register (FP_REGNUM));
628   write_register (FP_REGNUM, sp);
629   for (regnum = 0; regnum < NUM_REGS; regnum++)
630     {
631       read_register_gen (regnum, regbuf);
632       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
633     }
634   write_register (SP_REGNUM, sp);
635 }
636
637 void
638 i386_pop_frame ()
639 {
640   struct frame_info *frame = get_current_frame ();
641   CORE_ADDR fp;
642   int regnum;
643   struct frame_saved_regs fsr;
644   char regbuf[MAX_REGISTER_RAW_SIZE];
645
646   fp = FRAME_FP (frame);
647   get_frame_saved_regs (frame, &fsr);
648   for (regnum = 0; regnum < NUM_REGS; regnum++)
649     {
650       CORE_ADDR adr;
651       adr = fsr.regs[regnum];
652       if (adr)
653         {
654           read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
655           write_register_bytes (REGISTER_BYTE (regnum), regbuf,
656                                 REGISTER_RAW_SIZE (regnum));
657         }
658     }
659   write_register (FP_REGNUM, read_memory_integer (fp, 4));
660   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
661   write_register (SP_REGNUM, fp + 8);
662   flush_cached_frames ();
663 }
664
665 #ifdef GET_LONGJMP_TARGET
666
667 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
668    We expect the first arg to be a pointer to the jmp_buf structure from which
669    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
670    This routine returns true on success. */
671
672 int
673 get_longjmp_target (pc)
674      CORE_ADDR *pc;
675 {
676   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
677   CORE_ADDR sp, jb_addr;
678
679   sp = read_register (SP_REGNUM);
680
681   if (target_read_memory (sp + SP_ARG0,         /* Offset of first arg on stack */
682                           buf,
683                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
684     return 0;
685
686   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
687
688   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
689                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
690     return 0;
691
692   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
693
694   return 1;
695 }
696
697 #endif /* GET_LONGJMP_TARGET */
698
699 void
700 i386_extract_return_value (type, regbuf, valbuf)
701      struct type *type;
702      char regbuf[REGISTER_BYTES];
703      char *valbuf;
704 {
705   /* On AIX, i386 GNU/Linux and DJGPP, floating point values are
706      returned in floating point registers.  */
707   /* FIXME: cagney/2000-02-29: This function needs to be rewritten
708      using multi-arch. Please don't keep adding to this #ifdef
709      spaghetti. */
710 #if defined(I386_AIX_TARGET) || defined(I386_GNULINUX_TARGET) || defined(I386_DJGPP_TARGET)
711   if (TYPE_CODE_FLT == TYPE_CODE (type))
712     {
713       double d;
714       /* 387 %st(0), gcc uses this */
715       floatformat_to_double (&floatformat_i387_ext,
716 #if defined(FPDATA_REGNUM)
717                              &regbuf[REGISTER_BYTE (FPDATA_REGNUM)],
718 #else /* !FPDATA_REGNUM */
719                              &regbuf[REGISTER_BYTE (FP0_REGNUM)],
720 #endif /* FPDATA_REGNUM */
721
722                              &d);
723       store_floating (valbuf, TYPE_LENGTH (type), d);
724     }
725   else
726 #endif /* I386_AIX_TARGET || I386_GNULINUX_TARGET || I386_DJGPP_TARGET */
727     {
728 #if defined(LOW_RETURN_REGNUM)
729       int len = TYPE_LENGTH (type);
730       int low_size = REGISTER_RAW_SIZE (LOW_RETURN_REGNUM);
731       int high_size = REGISTER_RAW_SIZE (HIGH_RETURN_REGNUM);
732
733       if (len <= low_size)
734         memcpy (valbuf, regbuf + REGISTER_BYTE (LOW_RETURN_REGNUM), len);
735       else if (len <= (low_size + high_size))
736         {
737           memcpy (valbuf,
738                   regbuf + REGISTER_BYTE (LOW_RETURN_REGNUM),
739                   low_size);
740           memcpy (valbuf + low_size,
741                   regbuf + REGISTER_BYTE (HIGH_RETURN_REGNUM),
742                   len - low_size);
743         }
744       else
745         error ("GDB bug: i386-tdep.c (i386_extract_return_value): Don't know how to find a return value %d bytes long", len);
746 #else /* !LOW_RETURN_REGNUM */
747       memcpy (valbuf, regbuf, TYPE_LENGTH (type));
748 #endif /* LOW_RETURN_REGNUM */
749     }
750 }
751
752 #ifdef I386V4_SIGTRAMP_SAVED_PC
753 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
754    for all three variants of SVR4 sigtramps.  */
755
756 CORE_ADDR
757 i386v4_sigtramp_saved_pc (frame)
758      struct frame_info *frame;
759 {
760   CORE_ADDR saved_pc_offset = 4;
761   char *name = NULL;
762
763   find_pc_partial_function (frame->pc, &name, NULL, NULL);
764   if (name)
765     {
766       if (STREQ (name, "_sigreturn"))
767         saved_pc_offset = 132 + 14 * 4;
768       else if (STREQ (name, "_sigacthandler"))
769         saved_pc_offset = 80 + 14 * 4;
770       else if (STREQ (name, "sigvechandler"))
771         saved_pc_offset = 120 + 14 * 4;
772     }
773
774   if (frame->next)
775     return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
776   return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
777 }
778 #endif /* I386V4_SIGTRAMP_SAVED_PC */
779
780 #ifdef I386_LINUX_SIGTRAMP
781
782 /* When the i386 Linux kernel calls a signal handler, the return
783    address points to a bit of code on the stack.  This function
784    returns whether the PC appears to be within this bit of code.
785
786    The instruction sequence is
787        pop    %eax
788        mov    $0x77,%eax
789        int    $0x80
790    or 0x58 0xb8 0x77 0x00 0x00 0x00 0xcd 0x80.
791
792    Checking for the code sequence should be somewhat reliable, because
793    the effect is to call the system call sigreturn.  This is unlikely
794    to occur anywhere other than a signal trampoline.
795
796    It kind of sucks that we have to read memory from the process in
797    order to identify a signal trampoline, but there doesn't seem to be
798    any other way.  The IN_SIGTRAMP macro in tm-linux.h arranges to
799    only call us if no function name could be identified, which should
800    be the case since the code is on the stack.  */
801
802 #define LINUX_SIGTRAMP_INSN0 (0x58)     /* pop %eax */
803 #define LINUX_SIGTRAMP_OFFSET0 (0)
804 #define LINUX_SIGTRAMP_INSN1 (0xb8)     /* mov $NNNN,%eax */
805 #define LINUX_SIGTRAMP_OFFSET1 (1)
806 #define LINUX_SIGTRAMP_INSN2 (0xcd)     /* int */
807 #define LINUX_SIGTRAMP_OFFSET2 (6)
808
809 static const unsigned char linux_sigtramp_code[] =
810 {
811   LINUX_SIGTRAMP_INSN0,                                 /* pop %eax */
812   LINUX_SIGTRAMP_INSN1, 0x77, 0x00, 0x00, 0x00,         /* mov $0x77,%eax */
813   LINUX_SIGTRAMP_INSN2, 0x80                            /* int $0x80 */
814 };
815
816 #define LINUX_SIGTRAMP_LEN (sizeof linux_sigtramp_code)
817
818 /* If PC is in a sigtramp routine, return the address of the start of
819    the routine.  Otherwise, return 0.  */
820
821 static CORE_ADDR
822 i386_linux_sigtramp_start (pc)
823      CORE_ADDR pc;
824 {
825   unsigned char buf[LINUX_SIGTRAMP_LEN];
826
827   /* We only recognize a signal trampoline if PC is at the start of
828      one of the three instructions.  We optimize for finding the PC at
829      the start, as will be the case when the trampoline is not the
830      first frame on the stack.  We assume that in the case where the
831      PC is not at the start of the instruction sequence, there will be
832      a few trailing readable bytes on the stack.  */
833
834   if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
835     return 0;
836
837   if (buf[0] != LINUX_SIGTRAMP_INSN0)
838     {
839       int adjust;
840
841       switch (buf[0])
842         {
843         case LINUX_SIGTRAMP_INSN1:
844           adjust = LINUX_SIGTRAMP_OFFSET1;
845           break;
846         case LINUX_SIGTRAMP_INSN2:
847           adjust = LINUX_SIGTRAMP_OFFSET2;
848           break;
849         default:
850           return 0;
851         }
852
853       pc -= adjust;
854
855       if (read_memory_nobpt (pc, (char *) buf, LINUX_SIGTRAMP_LEN) != 0)
856         return 0;
857     }
858
859   if (memcmp (buf, linux_sigtramp_code, LINUX_SIGTRAMP_LEN) != 0)
860     return 0;
861
862   return pc;
863 }
864
865 /* Return whether PC is in a Linux sigtramp routine.  */
866
867 int
868 i386_linux_sigtramp (pc)
869      CORE_ADDR pc;
870 {
871   return i386_linux_sigtramp_start (pc) != 0;
872 }
873
874 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
875    program counter.  The Linux kernel will set up a sigcontext
876    structure immediately before the sigtramp routine on the stack.  */
877
878 CORE_ADDR
879 i386_linux_sigtramp_saved_pc (frame)
880      struct frame_info *frame;
881 {
882   CORE_ADDR pc;
883
884   pc = i386_linux_sigtramp_start (frame->pc);
885   if (pc == 0)
886     error ("i386_linux_sigtramp_saved_pc called when no sigtramp");
887   return read_memory_integer ((pc
888                                - LINUX_SIGCONTEXT_SIZE
889                                + LINUX_SIGCONTEXT_PC_OFFSET),
890                               4);
891 }
892
893 /* Assuming FRAME is for a Linux sigtramp routine, return the saved
894    stack pointer.  The Linux kernel will set up a sigcontext structure
895    immediately before the sigtramp routine on the stack.  */
896
897 CORE_ADDR
898 i386_linux_sigtramp_saved_sp (frame)
899      struct frame_info *frame;
900 {
901   CORE_ADDR pc;
902
903   pc = i386_linux_sigtramp_start (frame->pc);
904   if (pc == 0)
905     error ("i386_linux_sigtramp_saved_sp called when no sigtramp");
906   return read_memory_integer ((pc
907                                - LINUX_SIGCONTEXT_SIZE
908                                + LINUX_SIGCONTEXT_SP_OFFSET),
909                               4);
910 }
911
912 #endif /* I386_LINUX_SIGTRAMP */
913
914 #ifdef STATIC_TRANSFORM_NAME
915 /* SunPRO encodes the static variables.  This is not related to C++ mangling,
916    it is done for C too.  */
917
918 char *
919 sunpro_static_transform_name (name)
920      char *name;
921 {
922   char *p;
923   if (IS_STATIC_TRANSFORM_NAME (name))
924     {
925       /* For file-local statics there will be a period, a bunch
926          of junk (the contents of which match a string given in the
927          N_OPT), a period and the name.  For function-local statics
928          there will be a bunch of junk (which seems to change the
929          second character from 'A' to 'B'), a period, the name of the
930          function, and the name.  So just skip everything before the
931          last period.  */
932       p = strrchr (name, '.');
933       if (p != NULL)
934         name = p + 1;
935     }
936   return name;
937 }
938 #endif /* STATIC_TRANSFORM_NAME */
939
940
941
942 /* Stuff for WIN32 PE style DLL's but is pretty generic really. */
943
944 CORE_ADDR
945 skip_trampoline_code (pc, name)
946      CORE_ADDR pc;
947      char *name;
948 {
949   if (pc && read_memory_unsigned_integer (pc, 2) == 0x25ff)     /* jmp *(dest) */
950     {
951       unsigned long indirect = read_memory_unsigned_integer (pc + 2, 4);
952       struct minimal_symbol *indsym =
953       indirect ? lookup_minimal_symbol_by_pc (indirect) : 0;
954       char *symname = indsym ? SYMBOL_NAME (indsym) : 0;
955
956       if (symname)
957         {
958           if (strncmp (symname, "__imp_", 6) == 0
959               || strncmp (symname, "_imp_", 5) == 0)
960             return name ? 1 : read_memory_unsigned_integer (indirect, 4);
961         }
962     }
963   return 0;                     /* not a trampoline */
964 }
965
966 static int
967 gdb_print_insn_i386 (memaddr, info)
968      bfd_vma memaddr;
969      disassemble_info *info;
970 {
971   if (disassembly_flavor == att_flavor)
972     return print_insn_i386_att (memaddr, info);
973   else if (disassembly_flavor == intel_flavor)
974     return print_insn_i386_intel (memaddr, info);
975   /* Never reached - disassembly_flavour is always either att_flavor
976      or intel_flavor */
977   abort ();
978 }
979
980 /* If the disassembly mode is intel, we have to also switch the
981    bfd mach_type.  This function is run in the set disassembly_flavor
982    command, and does that.  */
983
984 static void
985 set_disassembly_flavor_sfunc (args, from_tty, c)
986      char *args;
987      int from_tty;
988      struct cmd_list_element *c;
989 {
990   set_disassembly_flavor ();
991 }
992
993 static void
994 set_disassembly_flavor ()
995 {
996   if (disassembly_flavor == att_flavor)
997     set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386);
998   else if (disassembly_flavor == intel_flavor)
999     set_architecture_from_arch_mach (bfd_arch_i386, bfd_mach_i386_i386_intel_syntax);
1000 }
1001
1002
1003 void
1004 _initialize_i386_tdep ()
1005 {
1006   /* Initialize the table saying where each register starts in the
1007      register file.  */
1008   {
1009     int i, offset;
1010
1011     offset = 0;
1012     for (i = 0; i < MAX_NUM_REGS; i++)
1013       {
1014         i386_register_byte[i] = offset;
1015         offset += i386_register_raw_size[i];
1016       }
1017   }
1018
1019   /* Initialize the table of virtual register sizes.  */
1020   {
1021     int i;
1022
1023     for (i = 0; i < MAX_NUM_REGS; i++)
1024       i386_register_virtual_size[i] = TYPE_LENGTH (REGISTER_VIRTUAL_TYPE (i));
1025   }
1026
1027   tm_print_insn = gdb_print_insn_i386;
1028   tm_print_insn_info.mach = bfd_lookup_arch (bfd_arch_i386, 0)->mach;
1029
1030   /* Add the variable that controls the disassembly flavor */
1031   {
1032     struct cmd_list_element *new_cmd;
1033
1034     new_cmd = add_set_enum_cmd ("disassembly-flavor", no_class,
1035                                 valid_flavors,
1036                                 (char *) &disassembly_flavor,
1037                                 "Set the disassembly flavor, the valid values are \"att\" and \"intel\", \
1038 and the default value is \"att\".",
1039                                 &setlist);
1040     new_cmd->function.sfunc = set_disassembly_flavor_sfunc;
1041     add_show_from_set (new_cmd, &showlist);
1042   }
1043
1044   /* Finally, initialize the disassembly flavor to the default given
1045      in the disassembly_flavor variable */
1046
1047   set_disassembly_flavor ();
1048 }