1DL_ITERATE_PHDR(3)         Linux Programmer's Manual        DL_ITERATE_PHDR(3)
2
3
4

NAME

6       dl_iterate_phdr - walk through list of shared objects
7

SYNOPSIS

9       #define _GNU_SOURCE         /* See feature_test_macros(7) */
10       #include <link.h>
11
12       int dl_iterate_phdr(
13                 int (*callback) (struct dl_phdr_info *info,
14                                  size_t size, void *data,
15                 void *data;
16

DESCRIPTION

18       The  dl_iterate_phdr() function allows an application to inquire at run
19       time to find out which shared objects it has loaded.
20
21       The dl_iterate_phdr() function walks through the list  of  an  applica‐
22       tion's  shared  objects  and  calls the function callback once for each
23       object, until either all shared objects have been processed or callback
24       returns a nonzero value.
25
26       Each  call  to  callback  receives  three  arguments:  info, which is a
27       pointer to a structure containing information about the shared  object;
28       size,  which is the size of the structure pointed to by info; and data,
29       which is a copy of whatever value was passed by the calling program  as
30       the second argument (also named data) in the call to dl_iterate_phdr().
31
32       The info argument is a structure of the following type:
33
34           struct dl_phdr_info {
35               ElfW(Addr)        dlpi_addr;  /* Base address of object */
36               const char       *dlpi_name;  /* (Null-terminated) name of
37                                                object */
38               const ElfW(Phdr) *dlpi_phdr;  /* Pointer to array of
39                                                ELF program headers
40                                                for this object */
41               ElfW(Half)        dlpi_phnum; /* # of items in dlpi_phdr */
42           };
43
44       (The ElfW() macro definition turns its argument into the name of an ELF
45       data type suitable for the hardware architecture.  For  example,  on  a
46       32-bit platform, ElfW(Addr) yields the data type name Elf32_Addr.  Fur‐
47       ther information on these  types  can  be  found  in  the  <elf.h>  and
48       <link.h> header files.)
49
50       The  dlpi_addr  field  indicates  the base address of the shared object
51       (i.e., the difference between the virtual memory address of the  shared
52       object  and  the  offset  of  that object in the file from which it was
53       loaded).  The dlpi_name field is a null-terminated  string  giving  the
54       pathname from which the shared object was loaded.
55
56       To  understand  the  meaning of the dlpi_phdr and dlpi_phnum fields, we
57       need to be aware that an ELF shared object consists of a number of seg‐
58       ments,  each of which has a corresponding program header describing the
59       segment.  The dlpi_phdr field is a pointer to an array of  the  program
60       headers  for  this  shared  object.  The dlpi_phnum field indicates the
61       size of this array.
62
63       These program headers are structures of the following form:
64
65           typedef struct {
66               Elf32_Word  p_type;    /* Segment type */
67               Elf32_Off   p_offset;  /* Segment file offset */
68               Elf32_Addr  p_vaddr;   /* Segment virtual address */
69               Elf32_Addr  p_paddr;   /* Segment physical address */
70               Elf32_Word  p_filesz;  /* Segment size in file */
71               Elf32_Word  p_memsz;   /* Segment size in memory */
72               Elf32_Word  p_flags;   /* Segment flags */
73               Elf32_Word  p_align;   /* Segment alignment */
74           } Elf32_Phdr;
75
76       Note that we can calculate the location of a particular program header,
77       x, in virtual memory using the formula:
78
79         addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
80

RETURN VALUE

82       The  dl_iterate_phdr()  function returns whatever value was returned by
83       the last call to callback.
84

VERSIONS

86       dl_iterate_phdr() has been supported in glibc since version 2.2.4.
87

CONFORMING TO

89       The dl_iterate_phdr() function is Linux-specific and should be  avoided
90       in portable applications.
91

EXAMPLE

93       The  following  program  displays  a  list  of  pathnames of the shared
94       objects it has loaded.  For each shared object, the program  lists  the
95       virtual addresses at which the object's ELF segments are loaded.
96
97       #define _GNU_SOURCE
98       #include <link.h>
99       #include <stdlib.h>
100       #include <stdio.h>
101
102       static int
103       callback(struct dl_phdr_info *info, size_t size, void *data)
104       {
105           int j;
106
107           printf("name=%s (%d segments)\n", info->dlpi_name,
108               info->dlpi_phnum);
109
110           for (j = 0; j < info->dlpi_phnum; j++)
111                printf("\t\t header %2d: address=%10p\n", j,
112                    (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
113           return 0;
114       }
115
116       int
117       main(int argc, char *argv[])
118       {
119           dl_iterate_phdr(callback, NULL);
120
121           exit(EXIT_SUCCESS);
122       }
123

SEE ALSO

125       ldd(1), objdump(1), readelf(1), dlopen(3), elf(5), ld.so(8)
126
127       Executable and Linking Format Specification, available at various loca‐
128       tions online.
129

COLOPHON

131       This page is part of release 3.53 of the Linux  man-pages  project.   A
132       description  of  the project, and information about reporting bugs, can
133       be found at http://www.kernel.org/doc/man-pages/.
134
135
136
137GNU                               2007-05-18                DL_ITERATE_PHDR(3)
Impressum