Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 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. void onLoad (const struct mach_header* h, intptr_t slide)
  16. {
  17.     printf("loading image\n");
  18. }
  19.  
  20. void onUnload (const struct mach_header* h, intptr_t slide)
  21. {
  22.     printf("unloading image\n");
  23. }
  24.  
  25. void foo ()
  26. {
  27.     printf("foo\n");
  28.    
  29.     _dyld_register_func_for_add_image(&onLoad);
  30.     _dyld_register_func_for_remove_image(&onUnload);
  31. }
  32.  
  33. //////// main.c
  34.  
  35. #include <stdio.h>
  36. #include <dlfcn.h>
  37.  
  38. void loadBar ()
  39. {
  40.     void* handle = dlopen("bar.dylib", RTLD_LAZY);
  41.     void (*bar)() = dlsym(handle, "bar");
  42.     bar();
  43.     int r = dlclose(handle);
  44. }
  45.  
  46. void loadFoo ()
  47. {
  48.     void* handle = dlopen("foo.dylib", RTLD_LAZY);
  49.     void (*foo)() = dlsym(handle, "foo");
  50.     foo();
  51.     int r = dlclose(handle);
  52. }
  53.  
  54. int main (int argc, const char *argv[])
  55. {
  56.     loadFoo();
  57.     loadBar();
  58.     return 0;
  59. }
  60.  
  61. $ gcc -o foo.dylib foo.c -dynamiclib
  62. $ gcc -o bar.dylib bar.c -dynamiclib
  63. $ gcc -o main main.c
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement