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 \fBint dl_iterate_phdr(\fP
34 \fBint (*\fPcallback\fB) \
35 (struct dl_phdr_info *\fPinfo\fB,\fP
36 \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
37 \fBvoid *\fPdata\fB);\fP
41 .BR dl_iterate_phdr ()
42 function allows an application to inquire at run time to find
43 out which shared objects it has loaded.
46 .BR dl_iterate_phdr ()
47 function walks through the list of an
48 application's shared objects and calls the function
51 until either all shared objects have been processed or
53 returns a nonzero value.
57 receives three arguments:
59 which is a pointer to a structure containing information
60 about the shared object;
62 which is the size of the structure pointed to by
66 which is a copy of whatever value was passed by the calling
67 program as the second argument (also named
70 .BR dl_iterate_phdr ().
74 argument is a structure of the following type:
79 ElfW(Addr) dlpi_addr; /* Base address of object */
80 const char *dlpi_name; /* (Null-terminated) name of
82 const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of
85 ElfW(Half) dlpi_phnum; /* # of items in \fIdlpi_phdr\fP */
92 macro definition turns its argument into the name of an ELF data
93 type suitable for the hardware architecture.
94 For example, on a 32-bit platform,
95 ElfW(Addr) yields the data type name Elf32_Addr.
96 Further information on these types can be found in the
97 .IR <elf.h> " and " <link.h>
102 field indicates the base address of the shared object
103 (i.e., the difference between the virtual memory address of
104 the shared object and the offset of that object in the file
105 from which it was loaded).
108 field is a null-terminated string giving the pathname
109 from which the shared object was loaded.
111 To understand the meaning of the
115 fields, we need to be aware that an ELF shared object consists
116 of a number of segments, each of which has a corresponding
117 program header describing the segment.
120 field is a pointer to an array of the program headers for this
124 field indicates the size of this array.
126 These program headers are structures of the following form:
131 Elf32_Word p_type; /* Segment type */
132 Elf32_Off p_offset; /* Segment file offset */
133 Elf32_Addr p_vaddr; /* Segment virtual address */
134 Elf32_Addr p_paddr; /* Segment physical address */
135 Elf32_Word p_filesz; /* Segment size in file */
136 Elf32_Word p_memsz; /* Segment size in memory */
137 Elf32_Word p_flags; /* Segment flags */
138 Elf32_Word p_align; /* Segment alignment */
143 Note that we can calculate the location of a particular program header,
145 in virtual memory using the formula:
148 addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
152 .BR dl_iterate_phdr ()
153 function returns whatever value was returned by the last call to
156 .BR dl_iterate_phdr ()
157 has been supported in glibc since version 2.2.4.
160 .BR dl_iterate_phdr ()
161 function is Linux-specific and should be avoided in portable applications.
163 The following program displays a list of pathnames of the
164 shared objects it has loaded.
165 For each shared object, the program lists the virtual addresses
166 at which the object's ELF segments are loaded.
175 callback(struct dl_phdr_info *info, size_t size, void *data)
179 printf("name=%s (%d segments)\\n", info\->dlpi_name,
182 for (j = 0; j < info\->dlpi_phnum; j++)
183 printf("\\t\\t header %2d: address=%10p\\n", j,
184 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
189 main(int argc, char *argv[])
191 dl_iterate_phdr(callback, NULL);
204 .IR "Executable and Linking Format Specification" ,
205 available at various locations online.