xref: /freebsd/lib/libc/gen/dlinfo.3 (revision d0ca9a7fe4450aaa19ad281a9cef37f413bd9e5c)
18647a1edSAlexey Zelkin.\"
28647a1edSAlexey Zelkin.\" Copyright (c) 2003 Alexey Zelkin <phantom@FreeBSD.org>
38647a1edSAlexey Zelkin.\" All rights reserved.
48647a1edSAlexey Zelkin.\"
58647a1edSAlexey Zelkin.\" Redistribution and use in source and binary forms, with or without
68647a1edSAlexey Zelkin.\" modification, are permitted provided that the following conditions
78647a1edSAlexey Zelkin.\" are met:
88647a1edSAlexey Zelkin.\" 1. Redistributions of source code must retain the above copyright
98647a1edSAlexey Zelkin.\"    notice, this list of conditions and the following disclaimer.
108647a1edSAlexey Zelkin.\" 2. Redistributions in binary form must reproduce the above copyright
118647a1edSAlexey Zelkin.\"    notice, this list of conditions and the following disclaimer in the
128647a1edSAlexey Zelkin.\"    documentation and/or other materials provided with the distribution.
138647a1edSAlexey Zelkin.\"
148647a1edSAlexey Zelkin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158647a1edSAlexey Zelkin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168647a1edSAlexey Zelkin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178647a1edSAlexey Zelkin.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188647a1edSAlexey Zelkin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198647a1edSAlexey Zelkin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208647a1edSAlexey Zelkin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218647a1edSAlexey Zelkin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228647a1edSAlexey Zelkin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238647a1edSAlexey Zelkin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248647a1edSAlexey Zelkin.\" SUCH DAMAGE.
258647a1edSAlexey Zelkin.\"
268647a1edSAlexey Zelkin.\" $FreeBSD$
278647a1edSAlexey Zelkin.\"
282c6d9dc0SKonstantin Belousov.Dd May 19, 2020
298647a1edSAlexey Zelkin.Dt DLINFO 3
30aa12cea2SUlrich Spörlein.Os
318647a1edSAlexey Zelkin.Sh NAME
328647a1edSAlexey Zelkin.Nm dlinfo
338647a1edSAlexey Zelkin.Nd information about dynamically loaded object
348647a1edSAlexey Zelkin.Sh LIBRARY
358647a1edSAlexey Zelkin.Lb libc
368647a1edSAlexey Zelkin.Sh SYNOPSIS
378647a1edSAlexey Zelkin.In link.h
388647a1edSAlexey Zelkin.In dlfcn.h
398647a1edSAlexey Zelkin.Ft int
400e35e492SRuslan Ermilov.Fn dlinfo "void * restrict handle" "int request" "void * restrict p"
418647a1edSAlexey Zelkin.Sh DESCRIPTION
428647a1edSAlexey ZelkinThe
438647a1edSAlexey Zelkin.Fn dlinfo
448647a1edSAlexey Zelkinfunction provides information about dynamically loaded object.
458647a1edSAlexey ZelkinThe action taken by
468647a1edSAlexey Zelkin.Fn dlinfo
478647a1edSAlexey Zelkinand exact meaning and type of
488647a1edSAlexey Zelkin.Fa p
498647a1edSAlexey Zelkinargument depend on value of the
508647a1edSAlexey Zelkin.Fa request
518647a1edSAlexey Zelkinargument provided by caller.
528647a1edSAlexey Zelkin.Pp
530e35e492SRuslan ErmilovThe
548647a1edSAlexey Zelkin.Fa handle
550e35e492SRuslan Ermilovargument is either the value returned from the
560e35e492SRuslan Ermilov.Xr dlopen 3
578647a1edSAlexey Zelkinfunction call or special handle
588647a1edSAlexey Zelkin.Dv RTLD_SELF .
590e35e492SRuslan ErmilovIf
600e35e492SRuslan Ermilov.Fa handle
610e35e492SRuslan Ermilovis the value returned from
620e35e492SRuslan Ermilov.Xr dlopen 3 ,
630e35e492SRuslan Ermilovthe information returned by the
648647a1edSAlexey Zelkin.Fn dlinfo
650e35e492SRuslan Ermilovfunction pertains to the specified object.
668647a1edSAlexey ZelkinIf handle is the special handle
678647a1edSAlexey Zelkin.Dv RTLD_SELF ,
688647a1edSAlexey Zelkinthe information returned pertains to the caller itself.
698647a1edSAlexey Zelkin.Pp
700e35e492SRuslan ErmilovPossible values for the
718647a1edSAlexey Zelkin.Fa request
720e35e492SRuslan Ermilovargument are:
730e35e492SRuslan Ermilov.Bl -tag -width indent
740e35e492SRuslan Ermilov.It Dv RTLD_DI_LINKMAP
750e35e492SRuslan ErmilovRetrieve the
760e35e492SRuslan Ermilov.Vt Link_map
770e35e492SRuslan Ermilov.Pq Vt "struct link_map"
780e35e492SRuslan Ermilovstructure pointer for the specified
790e35e492SRuslan Ermilov.Fa handle .
800e35e492SRuslan ErmilovOn successful return, the
818647a1edSAlexey Zelkin.Fa p
820e35e492SRuslan Ermilovargument is filled with the pointer to the
830e35e492SRuslan Ermilov.Vt Link_map
840e35e492SRuslan Ermilovstructure
850e35e492SRuslan Ermilov.Pq Fa "Link_map **p"
860e35e492SRuslan Ermilovdescribing a shared object specified by the
878647a1edSAlexey Zelkin.Fa handle
888647a1edSAlexey Zelkinargument.
890e35e492SRuslan ErmilovThe
900e35e492SRuslan Ermilov.Vt Link_map
9181181557SMike Pritchardstructures are maintained as a doubly linked list by
920e35e492SRuslan Ermilov.Xr ld.so 1 ,
930e35e492SRuslan Ermilovin the same order as
940e35e492SRuslan Ermilov.Xr dlopen 3
958647a1edSAlexey Zelkinand
960e35e492SRuslan Ermilov.Xr dlclose 3
978647a1edSAlexey Zelkinare called.
988647a1edSAlexey ZelkinSee
990e35e492SRuslan Ermilov.Sx EXAMPLES ,
1000e35e492SRuslan Ermilovexample 1.
1016bc55edbSAlexey Zelkin.Pp
1026bc55edbSAlexey ZelkinThe
1030e35e492SRuslan Ermilov.Vt Link_map
1040e35e492SRuslan Ermilovstructure is defined in
105fe08efe6SRuslan Ermilov.In link.h
1060e35e492SRuslan Ermilovand has the following members:
1070e35e492SRuslan Ermilov.Bd -literal -offset indent
108*d0ca9a7fSKonstantin Belousovcaddr_t         l_base;    /* Base Address of library */
1096bc55edbSAlexey Zelkinconst char      *l_name;   /* Absolute Path to Library */
1106bc55edbSAlexey Zelkinconst void      *l_ld;     /* Pointer to .dynamic in memory */
1110e35e492SRuslan Ermilovstruct link_map *l_next,   /* linked list of mapped libs */
1126bc55edbSAlexey Zelkin                *l_prev;
113*d0ca9a7fSKonstantin Belousovcaddr_t         l_addr;     /* Load Offset of library */
1146bc55edbSAlexey Zelkin.Ed
1150e35e492SRuslan Ermilov.Bl -tag -width ".Va l_addr"
116*d0ca9a7fSKonstantin Belousov.It Va l_base
117*d0ca9a7fSKonstantin BelousovThe base address of the object loaded into memory.
1180e35e492SRuslan Ermilov.It Va l_name
1190e35e492SRuslan ErmilovThe full name of the loaded shared object.
1200e35e492SRuslan Ermilov.It Va l_ld
1210e35e492SRuslan ErmilovThe address of the dynamic linking information segment
1220e35e492SRuslan Ermilov.Pq Dv PT_DYNAMIC
1236bc55edbSAlexey Zelkinloaded into memory.
1240e35e492SRuslan Ermilov.It Va l_next
1250e35e492SRuslan ErmilovThe next
1260e35e492SRuslan Ermilov.Vt Link_map
1270e35e492SRuslan Ermilovstructure on the link-map list.
1280e35e492SRuslan Ermilov.It Va l_prev
1290e35e492SRuslan ErmilovThe previous
1300e35e492SRuslan Ermilov.Vt Link_map
1310e35e492SRuslan Ermilovstructure on the link-map list.
132*d0ca9a7fSKonstantin Belousov.It Va l_addr
133*d0ca9a7fSKonstantin BelousovThe load offset of the object, that is, the difference between
134*d0ca9a7fSKonstantin Belousovthe actual load address and the base virtual address the object
135*d0ca9a7fSKonstantin Belousovwas linked at.
1366bc55edbSAlexey Zelkin.El
1370e35e492SRuslan Ermilov.It Dv RTLD_DI_SERINFO
1380e35e492SRuslan ErmilovRetrieve the library search paths associated with the given
1398647a1edSAlexey Zelkin.Fa handle
1408647a1edSAlexey Zelkinargument.
1418647a1edSAlexey ZelkinThe
1428647a1edSAlexey Zelkin.Fa p
1438647a1edSAlexey Zelkinargument should point to
1440e35e492SRuslan Ermilov.Vt Dl_serinfo
1458647a1edSAlexey Zelkinstructure buffer
1460e35e492SRuslan Ermilov.Pq Fa "Dl_serinfo *p" .
1470e35e492SRuslan ErmilovThe
1480e35e492SRuslan Ermilov.Vt Dl_serinfo
1490e35e492SRuslan Ermilovstructure must be initialized first with the
1508647a1edSAlexey Zelkin.Dv RTLD_DI_SERINFOSIZE
1518647a1edSAlexey Zelkinrequest.
1528647a1edSAlexey Zelkin.Pp
1538647a1edSAlexey ZelkinThe returned
1540e35e492SRuslan Ermilov.Vt Dl_serinfo
1558647a1edSAlexey Zelkinstructure contains
1560e35e492SRuslan Ermilov.Va dls_cnt
1570e35e492SRuslan Ermilov.Vt Dl_serpath
1588647a1edSAlexey Zelkinentries.
1598647a1edSAlexey ZelkinEach entry's
1600e35e492SRuslan Ermilov.Va dlp_name
1618647a1edSAlexey Zelkinfield points to the search path.
1628647a1edSAlexey ZelkinThe corresponding
1630e35e492SRuslan Ermilov.Va dlp_info
1648647a1edSAlexey Zelkinfield contains one of more flags indicating the origin of the path (see the
1658647a1edSAlexey Zelkin.Dv LA_SER_*
1660e35e492SRuslan Ermilovflags defined in the
167fe08efe6SRuslan Ermilov.In link.h
1680e35e492SRuslan Ermilovheader file).
1698647a1edSAlexey ZelkinSee
1700e35e492SRuslan Ermilov.Sx EXAMPLES ,
1710e35e492SRuslan Ermilovexample 2, for a usage example.
1720e35e492SRuslan Ermilov.It Dv RTLD_DI_SERINFOSIZE
1738647a1edSAlexey ZelkinInitialize a
1740e35e492SRuslan Ermilov.Vt Dl_serinfo
1758647a1edSAlexey Zelkinstructure for use in a
1768647a1edSAlexey Zelkin.Dv RTLD_DI_SERINFO
1778647a1edSAlexey Zelkinrequest.
1788647a1edSAlexey ZelkinBoth the
1790e35e492SRuslan Ermilov.Va dls_cnt
1808647a1edSAlexey Zelkinand
1810e35e492SRuslan Ermilov.Va dls_size
1828647a1edSAlexey Zelkinfields are returned to indicate the number of search paths applicable
1838647a1edSAlexey Zelkinto the handle, and the total size of a
1840e35e492SRuslan Ermilov.Vt Dl_serinfo
1858647a1edSAlexey Zelkinbuffer required to hold
1860e35e492SRuslan Ermilov.Va dls_cnt
1870e35e492SRuslan Ermilov.Vt Dl_serpath
1888647a1edSAlexey Zelkinentries and the associated search path strings.
1898647a1edSAlexey ZelkinSee
1900e35e492SRuslan Ermilov.Sx EXAMPLES ,
1910e35e492SRuslan Ermilovexample 2, for a usage example.
1920e35e492SRuslan Ermilov.It Va RTLD_DI_ORIGIN
1938647a1edSAlexey ZelkinRetrieve the origin of the dynamic object associated with the handle.
1940e35e492SRuslan ErmilovOn successful return,
1958647a1edSAlexey Zelkin.Fa p
1960e35e492SRuslan Ermilovargument is filled with the
1970e35e492SRuslan Ermilov.Vt char
1988647a1edSAlexey Zelkinpointer
1990e35e492SRuslan Ermilov.Pq Fa "char *p" .
2008647a1edSAlexey Zelkin.El
20124a0682cSRuslan Ermilov.Sh RETURN VALUES
20224a0682cSRuslan ErmilovThe
20324a0682cSRuslan Ermilov.Fn dlinfo
20424a0682cSRuslan Ermilovfunction returns 0 on success, or \-1 if an error occurred.
20524a0682cSRuslan ErmilovWhenever an error has been detected, a message detailing it can
20624a0682cSRuslan Ermilovbe retrieved via a call to
20724a0682cSRuslan Ermilov.Xr dlerror 3 .
2088647a1edSAlexey Zelkin.Sh EXAMPLES
209f7344926SAlexey ZelkinExample 1: Using
210f7344926SAlexey Zelkin.Fn dlinfo
2110e35e492SRuslan Ermilovto retrieve
2120e35e492SRuslan Ermilov.Vt Link_map
2130e35e492SRuslan Ermilovstructure.
214f7344926SAlexey Zelkin.Pp
215f7344926SAlexey ZelkinThe following example shows how dynamic library can detect the list
216f7344926SAlexey Zelkinof shared libraries loaded after caller's one.
217f7344926SAlexey ZelkinFor simplicity, error checking has been omitted.
2180e35e492SRuslan Ermilov.Bd -literal -offset indent
219f7344926SAlexey ZelkinLink_map *map;
220f7344926SAlexey Zelkin
221f7344926SAlexey Zelkindlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
222f7344926SAlexey Zelkin
223f7344926SAlexey Zelkinwhile (map != NULL) {
224aba60fa6SRuslan Ermilov	printf("%p: %s\\n", map->l_addr, map->l_name);
225f7344926SAlexey Zelkin	map = map->l_next;
226f7344926SAlexey Zelkin}
227f7344926SAlexey Zelkin.Ed
228f7344926SAlexey Zelkin.Pp
229f7344926SAlexey ZelkinExample 2: Using
230f7344926SAlexey Zelkin.Fn dlinfo
231f7344926SAlexey Zelkinto retrieve the library search paths.
232f7344926SAlexey Zelkin.Pp
233f7344926SAlexey ZelkinThe following example shows how a dynamic object can inspect the library
234f7344926SAlexey Zelkinsearch paths that would be used to locate a simple filename with
2350e35e492SRuslan Ermilov.Xr dlopen 3 .
236f7344926SAlexey ZelkinFor simplicity, error checking has been omitted.
2370e35e492SRuslan Ermilov.Bd -literal -offset indent
238f7344926SAlexey ZelkinDl_serinfo	 _info, *info = &_info;
239f7344926SAlexey ZelkinDl_serpath	*path;
240f7344926SAlexey Zelkinunsigned int	 cnt;
241f7344926SAlexey Zelkin
242f7344926SAlexey Zelkin/* determine search path count and required buffer size */
243f7344926SAlexey Zelkindlinfo(RTLD_SELF, RTLD_DI_SERINFOSIZE, (void *)info);
244f7344926SAlexey Zelkin
245f7344926SAlexey Zelkin/* allocate new buffer and initialize */
246f7344926SAlexey Zelkininfo = malloc(_info.dls_size);
247f7344926SAlexey Zelkininfo->dls_size = _info.dls_size;
248f7344926SAlexey Zelkininfo->dls_cnt = _info.dls_cnt;
249f7344926SAlexey Zelkin
250f7344926SAlexey Zelkin/* obtain sarch path information */
251f7344926SAlexey Zelkindlinfo(RTLD_SELF, RTLD_DI_SERINFO, (void *)info);
252f7344926SAlexey Zelkin
253f7344926SAlexey Zelkinpath = &info->dls_serpath[0];
254f7344926SAlexey Zelkin
255f7344926SAlexey Zelkinfor (cnt = 1; cnt <= info->dls_cnt; cnt++, path++) {
256aba60fa6SRuslan Ermilov	(void) printf("%2d: %s\\n", cnt, path->dls_name);
257f7344926SAlexey Zelkin}
258f7344926SAlexey Zelkin.Ed
2598647a1edSAlexey Zelkin.Sh SEE ALSO
2608647a1edSAlexey Zelkin.Xr rtld 1 ,
2618647a1edSAlexey Zelkin.Xr dladdr 3 ,
2628647a1edSAlexey Zelkin.Xr dlopen 3 ,
2638647a1edSAlexey Zelkin.Xr dlsym 3
2648647a1edSAlexey Zelkin.Sh HISTORY
2658647a1edSAlexey ZelkinThe
2668647a1edSAlexey Zelkin.Fn dlinfo
2678647a1edSAlexey Zelkinfunction first appeared in the Solaris operating system.
2688647a1edSAlexey ZelkinIn
2690e35e492SRuslan Ermilov.Fx ,
2708647a1edSAlexey Zelkinit first appeared in
2718647a1edSAlexey Zelkin.Fx 4.8 .
2728647a1edSAlexey Zelkin.Sh AUTHORS
2730e35e492SRuslan Ermilov.An -nosplit
2748647a1edSAlexey ZelkinThe
2756bc55edbSAlexey Zelkin.Fx
2760e35e492SRuslan Ermilovimplementation of the
2778647a1edSAlexey Zelkin.Fn dlinfo
2788647a1edSAlexey Zelkinfunction was originally written by
2798fbf3d50SBaptiste Daroussin.An Alexey Zelkin Aq Mt phantom@FreeBSD.org
2808647a1edSAlexey Zelkinand later extended and improved by
2818fbf3d50SBaptiste Daroussin.An Alexander Kabaev Aq Mt kan@FreeBSD.org .
2828647a1edSAlexey Zelkin.Pp
2838647a1edSAlexey ZelkinThe manual page for this function was written by
2848fbf3d50SBaptiste Daroussin.An Alexey Zelkin Aq Mt phantom@FreeBSD.org .
285