Minimal C Library
Overview
The Minimal C Library provides essential C runtime functions for FreeWorld OS. It implements only the functions needed to run basic programs, making it much simpler and faster to build than full glibc.
Components
1. System Call Wrappers
Location: libc-minimal/src/syscalls.c
Wrappers for kernel system calls:
_exit()- Terminate processwrite()- Write to file descriptorread()- Read from file descriptoropen()- Open fileclose()- Close file descriptorexecve()- Execute program
2. Memory Allocation
Location: libc-minimal/src/malloc.c
Simple heap allocator:
malloc()- Allocate memoryfree()- Free memory (stub - no-op for now)calloc()- Allocate and zero memoryrealloc()- Reallocate memory
Note: Uses a simple 1MB heap. For production, this should be replaced with a proper allocator.
3. String Functions
Location: libc-minimal/src/string.c
Standard string manipulation:
strlen()- String lengthstrcpy()- Copy stringstrncpy()- Copy string with length limitstrcmp()- Compare stringsstrncmp()- Compare strings with length limitmemset()- Set memorymemcpy()- Copy memorymemmove()- Move memory (handles overlap)memcmp()- Compare memory
4. Standard I/O
Location: libc-minimal/src/stdio.c
Basic I/O functions:
printf()- Formatted output (supports %d, %u, %x, %s, %c)puts()- Print string with newlineputchar()- Print single character
Note: printf() is a basic implementation. Full printf support can be added later.
5. Program Entry Point
Location: libc-minimal/src/start.c
Provides _start() function that:
- Gets argc, argv, envp from stack
- Calls
main() - Calls
_exit()with return value
Building
Prerequisites
- Cross-compiler toolchain (created by
build-toolchain.sh) - Kernel headers (created by
scripts/create-kernel-headers.sh)
Build Steps
cd libc-minimal
make
make install
This will:
- Compile all source files
- Create static library
lib/libc.a - Install to
build/sysroot/lib/andbuild/sysroot/usr/lib/
Using the Library
Compiling Programs
x86_64-freeworld-gcc -o hello hello.c -L../build/sysroot/lib -lc
Example Program
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Hello, FreeWorld OS!\n");
printf("argc = %d\n", argc);
if (argc > 0 && argv[0]) {
printf("argv[0] = %s\n", argv[0]);
}
return 0;
}
Comparison with glibc
| Feature | Minimal C Library | glibc |
|---|---|---|
| Lines of Code | ~500 lines | Millions of lines |
| Build Time | Seconds | Hours to days |
| Functions | Essential only | Complete POSIX |
| Complexity | Simple, understandable | Extremely complex |
| Extensibility | Easy to extend | Difficult to modify |
| Use Case | Getting started, basic programs | Full compatibility, production |
Extending the Library
To add new functions:
- Add implementation to
src/ - Add header declaration to
include/ - Rebuild:
make clean && make && make install
Future Additions
- More printf format specifiers
- File I/O functions (fopen, fread, fwrite, fclose)
- String functions (strcat, strstr, sprintf)
- Math functions (basic operations)
- Time functions (gettimeofday, etc.)
Integration
With Kernel
The library uses kernel system calls for all operations.
With Build System
Programs link against libc.a from build/sysroot/lib/.
With ELF Loader
Programs compiled with this library can be loaded and executed by the ELF loader.
Limitations
- No dynamic linking support
- Simple heap allocator (no free() implementation yet)
- Basic printf (limited format specifiers)
- No floating point support
- No locale support
- No threading support
These can be added incrementally as needed.
Related Documentation
- System Calls - Kernel system call interface
- ELF Loader - Loading programs
- execve() - Executing programs
- Build System - Compiling programs