Compile and run program without main() in C

删除回忆录丶 提交于 2019-11-29 19:30:36
StoryTeller

Let's have a look at the generated assembly of your program:

.LC0:
        .string "Hello World..."
.LC1:
        .string "Successfully run without main..."
nomain:
        push    rbp
        mov     rbp, rsp
        mov     edi, OFFSET FLAT:.LC0
        call    puts
        mov     edi, OFFSET FLAT:.LC1
        call    puts
        nop
        pop     rbp
        ret

Note the ret statement. Your program's entry point is determined to be nomain, all is fine with that. But once the function returns, it attempts to jump into an address on the call stack... that isn't populated. That's an illegal access and a segmentation fault follows.

A quick solution would be to call exit() at the end of your program (and assuming C11 we might as well mark the function as _Noreturn):

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

_Noreturn void nomain(void)
{
    printf("Hello World...\n");
    printf("Successfully run without main...\n");
    exit(0);
}

In fact, now your function behaves pretty much like a regular main function, since after returning from main, the exit function is called with main's return value.

In C, when functions/subroutines are called the stack is populated as (in the order):

  1. The arguments,
  2. Return address,
  3. Local variables, --> top of the stack

main() being the start point, ELF structures the program in such a way that whatever instructions comes first would get pushed first, in this case printfs are.

Now, program is sort of truncated without return-address OR __end__ and infact it assumes that whatever is there on the stack at that(__end__) location is the return-address, but unfortunately its not and hence it crashes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!