xref: /dragonfly/lib/libc/gen/dladdr.3 (revision 2cd2d2b5)
1.\"
2.\" Copyright (c) 1998 John D. Polstra
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\" SUCH DAMAGE.
25.\"
26.\" $FreeBSD: src/lib/libc/gen/dladdr.3,v 1.3.2.4 2003/03/15 15:11:05 trhodes Exp $
27.\" $DragonFly: src/lib/libc/gen/dladdr.3,v 1.2 2003/06/17 04:26:42 dillon Exp $
28.\"
29.Dd February 5, 1998
30.Os
31.Dt DLADDR 3
32.Sh NAME
33.Nm dladdr
34.Nd find the shared object containing a given address
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In dlfcn.h
39.Ft int
40.Fn dladdr "const void *addr" "Dl_info *info"
41.Sh DESCRIPTION
42The
43.Fn dladdr
44function
45queries the dynamic linker for information about the shared object
46containing the address
47.Fa addr .
48The information is returned in the structure specified by
49.Fa info .
50The structure contains at least the following members:
51.Bl -tag -width "XXXconst char *dli_fname"
52.It Li "const char *dli_fname"
53The pathname of the shared object containing the address.
54.It Li "void *dli_fbase"
55The base address at which the shared object is mapped into the
56address space of the calling process.
57.It Li "const char *dli_sname"
58The name of the nearest run-time symbol with a value less than or
59equal to
60.Fa addr .
61When possible, the symbol name is returned as it would appear in C
62source code.
63.Pp
64If no symbol with a suitable value is found, both this field and
65.Va dli_saddr
66are set to
67.Dv NULL .
68.It Li "void *dli_saddr"
69The value of the symbol returned in
70.Li dli_sname .
71.El
72.Pp
73The
74.Fn dladdr
75function
76is available only in dynamically linked programs.
77.Sh ERRORS
78If a mapped shared object containing
79.Fa addr
80cannot be found,
81.Fn dladdr
82returns 0.
83In that case, a message detailing the failure can be retrieved by
84calling
85.Fn dlerror .
86.Pp
87On success, a non-zero value is returned.
88.Sh SEE ALSO
89.Xr rtld 1 ,
90.Xr dlopen 3
91.Sh HISTORY
92The
93.Fn dladdr
94function first appeared in the Solaris operating system.
95.Sh BUGS
96This implementation is bug-compatible with the Solaris
97implementation.  In particular, the following bugs are present:
98.Bl -bullet
99.It
100If
101.Fa addr
102lies in the main executable rather than in a shared library, the
103pathname returned in
104.Va dli_fname
105may not be correct.  The pathname is taken directly from
106.Va argv[0]
107of the calling process.  When executing a program specified by its
108full pathname, most shells set
109.Va argv[0]
110to the pathname.  But this is not required of shells or guaranteed
111by the operating system.
112.It
113If
114.Fa addr
115is of the form
116.Va &func ,
117where
118.Va func
119is a global function, its value may be an unpleasant surprise.  In
120dynamically linked programs, the address of a global function is
121considered to point to its program linkage table entry, rather than to
122the entry point of the function itself.  This causes most global
123functions to appear to be defined within the main executable, rather
124than in the shared libraries where the actual code resides.
125.It
126Returning 0 as an indication of failure goes against long-standing
127Unix tradition.
128.El
129