x86call.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 611 行 · 第 1/2 页

C
611
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Stack and call analysis routines
*
****************************************************************************/


#include <stddef.h>
#include <string.h>
#include "x86.h"
#include "madregs.h"

#define OP_1 0
#define OP_2 1
#define OP_3 2

mad_string              DIGENTRY MICallStackGrowsUp( void )
{
    return( MS_FAIL );
}

const mad_string        *DIGENTRY MICallTypeList( void )
{
    static const mad_string list[] = { MSTR_NEAR, MSTR_FAR, MSTR_INTERRUPT, MSTR_NIL };

    return( list );
}


mad_status      DIGENTRY MICallBuildFrame( mad_string call, address ret, address rtn, const mad_registers *in, mad_registers *out )
{
    unsigned    dec;
    unsigned_32 value;
    address     sp;

    dec = BIG_SEG( rtn ) ? 4 : 2;
    if( call == MSTR_NIL ) {
        call = (dec == 2) ? MSTR_FAR : MSTR_NEAR;
    }
    out->x86 = in->x86;
    sp = GetRegSP( out );
    switch( call ) {
    case MSTR_INTERRUPT:
        sp.mach.offset -= dec;
        value = out->x86.cpu.efl;
        MCWriteMem( sp, dec, &value );
        /* fall through */
    case MSTR_FAR:
        sp.mach.offset -= dec;
        value = ret.mach.segment;
        MCWriteMem( sp, dec, &value );
        /* fall through */
    case MSTR_NEAR:
        sp.mach.offset -= dec;
        value = ret.mach.offset;
        MCWriteMem( sp, dec, &value );
        break;
    }
    out->x86.cpu.esp = sp.mach.offset;
    out->x86.cpu.cs  = rtn.mach.segment;
    out->x86.cpu.eip = rtn.mach.offset;
    return( MS_OK );
}

const mad_reg_info      *DIGENTRY MICallReturnReg( mad_string call, address rtn )
{
    call = call;
    if( BIG_SEG( rtn ) ) {
        return( &CPU_eax.info );
    } else {
        return( &CPU_ax.info );
    }
}

const mad_reg_info      **DIGENTRY MICallParmRegList( mad_string call, address rtn )
{
    static const mad_reg_info *list32[] =
        { &CPU_eax.info, &CPU_edx.info, &CPU_ebx.info, &CPU_ecx.info, NULL };
    static const mad_reg_info *list16[] =
        { &CPU_ax.info,  &CPU_dx.info,  &CPU_bx.info,  &CPU_cx.info, NULL };

    call = call;
    if( BIG_SEG( rtn ) ) {
        return( list32 );
    } else {
        return( list16 );
    }
}

/*
    WHAT'S a halfways normal prolog look like? Here are some snippets

----------------------------------------------------------------------
            PUSH ESP
            PUSH c
            PUSH c
            CALL __TNK/__TNC
----------------------------------------------------------------------
            CALL __GETDS
----------------------------------------------------------------------
            PUSH c
            CALL __GRO
----------------------------------------------------------------------
            PUSH c
            CALL __CHK
----------------------------------------------------------------------
            CALL __PRO
----------------------------------------------------------------------
            INC     BP
            PUSH    BP
            MOV     BP,SP
            SUB     SP,n
----------------------------------------------------------------------
            {
                PUSH    DS
                POP     AX
                NOP
            }
            {
                NOP
                NOP
                NOP
            }
            {
                MOV     AX,DGROUP
            }
            INC     BP
            PUSH    BP
            MOV     BP,SP
            PUSH    DS
            MOV     DS,AX
            SUB SP,n
----------------------------------------------------------------------
            PUSH        BP
            MOV BP,SP
            SUB SP,n
----------------------------------------------------------------------
            ENTER X,0
----------------------------------------------------------------------
            SUB BP,n
----------------------------------------------------------------------
            SUB sp,c
----------------------------------------------------------------------
            AND sp,-8
----------------------------------------------------------------------
            { PUSH AX }
            MOV AX,c
            CALL __CHK
            { POP AX }
----------------------------------------------------------------------
            MOV [esp],al
----------------------------------------------------------------------
            PLUS a liberal sprinkling of pushes and pops anywhere

This boils down to the following set of recognizable stuff

        Jackpot instructions

            MOV         BP,SP
            ENTER       X,0

        These fiddle the stack and must be counted!

            PUSH        REG
            POP         REG
            SUB         SP,n
            SUB         BP,n


        Indicates a far routine

            INC         BP
        Things we may skip over

            PUSH        const           // popped by __xxx
            PUSH        ESP             // popped by __TNC
            MOV         AX,c
            MOV         [esp],al
            MOV         DS,AX
            NOP
            CALL        __xxx

        Anything else stops us in our tracks


*/

#define IsSPReg( op ) ( (op).type == DO_REG && \
                        ( (op).base == DR_X86_sp || (op).base == DR_X86_esp ) )
#define IsCSReg( op ) ( (op).type == DO_REG && (op).base == DR_X86_cs )
#define IsBPReg( op ) ( (op).type == DO_REG && \
                        ( (op).base == DR_X86_bp || (op).base == DR_X86_ebp ) )
#define ConstOp( op ) ( (op).type == DO_IMMED )

typedef struct {
    const char  *name;
    int         pop;
} prolog_call;

static const prolog_call PrologCalls[] =
{
    { "__GETDS", 0 },
    { "__PRO", 0 },
    { "__CHK", 4 },
    { "__GRO", 4 },
    { "__TNK", 12 },
    { "__TNC", 12 },
    { "__STK", 0 },
    { NULL, 0 }
};

static int      Is32BitSegment;

static int IdentifyFunc( char *name, long *sp_adjust )
{
    const prolog_call   *p;

    for( p = PrologCalls; p->name != NULL; ++p ) {
        if( strncmp( name, p->name, strlen( p->name ) ) == 0 ) {
            *sp_adjust += p->pop;
            return( 1 );
        }
    }
    return( 0 );
}

static addr_off GetAnOffset( address *where )
{
    DbgAddr = *where;
    if( Is32BitSegment ) {
        return( GetDataLong() );
    } else {
        return( (unsigned short) GetDataWord() );
    }
}


static int GetBPFromStack( address *where, address *value )
{
    long        offset;

    *value = *where;
    offset = GetAnOffset( where );
    value->mach.offset = offset & ~1;
    return( ( offset & 1 ) != 0 );
}


address GetFarAddr( address *return_location )
{
    address addr;

    addr.mach.offset = GetAnOffset( return_location );
    addr.mach.segment = GetDataWord();
    if( Is32BitSegment ) {
        GetDataWord();
    }
    return( addr );
}

static void DisAsm( mad_disasm_data *dd )
{
    DoCode( dd, Is32BitSegment );
    DbgAddr.mach.offset += dd->ins.size;
}

static int FindCall( address *ip_value, address *return_addr_location )
{
    address         prev_ins;
    address         return_addr;
    mad_disasm_data dd;

    return_addr = *ip_value;
    return_addr.mach.offset = GetAnOffset( return_addr_location );
    prev_ins = return_addr;
    if( GetDisasmPrev( &prev_ins ) != MS_OK )
        return( 0 );
    DbgAddr = prev_ins;
    DisAsm( &dd );
    if( dd.ins.type == DI_X86_call ) {
        DbgAddr = prev_ins;
        if( GetDisasmPrev( &DbgAddr ) != MS_OK )

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?