]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/dl_iterate_phdr.3
Formatting fixes.
[thirdparty/man-pages.git] / man3 / dl_iterate_phdr.3
1 .\" Copyright (c) 2003 by Michael Kerrisk <mtk-manpages@gmx.net>
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.
16 .\"
17 .\" Formatted or processed versions of this manual, if unaccompanied by
18 .\" the source, must acknowledge the copyright and authors of this work.
19 .\" License.
20 .\"
21 .TH DL_ITERATE_PHDR 3 "Linux 2.4.21" "Linux Programmer's Manual"
22 .SH NAME
23 dl_iterate_phdr \- walk through list of shared objects
24 .SH SYNOPSIS
25 .nf
26 #define _GNU_SOURCE
27 .B #include <link.h>
28
29 \fBint dl_iterate_phdr(\fP
30 \fBint (*\fPcallback\fB) \
31 (struct dl_phdr_info *\fPinfo\fB,\fP
32 \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
33 \fBvoid *\fPdata\fB);\fP
34 .fi
35 .SH DESCRIPTION
36 The
37 .B dl_iterate_phdr
38 function allows an application to inquire at run-time to find
39 out which shared objects it has loaded.
40
41 The
42 .B dl_iterate_phdr
43 function walks through the list of an
44 application's shared objects and calls the function
45 .I callback
46 once for each object,
47 until either all shared objects have been processed or
48 .I callback
49 returns a non-zero value.
50
51 Each call to
52 .I callback
53 receives three arguments:
54 .IR info ,
55 which is a pointer to a structure containing information
56 about the shared object;
57 .IR size ,
58 which is the size of the structure pointed to by
59 .IR info ;
60 and
61 .IR data ,
62 which is a copy of whatever value was passed by the calling
63 program as the second argument (also named
64 .IR data )
65 in the call to
66 .BR dl_iterate_phdr .
67
68 The
69 .I info
70 argument is a structure of the following type:
71
72 .nf
73 struct dl_phdr_info {
74 ElfW(Addr) dlpi_addr; /* Base address of object */
75 const char *dlpi_name; /* (Null-terminated) name of
76 object
77 const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of
78 ELF program headers
79 for this object */
80 ElfW(Half) dlpi_phnum; /* # of items in 'dlpi_phdr' */
81 };
82 .fi
83
84 (The
85 .I ElfW()
86 macro definition turns its argument into the name of an ELF data
87 type suitable for the hardware architecture.
88 For example, on a 32-bit platform,
89 ElfW(Addr) yields the data type name Elf32_Addr.
90 Further information on these types can be found in the
91 .IR <elf.h> " and " <link.h>
92 header files.)
93
94 The
95 .I dlpi_addr
96 field indicates the base address of the shared object
97 (i.e., the difference between the virtual memory address of
98 the shared object and the offset of that object in the file
99 from which it was loaded).
100 The
101 .I dlpi_name
102 field is a null-terminated string giving the pathname
103 from which the shared object was loaded.
104
105 To understand the meaning of the
106 .I dlpi_phdr
107 and
108 .I dlpi_phnum
109 fields, we need to be aware that an ELF shared object consists
110 of a number of segments, each of which has a corresponding
111 program header describing the segment.
112 The
113 .I dlpi_phdr
114 field is a pointer to an array of the program headers for this
115 shared object.
116 The
117 .I dlpi_phnum
118 field indicates the size of this array.
119
120 These program headers are structures of the following form:
121 .nf
122
123 typedef struct
124 {
125 Elf32_Word p_type; /* Segment type */
126 Elf32_Off p_offset; /* Segment file offset */
127 Elf32_Addr p_vaddr; /* Segment virtual address */
128 Elf32_Addr p_paddr; /* Segment physical address */
129 Elf32_Word p_filesz; /* Segment size in file */
130 Elf32_Word p_memsz; /* Segment size in memory */
131 Elf32_Word p_flags; /* Segment flags */
132 Elf32_Word p_align; /* Segment alignment */
133 } Elf32_Phdr;
134 .fi
135
136 Note that we can calculate the location of a particular program header,
137 .IR x ,
138 in virtual memory using the formula:
139
140 .nf
141 addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
142 .fi
143 .SH EXAMPLE PROGRAM
144 The following program displays a list of pathnames of the
145 shared objects it has loaded.
146 For each shared object, the program lists the virtual addresses
147 at which the object's ELF segments are loaded.
148
149 .nf
150 #define _GNU_SOURCE
151 #include <link.h>
152 #include <stdlib.h>
153 #include <stdio.h>
154
155 static int
156 callback(struct dl_phdr_info *info, size_t size, void *data)
157 {
158 int j;
159
160 printf("name=%s (%d segments)\\n", info->dlpi_name,
161 info->dlpi_phnum);
162
163 for (j = 0; j < info->dlpi_phnum; j++)
164 printf("\\t\\t header %2d: address=%10p\\n", j,
165 (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
166 return 0;
167 }
168
169 int
170 main(int argc, char *argv[])
171 {
172 dl_iterate_phdr(callback, NULL);
173
174 exit(EXIT_SUCCESS);
175 }
176 .fi
177 .SH RETURN VALUE
178 The
179 .B dl_iterate_phdr
180 function returns whatever value was returned by the last call to
181 .IR callback .
182 .SH "CONFORMING TO"
183 The
184 .B dl_iterate_phdr
185 function is Linux specific and should be avoided in portable applications.
186 .SH "SEE ALSO"
187 .BR ldd (1),
188 .BR objdump (1),
189 .BR readelf (1),
190 .BR dlopen (3),
191 .BR ld.so (8),
192 and the
193 .I "Executable and Linking Format Specification"
194 available at various locations online.