Advertisement
Guest User

Untitled

a guest
Mar 27th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. /* <sys/linker_set.h> */
  4. #include <mach-o/getsect.h>
  5.  
  6. #define __LS_VA_STRINGIFY(_x...)    #_x
  7. #define __LS_VA_STRCONCAT(_x,_y)    __LS_VA_STRINGIFY(_x,_y)
  8. #define __LINKER_MAKE_SET(_set, _sym)                   \
  9.     /*__unused*/ /*static*/ void const * /*const*/ __set_##_set##_sym_##_sym        \
  10.     __attribute__ ((section(__LS_VA_STRCONCAT(__DATA,_set)))) = (void *)&_sym
  11. /* */
  12.  
  13. /* <sys/linker_set.h>: getsectdatafromheader() -> getsectdata() */
  14. static __inline void **
  15. __linker_set_object_begin_fixed(const char *_set)
  16. {
  17.     void *_set_begin;
  18.     unsigned long _size;
  19.  
  20.     _set_begin = getsectdata("__DATA", _set, &_size);
  21.     return( (void **) _set_begin );
  22. }
  23. static __inline void **
  24. __linker_set_object_limit_fixed(const char *_set)
  25. {
  26.     void *_set_begin;
  27.     unsigned long _size;
  28.  
  29.     _set_begin = getsectdata("__DATA", _set, &_size);
  30.    
  31.     return ((void **) ((uintptr_t) _set_begin + _size));
  32. }
  33. /* */
  34.  
  35. char section[] = "my";
  36.  
  37. int x = 5;
  38. int y = 10;
  39. int z = 22;
  40.  
  41. __LINKER_MAKE_SET(my,x);
  42. __LINKER_MAKE_SET(my,y);
  43. __LINKER_MAKE_SET(my,z);
  44.  
  45. void
  46. main()
  47. {
  48.     printf("Number of entries in section: %d\n",
  49.         __linker_set_object_limit_fixed(section) - __linker_set_object_begin_fixed(section));
  50.     printf("Data of first entry: %d\n", *((int *) *__linker_set_object_begin_fixed(section)) );
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement