home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / gdb-4.12-src.lha / src / amiga / gdb-4.12 / gdb / i386-tdep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-03  |  15.0 KB  |  582 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.   unsigned char buf[4];
  119.   long delta;
  120.  
  121.   int data16;
  122.   CORE_ADDR 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 (buf, 2);
  140.       delta = extract_signed_integer (buf, 2);
  141.  
  142.       /* include size of jmp inst (including the 0x66 prefix).  */
  143.       pos += delta + 4; 
  144.     }
  145.       else
  146.     {
  147.       codestream_read (buf, 4);
  148.       delta = extract_signed_integer (buf, 4);
  149.  
  150.       pos += delta + 5;
  151.     }
  152.       break;
  153.     case 0xeb:
  154.       /* relative jump, disp8 (ignore data16) */
  155.       codestream_read (buf, 1);
  156.       /* Sign-extend it.  */
  157.       delta = extract_signed_integer (buf, 1);
  158.  
  159.       pos += 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.       char buf[4];
  259.       /* Maybe it is subl with 32 bit immedediate.  */
  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.       /* It is subl with 32 bit immediate.  */
  268.       codestream_read ((unsigned char *)buf, 4);
  269.       return extract_signed_integer (buf, 4);
  270.     }
  271.       else
  272.     {
  273.       return (0);
  274.     }
  275.     }
  276.   else if (op == 0xc8)
  277.     {
  278.       char buf[2];
  279.       /* enter instruction: arg is 16 bit unsigned immed */
  280.       codestream_read ((unsigned char *)buf, 2);
  281.       codestream_get (); /* flush final byte of enter instruction */
  282.       return extract_unsigned_integer (buf, 2);
  283.     }
  284.   return (-1);
  285. }
  286.  
  287. /* Return number of args passed to a frame.
  288.    Can return -1, meaning no way to tell.  */
  289.  
  290. int
  291. i386_frame_num_args (fi)
  292.      struct frame_info *fi;
  293. {
  294. #if 1
  295.   return -1;
  296. #else
  297.   /* This loses because not only might the compiler not be popping the
  298.      args right after the function call, it might be popping args from both
  299.      this call and a previous one, and we would say there are more args
  300.      than there really are.  */
  301.  
  302.   int retpc;                        
  303.   unsigned char op;                    
  304.   struct frame_info *pfi;
  305.  
  306.   /* on the 386, the instruction following the call could be:
  307.      popl %ecx        -  one arg
  308.      addl $imm, %esp  -  imm/4 args; imm may be 8 or 32 bits
  309.      anything else    -  zero args  */
  310.  
  311.   int frameless;
  312.  
  313.   FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
  314.   if (frameless)
  315.     /* In the absence of a frame pointer, GDB doesn't get correct values
  316.        for nameless arguments.  Return -1, so it doesn't print any
  317.        nameless arguments.  */
  318.     return -1;
  319.  
  320.   pfi = get_prev_frame_info (fi);            
  321.   if (pfi == 0)
  322.     {
  323.       /* Note:  this can happen if we are looking at the frame for
  324.      main, because FRAME_CHAIN_VALID won't let us go into
  325.      start.  If we have debugging symbols, that's not really
  326.      a big deal; it just means it will only show as many arguments
  327.      to main as are declared.  */
  328.       return -1;
  329.     }
  330.   else
  331.     {
  332.       retpc = pfi->pc;                    
  333.       op = read_memory_integer (retpc, 1);            
  334.       if (op == 0x59)                    
  335.     /* pop %ecx */                   
  336.     return 1;                
  337.       else if (op == 0x83)
  338.     {
  339.       op = read_memory_integer (retpc+1, 1);    
  340.       if (op == 0xc4)                
  341.         /* addl $<signed imm 8 bits>, %esp */    
  342.         return (read_memory_integer (retpc+2,1)&0xff)/4;
  343.       else
  344.         return 0;
  345.     }
  346.       else if (op == 0x81)
  347.     { /* add with 32 bit immediate */
  348.       op = read_memory_integer (retpc+1, 1);    
  349.       if (op == 0xc4)                
  350.         /* addl $<imm 32>, %esp */        
  351.         return read_memory_integer (retpc+2, 4) / 4;
  352.       else
  353.         return 0;
  354.     }
  355.       else
  356.     {
  357.       return 0;
  358.     }
  359.     }
  360. #endif
  361. }
  362.  
  363. /*
  364.  * parse the first few instructions of the function to see
  365.  * what registers were stored.
  366.  *
  367.  * We handle these cases:
  368.  *
  369.  * The startup sequence can be at the start of the function,
  370.  * or the function can start with a branch to startup code at the end.
  371.  *
  372.  * %ebp can be set up with either the 'enter' instruction, or 
  373.  * 'pushl %ebp, movl %esp, %ebp' (enter is too slow to be useful,
  374.  * but was once used in the sys5 compiler)
  375.  *
  376.  * Local space is allocated just below the saved %ebp by either the
  377.  * 'enter' instruction, or by 'subl $<size>, %esp'.  'enter' has
  378.  * a 16 bit unsigned argument for space to allocate, and the
  379.  * 'addl' instruction could have either a signed byte, or
  380.  * 32 bit immediate.
  381.  *
  382.  * Next, the registers used by this function are pushed.  In
  383.  * the sys5 compiler they will always be in the order: %edi, %esi, %ebx
  384.  * (and sometimes a harmless bug causes it to also save but not restore %eax);
  385.  * however, the code below is willing to see the pushes in any order,
  386.  * and will handle up to 8 of them.
  387.  *
  388.  * If the setup sequence is at the end of the function, then the
  389.  * next instruction will be a branch back to the start.
  390.  */
  391.  
  392. void
  393. i386_frame_find_saved_regs (fip, fsrp)
  394.      struct frame_info *fip;
  395.      struct frame_saved_regs *fsrp;
  396. {
  397.   long locals;
  398.   unsigned char op;
  399.   CORE_ADDR dummy_bottom;
  400.   CORE_ADDR adr;
  401.   int i;
  402.   
  403.   memset (fsrp, 0, sizeof *fsrp);
  404.   
  405.   /* if frame is the end of a dummy, compute where the
  406.    * beginning would be
  407.    */
  408.   dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
  409.   
  410.   /* check if the PC is in the stack, in a dummy frame */
  411.   if (dummy_bottom <= fip->pc && fip->pc <= fip->frame) 
  412.     {
  413.       /* all regs were saved by push_call_dummy () */
  414.       adr = fip->frame;
  415.       for (i = 0; i < NUM_REGS; i++) 
  416.     {
  417.       adr -= REGISTER_RAW_SIZE (i);
  418.       fsrp->regs[i] = adr;
  419.     }
  420.       return;
  421.     }
  422.   
  423.   locals = i386_get_frame_setup (get_pc_function_start (fip->pc));
  424.   
  425.   if (locals >= 0) 
  426.     {
  427.       adr = fip->frame - 4 - locals;
  428.       for (i = 0; i < 8; i++) 
  429.     {
  430.       op = codestream_get ();
  431.       if (op < 0x50 || op > 0x57)
  432.         break;
  433.       fsrp->regs[op - 0x50] = adr;
  434.       adr -= 4;
  435.     }
  436.     }
  437.   
  438.   fsrp->regs[PC_REGNUM] = fip->frame + 4;
  439.   fsrp->regs[FP_REGNUM] = fip->frame;
  440. }
  441.  
  442. /* return pc of first real instruction */
  443.  
  444. int
  445. i386_skip_prologue (pc)
  446.      int pc;
  447. {
  448.   unsigned char op;
  449.   int i;
  450.   
  451.   if (i386_get_frame_setup (pc) < 0)
  452.     return (pc);
  453.   
  454.   /* found valid frame setup - codestream now points to 
  455.    * start of push instructions for saving registers
  456.    */
  457.   
  458.   /* skip over register saves */
  459.   for (i = 0; i < 8; i++)
  460.     {
  461.       op = codestream_peek ();
  462.       /* break if not pushl inst */
  463.       if (op < 0x50 || op > 0x57) 
  464.     break;
  465.       codestream_get ();
  466.     }
  467.   
  468.   i386_follow_jump ();
  469.   
  470.   return (codestream_tell ());
  471. }
  472.  
  473. void
  474. i386_push_dummy_frame ()
  475. {
  476.   CORE_ADDR sp = read_register (SP_REGNUM);
  477.   int regnum;
  478.   char regbuf[MAX_REGISTER_RAW_SIZE];
  479.   
  480.   sp = push_word (sp, read_register (PC_REGNUM));
  481.   sp = push_word (sp, read_register (FP_REGNUM));
  482.   write_register (FP_REGNUM, sp);
  483.   for (regnum = 0; regnum < NUM_REGS; regnum++)
  484.     {
  485.       read_register_gen (regnum, regbuf);
  486.       sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
  487.     }
  488.   write_register (SP_REGNUM, sp);
  489. }
  490.  
  491. void
  492. i386_pop_frame ()
  493. {
  494.   FRAME frame = get_current_frame ();
  495.   CORE_ADDR fp;
  496.   int regnum;
  497.   struct frame_saved_regs fsr;
  498.   struct frame_info *fi;
  499.   char regbuf[MAX_REGISTER_RAW_SIZE];
  500.   
  501.   fi = get_frame_info (frame);
  502.   fp = fi->frame;
  503.   get_frame_saved_regs (fi, &fsr);
  504.   for (regnum = 0; regnum < NUM_REGS; regnum++) 
  505.     {
  506.       CORE_ADDR adr;
  507.       adr = fsr.regs[regnum];
  508.       if (adr)
  509.     {
  510.       read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
  511.       write_register_bytes (REGISTER_BYTE (regnum), regbuf,
  512.                 REGISTER_RAW_SIZE (regnum));
  513.     }
  514.     }
  515.   write_register (FP_REGNUM, read_memory_integer (fp, 4));
  516.   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
  517.   write_register (SP_REGNUM, fp + 8);
  518.   flush_cached_frames ();
  519.   set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  520.                     read_pc ()));
  521. }
  522.  
  523. #ifdef GET_LONGJMP_TARGET
  524.  
  525. /* Figure out where the longjmp will land.  Slurp the args out of the stack.
  526.    We expect the first arg to be a pointer to the jmp_buf structure from which
  527.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  528.    This routine returns true on success. */
  529.  
  530. int
  531. get_longjmp_target(pc)
  532.      CORE_ADDR *pc;
  533. {
  534.   char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
  535.   CORE_ADDR sp, jb_addr;
  536.  
  537.   sp = read_register (SP_REGNUM);
  538.  
  539.   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
  540.               buf,
  541.               TARGET_PTR_BIT / TARGET_CHAR_BIT))
  542.     return 0;
  543.  
  544.   jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  545.  
  546.   if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
  547.               TARGET_PTR_BIT / TARGET_CHAR_BIT))
  548.     return 0;
  549.  
  550.   *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
  551.  
  552.   return 1;
  553. }
  554.  
  555. #endif /* GET_LONGJMP_TARGET */
  556.  
  557. #ifdef I386_AIX_TARGET
  558. /* On AIX, floating point values are returned in floating point registers.  */
  559.  
  560. void
  561. i386_extract_return_value(type, regbuf, valbuf)
  562.      struct type *type;
  563.      char regbuf[REGISTER_BYTES];
  564.      char *valbuf;
  565. {
  566.   if (TYPE_CODE_FLT == TYPE_CODE(type))
  567.     {
  568.       extern struct ext_format ext_format_i387;
  569.       double d;
  570.       /* 387 %st(0), gcc uses this */
  571.       ieee_extended_to_double (&ext_format_i387,
  572.                    ®buf[REGISTER_BYTE(FP0_REGNUM)],
  573.                    &d);
  574.       store_floating (valbuf, len, d);
  575.     }
  576.   else
  577.     { 
  578.       memcpy (valbuf, regbuf, TYPE_LENGTH (type)); 
  579.     }
  580. }
  581. #endif /* I386_AIX_TARGET */
  582.