如何在 Mac OS X 上构建包含入口点的 C 程序?

发布于 2024-10-05 20:32:47 字数 802 浏览 0 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2024-10-12 20:32:47

您可以通过使用 asm 单独声明来控制 gcc 为函数提供的符号:

void start() asm ("start");

确保这是一个独立的声明,与函数定义分开。

您可以在此处阅读有关控制生成符号的更多详细信息:https://stackoverflow.com/a/1035937/12928775

正如 Peter Cordes 指出的那样,您可能应该在函数定义中包含 __attribute__((naked)) 以防止 gcc 在入口处生成堆栈帧。

完整的代码是:

void start() asm ("start");

__attribute__((naked)) void start() {
    /* exit system call */
    asm("movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );
}

You can control the symbol that gcc gives to the function by declaring it separately with asm:

void start() asm ("start");

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:

void start() asm ("start");

__attribute__((naked)) void start() {
    /* exit system call */
    asm("movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );
}
断桥再见 2024-10-12 20:32:47

当您希望入口点不是 start 时, gcc -e 选项定义入口点。这样您就可以创建 mystart() 作为入口点。

gcc -e mystart mycode.c -o mycode

我不知道如何在 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.

gcc -e mystart mycode.c -o mycode

I do not know how to set the -e option in Xcode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文