1995-02-08 17:56:27 +00:00
|
|
|
.\" This source code is a product of Sun Microsystems, Inc. and is provided
|
|
|
|
.\" for unrestricted use provided that this legend is included on all tape
|
|
|
|
.\" media and as a part of the software program in whole or part. Users
|
|
|
|
.\" may copy or modify this source code without charge, but are not authorized
|
|
|
|
.\" to license or distribute it to anyone else except as part of a product or
|
|
|
|
.\" program developed by the user.
|
|
|
|
.\"
|
|
|
|
.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
|
|
|
|
.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
|
|
|
|
.\" OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT
|
|
|
|
.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS
|
|
|
|
.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
|
|
|
|
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN
|
|
|
|
.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
|
|
|
|
.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
|
2000-10-30 13:23:19 +00:00
|
|
|
.\"
|
1995-02-08 17:56:27 +00:00
|
|
|
.\" This source code is provided with no support and without any obligation on
|
2000-10-30 13:23:19 +00:00
|
|
|
.\" the part of Sun Microsystems, Inc. to assist in its use, correction,
|
1995-02-08 17:56:27 +00:00
|
|
|
.\" modification or enhancement.
|
|
|
|
.\"
|
|
|
|
.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
|
|
|
|
.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
|
|
|
|
.\" SOURCE CODE OR ANY PART THEREOF.
|
|
|
|
.\"
|
|
|
|
.\" Sun Microsystems, Inc.
|
|
|
|
.\" 2550 Garcia Avenue
|
|
|
|
.\" Mountain View, California 94043
|
|
|
|
.\"
|
|
|
|
.\" Copyright (c) 1991 Sun Microsystems, Inc.
|
|
|
|
.\"
|
|
|
|
.\" @(#) dlopen.3 1.6 90/01/31 SMI
|
1999-08-28 00:22:10 +00:00
|
|
|
.\" $FreeBSD$
|
1999-07-12 20:50:10 +00:00
|
|
|
.\"
|
1997-01-12 19:55:49 +00:00
|
|
|
.Dd September 24, 1989
|
2001-07-10 13:41:46 +00:00
|
|
|
.Os
|
1997-01-12 19:55:49 +00:00
|
|
|
.Dt DLOPEN 3
|
|
|
|
.Sh NAME
|
2001-02-01 16:38:02 +00:00
|
|
|
.Nm dlopen , dlsym , dlerror , dlclose
|
1997-01-12 19:55:49 +00:00
|
|
|
.Nd programmatic interface to the dynamic linker
|
2000-04-21 09:42:15 +00:00
|
|
|
.Sh LIBRARY
|
|
|
|
.Lb libc
|
1997-01-12 19:55:49 +00:00
|
|
|
.Sh SYNOPSIS
|
2001-10-01 16:09:29 +00:00
|
|
|
.In dlfcn.h
|
1997-01-12 19:55:49 +00:00
|
|
|
.Ft void *
|
1997-11-22 03:34:46 +00:00
|
|
|
.Fn dlopen "const char *path" "int mode"
|
1997-01-12 19:55:49 +00:00
|
|
|
.Ft void *
|
1997-11-22 03:34:46 +00:00
|
|
|
.Fn dlsym "void *handle" "const char *symbol"
|
|
|
|
.Ft const char *
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlerror "void"
|
|
|
|
.Ft int
|
|
|
|
.Fn dlclose "void *handle"
|
|
|
|
.Sh DESCRIPTION
|
1995-02-08 17:56:27 +00:00
|
|
|
These functions provide a simple programmatic interface to the services of the
|
1997-01-12 19:55:49 +00:00
|
|
|
dynamic linker.
|
|
|
|
Operations are provided to add new shared objects to a
|
|
|
|
program's address space, to obtain the address bindings of symbols
|
|
|
|
defined by such
|
1995-02-08 17:56:27 +00:00
|
|
|
objects, and to remove such objects when their use is no longer required.
|
1997-01-12 19:55:49 +00:00
|
|
|
.Pp
|
|
|
|
.Fn dlopen
|
2000-10-30 13:23:19 +00:00
|
|
|
provides access to the shared object in
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa path ,
|
1995-02-08 17:56:27 +00:00
|
|
|
returning a descriptor that can be used for later
|
2000-10-30 13:23:19 +00:00
|
|
|
references to the object in calls to
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlsym
|
1995-02-08 17:56:27 +00:00
|
|
|
and
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlclose .
|
1995-02-08 17:56:27 +00:00
|
|
|
If
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa path
|
1995-02-08 17:56:27 +00:00
|
|
|
was not in the address space prior to the call to
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlopen ,
|
|
|
|
it is placed in the address space.
|
|
|
|
When an object is first loaded into the address space in this way, its
|
|
|
|
function
|
|
|
|
.Fn _init ,
|
|
|
|
if any, is called by the dynamic linker.
|
|
|
|
If
|
|
|
|
.Fa path
|
1995-02-08 17:56:27 +00:00
|
|
|
has already been placed in the address space in a previous call to
|
2000-10-30 13:23:19 +00:00
|
|
|
.Fn dlopen ,
|
|
|
|
it is not added a second time, although a reference count of
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlopen
|
1995-02-08 17:56:27 +00:00
|
|
|
operations on
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa path
|
|
|
|
is maintained.
|
2000-10-30 13:23:19 +00:00
|
|
|
A null pointer supplied for
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa path
|
|
|
|
is interpreted as a reference to the main
|
1995-02-08 17:56:27 +00:00
|
|
|
executable of the process.
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa mode
|
|
|
|
controls the way in which external function references from the
|
|
|
|
loaded object are bound to their referents.
|
2000-09-19 04:02:05 +00:00
|
|
|
It must contain one of the following values, possibly ORed with
|
|
|
|
additional flags which will be described subsequently:
|
1997-01-12 19:55:49 +00:00
|
|
|
.Bl -tag -width RTLD_LAZYX
|
|
|
|
.It Dv RTLD_LAZY
|
|
|
|
Each external function reference is resolved when the function is first
|
|
|
|
called.
|
|
|
|
.It Dv RTLD_NOW
|
|
|
|
All external function references are bound immediately by
|
|
|
|
.Fn dlopen .
|
|
|
|
.El
|
|
|
|
.Pp
|
|
|
|
.Dv RTLD_LAZY
|
|
|
|
is normally preferred, for reasons of efficiency.
|
|
|
|
However,
|
|
|
|
.Dv RTLD_NOW
|
|
|
|
is useful to ensure that any undefined symbols are discovered during the
|
|
|
|
call to
|
|
|
|
.Fn dlopen .
|
2000-09-19 04:02:05 +00:00
|
|
|
.Pp
|
|
|
|
One of the following flags may be ORed into the
|
|
|
|
.Fa mode
|
|
|
|
argument:
|
|
|
|
.Bl -tag -width RTLD_GLOBALX
|
|
|
|
.It Dv RTLD_GLOBAL
|
|
|
|
Symbols from this shared object and its directed acyclic graph (DAG)
|
|
|
|
of needed objects will be available for resolving undefined references
|
|
|
|
from all other shared objects.
|
|
|
|
.It Dv RTLD_LOCAL
|
|
|
|
Symbols in this shared object and its DAG of needed objects will be
|
|
|
|
available for resolving undefined references only from other objects
|
|
|
|
in the same DAG. This is the default, but it may be specified
|
|
|
|
explicitly with this flag.
|
|
|
|
.El
|
|
|
|
.Pp
|
2000-10-30 13:23:19 +00:00
|
|
|
If
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlopen
|
|
|
|
fails, it returns a null pointer, and sets an error condition which may
|
|
|
|
be interrogated with
|
|
|
|
.Fn dlerror .
|
|
|
|
.Pp
|
|
|
|
.Fn dlsym
|
1995-02-08 17:56:27 +00:00
|
|
|
returns the address binding of the symbol described in the null-terminated
|
|
|
|
character string
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa symbol ,
|
1995-02-08 17:56:27 +00:00
|
|
|
as it occurs in the shared object identified by
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa handle .
|
2000-10-30 13:23:19 +00:00
|
|
|
The symbols exported by objects added to the address space by
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlopen
|
|
|
|
can be accessed only through calls to
|
|
|
|
.Fn dlsym .
|
|
|
|
Such symbols do not supersede any definition of those symbols already present
|
1995-02-08 17:56:27 +00:00
|
|
|
in the address space when the object is loaded, nor are they available to
|
1997-01-12 19:55:49 +00:00
|
|
|
satisfy normal dynamic linking references.
|
2000-09-19 04:28:34 +00:00
|
|
|
.Pp
|
|
|
|
If
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlsym
|
2000-09-19 04:28:34 +00:00
|
|
|
is called with the special
|
|
|
|
.Fa handle
|
|
|
|
.Dv NULL ,
|
|
|
|
it is interpreted as a reference to the executable or shared object
|
|
|
|
from which the call
|
1997-01-12 19:55:49 +00:00
|
|
|
is being made. Thus a shared object can reference its own symbols.
|
2000-09-19 04:28:34 +00:00
|
|
|
.Pp
|
|
|
|
If
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlsym
|
2000-09-19 04:28:34 +00:00
|
|
|
is called with the special
|
|
|
|
.Fa handle
|
|
|
|
.Dv RTLD_DEFAULT ,
|
|
|
|
the search for the symbol follows the algorithm used for resolving
|
|
|
|
undefined symbols when objects are loaded. The objects searched are
|
|
|
|
as follows, in the given order:
|
|
|
|
.Bl -enum
|
|
|
|
.It
|
|
|
|
The referencing object itself (or the object from which the call to
|
|
|
|
.Fn dlsym
|
|
|
|
is made), if that object was linked using the
|
|
|
|
.Fl Wsymbolic
|
|
|
|
option to
|
|
|
|
.Xr ld 1 .
|
|
|
|
.It
|
|
|
|
All objects loaded at program start-up.
|
|
|
|
.It
|
|
|
|
All objects loaded via
|
|
|
|
.Fn dlopen
|
|
|
|
which are in needed-object DAGs that also contain the referencing object.
|
|
|
|
.It
|
|
|
|
All objects loaded via
|
|
|
|
.Fn dlopen
|
|
|
|
with the
|
|
|
|
.Dv RTLD_GLOBAL
|
|
|
|
flag set in the
|
|
|
|
.Fa mode
|
|
|
|
argument.
|
|
|
|
.El
|
1997-01-12 19:55:49 +00:00
|
|
|
.Pp
|
1997-08-02 04:56:44 +00:00
|
|
|
If
|
|
|
|
.Fn dlsym
|
|
|
|
is called with the special
|
|
|
|
.Fa handle
|
|
|
|
.Dv RTLD_NEXT ,
|
|
|
|
then the search for the symbol is limited to the shared objects
|
|
|
|
which were loaded after the one issuing the call to
|
|
|
|
.Fn dlsym .
|
|
|
|
Thus, if the function is called from the main program, all
|
|
|
|
the shared libraries are searched.
|
|
|
|
If it is called from a shared library, all subsequent shared
|
|
|
|
libraries are searched.
|
|
|
|
.Dv RTLD_NEXT
|
|
|
|
is useful for implementing wrappers around library functions.
|
|
|
|
For example, a wrapper function
|
|
|
|
.Fn getpid
|
|
|
|
could access the
|
|
|
|
.Dq real
|
|
|
|
.Fn getpid
|
|
|
|
with
|
1999-11-02 18:18:26 +00:00
|
|
|
.Li dlsym(RTLD_NEXT, \&"getpid\&") .
|
1997-08-02 04:56:44 +00:00
|
|
|
.Pp
|
2000-09-19 04:28:34 +00:00
|
|
|
.Fn dlsym
|
|
|
|
returns a null pointer if the symbol cannot be found, and sets an error
|
|
|
|
condition which may be queried with
|
|
|
|
.Fn dlerror .
|
|
|
|
.Pp
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlerror
|
1995-02-08 17:56:27 +00:00
|
|
|
returns a null-terminated character string describing the last error that
|
1997-01-12 19:55:49 +00:00
|
|
|
occurred during a call to
|
|
|
|
.Fn dlopen ,
|
|
|
|
.Fn dlsym ,
|
1995-02-08 17:56:27 +00:00
|
|
|
or
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlclose .
|
|
|
|
If no such error has occurred,
|
|
|
|
.Fn dlerror
|
|
|
|
returns a null pointer.
|
1995-02-08 17:56:27 +00:00
|
|
|
At each call to
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlerror ,
|
|
|
|
the error indication is reset. Thus in the case of two calls
|
1995-02-08 17:56:27 +00:00
|
|
|
to
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlerror ,
|
|
|
|
where the second call follows the first immediately, the second call
|
1995-02-08 17:56:27 +00:00
|
|
|
will always return a null pointer.
|
1997-01-12 19:55:49 +00:00
|
|
|
.Pp
|
|
|
|
.Fn dlclose
|
1995-02-08 17:56:27 +00:00
|
|
|
deletes a reference to the shared object referenced by
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fa handle .
|
|
|
|
If the reference count drops to 0, the object is removed from the
|
|
|
|
address space, and
|
|
|
|
.Fa handle
|
|
|
|
is rendered invalid.
|
|
|
|
Just before removing a shared object in this way, the dynamic linker
|
|
|
|
calls the object's
|
|
|
|
.Fn _fini
|
|
|
|
function, if such a function is defined by the object.
|
2000-10-30 13:23:19 +00:00
|
|
|
If
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlclose
|
|
|
|
is successful, it returns a value of 0.
|
|
|
|
Otherwise it returns -1, and sets an error condition that can be
|
|
|
|
interrogated with
|
|
|
|
.Fn dlerror .
|
|
|
|
.Pp
|
2000-10-30 13:23:19 +00:00
|
|
|
The object-intrinsic functions
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn _init
|
|
|
|
and
|
|
|
|
.Fn _fini
|
|
|
|
are called with no arguments, and are not expected to return values.
|
1999-02-17 02:41:04 +00:00
|
|
|
.Sh NOTES
|
|
|
|
ELF executables need to be linked
|
|
|
|
using the
|
|
|
|
.Fl export-dynamic
|
|
|
|
option to
|
|
|
|
.Xr ld 1
|
2000-10-30 13:23:19 +00:00
|
|
|
for symbols defined in the executable to become visible to
|
1999-02-17 02:41:04 +00:00
|
|
|
.Fn dlsym .
|
1999-11-02 18:18:26 +00:00
|
|
|
.Pp
|
|
|
|
In previous implementations, it was necessary to prepend an underscore
|
|
|
|
to all external symbols in order to gain symbol
|
|
|
|
compatibility with object code compiled from the C language. This is
|
|
|
|
still the case when using the (obsolete)
|
|
|
|
.Fl aout
|
|
|
|
option to the C language compiler.
|
1997-01-12 19:55:49 +00:00
|
|
|
.Sh ERRORS
|
|
|
|
.Fn dlopen
|
1995-02-08 17:56:27 +00:00
|
|
|
and
|
1997-01-12 19:55:49 +00:00
|
|
|
.Fn dlsym
|
|
|
|
return the null pointer in the event of errors.
|
|
|
|
.Fn dlclose
|
|
|
|
returns 0 on success, or -1 if an error occurred.
|
|
|
|
Whenever an error has been detected, a message detailing it can be
|
|
|
|
retrieved via a call to
|
|
|
|
.Fn dlerror .
|
|
|
|
.Sh SEE ALSO
|
|
|
|
.Xr ld 1 ,
|
1997-09-29 19:11:55 +00:00
|
|
|
.Xr rtld 1 ,
|
2001-06-08 22:50:33 +00:00
|
|
|
.Xr dladdr 3 ,
|
1997-09-29 19:11:55 +00:00
|
|
|
.Xr link 5
|