Where is the source to the system calls?

Dag-Erling Smørgrav des at des.no
Mon Nov 8 17:20:29 PST 2004


Dan Strick <strick at covad.net> writes:
> Thanks for the pointer to /usr/src/lib/libc/i386/SYS.h.  It contains
> precisely the "secret macro instructions invoking undocumented gnu
> C-compiler asm() features" that I suspected but could not find.
>
> I still don't understand all the details but I do understand enough
> to realize that I don't want to understand any more.

I don't see what's so hard to understand:

#define SYSCALL(x)      2: PIC_PROLOGUE; jmp PIC_PLT(HIDENAME(cerror)); \
                        ENTRY(__CONCAT(__sys_,x));                      \
                        .weak CNAME(x);                                 \
                        .set CNAME(x),CNAME(__CONCAT(__sys_,x));        \
                        .weak CNAME(__CONCAT(_,x));                     \
                        .set CNAME(__CONCAT(_,x)),CNAME(__CONCAT(__sys_,x)); \
                        mov __CONCAT($SYS_,x),%eax; KERNCALL; jb 2b

the important part is the beginning of the last line, which for open()
corresponds to:

        mov SYS_open, %eax
        int $0x80

(where SYS_open is the syscall number for open(), normally 5)

the rest is just error handling ('jb 2b' jumps back to the 'jmp
cerror' if the system call returns an error) and namespace management
(the .weak and .set stuff create two weak aliases, _open and open, for
the real syscall name which is __sys_open)

most of the complexity comes from the use of macros to hide
differences between relocatable and non-relocatable code so that the
same definition can be used for both.

DES
-- 
Dag-Erling Smørgrav - des at des.no


More information about the freebsd-hackers mailing list