Iterate over the shared objects that are loaded into a process's address space
#include <sys/link.h> int dl_iterate_phdr( Dl_Iterate_Phdr_t callback, void *data );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The dl_iterate_phdr() function iterates over all the shared objects that are loaded into the calling process's address space and calls the given callback once for each object, until it has processed all the shared objects, or the callback returns a nonzero value.
The prototype for the callback routine is as follows:
int callback ( const struct dl_phdr_info *info, size_t size, void *data);
Its arguments are:
struct dl_phdr_info { #if __PTR_BITS__ == 32 Elf32_Addr dlpi_addr; const char *dlpi_name; const Elf32_Phdr *dlpi_phdr; Elf32_Half dlpi_phnum; #elif __PTR_BITS__ == 64 Elf64_Addr dlpi_addr; const char *dlpi_name; const Elf64_Phdr *dlpi_phdr; Elf64_Half dlpi_phnum; #else #error __PTR_BITS__ not set properly #endif };
The entries are as follows:
The callback must not call dlclose(), but it can call dl_iterate_phdr(). It should return 0 on success, or a nonzero value if an error occurred.
The value that the last invocation of the callback returned.
#include <stdio.h> #include <stdlib.h> #include <sys/link.h> int my_callback ( const struct dl_phdr_info *info, size_t size, void *data) { int hdr_num; printf ("\n%s: base addr: %ld; %d program headers:\n", info->dlpi_name, info->dlpi_addr, info->dlpi_phnum); for (hdr_num = 0; hdr_num < info->dlpi_phnum; hdr_num++) printf(" %2d: address=%10p\n", hdr_num, (void *) (info->dlpi_addr + info->dlpi_phdr[hdr_num].p_vaddr)); return 0; } int main(int argc, char** argv) { printf ("My shared objects are as follows:\n"); dl_iterate_phdr( my_callback, NULL); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |