README revision 1.2
11.2Sriastrad	$NetBSD: README,v 1.2 2015/03/20 13:26:51 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.1Sriastrad   The name `consttime_memequal' is declared in <string.h>
421.1Sriastrad   (src/include/string.h) if the caller defines _NETBSD_SOURCE.
431.1Sriastrad
441.1Sriastrad   The name `consttime_memequal' is defined as a macro in "namespace.h"
451.1Sriastrad   (src/lib/libc/include/namespace.h) expanding to
461.1Sriastrad   `_consttime_memequal'.  The source name `consttime_memequal' is
471.1Sriastrad   defined in src/common/lib/libc/string/consttime_memequal.c, causing
481.1Sriastrad   the ELF global symbol `_consttime_memequal' to be defined, after
491.1Sriastrad   macro expansion.
501.1Sriastrad
511.1Sriastrad   Alongside the definition is
521.1Sriastrad
531.1Sriastrad	__weak_alias(consttime_memequal,_consttime_memequal)
541.1Sriastrad
551.1Sriastrad   to provide `consttime_memequal' as an ELF weak symbol aliasing
561.1Sriastrad   `_consttime_memequal'.
571.1Sriastrad
581.1SriastradIf a library routine is internal to libc, it is defined as an ELF
591.2Sriastradglobal symbol with an underscore prefix.  Its name is declared in the
601.2Sriastradappropriate internal header file.
611.1Sriastrad
621.1Sriastrad=> Example: For the internal library routine _initdir, used by the
631.1Sriastrad   implementations of opendir and rewinddir, libc defines a global
641.2Sriastrad   symbol `_initdir'.  The name `_initdir' is declared normally in
651.2Sriastrad   src/lib/libc/gen/dirent_private.h, and defined normally in
661.2Sriastrad   src/lib/libc/gen/initdir.c.
671.1Sriastrad
681.1SriastradIf the signature or semantics of a library routine foo changed in (for
691.1Sriastradexample) NetBSD 6.0, then libc provides
701.1Sriastrad
711.1Sriastrad(1) an ELF global symbol `_foo' implementing its old signature,
721.1Sriastrad(2) an ELF weak symbol `foo' aliasing `_foo', and
731.1Sriastrad(3) an ELF global symbol `__foo50' implementing its new signature (yes,
741.1Sriastrad    `__foo50', not `__foo60').
751.1Sriastrad
761.1SriastradThe name foo is declared in the appropriate header file, under any
771.1Sriastradrelevant feature macros, with a __RENAME directive so that for calls to
781.1Sriastradfoo, the compiler will generate relocations for __foo50.  Old programs,
791.1Sriastradcompiled with the old signature, will continue to use the old symbol.
801.1Sriastrad
811.1Sriastrad=> Example: In NetBSD 5.0, time_t was int32_t on every machine.  In
821.1Sriastrad   NetBSD 6.0 and onward, time_t is int64_t on every machine.
831.1Sriastrad   Consequently, the signature of time(3), written as
841.1Sriastrad
851.1Sriastrad	time_t time(time_t *);
861.1Sriastrad
871.1Sriastrad   changed in NetBSD 6.0 from being effectively
881.1Sriastrad
891.1Sriastrad	int32_t time(int32_t *);
901.1Sriastrad
911.1Sriastrad   to being effectively
921.1Sriastrad
931.1Sriastrad	int64_t time(int64_t *);
941.1Sriastrad
951.1Sriastrad   Thus, libc provides
961.1Sriastrad
971.1Sriastrad   (1) the ELF global symbol `_time' implementing the old signature,
981.1Sriastrad   (2) the ELF weak symbol `time' aliasing `_time', and
991.1Sriastrad   (3) the ELF global symbol `__time50' implementing the new signature.
1001.1Sriastrad
1011.2Sriastrad   The header file <time.h> (src/include/time.h) declares
1021.1Sriastrad
1031.1Sriastrad	time_t time(time_t *) __RENAME(__time50);
1041.1Sriastrad
1051.1Sriastrad   so that compiling C programs that call time will yield objects that
1061.1Sriastrad   use the __time50 symbol from libc.  However, old programs that were
1071.1Sriastrad   compiled against the 32-bit declaration will continue to use the
1081.1Sriastrad   32-bit symbol from libc.
1091.2Sriastrad
1101.2Sriastrad   The header file "namespace.h" (src/lib/libc/include/namespace.h)
1111.2Sriastrad   defines `time' as a macro expanding to `_time'.
1121.2Sriastrad
1131.2Sriastrad   The source file src/lib/libc/gen/time.c includes "namespace.h" and
1141.2Sriastrad   <time.h> and defines `time' normally.  The declaration of `time' in
1151.2Sriastrad   <time.h> is replaced after macro expansion by a declaration of
1161.2Sriastrad   `_time', and the definition in time.c is replaced by a definition of
1171.2Sriastrad   `_time'.  But the __RENAME directive causes the resulting ELF global
1181.2Sriastrad   symbol to be `__time50'.
1191.2Sriastrad
1201.2Sriastrad   The header file <compat/include/time.h>
1211.2Sriastrad   (src/lib/libc/compat/include/time.h) declares
1221.2Sriastrad
1231.2Sriastrad	int32_t time(int32_t *);
1241.2Sriastrad
1251.2Sriastrad   The source file src/lib/libc/compat/gen/compat_time.c includes
1261.2Sriastrad   "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses
1271.2Sriastrad   the normal declaration of `time' in <time.h> by defining
1281.2Sriastrad   __LIBC12_SOURCE__.  Then compat_time.c defines `time' normally.
1291.2Sriastrad   Again, the name is replaced after macro expansion by `_time', but
1301.2Sriastrad   since there is no __RENAME directive in <compat/include/time.h>, the
1311.2Sriastrad   resulting ELF global symbol is `_time'.
1321.2Sriastrad
1331.2Sriastrad   Finally, alongside the definition in compat_time.c is
1341.2Sriastrad
1351.2Sriastrad	__weak_alias(time,_time)
1361.2Sriastrad
1371.2Sriastrad   to provide `time' as an ELF weak symbol aliasing `_time'.
1381.2Sriastrad
1391.2Sriastrad   The net effect is that NetBSD 6's libc provides the same definitions
1401.2Sriastrad   as NetBSD 5's libc for the symbols `time' and `_time', so that old
1411.2Sriastrad   programs that were compiled in NetBSD 5 will continue to work with
1421.2Sriastrad   NetBSD 6's libc.  But programs compiled in NetBSD 6 will have 64-bit
1431.2Sriastrad   time_t.
144