如何在 Mac OS X 上构建包含入口点的 C 程序?
如何在 Mac OS X 上构建包含入口点的 C 程序?
我想构建:
start() {
/* exit system call */
asm("movl $1,%eax;"
"xorl %ebx,%ebx;"
"int $0x80"
);
}
但当我运行时:
gcc -nostdlib min.c
我总是得到:
ld: could not find entry point "start" (perhaps missing crt1.o)
collect2: ld returned 1 exit status
我所做的另一次尝试只是为了看看它在做什么,
gcc -nostdlib -c min.c && otool -tV min.o
然后运行: 输出是:
(__TEXT,__text) section
_start:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 leave
0000000000000005 ret
那么,在“开始”函数之前,下划线来自哪里?我该如何防止这种情况发生?或者更简单地说:
如何构建包含 Mac OS X 入口点的 C 程序?
谢谢, 疯狂陈兹
How do you build a C program that includes the entry point on Mac OS X?
I want to build:
start() {
/* exit system call */
asm("movl $1,%eax;"
"xorl %ebx,%ebx;"
"int $0x80"
);
}
but when I run:
gcc -nostdlib min.c
I always get:
ld: could not find entry point "start" (perhaps missing crt1.o)
collect2: ld returned 1 exit status
The one other attempt I made just to see what it was doing was run:
gcc -nostdlib -c min.c && otool -tV min.o
And the output was:
(__TEXT,__text) section
_start:
0000000000000000 pushq %rbp
0000000000000001 movq %rsp,%rbp
0000000000000004 leave
0000000000000005 ret
So where did that underscore come from before the "start" function? How do I prevent that from happening? Or more simply:
How do you build a C program that includes the entry point on Mac OS X?
Thanks,
CrazyChenz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过使用
asm
单独声明来控制 gcc 为函数提供的符号:确保这是一个独立的声明,与函数定义分开。
您可以在此处阅读有关控制生成符号的更多详细信息:https://stackoverflow.com/a/1035937/12928775
正如 Peter Cordes 指出的那样,您可能应该在函数定义中包含 __attribute__((naked)) 以防止 gcc 在入口处生成堆栈帧。
完整的代码是:
You can control the symbol that gcc gives to the function by declaring it separately with
asm
:Make sure this is a standalone declaration, separate from the function definition.
You can read more details about controlling the generated symbol here: https://stackoverflow.com/a/1035937/12928775
Also, as Peter Cordes points out, you should probably include
__attribute__((naked))
on the function definition to prevent gcc from generating a stack frame on entry.The full code would be:
当您希望入口点不是 start 时, gcc -e 选项定义入口点。这样您就可以创建 mystart() 作为入口点。
我不知道如何在 Xcode 中设置 -e 选项。
The gcc -e option defines the entry point, when you want the entry point to be something other than start. This way you can create mystart() as you entry point.
I do not know how to set the -e option in Xcode.