dynamic-dso/example.c

55 lines
1.2 KiB
C

#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include "dynso.h"
static void assert_ok_base(const char* file, int line, enum dynso_err e) {
if (e != dynso_ok) {
printf("rip! at %s:%d: %d\n", file, line, e);
}
}
#define assert_ok(x) assert_ok_base(__FILE__, __LINE__, (x))
static void a_function(void) {
printf("hello world!\n");
}
int main(int argc, char* argv[]) {
void* x;
printf("pre:\n");
x = dlsym(RTLD_DEFAULT, "testsym");
printf(" dlsym(\"testsym\") = %p\n", x);
x = dlsym(RTLD_DEFAULT, "testfunction");
printf(" dlsym(\"testfunction\") = %p\n", x);
struct dynso_lib* l;
assert_ok(dynso_create(&l, 0,
(char*)"this is just a display name", "libtest",
NULL, LM_ID_BASE));
assert_ok(dynso_add_sym(l, "testsym", (void*)0x694201337));
assert_ok(dynso_add_sym(l, "testfunction", a_function));
assert_ok(dynso_bind(l));
printf("post:\n");
x = dlsym(RTLD_DEFAULT, "testsym");
printf(" dlsym(\"testsym\") = %p\n", x);
x = dlsym(RTLD_DEFAULT, "testfunction");
printf(" dlsym(\"testfunction\") = %p\n", x);
void (*somefunc)(void) = x;
printf("calling the resolved function:\n");
somefunc();
dynso_remove(l);
return 0;
}