Skip to content

EliasJHL/MiniLibC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniLibC

This project is a custom implementation of several standard C library functions written entirely in x86-64 assembly (NASM). The functions are compiled into a dynamic shared library (.so) that can be loaded and used in C programs.

Implemented Functions

The following standard library functions have been re-implemented:

  • strlen
  • strchr
  • strrchr
  • strcmp
  • strncmp
  • strcasecmp
  • strstr
  • strpbrk
  • strcspn
  • memset
  • memcpy
  • memmove

Prerequisites

To build and test the library, you will need the following tools:

  • make
  • nasm (Netwide Assembler)
  • gcc (GNU Compiler Collection)
  • criterion (for running the unit tests)

Building the Library

You can use the provided Makefile to build the shared library libasm.so.

  • Build the library:

    make
  • Clean object files:

    make clean
  • Clean all generated files (library and objects):

    make fclean
  • Rebuild the library from scratch:

    make re

Running Tests

The repository includes a comprehensive test suite using the Criterion framework. The tests compare the behavior of each custom assembly function against its original C standard library counterpart.

  • To compile and run the unit tests:
    make test_run

Usage

You can use the generated libasm.so in your own C projects by dynamically loading it.

Here is an example of how to use the custom strlen function from this library.

  1. First, build the library:

    make
  2. Create a C file, for example, main.c:

    #include <stdio.h>
    #include <dlfcn.h>
    #include <stdlib.h>
    
    int main(void) {
        void *handle;
        size_t (*my_strlen)(const char *);
        char *error;
    
        // Load the shared library
        handle = dlopen("./libasm.so", RTLD_LAZY);
        if (!handle) {
            fprintf(stderr, "Error loading library: %s\n", dlerror());
            exit(EXIT_FAILURE);
        }
    
        // Clear any existing error
        dlerror();
    
        // Get a pointer to the custom strlen function
        *(void **)(&my_strlen) = dlsym(handle, "strlen");
        error = dlerror();
        if (error != NULL) {
            fprintf(stderr, "Error finding function: %s\n", error);
            exit(EXIT_FAILURE);
        }
    
        // Use the function
        const char *str = "Hello from MiniLibC!";
        printf("String: \"%s\"\n", str);
        printf("Length (using my_strlen): %zu\n", my_strlen(str));
    
        // Unload the library
        dlclose(handle);
    
        return 0;
    }
  3. Compile your C program, linking against the dynamic linking library (-ldl):

    gcc -o my_program main.c -ldl
  4. Run your executable. Ensure libasm.so is in the same directory or in your LD_LIBRARY_PATH.

    ./my_program

    Output:

    String: "Hello from MiniLibC!"
    Length (using my_strlen): 20
    

About

Basic C standard library functions written in Assembly

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors