1fcf53d9bSJohn Marino.\" This source code is a product of Sun Microsystems, Inc. and is provided 2fcf53d9bSJohn Marino.\" for unrestricted use provided that this legend is included on all tape 3fcf53d9bSJohn Marino.\" media and as a part of the software program in whole or part. Users 4fcf53d9bSJohn Marino.\" may copy or modify this source code without charge, but are not authorized 5fcf53d9bSJohn Marino.\" to license or distribute it to anyone else except as part of a product or 6fcf53d9bSJohn Marino.\" program developed by the user. 7fcf53d9bSJohn Marino.\" 8fcf53d9bSJohn Marino.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC. 9fcf53d9bSJohn Marino.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY 10fcf53d9bSJohn Marino.\" OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT 11fcf53d9bSJohn Marino.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS 12fcf53d9bSJohn Marino.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED 13fcf53d9bSJohn Marino.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN 14fcf53d9bSJohn Marino.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT, 15fcf53d9bSJohn Marino.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 16fcf53d9bSJohn Marino.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY. 17fcf53d9bSJohn Marino.\" 18fcf53d9bSJohn Marino.\" This source code is provided with no support and without any obligation on 19fcf53d9bSJohn Marino.\" the part of Sun Microsystems, Inc. to assist in its use, correction, 20fcf53d9bSJohn Marino.\" modification or enhancement. 21fcf53d9bSJohn Marino.\" 22fcf53d9bSJohn Marino.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 23fcf53d9bSJohn Marino.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS 24fcf53d9bSJohn Marino.\" SOURCE CODE OR ANY PART THEREOF. 25fcf53d9bSJohn Marino.\" 26fcf53d9bSJohn Marino.\" Sun Microsystems, Inc. 27fcf53d9bSJohn Marino.\" 2550 Garcia Avenue 28fcf53d9bSJohn Marino.\" Mountain View, California 94043 29fcf53d9bSJohn Marino.\" 30fcf53d9bSJohn Marino.\" Copyright (c) 1991 Sun Microsystems, Inc. 31fcf53d9bSJohn Marino.\" 32fcf53d9bSJohn Marino.\" $FreeBSD: release/8.1.0/lib/libc/gen/dlopen.3 205979 2010-03-31 13:51:31Z gahr $ 33fcf53d9bSJohn Marino.\" 34bb5b9becSSascha Wildner.Dd February 22, 2018 35fcf53d9bSJohn Marino.Dt DLSYM 3 36fb5b3747SSascha Wildner.Os 37fcf53d9bSJohn Marino.Sh NAME 38fcf53d9bSJohn Marino.Nm dlsym , 39fcf53d9bSJohn Marino.Nm dlfunc 40fcf53d9bSJohn Marino.Nd shared object symbol lookup function 41fcf53d9bSJohn Marino.Sh LIBRARY 429f488f94SSascha WildnerThis function is not in a library. 439f488f94SSascha WildnerIt is included in every dynamically linked program automatically. 44fcf53d9bSJohn Marino.Sh SYNOPSIS 45fcf53d9bSJohn Marino.In dlfcn.h 46fcf53d9bSJohn Marino.Ft void * 47bb5b9becSSascha Wildner.Fn dlsym "void * restrict handle" "const char * restrict name" 48fcf53d9bSJohn Marino.Ft dlfunc_t 49bb5b9becSSascha Wildner.Fn dlfunc "void * restrict handle" "const char * restrict name" 50fcf53d9bSJohn Marino.Sh DESCRIPTION 51fcf53d9bSJohn MarinoThe 52fcf53d9bSJohn Marino.Fn dlsym 53fcf53d9bSJohn Marinofunction 54fcf53d9bSJohn Marinoreturns the address binding of the symbol described in the null-terminated 55fcf53d9bSJohn Marinocharacter string 56fcf53d9bSJohn Marino.Fa symbol , 57fcf53d9bSJohn Marinoas it occurs in the shared object identified by 58fcf53d9bSJohn Marino.Fa handle . 59fcf53d9bSJohn MarinoThe symbols exported by objects added to the address space by 60fcf53d9bSJohn Marino.Fn dlopen 61fcf53d9bSJohn Marinocan be accessed only through calls to 62fcf53d9bSJohn Marino.Fn dlsym . 63fcf53d9bSJohn MarinoSuch symbols do not supersede any definition of those symbols already present 64fcf53d9bSJohn Marinoin the address space when the object is loaded, nor are they available to 65fcf53d9bSJohn Marinosatisfy normal dynamic linking references. 66fcf53d9bSJohn Marino.Pp 67fcf53d9bSJohn MarinoIf 68fcf53d9bSJohn Marino.Fn dlsym 69fcf53d9bSJohn Marinois called with the special 70fcf53d9bSJohn Marino.Fa handle 71fcf53d9bSJohn Marino.Dv NULL , 72fcf53d9bSJohn Marinoit is interpreted as a reference to the executable or shared object 73fcf53d9bSJohn Marinofrom which the call 74fcf53d9bSJohn Marinois being made. 75fcf53d9bSJohn MarinoThus a shared object can reference its own symbols. 76fcf53d9bSJohn Marino.Pp 77fcf53d9bSJohn MarinoIf 78fcf53d9bSJohn Marino.Fn dlsym 79fcf53d9bSJohn Marinois called with the special 80fcf53d9bSJohn Marino.Fa handle 81fcf53d9bSJohn Marino.Dv RTLD_DEFAULT , 82fcf53d9bSJohn Marinothe search for the symbol follows the algorithm used for resolving 83fcf53d9bSJohn Marinoundefined symbols when objects are loaded. 84fcf53d9bSJohn MarinoThe objects searched are 85fcf53d9bSJohn Marinoas follows, in the given order: 86fcf53d9bSJohn Marino.Bl -enum 87fcf53d9bSJohn Marino.It 88fcf53d9bSJohn MarinoThe referencing object itself (or the object from which the call to 89fcf53d9bSJohn Marino.Fn dlsym 90fcf53d9bSJohn Marinois made), if that object was linked using the 91fcf53d9bSJohn Marino.Fl Wsymbolic 92fcf53d9bSJohn Marinooption to 93fcf53d9bSJohn Marino.Xr ld 1 . 94fcf53d9bSJohn Marino.It 95fcf53d9bSJohn MarinoAll objects loaded at program start-up. 96fcf53d9bSJohn Marino.It 97fcf53d9bSJohn MarinoAll objects loaded via 98fcf53d9bSJohn Marino.Fn dlopen 99fcf53d9bSJohn Marinowith the 100fcf53d9bSJohn Marino.Dv RTLD_GLOBAL 101fcf53d9bSJohn Marinoflag set in the 102fcf53d9bSJohn Marino.Fa mode 103fcf53d9bSJohn Marinoargument. 104fcf53d9bSJohn Marino.It 105fcf53d9bSJohn MarinoAll objects loaded via 106fcf53d9bSJohn Marino.Fn dlopen 107fcf53d9bSJohn Marinowhich are in needed-object DAGs that also contain the referencing object. 108fcf53d9bSJohn Marino.El 109fcf53d9bSJohn Marino.Pp 110fcf53d9bSJohn MarinoIf 111fcf53d9bSJohn Marino.Fn dlsym 112fcf53d9bSJohn Marinois called with the special 113fcf53d9bSJohn Marino.Fa handle 114fcf53d9bSJohn Marino.Dv RTLD_NEXT , 115fcf53d9bSJohn Marinothen the search for the symbol is limited to the shared objects 116fcf53d9bSJohn Marinowhich were loaded after the one issuing the call to 117fcf53d9bSJohn Marino.Fn dlsym . 118fcf53d9bSJohn MarinoThus, if the function is called from the main program, all 119fcf53d9bSJohn Marinothe shared libraries are searched. 120fcf53d9bSJohn MarinoIf it is called from a shared library, all subsequent shared 121fcf53d9bSJohn Marinolibraries are searched. 122fcf53d9bSJohn Marino.Dv RTLD_NEXT 123fcf53d9bSJohn Marinois useful for implementing wrappers around library functions. 124fcf53d9bSJohn MarinoFor example, a wrapper function 125fcf53d9bSJohn Marino.Fn getpid 126fcf53d9bSJohn Marinocould access the 127fcf53d9bSJohn Marino.Dq real 128fcf53d9bSJohn Marino.Fn getpid 129fcf53d9bSJohn Marinowith 130fcf53d9bSJohn Marino.Li dlsym(RTLD_NEXT, \&"getpid\&") . 131fcf53d9bSJohn Marino(Actually, the 132fcf53d9bSJohn Marino.Fn dlfunc 133fcf53d9bSJohn Marinointerface, below, should be used, since 134fcf53d9bSJohn Marino.Fn getpid 135fcf53d9bSJohn Marinois a function and not a data object.) 136fcf53d9bSJohn Marino.Pp 137fcf53d9bSJohn MarinoIf 138fcf53d9bSJohn Marino.Fn dlsym 139fcf53d9bSJohn Marinois called with the special 140fcf53d9bSJohn Marino.Fa handle 141fcf53d9bSJohn Marino.Dv RTLD_SELF , 142fcf53d9bSJohn Marinothen the search for the symbol is limited to the shared object 143fcf53d9bSJohn Marinoissuing the call to 144fcf53d9bSJohn Marino.Fn dlsym 145fcf53d9bSJohn Marinoand those shared objects which were loaded after it. 146fcf53d9bSJohn Marino.Pp 147fcf53d9bSJohn MarinoThe 148fcf53d9bSJohn Marino.Fn dlfunc 149fcf53d9bSJohn Marinofunction 150fcf53d9bSJohn Marinoimplements all of the behavior of 151fcf53d9bSJohn Marino.Fn dlsym , 152fcf53d9bSJohn Marinobut has a return type which can be cast to a function pointer without 153fcf53d9bSJohn Marinotriggering compiler diagnostics. 154fcf53d9bSJohn Marino(The 155fcf53d9bSJohn Marino.Fn dlsym 156fcf53d9bSJohn Marinofunction 157fcf53d9bSJohn Marinoreturns a data pointer; in the C standard, conversions between 158fcf53d9bSJohn Marinodata and function pointer types are undefined. 15984f80053SSascha WildnerSome compilers and code checkers warn about such casts.) 160fcf53d9bSJohn MarinoThe precise return type of 161fcf53d9bSJohn Marino.Fn dlfunc 162fcf53d9bSJohn Marinois unspecified; applications must cast it to an appropriate function pointer 163fcf53d9bSJohn Marinotype. 164fcf53d9bSJohn Marino.Sh NOTES 165fcf53d9bSJohn MarinoELF executables need to be linked 166fcf53d9bSJohn Marinousing the 167fcf53d9bSJohn Marino.Fl export-dynamic 168fcf53d9bSJohn Marinooption to 169fcf53d9bSJohn Marino.Xr ld 1 170fcf53d9bSJohn Marinofor symbols defined in the executable to become visible to 171fcf53d9bSJohn Marino.Fn dlsym . 172*4a5f69f6SSascha Wildner.Sh RETURN VALUES 173fcf53d9bSJohn MarinoThe 174fcf53d9bSJohn Marino.Fn dlsym 175fcf53d9bSJohn Marinoand 176fcf53d9bSJohn Marino.Fn dlfunc 177fcf53d9bSJohn Marinofunctions 178fcf53d9bSJohn Marinoreturn the address of the symbol unless the symbol can not be found. 179fcf53d9bSJohn MarinoIn this case, they return a null pointer and set an error condition 180fcf53d9bSJohn Marinowhich may be queried with 181fcf53d9bSJohn Marino.Fn dlerror . 182*4a5f69f6SSascha Wildner.Sh EXAMPLES 183fcf53d9bSJohn MarinoThe following program will obtain a pointer to the cosine function using 184fcf53d9bSJohn Marinodlsym, and then it will use it to print out the value of cosine (2.0). 185fcf53d9bSJohn Marino.Bd -literal 186fcf53d9bSJohn Marino#include <dlfcn.h> 187fcf53d9bSJohn Marino#include <stdlib.h> 188fcf53d9bSJohn Marino#include <stdio.h> 189fcf53d9bSJohn Marino 190fcf53d9bSJohn Marinoint 191fcf53d9bSJohn Marinomain (int argc, char *argv[]) 192fcf53d9bSJohn Marino{ 193fcf53d9bSJohn Marino void *handle; 194fcf53d9bSJohn Marino double (*func_cosine)(double x); 195fcf53d9bSJohn Marino 196fcf53d9bSJohn Marino /* open the system shared math library */ 197fcf53d9bSJohn Marino handle = dlopen("libm.so", RTLD_LAZY); 198fcf53d9bSJohn Marino if (!handle) { 199c19ddaabSSascha Wildner fprintf (stderr, "%s\en", dlerror ()); 200fcf53d9bSJohn Marino exit (EXIT_FAILURE); 201fcf53d9bSJohn Marino } 202fcf53d9bSJohn Marino 203fcf53d9bSJohn Marino /* get pointer to cosine function */ 204fcf53d9bSJohn Marino func_cosine = dlsym (handle, "cos"); 205fcf53d9bSJohn Marino if (func_cosine == NULL) { 206c19ddaabSSascha Wildner fprintf (stderr, "%s function not found\en", "cos"); 207fcf53d9bSJohn Marino dlclose (handle); 208fcf53d9bSJohn Marino exit (EXIT_FAILURE); 209fcf53d9bSJohn Marino } 210fcf53d9bSJohn Marino 211fcf53d9bSJohn Marino /* Calculate and display the cosine of 2.0 */ 212c19ddaabSSascha Wildner printf ("cosine of 2.0 = %f\en", func_cosine(2.0)); 213fcf53d9bSJohn Marino dlclose (handle); 214fcf53d9bSJohn Marino 215fcf53d9bSJohn Marino exit(EXIT_SUCCESS); 216fcf53d9bSJohn Marino} 217fcf53d9bSJohn Marino.Ed 218fcf53d9bSJohn Marino.Sh SEE ALSO 219fcf53d9bSJohn Marino.Xr rtld 1 , 220fcf53d9bSJohn Marino.Xr dlfcn 3 , 221fcf53d9bSJohn Marino.Xr dlopen 3 , 222fcf53d9bSJohn Marino.Xr dlvsym 3 223