home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / i386-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  14.3 KB  |  552 lines

  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. static long
  27. i386_get_frame_setup PARAMS ((int));
  28.  
  29. static void
  30. i386_follow_jump PARAMS ((void));
  31.  
  32. static void
  33. codestream_read PARAMS ((unsigned char *, int));
  34.  
  35. static void
  36. codestream_seek PARAMS ((int));
  37.  
  38. static unsigned char 
  39. codestream_fill PARAMS ((int));
  40.  
  41. /* helper functions for tm-i386.h */
  42.  
  43. /* Stdio style buffering was used to minimize calls to ptrace, but this
  44.    buffering did not take into account that the code section being accessed
  45.    may not be an even number of buffers long (even if the buffer is only
  46.    sizeof(int) long).  In cases where the code section size happened to
  47.    be a non-integral number of buffers long, attempting to read the last
  48.    buffer would fail.  Simply using target_read_memory and ignoring errors,
  49.    rather than read_memory, is not the correct solution, since legitimate
  50.    access errors would then be totally ignored.  To properly handle this
  51.    situation and continue to use buffering would require that this code
  52.    be able to determine the minimum code section size granularity (not the
  53.    alignment of the section itself, since the actual failing case that
  54.    pointed out this problem had a section alignment of 4 but was not a
  55.    multiple of 4 bytes long), on a target by target basis, and then
  56.    adjust it's buffer size accordingly.  This is messy, but potentially
  57.    feasible.  It probably needs the bfd library's help and support.  For
  58.    now, the buffer size is set to 1.  (FIXME -fnf) */
  59.  
  60. #define CODESTREAM_BUFSIZ 1    /* Was sizeof(int), see note above. */
  61. static CORE_ADDR codestream_next_addr;
  62. static CORE_ADDR codestream_addr;
  63. static unsigned char codestream_buf[CODESTREAM_BUFSIZ];
  64. static int codestream_off;
  65. static int codestream_cnt;
  66.  
  67. #define codestream_tell() (codestream_addr + codestream_off)
  68. #define codestream_peek() (codestream_cnt == 0 ? \
  69.                codestream_fill(1): codestream_buf[codestream_off])
  70. #define codestream_get() (codestream_cnt-- == 0 ? \
  71.              codestream_fill(0) : codestream_buf[codestream_off++])
  72.  
  73. static unsigned char 
  74. codestream_fill (peek_flag)
  75.     int peek_flag;
  76. {
  77.   codestream_addr = codestream_next_addr;
  78.   codestream_next_addr += CODESTREAM_BUFSIZ;
  79.   codestream_off = 0;
  80.   codestream_cnt = CODESTREAM_BUFSIZ;
  81.   read_memory (codestream_addr, (char *) codestream_buf, CODESTREAM_BUFSIZ);
  82.   
  83.   if (peek_flag)
  84.     return (codestream_peek());
  85.   else
  86.     return (codestream_get());
  87. }
  88.  
  89. static void
  90. codestream_seek (place)
  91.     int place;
  92. {
  93.   codestream_next_addr = place / CODESTREAM_BUFSIZ;
  94.   codestream_next_addr *= CODESTREAM_BUFSIZ;
  95.   codestream_cnt = 0;
  96.   codestream_fill (1);
  97.   while (codestream_tell() != place)
  98.     codestream_get ();
  99. }
  100.  
  101. static void
  102. codestream_read (buf, count)
  103.      unsigned char *buf;
  104.      int count;
  105. {
  106.   unsigned char *p;
  107.   int i;
  108.   p = buf;
  109.   for (i = 0; i < count; i++)
  110.     *p++ = codestream_get ();
  111. }
  112.  
  113. /* next instruction is a jump, move to target */
  114.  
  115. static void
  116. i386_follow_jump ()
  117. {
  118.   int long_delta;
  119.   short short_delta;
  120.   char byte_delta;
  121.   int data16;
  122.   int pos;
  123.   
  124.   pos = codestream_tell ();
  125.   
  126.   data16 = 0;
  127.   if (codestream_peek () == 0x66)
  128.     {
  129.       codestream_get ();
  130.       data16 = 1;
  131.     }
  132.   
  133.   switch (codestream_get ())
  134.     {
  135.     case 0xe9:
  136.       /* relative jump: if data16 == 0, disp32, else disp16 */
  137.       if (data16)
  138.     {
  139.       codestream_read ((unsigned char *)&short_delta, 2);
  140.  
  141.       /* include size of jmp inst (including the 0x66 prefix).  */
  142.       pos += short_delta + 4; 
  143.     }
  144.       else
  145.     {
  146.       codestream_read ((unsigned char *)&long_delta, 4);
  147.       pos += long_delta + 5;
  148.     }
  149.       break;
  150.     case 0xeb:
  151.       /* relative jump, disp8 (ignore data16) */
  152.       codestream_read ((unsigned char *)&byte_delta, 1);
  153.       pos += byte_delta + 2;
  154.       break;
  155.     }
  156.   codestream_seek (pos);
  157. }
  158.  
  159. /*
  160.  * find & return amound a local space allocated, and advance codestream to
  161.  * first register push (if any)
  162.  *
  163.  * if entry sequence doesn't make sense, return -1, and leave 
  164.  * codestream pointer random
  165.  */
  166.  
  167. static long
  168. i386_get_frame_setup (pc)
  169.      int pc;
  170. {
  171.   unsigned char op;
  172.   
  173.   codestream_seek (pc);
  174.   
  175.   i386_follow_jump ();
  176.   
  177.   op = codestream_get ();
  178.   
  179.   if (op == 0x58)        /* popl %eax */
  180.     {
  181.       /*
  182.        * this function must start with
  183.        * 
  184.        *    popl %eax          0x58
  185.        *    xchgl %eax, (%esp)  0x87 0x04 0x24
  186.        * or xchgl %eax, 0(%esp) 0x87 0x44 0x24 0x00
  187.        *
  188.        * (the system 5 compiler puts out the second xchg
  189.        * inst, and the assembler doesn't try to optimize it,
  190.        * so the 'sib' form gets generated)
  191.        * 
  192.        * this sequence is used to get the address of the return
  193.        * buffer for a function that returns a structure
  194.        */
  195.       int pos;
  196.       unsigned char buf[4];
  197.       static unsigned char proto1[3] = { 0x87,0x04,0x24 };
  198.       static unsigned char proto2[4] = { 0x87,0x44,0x24,0x00 };
  199.       pos = codestream_tell ();
  200.       codestream_read (buf, 4);
  201.       if (memcmp (buf, proto1, 3) == 0)
  202.     pos += 3;
  203.       else if (memcmp (buf, proto2, 4) == 0)
  204.     pos += 4;
  205.       
  206.       codestream_seek (pos);
  207.       op = codestream_get (); /* update next opcode */
  208.     }
  209.   
  210.   if (op == 0x55)        /* pushl %ebp */
  211.     {            
  212.       /* check for movl %esp, %ebp - can be written two ways */
  213.       switch (codestream_get ())
  214.     {
  215.     case 0x8b:
  216.       if (codestream_get () != 0xec)
  217.         return (-1);
  218.       break;
  219.     case 0x89:
  220.       if (codestream_get () != 0xe5)
  221.         return (-1);
  222.       break;
  223.     default:
  224.       return (-1);
  225.     }
  226.       /* check for stack adjustment 
  227.        *
  228.        *  subl $XXX, %esp
  229.        *
  230.        * note: you can't subtract a 16 bit immediate
  231.        * from a 32 bit reg, so we don't have to worry
  232.        * about a data16 prefix 
  233.        */
  234.       op = codestream_peek ();
  235.       if (op == 0x83)
  236.     {
  237.       /* subl with 8 bit immed */
  238.       codestream_get ();
  239.       if (codestream_get () != 0xec)
  240.         /* Some instruction starting with 0x83 other than subl.  */
  241.         {
  242.           codestream_seek (codestream_tell () - 2);
  243.           return 0;
  244.         }
  245.       /* subl with signed byte immediate 
  246.        * (though it wouldn't make sense to be negative)
  247.        */
  248.       return (codestream_get());
  249.     }
  250.       else if (op == 0x81)
  251.     {
  252.       /* subl with 32 bit immed */
  253.       int locals;
  254.       codestream_get();
  255.       if (codestream_get () != 0xec)
  256.         /* Some instruction starting with 0x81 other than subl.  */
  257.         {
  258.           codestream_seek (codestream_tell () - 2);
  259.           return 0;
  260.         }
  261.       /* subl with 32 bit immediate */
  262.       codestream_read ((unsigned char *)&locals, 4);
  263.       SWAP_TARGET_AND_HOST (&locals, 4);
  264.       return (locals);
  265.     }
  266.       else
  267.     {
  268.       return (0);
  269.     }
  270.     }
  271.   else if (op == 0xc8)
  272.     {
  273.       /* enter instruction: arg is 16 bit unsigned immed */
  274.       unsigned short slocals;
  275.       codestream_read ((unsigned char *)&slocals, 2);
  276.       SWAP_TARGET_AND_HOST (&slocals, 2);
  277.       codestream_get (); /* flush final byte of enter instruction */
  278.       return (slocals);
  279.     }
  280.   return (-1);
  281. }
  282.  
  283. /* Return number of args passed to a frame.
  284.    Can return -1, meaning no way to tell.  */
  285.  
  286. int
  287. i386_frame_num_args (fi)
  288.      struct frame_info *fi;
  289. {
  290. #if 1
  291.   return -1;
  292. #else
  293.   /* This loses because not only might the compiler not be popping the
  294.      args right after the function call, it might be popping args from both
  295.      this call and a previous one, and we would say there are more args
  296.      than there really are.  */
  297.  
  298.   int retpc;                        
  299.   unsigned char op;                    
  300.   struct frame_info *pfi;
  301.  
  302.   /* on the 386, the instruction following the call could be:
  303.      popl %ecx        -  one arg
  304.      addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  305.      anything else    -  zero args  */
  306.  
  307.   int frameless;
  308.  
  309.   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
  310.   if (frameless)
  311.     /* In the absence of a frame pointer, GDB doesn't get correct values
  312.        for nameless arguments.  Return -1, so it doesn't print any
  313.        nameless arguments.  */
  314.     return -1;
  315.  
  316.   pfi = get_prev_frame_info (fi);            
  317.   if (pfi == 0)
  318.     {
  319.       /* Note:  this can happen if we are looking at the frame for
  320.      main, because FRAME_CHAIN_VALID won't let us go into
  321.      start.  If we have debugging symbols, that's not really
  322.      a big deal; it just means it will only show as many arguments
  323.      to main as are declared.  */
  324.       return -1;
  325.     }
  326.   else
  327.     {
  328.       retpc = pfi->pc;                    
  329.       op = read_memory_integer (retpc, 1);            
  330.       if (op == 0x59)                    
  331.     /* pop %ecx */                   
  332.     return 1;                
  333.       else if (op == 0x83)
  334.     {
  335.       op = read_memory_integer (retpc+1, 1);    
  336.       if (op == 0xc4)                
  337.         /* addl $<signed imm 8 bits>, %esp */    
  338.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  339.       else
  340.         return 0;
  341.     }
  342.       else if (op == 0x81)
  343.     { /* add with 32 bit immediate */
  344.       op = read_memory_integer (retpc+1, 1);    
  345.       if (op == 0xc4)                
  346.         /* addl $<imm 32>, %esp */        
  347.         return read_memory_integer (retpc+2, 4) / 4;
  348.       else
  349.         return 0;
  350.     }
  351.       else
  352.     {
  353.       return 0;
  354.     }
  355.     }
  356. #endif
  357. }
  358.  
  359. /*
  360.  * parse the first few instructions of the function to see
  361.  * what registers were stored.
  362.  *
  363.  * We handle these cases:
  364.  *
  365.  * The startup sequence can be at the start of the function,
  366.  * or the function can start with a branch to startup code at the end.
  367.  *
  368.  * %ebp can be set up with either the 'enter' instruction, or 
  369.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  370.  * but was once used in the sys5 compiler)
  371.  *
  372.  * Local space is allocated just below the saved %ebp by either the
  373.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  374.  * a 16 bit unsigned argument for space to allocate, and the
  375.  * 'addl' instruction could have either a signed byte, or
  376.  * 32 bit immediate.
  377.  *
  378.  * Next, the registers used by this function are pushed.  In
  379.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  380.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  381.  * however, the code below is willing to see the pushes in any order,
  382.  * and will handle up to 8 of them.
  383.  *
  384.  * If the setup sequence is at the end of the function, then the
  385.  * next instruction will be a branch back to the start.
  386.  */
  387.  
  388. void
  389. i386_frame_find_saved_regs (fip, fsrp)
  390.      struct frame_info *fip;
  391.      struct frame_saved_regs *fsrp;
  392. {
  393.   long locals;
  394.   unsigned char op;
  395.   CORE_ADDR dummy_bottom;
  396.   CORE_ADDR adr;
  397.   int i;
  398.   
  399.   memset (fsrp, 0, sizeof *fsrp);
  400.   
  401.   /* if frame is the end of a dummy, compute where the
  402.    * beginning would be
  403.    */
  404.   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
  405.   
  406.   /* check if the PC is in the stack, in a dummy frame */
  407.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  408.     {
  409.       /* all regs were saved by push_call_dummy () */
  410.       adr = fip->frame;
  411.       for (i = 0; i < NUM_REGS; i++) 
  412.     {
  413.       adr -= REGISTER_RAW_SIZE (i);
  414.       fsrp->regs[i] = adr;
  415.     }
  416.       return;
  417.     }
  418.   
  419.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  420.   
  421.   if (locals >= 0) 
  422.     {
  423.       adr = fip->frame - 4 - locals;
  424.       for (i = 0; i < 8; i++) 
  425.     {
  426.       op = codestream_get ();
  427.       if (op < 0x50 || op > 0x57)
  428.         break;
  429.       fsrp->regs[op - 0x50] = adr;
  430.       adr -= 4;
  431.     }
  432.     }
  433.   
  434.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  435.   fsrp->regs[FP_REGNUM] = fip->frame;
  436. }
  437.  
  438. /* return pc of first real instruction */
  439.  
  440. int
  441. i386_skip_prologue (pc)
  442.      int pc;
  443. {
  444.   unsigned char op;
  445.   int i;
  446.   
  447.   if (i386_get_frame_setup (pc) < 0)
  448.     return (pc);
  449.   
  450.   /* found valid frame setup - codestream now points to 
  451.    * start of push instructions for saving registers
  452.    */
  453.   
  454.   /* skip over register saves */
  455.   for (i = 0; i < 8; i++)
  456.     {
  457.       op = codestream_peek ();
  458.       /* break if not pushl inst */
  459.       if (op < 0x50 || op > 0x57) 
  460.     break;
  461.       codestream_get ();
  462.     }
  463.   
  464.   i386_follow_jump ();
  465.   
  466.   return (codestream_tell ());
  467. }
  468.  
  469. void
  470. i386_push_dummy_frame ()
  471. {
  472.   CORE_ADDR sp = read_register (SP_REGNUM);
  473.   int regnum;
  474.   char regbuf[MAX_REGISTER_RAW_SIZE];
  475.   
  476.   sp = push_word (sp, read_register (PC_REGNUM));
  477.   sp = push_word (sp, read_register (FP_REGNUM));
  478.   write_register (FP_REGNUM, sp);
  479.   for (regnum = 0; regnum < NUM_REGS; regnum++)
  480.     {
  481.       read_register_gen (regnum, regbuf);
  482.       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
  483.     }
  484.   write_register (SP_REGNUM, sp);
  485. }
  486.  
  487. void
  488. i386_pop_frame ()
  489. {
  490.   FRAME frame = get_current_frame ();
  491.   CORE_ADDR fp;
  492.   int regnum;
  493.   struct frame_saved_regs fsr;
  494.   struct frame_info *fi;
  495.   char regbuf[MAX_REGISTER_RAW_SIZE];
  496.   
  497.   fi = get_frame_info (frame);
  498.   fp = fi->frame;
  499.   get_frame_saved_regs (fi, &fsr);
  500.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  501.     {
  502.       CORE_ADDR adr;
  503.       adr = fsr.regs[regnum];
  504.       if (adr)
  505.     {
  506.       read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
  507.       write_register_bytes (REGISTER_BYTE (regnum), regbuf,
  508.                 REGISTER_RAW_SIZE (regnum));
  509.     }
  510.     }
  511.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  512.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  513.   write_register (SP_REGNUM, fp + 8);
  514.   flush_cached_frames ();
  515.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  516.                     read_pc ()));
  517. }
  518.  
  519. #ifdef GET_LONGJMP_TARGET
  520.  
  521. /* Figure out where the longjmp will land.  Slurp the args out of the stack.
  522.    We expect the first arg to be a pointer to the jmp_buf structure from which
  523.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  524.    This routine returns true on success. */
  525.  
  526. int
  527. get_longjmp_target(pc)
  528.      CORE_ADDR *pc;
  529. {
  530.   CORE_ADDR sp, jb_addr;
  531.  
  532.   sp = read_register(SP_REGNUM);
  533.  
  534.   if (target_read_memory(sp + SP_ARG0, /* Offset of first arg on stack */
  535.              (char *) &jb_addr,
  536.              sizeof(CORE_ADDR)))
  537.     return 0;
  538.  
  539.  
  540.   SWAP_TARGET_AND_HOST(&jb_addr, sizeof(CORE_ADDR));
  541.  
  542.   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, (char *) pc,
  543.              sizeof(CORE_ADDR)))
  544.     return 0;
  545.  
  546.   SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
  547.  
  548.   return 1;
  549. }
  550.  
  551. #endif /* GET_LONGJMP_TARGET */
  552.