不使用 libc 编译 [英] Compiling without libc
问题描述
我想在没有 (g)libc 的情况下编译我的 C 代码.如何停用它以及哪些功能依赖于它?
I want to compile my C-code without the (g)libc. How can I deactivate it and which functions depend on it?
我试过 -nostdlib 但它没有帮助:代码是可编译和运行的,但我仍然可以在我的可执行文件的十六进制转储中找到 libc 的名称.
I tried -nostdlib but it doesn't help: The code is compilable and runs, but I can still find the name of the libc in the hexdump of my executable.
推荐答案
如果你用 -nostdlib
编译你的代码,你将无法调用任何 C 库函数(当然),但你也没有得到常规的 C 引导代码.特别是,Linux 上程序的真正入口点不是 main()
,而是一个名为 _start()
的函数.标准库通常提供一个版本,它运行一些初始化代码,然后调用 main()
.
If you compile your code with -nostdlib
, you won't be able to call any C library functions (of course), but you also don't get the regular C bootstrap code. In particular, the real entry point of a program on Linux is not main()
, but rather a function called _start()
. The standard libraries normally provide a version of this that runs some initialization code, then calls main()
.
尝试用 gcc -nostdlib -m32
编译:
void _start() {
/* main body of program: call main(), etc */
/* exit system call */
asm("movl $1,%eax;"
"xorl %ebx,%ebx;"
"int $0x80"
);
}
_start()
函数应始终以调用 exit
(或其他非返回系统调用,例如 exec
)结束.由于通常的 exit()
不可用,上面的示例直接使用内联汇编调用系统调用.
The _start()
function should always end with a call to exit
(or other non-returning system call such as exec
). The above example invokes the system call directly with inline assembly since the usual exit()
is not available.
这篇关于不使用 libc 编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!