为什么C运行时不调用我的exit()?

Dev*_*lar 3 c runtime crt

C标准指出...

...从初始调用返回到main函数等同于exit以主函数返回的值作为参数来调用函数。

从我的角度来看,这通常是通过C运行时支持代码(crt0.c)来实现的,方法就是精确地做到这一点-调用exitfrom中的返回值main

glibc

result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
exit (result);
Run Code Online (Sandbox Code Playgroud)

尤利西斯·利比Ulysses Libc)

exit(main(argc, argv, envp));
Run Code Online (Sandbox Code Playgroud)

但是,当我编写自己的版本时exit不会被调用:

#include <stdio.h>
#include <stdlib.h>

void exit( int rc )
{
    puts( "ok" );
    fflush( stdout );
}

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这不会产生我期望的“确定”输出。显然我在这里缺少什么?


上下文:我正在实现一个C标准库,仅是ISO部分,即没有crt0.c。我希望现有的系统运行时可以调用我自己的exit实现,因此“我的”清理(如刷新和关闭流,处理向其注册的函数atexit等)将在从main链接到我的库的返回时自动运行。显然不是这样,我只是不明白为什么不这样做。

Aja*_*iya 5

如果我理解正确,则您在尝试使用C运行时的某些部分(即调用主函数并退出的部分)时,尝试在C标准库中实现这些功能。

通常执行此操作的代码部分是_start函数。这通常是使用Linux加载程序的ELF二进制文件的入口点。

_start函数在编译器正在使用的C运行时中定义,并且退出调用已链接(地址已修补到调用站点中)。它很可能只是内联到_start函数中。

要获取_start调用您的函数exit,您需要做的是重新定义_start自身。然后,您必须确保_start未使用C运行时。

我会选择这样的东西-

// Assuming your have included files that declare the puts and fflush functions and the stdout macro. 
int main(int argc, char* argv[]); // This is here just so that there is a declaration before the call
void _start(void) {
    char *argv[] = {"executable"}; // There is a way to get the real arguments, but I think you will have to write some assembly for that. 

    int return_value = main(1, argv);
    exit(return_value);
    // Control should NEVER reach here, because you cannot return from the _start function
}

void exit(int ret) {
    puts("ok"); 
    fflush(stdout); // Assuming your runtime defines these functions and stdout somewhere. 
    // To simulate an exit, we will just spin infinitely - 
    while(1);
}

int main(int argc, char* argv[]) {
    puts("hello world\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以将文件编译为-

gcc test.c -o executable -nostdlib
Run Code Online (Sandbox Code Playgroud)

-nostdlib通知不反对其具有的执行标准运行时链接的链接_start

现在,您可以执行可执行文件,它将按预期方式调用“退出”,然后将永远循环下去。您可以按ctrl + c或以其他方式发送SIGKILL杀死它。

附录

为了完整起见,我还尝试写下其余功能的实现。

您可以首先在代码顶部添加以下声明和定义。

#define stdout (1)
int puts(char *s);
long unsigned int strlen(const char *s) {
        int len = 0;
        while (s[len])
                len++;
        return len;
}
int fflush(int s) {
}
void exit(int n);
Run Code Online (Sandbox Code Playgroud)

strlen被定义为预期的并且fflush是空操作,因为我们没有为stdio函数实现缓冲。

现在在一个单独的文件puts.s中编写以下程序集(假设x64 linux。如果您的平台不同,请更改syscall号和参数)。

        .text
        .globl puts
puts:
        movq    %rdi, %r12
        callq    strlen
        movq    $1, %rdi
        movq    %r12, %rsi
        movq    %rax, %rdx
        movq    $1, %rax
        syscall
        retq
Run Code Online (Sandbox Code Playgroud)

这是最简单的实现puts,先调用strlen函数,然后再写syscall。

您现在可以将所有内容编译和链接为-

gcc test.c put.s -o executable -nostdlib
Run Code Online (Sandbox Code Playgroud)

运行产生的文件时executable,得到以下输出-

hello world
ok
Run Code Online (Sandbox Code Playgroud)

然后该过程挂起。我可以按ctrl + c杀死它。