从C中的二进制文件调用函数(main()) [英] Invoking a function (main()) from a binary file in C
问题描述
我有简单的c程序,例如my_bin.c:
I have simple c program like, my_bin.c:
#include <stdio.h>
int main()
{
printf("Success!\n");
return 0;
}
我用gcc编译并获得可执行文件:my_bin.
I compile it with gcc and got executable: my_bin.
现在,我想使用另一个C程序调用main(或运行此my_bin).我对mmap和函数指针所做的操作如下:
Now I want to invoke main (or run this my_bin) using another C program. That I did with mmap and function pointer like this:
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
int main()
{
void (*fun)();
int fd;
int *map;
fd = open("./my_bin", O_RDONLY);
map = mmap(0, 8378, PROT_READ, MAP_SHARED, fd, 0);
fun = map;
fun();
return 0;
}
添加了PROT_EXEC 从响应中使其更加清晰... 我想在第二个程序中调用一个外部二进制程序.
EDIT 1: added PROT_EXEC Making it more clear from responses ... I want to call an external binary program within second program.
我不知道如何使用main(其他程序)的地址初始化函数指针.有什么主意吗?
I don't know how to initialize function pointer with the address of main(other program's). any idea?
为什么谷歌搜索后发现段错误,这是因为我的mmap的大小和offset参数.它应该是页面大小的倍数. [参考:在C中使用mmap读取二进制文件时出现段错误
Why seg fault, after googling, figured out, its because of my size and offset argument of mmap. It should be multiple of pagesize. [Reference: Segfault while using mmap in C for reading binary files
现在代码如下:
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
int main()
{
void (*fun)();
int fd;
int *map;
int offset = 8378;
int pageoffset = offset % getpagesize();
fd = open("./my_bin", O_RDONLY);
if(fd == -1) {
printf("Err opening file\n");
return -1;
}
map = mmap(0, 8378 + pageoffset, PROT_READ|PROT_EXEC,
MAP_SHARED, fd, offset - pageoffset);
perror("Err\n"); //This is printing err and Success!
//fun = map; // If I uncomment this and
//fun(); // this line then, still it
// print err and Success! from perror
// but later it says Illegal instruction.
return 0;
}
仍然带有fun()或没有它仍无法打印...不确定如何提供main()地址.
Still with fun() or without that its not printing ... not sure how to give main() address.
第一件事:我没有正确读取定义,我已经给出了应该从中读取二进制文件的地址. 第二个:mmap:大小和偏移量应为页面大小的倍数.
First thing: I didn't read definition properly, I have already given address from which I should read binary file. Second: mmap: size and offset should be multiple of pagesize.
推荐答案
main()
通常不是C程序中的第一个函数.链接器将在此之前放置一些设置/初始化代码,其中包括设置环境,获取命令行参数,将其解析为字符串数组之类的东西.
main()
usually isn't the first function in a C program. The linker will put some setup/init code before that which, among other things, will set up the environment, get the command line arguments, parse them into a string array, stuff like that.
新的main()
函数开始设置内存分配例程时会出现问题-基本上,这将破坏主应用程序的所有重要数据结构.
It gets problematic when the new main()
function starts to set up the memory allocation routines - basically, this will ruin all the important data structures of your main application.
如果要执行功能(即不使用main()
),则将C代码编译到共享库中,并使用dlopen()
或操作系统的等效库进行加载.
If you want to execute a function (i.e. without main()
), then compile your C code into a shared library and load that with dlopen()
or your OS's equivalent.
如果您确实需要main()
,请使用fork()
和exec()
.
If you really need main()
, use fork()
and exec()
.
这篇关于从C中的二进制文件调用函数(main())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!