]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/dl_iterate_phdr.3
dl_iterate_phdr.3: SEE ALSO: add dladdr(3)
[thirdparty/man-pages.git] / man3 / dl_iterate_phdr.3
1 .\" Copyright (c) 2003 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH DL_ITERATE_PHDR 3 2015-07-23 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 dl_iterate_phdr \- walk through list of shared objects
28 .SH SYNOPSIS
29 .nf
30 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
31 .B #include <link.h>
32
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 ");"
37 .fi
38 .SH DESCRIPTION
39 The
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.
43
44 The
45 .BR dl_iterate_phdr ()
46 function walks through the list of an
47 application's shared objects and calls the function
48 .I callback
49 once for each object,
50 until either all shared objects have been processed or
51 .I callback
52 returns a nonzero value.
53
54 Each call to
55 .I callback
56 receives three arguments:
57 .IR info ,
58 which is a pointer to a structure containing information
59 about the shared object;
60 .IR size ,
61 which is the size of the structure pointed to by
62 .IR info ;
63 and
64 .IR data ,
65 which is a copy of whatever value was passed by the calling
66 program as the second argument (also named
67 .IR data )
68 in the call to
69 .BR dl_iterate_phdr ().
70
71 The
72 .I info
73 argument is a structure of the following type:
74
75 .in +4n
76 .nf
77 struct dl_phdr_info {
78 ElfW(Addr) dlpi_addr; /* Base address of object */
79 const char *dlpi_name; /* (Null-terminated) name of
80 object */
81 const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of
82 ELF program headers
83 for this object */
84 ElfW(Half) dlpi_phnum; /* # of items in \fIdlpi_phdr\fP */
85 };
86 .fi
87 .in
88
89 (The
90 .IR ElfW ()
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 .I ElfW(Addr)
95 yields the data type name
96 .IR Elf32_Addr .
97 Further information on these types can be found in the
98 .IR <elf.h> " and " <link.h>
99 header files.)
100
101 The
102 .I dlpi_addr
103 field indicates the base address of the shared object
104 (i.e., the difference between the virtual memory address of
105 the shared object and the offset of that object in the file
106 from which it was loaded).
107 The
108 .I dlpi_name
109 field is a null-terminated string giving the pathname
110 from which the shared object was loaded.
111
112 To understand the meaning of the
113 .I dlpi_phdr
114 and
115 .I dlpi_phnum
116 fields, we need to be aware that an ELF shared object consists
117 of a number of segments, each of which has a corresponding
118 program header describing the segment.
119 The
120 .I dlpi_phdr
121 field is a pointer to an array of the program headers for this
122 shared object.
123 The
124 .I dlpi_phnum
125 field indicates the size of this array.
126
127 These program headers are structures of the following form:
128 .in +4n
129 .nf
130
131 typedef struct {
132 Elf32_Word p_type; /* Segment type */
133 Elf32_Off p_offset; /* Segment file offset */
134 Elf32_Addr p_vaddr; /* Segment virtual address */
135 Elf32_Addr p_paddr; /* Segment physical address */
136 Elf32_Word p_filesz; /* Segment size in file */
137 Elf32_Word p_memsz; /* Segment size in memory */
138 Elf32_Word p_flags; /* Segment flags */
139 Elf32_Word p_align; /* Segment alignment */
140 } Elf32_Phdr;
141 .fi
142 .in
143
144 Note that we can calculate the location of a particular program header,
145 .IR x ,
146 in virtual memory using the formula:
147
148 .nf
149 addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
150 .fi
151 .SH RETURN VALUE
152 The
153 .BR dl_iterate_phdr ()
154 function returns whatever value was returned by the last call to
155 .IR callback .
156 .SH VERSIONS
157 .BR dl_iterate_phdr ()
158 has been supported in glibc since version 2.2.4.
159 .SH ATTRIBUTES
160 For an explanation of the terms used in this section, see
161 .BR attributes (7).
162 .TS
163 allbox;
164 lb lb lb
165 l l l.
166 Interface Attribute Value
167 T{
168 .BR dl_iterate_phdr ()
169 T} Thread safety MT-Safe
170 .TE
171
172 .SH CONFORMING TO
173 The
174 .BR dl_iterate_phdr ()
175 function is not specified in any standard.
176 Various other systems provide a version of this function,
177 although details of the returned
178 .I dl_phdr_info
179 structure differ.
180 On the BSDs and Solaris, the structure includes the fields
181 .IR dlpi_addr ,
182 .IR dlpi_name ,
183 .IR dlpi_phdr ,
184 and
185 .IR dlpi_phnum
186 in addition to other implementation-specific fields.
187 .SH NOTES
188 Future versions of the C library may add further fields to the
189 .IR dl_phdr_info
190 structure; in that event, the
191 .I size
192 argument provides a mechanism for the callback function to discover
193 whether it is running on a system with added fields.
194 .SH EXAMPLE
195 The following program displays a list of pathnames of the
196 shared objects it has loaded.
197 For each shared object, the program lists the virtual addresses
198 at which the object's ELF segments are loaded.
199
200 .nf
201 #define _GNU_SOURCE
202 #include <link.h>
203 #include <stdlib.h>
204 #include <stdio.h>
205
206 static int
207 callback(struct dl_phdr_info *info, size_t size, void *data)
208 {
209 int j;
210
211 printf("name=%s (%d segments)\\n", info\->dlpi_name,
212 info\->dlpi_phnum);
213
214 for (j = 0; j < info\->dlpi_phnum; j++)
215 printf("\\t\\t header %2d: address=%10p\\n", j,
216 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
217 return 0;
218 }
219
220 int
221 main(int argc, char *argv[])
222 {
223 dl_iterate_phdr(callback, NULL);
224
225 exit(EXIT_SUCCESS);
226 }
227 .fi
228 .SH SEE ALSO
229 .BR ldd (1),
230 .BR objdump (1),
231 .BR readelf (1),
232 .BR dladdr (3),
233 .BR dlopen (3),
234 .BR elf (5),
235 .BR ld.so (8)
236
237 .IR "Executable and Linking Format Specification" ,
238 available at various locations online.