3

Possible Duplicate:
Need help with glibc source

I understand how to implement our own system calls in linux kernel. I know we can call this with syscall() or with _asm() in a c program.

  1. But I want to understand how to write glibc api for this new system call?.

  2. How the open() and read() glibc function calls mapping into system call in kernel?.

    char      message[ ] = "Hello!\n";
    
    int main( void )
    {
            write( 1, message, 7 );
            exit( 0 );
    }
    

When I convert the above program into assembly it is giving

main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $20, %esp
    movl    $7, 8(%esp)
    movl    $message, 4(%esp)
    movl    $1, (%esp)
    call    write
    movl    $0, (%esp)
    call    exit
    .size   main, .-main
    .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
    .section        .note.GNU-stack,"",@progbits

~

3, In "call write" I think write is glibc call here ?. what happens after that? how it maps the glibc call to system call?

Community
  • 1
  • 1
user1694118
  • 89
  • 1
  • 2
  • 4

1 Answers1

2

See e.g. this answer and that answer to similar questions. Read also more about syscalls, the linux kernel, an overview of linux syscalls, and the assembly howto

The write function from glibc is not the true syscall. It is a wrapper (doing the sycall thru e.g. sysenter machine instruction, perhaps with the VDSO, and setting errno). You can use strace to understand the system calls done by some program.

For example, the MUSL libc has this write.c implementation for write. For GNU libc, look at its write.c.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Basile, Thanks for the quick reply. I understand how it is working in kernel. I could not get glibc mapping to system call where it is generating interrupt . see in your example in MUSL libc in write.c i see "return syscall_cp(SYS_write, fd, buf, count);" . I guess this is similar to syscall() in glibc. But, I am not finding in glibc write() definition where it is sending int $0x80. Thanks Again – user1694118 Sep 24 '12 at 11:12
  • I guess that glibc is not doing directly an `int 80` it is using the VDSO, as mentioned in my answer. – Basile Starynkevitch Sep 24 '12 at 11:32
  • 6
    I think the glibc code isn't the correct implementation of `write()`; instead it's a stub that gets inserted when `write()` isn't available. But I couldn't find the correct implementation, either :-/ See this question: http://stackoverflow.com/questions/6515583/need-help-with-glibc-source – Aaron Digulla Sep 24 '12 at 12:45