xref: /minix3/lib/libc/README (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc	$NetBSD: README,v 1.5 2015/07/11 15:23:57 riastradh Exp $
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuclibc: The C library.
4*0a6a1f1dSLionel Sambuc
5*0a6a1f1dSLionel Sambuc* ELF symbols and source names
6*0a6a1f1dSLionel Sambuc
7*0a6a1f1dSLionel Sambuclibc contains symbols for:
8*0a6a1f1dSLionel Sambuc
9*0a6a1f1dSLionel Sambuc(a) standard library routines in C and POSIX,
10*0a6a1f1dSLionel Sambuc(b) published NetBSD-specific nonstandard extensions,
11*0a6a1f1dSLionel Sambuc(c) internal symbols, and
12*0a6a1f1dSLionel Sambuc(d) old versions of any published library routines.
13*0a6a1f1dSLionel Sambuc
14*0a6a1f1dSLionel Sambuc** Standard library routines
15*0a6a1f1dSLionel Sambuc
16*0a6a1f1dSLionel SambucIf a library routine is standard and its signature has never changed,
17*0a6a1f1dSLionel Sambucit is provided as an ELF global symbol.  Its name is declared normally
18*0a6a1f1dSLionel Sambucin the appropriate header file.
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel Sambuc=> Example: The names `malloc' and `free' are declared normally in
21*0a6a1f1dSLionel Sambuc   <stdlib.h> (src/include/stdlib.h):
22*0a6a1f1dSLionel Sambuc
23*0a6a1f1dSLionel Sambuc	void	*malloc(size_t);
24*0a6a1f1dSLionel Sambuc	void	 free(void *);
25*0a6a1f1dSLionel Sambuc
26*0a6a1f1dSLionel Sambuc   libc provides the following ELF symbols:
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc	malloc		global
29*0a6a1f1dSLionel Sambuc	free		global
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel Sambuc   In the implementation of libc, malloc and free are defined normally
32*0a6a1f1dSLionel Sambuc   in src/lib/libc/stdlib/jemalloc.c:
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc	void *
35*0a6a1f1dSLionel Sambuc	malloc(size_t size)
36*0a6a1f1dSLionel Sambuc	{
37*0a6a1f1dSLionel Sambuc	...
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc	void
40*0a6a1f1dSLionel Sambuc	free(void *ptr)
41*0a6a1f1dSLionel Sambuc	{
42*0a6a1f1dSLionel Sambuc	...
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc** NetBSD-specific nonstandard extensions
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel SambucIf a library routine is nonstandard but published and its signature has
47*0a6a1f1dSLionel Sambucnever changed, it is provided as an ELF weak symbol aliasing an ELF
48*0a6a1f1dSLionel Sambucglobal symbol of the same name with an underscore prefix.
49*0a6a1f1dSLionel Sambuc
50*0a6a1f1dSLionel SambucThe name is declared normally in the appropriate header file, provided
51*0a6a1f1dSLionel Sambucthat the relevant feature macro, such as _NETBSD_SOURCE, is defined.
52*0a6a1f1dSLionel Sambuc
53*0a6a1f1dSLionel SambucWithin libc, the name is defined in "namespace.h"
54*0a6a1f1dSLionel Sambuc(src/lib/libc/include/namespace.h) as a macro expanding to the
55*0a6a1f1dSLionel Sambucunderscored name, which is included before the relevant header file, so
56*0a6a1f1dSLionel Sambucthat
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc(a) the definition in a .c file will define the underscored ELF global
59*0a6a1f1dSLionel Sambucsymbol, and
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc(b) the declaration in the standard header file will match the
62*0a6a1f1dSLionel Sambucdefinition in the .c file.
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel SambucAlongside the definition in the .c file is a __weak_alias directive to
65*0a6a1f1dSLionel Sambuccreate the ELF weak symbol alias.
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc=> Example: For the nonstandard extension consttime_memequal, the
68*0a6a1f1dSLionel Sambuc   header file <string.h> (src/include/string.h) declares
69*0a6a1f1dSLionel Sambuc   `consttime_memequal' normally, if the caller defines _NETBSD_SOURCE:
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc	#if defined(_NETBSD_SOURCE)
72*0a6a1f1dSLionel Sambuc	...
73*0a6a1f1dSLionel Sambuc	int	consttime_memequal(const void *, const void *, size_t);
74*0a6a1f1dSLionel Sambuc	...
75*0a6a1f1dSLionel Sambuc	#endif	/* _NETBSD_SOURCE */
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc   libc provides the following ELF symbols:
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc	_consttime_memequal	global
80*0a6a1f1dSLionel Sambuc	consttime_memequal	weak alias for	_consttime_memequal
81*0a6a1f1dSLionel Sambuc
82*0a6a1f1dSLionel Sambuc   In the implementation of libc, the header file "namespace.h"
83*0a6a1f1dSLionel Sambuc   (src/lib/libc/include/namespace.h) defines `consttime_memequal' as a
84*0a6a1f1dSLionel Sambuc   macro expanding to `_consttime_memequal':
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc	#define	consttime_memequal	_consttime_memequal
87*0a6a1f1dSLionel Sambuc
88*0a6a1f1dSLionel Sambuc   The source file src/common/lib/libc/string/consttime_memequal.c
89*0a6a1f1dSLionel Sambuc   includes "namespace.h" and <string.h>, and defines
90*0a6a1f1dSLionel Sambuc   `consttime_memequal' normally:
91*0a6a1f1dSLionel Sambuc
92*0a6a1f1dSLionel Sambuc	int
93*0a6a1f1dSLionel Sambuc	consttime_memequal(const void *b1, const void *b2, size_t len)
94*0a6a1f1dSLionel Sambuc	{
95*0a6a1f1dSLionel Sambuc	...
96*0a6a1f1dSLionel Sambuc
97*0a6a1f1dSLionel Sambuc   Macro expansion replaces `consttime_memequal' by
98*0a6a1f1dSLionel Sambuc   `_consttime_memequal', which is the ELF global symbol this defines.
99*0a6a1f1dSLionel Sambuc   Alongside the definition is
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc	__weak_alias(consttime_memequal,_consttime_memequal)
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc   to provide `consttime_memequal' as an ELF weak symbol aliasing
104*0a6a1f1dSLionel Sambuc   `_consttime_memequal'.
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc** Internal symbols
107*0a6a1f1dSLionel Sambuc
108*0a6a1f1dSLionel SambucIf a library routine is internal to libc, it is defined as an ELF
109*0a6a1f1dSLionel Sambucglobal symbol with an underscore prefix.  Its name is declared in the
110*0a6a1f1dSLionel Sambucappropriate internal header file.
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc=> Example: The implementations of opendir and rewinddir use a common
113*0a6a1f1dSLionel Sambuc   subroutine _initdir, which is not part of the libc API or ABI -- it
114*0a6a1f1dSLionel Sambuc   is just an internal subroutine.
115*0a6a1f1dSLionel Sambuc
116*0a6a1f1dSLionel Sambuc   libc provides the following ELF symbols:
117*0a6a1f1dSLionel Sambuc
118*0a6a1f1dSLionel Sambuc	_initdir	global
119*0a6a1f1dSLionel Sambuc
120*0a6a1f1dSLionel Sambuc   The name `_initdir' is declared normally in
121*0a6a1f1dSLionel Sambuc   src/lib/libc/gen/dirent_private.h:
122*0a6a1f1dSLionel Sambuc
123*0a6a1f1dSLionel Sambuc	int	_initdir(DIR *, int, const char *);
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc   The name `_initdir' is defined normally in
126*0a6a1f1dSLionel Sambuc   src/lib/libc/gen/initdir.c:
127*0a6a1f1dSLionel Sambuc
128*0a6a1f1dSLionel Sambuc	int
129*0a6a1f1dSLionel Sambuc	_initdir(DIR *dirp, int fd, const char *name)
130*0a6a1f1dSLionel Sambuc	{
131*0a6a1f1dSLionel Sambuc	...
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc** Old versions of library routines
134*0a6a1f1dSLionel Sambuc
135*0a6a1f1dSLionel SambucIf the signature or semantics of a library routine foo changed in (for
136*0a6a1f1dSLionel Sambucexample) NetBSD 6.0, then libc provides
137*0a6a1f1dSLionel Sambuc
138*0a6a1f1dSLionel Sambuc(1) an ELF global symbol `_foo' implementing its old signature,
139*0a6a1f1dSLionel Sambuc(2) an ELF weak symbol `foo' aliasing `_foo', and
140*0a6a1f1dSLionel Sambuc(3) an ELF global symbol `__foo50' implementing its new signature (yes,
141*0a6a1f1dSLionel Sambuc    `__foo50', not `__foo60').
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel SambucThe name foo is declared in the appropriate header file, under any
144*0a6a1f1dSLionel Sambucrelevant feature macros, with a __RENAME directive so that for calls to
145*0a6a1f1dSLionel Sambucfoo, the compiler will generate relocations for __foo50.  Old programs,
146*0a6a1f1dSLionel Sambuccompiled with the old signature, will continue to use the old symbol.
147*0a6a1f1dSLionel Sambuc
148*0a6a1f1dSLionel Sambuc=> Example: In NetBSD 5.0, time_t was int32_t on every machine.  In
149*0a6a1f1dSLionel Sambuc   NetBSD 6.0 and onward, time_t is int64_t on every machine.
150*0a6a1f1dSLionel Sambuc   Consequently, the signature of time(3), written as
151*0a6a1f1dSLionel Sambuc
152*0a6a1f1dSLionel Sambuc	time_t	time(time_t *);
153*0a6a1f1dSLionel Sambuc
154*0a6a1f1dSLionel Sambuc   was effectively
155*0a6a1f1dSLionel Sambuc
156*0a6a1f1dSLionel Sambuc	int32_t	time(int32_t *);
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc   before NetBSD 6.0.  In NetBSD 6.0, it changed to be effectively
159*0a6a1f1dSLionel Sambuc
160*0a6a1f1dSLionel Sambuc	int64_t time(int64_t *);
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc   Before NetBSD 6.0, libc provided the following libc symbols:
163*0a6a1f1dSLionel Sambuc
164*0a6a1f1dSLionel Sambuc	_time		global (implementing the old signature)
165*0a6a1f1dSLionel Sambuc	time		weak alias for _time
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc   In NetBSD 6.0 and later, libc provides the following ELF symbols:
168*0a6a1f1dSLionel Sambuc
169*0a6a1f1dSLionel Sambuc	_time		global (implementing the old signature)
170*0a6a1f1dSLionel Sambuc	time		weak alias for _time
171*0a6a1f1dSLionel Sambuc	__time50	global (implementing the new signature)
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc   (Note that the only change is to add __time50, so that existing
174*0a6a1f1dSLionel Sambuc   programs linked against old versions of libc will see the same
175*0a6a1f1dSLionel Sambuc   semantics for the symbols that were already there.)
176*0a6a1f1dSLionel Sambuc
177*0a6a1f1dSLionel Sambuc   The header file <time.h> (src/include/time.h) declares
178*0a6a1f1dSLionel Sambuc
179*0a6a1f1dSLionel Sambuc	time_t	time(time_t *) __RENAME(__time50);
180*0a6a1f1dSLionel Sambuc
181*0a6a1f1dSLionel Sambuc   so that compiling C programs that call time will yield objects that
182*0a6a1f1dSLionel Sambuc   use the __time50 symbol from libc.  However, old programs that were
183*0a6a1f1dSLionel Sambuc   compiled against the 32-bit declaration will continue to use the
184*0a6a1f1dSLionel Sambuc   32-bit symbol from libc.
185*0a6a1f1dSLionel Sambuc
186*0a6a1f1dSLionel Sambuc   The header file "namespace.h" (src/lib/libc/include/namespace.h)
187*0a6a1f1dSLionel Sambuc   defines `time' as a macro expanding to `_time':
188*0a6a1f1dSLionel Sambuc
189*0a6a1f1dSLionel Sambuc	#define	time	_time
190*0a6a1f1dSLionel Sambuc
191*0a6a1f1dSLionel Sambuc   The source file src/lib/libc/gen/time.c includes "namespace.h" and
192*0a6a1f1dSLionel Sambuc   <time.h> and defines `time' normally:
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc	time_t
195*0a6a1f1dSLionel Sambuc	time(time_t *t)
196*0a6a1f1dSLionel Sambuc	{
197*0a6a1f1dSLionel Sambuc	...
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc   Macro expansion replaces `time' by `_time', but the
200*0a6a1f1dSLionel Sambuc   `__RENAME(__time50)' directive on the declaration <time.h> (to which
201*0a6a1f1dSLionel Sambuc   the "namespace.h" macro expansion also applies) means the ELF global
202*0a6a1f1dSLionel Sambuc   symbol defined here is actually `__time50'.
203*0a6a1f1dSLionel Sambuc
204*0a6a1f1dSLionel Sambuc   The header file <compat/include/time.h>
205*0a6a1f1dSLionel Sambuc   (src/lib/libc/compat/include/time.h) declares
206*0a6a1f1dSLionel Sambuc
207*0a6a1f1dSLionel Sambuc	int32_t	time(int32_t *);
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc   The source file src/lib/libc/compat/gen/compat_time.c includes
210*0a6a1f1dSLionel Sambuc   "namespace.h", <compat/include/time.h>, and <time.h>, but suppresses
211*0a6a1f1dSLionel Sambuc   the normal declaration of `time' in <time.h> by defining
212*0a6a1f1dSLionel Sambuc   __LIBC12_SOURCE__.  Instead, <compat/include/time.h>
213*0a6a1f1dSLionel Sambuc   (src/lib/libc/compat/include/time.h) declares `time' with the
214*0a6a1f1dSLionel Sambuc   effective old signature:
215*0a6a1f1dSLionel Sambuc
216*0a6a1f1dSLionel Sambuc	int32_t	time(int32_t *);
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc   Then compat_time.c defines `time' normally:
219*0a6a1f1dSLionel Sambuc
220*0a6a1f1dSLionel Sambuc	time_t
221*0a6a1f1dSLionel Sambuc	time(time_t *t)
222*0a6a1f1dSLionel Sambuc	{
223*0a6a1f1dSLionel Sambuc	...
224*0a6a1f1dSLionel Sambuc
225*0a6a1f1dSLionel Sambuc   Again, macro expansion replaces `time' by `_time', but since there
226*0a6a1f1dSLionel Sambuc   is no __RENAME directive in <compat/include/time.h>, the resulting
227*0a6a1f1dSLionel Sambuc   ELF global symbol is `_time'.
228*0a6a1f1dSLionel Sambuc
229*0a6a1f1dSLionel Sambuc   Finally, alongside the definition in compat_time.c is
230*0a6a1f1dSLionel Sambuc
231*0a6a1f1dSLionel Sambuc	__weak_alias(time,_time)
232*0a6a1f1dSLionel Sambuc
233*0a6a1f1dSLionel Sambuc   to define `time' as an ELF weak symbol aliasing `_time'.
234*0a6a1f1dSLionel Sambuc
235*0a6a1f1dSLionel Sambuc   The net effect is that NetBSD 6's libc provides the same definitions
236*0a6a1f1dSLionel Sambuc   as NetBSD 5's libc for the symbols `time' and `_time', so that old
237*0a6a1f1dSLionel Sambuc   programs that were compiled in NetBSD 5 will continue to work with
238*0a6a1f1dSLionel Sambuc   NetBSD 6's libc.  But programs compiled in NetBSD 6 will have 64-bit
239*0a6a1f1dSLionel Sambuc   time_t.
240