Skip to content
dklibc edited this page Sep 25, 2018 · 21 revisions

"Person who say it cannot be done should not interrupt person doing it" -- Chinese proverb. From here

dklibc -- small and simple realization of libc for x86 32-bit Linux kernel (2.6 and older). I hope may be someone will use it for teaching purposes. Coding libc byself will get you good knowledge of universe design)) Main part of the dklibc I wrote in Feb-March, 2016.

Sources of dietlibc and klibc are quite readable and I recommend you to investigate them. May be you will get some interesting tips from there.

Compilation

For compilation of the several asm modules you need FASM. For compilation of the C modules you need any C compiler (GCC isn't mandatory). The resulted object modules are added to an archive (static lib) -- dklibc.a This lib you need to statically link to your program.

Linking

Any C compiler automatically link system libc with your program. We need to use dklibc instead. GCC option -nostdlib does the job (it's equivalent to -nostartfiles -nodefaultlibs). Also will be better if a compiler will use dklibc headers instead of system libc headers. So, compilation line for GCC should be like this: gcc -nostdlib -o myprog myprog.c -I/include/dklibc -l:/usr/lib/dklibc.a

List of implemented functions

Standard C:

  • String functions: strcmp, strcpy, strlen, strchr, memcpy, memmove, memcmp, memset, atoi
  • Heap functions: malloc based on brk/sbrk syscall, also free, realloc, calloc
  • Streams (buffered I/O): struct FILE *, fopen, fclose, fseek, fread, fwrite, gets, getchar, fgetc, puts, fputs, fflush
  • Printf: vprintf, sprintf, snprintf, fprintf, ... NOTE: all of them are wrappers on vsnprintf
  • Time functions: strftime, time, localtime, gmtime, mktime
  • stdarg.h macros: va_list, va_start, va_arg
  • errno, strerror
  • sleep
  • exit
BSD Sockets:
  • htons, htonl, ntohs, ntohl
  • socket, bind, accept, listen, connect, recv, send, recvfrom, sendto, shutdown, getpeername, getsockname, ... //just a wrappers for a single entry point in kernel
POSIX:
  • Syscalls: open, close, read, write, lseek, fork, execve, execv, execl, brk, sbrk, kill, chdir, rename, time, dup, dup2, pipe, truncate, sync, wait, waitpid, getpid, ioctl, unlink, link, utime, mkdir, chown, chmod, getcwd, getuid, getgid, alarm, signal, uname, sigprocmask, setuid, setgid, pause, mknod, mount, umount, reboot, umask, ...
  • getenv, getopt
  • Functions for sending messages to syslogd: openlog, syslog
  • Functions for reading dirs: DIR, opendir, closedir, readdir
  • getpass -- function to get password from terminal without echo
  • Functions to work with the files: utmp, /etc/password, /etc/shadow, /etc/group
Clone this wiki locally