]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/dl_iterate_phdr.3
s/\\'/\\(aq/
[thirdparty/man-pages.git] / man3 / dl_iterate_phdr.3
1 .\" Copyright (c) 2003 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\"
24 .TH DL_ITERATE_PHDR 3 2007-05-18 "GNU" "Linux Programmer's Manual"
25 .SH NAME
26 dl_iterate_phdr \- walk through list of shared objects
27 .SH SYNOPSIS
28 .nf
29 .B #define _GNU_SOURCE
30 .B #include <link.h>
31
32 \fBint dl_iterate_phdr(\fP
33 \fBint (*\fPcallback\fB) \
34 (struct dl_phdr_info *\fPinfo\fB,\fP
35 \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
36 \fBvoid *\fPdata\fB);\fP
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 non-zero 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 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>
97 header files.)
98
99 The
100 .I dlpi_addr
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).
105 The
106 .I dlpi_name
107 field is a null-terminated string giving the pathname
108 from which the shared object was loaded.
109
110 To understand the meaning of the
111 .I dlpi_phdr
112 and
113 .I dlpi_phnum
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.
117 The
118 .I dlpi_phdr
119 field is a pointer to an array of the program headers for this
120 shared object.
121 The
122 .I dlpi_phnum
123 field indicates the size of this array.
124
125 These program headers are structures of the following form:
126 .in +4n
127 .nf
128
129 typedef struct {
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 */
138 } Elf32_Phdr;
139 .fi
140 .in
141
142 Note that we can calculate the location of a particular program header,
143 .IR x ,
144 in virtual memory using the formula:
145
146 .nf
147 addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
148 .fi
149 .SH RETURN VALUE
150 The
151 .BR dl_iterate_phdr ()
152 function returns whatever value was returned by the last call to
153 .IR callback .
154 .SH VERSIONS
155 .BR dl_iterate_phdr ()
156 has been supported in glibc since version 2.2.4.
157 .SH "CONFORMING TO"
158 The
159 .BR dl_iterate_phdr ()
160 function is Linux-specific and should be avoided in portable applications.
161 .SH EXAMPLE
162 The following program displays a list of pathnames of the
163 shared objects it has loaded.
164 For each shared object, the program lists the virtual addresses
165 at which the object's ELF segments are loaded.
166
167 .nf
168 #define _GNU_SOURCE
169 #include <link.h>
170 #include <stdlib.h>
171 #include <stdio.h>
172
173 static int
174 callback(struct dl_phdr_info *info, size_t size, void *data)
175 {
176 int j;
177
178 printf("name=%s (%d segments)\\n", info\->dlpi_name,
179 info\->dlpi_phnum);
180
181 for (j = 0; j < info\->dlpi_phnum; j++)
182 printf("\\t\\t header %2d: address=%10p\\n", j,
183 (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
184 return 0;
185 }
186
187 int
188 main(int argc, char *argv[])
189 {
190 dl_iterate_phdr(callback, NULL);
191
192 exit(EXIT_SUCCESS);
193 }
194 .fi
195 .SH "SEE ALSO"
196 .BR ldd (1),
197 .BR objdump (1),
198 .BR readelf (1),
199 .BR dlopen (3),
200 .BR feature_test_macros (7),
201 .BR ld.so (8),
202 and the
203 .I "Executable and Linking Format Specification"
204 available at various locations online.