README revision 1.7
11.7Sriastrad	$NetBSD: README,v 1.7 2017/02/08 13:31:36 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.5Sriastradit is provided as an ELF global symbol.  Its name is declared normally
181.1Sriastradin the appropriate header file.
191.1Sriastrad
201.5Sriastrad=> Example: The names `malloc' and `free' are declared normally in
211.5Sriastrad   <stdlib.h> (src/include/stdlib.h):
221.5Sriastrad
231.5Sriastrad	void	*malloc(size_t);
241.5Sriastrad	void	 free(void *);
251.5Sriastrad
261.5Sriastrad   libc provides the following ELF symbols:
271.5Sriastrad
281.5Sriastrad	malloc		global
291.5Sriastrad	free		global
301.5Sriastrad
311.5Sriastrad   In the implementation of libc, malloc and free are defined normally
321.5Sriastrad   in src/lib/libc/stdlib/jemalloc.c:
331.5Sriastrad
341.5Sriastrad	void *
351.5Sriastrad	malloc(size_t size)
361.5Sriastrad	{
371.5Sriastrad	...
381.5Sriastrad
391.5Sriastrad	void
401.5Sriastrad	free(void *ptr)
411.5Sriastrad	{
421.5Sriastrad	...
431.1Sriastrad
441.4Sriastrad** NetBSD-specific nonstandard extensions
451.4Sriastrad
461.1SriastradIf a library routine is nonstandard but published and its signature has
471.5Sriastradnever changed, it is provided as an ELF weak symbol aliasing an ELF
481.1Sriastradglobal symbol of the same name with an underscore prefix.
491.1Sriastrad
501.1SriastradThe name is declared normally in the appropriate header file, provided
511.1Sriastradthat the relevant feature macro, such as _NETBSD_SOURCE, is defined.
521.1Sriastrad
531.1SriastradWithin libc, the name is defined in "namespace.h"
541.1Sriastrad(src/lib/libc/include/namespace.h) as a macro expanding to the
551.5Sriastradunderscored name, which is included before the relevant header file, so
561.5Sriastradthat
571.5Sriastrad
581.5Sriastrad(a) the definition in a .c file will define the underscored ELF global
591.5Sriastradsymbol, and
601.5Sriastrad
611.5Sriastrad(b) the declaration in the standard header file will match the
621.5Sriastraddefinition in the .c file.
631.1Sriastrad
641.1SriastradAlongside the definition in the .c file is a __weak_alias directive to
651.1Sriastradcreate the ELF weak symbol alias.
661.1Sriastrad
671.5Sriastrad=> Example: For the nonstandard extension consttime_memequal, the
681.5Sriastrad   header file <string.h> (src/include/string.h) declares
691.5Sriastrad   `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE:
701.5Sriastrad
711.5Sriastrad	#if defined(_NETBSD_SOURCE)
721.5Sriastrad	...
731.5Sriastrad	int	consttime_memequal(const void *, const void *, size_t);
741.5Sriastrad	...
751.5Sriastrad	#endif	/* _NETBSD_SOURCE */
761.5Sriastrad
771.5Sriastrad   libc provides the following ELF symbols:
781.5Sriastrad
791.5Sriastrad	_consttime_memequal	global
801.5Sriastrad	consttime_memequal	weak alias for	_consttime_memequal
811.5Sriastrad
821.5Sriastrad   In the implementation of libc, the header file "namespace.h"
831.5Sriastrad   (src/lib/libc/include/namespace.h) defines `consttime_memequal' as a
841.5Sriastrad   macro expanding to `_consttime_memequal':
851.1Sriastrad
861.5Sriastrad	#define	consttime_memequal	_consttime_memequal
871.3Sriastrad
881.3Sriastrad   The source file src/common/lib/libc/string/consttime_memequal.c
891.3Sriastrad   includes "namespace.h" and <string.h>, and defines
901.5Sriastrad   `consttime_memequal' normally:
911.1Sriastrad
921.5Sriastrad	int
931.5Sriastrad	consttime_memequal(const void *b1, const void *b2, size_t len)
941.5Sriastrad	{
951.5Sriastrad	...
961.5Sriastrad
971.5Sriastrad   Macro expansion replaces `consttime_memequal' by
981.5Sriastrad   `_consttime_memequal', which is the ELF global symbol this defines.
991.1Sriastrad   Alongside the definition is
1001.1Sriastrad
1011.1Sriastrad	__weak_alias(consttime_memequal,_consttime_memequal)
1021.1Sriastrad
1031.1Sriastrad   to provide `consttime_memequal' as an ELF weak symbol aliasing
1041.1Sriastrad   `_consttime_memequal'.
1051.1Sriastrad
1061.4Sriastrad** Internal symbols
1071.4Sriastrad
1081.1SriastradIf a library routine is internal to libc, it is defined as an ELF
1091.2Sriastradglobal symbol with an underscore prefix.  Its name is declared in the
1101.2Sriastradappropriate internal header file.
1111.1Sriastrad
1121.5Sriastrad=> Example: The implementations of opendir and rewinddir use a common
1131.5Sriastrad   subroutine _initdir, which is not part of the libc API or ABI -- it
1141.5Sriastrad   is just an internal subroutine.
1151.5Sriastrad
1161.5Sriastrad   libc provides the following ELF symbols:
1171.5Sriastrad
1181.5Sriastrad	_initdir	global
1191.5Sriastrad
1201.5Sriastrad   The name `_initdir' is declared normally in
1211.5Sriastrad   src/lib/libc/gen/dirent_private.h:
1221.5Sriastrad
1231.5Sriastrad	int	_initdir(DIR *, int, const char *);
1241.5Sriastrad
1251.5Sriastrad   The name `_initdir' is defined normally in
1261.5Sriastrad   src/lib/libc/gen/initdir.c:
1271.5Sriastrad
1281.5Sriastrad	int
1291.5Sriastrad	_initdir(DIR *dirp, int fd, const char *name)
1301.5Sriastrad	{
1311.5Sriastrad	...
1321.1Sriastrad
1331.4Sriastrad** Old versions of library routines
1341.4Sriastrad
1351.1SriastradIf the signature or semantics of a library routine foo changed in (for
1361.1Sriastradexample) NetBSD 6.0, then libc provides
1371.1Sriastrad
1381.1Sriastrad(1) an ELF global symbol `_foo' implementing its old signature,
1391.1Sriastrad(2) an ELF weak symbol `foo' aliasing `_foo', and
1401.1Sriastrad(3) an ELF global symbol `__foo50' implementing its new signature (yes,
1411.1Sriastrad    `__foo50', not `__foo60').
1421.1Sriastrad
1431.1SriastradThe name foo is declared in the appropriate header file, under any
1441.1Sriastradrelevant feature macros, with a __RENAME directive so that for calls to
1451.1Sriastradfoo, the compiler will generate relocations for __foo50.  Old programs,
1461.1Sriastradcompiled with the old signature, will continue to use the old symbol.
1471.1Sriastrad
1481.1Sriastrad=> Example: In NetBSD 5.0, time_t was int32_t on every machine.  In
1491.1Sriastrad   NetBSD 6.0 and onward, time_t is int64_t on every machine.
1501.1Sriastrad   Consequently, the signature of time(3), written as
1511.1Sriastrad
1521.5Sriastrad	time_t	time(time_t *);
1531.1Sriastrad
1541.5Sriastrad   was effectively
1551.1Sriastrad
1561.5Sriastrad	int32_t	time(int32_t *);
1571.1Sriastrad
1581.5Sriastrad   before NetBSD 6.0.  In NetBSD 6.0, it changed to be effectively
1591.1Sriastrad
1601.1Sriastrad	int64_t time(int64_t *);
1611.1Sriastrad
1621.5Sriastrad   Before NetBSD 6.0, libc provided the following libc symbols:
1631.5Sriastrad
1641.5Sriastrad	_time		global (implementing the old signature)
1651.5Sriastrad	time		weak alias for _time
1661.5Sriastrad
1671.5Sriastrad   In NetBSD 6.0 and later, libc provides the following ELF symbols:
1681.1Sriastrad
1691.5Sriastrad	_time		global (implementing the old signature)
1701.5Sriastrad	time		weak alias for _time
1711.5Sriastrad	__time50	global (implementing the new signature)
1721.5Sriastrad
1731.5Sriastrad   (Note that the only change is to add __time50, so that existing
1741.5Sriastrad   programs linked against old versions of libc will see the same
1751.5Sriastrad   semantics for the symbols that were already there.)
1761.1Sriastrad
1771.2Sriastrad   The header file <time.h> (src/include/time.h) declares
1781.1Sriastrad
1791.5Sriastrad	time_t	time(time_t *) __RENAME(__time50);
1801.1Sriastrad
1811.1Sriastrad   so that compiling C programs that call time will yield objects that
1821.1Sriastrad   use the __time50 symbol from libc.  However, old programs that were
1831.1Sriastrad   compiled against the 32-bit declaration will continue to use the
1841.1Sriastrad   32-bit symbol from libc.
1851.2Sriastrad
1861.2Sriastrad   The header file "namespace.h" (src/lib/libc/include/namespace.h)
1871.5Sriastrad   defines `time' as a macro expanding to `_time':
1881.5Sriastrad
1891.5Sriastrad	#define	time	_time
1901.2Sriastrad
1911.2Sriastrad   The source file src/lib/libc/gen/time.c includes "namespace.h" and
1921.5Sriastrad   <time.h> and defines `time' normally:
1931.5Sriastrad
1941.5Sriastrad	time_t
1951.5Sriastrad	time(time_t *t)
1961.5Sriastrad	{
1971.5Sriastrad	...
1981.5Sriastrad
1991.5Sriastrad   Macro expansion replaces `time' by `_time', but the
2001.5Sriastrad   `__RENAME(__time50)' directive on the declaration <time.h> (to which
2011.5Sriastrad   the "namespace.h" macro expansion also applies) means the ELF global
2021.5Sriastrad   symbol defined here is actually `__time50'.
2031.2Sriastrad
2041.2Sriastrad   The header file <compat/include/time.h>
2051.2Sriastrad   (src/lib/libc/compat/include/time.h) declares
2061.2Sriastrad
2071.5Sriastrad	int32_t	time(int32_t *);
2081.2Sriastrad
2091.2Sriastrad   The source file src/lib/libc/compat/gen/compat_time.c includes
2101.2Sriastrad   "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses
2111.2Sriastrad   the normal declaration of `time' in <time.h> by defining
2121.7Sriastrad   __LIBC12_SOURCE__ and thus gets it from <compat/include/time.h>
2131.7Sriastrad   instead.  Then compat_time.c defines `time' normally:
2141.5Sriastrad
2151.6Sriastrad	int32_t
2161.6Sriastrad	time(int32_t *t)
2171.5Sriastrad	{
2181.5Sriastrad	...
2191.5Sriastrad
2201.5Sriastrad   Again, macro expansion replaces `time' by `_time', but since there
2211.5Sriastrad   is no __RENAME directive in <compat/include/time.h>, the resulting
2221.6Sriastrad   ELF global symbol is `_time'.  (Actually, compat_time.c just has
2231.6Sriastrad   `#define time_t int32_t' and `#include "gen/time.c"' to get the same
2241.6Sriastrad   text of the definition of time.  The above definition is what we get
2251.6Sriastrad   effectively by substituting int32_t for the type time_t.)
2261.2Sriastrad
2271.2Sriastrad   Finally, alongside the definition in compat_time.c is
2281.2Sriastrad
2291.2Sriastrad	__weak_alias(time,_time)
2301.2Sriastrad
2311.5Sriastrad   to define `time' as an ELF weak symbol aliasing `_time'.
2321.2Sriastrad
2331.2Sriastrad   The net effect is that NetBSD 6's libc provides the same definitions
2341.2Sriastrad   as NetBSD 5's libc for the symbols `time' and `_time', so that old
2351.2Sriastrad   programs that were compiled in NetBSD 5 will continue to work with
2361.2Sriastrad   NetBSD 6's libc.  But programs compiled in NetBSD 6 will have 64-bit
2371.2Sriastrad   time_t.
238