-nostdlib

When compiling with -nostdlib, one must understand that only main function he got. When main finishes it expects to return to its caller which would normally bet set in _start(), has not been set. When main returns, it will jump to an invalid address.

Exit is a library function that handles proper shutdown of a C program. _exit() is a syscall that tells the kernel to clean up its own data structures, and wipe away the process. It’s essentially eqivalent to this asm (on x86):

mov eax,1   ; 1 is the syscall number

mov ebx,0   ; 0 for "return 0"

int 80h   ; interrupt, trap to kernel routine

Here is the code and how to compile it:

int main()
{

__asm__("movl $1,%EAX;\n\t"
"movl $0,%EBX;\n\t"
"int $0x80;");

return 0;
}

#gcc -e main -nostdlib test.c
source

More on assembly: here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s