* config/i386/tm-i386v4.h (I386V4_SIGTRAMP_SAVED_PC, IN_SIGTRAMP,
[external/binutils.git] / gdb / i386-tdep.c
1 /* Intel 386 target-dependent stuff.
2    Copyright (C) 1988, 1989, 1991, 1994 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "gdbcore.h"
24 #include "target.h"
25 #include "symtab.h"
26
27 static long
28 i386_get_frame_setup PARAMS ((int));
29
30 static void
31 i386_follow_jump PARAMS ((void));
32
33 static void
34 codestream_read PARAMS ((unsigned char *, int));
35
36 static void
37 codestream_seek PARAMS ((int));
38
39 static unsigned char 
40 codestream_fill PARAMS ((int));
41
42 /* helper functions for tm-i386.h */
43
44 /* Stdio style buffering was used to minimize calls to ptrace, but this
45    buffering did not take into account that the code section being accessed
46    may not be an even number of buffers long (even if the buffer is only
47    sizeof(int) long).  In cases where the code section size happened to
48    be a non-integral number of buffers long, attempting to read the last
49    buffer would fail.  Simply using target_read_memory and ignoring errors,
50    rather than read_memory, is not the correct solution, since legitimate
51    access errors would then be totally ignored.  To properly handle this
52    situation and continue to use buffering would require that this code
53    be able to determine the minimum code section size granularity (not the
54    alignment of the section itself, since the actual failing case that
55    pointed out this problem had a section alignment of 4 but was not a
56    multiple of 4 bytes long), on a target by target basis, and then
57    adjust it's buffer size accordingly.  This is messy, but potentially
58    feasible.  It probably needs the bfd library's help and support.  For
59    now, the buffer size is set to 1.  (FIXME -fnf) */
60
61 #define CODESTREAM_BUFSIZ 1     /* Was sizeof(int), see note above. */
62 static CORE_ADDR codestream_next_addr;
63 static CORE_ADDR codestream_addr;
64 static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
65 static int codestream_off;
66 static int codestream_cnt;
67
68 #define codestream_tell() (codestream_addr + codestream_off)
69 #define codestream_peek() (codestream_cnt == 0 ? \
70                            codestream_fill(1): codestream_buf[codestream_off])
71 #define codestream_get() (codestream_cnt-- == 0 ? \
72                          codestream_fill(0) : codestream_buf[codestream_off++])
73
74 static unsigned char 
75 codestream_fill (peek_flag)
76     int peek_flag;
77 {
78   codestream_addr = codestream_next_addr;
79   codestream_next_addr += CODESTREAM_BUFSIZ;
80   codestream_off = 0;
81   codestream_cnt = CODESTREAM_BUFSIZ;
82   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
83   
84   if (peek_flag)
85     return (codestream_peek());
86   else
87     return (codestream_get());
88 }
89
90 static void
91 codestream_seek (place)
92     int place;
93 {
94   codestream_next_addr = place / CODESTREAM_BUFSIZ;
95   codestream_next_addr *= CODESTREAM_BUFSIZ;
96   codestream_cnt = 0;
97   codestream_fill (1);
98   while (codestream_tell() != place)
99     codestream_get ();
100 }
101
102 static void
103 codestream_read (buf, count)
104      unsigned char *buf;
105      int count;
106 {
107   unsigned char *p;
108   int i;
109   p = buf;
110   for (i = 0; i < count; i++)
111     *p++ = codestream_get ();
112 }
113
114 /* next instruction is a jump, move to target */
115
116 static void
117 i386_follow_jump ()
118 {
119   unsigned char buf[4];
120   long delta;
121
122   int data16;
123   CORE_ADDR pos;
124
125   pos = codestream_tell ();
126
127   data16 = 0;
128   if (codestream_peek () == 0x66)
129     {
130       codestream_get ();
131       data16 = 1;
132     }
133
134   switch (codestream_get ())
135     {
136     case 0xe9:
137       /* relative jump: if data16 == 0, disp32, else disp16 */
138       if (data16)
139         {
140           codestream_read (buf, 2);
141           delta = extract_signed_integer (buf, 2);
142
143           /* include size of jmp inst (including the 0x66 prefix).  */
144           pos += delta + 4; 
145         }
146       else
147         {
148           codestream_read (buf, 4);
149           delta = extract_signed_integer (buf, 4);
150
151           pos += delta + 5;
152         }
153       break;
154     case 0xeb:
155       /* relative jump, disp8 (ignore data16) */
156       codestream_read (buf, 1);
157       /* Sign-extend it.  */
158       delta = extract_signed_integer (buf, 1);
159
160       pos += delta + 2;
161       break;
162     }
163   codestream_seek (pos);
164 }
165
166 /*
167  * find & return amound a local space allocated, and advance codestream to
168  * first register push (if any)
169  *
170  * if entry sequence doesn't make sense, return -1, and leave 
171  * codestream pointer random
172  */
173
174 static long
175 i386_get_frame_setup (pc)
176      int pc;
177 {
178   unsigned char op;
179
180   codestream_seek (pc);
181
182   i386_follow_jump ();
183
184   op = codestream_get ();
185
186   if (op == 0x58)               /* popl %eax */
187     {
188       /*
189        * this function must start with
190        * 
191        *    popl %eax             0x58
192        *    xchgl %eax, (%esp)  0x87 0x04 0x24
193        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
194        *
195        * (the system 5 compiler puts out the second xchg
196        * inst, and the assembler doesn't try to optimize it,
197        * so the 'sib' form gets generated)
198        * 
199        * this sequence is used to get the address of the return
200        * buffer for a function that returns a structure
201        */
202       int pos;
203       unsigned char buf[4];
204       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
205       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
206       pos = codestream_tell ();
207       codestream_read (buf, 4);
208       if (memcmp (buf, proto1, 3) == 0)
209         pos += 3;
210       else if (memcmp (buf, proto2, 4) == 0)
211         pos += 4;
212
213       codestream_seek (pos);
214       op = codestream_get (); /* update next opcode */
215     }
216
217   if (op == 0x55)               /* pushl %ebp */
218     {                   
219       /* check for movl %esp, %ebp - can be written two ways */
220       switch (codestream_get ())
221         {
222         case 0x8b:
223           if (codestream_get () != 0xec)
224             return (-1);
225           break;
226         case 0x89:
227           if (codestream_get () != 0xe5)
228             return (-1);
229           break;
230         default:
231           return (-1);
232         }
233       /* check for stack adjustment 
234        *
235        *  subl $XXX, %esp
236        *
237        * note: you can't subtract a 16 bit immediate
238        * from a 32 bit reg, so we don't have to worry
239        * about a data16 prefix 
240        */
241       op = codestream_peek ();
242       if (op == 0x83)
243         {
244           /* subl with 8 bit immed */
245           codestream_get ();
246           if (codestream_get () != 0xec)
247             /* Some instruction starting with 0x83 other than subl.  */
248             {
249               codestream_seek (codestream_tell () - 2);
250               return 0;
251             }
252           /* subl with signed byte immediate 
253            * (though it wouldn't make sense to be negative)
254            */
255           return (codestream_get());
256         }
257       else if (op == 0x81)
258         {
259           char buf[4];
260           /* Maybe it is subl with 32 bit immedediate.  */
261           codestream_get();
262           if (codestream_get () != 0xec)
263             /* Some instruction starting with 0x81 other than subl.  */
264             {
265               codestream_seek (codestream_tell () - 2);
266               return 0;
267             }
268           /* It is subl with 32 bit immediate.  */
269           codestream_read ((unsigned char *)buf, 4);
270           return extract_signed_integer (buf, 4);
271         }
272       else
273         {
274           return (0);
275         }
276     }
277   else if (op == 0xc8)
278     {
279       char buf[2];
280       /* enter instruction: arg is 16 bit unsigned immed */
281       codestream_read ((unsigned char *)buf, 2);
282       codestream_get (); /* flush final byte of enter instruction */
283       return extract_unsigned_integer (buf, 2);
284     }
285   return (-1);
286 }
287
288 /* Return number of args passed to a frame.
289    Can return -1, meaning no way to tell.  */
290
291 int
292 i386_frame_num_args (fi)
293      struct frame_info *fi;
294 {
295 #if 1
296   return -1;
297 #else
298   /* This loses because not only might the compiler not be popping the
299      args right after the function call, it might be popping args from both
300      this call and a previous one, and we would say there are more args
301      than there really are.  */
302
303   int retpc;                                            
304   unsigned char op;                                     
305   struct frame_info *pfi;
306
307   /* on the 386, the instruction following the call could be:
308      popl %ecx        -  one arg
309      addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
310      anything else    -  zero args  */
311
312   int frameless;
313
314   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
315   if (frameless)
316     /* In the absence of a frame pointer, GDB doesn't get correct values
317        for nameless arguments.  Return -1, so it doesn't print any
318        nameless arguments.  */
319     return -1;
320
321   pfi = get_prev_frame_info (fi);                       
322   if (pfi == 0)
323     {
324       /* Note:  this can happen if we are looking at the frame for
325          main, because FRAME_CHAIN_VALID won't let us go into
326          start.  If we have debugging symbols, that's not really
327          a big deal; it just means it will only show as many arguments
328          to main as are declared.  */
329       return -1;
330     }
331   else
332     {
333       retpc = pfi->pc;                                  
334       op = read_memory_integer (retpc, 1);                      
335       if (op == 0x59)                                   
336         /* pop %ecx */                         
337         return 1;                               
338       else if (op == 0x83)
339         {
340           op = read_memory_integer (retpc+1, 1);        
341           if (op == 0xc4)                               
342             /* addl $<signed imm 8 bits>, %esp */       
343             return (read_memory_integer (retpc+2,1)&0xff)/4;
344           else
345             return 0;
346         }
347       else if (op == 0x81)
348         { /* add with 32 bit immediate */
349           op = read_memory_integer (retpc+1, 1);        
350           if (op == 0xc4)                               
351             /* addl $<imm 32>, %esp */          
352             return read_memory_integer (retpc+2, 4) / 4;
353           else
354             return 0;
355         }
356       else
357         {
358           return 0;
359         }
360     }
361 #endif
362 }
363
364 /*
365  * parse the first few instructions of the function to see
366  * what registers were stored.
367  *
368  * We handle these cases:
369  *
370  * The startup sequence can be at the start of the function,
371  * or the function can start with a branch to startup code at the end.
372  *
373  * %ebp can be set up with either the 'enter' instruction, or 
374  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
375  * but was once used in the sys5 compiler)
376  *
377  * Local space is allocated just below the saved %ebp by either the
378  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
379  * a 16 bit unsigned argument for space to allocate, and the
380  * 'addl' instruction could have either a signed byte, or
381  * 32 bit immediate.
382  *
383  * Next, the registers used by this function are pushed.  In
384  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
385  * (and sometimes a harmless bug causes it to also save but not restore %eax);
386  * however, the code below is willing to see the pushes in any order,
387  * and will handle up to 8 of them.
388  *
389  * If the setup sequence is at the end of the function, then the
390  * next instruction will be a branch back to the start.
391  */
392
393 void
394 i386_frame_find_saved_regs (fip, fsrp)
395      struct frame_info *fip;
396      struct frame_saved_regs *fsrp;
397 {
398   long locals;
399   unsigned char op;
400   CORE_ADDR dummy_bottom;
401   CORE_ADDR adr;
402   int i;
403   
404   memset (fsrp, 0, sizeof *fsrp);
405   
406   /* if frame is the end of a dummy, compute where the
407    * beginning would be
408    */
409   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
410   
411   /* check if the PC is in the stack, in a dummy frame */
412   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
413     {
414       /* all regs were saved by push_call_dummy () */
415       adr = fip->frame;
416       for (i = 0; i < NUM_REGS; i++) 
417         {
418           adr -= REGISTER_RAW_SIZE (i);
419           fsrp->regs[i] = adr;
420         }
421       return;
422     }
423   
424   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
425   
426   if (locals >= 0) 
427     {
428       adr = fip->frame - 4 - locals;
429       for (i = 0; i < 8; i++) 
430         {
431           op = codestream_get ();
432           if (op < 0x50 || op > 0x57)
433             break;
434           fsrp->regs[op - 0x50] = adr;
435           adr -= 4;
436         }
437     }
438   
439   fsrp->regs[PC_REGNUM] = fip->frame + 4;
440   fsrp->regs[FP_REGNUM] = fip->frame;
441 }
442
443 /* return pc of first real instruction */
444
445 int
446 i386_skip_prologue (pc)
447      int pc;
448 {
449   unsigned char op;
450   int i;
451   static unsigned char pic_pat[6] = { 0xe8, 0, 0, 0, 0, /* call   0x0 */
452                                       0x5b,             /* popl   %ebx */
453                                     };
454   CORE_ADDR pos;
455   
456   if (i386_get_frame_setup (pc) < 0)
457     return (pc);
458   
459   /* found valid frame setup - codestream now points to 
460    * start of push instructions for saving registers
461    */
462   
463   /* skip over register saves */
464   for (i = 0; i < 8; i++)
465     {
466       op = codestream_peek ();
467       /* break if not pushl inst */
468       if (op < 0x50 || op > 0x57) 
469         break;
470       codestream_get ();
471     }
472
473   /* The native cc on SVR4 in -K PIC mode inserts the following code to get
474      the address of the global offset table (GOT) into register %ebx.
475       call      0x0
476       popl      %ebx
477       movl      %ebx,x(%ebp)    (optional)
478       addl      y,%ebx
479      This code is with the rest of the prologue (at the end of the
480      function), so we have to skip it to get to the first real
481      instruction at the start of the function.  */
482      
483   pos = codestream_tell ();
484   for (i = 0; i < 6; i++)
485     {
486       op = codestream_get ();
487       if (pic_pat [i] != op)
488         break;
489     }
490   if (i == 6)
491     {
492       unsigned char buf[4];
493       long delta = 6;
494
495       op = codestream_get ();
496       if (op == 0x89)                   /* movl %ebx, x(%ebp) */
497         {
498           op = codestream_get ();
499           if (op == 0x5d)               /* one byte offset from %ebp */
500             {
501               delta += 3;
502               codestream_read (buf, 1);
503             }
504           else if (op == 0x9d)          /* four byte offset from %ebp */
505             {
506               delta += 6;
507               codestream_read (buf, 4);
508             }
509           else                          /* unexpected instruction */
510               delta = -1;
511           op = codestream_get ();
512         }
513                                         /* addl y,%ebx */
514       if (delta > 0 && op == 0x81 && codestream_get () == 0xc3) 
515         {
516             pos += delta + 6;
517         }
518     }
519   codestream_seek (pos);
520   
521   i386_follow_jump ();
522   
523   return (codestream_tell ());
524 }
525
526 void
527 i386_push_dummy_frame ()
528 {
529   CORE_ADDR sp = read_register (SP_REGNUM);
530   int regnum;
531   char regbuf[MAX_REGISTER_RAW_SIZE];
532   
533   sp = push_word (sp, read_register (PC_REGNUM));
534   sp = push_word (sp, read_register (FP_REGNUM));
535   write_register (FP_REGNUM, sp);
536   for (regnum = 0; regnum < NUM_REGS; regnum++)
537     {
538       read_register_gen (regnum, regbuf);
539       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
540     }
541   write_register (SP_REGNUM, sp);
542 }
543
544 void
545 i386_pop_frame ()
546 {
547   FRAME frame = get_current_frame ();
548   CORE_ADDR fp;
549   int regnum;
550   struct frame_saved_regs fsr;
551   struct frame_info *fi;
552   char regbuf[MAX_REGISTER_RAW_SIZE];
553   
554   fi = get_frame_info (frame);
555   fp = fi->frame;
556   get_frame_saved_regs (fi, &fsr);
557   for (regnum = 0; regnum < NUM_REGS; regnum++) 
558     {
559       CORE_ADDR adr;
560       adr = fsr.regs[regnum];
561       if (adr)
562         {
563           read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
564           write_register_bytes (REGISTER_BYTE (regnum), regbuf,
565                                 REGISTER_RAW_SIZE (regnum));
566         }
567     }
568   write_register (FP_REGNUM, read_memory_integer (fp, 4));
569   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
570   write_register (SP_REGNUM, fp + 8);
571   flush_cached_frames ();
572   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
573                                         read_pc ()));
574 }
575
576 #ifdef GET_LONGJMP_TARGET
577
578 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
579    We expect the first arg to be a pointer to the jmp_buf structure from which
580    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
581    This routine returns true on success. */
582
583 int
584 get_longjmp_target(pc)
585      CORE_ADDR *pc;
586 {
587   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
588   CORE_ADDR sp, jb_addr;
589
590   sp = read_register (SP_REGNUM);
591
592   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
593                           buf,
594                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
595     return 0;
596
597   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
598
599   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
600                           TARGET_PTR_BIT / TARGET_CHAR_BIT))
601     return 0;
602
603   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
604
605   return 1;
606 }
607
608 #endif /* GET_LONGJMP_TARGET */
609
610 #ifdef I386_AIX_TARGET
611 /* On AIX, floating point values are returned in floating point registers.  */
612
613 void
614 i386_extract_return_value(type, regbuf, valbuf)
615      struct type *type;
616      char regbuf[REGISTER_BYTES];
617      char *valbuf;
618 {
619   if (TYPE_CODE_FLT == TYPE_CODE(type))
620     {
621       extern struct ext_format ext_format_i387;
622       double d;
623       /* 387 %st(0), gcc uses this */
624       ieee_extended_to_double (&ext_format_i387,
625                                &regbuf[REGISTER_BYTE(FP0_REGNUM)],
626                                &d);
627       store_floating (valbuf, TYPE_LENGTH (type), d);
628     }
629   else
630     { 
631       memcpy (valbuf, regbuf, TYPE_LENGTH (type)); 
632     }
633 }
634 #endif /* I386_AIX_TARGET */
635
636 #ifdef I386V4_SIGTRAMP_SAVED_PC
637 /* Get saved user PC for sigtramp from the pushed ucontext on the stack
638    for all three variants of SVR4 sigtramps.  */
639
640 CORE_ADDR
641 i386v4_sigtramp_saved_pc (frame)
642      FRAME frame;
643 {
644   CORE_ADDR saved_pc_offset = 4;
645   char *name = NULL;
646
647   find_pc_partial_function (frame->pc, &name,
648                             (CORE_ADDR *)NULL,(CORE_ADDR *)NULL);
649   if (name)
650     {
651       if (STREQ (name, "_sigreturn"))
652         saved_pc_offset = 132 + 14 * 4;
653       if (STREQ (name, "_sigacthandler"))
654         saved_pc_offset = 80 + 14 * 4;
655       if (STREQ (name, "sigvechandler"))
656         saved_pc_offset = 120 + 14 * 4;
657     }
658
659   if (frame->next)
660     return read_memory_integer (frame->next->frame + saved_pc_offset, 4);
661   return read_memory_integer (read_register (SP_REGNUM) + saved_pc_offset, 4);
662 }
663 #endif /* I386V4_SIGTRAMP_SAVED_PC */