blob: 862b49d956dabff632c0b62963bd074e3effc8f5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001@node Maintenance, Platform, Installation, Top
2@c %MENU% How to enhance and port the GNU C Library
3@appendix Library Maintenance
4
5@menu
6* Source Layout:: How to add new functions or header files
7 to the GNU C Library.
8* Porting:: How to port the GNU C Library to
9 a new machine or operating system.
10@end menu
11
12@node Source Layout
13@appendixsec Adding New Functions
14
15The process of building the library is driven by the makefiles, which
16make heavy use of special features of GNU @code{make}. The makefiles
17are very complex, and you probably don't want to try to understand them.
18But what they do is fairly straightforward, and only requires that you
19define a few variables in the right places.
20
21The library sources are divided into subdirectories, grouped by topic.
22
23The @file{string} subdirectory has all the string-manipulation
24functions, @file{math} has all the mathematical functions, etc.
25
26Each subdirectory contains a simple makefile, called @file{Makefile},
27which defines a few @code{make} variables and then includes the global
28makefile @file{Rules} with a line like:
29
30@smallexample
31include ../Rules
32@end smallexample
33
34@noindent
35The basic variables that a subdirectory makefile defines are:
36
37@table @code
38@item subdir
39The name of the subdirectory, for example @file{stdio}.
40This variable @strong{must} be defined.
41
42@item headers
43The names of the header files in this section of the library,
44such as @file{stdio.h}.
45
46@item routines
47@itemx aux
48The names of the modules (source files) in this section of the library.
49These should be simple names, such as @samp{strlen} (rather than
50complete file names, such as @file{strlen.c}). Use @code{routines} for
51modules that define functions in the library, and @code{aux} for
52auxiliary modules containing things like data definitions. But the
53values of @code{routines} and @code{aux} are just concatenated, so there
54really is no practical difference.@refill
55
56@item tests
57The names of test programs for this section of the library. These
58should be simple names, such as @samp{tester} (rather than complete file
59names, such as @file{tester.c}). @w{@samp{make tests}} will build and
60run all the test programs. If a test program needs input, put the test
61data in a file called @file{@var{test-program}.input}; it will be given to
62the test program on its standard input. If a test program wants to be
63run with arguments, put the arguments (all on a single line) in a file
64called @file{@var{test-program}.args}. Test programs should exit with
65zero status when the test passes, and nonzero status when the test
66indicates a bug in the library or error in building.
67
68@item others
69The names of ``other'' programs associated with this section of the
70library. These are programs which are not tests per se, but are other
71small programs included with the library. They are built by
72@w{@samp{make others}}.@refill
73
74@item install-lib
75@itemx install-data
76@itemx install
77Files to be installed by @w{@samp{make install}}. Files listed in
78@samp{install-lib} are installed in the directory specified by
79@samp{libdir} in @file{configparms} or @file{Makeconfig}
80(@pxref{Installation}). Files listed in @code{install-data} are
81installed in the directory specified by @samp{datadir} in
82@file{configparms} or @file{Makeconfig}. Files listed in @code{install}
83are installed in the directory specified by @samp{bindir} in
84@file{configparms} or @file{Makeconfig}.@refill
85
86@item distribute
87Other files from this subdirectory which should be put into a
88distribution tar file. You need not list here the makefile itself or
89the source and header files listed in the other standard variables.
90Only define @code{distribute} if there are files used in an unusual way
91that should go into the distribution.
92
93@item generated
94Files which are generated by @file{Makefile} in this subdirectory.
95These files will be removed by @w{@samp{make clean}}, and they will
96never go into a distribution.
97
98@item extra-objs
99Extra object files which are built by @file{Makefile} in this
100subdirectory. This should be a list of file names like @file{foo.o};
101the files will actually be found in whatever directory object files are
102being built in. These files will be removed by @w{@samp{make clean}}.
103This variable is used for secondary object files needed to build
104@code{others} or @code{tests}.
105@end table
106
107@menu
108* Platform: Adding Platform-specific. Adding platform-specific
109 features.
110@end menu
111
112@node Adding Platform-specific
113@appendixsubsec Platform-specific types, macros and functions
114
115It's sometimes necessary to provide nonstandard, platform-specific
116features to developers. The C library is traditionally the
117lowest library layer, so it makes sense for it to provide these
118low-level features. However, including these features in the C
119library may be a disadvantage if another package provides them
120as well as there will be two conflicting versions of them. Also,
121the features won't be available to projects that do not use
122@theglibc{} but use other GNU tools, like GCC.
123
124The current guidelines are:
125@itemize @bullet
126@item
127If the header file provides features that only make sense on a particular
128machine architecture and have nothing to do with an operating system, then
129the features should ultimately be provided as GCC built-in functions. Until
130then, @theglibc{} may provide them in the header file. When the GCC built-in
131functions become available, those provided in the header file should be made
132conditionally available prior to the GCC version in which the built-in
133function was made available.
134
135@item
136If the header file provides features that are specific to an operating system,
137both GCC and @theglibc{} could provide it, but @theglibc{} is preferred
138as it already has a lot of information about the operating system.
139
140@item
141If the header file provides features that are specific to an operating system
142but used by @theglibc{}, then @theglibc{} should provide them.
143@end itemize
144
145The general solution for providing low-level features is to export them as
146follows:
147
148@itemize @bullet
149@item
150A nonstandard, low-level header file that defines macros and inline
151functions should be called @file{sys/platform/@var{name}.h}.
152
153@item
154Each header file's name should include the platform name, to avoid
155users thinking there is anything in common between different the
156header files for different platforms. For example, a
157@file{sys/platform/@var{arch}.h} name such as
158@file{sys/platform/ppc.h} is better than @file{sys/platform.h}.
159
160@item
161A platform-specific header file provided by @theglibc{} should coordinate
162with GCC such that compiler built-in versions of the functions and macros are
163preferred if available. This means that user programs will only ever need to
164include @file{sys/platform/@var{arch}.h}, keeping the same names of types,
165macros, and functions for convenience and portability.
166
167@item
168Each included symbol must have the prefix @code{__@var{arch}_}, such as
169@code{__ppc_get_timebase}.
170@end itemize
171
172
173The easiest way to provide a header file is to add it to the
174@code{sysdep_headers} variable. For example, the combination of
175Linux-specific header files on PowerPC could be provided like this:
176
177@smallexample
178sysdep_headers += sys/platform/ppc.h
179@end smallexample
180
181Then ensure that you have added a @file{sys/platform/ppc.h}
182header file in the machine-specific directory, e.g.,
183@file{sysdeps/powerpc/sys/platform/ppc.h}.
184
185
186@node Porting
187@appendixsec Porting @theglibc{}
188
189@Theglibc{} is written to be easily portable to a variety of
190machines and operating systems. Machine- and operating system-dependent
191functions are well separated to make it easy to add implementations for
192new machines or operating systems. This section describes the layout of
193the library source tree and explains the mechanisms used to select
194machine-dependent code to use.
195
196All the machine-dependent and operating system-dependent files in the
197library are in the subdirectory @file{sysdeps} under the top-level
198library source directory. This directory contains a hierarchy of
199subdirectories (@pxref{Hierarchy Conventions}).
200
201Each subdirectory of @file{sysdeps} contains source files for a
202particular machine or operating system, or for a class of machine or
203operating system (for example, systems by a particular vendor, or all
204machines that use IEEE 754 floating-point format). A configuration
205specifies an ordered list of these subdirectories. Each subdirectory
206implicitly appends its parent directory to the list. For example,
207specifying the list @file{unix/bsd/vax} is equivalent to specifying the
208list @file{unix/bsd/vax unix/bsd unix}. A subdirectory can also specify
209that it implies other subdirectories which are not directly above it in
210the directory hierarchy. If the file @file{Implies} exists in a
211subdirectory, it lists other subdirectories of @file{sysdeps} which are
212appended to the list, appearing after the subdirectory containing the
213@file{Implies} file. Lines in an @file{Implies} file that begin with a
214@samp{#} character are ignored as comments. For example,
215@file{unix/bsd/Implies} contains:@refill
216@smallexample
217# BSD has Internet-related things.
218unix/inet
219@end smallexample
220@noindent
221and @file{unix/Implies} contains:
222@need 300
223@smallexample
224posix
225@end smallexample
226
227@noindent
228So the final list is @file{unix/bsd/vax unix/bsd unix/inet unix posix}.
229
230@file{sysdeps} has a ``special'' subdirectory called @file{generic}. It
231is always implicitly appended to the list of subdirectories, so you
232needn't put it in an @file{Implies} file, and you should not create any
233subdirectories under it intended to be new specific categories.
234@file{generic} serves two purposes. First, the makefiles do not bother
235to look for a system-dependent version of a file that's not in
236@file{generic}. This means that any system-dependent source file must
237have an analogue in @file{generic}, even if the routines defined by that
238file are not implemented on other platforms. Second, the @file{generic}
239version of a system-dependent file is used if the makefiles do not find
240a version specific to the system you're compiling for.
241
242If it is possible to implement the routines in a @file{generic} file in
243machine-independent C, using only other machine-independent functions in
244the C library, then you should do so. Otherwise, make them stubs. A
245@dfn{stub} function is a function which cannot be implemented on a
246particular machine or operating system. Stub functions always return an
247error, and set @code{errno} to @code{ENOSYS} (Function not implemented).
248@xref{Error Reporting}. If you define a stub function, you must place
249the statement @code{stub_warning(@var{function})}, where @var{function}
250is the name of your function, after its definition. This causes the
251function to be listed in the installed @code{<gnu/stubs.h>}, and
252makes GNU ld warn when the function is used.
253
254Some rare functions are only useful on specific systems and aren't
255defined at all on others; these do not appear anywhere in the
256system-independent source code or makefiles (including the
257@file{generic} directory), only in the system-dependent @file{Makefile}
258in the specific system's subdirectory.
259
260If you come across a file that is in one of the main source directories
261(@file{string}, @file{stdio}, etc.), and you want to write a machine- or
262operating system-dependent version of it, move the file into
263@file{sysdeps/generic} and write your new implementation in the
264appropriate system-specific subdirectory. Note that if a file is to be
265system-dependent, it @strong{must not} appear in one of the main source
266directories.@refill
267
268There are a few special files that may exist in each subdirectory of
269@file{sysdeps}:
270
271@comment Blank lines after items make the table look better.
272@table @file
273@item Makefile
274
275A makefile for this machine or operating system, or class of machine or
276operating system. This file is included by the library makefile
277@file{Makerules}, which is used by the top-level makefile and the
278subdirectory makefiles. It can change the variables set in the
279including makefile or add new rules. It can use GNU @code{make}
280conditional directives based on the variable @samp{subdir} (see above) to
281select different sets of variables and rules for different sections of
282the library. It can also set the @code{make} variable
283@samp{sysdep-routines}, to specify extra modules to be included in the
284library. You should use @samp{sysdep-routines} rather than adding
285modules to @samp{routines} because the latter is used in determining
286what to distribute for each subdirectory of the main source tree.@refill
287
288Each makefile in a subdirectory in the ordered list of subdirectories to
289be searched is included in order. Since several system-dependent
290makefiles may be included, each should append to @samp{sysdep-routines}
291rather than simply setting it:
292
293@smallexample
294sysdep-routines := $(sysdep-routines) foo bar
295@end smallexample
296
297@need 1000
298@item Subdirs
299
300This file contains the names of new whole subdirectories under the
301top-level library source tree that should be included for this system.
302These subdirectories are treated just like the system-independent
303subdirectories in the library source tree, such as @file{stdio} and
304@file{math}.
305
306Use this when there are completely new sets of functions and header
307files that should go into the library for the system this subdirectory
308of @file{sysdeps} implements. For example,
309@file{sysdeps/unix/inet/Subdirs} contains @file{inet}; the @file{inet}
310directory contains various network-oriented operations which only make
311sense to put in the library on systems that support the Internet.@refill
312
313@item configure
314
315This file is a shell script fragment to be run at configuration time.
316The top-level @file{configure} script uses the shell @code{.} command to
317read the @file{configure} file in each system-dependent directory
318chosen, in order. The @file{configure} files are often generated from
319@file{configure.ac} files using Autoconf.
320
321A system-dependent @file{configure} script will usually add things to
322the shell variables @samp{DEFS} and @samp{config_vars}; see the
323top-level @file{configure} script for details. The script can check for
324@w{@samp{--with-@var{package}}} options that were passed to the
325top-level @file{configure}. For an option
326@w{@samp{--with-@var{package}=@var{value}}} @file{configure} sets the
327shell variable @w{@samp{with_@var{package}}} (with any dashes in
328@var{package} converted to underscores) to @var{value}; if the option is
329just @w{@samp{--with-@var{package}}} (no argument), then it sets
330@w{@samp{with_@var{package}}} to @samp{yes}.
331
332@item configure.ac
333
334This file is an Autoconf input fragment to be processed into the file
335@file{configure} in this subdirectory. @xref{Introduction,,,
336autoconf.info, Autoconf: Generating Automatic Configuration Scripts},
337for a description of Autoconf. You should write either @file{configure}
338or @file{configure.ac}, but not both. The first line of
339@file{configure.ac} should invoke the @code{m4} macro
340@samp{GLIBC_PROVIDES}. This macro does several @code{AC_PROVIDE} calls
341for Autoconf macros which are used by the top-level @file{configure}
342script; without this, those macros might be invoked again unnecessarily
343by Autoconf.
344@end table
345
346That is the general system for how system-dependencies are isolated.
347@iftex
348The next section explains how to decide what directories in
349@file{sysdeps} to use. @ref{Porting to Unix}, has some tips on porting
350the library to Unix variants.
351@end iftex
352
353@menu
354* Hierarchy Conventions:: The layout of the @file{sysdeps} hierarchy.
355* Porting to Unix:: Porting the library to an average
356 Unix-like system.
357@end menu
358
359@node Hierarchy Conventions
360@appendixsubsec Layout of the @file{sysdeps} Directory Hierarchy
361
362A GNU configuration name has three parts: the CPU type, the
363manufacturer's name, and the operating system. @file{configure} uses
364these to pick the list of system-dependent directories to look for. If
365the @samp{--nfp} option is @emph{not} passed to @file{configure}, the
366directory @file{@var{machine}/fpu} is also used. The operating system
367often has a @dfn{base operating system}; for example, if the operating
368system is @samp{Linux}, the base operating system is @samp{unix/sysv}.
369The algorithm used to pick the list of directories is simple:
370@file{configure} makes a list of the base operating system,
371manufacturer, CPU type, and operating system, in that order. It then
372concatenates all these together with slashes in between, to produce a
373directory name; for example, the configuration @w{@samp{i686-linux-gnu}}
374results in @file{unix/sysv/linux/i386/i686}. @file{configure} then
375tries removing each element of the list in turn, so
376@file{unix/sysv/linux} and @file{unix/sysv} are also tried, among others.
377Since the precise version number of the operating system is often not
378important, and it would be very inconvenient, for example, to have
379identical @file{irix6.2} and @file{irix6.3} directories,
380@file{configure} tries successively less specific operating system names
381by removing trailing suffixes starting with a period.
382
383As an example, here is the complete list of directories that would be
384tried for the configuration @w{@samp{i686-linux-gnu}} (with the
385@file{crypt} and @file{linuxthreads} add-on):
386
387@smallexample
388sysdeps/i386/elf
389crypt/sysdeps/unix
390linuxthreads/sysdeps/unix/sysv/linux
391linuxthreads/sysdeps/pthread
392linuxthreads/sysdeps/unix/sysv
393linuxthreads/sysdeps/unix
394linuxthreads/sysdeps/i386/i686
395linuxthreads/sysdeps/i386
396linuxthreads/sysdeps/pthread/no-cmpxchg
397sysdeps/unix/sysv/linux/i386
398sysdeps/unix/sysv/linux
399sysdeps/gnu
400sysdeps/unix/common
401sysdeps/unix/mman
402sysdeps/unix/inet
403sysdeps/unix/sysv/i386/i686
404sysdeps/unix/sysv/i386
405sysdeps/unix/sysv
406sysdeps/unix/i386
407sysdeps/unix
408sysdeps/posix
409sysdeps/i386/i686
410sysdeps/i386/i486
411sysdeps/libm-i387/i686
412sysdeps/i386/fpu
413sysdeps/libm-i387
414sysdeps/i386
415sysdeps/wordsize-32
416sysdeps/ieee754
417sysdeps/libm-ieee754
418sysdeps/generic
419@end smallexample
420
421Different machine architectures are conventionally subdirectories at the
422top level of the @file{sysdeps} directory tree. For example,
423@w{@file{sysdeps/sparc}} and @w{@file{sysdeps/m68k}}. These contain
424files specific to those machine architectures, but not specific to any
425particular operating system. There might be subdirectories for
426specializations of those architectures, such as
427@w{@file{sysdeps/m68k/68020}}. Code which is specific to the
428floating-point coprocessor used with a particular machine should go in
429@w{@file{sysdeps/@var{machine}/fpu}}.
430
431There are a few directories at the top level of the @file{sysdeps}
432hierarchy that are not for particular machine architectures.
433
434@table @file
435@item generic
436As described above (@pxref{Porting}), this is the subdirectory
437that every configuration implicitly uses after all others.
438
439@item ieee754
440This directory is for code using the IEEE 754 floating-point format,
441where the C type @code{float} is IEEE 754 single-precision format, and
442@code{double} is IEEE 754 double-precision format. Usually this
443directory is referred to in the @file{Implies} file in a machine
444architecture-specific directory, such as @file{m68k/Implies}.
445
446@item libm-ieee754
447This directory contains an implementation of a mathematical library
448usable on platforms which use @w{IEEE 754} conformant floating-point
449arithmetic.
450
451@item libm-i387
452This is a special case. Ideally the code should be in
453@file{sysdeps/i386/fpu} but for various reasons it is kept aside.
454
455@item posix
456This directory contains implementations of things in the library in
457terms of @sc{POSIX.1} functions. This includes some of the @sc{POSIX.1}
458functions themselves. Of course, @sc{POSIX.1} cannot be completely
459implemented in terms of itself, so a configuration using just
460@file{posix} cannot be complete.
461
462@item unix
463This is the directory for Unix-like things. @xref{Porting to Unix}.
464@file{unix} implies @file{posix}. There are some special-purpose
465subdirectories of @file{unix}:
466
467@table @file
468@item unix/common
469This directory is for things common to both BSD and System V release 4.
470Both @file{unix/bsd} and @file{unix/sysv/sysv4} imply @file{unix/common}.
471
472@item unix/inet
473This directory is for @code{socket} and related functions on Unix systems.
474@file{unix/inet/Subdirs} enables the @file{inet} top-level subdirectory.
475@file{unix/common} implies @file{unix/inet}.
476@end table
477
478@item mach
479This is the directory for things based on the Mach microkernel from CMU
480(including @gnuhurdsystems{}). Other basic operating systems
481(VMS, for example) would have their own directories at the top level of
482the @file{sysdeps} hierarchy, parallel to @file{unix} and @file{mach}.
483@end table
484
485@node Porting to Unix
486@appendixsubsec Porting @theglibc{} to Unix Systems
487
488Most Unix systems are fundamentally very similar. There are variations
489between different machines, and variations in what facilities are
490provided by the kernel. But the interface to the operating system
491facilities is, for the most part, pretty uniform and simple.
492
493The code for Unix systems is in the directory @file{unix}, at the top
494level of the @file{sysdeps} hierarchy. This directory contains
495subdirectories (and subdirectory trees) for various Unix variants.
496
497The functions which are system calls in most Unix systems are
498implemented in assembly code, which is generated automatically from
499specifications in files named @file{syscalls.list}. There are several
500such files, one in @file{sysdeps/unix} and others in its subdirectories.
501Some special system calls are implemented in files that are named with a
502suffix of @samp{.S}; for example, @file{_exit.S}. Files ending in
503@samp{.S} are run through the C preprocessor before being fed to the
504assembler.
505
506These files all use a set of macros that should be defined in
507@file{sysdep.h}. The @file{sysdep.h} file in @file{sysdeps/unix}
508partially defines them; a @file{sysdep.h} file in another directory must
509finish defining them for the particular machine and operating system
510variant. See @file{sysdeps/unix/sysdep.h} and the machine-specific
511@file{sysdep.h} implementations to see what these macros are and what
512they should do.@refill
513
514The system-specific makefile for the @file{unix} directory
515(@file{sysdeps/unix/Makefile}) gives rules to generate several files
516from the Unix system you are building the library on (which is assumed
517to be the target system you are building the library @emph{for}). All
518the generated files are put in the directory where the object files are
519kept; they should not affect the source tree itself. The files
520generated are @file{ioctls.h}, @file{errnos.h}, @file{sys/param.h}, and
521@file{errlist.c} (for the @file{stdio} section of the library).
522
523@ignore
524@c This section might be a good idea if it is finished,
525@c but there's no point including it as it stands. --rms
526@c @appendixsec Compatibility with Traditional C
527
528@c ??? This section is really short now. Want to keep it? --roland
529
530@c It's not anymore true. glibc 2.1 cannot be used with K&R compilers.
531@c --drepper
532
533Although @theglibc{} implements the @w{ISO C} library facilities, you
534@emph{can} use @theglibc{} with traditional, ``pre-ISO'' C
535compilers. However, you need to be careful because the content and
536organization of the @glibcadj{} header files differs from that of
537traditional C implementations. This means you may need to make changes
538to your program in order to get it to compile.
539@end ignore