1 .\" Copyright (c) 2003, 2017 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 2016-03-15 "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 */
86 /* The following fields were added in glibc 2.4, after the first
87 version of this structure was available. Check the \fIsize\fP
88 argument passed to the dl_iterate_phdr callback to determine
89 whether or not each later member is available. */
91 unsigned long long int dlpi_adds;
92 /* Incremented when a new object may
94 unsigned long long int dlpi_subs;
95 /* Incremented when an object may
97 size_t dlpi_tls_modid;
98 /* If there is a PT_TLS segment, its module
99 ID as used in TLS relocations, else zero */
101 /* The address of the calling thread's instance
102 of this module's PT_TLS segment, if it has
103 one and it has been allocated in the calling
104 thread, otherwise a null pointer */
111 macro definition turns its argument into the name of an ELF data
112 type suitable for the hardware architecture.
113 For example, on a 32-bit platform,
115 yields the data type name
117 Further information on these types can be found in the
118 .IR <elf.h> " and " <link.h>
123 field indicates the base address of the shared object
124 (i.e., the difference between the virtual memory address of
125 the shared object and the offset of that object in the file
126 from which it was loaded).
129 field is a null-terminated string giving the pathname
130 from which the shared object was loaded.
132 To understand the meaning of the
136 fields, we need to be aware that an ELF shared object consists
137 of a number of segments, each of which has a corresponding
138 program header describing the segment.
141 field is a pointer to an array of the program headers for this
145 field indicates the size of this array.
147 These program headers are structures of the following form:
152 Elf32_Word p_type; /* Segment type */
153 Elf32_Off p_offset; /* Segment file offset */
154 Elf32_Addr p_vaddr; /* Segment virtual address */
155 Elf32_Addr p_paddr; /* Segment physical address */
156 Elf32_Word p_filesz; /* Segment size in file */
157 Elf32_Word p_memsz; /* Segment size in memory */
158 Elf32_Word p_flags; /* Segment flags */
159 Elf32_Word p_align; /* Segment alignment */
164 Note that we can calculate the location of a particular program header,
166 in virtual memory using the formula:
169 addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
174 include the following (see
176 for further details):
180 #define PT_LOAD 1 /* Loadable program segment */
181 #define PT_DYNAMIC 2 /* Dynamic linking information */
182 #define PT_INTERP 3 /* Program interpreter */
183 #define PT_NOTE 4 /* Auxiliary information */
184 #define PT_SHLIB 5 /* Reserved */
185 #define PT_PHDR 6 /* Entry for header table itself */
186 #define PT_TLS 7 /* Thread-local storage segment */
187 #define PT_GNU_EH_FRAME 0x6474e550 /* GCC .eh_frame_hdr segment */
188 #define PT_GNU_STACK 0x6474e551 /* Indicates stack executability */
189 .\" For PT_GNU_STACK, see http://www.airs.com/blog/archives/518
190 #define PT_GNU_RELRO 0x6474e552 /* Read-only after relocation */
195 .BR dl_iterate_phdr ()
196 function returns whatever value was returned by the last call to
199 .BR dl_iterate_phdr ()
200 has been supported in glibc since version 2.2.4.
202 For an explanation of the terms used in this section, see
208 Interface Attribute Value
210 .BR dl_iterate_phdr ()
211 T} Thread safety MT-Safe
216 .BR dl_iterate_phdr ()
217 function is not specified in any standard.
218 Various other systems provide a version of this function,
219 although details of the returned
222 On the BSDs and Solaris, the structure includes the fields
228 in addition to other implementation-specific fields.
230 Future versions of the C library may add further fields to the
232 structure; in that event, the
234 argument provides a mechanism for the callback function to discover
235 whether it is running on a system with added fields.
237 The first object visited by
240 For the main program, the
242 field will be an empty string.
244 The following program displays a list of pathnames of the
245 shared objects it has loaded.
246 For each shared object, the program lists some information
247 (virtual address, size, flags, and type)
248 for each of the objects ELF segments.
250 The following shell session demonstrates the output
251 produced by the program on an x86-64 system.
252 The first shared object for which output is displayed
253 (where the name is an empty string)
258 Name: "" (9 segments)
259 0: [ 0x400040; memsz: 1f8] flags: 0x5; PT_PHDR
260 1: [ 0x400238; memsz: 1c] flags: 0x4; PT_INTERP
261 2: [ 0x400000; memsz: ac4] flags: 0x5; PT_LOAD
262 3: [ 0x600e10; memsz: 240] flags: 0x6; PT_LOAD
263 4: [ 0x600e28; memsz: 1d0] flags: 0x6; PT_DYNAMIC
264 5: [ 0x400254; memsz: 44] flags: 0x4; PT_NOTE
265 6: [ 0x400970; memsz: 3c] flags: 0x4; PT_GNU_EH_FRAME
266 7: [ (nil); memsz: 0] flags: 0x6; PT_GNU_STACK
267 8: [ 0x600e10; memsz: 1f0] flags: 0x4; PT_GNU_RELRO
268 Name: "linux-vdso.so.1" (4 segments)
269 0: [0x7ffc6edd1000; memsz: e89] flags: 0x5; PT_LOAD
270 1: [0x7ffc6edd1360; memsz: 110] flags: 0x4; PT_DYNAMIC
271 2: [0x7ffc6edd17b0; memsz: 3c] flags: 0x4; PT_NOTE
272 3: [0x7ffc6edd17ec; memsz: 3c] flags: 0x4; PT_GNU_EH_FRAME
273 Name: "/lib64/libc.so.6" (10 segments)
274 0: [0x7f55712ce040; memsz: 230] flags: 0x5; PT_PHDR
275 1: [0x7f557145b980; memsz: 1c] flags: 0x4; PT_INTERP
276 2: [0x7f55712ce000; memsz: 1b6a5c] flags: 0x5; PT_LOAD
277 3: [0x7f55716857a0; memsz: 9240] flags: 0x6; PT_LOAD
278 4: [0x7f5571688b80; memsz: 1f0] flags: 0x6; PT_DYNAMIC
279 5: [0x7f55712ce270; memsz: 44] flags: 0x4; PT_NOTE
280 6: [0x7f55716857a0; memsz: 78] flags: 0x4; PT_TLS
281 7: [0x7f557145b99c; memsz: 544c] flags: 0x4; PT_GNU_EH_FRAME
282 8: [0x7f55712ce000; memsz: 0] flags: 0x6; PT_GNU_STACK
283 9: [0x7f55716857a0; memsz: 3860] flags: 0x4; PT_GNU_RELRO
284 Name: "/lib64/ld-linux-x86-64.so.2" (7 segments)
285 0: [0x7f557168f000; memsz: 20828] flags: 0x5; PT_LOAD
286 1: [0x7f55718afba0; memsz: 15a8] flags: 0x6; PT_LOAD
287 2: [0x7f55718afe10; memsz: 190] flags: 0x6; PT_DYNAMIC
288 3: [0x7f557168f1c8; memsz: 24] flags: 0x4; PT_NOTE
289 4: [0x7f55716acec4; memsz: 604] flags: 0x4; PT_GNU_EH_FRAME
290 5: [0x7f557168f000; memsz: 0] flags: 0x6; PT_GNU_STACK
291 6: [0x7f55718afba0; memsz: 460] flags: 0x4; PT_GNU_RELRO
304 callback(struct dl_phdr_info *info, size_t size, void *data)
309 printf("Name: \\"%s\\" (%d segments)\\n", info\->dlpi_name,
312 for (j = 0; j < info\->dlpi_phnum; j++) {
313 p_type = info\->dlpi_phdr[j].p_type;
314 type = (p_type == PT_LOAD) ? "PT_LOAD" :
315 (p_type == PT_DYNAMIC) ? "PT_DYNAMIC" :
316 (p_type == PT_INTERP) ? "PT_INTERP" :
317 (p_type == PT_NOTE) ? "PT_NOTE" :
318 (p_type == PT_INTERP) ? "PT_INTERP" :
319 (p_type == PT_PHDR) ? "PT_PHDR" :
320 (p_type == PT_TLS) ? "PT_TLS" :
321 (p_type == PT_GNU_EH_FRAME) ? "PT_GNU_EH_FRAME" :
322 (p_type == PT_GNU_STACK) ? "PT_GNU_STACK" :
323 (p_type == PT_GNU_RELRO) ? "PT_GNU_RELRO" : NULL;
325 printf(" %2d: [%14p; memsz:%7lx] flags: 0x%x; ", j,
326 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr),
327 info\->dlpi_phdr[j].p_memsz,
328 info\->dlpi_phdr[j].p_flags);
330 printf("%s\\n", type);
332 printf("[other (0x%x)]\\n", p_type);
339 main(int argc, char *argv[])
341 dl_iterate_phdr(callback, NULL);
355 .IR "Executable and Linking Format Specification" ,
356 available at various locations online.