dl_iterate_phdrのヘルプ・マニュアル
日本語 英語
dl_iterate_phdr --help
man dl_iterate_phdr
DL_ITERATE_PHDR(3) Linux Programmer’s Manual DL_ITERATE_PHDR(3)
名前
dl_iterate_phdr - 共有オブジェクトのリストを辿る
書式
#define _GNU_SOURCE
#include
int dl_iterate_phdr(
int (*callback) (struct dl_phdr_info *info,
size_t size, void *data),
void *data);
説明
dl_iterate_phdr() 関数を使うと、アプリケーションは実行時にどの共有オブ
ジェクトをロードしたかを見つけることができる。
dl_iterate_phdr() 関数はアプリケーションの共有オブジェクトのリストを 辿
り、各オブジェクトに対して関数 callback を 1 回ずつ呼び出す。これは全て
の共有オブジェクトが処理されるか、 callback が 0 以外の値を返すまで行わ
れる。
各 々の callback 呼び出しは 3 つの引き数を受け取る: info は共有オブジェ
クトの情報を保持する構造体へのポインタである。 size は info で指され る
構 造 体 の サ イ ズ で ある。 data は呼び出し元プログラムから dl_iter-
ate_phdr() の呼び出しの (同じく data という名前の) 第 2 引き数として 渡
される値のコピーである。
info 引き数は、以下のような型の構造体である。
struct dl_phdr_info {
ElfW(Addr) dlpi_addr; /* オブジェクトのベースアドレス */
const char *dlpi_name; /* (null 文字で終端された)
オブジェクト名 */
const ElfW(Phdr) *dlpi_phdr; /* このオブジェクトの
ELF プログラムヘッダの
配列へのポインタ */
ElfW(Half) dlpi_phnum; /* dlpi_phdr のアイテム数 */
};
(ElfW() マクロ定義は引き数をハードウェアアーキテクチャに適した ELF デー
タ型の名前に変換する。たとえば、32 ビット プ ラ ッ ト フ ォ ー ム で は
ElfW(Addr) はデータ型名 Elf32_Addr を生成する。これらの型についての更に
詳細な情報は、ヘッダファイル と にある。
dlpi_addr フィールドは共有オブジェクトのベースアドレス (つまり、共有 オ
ブ ジェクトの仮想メモリアドレスと、ファイル (このファイルから共有オブジ
ェクトがロードされる) における共有オブジェクトのオフセットとの差分) を
表す。 dlpi_name は null 文字で終端された文字列であり、このパス名のファ
イルから共有オブジェクトがロードされる。
dlpi_phdr と dlpi_phnum フィールドの意味を理解するには、 ELF 共有オブジ
ェ クトが幾つかのセグメントから構成されていることと、各セグメントがそれ
に対応するプログラムヘッダ (そのセグメントを説明する) を持っているこ と
を知っている必要がある。 dlpi_phdr フィールドは、この共有オブジェクトの
プログラムヘッダの配列へのポインタである。 dlpi_phnum は、この配列の サ
イズを表す。
これらのプログラムヘッダは以下のような形式の構造体である:
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;
特定のプログラムヘッダ x の仮想メモリにおける位置は、以下の式で計算でき
る点に注意すること:
addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
返り値
dl_iterate_phdr() 関数は最後の callback の呼び出しで返された値を返す。
バージョン
dl_iterate_phdr() は glibc のバージョン 2.2.4 以降でサポートされてい る
。
準拠
dl_iterate_phdr() 関数は Linux 固有であり、移植を考えたアプリケーション
では避けるべきである。
例
以下のプログラムは、共有オブジェクトがロードされたパス名の一覧を表示 す
る。各共有オブジェクトについて、このプログラムはオブジェクトの ELF セグ
メントがロードされた仮想アドレスの一覧を表示する。
#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)\n", info->dlpi_name,
info->dlpi_phnum);
for (j = 0; j < info->dlpi_phnum; j++)
printf("\t\t header %2d: address=%10p\n", 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);
}
関連項目
ldd(1), objdump(1), readelf(1), dlopen(3), elf(5), fea-
ture_test_macros(7), ld.so(8), オンラインのいろいろな場所で入手できる
Executable and Linking Format Specification
Linux 2007-05-18 DL_ITERATE_PHDR(3)
DL_ITERATE_PHDR(3) Linux Programmer’s Manual DL_ITERATE_PHDR(3)
NAME
dl_iterate_phdr - walk through list of shared objects
SYNOPSIS
#define _GNU_SOURCE
#include
int dl_iterate_phdr(
int (*callback) (struct dl_phdr_info *info,
size_t size, void *data),
void *data);
DESCRIPTION
The dl_iterate_phdr() function allows an application to inquire at run
time to find out which shared objects it has loaded.
The dl_iterate_phdr() function walks through the list of an applica-
tion’s shared objects and calls the function callback once for each
object, until either all shared objects have been processed or callback
returns a non-zero value.
Each call to callback receives three arguments: info, which is a
pointer to a structure containing information about the shared object;
size, which is the size of the structure pointed to by info; and data,
which is a copy of whatever value was passed by the calling program as
the second argument (also named data) in the call to dl_iterate_phdr().
The info argument is a structure of the following type:
struct dl_phdr_info {
ElfW(Addr) dlpi_addr; /* Base address of object */
const char *dlpi_name; /* (Null-terminated) name of
object */
const ElfW(Phdr) *dlpi_phdr; /* Pointer to array of
ELF program headers
for this object */
ElfW(Half) dlpi_phnum; /* # of items in dlpi_phdr */
};
(The ElfW() macro definition turns its argument into the name of an ELF
data type suitable for the hardware architecture. For example, on a
32-bit platform, ElfW(Addr) yields the data type name Elf32_Addr. Fur-
ther information on these types can be found in the and
header files.)
The dlpi_addr field indicates the base address of the shared object
(i.e., the difference between the virtual memory address of the shared
object and the offset of that object in the file from which it was
loaded). The dlpi_name field is a null-terminated string giving the
pathname from which the shared object was loaded.
To understand the meaning of the dlpi_phdr and dlpi_phnum fields, we
need to be aware that an ELF shared object consists of a number of seg-
ments, each of which has a corresponding program header describing the
segment. The dlpi_phdr field is a pointer to an array of the program
headers for this shared object. The dlpi_phnum field indicates the
size of this array.
These program headers are structures of the following form:
typedef struct {
Elf32_Word p_type; /* Segment type */
Elf32_Off p_offset; /* Segment file offset */
Elf32_Addr p_vaddr; /* Segment virtual address */
Elf32_Addr p_paddr; /* Segment physical address */
Elf32_Word p_filesz; /* Segment size in file */
Elf32_Word p_memsz; /* Segment size in memory */
Elf32_Word p_flags; /* Segment flags */
Elf32_Word p_align; /* Segment alignment */
} Elf32_Phdr;
Note that we can calculate the location of a particular program header,
x, in virtual memory using the formula:
addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
RETURN VALUE
The dl_iterate_phdr() function returns whatever value was returned by
the last call to callback.
VERSIONS
dl_iterate_phdr() has been supported in glibc since version 2.2.4.
CONFORMING TO
The dl_iterate_phdr() function is Linux-specific and should be avoided
in portable applications.
EXAMPLE
The following program displays a list of pathnames of the shared
objects it has loaded. For each shared object, the program lists the
virtual addresses at which the object’s ELF segments are loaded.
#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)\n", info->dlpi_name,
info->dlpi_phnum);
for (j = 0; j < info->dlpi_phnum; j++)
printf("\t\t header %2d: address=%10p\n", 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);
}
SEE ALSO
ldd(1), objdump(1), readelf(1), dlopen(3), elf(5), fea-
ture_test_macros(7), ld.so(8), and the Executable and Linking Format
Specification available at various locations online.
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2007-05-18 DL_ITERATE_PHDR(3)