OSDN Git Service

(split) LDP v3.30, v3.31 の定型的な変更内容を反映。
[linuxjm/LDP_man-pages.git] / draft / 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 .\" Japanese Version Copyright (c) 2005 Yuichi SATO
25 .\"         all rights reserved.
26 .\" Translated Thu Jan 27 07:03:56 JST 2005
27 .\"         by Yuichi SATO <ysato444@yahoo.co.jp>
28 .\"
29 .TH DL_ITERATE_PHDR 3 2007-05-18 "Linux" "Linux Programmer's Manual"
30 .\"O .SH NAME
31 .SH Ì¾Á°
32 .\"O dl_iterate_phdr \- walk through list of shared objects
33 dl_iterate_phdr \- ¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¥ê¥¹¥È¤òé¤ë
34 .\"O .SH SYNOPSIS
35 .SH ½ñ¼°
36 .nf
37 .\"O .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
38 .BR "#define _GNU_SOURCE" "         /* feature_test_macros(7) »²¾È */"
39 .B #include <link.h>
40
41 \fBint dl_iterate_phdr(\fP
42           \fBint (*\fPcallback\fB) \
43 (struct dl_phdr_info *\fPinfo\fB,\fP
44                            \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
45           \fBvoid *\fPdata\fB);\fP
46 .fi
47 .\"O .SH DESCRIPTION
48 .SH ÀâÌÀ
49 .\"O The
50 .\"O .BR dl_iterate_phdr ()
51 .\"O function allows an application to inquire at run time to find
52 .\"O out which shared objects it has loaded.
53 .BR dl_iterate_phdr ()
54 ´Ø¿ô¤ò»È¤¦¤È¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ï¼Â¹Ô»þ¤Ë
55 ¤É¤Î¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤ò¥í¡¼¥É¤·¤¿¤«¤ò¸«¤Ä¤±¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
56
57 .\"O The
58 .\"O .BR dl_iterate_phdr ()
59 .\"O function walks through the list of an
60 .\"O application's shared objects and calls the function
61 .\"O .I callback
62 .\"O once for each object,
63 .\"O until either all shared objects have been processed or
64 .\"O .I callback
65 .\"O returns a nonzero value.
66 .BR dl_iterate_phdr ()
67 ´Ø¿ô¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Î¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¥ê¥¹¥È¤òé¤ê¡¢
68 ³Æ¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ·¤Æ´Ø¿ô
69 .I callback
70 ¤ò 1 ²ó¤º¤Ä¸Æ¤Ó½Ð¤¹¡£
71 ¤³¤ì¤ÏÁ´¤Æ¤Î¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤¬½èÍý¤µ¤ì¤ë¤«¡¢
72 .I callback
73 ¤¬ 0 °Ê³°¤ÎÃͤòÊÖ¤¹¤Þ¤Ç¹Ô¤ï¤ì¤ë¡£
74
75 .\"O Each call to
76 .\"O .I callback
77 .\"O receives three arguments:
78 ³Æ¡¹¤Î
79 .I callback
80 ¸Æ¤Ó½Ð¤·¤Ï 3 ¤Ä¤Î°ú¤­¿ô¤ò¼õ¤±¼è¤ë:
81 .\"O .IR info ,
82 .\"O which is a pointer to a structure containing information
83 .\"O about the shared object;
84 .I info
85 ¤Ï¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¾ðÊó¤òÊÝ»ý¤¹¤ë¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
86 .\"O .IR size ,
87 .\"O which is the size of the structure pointed to by
88 .\"O .IR info ;
89 .I size
90 ¤Ï
91 .I info
92 ¤Ç»Ø¤µ¤ì¤ë¹½Â¤ÂΤΥµ¥¤¥º¤Ç¤¢¤ë¡£
93 .\"O and
94 .\"O .IR data ,
95 .\"O which is a copy of whatever value was passed by the calling
96 .\"O program as the second argument (also named
97 .\"O .IR data )
98 .\"O in the call to
99 .\"O .BR dl_iterate_phdr ().
100 .I data
101 ¤Ï¸Æ¤Ó½Ð¤·¸µ¥×¥í¥°¥é¥à¤«¤é
102 .BR dl_iterate_phdr ()
103 ¤Î¸Æ¤Ó½Ð¤·¤Î (Ʊ¤¸¤¯
104 .I data
105 ¤È¤¤¤¦Ì¾Á°¤Î) Âè 2 °ú¤­¿ô¤È¤·¤ÆÅϤµ¤ì¤ëÃͤΥ³¥Ô¡¼¤Ç¤¢¤ë¡£
106
107 .\"O The
108 .\"O .I info
109 .\"O argument is a structure of the following type:
110 .I info
111 °ú¤­¿ô¤Ï¡¢°Ê²¼¤Î¤è¤¦¤Ê·¿¤Î¹½Â¤ÂΤǤ¢¤ë¡£
112
113 .in +4n
114 .nf
115 struct dl_phdr_info {
116 .\"O     ElfW(Addr)        dlpi_addr;  /* Base address of object */
117     ElfW(Addr)        dlpi_addr;  /* ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Ù¡¼¥¹¥¢¥É¥ì¥¹ */
118 .\"O     const char       *dlpi_name;  /* (Null-terminated) name of
119 .\"O                                      object */
120     const char       *dlpi_name;  /* (null Ê¸»ú¤Ç½ªÃ¼¤µ¤ì¤¿)
121                                      ¥ª¥Ö¥¸¥§¥¯¥È̾ */
122 .\"O     const ElfW(Phdr) *dlpi_phdr;  /* Pointer to array of
123 .\"O                                      ELF program headers
124 .\"O                                      for this object */
125     const ElfW(Phdr) *dlpi_phdr;  /* ¤³¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Î
126                                      ELF ¥×¥í¥°¥é¥à¥Ø¥Ã¥À¤Î
127                                      ÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿ */
128 .\"O     ElfW(Half)        dlpi_phnum; /* # of items in \fIdlpi_phdr\fP */
129     ElfW(Half)        dlpi_phnum; /* \fIdlpi_phdr\fP ¤Î¥¢¥¤¥Æ¥à¿ô */
130 };
131 .fi
132 .in
133
134 .\"O (The
135 .\"O .IR ElfW ()
136 .\"O macro definition turns its argument into the name of an ELF data
137 .\"O type suitable for the hardware architecture.
138 .RI ( ElfW ()
139 ¥Þ¥¯¥íÄêµÁ¤Ï°ú¤­¿ô¤ò¥Ï¡¼¥É¥¦¥§¥¢¥¢¡¼¥­¥Æ¥¯¥Á¥ã¤ËŬ¤·¤¿
140 ELF ¥Ç¡¼¥¿·¿¤Î̾Á°¤ËÊÑ´¹¤¹¤ë¡£
141 .\"O For example, on a 32-bit platform,
142 .\"O ElfW(Addr) yields the data type name Elf32_Addr.
143 ¤¿¤È¤¨¤Ð¡¢32 ¥Ó¥Ã¥È¥×¥é¥Ã¥È¥Õ¥©¡¼¥à¤Ç¤Ï
144 ElfW(Addr) ¤Ï¥Ç¡¼¥¿·¿Ì¾ Elf32_Addr ¤òÀ¸À®¤¹¤ë¡£
145 .\"O Further information on these types can be found in the
146 .\"O .IR <elf.h> " and " <link.h>
147 .\"O header files.)
148 ¤³¤ì¤é¤Î·¿¤Ë¤Ä¤¤¤Æ¤Î¹¹¤Ë¾ÜºÙ¤Ê¾ðÊó¤Ï¡¢¥Ø¥Ã¥À¥Õ¥¡¥¤¥ë
149 .IR <elf.h> " ¤È " <link.h>
150 ¤Ë¤¢¤ë¡£
151
152 .\"O The
153 .\"O .I dlpi_addr
154 .\"O field indicates the base address of the shared object
155 .\"O (i.e., the difference between the virtual memory address of
156 .\"O the shared object and the offset of that object in the file
157 .\"O from which it was loaded).
158 .I dlpi_addr
159 ¥Õ¥£¡¼¥ë¥É¤Ï¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¥Ù¡¼¥¹¥¢¥É¥ì¥¹
160 (¤Ä¤Þ¤ê¡¢¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î²¾ÁÛ¥á¥â¥ê¥¢¥É¥ì¥¹¤È¡¢
161 ¥Õ¥¡¥¤¥ë (¤³¤Î¥Õ¥¡¥¤¥ë¤«¤é¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤¬¥í¡¼¥É¤µ¤ì¤ë) ¤Ë¤ª¤±¤ë
162 ¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¥ª¥Õ¥»¥Ã¥È¤È¤Îº¹Ê¬) ¤òɽ¤¹¡£
163 .\"O The
164 .\"O .I dlpi_name
165 .\"O field is a null-terminated string giving the pathname
166 .\"O from which the shared object was loaded.
167 .I dlpi_name
168 ¤Ï null Ê¸»ú¤Ç½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ç¤¢¤ê¡¢
169 ¤³¤Î¥Ñ¥¹Ì¾¤Î¥Õ¥¡¥¤¥ë¤«¤é¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤¬¥í¡¼¥É¤µ¤ì¤ë¡£
170
171 .\"O To understand the meaning of the
172 .\"O .I dlpi_phdr
173 .\"O and
174 .\"O .I dlpi_phnum
175 .\"O fields, we need to be aware that an ELF shared object consists
176 .\"O of a number of segments, each of which has a corresponding
177 .\"O program header describing the segment.
178 .I dlpi_phdr
179 ¤È
180 .I dlpi_phnum
181 ¥Õ¥£¡¼¥ë¥É¤Î°ÕÌ£¤òÍý²ò¤¹¤ë¤Ë¤Ï¡¢
182 ELF ¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤¬´ö¤Ä¤«¤Î¥»¥°¥á¥ó¥È¤«¤é¹½À®¤µ¤ì¤Æ¤¤¤ë¤³¤È¤È¡¢
183 ³Æ¥»¥°¥á¥ó¥È¤¬¤½¤ì¤ËÂбþ¤¹¤ë¥×¥í¥°¥é¥à¥Ø¥Ã¥À
184 (¤½¤Î¥»¥°¥á¥ó¥È¤òÀâÌÀ¤¹¤ë) ¤ò»ý¤Ã¤Æ¤¤¤ë¤³¤È¤òÃΤäƤ¤¤ëɬÍפ¬¤¢¤ë¡£
185 .\"O The
186 .\"O .I dlpi_phdr
187 .\"O field is a pointer to an array of the program headers for this
188 .\"O shared object.
189 .I dlpi_phdr
190 ¥Õ¥£¡¼¥ë¥É¤Ï¡¢¤³¤Î¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Î¥×¥í¥°¥é¥à¥Ø¥Ã¥À¤ÎÇÛÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤Ç¤¢¤ë¡£
191 .\"O The
192 .\"O .I dlpi_phnum
193 .\"O field indicates the size of this array.
194 .I dlpi_phnum
195 ¤Ï¡¢¤³¤ÎÇÛÎó¤Î¥µ¥¤¥º¤òɽ¤¹¡£
196
197 .\"O These program headers are structures of the following form:
198 ¤³¤ì¤é¤Î¥×¥í¥°¥é¥à¥Ø¥Ã¥À¤Ï°Ê²¼¤Î¤è¤¦¤Ê·Á¼°¤Î¹½Â¤ÂΤǤ¢¤ë:
199 .in +4n
200 .nf
201
202 typedef struct
203 {
204 .\"O     Elf32_Word  p_type;    /* Segment type */
205     Elf32_Word  p_type;    /* ¥»¥°¥á¥ó¥È¤Î·¿ */
206 .\"O     Elf32_Off   p_offset;  /* Segment file offset */
207     Elf32_Off   p_offset;  /* ¥»¥°¥á¥ó¥È¤Î¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È */
208 .\"O     Elf32_Addr  p_vaddr;   /* Segment virtual address */
209     Elf32_Addr  p_vaddr;   /* ¥»¥°¥á¥ó¥È¤Î²¾ÁÛ¥¢¥É¥ì¥¹ */
210 .\"O     Elf32_Addr  p_paddr;   /* Segment physical address */
211     Elf32_Addr  p_paddr;   /* ¥»¥°¥á¥ó¥È¤ÎʪÍý¥¢¥É¥ì¥¹ */
212 .\"O     Elf32_Word  p_filesz;  /* Segment size in file */
213     Elf32_Word  p_filesz;  /* ¥Õ¥¡¥¤¥ë¤Ë¤ª¤±¤ë¥»¥°¥á¥ó¥È¥µ¥¤¥º */
214 .\"O     Elf32_Word  p_memsz;   /* Segment size in memory */
215     Elf32_Word  p_memsz;   /* ¥á¥â¥ê¤Ë¤ª¤±¤ë¥»¥°¥á¥ó¥È¥µ¥¤¥º */
216 .\"O     Elf32_Word  p_flags;   /* Segment flags */
217     Elf32_Word  p_flags;   /* ¥»¥°¥á¥ó¥È¥Õ¥é¥° */
218 .\"O     Elf32_Word  p_align;   /* Segment alignment */
219     Elf32_Word  p_align;   /* ¥»¥°¥á¥ó¥È¤ÎÇÛÃÖ (alignment) */
220 } Elf32_Phdr;
221 .fi
222 .in
223
224 .\"O Note that we can calculate the location of a particular program header,
225 .\"O .IR x ,
226 .\"O in virtual memory using the formula:
227 ÆÃÄê¤Î¥×¥í¥°¥é¥à¥Ø¥Ã¥À
228 .I x
229 ¤Î²¾ÁÛ¥á¥â¥ê¤Ë¤ª¤±¤ë°ÌÃ֤ϡ¢°Ê²¼¤Î¼°¤Ç·×»»¤Ç¤­¤ëÅÀ¤ËÃí°Õ¤¹¤ë¤³¤È:
230
231 .nf
232   addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
233 .fi
234 .\"O .SH RETURN VALUE
235 .SH ÊÖ¤êÃÍ
236 .\"O The
237 .\"O .BR dl_iterate_phdr ()
238 .\"O function returns whatever value was returned by the last call to
239 .\"O .IR callback .
240 .BR dl_iterate_phdr ()
241 ´Ø¿ô¤ÏºÇ¸å¤Î
242 .I callback
243 ¤Î¸Æ¤Ó½Ð¤·¤ÇÊÖ¤µ¤ì¤¿ÃͤòÊÖ¤¹¡£
244 .\"O .SH VERSIONS
245 .SH ¥Ð¡¼¥¸¥ç¥ó
246 .\"O .BR dl_iterate_phdr ()
247 .\"O has been supported in glibc since version 2.2.4.
248 .BR dl_iterate_phdr ()
249 ¤Ï glibc ¤Î¥Ð¡¼¥¸¥ç¥ó 2.2.4 °Ê¹ß¤Ç¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¡£
250 .\"O .SH "CONFORMING TO"
251 .SH ½àµò
252 .\"O The
253 .\"O .BR dl_iterate_phdr ()
254 .\"O function is Linux-specific and should be avoided in portable applications.
255 .BR dl_iterate_phdr ()
256 ´Ø¿ô¤Ï Linux ¸ÇÍ­¤Ç¤¢¤ê¡¢°Ü¿¢¤ò¹Í¤¨¤¿¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ç¤ÏÈò¤±¤ë¤Ù¤­¤Ç¤¢¤ë¡£
257 .\"O .SH EXAMPLE
258 .SH Îã
259 .\"O The following program displays a list of pathnames of the
260 .\"O shared objects it has loaded.
261 °Ê²¼¤Î¥×¥í¥°¥é¥à¤Ï¡¢¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤¬¥í¡¼¥É¤µ¤ì¤¿
262 ¥Ñ¥¹Ì¾¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë¡£
263 .\"O For each shared object, the program lists the virtual addresses
264 .\"O at which the object's ELF segments are loaded.
265 ³Æ¶¦Í­¥ª¥Ö¥¸¥§¥¯¥È¤Ë¤Ä¤¤¤Æ¡¢¤³¤Î¥×¥í¥°¥é¥à¤Ï
266 ¥ª¥Ö¥¸¥§¥¯¥È¤Î ELF ¥»¥°¥á¥ó¥È¤¬¥í¡¼¥É¤µ¤ì¤¿
267 ²¾ÁÛ¥¢¥É¥ì¥¹¤Î°ìÍ÷¤òɽ¼¨¤¹¤ë¡£
268
269 .nf
270 #define _GNU_SOURCE
271 #include <link.h>
272 #include <stdlib.h>
273 #include <stdio.h>
274
275 static int
276 callback(struct dl_phdr_info *info, size_t size, void *data)
277 {
278     int j;
279
280     printf("name=%s (%d segments)\\n", info\->dlpi_name,
281         info\->dlpi_phnum);
282
283     for (j = 0; j < info\->dlpi_phnum; j++)
284          printf("\\t\\t header %2d: address=%10p\\n", j,
285              (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
286     return 0;
287 }
288
289 int
290 main(int argc, char *argv[])
291 {
292     dl_iterate_phdr(callback, NULL);
293
294     exit(EXIT_SUCCESS);
295 }
296 .fi
297 .\"O .SH "SEE ALSO"
298 .SH ´ØÏ¢¹àÌÜ
299 .BR ldd (1),
300 .BR objdump (1),
301 .BR readelf (1),
302 .BR dlopen (3),
303 .BR elf (5),
304 .BR ld.so (8),
305 .\"O and the
306 .\"O .I "Executable and Linking Format Specification"
307 .\"O available at various locations online.
308 ¥ª¥ó¥é¥¤¥ó¤Î¤¤¤í¤¤¤í¤Ê¾ì½ê¤ÇÆþ¼ê¤Ç¤­¤ë
309 .I "Executable and Linking Format Specification"