DL_ITERATE_PHDR(3)

NAME
dl_iterate_phdr - iterate over program headers

LIBRARY
This function is not in a library. It is included in every dynamically linked program automatically.

SYNOPSIS
#include <link.h>
int
dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void*), void *data);

DESCRIPTION
The dl_iterate_phdr() function iterates over all shared objects loaded into a process's address space, calling callback for each shared object, passing it information about the object's program headers and the data argument. The information about the program headers is passed in a structure that is defined as:

    struct dl_phdr_info {
            Elf_Addr                dlpi_addr;
            const char             *dlpi_name;
            const Elf_Phdr         *dlpi_phdr;
            Elf_Half                dlpi_phnum;
            unsigned long long int  dlpi_adds;
            unsigned long long int  dlpi_subs;
            size_t                  dlpi_tls_modid;
            void                   *dlpi_tls_data;
    };

The members of struct dl_phdr_info have the following meaning:

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.

dlpi_adds
    The number of objects added into the main program.

dlpi_subs
    The number of objects removed from the main program.

To make it possible for programs to check whether any new members have been added, the size of the structure is passed as an argument to callback.

RETURN VALUES
The dl_iterate_phdr() function returns whatever value was returned by the last call to callback.

EXAMPLES
The following program displays a list of pathnames of the shared objects it has loaded. For each shared object, the program lists the virtual addresses at which the object's ELF segments are loaded.

    #include <link.h>
    #include <stdlib.h>
    #include <stdio.h>
    static int
    callback(struct dl_phdr_info *info, size_t size, void *data)
    {
        int j;
        printf("name=%s (%d segments)\n", info->dlpi_name,
            info->dlpi_phnum);
        for (j = 0; j < info->dlpi_phnum; j++)
             printf("		 header %2d: address=%10p\n", j,
                 (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
        return 0;
    }
    int
    main(int argc, char *argv[])
    {
        dl_iterate_phdr(callback, NULL);
        exit(EXIT_SUCCESS);
    }

SEE ALSO
ld(1), ld-elf.so.2(1), dlfcn(3), elf(5)

HISTORY
The dl_iterate_phdr function first appeared in DragonFly 2.9.