xref: /dragonfly/lib/libc/gen/dl_iterate_phdr.3 (revision e6d22e9b)
1.\"   $NetBSD: dl_iterate_phdr.3,v 1.2 2010/10/16 12:05:48 wiz Exp $
2.\"   $OpenBSD: dl_iterate_phdr.3,v 1.3 2007/05/31 19:19:48 jmc Exp $
3.\"
4.\" Copyright (c) 2005 Mark Kettenis
5.\"
6.\" Permission to use, copy, modify, and distribute this software for any
7.\" purpose with or without fee is hereby granted, provided that the above
8.\" copyright notice and this permission notice appear in all copies.
9.\"
10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17.\"
18.Dd April 28, 2011
19.Dt DL_ITERATE_PHDR 3
20.Os
21.Sh NAME
22.Nm dl_iterate_phdr
23.Nd iterate over program headers
24.Sh LIBRARY
25This function is not in a library.
26It is included in every dynamically linked program automatically.
27.Sh SYNOPSIS
28.In link.h
29.Ft int
30.Fn dl_iterate_phdr "int (*callback)(struct dl_phdr_info *, size_t, void*)" "void *data"
31.Sh DESCRIPTION
32The
33.Fn dl_iterate_phdr
34function iterates over all shared objects loaded into a process's
35address space, calling
36.Fa callback
37for each shared object, passing it information about the object's
38program headers and the
39.Fa data
40argument.
41The information about the program headers is passed in a structure
42that is defined as:
43.Bd -literal
44struct dl_phdr_info {
45        Elf_Addr                dlpi_addr;
46        const char             *dlpi_name;
47        const Elf_Phdr         *dlpi_phdr;
48        Elf_Half                dlpi_phnum;
49        unsigned long long int  dlpi_adds;
50        unsigned long long int  dlpi_subs;
51        size_t                  dlpi_tls_modid;
52        void                   *dlpi_tls_data;
53};
54.Ed
55.Pp
56The members of
57.Li struct dl_phdr_info
58have the following meaning:
59.Bl -tag -width XXXdlpi_phdr
60.It Fa dlpi_addr
61The base address at which the shared object is mapped into the address
62space of the calling process.
63.It Fa dlpi_name
64The name of the shared object.
65.It Fa dlpi_phdr
66A pointer to the shared object's program headers.
67.It Fa dlpi_phnum
68The number of program headers in the shared object.
69.It Fa dlpi_adds
70The number of objects added into the main program.
71.It Fa dlpi_subs
72The number of objects removed from the main program.
73.El
74.Pp
75To make it possible for programs to check whether any new members have
76been added, the size of the structure is passed as an argument to
77.Fa callback .
78.Sh RETURN VALUES
79The
80.Fn dl_iterate_phdr
81function returns whatever value was returned by the last call to callback.
82.Sh EXAMPLES
83The following program displays a list of pathnames of the shared objects it has
84loaded.
85For each shared object, the program lists the virtual addresses at
86which the object's ELF segments are loaded.
87.Bd -literal
88#include <link.h>
89#include <stdlib.h>
90#include <stdio.h>
91
92static int
93callback(struct dl_phdr_info *info, size_t size, void *data)
94{
95    int j;
96    printf("name=%s (%d segments)\en", info->dlpi_name,
97        info->dlpi_phnum);
98    for (j = 0; j < info->dlpi_phnum; j++)
99         printf("\t\t header %2d: address=%10p\en", j,
100             (void *) (info->dlpi_addr + info->dlpi_phdr[j].p_vaddr));
101    return 0;
102}
103
104int
105main(int argc, char *argv[])
106{
107    dl_iterate_phdr(callback, NULL);
108    exit(EXIT_SUCCESS);
109}
110.Ed
111.Sh SEE ALSO
112.Xr ld 1 ,
113.Xr ld-elf.so.2 1 ,
114.Xr dlfcn 3 ,
115.Xr elf 5
116.Sh HISTORY
117The
118.Nm
119function first appeared in
120.Dx 2.9 .
121