From 799f46a3e38bbfa28fb0bc47ef73b63fa40c421f Mon Sep 17 00:00:00 2001 From: Snaipe Date: Fri, 7 Aug 2015 19:49:04 -0700 Subject: [PATCH] [Issue #30] Added special accessors for section limits on OS X --- src/posix-compat.c | 27 +++++++++++++++++++++++++++ src/posix-compat.h | 5 +++++ 2 files changed, 32 insertions(+) diff --git a/src/posix-compat.c b/src/posix-compat.c index 2056baa..037a0aa 100644 --- a/src/posix-compat.c +++ b/src/posix-compat.c @@ -360,3 +360,30 @@ void *get_win_section_end(const char *section) { return NULL; } #endif + +#ifdef __APPLE__ +# include +# include + +# define BASE_IMAGE_INDEX 0 + +static inline void *get_real_address(void *addr) { + if (!addr) + return NULL; + + // We need to slide the section address to get a valid pointer + // because ASLR will shift the image by a random offset + return addr + _dyld_get_image_vmaddr_slide(BASE_IMAGE_INDEX); +} + +void *get_osx_section_start(const char *section) { + unsigned long secsize; + return get_real_address(getsectdata("__DATA", section, &secsize)); +} + +void *get_osx_section_end(const char *section) { + unsigned long secsize; + char *section_start = getsectdata("__DATA", section, &secsize); + return get_real_address(section_start) + secsize; +} +#endif diff --git a/src/posix-compat.h b/src/posix-compat.h index e4b8d5e..82da12c 100644 --- a/src/posix-compat.h +++ b/src/posix-compat.h @@ -81,6 +81,11 @@ void *get_win_section_start(const char *section); void *get_win_section_end(const char *section); # define GET_SECTION_START(Name) get_win_section_start(#Name) # define GET_SECTION_END(Name) get_win_section_end(#Name) +# elif defined(__APPLE__) +void *get_osx_section_start(const char *section); +void *get_osx_section_end(const char *section); +# define GET_SECTION_START(Name) get_osx_section_start(#Name) +# define GET_SECTION_END(Name) get_osx_section_end(#Name) # else # define GET_SECTION_START(Name) SECTION_START(Name) # define GET_SECTION_END(Name) SECTION_END(Name)