.\" 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. .\" .\" This source code is provided with no support and without any obligation on .\" the part of Sun Microsystems, Inc. to assist in its use, correction, .\" 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 .\" $FreeBSD: head/lib/libc/gen/dlopen.3 211397 2010-08-16 15:18:30Z joel $ .\" .Dd April 28, 2011 .Dt DLOPEN 3 .Os .Sh NAME .Nm dlopen .Nd returns handle to dynamically loaded shared object .Sh LIBRARY This function is not in a library. It is included in every dynamically linked program automatically. .Sh SYNOPSIS .In dlfcn.h .Ft void * .Fn dlopen "const char *name" "int mode" .Sh DESCRIPTION The .Fn dlopen function provides access to the shared object in .Fa path , returning a descriptor that can be used for later references to the object in calls to other dl functions. If .Fa name was not in the address space prior to the call to .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 name has already been placed in the address space in a previous call to .Fn dlopen , it is not added a second time, although a reference count of .Fn dlopen operations on .Fa name is maintained. A null pointer supplied for .Fa name is interpreted as a reference to the main executable of the process. The .Fa mode argument controls the way in which external function references from the loaded object are bound to their referents. It must contain one of the following values, possibly ORed with additional flags which will be described subsequently: .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 . .Pp One of the following flags may be ORed into the .Fa mode argument: .Bl -tag -width RTLD_NODELETE .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. .It Dv RTLD_TRACE When set, causes dynamic linker to exit after loading all objects needed by this shared object and printing a summary which includes the absolute pathnames of all objects, to standard output. With this flag .Fn dlopen will return to the caller only in the case of error. .It Dv RTLD_NODELETE Prevents unload of the loaded object on .Fn dlclose . The same behaviour may be requested by .Fl "z nodelete" option of the static linker .Xr ld 1 . .It Dv RTLD_NOLOAD Ony return valid handle for the object if it is already loaded in the process address space, otherwise .Dv NULL is returned. Other mode flags may be specified, which will be applied for promotion for the found object. .El .Sh RETURN VALUE The .Fn dlopen function returns a null pointer in the event of an error. The Whenever an error has been detected, a message detailing it can be retrieved via a call to .Fn dlerror . .Sh EXAMPLE The following program will open any shared gcc library found and display the directory in which it was found using the dfinfo function. .Bd -literal #include #include #include int main (int argc, char *argv[]) { void *handle; int result; char origin[256]; /* open shared gcc library */ handle = dlopen("libgcc_s.so", RTLD_LAZY); if (!handle) { fprintf (stderr, "%s\en", dlerror ()); exit (EXIT_FAILURE); } /* get information about the library origin */ result = dlinfo (handle, RTLD_DI_ORIGIN, (void *)&origin); if (result < 0) { fprintf (stderr, "%s\en", dlerror ()); dlclose (handle); exit (EXIT_FAILURE); } /* Display the origin */ printf ("libgcc_s origin is %s\en", &origin[0]); dlclose (handle); exit(EXIT_SUCCESS); } .Ed .Sh SEE ALSO .Xr rtld 1 , .Xr dlclose 3 , .Xr dlerror 3 , .Xr dlfcn 3 , .Xr dlinfo 3