mr4th-dynamic-linking/xlist/base.c

73 lines
1.3 KiB
C
Raw Normal View History

// base symbols shared
#if defined(BASE_IMPLEMENTOR)
#include <stdio.h>
int x = 0;
void
base_func(void){
printf("x = %d\n", x);
x += 1;
}
#endif
// dynamic function linkage protocol
#if defined(BASE_IMPLEMENTOR)
EXPORT_SYMBOL BASE_Funcs*
base_export_functions(void){
static BASE_Funcs funcs = {0};
if (funcs.base_func == 0){
#define X(N,R,P) funcs.N = N;
#include "base.xlist.h"
#undef X
}
return(&funcs);
}
#endif
#if !defined(BASE_IMPLEMENTOR)
#if OS_WINDOWS
# include <Windows.h>
#elif OS_LINUX
# include <dlfcn.h>
#endif
BEFORE_MAIN(base_dynamic_user_init){
BASE_Funcs *funcs = 0;
#if OS_WINDOWS
HMODULE module = LoadLibraryA("base.dll");
if (module != 0){
BASE_ExportFuncs *base_export_functions = (BASE_ExportFuncs*)GetProcAddress(module, "base_export_functions");
if (base_export_functions != 0){
funcs = base_export_functions();
}
}
#elif OS_LINUX
void *module = dlopen("$ORIGIN/base.so", RTLD_NOW);
if (module != 0){
BASE_ExportFuncs *base_export_functions = (BASE_ExportFuncs*)dlsym(module, "base_export_functions");
if (base_export_functions != 0){
funcs = base_export_functions();
}
}
#else
# error base_import_functions not completed for this OS
#endif
#define X(N,R,P) N = funcs->N;
#include "base.xlist.h"
#undef X
}
#endif