Home | History | Annotate | Line # | Download | only in libc
README revision 1.3
      1  1.3  riastrad 	$NetBSD: README,v 1.3 2015/03/20 14:10:40 riastradh Exp $
      2  1.1  riastrad 
      3  1.1  riastrad libc: The C library.
      4  1.1  riastrad 
      5  1.1  riastrad * ELF symbols and source names
      6  1.1  riastrad 
      7  1.1  riastrad libc contains symbols for:
      8  1.1  riastrad 
      9  1.1  riastrad (a) standard library routines in C and POSIX,
     10  1.1  riastrad (b) published NetBSD-specific nonstandard extensions,
     11  1.1  riastrad (c) old versions of library routines, and
     12  1.1  riastrad (d) internal symbols.
     13  1.1  riastrad 
     14  1.1  riastrad If a library routine is standard and its signature has never changed,
     15  1.1  riastrad it is defined as an ELF global symbol.  Its name is declared normally
     16  1.1  riastrad in the appropriate header file.
     17  1.1  riastrad 
     18  1.1  riastrad => Example: libc defines global symbols `malloc' and `free' for the
     19  1.1  riastrad    standard C memory allocator routines.  The names `malloc' and `free'
     20  1.1  riastrad    are declared normally in <stdlib.h> (src/include/stdlib.h).
     21  1.1  riastrad 
     22  1.1  riastrad If a library routine is nonstandard but published and its signature has
     23  1.1  riastrad never changed, it is defined as an ELF weak symbol aliasing an ELF
     24  1.1  riastrad global symbol of the same name with an underscore prefix.
     25  1.1  riastrad 
     26  1.1  riastrad The name is declared normally in the appropriate header file, provided
     27  1.1  riastrad that the relevant feature macro, such as _NETBSD_SOURCE, is defined.
     28  1.1  riastrad 
     29  1.1  riastrad Within libc, the name is defined in "namespace.h"
     30  1.1  riastrad (src/lib/libc/include/namespace.h) as a macro expanding to the
     31  1.1  riastrad underscored name, so that the definition in a .c file will define the
     32  1.1  riastrad underscored ELF global symbol.
     33  1.1  riastrad 
     34  1.1  riastrad Alongside the definition in the .c file is a __weak_alias directive to
     35  1.1  riastrad create the ELF weak symbol alias.
     36  1.1  riastrad 
     37  1.1  riastrad => Example: For the nonstandard extension consttime_memequal, libc
     38  1.1  riastrad    defines a weak symbol `consttime_memequal' aliasing a global symbol
     39  1.1  riastrad    `_consttime_memequal'.
     40  1.1  riastrad 
     41  1.3  riastrad    The header file <string.h> (src/include/string.h) declares
     42  1.3  riastrad    `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE.
     43  1.1  riastrad 
     44  1.3  riastrad    The header file "namespace.h" (src/lib/libc/include/namespace.h)
     45  1.3  riastrad    defines `consttime_memequal' as a macro expanding to
     46  1.3  riastrad    `_consttime_memequal'.
     47  1.3  riastrad 
     48  1.3  riastrad    The source file src/common/lib/libc/string/consttime_memequal.c
     49  1.3  riastrad    includes "namespace.h" and <string.h>, and defines
     50  1.3  riastrad    `consttime_memequal' normally, which, after macro expansion, causes
     51  1.3  riastrad    the ELF global symbol `_consttime_memequal' to be defined.
     52  1.1  riastrad 
     53  1.1  riastrad    Alongside the definition is
     54  1.1  riastrad 
     55  1.1  riastrad 	__weak_alias(consttime_memequal,_consttime_memequal)
     56  1.1  riastrad 
     57  1.1  riastrad    to provide `consttime_memequal' as an ELF weak symbol aliasing
     58  1.1  riastrad    `_consttime_memequal'.
     59  1.1  riastrad 
     60  1.1  riastrad If a library routine is internal to libc, it is defined as an ELF
     61  1.2  riastrad global symbol with an underscore prefix.  Its name is declared in the
     62  1.2  riastrad appropriate internal header file.
     63  1.1  riastrad 
     64  1.1  riastrad => Example: For the internal library routine _initdir, used by the
     65  1.1  riastrad    implementations of opendir and rewinddir, libc defines a global
     66  1.2  riastrad    symbol `_initdir'.  The name `_initdir' is declared normally in
     67  1.2  riastrad    src/lib/libc/gen/dirent_private.h, and defined normally in
     68  1.2  riastrad    src/lib/libc/gen/initdir.c.
     69  1.1  riastrad 
     70  1.1  riastrad If the signature or semantics of a library routine foo changed in (for
     71  1.1  riastrad example) NetBSD 6.0, then libc provides
     72  1.1  riastrad 
     73  1.1  riastrad (1) an ELF global symbol `_foo' implementing its old signature,
     74  1.1  riastrad (2) an ELF weak symbol `foo' aliasing `_foo', and
     75  1.1  riastrad (3) an ELF global symbol `__foo50' implementing its new signature (yes,
     76  1.1  riastrad     `__foo50', not `__foo60').
     77  1.1  riastrad 
     78  1.1  riastrad The name foo is declared in the appropriate header file, under any
     79  1.1  riastrad relevant feature macros, with a __RENAME directive so that for calls to
     80  1.1  riastrad foo, the compiler will generate relocations for __foo50.  Old programs,
     81  1.1  riastrad compiled with the old signature, will continue to use the old symbol.
     82  1.1  riastrad 
     83  1.1  riastrad => Example: In NetBSD 5.0, time_t was int32_t on every machine.  In
     84  1.1  riastrad    NetBSD 6.0 and onward, time_t is int64_t on every machine.
     85  1.1  riastrad    Consequently, the signature of time(3), written as
     86  1.1  riastrad 
     87  1.1  riastrad 	time_t time(time_t *);
     88  1.1  riastrad 
     89  1.1  riastrad    changed in NetBSD 6.0 from being effectively
     90  1.1  riastrad 
     91  1.1  riastrad 	int32_t time(int32_t *);
     92  1.1  riastrad 
     93  1.1  riastrad    to being effectively
     94  1.1  riastrad 
     95  1.1  riastrad 	int64_t time(int64_t *);
     96  1.1  riastrad 
     97  1.1  riastrad    Thus, libc provides
     98  1.1  riastrad 
     99  1.1  riastrad    (1) the ELF global symbol `_time' implementing the old signature,
    100  1.1  riastrad    (2) the ELF weak symbol `time' aliasing `_time', and
    101  1.1  riastrad    (3) the ELF global symbol `__time50' implementing the new signature.
    102  1.1  riastrad 
    103  1.2  riastrad    The header file <time.h> (src/include/time.h) declares
    104  1.1  riastrad 
    105  1.1  riastrad 	time_t time(time_t *) __RENAME(__time50);
    106  1.1  riastrad 
    107  1.1  riastrad    so that compiling C programs that call time will yield objects that
    108  1.1  riastrad    use the __time50 symbol from libc.  However, old programs that were
    109  1.1  riastrad    compiled against the 32-bit declaration will continue to use the
    110  1.1  riastrad    32-bit symbol from libc.
    111  1.2  riastrad 
    112  1.2  riastrad    The header file "namespace.h" (src/lib/libc/include/namespace.h)
    113  1.2  riastrad    defines `time' as a macro expanding to `_time'.
    114  1.2  riastrad 
    115  1.2  riastrad    The source file src/lib/libc/gen/time.c includes "namespace.h" and
    116  1.2  riastrad    <time.h> and defines `time' normally.  The declaration of `time' in
    117  1.2  riastrad    <time.h> is replaced after macro expansion by a declaration of
    118  1.2  riastrad    `_time', and the definition in time.c is replaced by a definition of
    119  1.2  riastrad    `_time'.  But the __RENAME directive causes the resulting ELF global
    120  1.2  riastrad    symbol to be `__time50'.
    121  1.2  riastrad 
    122  1.2  riastrad    The header file <compat/include/time.h>
    123  1.2  riastrad    (src/lib/libc/compat/include/time.h) declares
    124  1.2  riastrad 
    125  1.2  riastrad 	int32_t time(int32_t *);
    126  1.2  riastrad 
    127  1.2  riastrad    The source file src/lib/libc/compat/gen/compat_time.c includes
    128  1.2  riastrad    "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses
    129  1.2  riastrad    the normal declaration of `time' in <time.h> by defining
    130  1.2  riastrad    __LIBC12_SOURCE__.  Then compat_time.c defines `time' normally.
    131  1.2  riastrad    Again, the name is replaced after macro expansion by `_time', but
    132  1.2  riastrad    since there is no __RENAME directive in <compat/include/time.h>, the
    133  1.2  riastrad    resulting ELF global symbol is `_time'.
    134  1.2  riastrad 
    135  1.2  riastrad    Finally, alongside the definition in compat_time.c is
    136  1.2  riastrad 
    137  1.2  riastrad 	__weak_alias(time,_time)
    138  1.2  riastrad 
    139  1.2  riastrad    to provide `time' as an ELF weak symbol aliasing `_time'.
    140  1.2  riastrad 
    141  1.2  riastrad    The net effect is that NetBSD 6's libc provides the same definitions
    142  1.2  riastrad    as NetBSD 5's libc for the symbols `time' and `_time', so that old
    143  1.2  riastrad    programs that were compiled in NetBSD 5 will continue to work with
    144  1.2  riastrad    NetBSD 6's libc.  But programs compiled in NetBSD 6 will have 64-bit
    145  1.2  riastrad    time_t.
    146