maint.texi 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. @node Maintenance, Platform, Installation, Top
  2. @c %MENU% How to enhance and port the GNU C Library
  3. @appendix Library Maintenance
  4. @menu
  5. * Source Layout:: How to add new functions or header files
  6. to the GNU C Library.
  7. * Source Fortification:: Fortification of function calls.
  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.
  11. @end menu
  12. @node Source Layout
  13. @appendixsec Adding New Functions
  14. The process of building the library is driven by the makefiles, which
  15. make heavy use of special features of GNU @code{make}. The makefiles
  16. are very complex, and you probably don't want to try to understand them.
  17. But what they do is fairly straightforward, and only requires that you
  18. define a few variables in the right places.
  19. The library sources are divided into subdirectories, grouped by topic.
  20. The @file{string} subdirectory has all the string-manipulation
  21. functions, @file{math} has all the mathematical functions, etc.
  22. Each subdirectory contains a simple makefile, called @file{Makefile},
  23. which defines a few @code{make} variables and then includes the global
  24. makefile @file{Rules} with a line like:
  25. @smallexample
  26. include ../Rules
  27. @end smallexample
  28. @noindent
  29. The basic variables that a subdirectory makefile defines are:
  30. @table @code
  31. @item subdir
  32. The name of the subdirectory, for example @file{stdio}.
  33. This variable @strong{must} be defined.
  34. @item headers
  35. The names of the header files in this section of the library,
  36. such as @file{stdio.h}.
  37. @item routines
  38. @itemx aux
  39. The names of the modules (source files) in this section of the library.
  40. These should be simple names, such as @samp{strlen} (rather than
  41. complete file names, such as @file{strlen.c}). Use @code{routines} for
  42. modules that define functions in the library, and @code{aux} for
  43. auxiliary modules containing things like data definitions. But the
  44. values of @code{routines} and @code{aux} are just concatenated, so there
  45. really is no practical difference.
  46. @item tests
  47. The names of test programs for this section of the library. These
  48. should be simple names, such as @samp{tester} (rather than complete file
  49. names, such as @file{tester.c}). @w{@samp{make tests}} will build and
  50. run all the test programs. If a test program needs input, put the test
  51. data in a file called @file{@var{test-program}.input}; it will be given to
  52. the test program on its standard input. If a test program wants to be
  53. run with arguments, put the arguments (all on a single line) in a file
  54. called @file{@var{test-program}.args}. Test programs should exit with
  55. zero status when the test passes, and nonzero status when the test
  56. indicates a bug in the library or error in building.
  57. @item others
  58. The names of ``other'' programs associated with this section of the
  59. library. These are programs which are not tests per se, but are other
  60. small programs included with the library. They are built by
  61. @w{@samp{make others}}.
  62. @item install-lib
  63. @itemx install-data
  64. @itemx install
  65. Files to be installed by @w{@samp{make install}}. Files listed in
  66. @samp{install-lib} are installed in the directory specified by
  67. @samp{libdir} in @file{configparms} or @file{Makeconfig}
  68. (@pxref{Installation}). Files listed in @code{install-data} are
  69. installed in the directory specified by @samp{datadir} in
  70. @file{configparms} or @file{Makeconfig}. Files listed in @code{install}
  71. are installed in the directory specified by @samp{bindir} in
  72. @file{configparms} or @file{Makeconfig}.
  73. @item distribute
  74. Other files from this subdirectory which should be put into a
  75. distribution tar file. You need not list here the makefile itself or
  76. the source and header files listed in the other standard variables.
  77. Only define @code{distribute} if there are files used in an unusual way
  78. that should go into the distribution.
  79. @item generated
  80. Files which are generated by @file{Makefile} in this subdirectory.
  81. These files will be removed by @w{@samp{make clean}}, and they will
  82. never go into a distribution.
  83. @item extra-objs
  84. Extra object files which are built by @file{Makefile} in this
  85. subdirectory. This should be a list of file names like @file{foo.o};
  86. the files will actually be found in whatever directory object files are
  87. being built in. These files will be removed by @w{@samp{make clean}}.
  88. This variable is used for secondary object files needed to build
  89. @code{others} or @code{tests}.
  90. @end table
  91. @menu
  92. * Platform: Adding Platform-specific. Adding platform-specific
  93. features.
  94. @end menu
  95. @node Adding Platform-specific
  96. @appendixsubsec Platform-specific types, macros and functions
  97. It's sometimes necessary to provide nonstandard, platform-specific
  98. features to developers. The C library is traditionally the
  99. lowest library layer, so it makes sense for it to provide these
  100. low-level features. However, including these features in the C
  101. library may be a disadvantage if another package provides them
  102. as well as there will be two conflicting versions of them. Also,
  103. the features won't be available to projects that do not use
  104. @theglibc{} but use other GNU tools, like GCC.
  105. The current guidelines are:
  106. @itemize @bullet
  107. @item
  108. If the header file provides features that only make sense on a particular
  109. machine architecture and have nothing to do with an operating system, then
  110. the features should ultimately be provided as GCC built-in functions. Until
  111. then, @theglibc{} may provide them in the header file. When the GCC built-in
  112. functions become available, those provided in the header file should be made
  113. conditionally available prior to the GCC version in which the built-in
  114. function was made available.
  115. @item
  116. If the header file provides features that are specific to an operating system,
  117. both GCC and @theglibc{} could provide it, but @theglibc{} is preferred
  118. as it already has a lot of information about the operating system.
  119. @item
  120. If the header file provides features that are specific to an operating system
  121. but used by @theglibc{}, then @theglibc{} should provide them.
  122. @end itemize
  123. The general solution for providing low-level features is to export them as
  124. follows:
  125. @itemize @bullet
  126. @item
  127. A nonstandard, low-level header file that defines macros and inline
  128. functions should be called @file{sys/platform/@var{name}.h}.
  129. @item
  130. Each header file's name should include the platform name, to avoid
  131. users thinking there is anything in common between the different
  132. header files for different platforms. For example, a
  133. @file{sys/platform/@var{arch}.h} name such as
  134. @file{sys/platform/ppc.h} is better than @file{sys/platform.h}.
  135. @item
  136. A platform-specific header file provided by @theglibc{} should coordinate
  137. with GCC such that compiler built-in versions of the functions and macros are
  138. preferred if available. This means that user programs will only ever need to
  139. include @file{sys/platform/@var{arch}.h}, keeping the same names of types,
  140. macros, and functions for convenience and portability.
  141. @item
  142. Each included symbol must have the prefix @code{__@var{arch}_}, such as
  143. @code{__ppc_get_timebase}.
  144. @end itemize
  145. The easiest way to provide a header file is to add it to the
  146. @code{sysdep_headers} variable. For example, the combination of
  147. Linux-specific header files on PowerPC could be provided like this:
  148. @smallexample
  149. sysdep_headers += sys/platform/ppc.h
  150. @end smallexample
  151. Then ensure that you have added a @file{sys/platform/ppc.h}
  152. header file in the machine-specific directory, e.g.,
  153. @file{sysdeps/powerpc/sys/platform/ppc.h}.
  154. @node Source Fortification
  155. @appendixsec Fortification of function calls
  156. This section contains implementation details of @theglibc{} and may not
  157. remain stable across releases.
  158. The @code{_FORTIFY_SOURCE} macro may be defined by users to control
  159. hardening of calls into some functions in @theglibc{}. The definition
  160. should be at the top of the source file before any headers are included
  161. or at the pre-processor commandline using the @code{-D} switch. The
  162. hardening primarily focuses on accesses to buffers passed to the
  163. functions but may also include checks for validity of other inputs to
  164. the functions.
  165. When the @code{_FORTIFY_SOURCE} macro is defined, it enables code that
  166. validates inputs passed to some functions in @theglibc to determine if
  167. they are safe. If the compiler is unable to determine that the inputs
  168. to the function call are safe, the call may be replaced by a call to its
  169. hardened variant that does additional safety checks at runtime. Some
  170. hardened variants need the size of the buffer to perform access
  171. validation and this is provided by the @code{__builtin_object_size} or
  172. the @code{__builtin_dynamic_object_size} builtin functions.
  173. @code{_FORTIFY_SOURCE} also enables additional compile time diagnostics,
  174. such as unchecked return values from some functions, to encourage
  175. developers to add error checking for those functions.
  176. At runtime, if any of those safety checks fail, the program will
  177. terminate with a @code{SIGABRT} signal. @code{_FORTIFY_SOURCE} may be
  178. defined to one of the following values:
  179. @itemize @bullet
  180. @item @math{1}: This enables buffer bounds checking using the value
  181. returned by the @code{__builtin_object_size} compiler builtin function.
  182. If the function returns @code{(size_t) -1}, the function call is left
  183. untouched. Additionally, this level also enables validation of flags to
  184. the @code{open}, @code{open64}, @code{openat} and @code{openat64}
  185. functions.
  186. @item @math{2}: This behaves like @math{1}, with the addition of some
  187. checks that may trap code that is conforming but unsafe, e.g. accepting
  188. @code{%n} only in read-only format strings.
  189. @item @math{3}: This enables buffer bounds checking using the value
  190. returned by the @code{__builtin_dynamic_object_size} compiler builtin
  191. function. If the function returns @code{(size_t) -1}, the function call
  192. is left untouched. Fortification at this level may have a impact on
  193. program performance if the function call that is fortified is frequently
  194. encountered and the size expression returned by
  195. @code{__builtin_dynamic_object_size} is complex.
  196. @end itemize
  197. In general, the fortified variants of the function calls use the name of
  198. the function with a @code{__} prefix and a @code{_chk} suffix. There
  199. are some exceptions, e.g. the @code{printf} family of functions where,
  200. depending on the architecture, one may also see fortified variants have
  201. the @code{_chkieee128} suffix or the @code{__nldbl___} prefix to their
  202. names.
  203. Another exception is the @code{open} family of functions, where their
  204. fortified replacements have the @code{__} prefix and a @code{_2} suffix.
  205. The @code{FD_SET}, @code{FD_CLR} and @code{FD_ISSET} macros use the
  206. @code{__fdelt_chk} function on fortification.
  207. The following functions and macros are fortified in @theglibc{}:
  208. @c Generated using the following command:
  209. @c find . -name Versions | xargs grep -e "_chk;" -e "_2;" |
  210. @c cut -d ':' -f 2 | sed 's/;/\n/g' | sed 's/ *//g' | grep -v "^$" |
  211. @c sort -u | grep ^__ |
  212. @c grep -v -e ieee128 -e __nldbl -e align_cpy -e "fdelt_warn" |
  213. @c sed 's/__fdelt_chk/@item @code{FD_SET}\n\n@item @code{FD_CLR}\n\n@item @code{FD_ISSET}\n/' |
  214. @c sed 's/__\(.*\)_\(chk\|2\)/@item @code{\1}\n/'
  215. @itemize @bullet
  216. @item @code{asprintf}
  217. @item @code{confstr}
  218. @item @code{dprintf}
  219. @item @code{explicit_bzero}
  220. @item @code{FD_SET}
  221. @item @code{FD_CLR}
  222. @item @code{FD_ISSET}
  223. @item @code{fgets}
  224. @item @code{fgets_unlocked}
  225. @item @code{fgetws}
  226. @item @code{fgetws_unlocked}
  227. @item @code{fprintf}
  228. @item @code{fread}
  229. @item @code{fread_unlocked}
  230. @item @code{fwprintf}
  231. @item @code{getcwd}
  232. @item @code{getdomainname}
  233. @item @code{getgroups}
  234. @item @code{gethostname}
  235. @item @code{getlogin_r}
  236. @item @code{gets}
  237. @item @code{getwd}
  238. @item @code{inet_ntop}
  239. @item @code{inet_pton}
  240. @item @code{longjmp}
  241. @item @code{mbsnrtowcs}
  242. @item @code{mbsrtowcs}
  243. @item @code{mbstowcs}
  244. @item @code{memcpy}
  245. @item @code{memmove}
  246. @item @code{mempcpy}
  247. @item @code{memset}
  248. @item @code{memset_explicit}
  249. @item @code{mq_open}
  250. @item @code{obstack_printf}
  251. @item @code{obstack_vprintf}
  252. @item @code{open}
  253. @item @code{open64}
  254. @item @code{openat}
  255. @item @code{openat64}
  256. @item @code{poll}
  257. @item @code{ppoll64}
  258. @item @code{ppoll}
  259. @item @code{pread64}
  260. @item @code{pread}
  261. @item @code{printf}
  262. @item @code{ptsname_r}
  263. @item @code{read}
  264. @item @code{readlinkat}
  265. @item @code{readlink}
  266. @item @code{realpath}
  267. @item @code{recv}
  268. @item @code{recvfrom}
  269. @item @code{snprintf}
  270. @item @code{sprintf}
  271. @item @code{stpcpy}
  272. @item @code{stpncpy}
  273. @item @code{strcat}
  274. @item @code{strcpy}
  275. @item @code{strlcat}
  276. @item @code{strlcpy}
  277. @item @code{strncat}
  278. @item @code{strncpy}
  279. @item @code{swprintf}
  280. @item @code{syslog}
  281. @item @code{ttyname_r}
  282. @item @code{vasprintf}
  283. @item @code{vdprintf}
  284. @item @code{vfprintf}
  285. @item @code{vfwprintf}
  286. @item @code{vprintf}
  287. @item @code{vsnprintf}
  288. @item @code{vsprintf}
  289. @item @code{vswprintf}
  290. @item @code{vsyslog}
  291. @item @code{vwprintf}
  292. @item @code{wcpcpy}
  293. @item @code{wcpncpy}
  294. @item @code{wcrtomb}
  295. @item @code{wcscat}
  296. @item @code{wcscpy}
  297. @item @code{wcslcat}
  298. @item @code{wcslcpy}
  299. @item @code{wcsncat}
  300. @item @code{wcsncpy}
  301. @item @code{wcsnrtombs}
  302. @item @code{wcsrtombs}
  303. @item @code{wcstombs}
  304. @item @code{wctomb}
  305. @item @code{wmemcpy}
  306. @item @code{wmemmove}
  307. @item @code{wmempcpy}
  308. @item @code{wmemset}
  309. @item @code{wprintf}
  310. @end itemize
  311. @node Symbol handling
  312. @appendixsec Symbol handling in the GNU C Library
  313. @menu
  314. * 64-bit time symbol handling :: How to handle 64-bit time related
  315. symbols in the GNU C Library.
  316. @end menu
  317. @node 64-bit time symbol handling
  318. @appendixsubsec 64-bit time symbol handling in the GNU C Library
  319. With respect to time handling, @glibcadj{} configurations fall in two
  320. classes depending on the value of @code{__TIMESIZE}:
  321. @table @code
  322. @item @code{__TIMESIZE == 32}
  323. These @dfn{dual-time} configurations have both 32-bit and 64-bit time
  324. support. 32-bit time support provides type @code{time_t} and cannot
  325. handle dates beyond @dfn{Y2038}. 64-bit time support provides type
  326. @code{__time64_t} and can handle dates beyond @dfn{Y2038}.
  327. In these configurations, time-related types have two declarations,
  328. a 64-bit one, and a 32-bit one; and time-related functions generally
  329. have two definitions: a 64-bit one, and a 32-bit one which is a wrapper
  330. around the former. Therefore, for every @code{time_t}-related symbol,
  331. there is a corresponding @code{__time64_t}-related symbol, the name of
  332. which is usually the 32-bit symbol's name with @code{__} (a double
  333. underscore) prepended and @code{64} appended. For instance, the
  334. 64-bit-time counterpart of @code{clock_gettime} is
  335. @code{__clock_gettime64}.
  336. @item @code{__TIMESIZE == 64}
  337. These @dfn{single-time} configurations only have a 64-bit @code{time_t}
  338. and related functions, which can handle dates beyond 2038-01-19
  339. 03:14:07 (aka @dfn{Y2038}).
  340. In these configurations, time-related types only have a 64-bit
  341. declaration; and time-related functions only have one 64-bit definition.
  342. However, for every @code{time_t}-related symbol, there is a
  343. corresponding @code{__time64_t}-related macro, the name of which is
  344. derived as in the dual-time configuration case, and which expands to
  345. the symbol's name. For instance, the macro @code{__clock_gettime64}
  346. expands to @code{clock_gettime}.
  347. When @code{__TIMESIZE} is set to 64, @theglibc{} will also define
  348. the @code{__USE_TIME_BITS64} macro. It is used by the Linux kernel ABI
  349. to set the expected @code{time_t} size used on some syscalls.
  350. These macros are purely internal to @theglibc{} and exist only so that
  351. a single definition of the 64-bit time functions can be used on both
  352. single-time and dual-time configurations, and so that glibc code can
  353. freely call the 64-bit functions internally in all configurations.
  354. @end table
  355. @c The following paragraph should be removed once external interfaces
  356. @c get support for both time sizes.
  357. Note: at this point, 64-bit time support in dual-time configurations is
  358. work-in-progress, so for these configurations, the public API only makes
  359. the 32-bit time support available. In a later change, the public API
  360. will allow user code to choose the time size for a given compilation
  361. unit.
  362. 64-bit variants of time-related types or functions are defined for all
  363. configurations and use 64-bit-time symbol names (for dual-time
  364. configurations) or macros (for single-time configurations).
  365. 32-bit variants of time-related types or functions are defined only for
  366. dual-time configurations.
  367. Here is an example with @code{localtime}:
  368. Function @code{localtime} is declared in @file{time/time.h} as
  369. @smallexample
  370. extern struct tm *localtime (const time_t *__timer) __THROW;
  371. libc_hidden_proto (localtime)
  372. @end smallexample
  373. For single-time configurations, @code{__localtime64} is a macro which
  374. evaluates to @code{localtime}; for dual-time configurations,
  375. @code{__localtime64} is a function similar to @code{localtime} except
  376. it uses Y2038-proof types:
  377. @smallexample
  378. #if __TIMESIZE == 64
  379. # define __localtime64 localtime
  380. #else
  381. extern struct tm *__localtime64 (const __time64_t *__timer) __THROW;
  382. libc_hidden_proto (__localtime64)
  383. #endif
  384. @end smallexample
  385. (note: type @code{time_t} is replaced with @code{__time64_t} because
  386. @code{time_t} is not Y2038-proof, but @code{struct tm} is not
  387. replaced because it is already Y2038-proof.)
  388. The 64-bit-time implementation of @code{localtime} is written as follows
  389. and is compiled for both dual-time and single-time configuration classes.
  390. @smallexample
  391. struct tm *
  392. __localtime64 (const __time64_t *t)
  393. @{
  394. return __tz_convert (*t, 1, &_tmbuf);
  395. @}
  396. libc_hidden_def (__localtime64)
  397. @end smallexample
  398. The 32-bit-time implementation is a wrapper and is only compiled for
  399. dual-time configurations:
  400. @smallexample
  401. #if __TIMESIZE != 64
  402. struct tm *
  403. localtime (const time_t *t)
  404. @{
  405. __time64_t t64 = *t;
  406. return __localtime64 (&t64);
  407. @}
  408. libc_hidden_def (localtime)
  409. #endif
  410. @end smallexample
  411. @node Porting
  412. @appendixsec Porting @theglibc{}
  413. @Theglibc{} is written to be easily portable to a variety of
  414. machines and operating systems. Machine- and operating system-dependent
  415. functions are well separated to make it easy to add implementations for
  416. new machines or operating systems. This section describes the layout of
  417. the library source tree and explains the mechanisms used to select
  418. machine-dependent code to use.
  419. All the machine-dependent and operating system-dependent files in the
  420. library are in the subdirectory @file{sysdeps} under the top-level
  421. library source directory. This directory contains a hierarchy of
  422. subdirectories (@pxref{Hierarchy Conventions}).
  423. Each subdirectory of @file{sysdeps} contains source files for a
  424. particular machine or operating system, or for a class of machine or
  425. operating system (for example, systems by a particular vendor, or all
  426. machines that use IEEE 754 floating-point format). A configuration
  427. specifies an ordered list of these subdirectories. Each subdirectory
  428. implicitly appends its parent directory to the list. For example,
  429. specifying the list @file{unix/bsd/vax} is equivalent to specifying the
  430. list @file{unix/bsd/vax unix/bsd unix}. A subdirectory can also specify
  431. that it implies other subdirectories which are not directly above it in
  432. the directory hierarchy. If the file @file{Implies} exists in a
  433. subdirectory, it lists other subdirectories of @file{sysdeps} which are
  434. appended to the list, appearing after the subdirectory containing the
  435. @file{Implies} file. Lines in an @file{Implies} file that begin with a
  436. @samp{#} character are ignored as comments. For example,
  437. @file{unix/bsd/Implies} contains:
  438. @smallexample
  439. # BSD has Internet-related things.
  440. unix/inet
  441. @end smallexample
  442. @noindent
  443. and @file{unix/Implies} contains:
  444. @need 300
  445. @smallexample
  446. posix
  447. @end smallexample
  448. @noindent
  449. So the final list is @file{unix/bsd/vax unix/bsd unix/inet unix posix}.
  450. @file{sysdeps} has a ``special'' subdirectory called @file{generic}. It
  451. is always implicitly appended to the list of subdirectories, so you
  452. needn't put it in an @file{Implies} file, and you should not create any
  453. subdirectories under it intended to be new specific categories.
  454. @file{generic} serves two purposes. First, the makefiles do not bother
  455. to look for a system-dependent version of a file that's not in
  456. @file{generic}. This means that any system-dependent source file must
  457. have an analogue in @file{generic}, even if the routines defined by that
  458. file are not implemented on other platforms. Second, the @file{generic}
  459. version of a system-dependent file is used if the makefiles do not find
  460. a version specific to the system you're compiling for.
  461. If it is possible to implement the routines in a @file{generic} file in
  462. machine-independent C, using only other machine-independent functions in
  463. the C library, then you should do so. Otherwise, make them stubs. A
  464. @dfn{stub} function is a function which cannot be implemented on a
  465. particular machine or operating system. Stub functions always return an
  466. error, and set @code{errno} to @code{ENOSYS} (Function not implemented).
  467. @xref{Error Reporting}. If you define a stub function, you must place
  468. the statement @code{stub_warning(@var{function})}, where @var{function}
  469. is the name of your function, after its definition. This causes the
  470. function to be listed in the installed @code{<gnu/stubs.h>}, and
  471. makes GNU ld warn when the function is used.
  472. Some rare functions are only useful on specific systems and aren't
  473. defined at all on others; these do not appear anywhere in the
  474. system-independent source code or makefiles (including the
  475. @file{generic} directory), only in the system-dependent @file{Makefile}
  476. in the specific system's subdirectory.
  477. If you come across a file that is in one of the main source directories
  478. (@file{string}, @file{stdio}, etc.), and you want to write a machine- or
  479. operating system-dependent version of it, move the file into
  480. @file{sysdeps/generic} and write your new implementation in the
  481. appropriate system-specific subdirectory. Note that if a file is to be
  482. system-dependent, it @strong{must not} appear in one of the main source
  483. directories.
  484. There are a few special files that may exist in each subdirectory of
  485. @file{sysdeps}:
  486. @comment Blank lines after items make the table look better.
  487. @table @file
  488. @item Makefile
  489. A makefile for this machine or operating system, or class of machine or
  490. operating system. This file is included by the library makefile
  491. @file{Makerules}, which is used by the top-level makefile and the
  492. subdirectory makefiles. It can change the variables set in the
  493. including makefile or add new rules. It can use GNU @code{make}
  494. conditional directives based on the variable @samp{subdir} (see above) to
  495. select different sets of variables and rules for different sections of
  496. the library. It can also set the @code{make} variable
  497. @samp{sysdep-routines}, to specify extra modules to be included in the
  498. library. You should use @samp{sysdep-routines} rather than adding
  499. modules to @samp{routines} because the latter is used in determining
  500. what to distribute for each subdirectory of the main source tree.
  501. Each makefile in a subdirectory in the ordered list of subdirectories to
  502. be searched is included in order. Since several system-dependent
  503. makefiles may be included, each should append to @samp{sysdep-routines}
  504. rather than simply setting it:
  505. @smallexample
  506. sysdep-routines := $(sysdep-routines) foo bar
  507. @end smallexample
  508. @need 1000
  509. @item Subdirs
  510. This file contains the names of new whole subdirectories under the
  511. top-level library source tree that should be included for this system.
  512. These subdirectories are treated just like the system-independent
  513. subdirectories in the library source tree, such as @file{stdio} and
  514. @file{math}.
  515. Use this when there are completely new sets of functions and header
  516. files that should go into the library for the system this subdirectory
  517. of @file{sysdeps} implements. For example,
  518. @file{sysdeps/unix/inet/Subdirs} contains @file{inet}; the @file{inet}
  519. directory contains various network-oriented operations which only make
  520. sense to put in the library on systems that support the Internet.
  521. @item configure
  522. This file is a shell script fragment to be run at configuration time.
  523. The top-level @file{configure} script uses the shell @code{.} command to
  524. read the @file{configure} file in each system-dependent directory
  525. chosen, in order. The @file{configure} files are often generated from
  526. @file{configure.ac} files using Autoconf.
  527. A system-dependent @file{configure} script will usually add things to
  528. the shell variables @samp{DEFS} and @samp{config_vars}; see the
  529. top-level @file{configure} script for details. The script can check for
  530. @w{@samp{--with-@var{package}}} options that were passed to the
  531. top-level @file{configure}. For an option
  532. @w{@samp{--with-@var{package}=@var{value}}} @file{configure} sets the
  533. shell variable @w{@samp{with_@var{package}}} (with any dashes in
  534. @var{package} converted to underscores) to @var{value}; if the option is
  535. just @w{@samp{--with-@var{package}}} (no argument), then it sets
  536. @w{@samp{with_@var{package}}} to @samp{yes}.
  537. @item configure.ac
  538. This file is an Autoconf input fragment to be processed into the file
  539. @file{configure} in this subdirectory. @xref{Introduction,,, autoconf,
  540. Autoconf: Generating Automatic Configuration Scripts},
  541. for a description of Autoconf. You should write either @file{configure}
  542. or @file{configure.ac}, but not both. The first line of
  543. @file{configure.ac} should invoke the @code{m4} macro
  544. @samp{GLIBC_PROVIDES}. This macro does several @code{AC_PROVIDE} calls
  545. for Autoconf macros which are used by the top-level @file{configure}
  546. script; without this, those macros might be invoked again unnecessarily
  547. by Autoconf.
  548. @end table
  549. That is the general system for how system-dependencies are isolated.
  550. @iftex
  551. The next section explains how to decide what directories in
  552. @file{sysdeps} to use. @ref{Porting to Unix}, has some tips on porting
  553. the library to Unix variants.
  554. @end iftex
  555. @menu
  556. * Hierarchy Conventions:: The layout of the @file{sysdeps} hierarchy.
  557. * Porting to Unix:: Porting the library to an average
  558. Unix-like system.
  559. @end menu
  560. @node Hierarchy Conventions
  561. @appendixsubsec Layout of the @file{sysdeps} Directory Hierarchy
  562. A GNU configuration name has three parts: the CPU type, the
  563. manufacturer's name, and the operating system. @file{configure} uses
  564. these to pick the list of system-dependent directories to look for. If
  565. the @samp{--nfp} option is @emph{not} passed to @file{configure}, the
  566. directory @file{@var{machine}/fpu} is also used. The operating system
  567. often has a @dfn{base operating system}; for example, if the operating
  568. system is @samp{Linux}, the base operating system is @samp{unix/sysv}.
  569. The algorithm used to pick the list of directories is simple:
  570. @file{configure} makes a list of the base operating system,
  571. manufacturer, CPU type, and operating system, in that order. It then
  572. concatenates all these together with slashes in between, to produce a
  573. directory name; for example, the configuration @w{@samp{i686-linux-gnu}}
  574. results in @file{unix/sysv/linux/i386/i686}. @file{configure} then
  575. tries removing each element of the list in turn, so
  576. @file{unix/sysv/linux} and @file{unix/sysv} are also tried, among others.
  577. Since the precise version number of the operating system is often not
  578. important, and it would be very inconvenient, for example, to have
  579. identical @file{irix6.2} and @file{irix6.3} directories,
  580. @file{configure} tries successively less specific operating system names
  581. by removing trailing suffixes starting with a period.
  582. As an example, here is the complete list of directories that would be
  583. tried for the configuration @w{@samp{i686-linux-gnu}}:
  584. @smallexample
  585. sysdeps/i386/elf
  586. sysdeps/unix/sysv/linux/i386
  587. sysdeps/unix/sysv/linux
  588. sysdeps/gnu
  589. sysdeps/unix/common
  590. sysdeps/unix/mman
  591. sysdeps/unix/inet
  592. sysdeps/unix/sysv/i386/i686
  593. sysdeps/unix/sysv/i386
  594. sysdeps/unix/sysv
  595. sysdeps/unix/i386
  596. sysdeps/unix
  597. sysdeps/posix
  598. sysdeps/i386/i686
  599. sysdeps/i386/i486
  600. sysdeps/libm-i387/i686
  601. sysdeps/i386/fpu
  602. sysdeps/libm-i387
  603. sysdeps/i386
  604. sysdeps/wordsize-32
  605. sysdeps/ieee754
  606. sysdeps/libm-ieee754
  607. sysdeps/generic
  608. @end smallexample
  609. Different machine architectures are conventionally subdirectories at the
  610. top level of the @file{sysdeps} directory tree. For example,
  611. @w{@file{sysdeps/sparc}} and @w{@file{sysdeps/m68k}}. These contain
  612. files specific to those machine architectures, but not specific to any
  613. particular operating system. There might be subdirectories for
  614. specializations of those architectures, such as
  615. @w{@file{sysdeps/m68k/68020}}. Code which is specific to the
  616. floating-point coprocessor used with a particular machine should go in
  617. @w{@file{sysdeps/@var{machine}/fpu}}.
  618. There are a few directories at the top level of the @file{sysdeps}
  619. hierarchy that are not for particular machine architectures.
  620. @table @file
  621. @item generic
  622. As described above (@pxref{Porting}), this is the subdirectory
  623. that every configuration implicitly uses after all others.
  624. @item ieee754
  625. This directory is for code using the IEEE 754 floating-point format,
  626. where the C type @code{float} is IEEE 754 single-precision format, and
  627. @code{double} is IEEE 754 double-precision format. Usually this
  628. directory is referred to in the @file{Implies} file in a machine
  629. architecture-specific directory, such as @file{m68k/Implies}.
  630. @item libm-ieee754
  631. This directory contains an implementation of a mathematical library
  632. usable on platforms which use @w{IEEE 754} conformant floating-point
  633. arithmetic.
  634. @item libm-i387
  635. This is a special case. Ideally the code should be in
  636. @file{sysdeps/i386/fpu} but for various reasons it is kept aside.
  637. @item posix
  638. This directory contains implementations of things in the library in
  639. terms of @sc{POSIX.1} functions. This includes some of the @sc{POSIX.1}
  640. functions themselves. Of course, @sc{POSIX.1} cannot be completely
  641. implemented in terms of itself, so a configuration using just
  642. @file{posix} cannot be complete.
  643. @item unix
  644. This is the directory for Unix-like things. @xref{Porting to Unix}.
  645. @file{unix} implies @file{posix}. There are some special-purpose
  646. subdirectories of @file{unix}:
  647. @table @file
  648. @item unix/common
  649. This directory is for things common to both BSD and System V release 4.
  650. Both @file{unix/bsd} and @file{unix/sysv/sysv4} imply @file{unix/common}.
  651. @item unix/inet
  652. This directory is for @code{socket} and related functions on Unix systems.
  653. @file{unix/inet/Subdirs} enables the @file{inet} top-level subdirectory.
  654. @file{unix/common} implies @file{unix/inet}.
  655. @end table
  656. @item mach
  657. This is the directory for things based on the Mach microkernel from CMU
  658. (including @gnuhurdsystems{}). Other basic operating systems
  659. (VMS, for example) would have their own directories at the top level of
  660. the @file{sysdeps} hierarchy, parallel to @file{unix} and @file{mach}.
  661. @end table
  662. @node Porting to Unix
  663. @appendixsubsec Porting @theglibc{} to Unix Systems
  664. Most Unix systems are fundamentally very similar. There are variations
  665. between different machines, and variations in what facilities are
  666. provided by the kernel. But the interface to the operating system
  667. facilities is, for the most part, pretty uniform and simple.
  668. The code for Unix systems is in the directory @file{unix}, at the top
  669. level of the @file{sysdeps} hierarchy. This directory contains
  670. subdirectories (and subdirectory trees) for various Unix variants.
  671. The functions which are system calls in most Unix systems are
  672. implemented in assembly code, which is generated automatically from
  673. specifications in files named @file{syscalls.list}. There are several
  674. such files, one in @file{sysdeps/unix} and others in its subdirectories.
  675. Some special system calls are implemented in files that are named with a
  676. suffix of @samp{.S}; for example, @file{_exit.S}. Files ending in
  677. @samp{.S} are run through the C preprocessor before being fed to the
  678. assembler.
  679. These files all use a set of macros that should be defined in
  680. @file{sysdep.h}. The @file{sysdep.h} file in @file{sysdeps/unix}
  681. partially defines them; a @file{sysdep.h} file in another directory must
  682. finish defining them for the particular machine and operating system
  683. variant. See @file{sysdeps/unix/sysdep.h} and the machine-specific
  684. @file{sysdep.h} implementations to see what these macros are and what
  685. they should do.
  686. The system-specific makefile for the @file{unix} directory
  687. (@file{sysdeps/unix/Makefile}) gives rules to generate several files
  688. from the Unix system you are building the library on (which is assumed
  689. to be the target system you are building the library @emph{for}). All
  690. the generated files are put in the directory where the object files are
  691. kept; they should not affect the source tree itself. The files
  692. generated are @file{ioctls.h}, @file{errnos.h}, @file{sys/param.h}, and
  693. @file{errlist.c} (for the @file{stdio} section of the library).
  694. @ignore
  695. @c This section might be a good idea if it is finished,
  696. @c but there's no point including it as it stands. --rms
  697. @c @appendixsec Compatibility with Traditional C
  698. @c ??? This section is really short now. Want to keep it? --roland
  699. @c It's not anymore true. glibc 2.1 cannot be used with K&R compilers.
  700. @c --drepper
  701. Although @theglibc{} implements the @w{ISO C} library facilities, you
  702. @emph{can} use @theglibc{} with traditional, ``pre-ISO'' C
  703. compilers. However, you need to be careful because the content and
  704. organization of the @glibcadj{} header files differs from that of
  705. traditional C implementations. This means you may need to make changes
  706. to your program in order to get it to compile.
  707. @end ignore