README revision 1.3
11.3Sriastrad $NetBSD: README,v 1.3 2015/03/20 14:10:40 riastradh Exp $ 21.1Sriastrad 31.1Sriastradlibc: The C library. 41.1Sriastrad 51.1Sriastrad* ELF symbols and source names 61.1Sriastrad 71.1Sriastradlibc contains symbols for: 81.1Sriastrad 91.1Sriastrad(a) standard library routines in C and POSIX, 101.1Sriastrad(b) published NetBSD-specific nonstandard extensions, 111.1Sriastrad(c) old versions of library routines, and 121.1Sriastrad(d) internal symbols. 131.1Sriastrad 141.1SriastradIf a library routine is standard and its signature has never changed, 151.1Sriastradit is defined as an ELF global symbol. Its name is declared normally 161.1Sriastradin the appropriate header file. 171.1Sriastrad 181.1Sriastrad=> Example: libc defines global symbols `malloc' and `free' for the 191.1Sriastrad standard C memory allocator routines. The names `malloc' and `free' 201.1Sriastrad are declared normally in <stdlib.h> (src/include/stdlib.h). 211.1Sriastrad 221.1SriastradIf a library routine is nonstandard but published and its signature has 231.1Sriastradnever changed, it is defined as an ELF weak symbol aliasing an ELF 241.1Sriastradglobal symbol of the same name with an underscore prefix. 251.1Sriastrad 261.1SriastradThe name is declared normally in the appropriate header file, provided 271.1Sriastradthat the relevant feature macro, such as _NETBSD_SOURCE, is defined. 281.1Sriastrad 291.1SriastradWithin libc, the name is defined in "namespace.h" 301.1Sriastrad(src/lib/libc/include/namespace.h) as a macro expanding to the 311.1Sriastradunderscored name, so that the definition in a .c file will define the 321.1Sriastradunderscored ELF global symbol. 331.1Sriastrad 341.1SriastradAlongside the definition in the .c file is a __weak_alias directive to 351.1Sriastradcreate the ELF weak symbol alias. 361.1Sriastrad 371.1Sriastrad=> Example: For the nonstandard extension consttime_memequal, libc 381.1Sriastrad defines a weak symbol `consttime_memequal' aliasing a global symbol 391.1Sriastrad `_consttime_memequal'. 401.1Sriastrad 411.3Sriastrad The header file <string.h> (src/include/string.h) declares 421.3Sriastrad `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE. 431.1Sriastrad 441.3Sriastrad The header file "namespace.h" (src/lib/libc/include/namespace.h) 451.3Sriastrad defines `consttime_memequal' as a macro expanding to 461.3Sriastrad `_consttime_memequal'. 471.3Sriastrad 481.3Sriastrad The source file src/common/lib/libc/string/consttime_memequal.c 491.3Sriastrad includes "namespace.h" and <string.h>, and defines 501.3Sriastrad `consttime_memequal' normally, which, after macro expansion, causes 511.3Sriastrad the ELF global symbol `_consttime_memequal' to be defined. 521.1Sriastrad 531.1Sriastrad Alongside the definition is 541.1Sriastrad 551.1Sriastrad __weak_alias(consttime_memequal,_consttime_memequal) 561.1Sriastrad 571.1Sriastrad to provide `consttime_memequal' as an ELF weak symbol aliasing 581.1Sriastrad `_consttime_memequal'. 591.1Sriastrad 601.1SriastradIf a library routine is internal to libc, it is defined as an ELF 611.2Sriastradglobal symbol with an underscore prefix. Its name is declared in the 621.2Sriastradappropriate internal header file. 631.1Sriastrad 641.1Sriastrad=> Example: For the internal library routine _initdir, used by the 651.1Sriastrad implementations of opendir and rewinddir, libc defines a global 661.2Sriastrad symbol `_initdir'. The name `_initdir' is declared normally in 671.2Sriastrad src/lib/libc/gen/dirent_private.h, and defined normally in 681.2Sriastrad src/lib/libc/gen/initdir.c. 691.1Sriastrad 701.1SriastradIf the signature or semantics of a library routine foo changed in (for 711.1Sriastradexample) NetBSD 6.0, then libc provides 721.1Sriastrad 731.1Sriastrad(1) an ELF global symbol `_foo' implementing its old signature, 741.1Sriastrad(2) an ELF weak symbol `foo' aliasing `_foo', and 751.1Sriastrad(3) an ELF global symbol `__foo50' implementing its new signature (yes, 761.1Sriastrad `__foo50', not `__foo60'). 771.1Sriastrad 781.1SriastradThe name foo is declared in the appropriate header file, under any 791.1Sriastradrelevant feature macros, with a __RENAME directive so that for calls to 801.1Sriastradfoo, the compiler will generate relocations for __foo50. Old programs, 811.1Sriastradcompiled with the old signature, will continue to use the old symbol. 821.1Sriastrad 831.1Sriastrad=> Example: In NetBSD 5.0, time_t was int32_t on every machine. In 841.1Sriastrad NetBSD 6.0 and onward, time_t is int64_t on every machine. 851.1Sriastrad Consequently, the signature of time(3), written as 861.1Sriastrad 871.1Sriastrad time_t time(time_t *); 881.1Sriastrad 891.1Sriastrad changed in NetBSD 6.0 from being effectively 901.1Sriastrad 911.1Sriastrad int32_t time(int32_t *); 921.1Sriastrad 931.1Sriastrad to being effectively 941.1Sriastrad 951.1Sriastrad int64_t time(int64_t *); 961.1Sriastrad 971.1Sriastrad Thus, libc provides 981.1Sriastrad 991.1Sriastrad (1) the ELF global symbol `_time' implementing the old signature, 1001.1Sriastrad (2) the ELF weak symbol `time' aliasing `_time', and 1011.1Sriastrad (3) the ELF global symbol `__time50' implementing the new signature. 1021.1Sriastrad 1031.2Sriastrad The header file <time.h> (src/include/time.h) declares 1041.1Sriastrad 1051.1Sriastrad time_t time(time_t *) __RENAME(__time50); 1061.1Sriastrad 1071.1Sriastrad so that compiling C programs that call time will yield objects that 1081.1Sriastrad use the __time50 symbol from libc. However, old programs that were 1091.1Sriastrad compiled against the 32-bit declaration will continue to use the 1101.1Sriastrad 32-bit symbol from libc. 1111.2Sriastrad 1121.2Sriastrad The header file "namespace.h" (src/lib/libc/include/namespace.h) 1131.2Sriastrad defines `time' as a macro expanding to `_time'. 1141.2Sriastrad 1151.2Sriastrad The source file src/lib/libc/gen/time.c includes "namespace.h" and 1161.2Sriastrad <time.h> and defines `time' normally. The declaration of `time' in 1171.2Sriastrad <time.h> is replaced after macro expansion by a declaration of 1181.2Sriastrad `_time', and the definition in time.c is replaced by a definition of 1191.2Sriastrad `_time'. But the __RENAME directive causes the resulting ELF global 1201.2Sriastrad symbol to be `__time50'. 1211.2Sriastrad 1221.2Sriastrad The header file <compat/include/time.h> 1231.2Sriastrad (src/lib/libc/compat/include/time.h) declares 1241.2Sriastrad 1251.2Sriastrad int32_t time(int32_t *); 1261.2Sriastrad 1271.2Sriastrad The source file src/lib/libc/compat/gen/compat_time.c includes 1281.2Sriastrad "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses 1291.2Sriastrad the normal declaration of `time' in <time.h> by defining 1301.2Sriastrad __LIBC12_SOURCE__. Then compat_time.c defines `time' normally. 1311.2Sriastrad Again, the name is replaced after macro expansion by `_time', but 1321.2Sriastrad since there is no __RENAME directive in <compat/include/time.h>, the 1331.2Sriastrad resulting ELF global symbol is `_time'. 1341.2Sriastrad 1351.2Sriastrad Finally, alongside the definition in compat_time.c is 1361.2Sriastrad 1371.2Sriastrad __weak_alias(time,_time) 1381.2Sriastrad 1391.2Sriastrad to provide `time' as an ELF weak symbol aliasing `_time'. 1401.2Sriastrad 1411.2Sriastrad The net effect is that NetBSD 6's libc provides the same definitions 1421.2Sriastrad as NetBSD 5's libc for the symbols `time' and `_time', so that old 1431.2Sriastrad programs that were compiled in NetBSD 5 will continue to work with 1441.2Sriastrad NetBSD 6's libc. But programs compiled in NetBSD 6 will have 64-bit 1451.2Sriastrad time_t. 146