summaryrefslogtreecommitdiffstats
path: root/upstream/debian-bookworm/man3/dlinfo.3
blob: be6516aaf6c9a6d68fb0bc2ff11a36e49c045a41 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'\" t
.\" Copyright (C) 2015 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" SPDX-License-Identifier: Linux-man-pages-copyleft
.\"
.TH dlinfo 3 2023-02-05 "Linux man-pages 6.03"
.SH NAME
dlinfo \- obtain information about a dynamically loaded object
.SH LIBRARY
Dynamic linking library
.RI ( libdl ", " \-ldl )
.SH SYNOPSIS
.nf
.B #define _GNU_SOURCE
.B #include <link.h>
.B #include <dlfcn.h>
.PP
.BR "int dlinfo(void *restrict " handle ", int " request \
", void *restrict " info );
.fi
.SH DESCRIPTION
The
.BR dlinfo ()
function obtains information about the dynamically loaded object
referred to by
.I handle
(typically obtained by an earlier call to
.BR dlopen (3)
or
.BR dlmopen (3)).
The
.I request
argument specifies which information is to be returned.
The
.I info
argument is a pointer to a buffer used to store information
returned by the call; the type of this argument depends on
.IR request .
.PP
The following values are supported for
.I request
(with the corresponding type for
.I info
shown in parentheses):
.TP
.BR RTLD_DI_LMID " (\fILmid_t *\fP)"
Obtain the ID of the link-map list (namespace) in which
.I handle
is loaded.
.TP
.BR RTLD_DI_LINKMAP " (\fIstruct link_map **\fP)"
Obtain a pointer to the
.I link_map
structure corresponding to
.IR handle .
The
.I info
argument points to a pointer to a
.I link_map
structure, defined in
.I <link.h>
as:
.IP
.in +4n
.EX
struct link_map {
    ElfW(Addr) l_addr;  /* Difference between the
                           address in the ELF file and
                           the address in memory */
    char      *l_name;  /* Absolute pathname where
                           object was found */
    ElfW(Dyn) *l_ld;    /* Dynamic section of the
                           shared object */
    struct link_map *l_next, *l_prev;
                        /* Chain of loaded objects */

    /* Plus additional fields private to the
       implementation */
};
.EE
.in
.TP
.BR RTLD_DI_ORIGIN " (\fIchar *\fP)"
Copy the pathname of the origin of the shared object corresponding to
.I handle
to the location pointed to by
.IR info .
.TP
.BR RTLD_DI_SERINFO " (\fIDl_serinfo *\fP)"
Obtain the library search paths for the shared object referred to by
.IR handle .
The
.I info
argument is a pointer to a
.I Dl_serinfo
that contains the search paths.
Because the number of search paths may vary,
the size of the structure pointed to by
.I info
can vary.
The
.B RTLD_DI_SERINFOSIZE
request described below allows applications to size the buffer suitably.
The caller must perform the following steps:
.RS
.IP (1) 5
Use a
.B RTLD_DI_SERINFOSIZE
request to populate a
.I Dl_serinfo
structure with the size
.RI ( dls_size )
of the structure needed for the subsequent
.B RTLD_DI_SERINFO
request.
.IP (2)
Allocate a
.I Dl_serinfo
buffer of the correct size
.RI ( dls_size ).
.IP (3)
Use a further
.B RTLD_DI_SERINFOSIZE
request to populate the
.I dls_size
and
.I dls_cnt
fields of the buffer allocated in the previous step.
.IP (4)
Use a
.B RTLD_DI_SERINFO
to obtain the library search paths.
.RE
.IP
The
.I Dl_serinfo
structure is defined as follows:
.IP
.in +4n
.EX
typedef struct {
    size_t dls_size;           /* Size in bytes of
                                  the whole buffer */
    unsigned int dls_cnt;      /* Number of elements
                                  in \[aq]dls_serpath\[aq] */
    Dl_serpath dls_serpath[1]; /* Actually longer,
                                  \[aq]dls_cnt\[aq] elements */
} Dl_serinfo;
.EE
.in
.IP
Each of the
.I dls_serpath
elements in the above structure is a structure of the following form:
.IP
.in +4n
.EX
typedef struct {
    char *dls_name;            /* Name of library search
                                  path directory */
    unsigned int dls_flags;    /* Indicates where this
                                  directory came from */
} Dl_serpath;
.EE
.in
.IP
The
.I dls_flags
field is currently unused, and always contains zero.
.TP
.BR RTLD_DI_SERINFOSIZE " (\fIDl_serinfo *\fP)"
Populate the
.I dls_size
and
.I dls_cnt
fields of the
.I Dl_serinfo
structure pointed to by
.I info
with values suitable for allocating a buffer for use in a subsequent
.B RTLD_DI_SERINFO
request.
.TP
.BR RTLD_DI_TLS_MODID " (\fIsize_t *\fP, since glibc 2.4)"
Obtain the module ID of this shared object's TLS (thread-local storage)
segment, as used in TLS relocations.
If this object does not define a TLS segment, zero is placed in
.IR *info .
.TP
.BR RTLD_DI_TLS_DATA " (\fIvoid **\fP, since glibc 2.4)"
Obtain a pointer to the calling
thread's TLS block corresponding to this shared object's TLS segment.
If this object does not define a PT_TLS segment,
or if the calling thread has not allocated a block for it,
NULL is placed in
.IR *info .
.SH RETURN VALUE
On success,
.BR dlinfo ()
returns 0.
On failure, it returns \-1; the cause of the error can be diagnosed using
.BR dlerror (3).
.SH VERSIONS
.BR dlinfo ()
first appeared in glibc 2.3.3.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.ad l
.nh
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.BR dlinfo ()
T}	Thread safety	MT-Safe
.TE
.hy
.ad
.sp 1
.SH STANDARDS
This function is a nonstandard GNU extension.
.SH NOTES
This function derives from the Solaris function of the same name
and also appears on some other systems.
The sets of requests supported by the various implementations
overlaps only partially.
.SH EXAMPLES
The program below opens a shared objects using
.BR dlopen (3)
and then uses the
.B RTLD_DI_SERINFOSIZE
and
.B RTLD_DI_SERINFO
requests to obtain the library search path list for the library.
Here is an example of what we might see when running the program:
.PP
.in +4n
.EX
$ \fB./a.out /lib64/libm.so.6\fP
dls_serpath[0].dls_name = /lib64
dls_serpath[1].dls_name = /usr/lib64
.EE
.in
.SS Program source
\&
.\" SRC BEGIN (dlinfo.c)
.EX
#define _GNU_SOURCE
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    void *handle;
    Dl_serinfo serinfo;
    Dl_serinfo *sip;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <libpath>\en", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* Obtain a handle for shared object specified on command line. */

    handle = dlopen(argv[1], RTLD_NOW);
    if (handle == NULL) {
        fprintf(stderr, "dlopen() failed: %s\en", dlerror());
        exit(EXIT_FAILURE);
    }

    /* Discover the size of the buffer that we must pass to
       RTLD_DI_SERINFO. */

    if (dlinfo(handle, RTLD_DI_SERINFOSIZE, &serinfo) == \-1) {
        fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\en", dlerror());
        exit(EXIT_FAILURE);
    }

    /* Allocate the buffer for use with RTLD_DI_SERINFO. */

    sip = malloc(serinfo.dls_size);
    if (sip == NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    /* Initialize the \[aq]dls_size\[aq] and \[aq]dls_cnt\[aq] fields in the newly
       allocated buffer. */

    if (dlinfo(handle, RTLD_DI_SERINFOSIZE, sip) == \-1) {
        fprintf(stderr, "RTLD_DI_SERINFOSIZE failed: %s\en", dlerror());
        exit(EXIT_FAILURE);
    }

    /* Fetch and print library search list. */

    if (dlinfo(handle, RTLD_DI_SERINFO, sip) == \-1) {
        fprintf(stderr, "RTLD_DI_SERINFO failed: %s\en", dlerror());
        exit(EXIT_FAILURE);
    }

    for (size_t j = 0; j < serinfo.dls_cnt; j++)
        printf("dls_serpath[%zu].dls_name = %s\en",
               j, sip\->dls_serpath[j].dls_name);

    exit(EXIT_SUCCESS);
}
.EE
.\" SRC END
.SH SEE ALSO
.BR dl_iterate_phdr (3),
.BR dladdr (3),
.BR dlerror (3),
.BR dlopen (3),
.BR dlsym (3),
.BR ld.so (8)