5 @cindex linking the C library
6 The C subroutine library depends on a handful of subroutine calls for
7 operating system services. If you use the C library on a system that
8 complies with the POSIX
.1 standard (also known as IEEE
1003.1), most of
9 these subroutines are supplied with your operating system.
11 If some of these subroutines are not provided with your system---in
12 the extreme case, if you are developing software for a ``bare board''
13 system, without an OS---you will at least need to provide do-nothing
14 stubs (or subroutines with minimal functionality) to allow your
15 programs to link with the subroutines in @code
{libc.a
}.
18 * Stubs:: Definitions for OS interface
19 * Reentrant Syscalls:: Reentrant covers for OS subroutines
23 @section Definitions for OS interface
26 @cindex subroutines for OS interface
27 @cindex OS interface subroutines
28 This is the complete set of system definitions (primarily subroutines)
29 required; the examples shown implement the minimal functionality
30 required to allow @code
{libc
} to link, and fail gracefully where OS
31 services are not available.
33 Graceful failure is permitted by returning an error code. A minor
34 complication arises here: the C library must be compatible with
35 development environments that supply fully functional versions of these
36 subroutines. Such environments usually return error codes in a global
37 @code
{errno
}. However, the Red Hat newlib C library provides a @emph
{macro
}
38 definition for @code
{errno
} in the header file @file
{errno.h
}, as part
39 of its support for reentrant routines (@pxref
{Reentrancy,,Reentrancy
}).
41 @cindex @code
{errno
} global vs macro
42 The bridge between these two interpretations of @code
{errno
} is
43 straightforward: the C library routines with OS interface calls
44 capture the @code
{errno
} values returned globally, and record them in
45 the appropriate field of the reentrancy structure (so that you can query
46 them using the @code
{errno
} macro from @file
{errno.h
}).
48 This mechanism becomes visible when you write stub routines for OS
49 interfaces. You must include @file
{errno.h
}, then disable the macro,
59 The examples in this chapter include this treatment of @code
{errno
}.
63 Exit a program without cleaning up files. If your system doesn't
64 provide this, it is best to avoid linking with subroutines that require
65 it (@code
{exit
}, @code
{system
}).
68 Close a file. Minimal implementation:
71 int close(int file) @
{
77 A pointer to a list of environment variables and their values. For a
78 minimal environment, this empty list is adequate:
81 char *__env
[1] = @
{ 0 @
};
82 char **environ = __env;
86 Transfer control to a new process. Minimal implementation (for a system
93 int execve(char *name, char **argv, char **env) @
{
100 Create a new process. Minimal implementation (for a system without processes):
113 Status of an open file. For consistency with other minimal
114 implementations in these examples, all files are regarded as character
115 special devices. The @file
{sys/stat.h
} header file required is
116 distributed in the @file
{include
} subdirectory for this C library.
119 #include <sys/stat.h>
120 int fstat(int file, struct stat *st) @
{
121 st->st_mode = S_IFCHR;
127 Process-ID; this is sometimes used to generate strings unlikely to
128 conflict with other processes. Minimal implementation, for a system
138 Query whether output stream is a terminal. For consistency with the
139 other minimal implementations, which only support output to
140 @code
{stdout
}, this minimal implementation is suggested:
143 int isatty(int file) @
{
149 Send a signal. Minimal implementation:
155 int kill(int pid, int sig) @
{
162 Establish a new name for an existing file. Minimal implementation:
168 int link(char *old, char *new) @
{
175 Set position in a file. Minimal implementation:
178 int lseek(int file, int ptr, int dir) @
{
184 Open a file. Minimal implementation:
187 int open(const char *name, int flags, int mode) @
{
193 Read from a file. Minimal implementation:
196 int read(int file, char *ptr, int len) @
{
202 Increase program data space. As @code
{malloc
} and related functions
203 depend on this, it is useful to have a working implementation. The
204 following suffices for a standalone system; it exploits the symbol
205 @code
{_end
} automatically defined by the GNU linker.
209 caddr_t sbrk(int incr) @
{
210 extern char _end; /* @r
{Defined by the linker
} */
211 static char *heap_end;
214 if (heap_end ==
0) @
{
217 prev_heap_end = heap_end;
218 if (heap_end + incr > stack_ptr) @
{
219 write (
1, "Heap and stack collision
\n",
25);
224 return (caddr_t) prev_heap_end;
230 Status of a file (by name). Minimal implementation:
233 int stat(char *file, struct stat *st) @
{
234 st->st_mode = S_IFCHR;
240 Timing information for current process. Minimal implementation:
243 int times(struct tms *buf) @
{
249 Remove a file's directory entry. Minimal implementation:
255 int unlink(char *name) @
{
262 Wait for a child process. Minimal implementation:
267 int wait(int *status) @
{
274 Write to a file. @file
{libc
} subroutines will use this
275 system routine for output to all files, @emph
{including
}
276 @code
{stdout
}---so if you need to generate any output, for example to a
277 serial port for debugging, you should make your minimal @code
{write
}
278 capable of doing this. The following minimal implementation is an
279 incomplete example; it relies on a @code
{outbyte
} subroutine (not
280 shown; typically, you must write this in assembler from examples
281 provided by your hardware manufacturer) to actually perform the output.
285 int write(int file, char *ptr, int len) @
{
288 for (todo =
0; todo < len; todo++) @
{
299 @node Reentrant Syscalls
300 @section Reentrant covers for OS subroutines
302 Since the system subroutines are used by other library routines that
303 require reentrancy, @file{libc.a} provides cover routines (for example,
304 the reentrant version of @code{fork} is @code{_fork_r}). These cover
305 routines are consistent with the other reentrant subroutines in this
306 library, and achieve reentrancy by using a reserved global data block
307 (@pxref{Reentrancy,,Reentrancy}).
310 * Function _close_r:: Reentrant version of close
311 * Function _execve_r:: Reentrant version of execve
312 * Function _fork_r:: Reentrant version of fork
314 * Function _fstat64_r:: Reentrant version of fstat64
316 * Function _fstat_r:: Reentrant version of fstat
317 * Function _getpid_r:: Reentrant version of getpid
318 * Function _kill_r:: Reentrant version of kill
319 * Function _link_r:: Reentrant version of link
321 * Function _lseek64_r:: Reentrant version of lseek64
323 * Function _lseek_r:: Reentrant version of lseek
325 * Function _open64_r:: Reentrant version of open64
327 * Function _open_r:: Reentrant version of open
328 * Function _read_r:: Reentrant version of read
329 * Function _sbrk_r:: Reentrant version of sbrk
331 * Function _stat64_r:: Reentrant version of stat64
333 * Function _stat_r:: Reentrant version of stat
334 * Function _times_r:: Reentrant version of times
335 * Function _unlink_r:: Reentrant version of unlink
336 * Function _wait_r:: Reentrant version of wait
337 * Function _write_r:: Reentrant version of write
342 @include reent/closer.def
345 @include reent/execr.def
349 @include reent/fstat64r.def
353 @include reent/fstatr.def
356 @include reent/linkr.def
360 @include reent/lseek64r.def
364 @include reent/lseekr.def
368 @include reent/open64r.def
372 @include reent/openr.def
375 @include reent/readr.def
378 @include reent/sbrkr.def
381 @include reent/signalr.def
385 @include reent/stat64r.def
389 @include reent/statr.def
392 @include reent/timesr.def
395 @include reent/unlinkr.def
398 @include reent/writer.def