suClick
suClick

Reputation: 996

Let the gcc compile syscalls in "int 0x80" way?

code here:

void main()
{
    _exit(0);
}

By disassembling the main section:

 80483d4:   55                      push   %ebp
 80483d5:   89 e5                   mov    %esp,%ebp
 80483d7:   83 e4 f0                and    $0xfffffff0,%esp
 80483da:   83 ec 10                sub    $0x10,%esp
 80483dd:   c7 04 24 00 00 00 00    movl   $0x0,(%esp)
 80483e4:   e8 17 ff ff ff          call   8048300 <_exit@plt>

As I know, the way to make syscalls is using "int 0x80", but I can just find "call 8048300 exit@plt" here, so how can I change the gcc to let it compile syscalls in "int 0x80" way(I need my program call syscall in this way)?

Upvotes: 1

Views: 2765

Answers (3)

You should compile with gcc -Wall (and also perhaps the -g flag).

This will give you more warnings, and will pin-point some easy mistakes, like the lack of appropriate #include

The exit(3) function is a library function (making atexit possible). The corresponding syscall is _exit(2), but on recent Linux exit is calling exit_group(2). So your example misses an #include <stdlib.h>

Current Linux implementations often do not use int 0x80 but go thru the VDSO or at least use SYSENTER or SYSCALL machine instructions. YMMV.

You could, as Jeremy answered, use asm ; (you might define your own headers for all the syscalls you are using, and having these syscalls be static inline functions doing some asm) beware that for other syscalls, you want to catch their failure and the errno error code.

Why are you asking?.... The libc (and its startup routines crt0.o ...) is doing complex tricks to call main...

See also this answer

Upvotes: 2

vonbrand
vonbrand

Reputation: 11791

The system calls are wrapped by glibc, which should use whatever the underlying kernel uses. Linux switched away from the int 0x80 mechanism for performance a while back...

Why do you want to do system calls in an outdated way?

Upvotes: 1

Jeremy
Jeremy

Reputation: 314

For 32-bit

asm( "int $0x80" :: "a" (1), "b" (0) );

for 64-bit

asm( "syscall" :: "a" (60), "D" (0) );

You might also need this if you have declared your exit function with attribute noreturn.

__builtin_unreachable();

Upvotes: 2

Related Questions