1 .\" Copyright (c) 2003 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH DL_ITERATE_PHDR 3 2007-05-18 "GNU" "Linux Programmer's Manual"
27 dl_iterate_phdr \- walk through list of shared objects
30 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
33 .BI "int dl_iterate_phdr("
34 .BI " int (*" callback ") (struct dl_phdr_info *" info ,
35 .BI " size_t " size ", void *" data "),"
36 .BI " void *" data ");"
40 .BR dl_iterate_phdr ()
41 function allows an application to inquire at run time to find
42 out which shared objects it has loaded.
45 .BR dl_iterate_phdr ()
46 function walks through the list of an
47 application's shared objects and calls the function
50 until either all shared objects have been processed or
52 returns a nonzero value.
56 receives three arguments:
58 which is a pointer to a structure containing information
59 about the shared object;
61 which is the size of the structure pointed to by
65 which is a copy of whatever value was passed by the calling
66 program as the second argument (also named
69 .BR dl_iterate_phdr ().
73 argument is a structure of the following type:
78 ElfW(Addr) dlpi_addr; /* Base address of object */
79 const char *dlpi_name; /* (Null-terminated) name of
81 const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of
84 ElfW(Half) dlpi_phnum; /* # of items in \fIdlpi_phdr\fP */
91 macro definition turns its argument into the name of an ELF data
92 type suitable for the hardware architecture.
93 For example, on a 32-bit platform,
94 ElfW(Addr) yields the data type name Elf32_Addr.
95 Further information on these types can be found in the
96 .IR <elf.h> " and " <link.h>
101 field indicates the base address of the shared object
102 (i.e., the difference between the virtual memory address of
103 the shared object and the offset of that object in the file
104 from which it was loaded).
107 field is a null-terminated string giving the pathname
108 from which the shared object was loaded.
110 To understand the meaning of the
114 fields, we need to be aware that an ELF shared object consists
115 of a number of segments, each of which has a corresponding
116 program header describing the segment.
119 field is a pointer to an array of the program headers for this
123 field indicates the size of this array.
125 These program headers are structures of the following form:
130 Elf32_Word p_type; /* Segment type */
131 Elf32_Off p_offset; /* Segment file offset */
132 Elf32_Addr p_vaddr; /* Segment virtual address */
133 Elf32_Addr p_paddr; /* Segment physical address */
134 Elf32_Word p_filesz; /* Segment size in file */
135 Elf32_Word p_memsz; /* Segment size in memory */
136 Elf32_Word p_flags; /* Segment flags */
137 Elf32_Word p_align; /* Segment alignment */
142 Note that we can calculate the location of a particular program header,
144 in virtual memory using the formula:
147 addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
151 .BR dl_iterate_phdr ()
152 function returns whatever value was returned by the last call to
155 .BR dl_iterate_phdr ()
156 has been supported in glibc since version 2.2.4.
158 For an explanation of the terms used in this section, see
164 Interface Attribute Value
166 .BR dl_iterate_phdr ()
167 T} Thread safety MT-Safe
172 .BR dl_iterate_phdr ()
173 function is Linux-specific and should be avoided in portable applications.
175 The following program displays a list of pathnames of the
176 shared objects it has loaded.
177 For each shared object, the program lists the virtual addresses
178 at which the object's ELF segments are loaded.
187 callback(struct dl_phdr_info *info, size_t size, void *data)
191 printf("name=%s (%d segments)\\n", info\->dlpi_name,
194 for (j = 0; j < info\->dlpi_phnum; j++)
195 printf("\\t\\t header %2d: address=%10p\\n", j,
196 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
201 main(int argc, char *argv[])
203 dl_iterate_phdr(callback, NULL);
216 .IR "Executable and Linking Format Specification" ,
217 available at various locations online.