Created
January 12, 2009 22:57
-
-
Save dreiss/46215 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program | |
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <dlfcn.h> | |
void shared_lib_operation(); | |
int main() { | |
void *handle; | |
void (*funptr)(void); | |
char *error; | |
printf("Main program executing shared library operation.\n"); | |
shared_lib_operation(); | |
printf("Loading plugin without RTLD_DEEPBIND.\n"); | |
handle = dlopen("libplug.so", RTLD_LAZY); | |
if (!handle) { | |
fprintf(stderr, "%s\n", dlerror()); | |
exit(EXIT_FAILURE); | |
} | |
dlerror(); | |
*(void **)(&funptr) = dlsym(handle, "execute_plugin"); | |
if ((error = dlerror()) != NULL) { | |
fprintf(stderr, "%s\n", error); | |
exit(EXIT_FAILURE); | |
} | |
funptr(); | |
dlclose(handle); | |
printf("Loading plugin with RTLD_DEEPBIND.\n"); | |
handle = dlopen("libplug.so", RTLD_LAZY | RTLD_DEEPBIND); | |
if (!handle) { | |
fprintf(stderr, "%s\n", dlerror()); | |
exit(EXIT_FAILURE); | |
} | |
dlerror(); | |
*(void **)(&funptr) = dlsym(handle, "execute_plugin"); | |
if ((error = dlerror()) != NULL) { | |
fprintf(stderr, "%s\n", error); | |
exit(EXIT_FAILURE); | |
} | |
funptr(); | |
dlclose(handle); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CFLAGS = -Wall -Wextra | |
all: program libplug.so libnorm.so libspec.so | |
program: main_program.c libspec.so | |
$(CC) -o $@ $< -L. -ldl -lspec | |
libplug.so: plugin.c libnorm.so | |
$(CC) -o $@ $< -L. -shared -fPIC -lnorm | |
libnorm.so: normal_shared_library.c | |
$(CC) -o $@ $< -shared -fPIC | |
libspec.so: special_shared_library.c | |
$(CC) -o $@ $< -shared -fPIC | |
.PHONY: clean run | |
run: all | |
LD_LIBRARY_PATH=$$PWD ./program | |
clean: | |
$(RM) program *.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void execute_plugin() { | |
printf("Plugin executing shared library operation.\n"); | |
shared_lib_operation(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment