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