xref: /freebsd/lib/libc/gen/dladdr.3 (revision 02e9120893770924227138ba49df1edb3896112a)
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.Dd February 5, 1998
27.Dt DLADDR 3
28.Os
29.Sh NAME
30.Nm dladdr
31.Nd find the shared object containing a given address
32.Sh LIBRARY
33.Lb libc
34.Sh SYNOPSIS
35.In dlfcn.h
36.Ft int
37.Fn dladdr "const void *addr" "Dl_info *info"
38.Sh DESCRIPTION
39The
40.Fn dladdr
41function
42queries the dynamic linker for information about the shared object
43containing the address
44.Fa addr .
45The information is returned in the structure specified by
46.Fa info .
47The structure contains at least the following members:
48.Bl -tag -width "XXXconst char *dli_fname"
49.It Li "const char *dli_fname"
50The pathname of the shared object containing the address.
51.It Li "void *dli_fbase"
52The base address at which the shared object is mapped into the
53address space of the calling process.
54.It Li "const char *dli_sname"
55The name of the nearest run-time symbol with a value less than or
56equal to
57.Fa addr .
58When possible, the symbol name is returned as it would appear in C
59source code.
60.Pp
61If no symbol with a suitable value is found, both this field and
62.Va dli_saddr
63are set to
64.Dv NULL .
65.It Li "void *dli_saddr"
66The value of the symbol returned in
67.Li dli_sname .
68.El
69.Pp
70The
71.Fn dladdr
72function
73is available only in dynamically linked programs.
74.Sh ERRORS
75If a mapped shared object containing
76.Fa addr
77cannot be found,
78.Fn dladdr
79returns 0.
80In that case, a message detailing the failure can be retrieved by
81calling
82.Fn dlerror .
83.Pp
84On success, a non-zero value is returned.
85.Sh SEE ALSO
86.Xr rtld 1 ,
87.Xr dlopen 3
88.Sh HISTORY
89The
90.Fn dladdr
91function first appeared in the Solaris operating system.
92.Sh BUGS
93This implementation is bug-compatible with the Solaris
94implementation.
95In particular, the following bugs are present:
96.Bl -bullet
97.It
98If
99.Fa addr
100lies in the main executable rather than in a shared library, the
101pathname returned in
102.Va dli_fname
103may not be correct.
104The pathname is taken directly from
105.Va argv[0]
106of the calling process.
107When executing a program specified by its
108full pathname, most shells set
109.Va argv[0]
110to the pathname.
111But this is not required of shells or guaranteed
112by the operating system.
113.It
114If
115.Fa addr
116is of the form
117.Va &func ,
118where
119.Va func
120is a global function, its value may be an unpleasant surprise.
121In
122dynamically linked programs, the address of a global function is
123considered to point to its program linkage table entry, rather than to
124the entry point of the function itself.
125This causes most global
126functions to appear to be defined within the main executable, rather
127than in the shared libraries where the actual code resides.
128.It
129Returning 0 as an indication of failure goes against long-standing
130Unix tradition.
131.El
132