summaryrefslogtreecommitdiffstats
path: root/lib/libc/gen/dlinfo.3
blob: 53ac2585d4cc34786d885fd9cd71b2494e680eef (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
.\"
.\" Copyright (c) 2003 Alexey Zelkin <phantom@FreeBSD.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd February 14, 2003
.Os
.Dt DLINFO 3
.Sh NAME
.Nm dlinfo
.Nd information about dynamically loaded object
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In link.h
.In dlfcn.h
.Ft int
.Fn dlinfo "void * __restrict handle" "int request" "void * __restrict p"
.Sh DESCRIPTION
The
.Fn dlinfo
function provides information about dynamically loaded object.
The action taken by
.Fn dlinfo
and exact meaning and type of
.Fa p
argument depend on value of the
.Fa request
argument provided by caller.
.Pp
A
.Fa handle
argument is either the value returned from a
.Fn dlopen
function call or special handle
.Dv RTLD_SELF .
If handle is the value returned from
.Fn dlopen
call, the information returned by the
.Fn dlinfo
function is pertains the specified object.
If handle is the special handle
.Dv RTLD_SELF ,
the information returned pertains to the caller itself.
.Pp
The following are possible values for
.Fa request
argument to be passed into
.Fn dlinfo :
.Bl -tag -width Ds
.It RTLD_DI_LINKMAP
Retrieve the Link_map (or
.Ft struct link_map )
structure pointer for
.Fa handle
specified.
On successful return the
.Fa p
argument is filled with pointer to Link_map structure
.Ft ( Link_map **p )
describing shared object specified by
.Fa handle
argument.
.Ft Link_map
stuctures are maintained as double-linked list by
.Xr ld.so 1 
in same order as
.Fn dlopen
and
.Fn dlclose
are called.
See
.Sx EXAMPLES
(Example 1.)
.Pp
The
.Ft Link_map
structure is defined in <link.h> and have following members:
.Pp
.Bd -literal
  caddr_t         l_addr;    /* Base Address of library */
  const char      *l_name;   /* Absolute Path to Library */
  const void      *l_ld;     /* Pointer to .dynamic in memory */
  struct link_map *l_next,   /* linked list of of mapped libs */
                  *l_prev;     
.Ed
.Bl -tag -width Ds
.It l_addr
The base address of the object loaded into memory.
.It l_name
The full name of loaded shared object.
.It l_ld
The address of dynamic linking information segment
.Dv ( PT_DYNAMIC )
loaded into memory.
.It l_next
The next Link_map structure on the link-map list.
.It l_prev
The previous Link_map structure on the link-map list.
.El
.It RTLD_DI_SERINFO
Retrieve the library search paths associated with given
.Fa handle
argument.
The
.Fa p
argument should point to
.Ft Dl_serinfo
structure buffer
.Fa ( Dl_serinfo *p ) .
.Ft Dl_serinfo
structure must be initialized first with a
.Dv RTLD_DI_SERINFOSIZE
request.
.Pp
The returned
.Ft Dl_serinfo
structure contains
.Dv dls_cnt
.Ft Dl_serpath
entries.
Each entry's
.Dv dlp_name
field points to the search path.
The corresponding
.Dv dlp_info
field contains one of more flags indicating the origin of the path (see the
.Dv LA_SER_*
flags defined in <link.h> header file.)
See
.Sx EXAMPLES
(Example 2) for usage example.
.It RTLD_DI_SERINFOSIZE
Initialize a
.Ft Dl_serinfo
structure for use in a
.Dv RTLD_DI_SERINFO
request.
Both the
.Dv dls_cnt
and
.Dv dls_size
fields are returned to indicate the number of search paths applicable
to the handle, and the total size of a
.Ft Dl_serinfo
buffer required to hold
.Dv dls_cnt
.Ft Dl_serpath
entries and the associated search path strings.
See
.Sx EXAMPLES
(Example 2) for usage example.
.It RTLD_DI_ORIGIN
Retrieve the origin of the dynamic object associated with the handle.
On successful return
.Fa p
argument is filled with
.Ft char
pointer
.Ft ( char *p ) .
.El
.Sh EXAMPLES
Example 1: Using
.Fn dlinfo
to retrieve Link_map structure.
.Pp
The following example shows how dynamic library can detect the list
of shared libraries loaded after caller's one.
For simplicity, error checking has been omitted.
.Bd -literal
     Link_map *map;

     dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);

     while (map != NULL) {
         printf("%p: %s\n", map->l_addr, map->l_name);
         map = map->l_next;
     }
.Ed
.Pp
Example 2: Using
.Fn dlinfo
to retrieve the library search paths.
.Pp
The following example shows how a dynamic object can inspect the library
search paths that would be used to locate a simple filename with
.Fn dlopen .
For simplicity, error checking has been omitted.
.Bd -literal
      Dl_serinfo     _info, *info = &_info;
      Dl_serpath     *path;
      unsigned int    cnt;

      /* determine search path count and required buffer size */
      dlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info);

      /* allocate new buffer and initialize */
      info = malloc(_info.dls_size);
      info->dls_size = _info.dls_size;
      info->dls_cnt = _info.dls_cnt;

      /* obtain sarch path information */
      dlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info);

      path = &info->dls_serpath[0];

      for (cnt = 1; cnt <= info->dls_cnt; cnt++, path++) {
          (void) printf("%2d: %s\n", cnt, path->dls_name);
      }
.Ed
.Sh RETURN VALUES
.Fn dlinfo
returns 0 on success, or -1 if error occured.
Whenever an error has been detected, a message detailing it can
be retrieved via a call to
.Fn dlerror .
.Sh SEE ALSO
.Xr rtld 1 ,
.Xr dladdr 3 ,
.Xr dlopen 3 ,
.Xr dlsym 3
.Sh HISTORY
The
.Fn dlinfo
function first appeared in the Solaris operating system.
In
.Fx
it first appeared in
.Fx 4.8 .
.Sh AUTHORS
The
.Fx
implementation of
.Fn dlinfo
function was originally written by
.An Alexey Zelkin
.Aq phantom@FreeBSD.org
and later extended and improved by
.An Alexander Kabaev
.Aq kan@FreeBSD.org .
.Pp
The manual page for this function was written by
.An Alexey Zelkin
.Aq phantom@FreeBSD.org .
OpenPOWER on IntegriCloud