diff --git a/postject-api.h b/postject-api.h index 4ec1f64..875ddc3 100644 --- a/postject-api.h +++ b/postject-api.h @@ -12,7 +12,6 @@ #elif defined(__linux__) #include #include -#include #include #elif defined(_WIN32) #include @@ -44,6 +43,16 @@ static inline bool postject_has_resource() { return sentinel[sizeof(POSTJECT_SENTINEL_FUSE)] == '1'; } +#if defined(__linux__) +static int postject__dl_iterate_phdr_callback(struct dl_phdr_info* info, + size_t size, + void* data) { + // Snag the dl_phdr_info struct for the main program, then stop iterating + *((struct dl_phdr_info*)data) = *info; + return 1; +} +#endif + static const void* postject_find_resource( const char* name, size_t* size, @@ -114,9 +123,12 @@ static const void* postject_find_resource( name = options->elf_section_name; } - uintptr_t p = getauxval(AT_PHDR); - size_t n = getauxval(AT_PHNUM); - uintptr_t base_addr = p - sizeof(ElfW(Ehdr)); + struct dl_phdr_info main_program_info; + dl_iterate_phdr(postject__dl_iterate_phdr_callback, &main_program_info); + + uintptr_t p = (uintptr_t)main_program_info.dlpi_phdr; + size_t n = main_program_info.dlpi_phnum; + uintptr_t base_addr = main_program_info.dlpi_addr; // iterate program header for (; n > 0; n--, p += sizeof(ElfW(Phdr))) { diff --git a/test/test.c b/test/test.c index d1b8ff4..f957c6f 100644 --- a/test/test.c +++ b/test/test.c @@ -1,3 +1,7 @@ +#define _GNU_SOURCE // This is needed because postject-api.h uses + // dl_iterate_phdr and dl_phdr_info which are non-standard + // GNU extensions. + #include #include