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?

4

1 回答 1

2

参见例如这个答案和类似问题的那个答案。阅读更多关于系统调用linux内核、 linux系统调用概述和汇编方法的信息。

glibc 中的write函数不是真正的系统调用。sysenter它是一个包装器(通过例如机器指令执行 sycall ,可能使用VDSO和设置errno)。您可以使用它strace来了解某些程序所做的系统调用。

例如,MUSL libc有这个write.c实现,用于write. 对于 GNU libc,请查看它的write.c

于 2012-09-24T10:35:14.757 回答