如何使用gcc更改C程序的入口点?
如何更改使用 gcc 编译的 C 程序的入口点?
就像下面的代码一样
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个链接器设置:
-Wl,...
事物将参数传递给链接器,链接器采用-e
参数来设置入口函数It's a linker setting:
the
-Wl,...
thing passes arguments to the linker, and the linker takes a-e
argument to set the entry function如果您使用的系统提供 GNU Binutils(例如 Linux),
您可以使用 objcopy 命令
使任意函数成为新的入口点。
假设一个名为
program.c
的文件包含entry
函数:首先使用
-c
编译它以生成可重定位目标文件:<前><代码>$ gcc -c 程序.c -o 程序.o
然后将
entry
重新定义为main< /代码>:
现在使用 gcc 编译新的目标文件:
注意:如果您的程序已经有一个名为
main
的函数,在步骤 2 之前,您可以执行单独的 objcopy 调用:If you are on a system that provides GNU Binutils (like Linux),
you can use the
objcopy
commandto make an arbitrary function the new entry point.
Suppose a file called
program.c
containing theentry
function:You first compile it using
-c
to generate a relocatable object file:Then you redefine
entry
to bemain
:Now use gcc to compile the new object file:
NOTE: If your program already has a function called
main
, before step 2, you can perform a separateobjcopy
invocation:您可以将源代码修改为:
“.interp”部分将使您的程序能够调用外部共享库。
exit 调用将使您的入口函数退出程序而不是返回。
然后将程序构建为可执行的共享库:
You can modify your source code as:
The ".interp" section will let your program able to call external shared library.
The exit call will make your entry function to exit program instead of return.
Then build the program as a shared library which is executable:
最小可运行示例和其他答案的注释
main.c
编译并运行:
注释:
没有
-nostartfiles
,链接失败并显示:可能是因为 glibc 设置代码在 main 之前运行
_start
通常调用main
。命令行参数不是为您设置的,大概是因为它们将由在 main 之前运行的 glibc 代码设置,因此尝试使用它们会打印未定义的值。我还没有找到适合他们的方法。
另一个选择是删除
-nostartfiles
,只定义一个虚拟main
:,然后编译并运行良好:
CLI args 仍然损坏,但也许更多的东西 stdlib 的东西会像这样工作。
在 Ubuntu 23.10 中测试。
Minimal runnable example and notes on other answers
main.c
compile and run:
The notes:
without
-nostartfiles
, the link fails with:presumably because the glibc setup code that runs before main in
_start
normally callsmain
.command line arguments are not setup for you, presumably because they would be setup by the glibc code that runs before main, so trying to use them prints undefined values. I haven't found a method that works for them.
Another option is to remove
-nostartfiles
is to just define a dummymain
:which then compile and runs fine with:
CLI args are still broken, but perhaps more things stdlib things will work like this.
Tested in Ubuntu 23.10.
如果您在托管环境(即 Linux、Windows 等)上执行此操作,那么使用链接器重新定义哪个函数将成为
main()
可能是一个更好的主意:这样,启动时调用的函数仍然是
main()
,因此操作系统的_start()
代码仍将正常工作。如果您尝试使用--entry
实际更改入口点名称,您可能会遇到很多问题,因为操作系统的_start
不会被调用。示例:
https://godbolt.org/z/aWGYEMhaE
If you're doing this on a hosted environment (i.e. Linux, Windows etc.) it's probably a much better idea to use the linker to redefine which function becomes
main()
with this:This way, the function being called at startup is still
main()
, so your operating system's_start()
code will still work as normal. If you try to actually change the entry point name with--entry
, you'll probably run into a lot of issues because the OS's_start
won't be called.Example:
https://godbolt.org/z/aWGYEMhaE