Skip to content

Instantly share code, notes, and snippets.

@Simon-L
Created June 9, 2022 16:09
Show Gist options
  • Select an option

  • Save Simon-L/8f7386ef1ee05075c8a783b80a1b4312 to your computer and use it in GitHub Desktop.

Select an option

Save Simon-L/8f7386ef1ee05075c8a783b80a1b4312 to your computer and use it in GitHub Desktop.
bare minimum dlsym (dlopen bug on box64)
#include <cstdio>
#include <dlfcn.h>
typedef void (*LibFunc)();
LibFunc getSym(const char* sopath) {
void* handle = dlopen(sopath, RTLD_NOW | RTLD_LOCAL);
if (!handle) {
printf("Oops exit1!\n");
return (LibFunc)(0);
}
dlerror();
const char* dlsym_error = dlerror();
LibFunc func = (LibFunc) dlsym(handle, "_Z6myFuncv");
dlsym_error = dlerror();
if (dlsym_error) {
printf("error! %s", dlsym_error);
return (LibFunc)(0);
}
if (!func) {
printf("Oops exit2!\n");
return (LibFunc)(0);
}
return func;
}
int main() {
printf("Loading function from shared1...");
LibFunc myFunc1 = getSym("shared1/sharedobj.so");
myFunc1();
printf("Loading function from shared2...");
LibFunc myFunc2 = getSym("shared2/sharedobj.so");
myFunc2();
return 0;
}
#include <cstdio>
void myFunc() {
printf("Hello from obj2!\n");
}
#include <cstdio>
void myFunc() {
printf("Hello from obj1!\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment