how sys_select, sys_fork, ... are defined for thread libraries?

Kostik Belousov kostikbel at gmail.com
Fri Aug 3 02:55:30 PDT 2007


On Thu, Aug 02, 2007 at 02:00:05PM -0700, Jin Guojun wrote:
> I am trying to understand how these __sys_xxx functions are defined for 
> thread libraries.
> Following string search tells that all thread libraries are using these 
> __sys_xxx functions,
> for example, __sys_select(). However, the search also shows that these 
> functions are not defined anywhere in the entire source tree.
> 
> /usr/src: findstring sys_select "*.[hcS]"
> total files= 21687 : pattern= sys_select rootdir= /usr/src
> regular mode: Thu Aug 2 13:31:40 PDT 2007
> ./lib/libc_r/uthread/uthread_fork.c
> 91:              * __sys_select:
> 
> ./lib/libpthread/thread/thr_private.h
> 1264:int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct 
> timeval *);
> 
> ./lib/libpthread/thread/thr_select.c
> 61:             ret = __sys_select(numfds, readfds, writefds, exceptfds, 
> timeout);
> 
> ./lib/libthr/thread/thr_private.h
> 805:int     __sys_select(int, fd_set *, fd_set *, fd_set *, struct 
> timeval *);
> 
> ./lib/libthr/thread/thr_syscalls.c
> 435:    ret = __sys_select(numfds, readfds, writefds, exceptfds, timeout);
> 
> 
> By searching the usr/lib objects, I found them in libc, but they are not 
> in libc source tree.
> Can someone shed some light on how these system calls are built into 
> libc and what is the
> different between standard syscall APIs and these __sys_syscall APIs,
> e.g., __sys_read() vs. read(), etc.
> 
> Thanks,
> 
> -Jin
> 
> 
> nm /usr/lib/libc.a | grep __sys_
> 00000008 T __sys_sigreturn
> 00000008 T __sys_setlogin
> 00000008 T __sys_reboot
> ... snapped
> 00000008 T __sys_kse_release
> 00000008 T __sys_kse_thr_interrupt
> 00000008 T __sys_kse_create
> 00000008 T __sys_kse_wakeup
> ... skipped
> 00000008 T __sys_getdtablesize
> 00000008 T __sys_select
> 00000008 T __sys_ioctl
> 00000008 T __sys_close
> 00000008 T __sys_write
> 00000008 T __sys_read
> 00000008 T __sys___syscall

The C standard specifies that namespace of identifiers starting with __ or
_<UPPERCASE> is reserved for implementation. On the other hand, C standard
allows for the programs to use any symbol not reserved by standard.

The thread libraries (and libc) shall internally use the "right"
implementation of syscalls, as opposed to some symbol supplied by user
binary.

To achieve this, for each syscall x, libc defines the normal symbol __sys_x,
and two weak symbols _x and x. See, for instance, the file lib/libc/select.S
from the obj directory, and lib/libc/i386/SYS.h for corresponding include
file.

Definition of weak symbols is provided by ELF specification. Simplyfing,
weakness of the symbol mean that it is used only unless somebody provides
the same normal symbol. Normal symbols from text segment are marked by "T"
in nm output, and weak symbols has "t" mark.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20070803/41690648/attachment.pgp


More information about the freebsd-questions mailing list