Compiling without libc

后端 未结 2 616
陌清茗
陌清茗 2020-11-27 10:40

I want to compile my C-code without the (g)libc. How can I deactivate it and which functions depend on it?

I tried -nostdlib but it doesn\'t help: The code is compil

2条回答
  •  佛祖请我去吃肉
    2020-11-27 11:36

    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().

    Try compiling this with gcc -nostdlib -m32:

    void _start() {
    
        /* main body of program: call main(), etc */
    
        /* exit system call */
        asm("movl $1,%eax;"
            "xorl %ebx,%ebx;"
            "int  $0x80"
        );
    }
    

    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.

提交回复
热议问题