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.

✅ Fully Implemented
Why Minimal? Building full glibc for a new OS is a multi-year project. This minimal library (~500 lines) provides enough functionality to run basic programs and can be extended incrementally.

Components

1. System Call Wrappers

Location: libc-minimal/src/syscalls.c

Wrappers for kernel system calls:

  • _exit() - Terminate process
  • write() - Write to file descriptor
  • read() - Read from file descriptor
  • open() - Open file
  • close() - Close file descriptor
  • execve() - Execute program

2. Memory Allocation

Location: libc-minimal/src/malloc.c

Simple heap allocator:

  • malloc() - Allocate memory
  • free() - Free memory (stub - no-op for now)
  • calloc() - Allocate and zero memory
  • realloc() - 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 length
  • strcpy() - Copy string
  • strncpy() - Copy string with length limit
  • strcmp() - Compare strings
  • strncmp() - Compare strings with length limit
  • memset() - Set memory
  • memcpy() - Copy memory
  • memmove() - 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 newline
  • putchar() - 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:

  1. Compile all source files
  2. Create static library lib/libc.a
  3. Install to build/sysroot/lib/ and build/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:

  1. Add implementation to src/
  2. Add header declaration to include/
  3. 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