Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. ////////// bar.c
  2. #include <stdio.h>
  3.  
  4. void bar ()
  5. {
  6.     printf("bar\n");
  7. }
  8.  
  9. /////////// foo.c
  10.  
  11. #include <stdio.h>
  12. #include <mach-o/dyld.h>
  13. #include <mach-o/loader.h>
  14.  
  15.  
  16. struct dyld_image_info {
  17.     const struct mach_header*   imageLoadAddress;   /* base address image is mapped into */
  18.     const char*                 imageFilePath;      /* path dyld used to load the image */
  19.     uintptr_t                   imageFileModDate;   /* time_t of image file */
  20.                                                     /* if stat().st_mtime of imageFilePath does not match imageFileModDate, */
  21.                                                     /* then file has been modified since dyld loaded it */
  22. };
  23.  
  24. enum dyld_image_states
  25. {
  26.     dyld_image_state_mapped                 = 10,       // No batch notification for this
  27.     dyld_image_state_dependents_mapped      = 20,       // Only batch notification for this
  28.     dyld_image_state_rebased                = 30,
  29.     dyld_image_state_bound                  = 40,
  30.     dyld_image_state_dependents_initialized = 45,       // Only single notification for this
  31.     dyld_image_state_initialized            = 50,
  32.     dyld_image_state_terminated             = 60        // Only single notification for this
  33. };
  34.  
  35. typedef const char* (*dyld_image_state_change_handler)(enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[]);
  36.  
  37. void dyld_register_image_state_change_handler(enum dyld_image_states state, int batch, dyld_image_state_change_handler handler);
  38.  
  39.  
  40. const char* onLoad (enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
  41. {
  42.     printf("loading image=%s\n", info[0].imageFilePath);
  43.     return NULL;
  44. }
  45.  
  46. const char* onUnload (enum dyld_image_states state, uint32_t infoCount, const struct dyld_image_info info[])
  47. {
  48.     printf("unloading image=%s\n", info[0].imageFilePath);
  49.     return NULL;
  50. }
  51.  
  52. void foo ()
  53. {
  54.     printf("foo\n");
  55.    
  56.     dyld_register_image_state_change_handler(dyld_image_state_initialized, 0, &onLoad);
  57.     dyld_register_image_state_change_handler(dyld_image_state_terminated, 0, &onUnload);
  58. }
  59.  
  60. //////// main.c
  61.  
  62. #include <stdio.h>
  63. #include <dlfcn.h>
  64.  
  65. void loadBar ()
  66. {
  67.     void* handle = dlopen("bar.dylib", RTLD_LAZY);
  68.     void (*bar)() = dlsym(handle, "bar");
  69.     bar();
  70.     int r = dlclose(handle);
  71. }
  72.  
  73. void loadFoo ()
  74. {
  75.     void* handle = dlopen("foo.dylib", RTLD_LAZY);
  76.     void (*foo)() = dlsym(handle, "foo");
  77.     foo();
  78.     int r = dlclose(handle);
  79. }
  80.  
  81. int main (int argc, const char *argv[])
  82. {
  83.     loadFoo();
  84.     loadBar();
  85.     return 0;
  86. }
  87.  
  88. $ gcc -o foo.dylib foo.c -dynamiclib
  89. $ gcc -o bar.dylib bar.c -dynamiclib
  90. $ gcc -o main main.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement