OSDN Git Service

(split) Convert release and draft pages to UTF-8.
[linuxjm/LDP_man-pages.git] / release / 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 .SH 名前
31 dl_iterate_phdr \- 共有オブジェクトのリストを辿る
32 .SH 書式
33 .nf
34 .BR "#define _GNU_SOURCE" "         /* feature_test_macros(7) 参照 */"
35 .B #include <link.h>
36
37 \fBint dl_iterate_phdr(\fP
38           \fBint (*\fPcallback\fB) \
39 (struct dl_phdr_info *\fPinfo\fB,\fP
40                            \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
41           \fBvoid *\fPdata\fB);\fP
42 .fi
43 .SH 説明
44 .BR dl_iterate_phdr ()
45 関数を使うと、アプリケーションは実行時に
46 どの共有オブジェクトをロードしたかを見つけることができる。
47
48 .BR dl_iterate_phdr ()
49 関数はアプリケーションの共有オブジェクトのリストを辿り、
50 各オブジェクトに対して関数
51 .I callback
52 を 1 回ずつ呼び出す。
53 これは全ての共有オブジェクトが処理されるか、
54 .I callback
55 が 0 以外の値を返すまで行われる。
56
57 各々の
58 .I callback
59 呼び出しは 3 つの引き数を受け取る:
60 .I info
61 は共有オブジェクトの情報を保持する構造体へのポインタである。
62 .I size
63
64 .I info
65 で指される構造体のサイズである。
66 .I data
67 は呼び出し元プログラムから
68 .BR dl_iterate_phdr ()
69 の呼び出しの (同じく
70 .I data
71 という名前の) 第 2 引き数として渡される値のコピーである。
72
73 .I info
74 引き数は、以下のような型の構造体である。
75
76 .in +4n
77 .nf
78 struct dl_phdr_info {
79     ElfW(Addr)        dlpi_addr;  /* オブジェクトのベースアドレス */
80     const char       *dlpi_name;  /* (null 文字で終端された)
81                                      オブジェクト名 */
82     const ElfW(Phdr) *dlpi_phdr;  /* このオブジェクトの
83                                      ELF プログラムヘッダの
84                                      配列へのポインタ */
85     ElfW(Half)        dlpi_phnum; /* \fIdlpi_phdr\fP のアイテム数 */
86 };
87 .fi
88 .in
89
90 .RI ( ElfW ()
91 マクロ定義は引き数をハードウェアアーキテクチャに適した
92 ELF データ型の名前に変換する。
93 たとえば、32 ビットプラットフォームでは
94 ElfW(Addr) はデータ型名 Elf32_Addr を生成する。
95 これらの型についての更に詳細な情報は、ヘッダファイル
96 .IR <elf.h> " と " <link.h>
97 にある。
98
99 .I dlpi_addr
100 フィールドは共有オブジェクトのベースアドレス
101 (つまり、共有オブジェクトの仮想メモリアドレスと、
102 ファイル (このファイルから共有オブジェクトがロードされる) における
103 共有オブジェクトのオフセットとの差分) を表す。
104 .I dlpi_name
105 は null 文字で終端された文字列であり、
106 このパス名のファイルから共有オブジェクトがロードされる。
107
108 .I dlpi_phdr
109
110 .I dlpi_phnum
111 フィールドの意味を理解するには、
112 ELF 共有オブジェクトが幾つかのセグメントから構成されていることと、
113 各セグメントがそれに対応するプログラムヘッダ
114 (そのセグメントを説明する) を持っていることを知っている必要がある。
115 .I dlpi_phdr
116 フィールドは、この共有オブジェクトのプログラムヘッダの配列へのポインタである。
117 .I dlpi_phnum
118 は、この配列のサイズを表す。
119
120 これらのプログラムヘッダは以下のような形式の構造体である:
121 .in +4n
122 .nf
123
124 typedef struct
125 {
126     Elf32_Word  p_type;    /* セグメントの型 */
127     Elf32_Off   p_offset;  /* セグメントのファイルオフセット */
128     Elf32_Addr  p_vaddr;   /* セグメントの仮想アドレス */
129     Elf32_Addr  p_paddr;   /* セグメントの物理アドレス */
130     Elf32_Word  p_filesz;  /* ファイルにおけるセグメントサイズ */
131     Elf32_Word  p_memsz;   /* メモリにおけるセグメントサイズ */
132     Elf32_Word  p_flags;   /* セグメントフラグ */
133     Elf32_Word  p_align;   /* セグメントの配置 (alignment) */
134 } Elf32_Phdr;
135 .fi
136 .in
137
138 特定のプログラムヘッダ
139 .I x
140 の仮想メモリにおける位置は、以下の式で計算できる点に注意すること:
141
142 .nf
143   addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr;
144 .fi
145 .SH 返り値
146 .BR dl_iterate_phdr ()
147 関数は最後の
148 .I callback
149 の呼び出しで返された値を返す。
150 .SH バージョン
151 .BR dl_iterate_phdr ()
152 は glibc のバージョン 2.2.4 以降でサポートされている。
153 .SH 準拠
154 .BR dl_iterate_phdr ()
155 関数は Linux 固有であり、移植を考えたアプリケーションでは避けるべきである。
156 .SH 例
157 以下のプログラムは、共有オブジェクトがロードされた
158 パス名の一覧を表示する。
159 各共有オブジェクトについて、このプログラムは
160 オブジェクトの ELF セグメントがロードされた
161 仮想アドレスの一覧を表示する。
162
163 .nf
164 #define _GNU_SOURCE
165 #include <link.h>
166 #include <stdlib.h>
167 #include <stdio.h>
168
169 static int
170 callback(struct dl_phdr_info *info, size_t size, void *data)
171 {
172     int j;
173
174     printf("name=%s (%d segments)\\n", info\->dlpi_name,
175         info\->dlpi_phnum);
176
177     for (j = 0; j < info\->dlpi_phnum; j++)
178          printf("\\t\\t header %2d: address=%10p\\n", j,
179              (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr));
180     return 0;
181 }
182
183 int
184 main(int argc, char *argv[])
185 {
186     dl_iterate_phdr(callback, NULL);
187
188     exit(EXIT_SUCCESS);
189 }
190 .fi
191 .SH 関連項目
192 .BR ldd (1),
193 .BR objdump (1),
194 .BR readelf (1),
195 .BR dlopen (3),
196 .BR elf (5),
197 .BR ld.so (8),
198 オンラインのいろいろな場所で入手できる
199 .I "Executable and Linking Format Specification"