Skip to content

Instantly share code, notes, and snippets.

@fennifith
Last active July 8, 2019 15:42
Show Gist options
  • Save fennifith/f054ce113285aca1b0b405f54dcca61a to your computer and use it in GitHub Desktop.
Save fennifith/f054ce113285aca1b0b405f54dcca61a to your computer and use it in GitHub Desktop.
Simple C99 utility to import/link dynamic libraries during runtime.
/*
* MIT License
*
* Copyright (c) 2019 James Fenn <me@jfenn.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <string.h>
#include <dlfcn.h>
#include <stdarg.h>
/**
* Link a method pointer to a specified function in a dynamic
* library.
*
* @param dlib The dynamic library to be linked, created
* with dlopen().
* @param method_ptr A function pointer to load the method into.
* @param method_name The (string) name of the method to link.
* @return 1 if linking was successful; 0 if not.
*/
char dlib_link(void* dlib, void* method_ptr, char* method_name) {
void (*method)() = dlsym(dlib, method_name); // load the method
if (method == NULL) { // fail if null
// Error: couldn't load method '%s' from shared library
return 0;
}
// copy method pointer to the location of the pointer passed in the array
memcpy((void*) method_ptr, &method, sizeof(void*));
return 1;
}
/**
* Load the specified dynamic library and assign its
* function pointers to the ones provided.
*
* @param name The name of the shared library.
* @param count The number of methods to load.
* @param ... Variadic arguments alternating between
* function pointers to write to and the
* method names to load into them.
* @return 1 if linking was successful; 0 if not.
*/
char dlib_import(char* name, unsigned int count, ...) {
va_list methods;
va_start(methods, count);
void* lib = dlopen(name, RTLD_NOW); // open the library
if (lib == NULL) { // fail if null
// "Error: couldn't open shared library
return 0;
}
for (unsigned int i = 0; i < count; i++) {
char status = dlib_link(
lib,
(void*) va_arg(methods, char*),
va_arg(methods, char*)
);
if (status == 0) {
// Error: couldn't load specific method.
return 0;
}
}
va_end(methods);
return 1;
}

This function is ridiculous, but I wrote it anyway.

Usage

The docs pretty much explain how it works, but here's an example of it in use:

#include "dlib_import.c"

// function declarations for zlib / dynamic library
unsigned long (*compressBound)(unsigned long length);
int (*compress)(void *dest, unsigned long* destLen, const void* source, unsigned long sourceLen);
int (*decompress)(void *dest, unsigned long* destLen, const void* source, unsigned long sourceLen);

// main function, simply loads library & exits
int main() {
    return dlib_import("libz.so", 3,
        (char*) &compressBound,    "compressBound",
        (char*) &compress,         "compress",
        (char*) &decompress,       "uncompress"
    );
}

Building

Same as usual. Don't forget the -ldl flag!

gcc main.c -ldl -g -o main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment