1*adfa6ca8Sriastradh $NetBSD: README,v 1.7 2017/02/08 13:31:36 riastradh Exp $ 226ef86fdSriastradh 326ef86fdSriastradhlibc: The C library. 426ef86fdSriastradh 526ef86fdSriastradh* ELF symbols and source names 626ef86fdSriastradh 726ef86fdSriastradhlibc contains symbols for: 826ef86fdSriastradh 926ef86fdSriastradh(a) standard library routines in C and POSIX, 1026ef86fdSriastradh(b) published NetBSD-specific nonstandard extensions, 11278ee21aSriastradh(c) internal symbols, and 12278ee21aSriastradh(d) old versions of any published library routines. 13278ee21aSriastradh 14278ee21aSriastradh** Standard library routines 1526ef86fdSriastradh 1626ef86fdSriastradhIf a library routine is standard and its signature has never changed, 17d06c586aSriastradhit is provided as an ELF global symbol. Its name is declared normally 1826ef86fdSriastradhin the appropriate header file. 1926ef86fdSriastradh 20d06c586aSriastradh=> Example: The names `malloc' and `free' are declared normally in 21d06c586aSriastradh <stdlib.h> (src/include/stdlib.h): 22d06c586aSriastradh 23d06c586aSriastradh void *malloc(size_t); 24d06c586aSriastradh void free(void *); 25d06c586aSriastradh 26d06c586aSriastradh libc provides the following ELF symbols: 27d06c586aSriastradh 28d06c586aSriastradh malloc global 29d06c586aSriastradh free global 30d06c586aSriastradh 31d06c586aSriastradh In the implementation of libc, malloc and free are defined normally 32d06c586aSriastradh in src/lib/libc/stdlib/jemalloc.c: 33d06c586aSriastradh 34d06c586aSriastradh void * 35d06c586aSriastradh malloc(size_t size) 36d06c586aSriastradh { 37d06c586aSriastradh ... 38d06c586aSriastradh 39d06c586aSriastradh void 40d06c586aSriastradh free(void *ptr) 41d06c586aSriastradh { 42d06c586aSriastradh ... 4326ef86fdSriastradh 44278ee21aSriastradh** NetBSD-specific nonstandard extensions 45278ee21aSriastradh 4626ef86fdSriastradhIf a library routine is nonstandard but published and its signature has 47d06c586aSriastradhnever changed, it is provided as an ELF weak symbol aliasing an ELF 4826ef86fdSriastradhglobal symbol of the same name with an underscore prefix. 4926ef86fdSriastradh 5026ef86fdSriastradhThe name is declared normally in the appropriate header file, provided 5126ef86fdSriastradhthat the relevant feature macro, such as _NETBSD_SOURCE, is defined. 5226ef86fdSriastradh 5326ef86fdSriastradhWithin libc, the name is defined in "namespace.h" 5426ef86fdSriastradh(src/lib/libc/include/namespace.h) as a macro expanding to the 55d06c586aSriastradhunderscored name, which is included before the relevant header file, so 56d06c586aSriastradhthat 57d06c586aSriastradh 58d06c586aSriastradh(a) the definition in a .c file will define the underscored ELF global 59d06c586aSriastradhsymbol, and 60d06c586aSriastradh 61d06c586aSriastradh(b) the declaration in the standard header file will match the 62d06c586aSriastradhdefinition in the .c file. 6326ef86fdSriastradh 6426ef86fdSriastradhAlongside the definition in the .c file is a __weak_alias directive to 6526ef86fdSriastradhcreate the ELF weak symbol alias. 6626ef86fdSriastradh 67d06c586aSriastradh=> Example: For the nonstandard extension consttime_memequal, the 68d06c586aSriastradh header file <string.h> (src/include/string.h) declares 69d06c586aSriastradh `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE: 7026ef86fdSriastradh 71d06c586aSriastradh #if defined(_NETBSD_SOURCE) 72d06c586aSriastradh ... 73d06c586aSriastradh int consttime_memequal(const void *, const void *, size_t); 74d06c586aSriastradh ... 75d06c586aSriastradh #endif /* _NETBSD_SOURCE */ 7626ef86fdSriastradh 77d06c586aSriastradh libc provides the following ELF symbols: 78d06c586aSriastradh 79d06c586aSriastradh _consttime_memequal global 80d06c586aSriastradh consttime_memequal weak alias for _consttime_memequal 81d06c586aSriastradh 82d06c586aSriastradh In the implementation of libc, the header file "namespace.h" 83d06c586aSriastradh (src/lib/libc/include/namespace.h) defines `consttime_memequal' as a 84d06c586aSriastradh macro expanding to `_consttime_memequal': 85d06c586aSriastradh 86d06c586aSriastradh #define consttime_memequal _consttime_memequal 87f97ad6c0Sriastradh 88f97ad6c0Sriastradh The source file src/common/lib/libc/string/consttime_memequal.c 89f97ad6c0Sriastradh includes "namespace.h" and <string.h>, and defines 90d06c586aSriastradh `consttime_memequal' normally: 9126ef86fdSriastradh 92d06c586aSriastradh int 93d06c586aSriastradh consttime_memequal(const void *b1, const void *b2, size_t len) 94d06c586aSriastradh { 95d06c586aSriastradh ... 96d06c586aSriastradh 97d06c586aSriastradh Macro expansion replaces `consttime_memequal' by 98d06c586aSriastradh `_consttime_memequal', which is the ELF global symbol this defines. 9926ef86fdSriastradh Alongside the definition is 10026ef86fdSriastradh 10126ef86fdSriastradh __weak_alias(consttime_memequal,_consttime_memequal) 10226ef86fdSriastradh 10326ef86fdSriastradh to provide `consttime_memequal' as an ELF weak symbol aliasing 10426ef86fdSriastradh `_consttime_memequal'. 10526ef86fdSriastradh 106278ee21aSriastradh** Internal symbols 107278ee21aSriastradh 10826ef86fdSriastradhIf a library routine is internal to libc, it is defined as an ELF 109934b3616Sriastradhglobal symbol with an underscore prefix. Its name is declared in the 110934b3616Sriastradhappropriate internal header file. 11126ef86fdSriastradh 112d06c586aSriastradh=> Example: The implementations of opendir and rewinddir use a common 113d06c586aSriastradh subroutine _initdir, which is not part of the libc API or ABI -- it 114d06c586aSriastradh is just an internal subroutine. 115d06c586aSriastradh 116d06c586aSriastradh libc provides the following ELF symbols: 117d06c586aSriastradh 118d06c586aSriastradh _initdir global 119d06c586aSriastradh 120d06c586aSriastradh The name `_initdir' is declared normally in 121d06c586aSriastradh src/lib/libc/gen/dirent_private.h: 122d06c586aSriastradh 123d06c586aSriastradh int _initdir(DIR *, int, const char *); 124d06c586aSriastradh 125d06c586aSriastradh The name `_initdir' is defined normally in 126d06c586aSriastradh src/lib/libc/gen/initdir.c: 127d06c586aSriastradh 128d06c586aSriastradh int 129d06c586aSriastradh _initdir(DIR *dirp, int fd, const char *name) 130d06c586aSriastradh { 131d06c586aSriastradh ... 13226ef86fdSriastradh 133278ee21aSriastradh** Old versions of library routines 134278ee21aSriastradh 13526ef86fdSriastradhIf the signature or semantics of a library routine foo changed in (for 13626ef86fdSriastradhexample) NetBSD 6.0, then libc provides 13726ef86fdSriastradh 13826ef86fdSriastradh(1) an ELF global symbol `_foo' implementing its old signature, 13926ef86fdSriastradh(2) an ELF weak symbol `foo' aliasing `_foo', and 14026ef86fdSriastradh(3) an ELF global symbol `__foo50' implementing its new signature (yes, 14126ef86fdSriastradh `__foo50', not `__foo60'). 14226ef86fdSriastradh 14326ef86fdSriastradhThe name foo is declared in the appropriate header file, under any 14426ef86fdSriastradhrelevant feature macros, with a __RENAME directive so that for calls to 14526ef86fdSriastradhfoo, the compiler will generate relocations for __foo50. Old programs, 14626ef86fdSriastradhcompiled with the old signature, will continue to use the old symbol. 14726ef86fdSriastradh 14826ef86fdSriastradh=> Example: In NetBSD 5.0, time_t was int32_t on every machine. In 14926ef86fdSriastradh NetBSD 6.0 and onward, time_t is int64_t on every machine. 15026ef86fdSriastradh Consequently, the signature of time(3), written as 15126ef86fdSriastradh 15226ef86fdSriastradh time_t time(time_t *); 15326ef86fdSriastradh 154d06c586aSriastradh was effectively 15526ef86fdSriastradh 15626ef86fdSriastradh int32_t time(int32_t *); 15726ef86fdSriastradh 158d06c586aSriastradh before NetBSD 6.0. In NetBSD 6.0, it changed to be effectively 15926ef86fdSriastradh 16026ef86fdSriastradh int64_t time(int64_t *); 16126ef86fdSriastradh 162d06c586aSriastradh Before NetBSD 6.0, libc provided the following libc symbols: 16326ef86fdSriastradh 164d06c586aSriastradh _time global (implementing the old signature) 165d06c586aSriastradh time weak alias for _time 166d06c586aSriastradh 167d06c586aSriastradh In NetBSD 6.0 and later, libc provides the following ELF symbols: 168d06c586aSriastradh 169d06c586aSriastradh _time global (implementing the old signature) 170d06c586aSriastradh time weak alias for _time 171d06c586aSriastradh __time50 global (implementing the new signature) 172d06c586aSriastradh 173d06c586aSriastradh (Note that the only change is to add __time50, so that existing 174d06c586aSriastradh programs linked against old versions of libc will see the same 175d06c586aSriastradh semantics for the symbols that were already there.) 17626ef86fdSriastradh 177934b3616Sriastradh The header file <time.h> (src/include/time.h) declares 17826ef86fdSriastradh 17926ef86fdSriastradh time_t time(time_t *) __RENAME(__time50); 18026ef86fdSriastradh 18126ef86fdSriastradh so that compiling C programs that call time will yield objects that 18226ef86fdSriastradh use the __time50 symbol from libc. However, old programs that were 18326ef86fdSriastradh compiled against the 32-bit declaration will continue to use the 18426ef86fdSriastradh 32-bit symbol from libc. 185934b3616Sriastradh 186934b3616Sriastradh The header file "namespace.h" (src/lib/libc/include/namespace.h) 187d06c586aSriastradh defines `time' as a macro expanding to `_time': 188d06c586aSriastradh 189d06c586aSriastradh #define time _time 190934b3616Sriastradh 191934b3616Sriastradh The source file src/lib/libc/gen/time.c includes "namespace.h" and 192d06c586aSriastradh <time.h> and defines `time' normally: 193d06c586aSriastradh 194d06c586aSriastradh time_t 195d06c586aSriastradh time(time_t *t) 196d06c586aSriastradh { 197d06c586aSriastradh ... 198d06c586aSriastradh 199d06c586aSriastradh Macro expansion replaces `time' by `_time', but the 200d06c586aSriastradh `__RENAME(__time50)' directive on the declaration <time.h> (to which 201d06c586aSriastradh the "namespace.h" macro expansion also applies) means the ELF global 202d06c586aSriastradh symbol defined here is actually `__time50'. 203934b3616Sriastradh 204934b3616Sriastradh The header file <compat/include/time.h> 205934b3616Sriastradh (src/lib/libc/compat/include/time.h) declares 206934b3616Sriastradh 207934b3616Sriastradh int32_t time(int32_t *); 208934b3616Sriastradh 209934b3616Sriastradh The source file src/lib/libc/compat/gen/compat_time.c includes 210934b3616Sriastradh "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses 211934b3616Sriastradh the normal declaration of `time' in <time.h> by defining 212*adfa6ca8Sriastradh __LIBC12_SOURCE__ and thus gets it from <compat/include/time.h> 213*adfa6ca8Sriastradh instead. Then compat_time.c defines `time' normally: 214d06c586aSriastradh 2150add037fSriastradh int32_t 2160add037fSriastradh time(int32_t *t) 217d06c586aSriastradh { 218d06c586aSriastradh ... 219d06c586aSriastradh 220d06c586aSriastradh Again, macro expansion replaces `time' by `_time', but since there 221d06c586aSriastradh is no __RENAME directive in <compat/include/time.h>, the resulting 2220add037fSriastradh ELF global symbol is `_time'. (Actually, compat_time.c just has 2230add037fSriastradh `#define time_t int32_t' and `#include "gen/time.c"' to get the same 2240add037fSriastradh text of the definition of time. The above definition is what we get 2250add037fSriastradh effectively by substituting int32_t for the type time_t.) 226934b3616Sriastradh 227934b3616Sriastradh Finally, alongside the definition in compat_time.c is 228934b3616Sriastradh 229934b3616Sriastradh __weak_alias(time,_time) 230934b3616Sriastradh 231d06c586aSriastradh to define `time' as an ELF weak symbol aliasing `_time'. 232934b3616Sriastradh 233934b3616Sriastradh The net effect is that NetBSD 6's libc provides the same definitions 234934b3616Sriastradh as NetBSD 5's libc for the symbols `time' and `_time', so that old 235934b3616Sriastradh programs that were compiled in NetBSD 5 will continue to work with 236934b3616Sriastradh NetBSD 6's libc. But programs compiled in NetBSD 6 will have 64-bit 237934b3616Sriastradh time_t. 238