summaryrefslogtreecommitdiffstats
path: root/man3/dl_iterate_phdr.3
blob: 17cb9c65514ff6f7b63b7bef071ec1d30bf95bda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
.\" Copyright (c) 2003 by Michael Kerrisk (mtk16@ext.canterbury.ac.nz)
.\"
.\" 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.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" License.
.\"
.TH DL_ITERATE_PHDR 3 "Linux 2.4.21" "Linux Programmer's Manual"
.SH NAME
dl_iterate_phdr \- walk through list of shared objects
.SH SYNOPSIS
.nf
#define _GNU_SOURCE
.B #include <link.h>

\fBint dl_iterate_phdr(\fP
          \fBint (*\fPcallback\fB) \
(struct dl_phdr_info *\fPinfo\fB,\fP
                           \fBsize_t\fP size\fB, void *\fPdata\fB),\fP
          \fBvoid *\fPdata\fB);\fP
.fi
.SH DESCRIPTION
The
.B dl_iterate_phdr
function allows an application to inquire at run-time to find
out which shared objects it has loaded.

The
.B dl_iterate_phdr
function walks through the list of an
application's shared objects and calls the function
.I callback
once for each object,
until either all shared objects have been processed or
.I callback
returns a non-zero value.

Each call to
.I callback
receives three arguments:
.IR info ,
which is a pointer to a structure containing information
about the shared object;
.IR size ,
which is the size of the structure pointed to by
.IR info ;
and
.IR data ,
which is a copy of whatever value was passed by the calling
program as the second argument (also named
.IR data )
in the call to
.BR dl_iterate_phdr.

The
.I info
argument is a structure of the following type:

.nf
  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' */
  };
.fi

(The
.I 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.
Further information on these types can be found in the
.IR <elf.h> " and " <link.h>
header files.)

The
.I 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
.I dlpi_name
field is a null-terminated string giving the pathname
from which the shared object was loaded.

To understand the meaning of the
.I dlpi_phdr
and
.I dlpi_phnum
fields, we need to be aware that an ELF shared object consists
of a number of segments, each of which has a corresponding
program header describing the segment.
The
.I dlpi_phdr
field is a pointer to an array of the program headers for this
shared object.
The
.I dlpi_phnum
field indicates the size of this array.

These program headers are structures of the following form:
.nf

  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;
.fi

Note that we can calculate the location of a particular program header,
.IR x ,
in virtual memory memory using the formula:

.nf
  addr == info->dlpi_addr + info->dlpi_phdr[x].p_vaddr;
.fi
.SH EXAMPLE PROGRAM
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.

.nf
#define _GNU_SOURCE
#include <link.h>
#include <stdlib.h>
#include <stdio.h>

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);
}
.fi
.SH RETURN VALUE
The
.B dl_iterate_phdr
function returns whatever value was returned by the last call to
.IR callback .
.SH "CONFORMING TO"
The
.B dl_iterate_phdr
function is Linux specific and should be avoided in portable applications.
.SH "SEE ALSO"
.BR ldd (1),
.BR objdump (1),
.BR readelf (1),
.BR dlopen (3),
.BR ld.so (8),
and the
.I "Executable and Linking Format Specification"
available at various locations online.