.\" Copyright (c) 2003 by Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2005 Yuichi SATO .\" all rights reserved. .\" Translated Thu Jan 27 07:03:56 JST 2005 .\" by Yuichi SATO .\" .TH DL_ITERATE_PHDR 3 2007\-05\-18 GNU "Linux Programmer's Manual" .SH 名前 dl_iterate_phdr \- 共有オブジェクトのリストを辿る .SH 書式 .nf \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP \fBint dl_iterate_phdr(\fP \fB int (*\fP\fIcallback\fP\fB) (struct dl_phdr_info *\fP\fIinfo\fP\fB,\fP \fB size_t \fP\fIsize\fP\fB, void *\fP\fIdata\fP\fB),\fP \fB void *\fP\fIdata\fP\fB);\fP .fi .SH 説明 \fBdl_iterate_phdr\fP() 関数を使うと、アプリケーションは実行時に どの共有オブジェクトをロードしたかを見つけることができる。 \fBdl_iterate_phdr\fP() 関数はアプリケーションの共有オブジェクトのリストを辿り、 各オブジェクトに対して関数 \fIcallback\fP を 1 回ずつ呼び出す。 これは全ての共有オブジェクトが処理されるか、 \fIcallback\fP が 0 以外の値を返すまで行われる。 各々の \fIcallback\fP 呼び出しは 3 つの引き数を受け取る: \fIinfo\fP は共有オブジェクトの情報を保持する構造体へのポインターである。 \fIsize\fP は \fIinfo\fP で指される構造体のサイズである。 \fIdata\fP は呼び出し元プログラムから \fBdl_iterate_phdr\fP() の呼び出しの (同じく \fIdata\fP という名前の) 第 2 引き数として渡される値のコピーである。 \fIinfo\fP 引き数は、以下のような型の構造体である。 .in +4n .nf struct dl_phdr_info { ElfW(Addr) dlpi_addr; /* オブジェクトのベースアドレス */ const char *dlpi_name; /* (ヌル文字で終端された) オブジェクト名 */ const ElfW(Phdr) *dlpi_phdr; /* このオブジェクトの ELF プログラムヘッダーの 配列へのポインター */ ElfW(Half) dlpi_phnum; /* \fIdlpi_phdr\fP のアイテム数 */ }; .fi .in (\fIElfW\fP() マクロ定義は引き数をハードウェアアーキテクチャーに適した ELF データ型の名前に変換する。 たとえば、32 ビットプラットフォームでは ElfW(Addr) はデータ型名 Elf32_Addr を生成する。 これらの型についての更に詳細な情報は、ヘッダーファイル \fI\fP と \fI\fP にある。 \fIdlpi_addr\fP フィールドは共有オブジェクトのベースアドレス (つまり、共有オブジェクトの仮想メモリーアドレスと、 ファイル (このファイルから共有オブジェクトがロードされる) における 共有オブジェクトのオフセットとの差分) を表す。 \fIdlpi_name\fP はヌル文字で終端された文字列であり、 このパス名のファイルから共有オブジェクトがロードされる。 \fIdlpi_phdr\fP と \fIdlpi_phnum\fP フィールドの意味を理解するには、 ELF 共有オブジェクトが幾つかのセグメントから構成されていることと、 各セグメントがそれに対応するプログラムヘッダー (そのセグメントを説明する) を持っていることを知っている必要がある。 \fIdlpi_phdr\fP フィールドは、この共有オブジェクトのプログラムヘッダーの配列へのポインターである。 \fIdlpi_phnum\fP は、この配列のサイズを表す。 これらのプログラムヘッダーは以下のような形式の構造体である: .in +4n .nf typedef struct { Elf32_Word p_type; /* セグメントの型 */ Elf32_Off p_offset; /* セグメントのファイルオフセット */ Elf32_Addr p_vaddr; /* セグメントの仮想アドレス */ Elf32_Addr p_paddr; /* セグメントの物理アドレス */ Elf32_Word p_filesz; /* ファイルにおけるセグメントサイズ */ Elf32_Word p_memsz; /* メモリーにおけるセグメントサイズ */ Elf32_Word p_flags; /* セグメントフラグ */ Elf32_Word p_align; /* セグメントの配置 (alignment) */ } Elf32_Phdr; .fi .in 特定のプログラムヘッダー \fIx\fP の仮想メモリーにおける位置は、以下の式で計算できる点に注意すること: .nf addr == info\->dlpi_addr + info\->dlpi_phdr[x].p_vaddr; .fi .SH 返り値 \fBdl_iterate_phdr\fP() 関数は最後の \fIcallback\fP の呼び出しで返された値を返す。 .SH バージョン \fBdl_iterate_phdr\fP() は glibc のバージョン 2.2.4 以降でサポートされている。 .SH 準拠 \fBdl_iterate_phdr\fP() 関数は Linux 固有であり、移植を考えたアプリケーションでは避けるべきである。 .SH 例 以下のプログラムは、共有オブジェクトがロードされた パス名の一覧を表示する。 各共有オブジェクトについて、このプログラムは オブジェクトの ELF セグメントがロードされた 仮想アドレスの一覧を表示する。 .nf #define _GNU_SOURCE #include #include #include static int callback(struct dl_phdr_info *info, size_t size, void *data) { int j; printf("name=%s (%d segments)\en", info\->dlpi_name, info\->dlpi_phnum); for (j = 0; j < info\->dlpi_phnum; j++) printf("\et\et header %2d: address=%10p\en", j, (void *) (info\->dlpi_addr + info\->dlpi_phdr[j].p_vaddr)); return 0; } int main(int argc, char *argv[]) { dl_iterate_phdr(callback, NULL); exit(EXIT_SUCCESS); } .fi .SH 関連項目 \fBldd\fP(1), \fBobjdump\fP(1), \fBreadelf\fP(1), \fBdlopen\fP(3), \fBelf\fP(5), \fBld.so\fP(8) オンラインのいろいろな場所で入手できる \fIExecutable and Linking Format Specification\fP .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。