1 @node Maintenance, Platform, Installation, Top
2 @c %MENU% How to enhance and port the GNU C Library
3 @appendix Library Maintenance
6 * Source Layout:: How to add new functions or header files
8 * Symbol handling:: How to handle symbols in the GNU C Library.
9 * Porting:: How to port the GNU C Library to
10 a new machine or operating system.
14 @appendixsec Adding New Functions
16 The process of building the library is driven by the makefiles, which
17 make heavy use of special features of GNU @code{make}. The makefiles
18 are very complex, and you probably don't want to try to understand them.
19 But what they do is fairly straightforward, and only requires that you
20 define a few variables in the right places.
22 The library sources are divided into subdirectories, grouped by topic.
24 The @file{string} subdirectory has all the string-manipulation
25 functions, @file{math} has all the mathematical functions, etc.
27 Each subdirectory contains a simple makefile, called @file{Makefile},
28 which defines a few @code{make} variables and then includes the global
29 makefile @file{Rules} with a line like:
36 The basic variables that a subdirectory makefile defines are:
40 The name of the subdirectory, for example @file{stdio}.
41 This variable @strong{must} be defined.
44 The names of the header files in this section of the library,
45 such as @file{stdio.h}.
49 The names of the modules (source files) in this section of the library.
50 These should be simple names, such as @samp{strlen} (rather than
51 complete file names, such as @file{strlen.c}). Use @code{routines} for
52 modules that define functions in the library, and @code{aux} for
53 auxiliary modules containing things like data definitions. But the
54 values of @code{routines} and @code{aux} are just concatenated, so there
55 really is no practical difference.@refill
58 The names of test programs for this section of the library. These
59 should be simple names, such as @samp{tester} (rather than complete file
60 names, such as @file{tester.c}). @w{@samp{make tests}} will build and
61 run all the test programs. If a test program needs input, put the test
62 data in a file called @file{@var{test-program}.input}; it will be given to
63 the test program on its standard input. If a test program wants to be
64 run with arguments, put the arguments (all on a single line) in a file
65 called @file{@var{test-program}.args}. Test programs should exit with
66 zero status when the test passes, and nonzero status when the test
67 indicates a bug in the library or error in building.
70 The names of ``other'' programs associated with this section of the
71 library. These are programs which are not tests per se, but are other
72 small programs included with the library. They are built by
73 @w{@samp{make others}}.@refill
78 Files to be installed by @w{@samp{make install}}. Files listed in
79 @samp{install-lib} are installed in the directory specified by
80 @samp{libdir} in @file{configparms} or @file{Makeconfig}
81 (@pxref{Installation}). Files listed in @code{install-data} are
82 installed in the directory specified by @samp{datadir} in
83 @file{configparms} or @file{Makeconfig}. Files listed in @code{install}
84 are installed in the directory specified by @samp{bindir} in
85 @file{configparms} or @file{Makeconfig}.@refill
88 Other files from this subdirectory which should be put into a
89 distribution tar file. You need not list here the makefile itself or
90 the source and header files listed in the other standard variables.
91 Only define @code{distribute} if there are files used in an unusual way
92 that should go into the distribution.
95 Files which are generated by @file{Makefile} in this subdirectory.
96 These files will be removed by @w{@samp{make clean}}, and they will
97 never go into a distribution.
100 Extra object files which are built by @file{Makefile} in this
101 subdirectory. This should be a list of file names like @file{foo.o};
102 the files will actually be found in whatever directory object files are
103 being built in. These files will be removed by @w{@samp{make clean}}.
104 This variable is used for secondary object files needed to build
105 @code{others} or @code{tests}.
109 * Platform: Adding Platform-specific. Adding platform-specific
113 @node Adding Platform-specific
114 @appendixsubsec Platform-specific types, macros and functions
116 It's sometimes necessary to provide nonstandard, platform-specific
117 features to developers. The C library is traditionally the
118 lowest library layer, so it makes sense for it to provide these
119 low-level features. However, including these features in the C
120 library may be a disadvantage if another package provides them
121 as well as there will be two conflicting versions of them. Also,
122 the features won't be available to projects that do not use
123 @theglibc{} but use other GNU tools, like GCC.
125 The current guidelines are:
128 If the header file provides features that only make sense on a particular
129 machine architecture and have nothing to do with an operating system, then
130 the features should ultimately be provided as GCC built-in functions. Until
131 then, @theglibc{} may provide them in the header file. When the GCC built-in
132 functions become available, those provided in the header file should be made
133 conditionally available prior to the GCC version in which the built-in
134 function was made available.
137 If the header file provides features that are specific to an operating system,
138 both GCC and @theglibc{} could provide it, but @theglibc{} is preferred
139 as it already has a lot of information about the operating system.
142 If the header file provides features that are specific to an operating system
143 but used by @theglibc{}, then @theglibc{} should provide them.
146 The general solution for providing low-level features is to export them as
151 A nonstandard, low-level header file that defines macros and inline
152 functions should be called @file{sys/platform/@var{name}.h}.
155 Each header file's name should include the platform name, to avoid
156 users thinking there is anything in common between the different
157 header files for different platforms. For example, a
158 @file{sys/platform/@var{arch}.h} name such as
159 @file{sys/platform/ppc.h} is better than @file{sys/platform.h}.
162 A platform-specific header file provided by @theglibc{} should coordinate
163 with GCC such that compiler built-in versions of the functions and macros are
164 preferred if available. This means that user programs will only ever need to
165 include @file{sys/platform/@var{arch}.h}, keeping the same names of types,
166 macros, and functions for convenience and portability.
169 Each included symbol must have the prefix @code{__@var{arch}_}, such as
170 @code{__ppc_get_timebase}.
174 The easiest way to provide a header file is to add it to the
175 @code{sysdep_headers} variable. For example, the combination of
176 Linux-specific header files on PowerPC could be provided like this:
179 sysdep_headers += sys/platform/ppc.h
182 Then ensure that you have added a @file{sys/platform/ppc.h}
183 header file in the machine-specific directory, e.g.,
184 @file{sysdeps/powerpc/sys/platform/ppc.h}.
187 @node Symbol handling
188 @appendixsec Symbol handling in the GNU C Library
191 * 64-bit time symbol handling :: How to handle 64-bit time related
192 symbols in the GNU C Library.
195 @node 64-bit time symbol handling
196 @appendixsubsec 64-bit time symbol handling in the GNU C Library
198 With respect to time handling, @glibcadj{} configurations fall in two
199 classes depending on the value of @code{__TIMESIZE}:
203 @item @code{__TIMESIZE == 32}
205 These @dfn{dual-time} configurations have both 32-bit and 64-bit time
206 support. 32-bit time support provides type @code{time_t} and cannot
207 handle dates beyond @dfn{Y2038}. 64-bit time support provides type
208 @code{__time64_t} and can handle dates beyond @dfn{Y2038}.
210 In these configurations, time-related types have two declarations,
211 a 64-bit one, and a 32-bit one; and time-related functions generally
212 have two definitions: a 64-bit one, and a 32-bit one which is a wrapper
213 around the former. Therefore, for every @code{time_t}-related symbol,
214 there is a corresponding @code{__time64_t}-related symbol, the name of
215 which is usually the 32-bit symbol's name with @code{__} (a double
216 underscore) prepended and @code{64} appended. For instance, the
217 64-bit-time counterpart of @code{clock_gettime} is
218 @code{__clock_gettime64}.
220 @item @code{__TIMESIZE == 64}
222 These @dfn{single-time} configurations only have a 64-bit @code{time_t}
223 and related functions, which can handle dates beyond 2038-01-19
224 03:14:07 (aka @dfn{Y2038}).
226 In these configurations, time-related types only have a 64-bit
227 declaration; and time-related functions only have one 64-bit definition.
228 However, for every @code{time_t}-related symbol, there is a
229 corresponding @code{__time64_t}-related macro, the name of which is
230 derived as in the dual-time configuration case, and which expands to
231 the symbol's name. For instance, the macro @code{__clock_gettime64}
232 expands to @code{clock_gettime}.
234 These macros are purely internal to @theglibc{} and exist only so that
235 a single definition of the 64-bit time functions can be used on both
236 single-time and dual-time configurations, and so that glibc code can
237 freely call the 64-bit functions internally in all configurations.
241 @c The following paragraph should be removed once external interfaces
242 @c get support for both time sizes.
244 Note: at this point, 64-bit time support in dual-time configurations is
245 work-in-progress, so for these configurations, the public API only makes
246 the 32-bit time support available. In a later change, the public API
247 will allow user code to choose the time size for a given compilation
250 64-bit variants of time-related types or functions are defined for all
251 configurations and use 64-bit-time symbol names (for dual-time
252 configurations) or macros (for single-time configurations).
254 32-bit variants of time-related types or functions are defined only for
255 dual-time configurations.
257 Here is an example with @code{localtime}:
259 Function @code{localtime} is declared in @file{time/time.h} as
261 extern struct tm *localtime (const time_t *__timer) __THROW;
262 libc_hidden_proto (localtime)
265 For single-time configurations, @code{__localtime64} is a macro which
266 evaluates to @code{localtime}; for dual-time configurations,
267 @code{__localtime64} is a function similar to @code{localtime} except
268 it uses Y2038-proof types:
271 # define __localtime64 localtime
273 extern struct tm *__localtime64 (const __time64_t *__timer) __THROW;
274 libc_hidden_proto (__localtime64)
278 (note: type @code{time_t} is replaced with @code{__time64_t} because
279 @code{time_t} is not Y2038-proof, but @code{struct tm} is not
280 replaced because it is already Y2038-proof.)
282 The 64-bit-time implementation of @code{localtime} is written as follows
283 and is compiled for both dual-time and single-time configuration classes.
287 __localtime64 (const __time64_t *t)
289 return __tz_convert (*t, 1, &_tmbuf);
291 libc_hidden_def (__localtime64)
294 The 32-bit-time implementation is a wrapper and is only compiled for
295 dual-time configurations:
301 localtime (const time_t *t)
304 return __localtime64 (&t64);
306 libc_hidden_def (localtime)
312 @appendixsec Porting @theglibc{}
314 @Theglibc{} is written to be easily portable to a variety of
315 machines and operating systems. Machine- and operating system-dependent
316 functions are well separated to make it easy to add implementations for
317 new machines or operating systems. This section describes the layout of
318 the library source tree and explains the mechanisms used to select
319 machine-dependent code to use.
321 All the machine-dependent and operating system-dependent files in the
322 library are in the subdirectory @file{sysdeps} under the top-level
323 library source directory. This directory contains a hierarchy of
324 subdirectories (@pxref{Hierarchy Conventions}).
326 Each subdirectory of @file{sysdeps} contains source files for a
327 particular machine or operating system, or for a class of machine or
328 operating system (for example, systems by a particular vendor, or all
329 machines that use IEEE 754 floating-point format). A configuration
330 specifies an ordered list of these subdirectories. Each subdirectory
331 implicitly appends its parent directory to the list. For example,
332 specifying the list @file{unix/bsd/vax} is equivalent to specifying the
333 list @file{unix/bsd/vax unix/bsd unix}. A subdirectory can also specify
334 that it implies other subdirectories which are not directly above it in
335 the directory hierarchy. If the file @file{Implies} exists in a
336 subdirectory, it lists other subdirectories of @file{sysdeps} which are
337 appended to the list, appearing after the subdirectory containing the
338 @file{Implies} file. Lines in an @file{Implies} file that begin with a
339 @samp{#} character are ignored as comments. For example,
340 @file{unix/bsd/Implies} contains:@refill
342 # BSD has Internet-related things.
346 and @file{unix/Implies} contains:
353 So the final list is @file{unix/bsd/vax unix/bsd unix/inet unix posix}.
355 @file{sysdeps} has a ``special'' subdirectory called @file{generic}. It
356 is always implicitly appended to the list of subdirectories, so you
357 needn't put it in an @file{Implies} file, and you should not create any
358 subdirectories under it intended to be new specific categories.
359 @file{generic} serves two purposes. First, the makefiles do not bother
360 to look for a system-dependent version of a file that's not in
361 @file{generic}. This means that any system-dependent source file must
362 have an analogue in @file{generic}, even if the routines defined by that
363 file are not implemented on other platforms. Second, the @file{generic}
364 version of a system-dependent file is used if the makefiles do not find
365 a version specific to the system you're compiling for.
367 If it is possible to implement the routines in a @file{generic} file in
368 machine-independent C, using only other machine-independent functions in
369 the C library, then you should do so. Otherwise, make them stubs. A
370 @dfn{stub} function is a function which cannot be implemented on a
371 particular machine or operating system. Stub functions always return an
372 error, and set @code{errno} to @code{ENOSYS} (Function not implemented).
373 @xref{Error Reporting}. If you define a stub function, you must place
374 the statement @code{stub_warning(@var{function})}, where @var{function}
375 is the name of your function, after its definition. This causes the
376 function to be listed in the installed @code{<gnu/stubs.h>}, and
377 makes GNU ld warn when the function is used.
379 Some rare functions are only useful on specific systems and aren't
380 defined at all on others; these do not appear anywhere in the
381 system-independent source code or makefiles (including the
382 @file{generic} directory), only in the system-dependent @file{Makefile}
383 in the specific system's subdirectory.
385 If you come across a file that is in one of the main source directories
386 (@file{string}, @file{stdio}, etc.), and you want to write a machine- or
387 operating system-dependent version of it, move the file into
388 @file{sysdeps/generic} and write your new implementation in the
389 appropriate system-specific subdirectory. Note that if a file is to be
390 system-dependent, it @strong{must not} appear in one of the main source
393 There are a few special files that may exist in each subdirectory of
396 @comment Blank lines after items make the table look better.
400 A makefile for this machine or operating system, or class of machine or
401 operating system. This file is included by the library makefile
402 @file{Makerules}, which is used by the top-level makefile and the
403 subdirectory makefiles. It can change the variables set in the
404 including makefile or add new rules. It can use GNU @code{make}
405 conditional directives based on the variable @samp{subdir} (see above) to
406 select different sets of variables and rules for different sections of
407 the library. It can also set the @code{make} variable
408 @samp{sysdep-routines}, to specify extra modules to be included in the
409 library. You should use @samp{sysdep-routines} rather than adding
410 modules to @samp{routines} because the latter is used in determining
411 what to distribute for each subdirectory of the main source tree.@refill
413 Each makefile in a subdirectory in the ordered list of subdirectories to
414 be searched is included in order. Since several system-dependent
415 makefiles may be included, each should append to @samp{sysdep-routines}
416 rather than simply setting it:
419 sysdep-routines := $(sysdep-routines) foo bar
425 This file contains the names of new whole subdirectories under the
426 top-level library source tree that should be included for this system.
427 These subdirectories are treated just like the system-independent
428 subdirectories in the library source tree, such as @file{stdio} and
431 Use this when there are completely new sets of functions and header
432 files that should go into the library for the system this subdirectory
433 of @file{sysdeps} implements. For example,
434 @file{sysdeps/unix/inet/Subdirs} contains @file{inet}; the @file{inet}
435 directory contains various network-oriented operations which only make
436 sense to put in the library on systems that support the Internet.@refill
440 This file is a shell script fragment to be run at configuration time.
441 The top-level @file{configure} script uses the shell @code{.} command to
442 read the @file{configure} file in each system-dependent directory
443 chosen, in order. The @file{configure} files are often generated from
444 @file{configure.ac} files using Autoconf.
446 A system-dependent @file{configure} script will usually add things to
447 the shell variables @samp{DEFS} and @samp{config_vars}; see the
448 top-level @file{configure} script for details. The script can check for
449 @w{@samp{--with-@var{package}}} options that were passed to the
450 top-level @file{configure}. For an option
451 @w{@samp{--with-@var{package}=@var{value}}} @file{configure} sets the
452 shell variable @w{@samp{with_@var{package}}} (with any dashes in
453 @var{package} converted to underscores) to @var{value}; if the option is
454 just @w{@samp{--with-@var{package}}} (no argument), then it sets
455 @w{@samp{with_@var{package}}} to @samp{yes}.
459 This file is an Autoconf input fragment to be processed into the file
460 @file{configure} in this subdirectory. @xref{Introduction,,,
461 autoconf.info, Autoconf: Generating Automatic Configuration Scripts},
462 for a description of Autoconf. You should write either @file{configure}
463 or @file{configure.ac}, but not both. The first line of
464 @file{configure.ac} should invoke the @code{m4} macro
465 @samp{GLIBC_PROVIDES}. This macro does several @code{AC_PROVIDE} calls
466 for Autoconf macros which are used by the top-level @file{configure}
467 script; without this, those macros might be invoked again unnecessarily
471 That is the general system for how system-dependencies are isolated.
473 The next section explains how to decide what directories in
474 @file{sysdeps} to use. @ref{Porting to Unix}, has some tips on porting
475 the library to Unix variants.
479 * Hierarchy Conventions:: The layout of the @file{sysdeps} hierarchy.
480 * Porting to Unix:: Porting the library to an average
484 @node Hierarchy Conventions
485 @appendixsubsec Layout of the @file{sysdeps} Directory Hierarchy
487 A GNU configuration name has three parts: the CPU type, the
488 manufacturer's name, and the operating system. @file{configure} uses
489 these to pick the list of system-dependent directories to look for. If
490 the @samp{--nfp} option is @emph{not} passed to @file{configure}, the
491 directory @file{@var{machine}/fpu} is also used. The operating system
492 often has a @dfn{base operating system}; for example, if the operating
493 system is @samp{Linux}, the base operating system is @samp{unix/sysv}.
494 The algorithm used to pick the list of directories is simple:
495 @file{configure} makes a list of the base operating system,
496 manufacturer, CPU type, and operating system, in that order. It then
497 concatenates all these together with slashes in between, to produce a
498 directory name; for example, the configuration @w{@samp{i686-linux-gnu}}
499 results in @file{unix/sysv/linux/i386/i686}. @file{configure} then
500 tries removing each element of the list in turn, so
501 @file{unix/sysv/linux} and @file{unix/sysv} are also tried, among others.
502 Since the precise version number of the operating system is often not
503 important, and it would be very inconvenient, for example, to have
504 identical @file{irix6.2} and @file{irix6.3} directories,
505 @file{configure} tries successively less specific operating system names
506 by removing trailing suffixes starting with a period.
508 As an example, here is the complete list of directories that would be
509 tried for the configuration @w{@samp{i686-linux-gnu}}:
513 sysdeps/unix/sysv/linux/i386
514 sysdeps/unix/sysv/linux
519 sysdeps/unix/sysv/i386/i686
520 sysdeps/unix/sysv/i386
527 sysdeps/libm-i387/i686
537 Different machine architectures are conventionally subdirectories at the
538 top level of the @file{sysdeps} directory tree. For example,
539 @w{@file{sysdeps/sparc}} and @w{@file{sysdeps/m68k}}. These contain
540 files specific to those machine architectures, but not specific to any
541 particular operating system. There might be subdirectories for
542 specializations of those architectures, such as
543 @w{@file{sysdeps/m68k/68020}}. Code which is specific to the
544 floating-point coprocessor used with a particular machine should go in
545 @w{@file{sysdeps/@var{machine}/fpu}}.
547 There are a few directories at the top level of the @file{sysdeps}
548 hierarchy that are not for particular machine architectures.
552 As described above (@pxref{Porting}), this is the subdirectory
553 that every configuration implicitly uses after all others.
556 This directory is for code using the IEEE 754 floating-point format,
557 where the C type @code{float} is IEEE 754 single-precision format, and
558 @code{double} is IEEE 754 double-precision format. Usually this
559 directory is referred to in the @file{Implies} file in a machine
560 architecture-specific directory, such as @file{m68k/Implies}.
563 This directory contains an implementation of a mathematical library
564 usable on platforms which use @w{IEEE 754} conformant floating-point
568 This is a special case. Ideally the code should be in
569 @file{sysdeps/i386/fpu} but for various reasons it is kept aside.
572 This directory contains implementations of things in the library in
573 terms of @sc{POSIX.1} functions. This includes some of the @sc{POSIX.1}
574 functions themselves. Of course, @sc{POSIX.1} cannot be completely
575 implemented in terms of itself, so a configuration using just
576 @file{posix} cannot be complete.
579 This is the directory for Unix-like things. @xref{Porting to Unix}.
580 @file{unix} implies @file{posix}. There are some special-purpose
581 subdirectories of @file{unix}:
585 This directory is for things common to both BSD and System V release 4.
586 Both @file{unix/bsd} and @file{unix/sysv/sysv4} imply @file{unix/common}.
589 This directory is for @code{socket} and related functions on Unix systems.
590 @file{unix/inet/Subdirs} enables the @file{inet} top-level subdirectory.
591 @file{unix/common} implies @file{unix/inet}.
595 This is the directory for things based on the Mach microkernel from CMU
596 (including @gnuhurdsystems{}). Other basic operating systems
597 (VMS, for example) would have their own directories at the top level of
598 the @file{sysdeps} hierarchy, parallel to @file{unix} and @file{mach}.
601 @node Porting to Unix
602 @appendixsubsec Porting @theglibc{} to Unix Systems
604 Most Unix systems are fundamentally very similar. There are variations
605 between different machines, and variations in what facilities are
606 provided by the kernel. But the interface to the operating system
607 facilities is, for the most part, pretty uniform and simple.
609 The code for Unix systems is in the directory @file{unix}, at the top
610 level of the @file{sysdeps} hierarchy. This directory contains
611 subdirectories (and subdirectory trees) for various Unix variants.
613 The functions which are system calls in most Unix systems are
614 implemented in assembly code, which is generated automatically from
615 specifications in files named @file{syscalls.list}. There are several
616 such files, one in @file{sysdeps/unix} and others in its subdirectories.
617 Some special system calls are implemented in files that are named with a
618 suffix of @samp{.S}; for example, @file{_exit.S}. Files ending in
619 @samp{.S} are run through the C preprocessor before being fed to the
622 These files all use a set of macros that should be defined in
623 @file{sysdep.h}. The @file{sysdep.h} file in @file{sysdeps/unix}
624 partially defines them; a @file{sysdep.h} file in another directory must
625 finish defining them for the particular machine and operating system
626 variant. See @file{sysdeps/unix/sysdep.h} and the machine-specific
627 @file{sysdep.h} implementations to see what these macros are and what
628 they should do.@refill
630 The system-specific makefile for the @file{unix} directory
631 (@file{sysdeps/unix/Makefile}) gives rules to generate several files
632 from the Unix system you are building the library on (which is assumed
633 to be the target system you are building the library @emph{for}). All
634 the generated files are put in the directory where the object files are
635 kept; they should not affect the source tree itself. The files
636 generated are @file{ioctls.h}, @file{errnos.h}, @file{sys/param.h}, and
637 @file{errlist.c} (for the @file{stdio} section of the library).
640 @c This section might be a good idea if it is finished,
641 @c but there's no point including it as it stands. --rms
642 @c @appendixsec Compatibility with Traditional C
644 @c ??? This section is really short now. Want to keep it? --roland
646 @c It's not anymore true. glibc 2.1 cannot be used with K&R compilers.
649 Although @theglibc{} implements the @w{ISO C} library facilities, you
650 @emph{can} use @theglibc{} with traditional, ``pre-ISO'' C
651 compilers. However, you need to be careful because the content and
652 organization of the @glibcadj{} header files differs from that of
653 traditional C implementations. This means you may need to make changes
654 to your program in order to get it to compile.