DragonFly On-Line Manual Pages

Search: Section:  


DLSYM(3)              DragonFly Library Functions Manual              DLSYM(3)

NAME

dlsym, dlfunc -- shared object symbol lookup function

LIBRARY

This function is not in a library. It is included in every dynamically linked program automatically.

SYNOPSIS

#include <dlfcn.h> void * dlsym(void * restrict handle, const char * restrict name); dlfunc_t dlfunc(void * restrict handle, const char * restrict name);

DESCRIPTION

The dlsym() function returns the address binding of the symbol described in the null-terminated character string symbol, as it occurs in the shared object identified by handle. The symbols exported by objects added to the address space by dlopen() can be accessed only through calls to dlsym(). Such symbols do not supersede any definition of those symbols already present in the address space when the object is loaded, nor are they available to satisfy normal dynamic linking references. If dlsym() is called with the special handle NULL, it is interpreted as a reference to the executable or shared object from which the call is being made. Thus a shared object can reference its own symbols. If dlsym() is called with the special handle 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: 1. The referencing object itself (or the object from which the call to dlsym() is made), if that object was linked using the -Wsymbolic option to ld(1). 2. All objects loaded at program start-up. 3. All objects loaded via dlopen() with the RTLD_GLOBAL flag set in the mode argument. 4. All objects loaded via dlopen() which are in needed-object DAGs that also contain the referencing object. If dlsym() is called with the special handle RTLD_NEXT, then the search for the symbol is limited to the shared objects which were loaded after the one issuing the call to 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. RTLD_NEXT is useful for implementing wrappers around library functions. For example, a wrapper function getpid() could access the ``real'' getpid() with dlsym(RTLD_NEXT, "getpid"). (Actually, the dlfunc() interface, below, should be used, since getpid() is a function and not a data object.) If dlsym() is called with the special handle RTLD_SELF, then the search for the symbol is limited to the shared object issuing the call to dlsym() and those shared objects which were loaded after it. The dlfunc() function implements all of the behavior of dlsym(), but has a return type which can be cast to a function pointer without triggering compiler diagnostics. (The dlsym() function returns a data pointer; in the C standard, conversions between data and function pointer types are undefined. Some compilers and code checkers warn about such casts.) The precise return type of dlfunc() is unspecified; applications must cast it to an appropriate function pointer type.

NOTES

ELF executables need to be linked using the -export-dynamic option to ld(1) for symbols defined in the executable to become visible to dlsym().

RETURN VALUES

The dlsym() and dlfunc() functions return the address of the symbol unless the symbol can not be found. In this case, they return a null pointer and set an error condition which may be queried with dlerror().

EXAMPLES

The following program will obtain a pointer to the cosine function using dlsym, and then it will use it to print out the value of cosine (2.0). #include <dlfcn.h> #include <stdlib.h> #include <stdio.h> int main (int argc, char *argv[]) { void *handle; double (*func_cosine)(double x); /* open the system shared math library */ handle = dlopen("libm.so", RTLD_LAZY); if (!handle) { fprintf (stderr, "%s\n", dlerror ()); exit (EXIT_FAILURE); } /* get pointer to cosine function */ func_cosine = dlsym (handle, "cos"); if (func_cosine == NULL) { fprintf (stderr, "%s function not found\n", "cos"); dlclose (handle); exit (EXIT_FAILURE); } /* Calculate and display the cosine of 2.0 */ printf ("cosine of 2.0 = %f\n", func_cosine(2.0)); dlclose (handle); exit(EXIT_SUCCESS); }

SEE ALSO

rtld(1), dlfcn(3), dlopen(3), dlvsym(3) DragonFly 5.5 February 22, 2018 DragonFly 5.5

Search: Section: