README revision 1.4
11.4Sriastrad	$NetBSD: README,v 1.4 2015/07/11 14:29:50 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.4Sriastrad(c) internal symbols, and
121.4Sriastrad(d) old versions of any published library routines.
131.4Sriastrad
141.4Sriastrad** Standard library routines
151.1Sriastrad
161.1SriastradIf a library routine is standard and its signature has never changed,
171.1Sriastradit is defined as an ELF global symbol.  Its name is declared normally
181.1Sriastradin the appropriate header file.
191.1Sriastrad
201.1Sriastrad=> Example: libc defines global symbols `malloc' and `free' for the
211.1Sriastrad   standard C memory allocator routines.  The names `malloc' and `free'
221.1Sriastrad   are declared normally in <stdlib.h> (src/include/stdlib.h).
231.1Sriastrad
241.4Sriastrad** NetBSD-specific nonstandard extensions
251.4Sriastrad
261.1SriastradIf a library routine is nonstandard but published and its signature has
271.1Sriastradnever changed, it is defined as an ELF weak symbol aliasing an ELF
281.1Sriastradglobal symbol of the same name with an underscore prefix.
291.1Sriastrad
301.1SriastradThe name is declared normally in the appropriate header file, provided
311.1Sriastradthat the relevant feature macro, such as _NETBSD_SOURCE, is defined.
321.1Sriastrad
331.1SriastradWithin libc, the name is defined in "namespace.h"
341.1Sriastrad(src/lib/libc/include/namespace.h) as a macro expanding to the
351.1Sriastradunderscored name, so that the definition in a .c file will define the
361.1Sriastradunderscored ELF global symbol.
371.1Sriastrad
381.1SriastradAlongside the definition in the .c file is a __weak_alias directive to
391.1Sriastradcreate the ELF weak symbol alias.
401.1Sriastrad
411.1Sriastrad=> Example: For the nonstandard extension consttime_memequal, libc
421.1Sriastrad   defines a weak symbol `consttime_memequal' aliasing a global symbol
431.1Sriastrad   `_consttime_memequal'.
441.1Sriastrad
451.3Sriastrad   The header file <string.h> (src/include/string.h) declares
461.3Sriastrad   `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE.
471.1Sriastrad
481.3Sriastrad   The header file "namespace.h" (src/lib/libc/include/namespace.h)
491.3Sriastrad   defines `consttime_memequal' as a macro expanding to
501.3Sriastrad   `_consttime_memequal'.
511.3Sriastrad
521.3Sriastrad   The source file src/common/lib/libc/string/consttime_memequal.c
531.3Sriastrad   includes "namespace.h" and <string.h>, and defines
541.3Sriastrad   `consttime_memequal' normally, which, after macro expansion, causes
551.3Sriastrad   the ELF global symbol `_consttime_memequal' to be defined.
561.1Sriastrad
571.1Sriastrad   Alongside the definition is
581.1Sriastrad
591.1Sriastrad	__weak_alias(consttime_memequal,_consttime_memequal)
601.1Sriastrad
611.1Sriastrad   to provide `consttime_memequal' as an ELF weak symbol aliasing
621.1Sriastrad   `_consttime_memequal'.
631.1Sriastrad
641.4Sriastrad** Internal symbols
651.4Sriastrad
661.1SriastradIf a library routine is internal to libc, it is defined as an ELF
671.2Sriastradglobal symbol with an underscore prefix.  Its name is declared in the
681.2Sriastradappropriate internal header file.
691.1Sriastrad
701.1Sriastrad=> Example: For the internal library routine _initdir, used by the
711.1Sriastrad   implementations of opendir and rewinddir, libc defines a global
721.2Sriastrad   symbol `_initdir'.  The name `_initdir' is declared normally in
731.2Sriastrad   src/lib/libc/gen/dirent_private.h, and defined normally in
741.2Sriastrad   src/lib/libc/gen/initdir.c.
751.1Sriastrad
761.4Sriastrad** Old versions of library routines
771.4Sriastrad
781.1SriastradIf the signature or semantics of a library routine foo changed in (for
791.1Sriastradexample) NetBSD 6.0, then libc provides
801.1Sriastrad
811.1Sriastrad(1) an ELF global symbol `_foo' implementing its old signature,
821.1Sriastrad(2) an ELF weak symbol `foo' aliasing `_foo', and
831.1Sriastrad(3) an ELF global symbol `__foo50' implementing its new signature (yes,
841.1Sriastrad    `__foo50', not `__foo60').
851.1Sriastrad
861.1SriastradThe name foo is declared in the appropriate header file, under any
871.1Sriastradrelevant feature macros, with a __RENAME directive so that for calls to
881.1Sriastradfoo, the compiler will generate relocations for __foo50.  Old programs,
891.1Sriastradcompiled with the old signature, will continue to use the old symbol.
901.1Sriastrad
911.1Sriastrad=> Example: In NetBSD 5.0, time_t was int32_t on every machine.  In
921.1Sriastrad   NetBSD 6.0 and onward, time_t is int64_t on every machine.
931.1Sriastrad   Consequently, the signature of time(3), written as
941.1Sriastrad
951.1Sriastrad	time_t time(time_t *);
961.1Sriastrad
971.1Sriastrad   changed in NetBSD 6.0 from being effectively
981.1Sriastrad
991.1Sriastrad	int32_t time(int32_t *);
1001.1Sriastrad
1011.1Sriastrad   to being effectively
1021.1Sriastrad
1031.1Sriastrad	int64_t time(int64_t *);
1041.1Sriastrad
1051.1Sriastrad   Thus, libc provides
1061.1Sriastrad
1071.1Sriastrad   (1) the ELF global symbol `_time' implementing the old signature,
1081.1Sriastrad   (2) the ELF weak symbol `time' aliasing `_time', and
1091.1Sriastrad   (3) the ELF global symbol `__time50' implementing the new signature.
1101.1Sriastrad
1111.2Sriastrad   The header file <time.h> (src/include/time.h) declares
1121.1Sriastrad
1131.1Sriastrad	time_t time(time_t *) __RENAME(__time50);
1141.1Sriastrad
1151.1Sriastrad   so that compiling C programs that call time will yield objects that
1161.1Sriastrad   use the __time50 symbol from libc.  However, old programs that were
1171.1Sriastrad   compiled against the 32-bit declaration will continue to use the
1181.1Sriastrad   32-bit symbol from libc.
1191.2Sriastrad
1201.2Sriastrad   The header file "namespace.h" (src/lib/libc/include/namespace.h)
1211.2Sriastrad   defines `time' as a macro expanding to `_time'.
1221.2Sriastrad
1231.2Sriastrad   The source file src/lib/libc/gen/time.c includes "namespace.h" and
1241.2Sriastrad   <time.h> and defines `time' normally.  The declaration of `time' in
1251.2Sriastrad   <time.h> is replaced after macro expansion by a declaration of
1261.2Sriastrad   `_time', and the definition in time.c is replaced by a definition of
1271.2Sriastrad   `_time'.  But the __RENAME directive causes the resulting ELF global
1281.2Sriastrad   symbol to be `__time50'.
1291.2Sriastrad
1301.2Sriastrad   The header file <compat/include/time.h>
1311.2Sriastrad   (src/lib/libc/compat/include/time.h) declares
1321.2Sriastrad
1331.2Sriastrad	int32_t time(int32_t *);
1341.2Sriastrad
1351.2Sriastrad   The source file src/lib/libc/compat/gen/compat_time.c includes
1361.2Sriastrad   "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses
1371.2Sriastrad   the normal declaration of `time' in <time.h> by defining
1381.2Sriastrad   __LIBC12_SOURCE__.  Then compat_time.c defines `time' normally.
1391.2Sriastrad   Again, the name is replaced after macro expansion by `_time', but
1401.2Sriastrad   since there is no __RENAME directive in <compat/include/time.h>, the
1411.2Sriastrad   resulting ELF global symbol is `_time'.
1421.2Sriastrad
1431.2Sriastrad   Finally, alongside the definition in compat_time.c is
1441.2Sriastrad
1451.2Sriastrad	__weak_alias(time,_time)
1461.2Sriastrad
1471.2Sriastrad   to provide `time' as an ELF weak symbol aliasing `_time'.
1481.2Sriastrad
1491.2Sriastrad   The net effect is that NetBSD 6's libc provides the same definitions
1501.2Sriastrad   as NetBSD 5's libc for the symbols `time' and `_time', so that old
1511.2Sriastrad   programs that were compiled in NetBSD 5 will continue to work with
1521.2Sriastrad   NetBSD 6's libc.  But programs compiled in NetBSD 6 will have 64-bit
1531.2Sriastrad   time_t.
154