Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////// bar.c
- #include <stdio.h>
- void bar ()
- {
- printf("bar\n");
- }
- /////////// foo.c
- #include <stdio.h>
- #include <mach-o/dyld.h>
- #include <mach-o/loader.h>
- void onLoad (const struct mach_header* h, intptr_t slide)
- {
- printf("loading image\n");
- }
- void onUnload (const struct mach_header* h, intptr_t slide)
- {
- printf("unloading image\n");
- }
- void foo ()
- {
- printf("foo\n");
- _dyld_register_func_for_add_image(&onLoad);
- _dyld_register_func_for_remove_image(&onUnload);
- }
- //////// main.c
- #include <stdio.h>
- #include <dlfcn.h>
- void loadBar ()
- {
- void* handle = dlopen("bar.dylib", RTLD_LAZY);
- void (*bar)() = dlsym(handle, "bar");
- bar();
- int r = dlclose(handle);
- }
- void loadFoo ()
- {
- void* handle = dlopen("foo.dylib", RTLD_LAZY);
- void (*foo)() = dlsym(handle, "foo");
- foo();
- int r = dlclose(handle);
- }
- int main (int argc, const char *argv[])
- {
- loadFoo();
- loadBar();
- return 0;
- }
- $ gcc -o foo.dylib foo.c -dynamiclib
- $ gcc -o bar.dylib bar.c -dynamiclib
- $ gcc -o main main.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement