中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Visual C++ > MFC或CPP
也谈C++中THUNK的一种实现技术
作者:paul2002 时间:2001-10-06 09:58 出处:互联网 责编:chinaitpower
              摘要:也谈C++中THUNK的一种实现技术

看了“我对C++中THUNK一种实现技术的分析”一文,有点不同的想法。
原代码如下:
#pragma pack(push,1)
// structure to store the machine code
struct Thunk
{
    char    m_jmp;          // op code of jmp instruction
    unsigned long   m_relproc;      // relative jmp

};
#pragma pack(pop)

//This type of structure can contain then thunk code, which can be executed on the fly. Let's take a look at the simple case in which we are going to execute our required function by thunk.

//Program 77

#include <iostream>

#include <windows.h>

using namespace std;

class C;

C* g_pC = NULL;

typedef void(*pFUN)();


class C

{

public:
 int a;
 int b;
 float bbj;
    Thunk    m_thunk;

       //virtual void g(){};

    void Init(pFUN pFun, void* pThis)

    {

        // op code of jump instruction

        m_thunk.m_jmp = 0xe9;

        // address of the appripriate function

/*@@@@*/  m_thunk.m_relproc = (int)pFun- ((int)&m_thunk+sizeof(Thunk));
    FlushInstructionCache(GetCurrentProcess(),

                                &m_thunk, sizeof(m_thunk));

    }

 

    // this is cour call back function

    static void CallBackFun()

    {

        C* pC = g_pC;
        // initilize the thunk

        pC->Init(StaticFun, pC);
        // get the address of thunk code

        pFUN pFun = (pFUN)&(pC->m_thunk);
        // start executing thunk code which will call StaticFun

        pFun();

        cout << "C::CallBackFun" << endl;

    }

 

    static void StaticFun()

    {

        cout << "C::StaticFun" << endl;

    }

};

 

int main()

{

    C objC;
    g_pC = &objC;
    C::CallBackFun();
    return 0;
}

首先我们知道段内直接跳转的JMP指令长5个字节,所以sizeof(Thunk)必须是五字节,这也是#pragma pack(push,1)保持struct 1字节对齐的原因。
再来看static member function StaticFun的地址pFun,在VC编译器下,实际上该地址指向一条段内JMP指令,该JMP指令跳转到StaticFun的执行代码,比如pFun=0040106E,即指向E9 ED 04 00 00,所以当前EIP=0040106E+5=401073(即下一条指令的地址),执行JMP,EIP=EIP+04ED=401560,401560即是StaticFun执行代码的起始地址。
接着看:
// get the address of thunk code
pFUN pFun = (pFUN)&(pC->m_thunk);
// start executing thunk code which will call StaticFun
pFun();
此处的段内直接函数call,将直接跳转到&m_thunk所指的代码执行,也即
JMP (int)pFun - ((int)this+sizeof(Thunk)),
而此时EIP=&m_thunk+5=this+5,所以这个JMP将跳转到
EIP=EIP+(int)pFun - ((int)this+sizeof(Thunk))=pFun,
这样就最终跳到StaticFun执行代码的起始地址。
所以如果m_thunk前还有其他变量,比如int a;则必须使
m_thunk.m_relproc = (int)pFun - ((int)this+sizeof(Thunk)+sizeof(int));
当pFun();时,EIP=&m_thunk+5=this+sizeof(int)+5,
EIP=EIP+(int)pFun - ((int)this+sizeof(Thunk)+sizeof(int))=pFun
这样才能最终跳到StaticFun执行代码的起始地址。
所以,最好写成:
m_thunk.m_relproc = (int)pFun - ((int)&m_thunk+sizeof(Thunk));

 

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有