Learn more about Russian war crimes in Ukraine.

How to make plugins with dlopen

Dynamic libraries, like /usr/lib/system/libsystem_c.dylib, are usually loaded before the program runs. MacOS’s execve system call knows how to extract references to dynamic libraries from Mach-O files, and load these into memory before continuing. But dynamic libraries can also be explicitly loaded at runtime. To explicitly load dynamic libraries, we use the dlfcn.h (“dynamic loading functions”?) API. This is how “plugins” work: a plugin is a dynamic library!

The three functions provided by dlfcn.h are dlopen, dlclose, and dlsym. The dlopen function loads a dynamic library from a file into memory. The dlclose unloads it from memory. In between, we use dlsym to extract references to symbols in the library. These symbols give us access to functions and other things in the library.

Here’s an example. We have main.c, our main program, and plugin.c, our plugin. The main program expects the plugin to define plugin_func, which is a function taking no arguments and returning an int. The main program loads the plugin, calls that function, then exits.

// main.c
#include <dlfcn.h>
#include <stdio.h>
typedef int plugin_func();
int main() {
  void* handle = dlopen("plugin.so", RTLD_LAZY);
  if (handle == NULL) {
    fprintf(stderr, "Could not open plugin: %s\n", dlerror());
    return 1;
  }
  plugin_func* f = dlsym(handle, "plugin_func");
  if (f == NULL) {
    fprintf(stderr, "Could not find plugin_func: %s\n", dlerror());
    return 1;
  }
  printf("Calling plugin\n");
  int ret = f();
  printf("Plugin returned %d\n", ret);
  if (dlclose(handle) != 0) {
    fprintf(stderr, "Could not close plugin: %s\n", dlerror());
    return 1;
  }
  return 0;
}
// plugin.c
#include <stdio.h>
int plugin_func(void) {
  printf("Hello from the plugin!\n");
  return 42;
}

We compile the plugin with -shared, and put the compiled object at plugin.so (“shared library”).

$ clang -shared -o plugin.so plugin.c
$ clang main.c
$ ./a.out
Calling plugin
Hello from the plugin!
Plugin returned 42

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.