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>
- struct dyld_image_info {
- const struct mach_header* imageLoadAddress; /* base address image is mapped into */
- const char* imageFilePath; /* path dyld used to load the image */
- uintptr_t imageFileModDate; /* time_t of image file */
- /* if stat().st_mtime of imageFilePath does not match imageFileModDate, */
- /* then file has been modified since dyld loaded it */
- };
- enum dyld_image_states
- {
- dyld_image_state_mapped = 10, // No batch notification for this
- dyld_image_state_dependents_mapped = 20, // Only batch notification for this
- dyld_image_state_rebased = 30,
- dyld_image_state_bound = 40,
- dyld_image_state_dependents_initialized = 45, // Only single notification for this
- dyld_image_state_initialized = 50,
- dyld_image_state_terminated = 60 // Only single notification for this
- };
- typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
- void dyld_register_image_state_change_handler(enum dyld_image_states state, int batch, dyld_image_state_change_handler handler);
- const char* onLoad (enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
- {
- printf("loading image=%s\n", info[0].imageFilePath);
- return NULL;
- }
- const char* onUnload (enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
- {
- printf("unloading image=%s\n", info[0].imageFilePath);
- return NULL;
- }
- void foo ()
- {
- printf("foo\n");
- dyld_register_image_state_change_handler(dyld_image_state_initialized, 0, &onLoad);
- dyld_register_image_state_change_handler(dyld_image_state_terminated, 0, &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