Skip to content

Freestanding mini-libc implementation for Linux x86_64, serving as a minimal C runtime. Features a custom memory allocator using mmap, unbuffered I/O wrappers, process control, and standard string operations. Designed for education, it bypasses external libraries to interact directly with the kernel via syscalls.

Notifications You must be signed in to change notification settings

GeorgeGrasu/Mini-libc-Implementation

Repository files navigation

Mini-libc Implementation Details

This document provides a technical overview of the internal implementation of the mini-libc library. Unlike the standard glibc, which is optimized for performance and complexity, to be robust, this mini-libc is designed for educational purposes, favoring simplicity and direct interaction with the kernel.

1. Core Architecture

The library is freestanding, meaning it has no external dependencies. It interacts directly with the Linux kernel via system calls.

  • Architecture: x86_64 (Linux)
  • EntryPoint: _start (defined in src/crt/start.S)
  • Syscall Mechanism: syscall instruction

2. Memory Management (mm/)

The memory allocator does not use a traditional heap (sbrk/brk). Instead, it relies entirely on memory mappings for every allocation.

  • malloc(size):

    • Directly calls mmap with MAP_PRIVATE | MAP_ANONYMOUS.
    • Stores metadata about the allocation in a global linked list (mem_list).
    • Trade-off: High overhead for small allocations (minimum 1 page per call), but extremely simple to implement and debug.
  • free(ptr):

    • Finds the pointer in mem_list.
    • Calls munmap to release the memory to the OS.
    • Removes the entry from the tracking list.
  • realloc(ptr, size):

    • Allocates a new block with malloc.
    • Copies data using memcpy.
    • Frees the old block.

3. String & Memory Operations (string/)

The string functions are implemented using standard pointer arithmetic logic, adhering to the C standard behavior.

  • strcpy / strncpy / strcat: Byte-by-byte copying until null terminator or limit.
  • memmove:
    • Handles overlapping memory regions correctly.
    • Checks if dest < src or dest > src to decide whether to copy forwards or backwards, preventing data corruption.
  • strstr: Brute-force substring search ($O(N*M)$ complexity).

4. System Call Interface (src/syscall.c)

All kernel interactions funnel through a central syscall() wrapper.

  • Implementation:
    • Uses the syscall function which wraps the inline assembly or architecture specific function.
    • Passes arguments in the correct registers for the x86_64 ABI (rdi, rsi, rdx, r10, r8, r9).
    • Returns the kernel result (usually 0 for success, negative for errno, or positive for value).

5. Input/Output (io/)

All I/O operations are unbuffered and map 1:1 to their respective system calls.

  • open, close, lseek: Direct wrappers around sys_open, sys_close, sys_lseek.
  • puts:
    • Implemented using write(1, ...).
    • Calculates string length first, then writes the string, followed by a newline \n.
    • No internal buffer (unlike stdio's fwrite).

6. Process Management (process/)

  • sleep / nanosleep:
    • sleep is a wrapper that constructs a timespec struct and calls the nanosleep syscall.
    • exit: calls sys_exit to terminate the process immediately.

7. Startup Sequence (crt/)

  1. _start (ASM): The kernel yields control here. It sets up the stack frame.
  2. __libc_start_main (C):
    • Initializes internal subsystems (like mem_list).
    • Calls the user's main().
    • Passes the return value of main to exit().

This implementation serves as a minimal, educational model of how C runtimes bridge user code and the operating system kernel.

About

Freestanding mini-libc implementation for Linux x86_64, serving as a minimal C runtime. Features a custom memory allocator using mmap, unbuffered I/O wrappers, process control, and standard string operations. Designed for education, it bypasses external libraries to interact directly with the kernel via syscalls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published