dl_iterate_phdr()

Updated: May 06, 2022

Iterate over the shared objects that are loaded into a process's address space

Synopsis:

#include <sys/link.h>

int dl_iterate_phdr( Dl_Iterate_Phdr_t callback,
                     void *data );

Arguments:

callback
A pointer to a function that you want to be called for each loaded shared object.
data
A pointer to some arbitrary data that you want to pass to the callback.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

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:

info
A pointer to a dl_phdr_info structure that includes information about the shared object:
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:

  • dlpi_addr — the base address at which the shared object is mapped into the address space of the calling process.
  • dlpi_name — the name of the shared object.
  • dlpi_phdr — a pointer to the shared object's program headers.
  • dlpi_phnum — the number of program headers in the shared object.
size
The size of the dl_phdr_info structure.
data
The data argument that you passed to dl_iterate_phdr().

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.

Returns:

The value that the last invocation of the callback returned.

Examples:

#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;
}

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes