首页
社区
课程
招聘
[求助] c与汇编混合编译时链接出错
2018-3-14 10:41 2378

[求助] c与汇编混合编译时链接出错

2018-3-14 10:41
2378

1.编译c文件

\\ hello.c
#include <stdio.h>

extern int fun();

void main(int argc,char* argv[])
{
    int number;
    number= fun();
    printf("number = %d\n",number);
    return ;
}
`

编译成obj目标文件:
cl /c hello.c -o hello.obj
图片描述

2编译汇编文件

.386
.model flat,stdcall
option casemap:none
fun proto ;
.code 
fun proc
    mov eax,55
    ret
fun endp
end

编译成myfun.obj文件:
ml /c myfun.asm
图片描述

3 链接文件生成可执行文件

link myfun.obj hello.obj
图片描述

 

分析一下文件:
dumpbin /all hello.obj
图片描述

 

dumpbin /all myfun.obj
图片描述

 

发现asm导出的函数名有问题,但是不知到怎么改,求助!!!

现在已经有一个解决方法(不靠谱!)

用010Edit修改符号,让myfun.obj文件中_fun@0强制修改成_fun.
图片描述
重新编译
图片描述
程序成功生成。
但是这个方法不靠谱,我不能没写一个函数就这么干!


[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

最后于 2018-3-14 10:59 被fily阳光编辑 ,原因:
收藏
点赞0
打赏
分享
最新回复 (7)
雪    币: 11345
活跃值: (4083)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
xie风腾 2018-3-14 10:50
2
0
来顶楼主,等大牛出手
雪    币: 2348
活跃值: (122)
能力值: ( LV5,RANK:60 )
在线值:
发帖
回帖
粉丝
Wszzy 1 2018-3-14 11:15
3
0
extern  "C"  int    __stdcall  fun();
雪    币: 205
活跃值: (2634)
能力值: ( LV7,RANK:140 )
在线值:
发帖
回帖
粉丝
yeyeshun 2 2018-3-14 11:23
4
0
Wszzy extern "C" int __stdcall fun();
extern    "C"    extern  int    __stdcall    fun();
雪    币: 32
活跃值: (82)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
fily阳光 2018-3-14 11:25
5
0
这个是c文件,extern  “C”  必须时c++程序
雪    币: 32
活跃值: (82)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
fily阳光 2018-3-14 11:35
6
0

解决方法

两种方法,一种是修改C生成文件 时指定stdcall方式 (Gz)
cl /c /Gz hello.c
第二种方式:
在汇编代码中去掉指定调用约定(这种方式调用WINAPI可能出错)
.model flat;注释这行,那么调用约定将编程c方式,stdcall

雪    币: 174
活跃值: (26)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
龙组 1 2018-3-14 15:12
7
0
第四行修改为“extern  int  __stdcall  fun();”就可以了。
你的汇编里面的  fun是stdcall的,所以生成的符号是  _fun@0
雪    币: 32
活跃值: (82)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
fily阳光 2018-3-15 08:40
8
0
龙组 第四行修改为“extern int __stdcall fun();”就可以了。 你的汇编里面的 fun是stdcall的,所以生成的符号是 _fun@0
明白,谢谢
游客
登录 | 注册 方可回帖
返回