]> Git Repo - binutils.git/blob - gdb/i386-tdep.c
ansi name abuse changes
[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 GDB 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 1, or (at your option)
9 any later version.
10
11 GDB 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 GDB; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 #include <stdio.h>
21 #include "defs.h"
22 #include "param.h"
23 #include "frame.h"
24 #include "inferior.h"
25 #include "gdbcore.h"
26
27 #ifdef USG
28 #include <sys/types.h>
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/dir.h>
33 #include <signal.h>
34 #include <sys/user.h>
35 #include <sys/ioctl.h>
36 #include <fcntl.h>
37
38 #ifndef N_SET_MAGIC
39 #ifdef COFF_FORMAT
40 #define N_SET_MAGIC(exec, val) ((exec).magic = (val))
41 #else
42 #define N_SET_MAGIC(exec, val) ((exec).a_magic = (val))
43 #endif
44 #endif
45
46 #include <sys/file.h>
47 #include <sys/stat.h>
48
49 /* helper functions for tm-i386.h */
50
51 /* stdio style buffering to minimize calls to ptrace */
52 static CORE_ADDR codestream_next_addr;
53 static CORE_ADDR codestream_addr;
54 static unsigned char codestream_buf[sizeof (int)];
55 static int codestream_off;
56 static int codestream_cnt;
57
58 #define codestream_tell() (codestream_addr + codestream_off)
59 #define codestream_peek() (codestream_cnt == 0 ? \
60                            codestream_fill(1): codestream_buf[codestream_off])
61 #define codestream_get() (codestream_cnt-- == 0 ? \
62                          codestream_fill(0) : codestream_buf[codestream_off++])
63
64 static unsigned char 
65 codestream_fill (peek_flag)
66 {
67   codestream_addr = codestream_next_addr;
68   codestream_next_addr += sizeof (int);
69   codestream_off = 0;
70   codestream_cnt = sizeof (int);
71   read_memory (codestream_addr,
72                (unsigned char *)codestream_buf,
73                sizeof (int));
74   
75   if (peek_flag)
76     return (codestream_peek());
77   else
78     return (codestream_get());
79 }
80
81 static void
82 codestream_seek (place)
83 {
84   codestream_next_addr = place & -sizeof (int);
85   codestream_cnt = 0;
86   codestream_fill (1);
87   while (codestream_tell() != place)
88     codestream_get ();
89 }
90
91 static void
92 codestream_read (buf, count)
93      unsigned char *buf;
94 {
95   unsigned char *p;
96   int i;
97   p = buf;
98   for (i = 0; i < count; i++)
99     *p++ = codestream_get ();
100 }
101
102 /* next instruction is a jump, move to target */
103 static
104 i386_follow_jump ()
105 {
106   int long_delta;
107   short short_delta;
108   char byte_delta;
109   int data16;
110   int pos;
111   
112   pos = codestream_tell ();
113   
114   data16 = 0;
115   if (codestream_peek () == 0x66)
116     {
117       codestream_get ();
118       data16 = 1;
119     }
120   
121   switch (codestream_get ())
122     {
123     case 0xe9:
124       /* relative jump: if data16 == 0, disp32, else disp16 */
125       if (data16)
126         {
127           codestream_read ((unsigned char *)&short_delta, 2);
128
129           /* include size of jmp inst (including the 0x66 prefix).  */
130           pos += short_delta + 4; 
131         }
132       else
133         {
134           codestream_read ((unsigned char *)&long_delta, 4);
135           pos += long_delta + 5;
136         }
137       break;
138     case 0xeb:
139       /* relative jump, disp8 (ignore data16) */
140       codestream_read ((unsigned char *)&byte_delta, 1);
141       pos += byte_delta + 2;
142       break;
143     }
144   codestream_seek (pos);
145 }
146
147 /*
148  * find & return amound a local space allocated, and advance codestream to
149  * first register push (if any)
150  *
151  * if entry sequence doesn't make sense, return -1, and leave 
152  * codestream pointer random
153  */
154 static long
155 i386_get_frame_setup (pc)
156 {
157   unsigned char op;
158   
159   codestream_seek (pc);
160   
161   i386_follow_jump ();
162   
163   op = codestream_get ();
164   
165   if (op == 0x58)               /* popl %eax */
166     {
167       /*
168        * this function must start with
169        * 
170        *    popl %eax             0x58
171        *    xchgl %eax, (%esp)  0x87 0x04 0x24
172        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
173        *
174        * (the system 5 compiler puts out the second xchg
175        * inst, and the assembler doesn't try to optimize it,
176        * so the 'sib' form gets generated)
177        * 
178        * this sequence is used to get the address of the return
179        * buffer for a function that returns a structure
180        */
181       int pos;
182       unsigned char buf[4];
183       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
184       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
185       pos = codestream_tell ();
186       codestream_read (buf, 4);
187       if (bcmp (buf, proto1, 3) == 0)
188         pos += 3;
189       else if (bcmp (buf, proto2, 4) == 0)
190         pos += 4;
191       
192       codestream_seek (pos);
193       op = codestream_get (); /* update next opcode */
194     }
195   
196   if (op == 0x55)               /* pushl %ebp */
197     {                   
198       /* check for movl %esp, %ebp - can be written two ways */
199       switch (codestream_get ())
200         {
201         case 0x8b:
202           if (codestream_get () != 0xec)
203             return (-1);
204           break;
205         case 0x89:
206           if (codestream_get () != 0xe5)
207             return (-1);
208           break;
209         default:
210           return (-1);
211         }
212       /* check for stack adjustment 
213        *
214        *  subl $XXX, %esp
215        *
216        * note: you can't subtract a 16 bit immediate
217        * from a 32 bit reg, so we don't have to worry
218        * about a data16 prefix 
219        */
220       op = codestream_peek ();
221       if (op == 0x83)
222         {
223           /* subl with 8 bit immed */
224           codestream_get ();
225           if (codestream_get () != 0xec)
226             /* Some instruction starting with 0x83 other than subl.  */
227             {
228               codestream_seek (codestream_tell () - 2);
229               return 0;
230             }
231           /* subl with signed byte immediate 
232            * (though it wouldn't make sense to be negative)
233            */
234           return (codestream_get());
235         }
236       else if (op == 0x81)
237         {
238           /* subl with 32 bit immed */
239           int locals;
240           codestream_get();
241           if (codestream_get () != 0xec)
242             /* Some instruction starting with 0x81 other than subl.  */
243             {
244               codestream_seek (codestream_tell () - 2);
245               return 0;
246             }
247           /* subl with 32 bit immediate */
248           codestream_read ((unsigned char *)&locals, 4);
249           SWAP_TARGET_AND_HOST (&locals, 4);
250           return (locals);
251         }
252       else
253         {
254           return (0);
255         }
256     }
257   else if (op == 0xc8)
258     {
259       /* enter instruction: arg is 16 bit unsigned immed */
260       unsigned short slocals;
261       codestream_read ((unsigned char *)&slocals, 2);
262       SWAP_TARGET_AND_HOST (&slocals, 2);
263       codestream_get (); /* flush final byte of enter instruction */
264       return (slocals);
265     }
266   return (-1);
267 }
268
269 /* Return number of args passed to a frame.
270    Can return -1, meaning no way to tell.  */
271
272 /* on the 386, the instruction following the call could be:
273  *  popl %ecx        -  one arg
274  *  addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
275  *  anything else    -  zero args
276  */
277
278 int
279 i386_frame_num_args (fi)
280      struct frame_info fi;
281 {
282   int retpc;                                            
283   unsigned char op;                                     
284   struct frame_info *pfi;
285
286   int frameless;
287
288   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
289   if (frameless)
290     /* In the absence of a frame pointer, GDB doesn't get correct values
291        for nameless arguments.  Return -1, so it doesn't print any
292        nameless arguments.  */
293     return -1;
294
295   pfi = get_prev_frame_info ((fi));                     
296   if (pfi == 0)
297     {
298       /* Note:  this can happen if we are looking at the frame for
299          main, because FRAME_CHAIN_VALID won't let us go into
300          start.  If we have debugging symbols, that's not really
301          a big deal; it just means it will only show as many arguments
302          to main as are declared.  */
303       return -1;
304     }
305   else
306     {
307       retpc = pfi->pc;                                  
308       op = read_memory_integer (retpc, 1);                      
309       if (op == 0x59)                                   
310         /* pop %ecx */                         
311         return 1;                               
312       else if (op == 0x83)
313         {
314           op = read_memory_integer (retpc+1, 1);        
315           if (op == 0xc4)                               
316             /* addl $<signed imm 8 bits>, %esp */       
317             return (read_memory_integer (retpc+2,1)&0xff)/4;
318           else
319             return 0;
320         }
321       else if (op == 0x81)
322         { /* add with 32 bit immediate */
323           op = read_memory_integer (retpc+1, 1);        
324           if (op == 0xc4)                               
325             /* addl $<imm 32>, %esp */          
326             return read_memory_integer (retpc+2, 4) / 4;
327           else
328             return 0;
329         }
330       else
331         {
332           return 0;
333         }
334     }
335 }
336
337 /*
338  * parse the first few instructions of the function to see
339  * what registers were stored.
340  *
341  * We handle these cases:
342  *
343  * The startup sequence can be at the start of the function,
344  * or the function can start with a branch to startup code at the end.
345  *
346  * %ebp can be set up with either the 'enter' instruction, or 
347  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
348  * but was once used in the sys5 compiler)
349  *
350  * Local space is allocated just below the saved %ebp by either the
351  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
352  * a 16 bit unsigned argument for space to allocate, and the
353  * 'addl' instruction could have either a signed byte, or
354  * 32 bit immediate.
355  *
356  * Next, the registers used by this function are pushed.  In
357  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
358  * (and sometimes a harmless bug causes it to also save but not restore %eax);
359  * however, the code below is willing to see the pushes in any order,
360  * and will handle up to 8 of them.
361  *
362  * If the setup sequence is at the end of the function, then the
363  * next instruction will be a branch back to the start.
364  */
365
366 i386_frame_find_saved_regs (fip, fsrp)
367      struct frame_info *fip;
368      struct frame_saved_regs *fsrp;
369 {
370   long locals;
371   unsigned char *p;
372   unsigned char op;
373   CORE_ADDR dummy_bottom;
374   CORE_ADDR adr;
375   int i;
376   
377   bzero (fsrp, sizeof *fsrp);
378   
379   /* if frame is the end of a dummy, compute where the
380    * beginning would be
381    */
382   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
383   
384   /* check if the PC is in the stack, in a dummy frame */
385   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
386     {
387       /* all regs were saved by push_call_dummy () */
388       adr = fip->frame;
389       for (i = 0; i < NUM_REGS; i++) 
390         {
391           adr -= REGISTER_RAW_SIZE (i);
392           fsrp->regs[i] = adr;
393         }
394       return;
395     }
396   
397   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
398   
399   if (locals >= 0) 
400     {
401       adr = fip->frame - 4 - locals;
402       for (i = 0; i < 8; i++) 
403         {
404           op = codestream_get ();
405           if (op < 0x50 || op > 0x57)
406             break;
407           fsrp->regs[op - 0x50] = adr;
408           adr -= 4;
409         }
410     }
411   
412   fsrp->regs[PC_REGNUM] = fip->frame + 4;
413   fsrp->regs[FP_REGNUM] = fip->frame;
414 }
415
416 /* return pc of first real instruction */
417 i386_skip_prologue (pc)
418 {
419   unsigned char op;
420   int i;
421   
422   if (i386_get_frame_setup (pc) < 0)
423     return (pc);
424   
425   /* found valid frame setup - codestream now points to 
426    * start of push instructions for saving registers
427    */
428   
429   /* skip over register saves */
430   for (i = 0; i < 8; i++)
431     {
432       op = codestream_peek ();
433       /* break if not pushl inst */
434       if (op < 0x50 || op > 0x57) 
435         break;
436       codestream_get ();
437     }
438   
439   i386_follow_jump ();
440   
441   return (codestream_tell ());
442 }
443
444 i386_push_dummy_frame ()
445 {
446   CORE_ADDR sp = read_register (SP_REGNUM);
447   int regnum;
448   char regbuf[MAX_REGISTER_RAW_SIZE];
449   
450   sp = push_word (sp, read_register (PC_REGNUM));
451   sp = push_word (sp, read_register (FP_REGNUM));
452   write_register (FP_REGNUM, sp);
453   for (regnum = 0; regnum < NUM_REGS; regnum++)
454     {
455       read_register_gen (regnum, regbuf);
456       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
457     }
458   write_register (SP_REGNUM, sp);
459 }
460
461 i386_pop_frame ()
462 {
463   FRAME frame = get_current_frame ();
464   CORE_ADDR fp;
465   int regnum;
466   struct frame_saved_regs fsr;
467   struct frame_info *fi;
468   char regbuf[MAX_REGISTER_RAW_SIZE];
469   
470   fi = get_frame_info (frame);
471   fp = fi->frame;
472   get_frame_saved_regs (fi, &fsr);
473   for (regnum = 0; regnum < NUM_REGS; regnum++) 
474     {
475       CORE_ADDR adr;
476       adr = fsr.regs[regnum];
477       if (adr)
478         {
479           read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
480           write_register_bytes (REGISTER_BYTE (regnum), regbuf,
481                                 REGISTER_RAW_SIZE (regnum));
482         }
483     }
484   write_register (FP_REGNUM, read_memory_integer (fp, 4));
485   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
486   write_register (SP_REGNUM, fp + 8);
487   flush_cached_frames ();
488   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
489                                         read_pc ()));
490 }
This page took 0.050954 seconds and 4 git commands to generate.