免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1011 | 回复: 0

Calling function via jmp [复制链接]

论坛徽章:
0
发表于 2009-12-09 15:28 |显示全部楼层

                The following code snippet implements calling the function via jmp.
This is here to verify that the difference between 'call' and 'jmp' instructions is that the former one pushes the return address to the stack before jump to the new label to execute while the later one won't. As a result we can emulate 'call' by pushing the address of the function we want to exec to the stack before 'jmp'.
If we can extract the content of eip before jmping we can push it into the stack before jmp. This would behave exactly as we use 'call' instruction I think.
// BEGIN OF THE PROGRAM
#include
void a();
void b(int first, int last);
void c();
int
main()
{
    a();
    printf("In main().\n");
    return 0;
}
/* stack
* |--------------|
* |  Param 2 (5) |
* |--------------|
* |  Param 1 (10)|
* |--------------|
* |Ret addr(stop)|
* |--------------|
*/
void a()
{
    printf("In a().\n");
    __asm__("pushl $5; pushl $10; pushl $stop; pushl $b; jmp c");
    // This line should be skipped
    printf("This should not be printed.\n");
    // Set a label here so we have somewhere to return after finishing
    // function b()
    __asm__("stop:");
    printf("Ending a().\n");
}
void b(int first, int last)
{
    printf("In b().\n");
    printf("param: %d and %d\n", first, last);
}
// We jump here. Because c is a function, it will invoke ret after the
// execution. ret pops the return address from stack which is the
// address of function b we pushed to stack manually in advance. Thus
// function b will be executed in turn without being called explicitly.
void c()
{
    printf("In c().\n");
}
// END OF THE PROGRAM
Compile and execute, the output will be:
In a().
In c().
In b().
param: 10 and 5
Ending a().
In main().
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/6646/showart_2115442.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP