1.\" This source code is a product of Sun Microsystems, Inc. and is provided 2.\" for unrestricted use provided that this legend is included on all tape 3.\" media and as a part of the software program in whole or part. Users 4.\" may copy or modify this source code without charge, but are not authorized 5.\" to license or distribute it to anyone else except as part of a product or 6.\" program developed by the user. 7.\" 8.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC. 9.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY 10.\" OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT 11.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS 12.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED 13.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN 14.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT, 15.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 16.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY. 17.\" 18.\" This source code is provided with no support and without any obligation on 19.\" the part of Sun Microsystems, Inc. to assist in its use, correction, 20.\" modification or enhancement. 21.\" 22.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 23.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS 24.\" SOURCE CODE OR ANY PART THEREOF. 25.\" 26.\" Sun Microsystems, Inc. 27.\" 2550 Garcia Avenue 28.\" Mountain View, California 94043 29.\" 30.\" Copyright (c) 1991 Sun Microsystems, Inc. 31.\" 32.\" @(#) dlopen.3 1.6 90/01/31 SMI 33.\" $FreeBSD$ 34.\" 35.Dd April 1, 2009 36.Os 37.Dt DLOPEN 3 38.Sh NAME 39.Nm dlopen , 40.Nm dlsym , 41.Nm dlfunc , 42.Nm dlerror , 43.Nm dlclose 44.Nd programmatic interface to the dynamic linker 45.Sh LIBRARY 46.Lb libc 47.Sh SYNOPSIS 48.In dlfcn.h 49.Ft void * 50.Fn dlopen "const char *path" "int mode" 51.Ft void * 52.Fn dlsym "void * restrict handle" "const char * restrict symbol" 53.Ft dlfunc_t 54.Fn dlfunc "void * restrict handle" "const char * restrict symbol" 55.Ft const char * 56.Fn dlerror "void" 57.Ft int 58.Fn dlclose "void *handle" 59.Sh DESCRIPTION 60These functions provide a simple programmatic interface to the services of the 61dynamic linker. 62Operations are provided to add new shared objects to a 63program's address space, to obtain the address bindings of symbols 64defined by such 65objects, and to remove such objects when their use is no longer required. 66.Pp 67The 68.Fn dlopen 69function 70provides access to the shared object in 71.Fa path , 72returning a descriptor that can be used for later 73references to the object in calls to 74.Fn dlsym 75and 76.Fn dlclose . 77If 78.Fa path 79was not in the address space prior to the call to 80.Fn dlopen , 81it is placed in the address space. 82When an object is first loaded into the address space in this way, its 83function 84.Fn _init , 85if any, is called by the dynamic linker. 86If 87.Fa path 88has already been placed in the address space in a previous call to 89.Fn dlopen , 90it is not added a second time, although a reference count of 91.Fn dlopen 92operations on 93.Fa path 94is maintained. 95A null pointer supplied for 96.Fa path 97is interpreted as a reference to the main 98executable of the process. 99The 100.Fa mode 101argument 102controls the way in which external function references from the 103loaded object are bound to their referents. 104It must contain one of the following values, possibly ORed with 105additional flags which will be described subsequently: 106.Bl -tag -width RTLD_LAZYX 107.It Dv RTLD_LAZY 108Each external function reference is resolved when the function is first 109called. 110.It Dv RTLD_NOW 111All external function references are bound immediately by 112.Fn dlopen . 113.El 114.Pp 115.Dv RTLD_LAZY 116is normally preferred, for reasons of efficiency. 117However, 118.Dv RTLD_NOW 119is useful to ensure that any undefined symbols are discovered during the 120call to 121.Fn dlopen . 122.Pp 123One of the following flags may be ORed into the 124.Fa mode 125argument: 126.Bl -tag -width RTLD_NODELETE 127.It Dv RTLD_GLOBAL 128Symbols from this shared object and its directed acyclic graph (DAG) 129of needed objects will be available for resolving undefined references 130from all other shared objects. 131.It Dv RTLD_LOCAL 132Symbols in this shared object and its DAG of needed objects will be 133available for resolving undefined references only from other objects 134in the same DAG. 135This is the default, but it may be specified 136explicitly with this flag. 137.It Dv RTLD_TRACE 138When set, causes dynamic linker to exit after loading all objects 139needed by this shared object and printing a summary which includes 140the absolute pathnames of all objects, to standard output. 141With this flag 142.Fn dlopen 143will return to the caller only in the case of error. 144.It Dv RTLD_NODELETE 145Prevents unload of the loaded object on 146.Fn dlclose . 147The same behaviour may be requested by 148.Fl "z nodelete" 149option of the static linker 150.Xr ld 1 . 151.El 152.Pp 153If 154.Fn dlopen 155fails, it returns a null pointer, and sets an error condition which may 156be interrogated with 157.Fn dlerror . 158.Pp 159The 160.Fn dlsym 161function 162returns the address binding of the symbol described in the null-terminated 163character string 164.Fa symbol , 165as it occurs in the shared object identified by 166.Fa handle . 167The symbols exported by objects added to the address space by 168.Fn dlopen 169can be accessed only through calls to 170.Fn dlsym . 171Such symbols do not supersede any definition of those symbols already present 172in the address space when the object is loaded, nor are they available to 173satisfy normal dynamic linking references. 174.Pp 175If 176.Fn dlsym 177is called with the special 178.Fa handle 179.Dv NULL , 180it is interpreted as a reference to the executable or shared object 181from which the call 182is being made. 183Thus a shared object can reference its own symbols. 184.Pp 185If 186.Fn dlsym 187is called with the special 188.Fa handle 189.Dv RTLD_DEFAULT , 190the search for the symbol follows the algorithm used for resolving 191undefined symbols when objects are loaded. 192The objects searched are 193as follows, in the given order: 194.Bl -enum 195.It 196The referencing object itself (or the object from which the call to 197.Fn dlsym 198is made), if that object was linked using the 199.Fl Wsymbolic 200option to 201.Xr ld 1 . 202.It 203All objects loaded at program start-up. 204.It 205All objects loaded via 206.Fn dlopen 207with the 208.Dv RTLD_GLOBAL 209flag set in the 210.Fa mode 211argument. 212.It 213All objects loaded via 214.Fn dlopen 215which are in needed-object DAGs that also contain the referencing object. 216.El 217.Pp 218If 219.Fn dlsym 220is called with the special 221.Fa handle 222.Dv RTLD_NEXT , 223then the search for the symbol is limited to the shared objects 224which were loaded after the one issuing the call to 225.Fn dlsym . 226Thus, if the function is called from the main program, all 227the shared libraries are searched. 228If it is called from a shared library, all subsequent shared 229libraries are searched. 230.Dv RTLD_NEXT 231is useful for implementing wrappers around library functions. 232For example, a wrapper function 233.Fn getpid 234could access the 235.Dq real 236.Fn getpid 237with 238.Li dlsym(RTLD_NEXT, \&"getpid\&") . 239(Actually, the 240.Fn dlfunc 241interface, below, should be used, since 242.Fn getpid 243is a function and not a data object.) 244.Pp 245If 246.Fn dlsym 247is called with the special 248.Fa handle 249.Dv RTLD_SELF , 250then the search for the symbol is limited to the shared object 251issuing the call to 252.Fn dlsym 253and those shared objects which were loaded after it. 254.Pp 255The 256.Fn dlsym 257function 258returns a null pointer if the symbol cannot be found, and sets an error 259condition which may be queried with 260.Fn dlerror . 261.Pp 262The 263.Fn dlfunc 264function 265implements all of the behavior of 266.Fn dlsym , 267but has a return type which can be cast to a function pointer without 268triggering compiler diagnostics. 269(The 270.Fn dlsym 271function 272returns a data pointer; in the C standard, conversions between 273data and function pointer types are undefined. 274Some compilers and 275.Xr lint 1 276utilities warn about such casts.) 277The precise return type of 278.Fn dlfunc 279is unspecified; applications must cast it to an appropriate function pointer 280type. 281.Pp 282The 283.Fn dlerror 284function 285returns a null-terminated character string describing the last error that 286occurred during a call to 287.Fn dlopen , 288.Fn dladdr , 289.Fn dlinfo , 290.Fn dlsym , 291.Fn dlfunc , 292or 293.Fn dlclose . 294If no such error has occurred, 295.Fn dlerror 296returns a null pointer. 297At each call to 298.Fn dlerror , 299the error indication is reset. 300Thus in the case of two calls 301to 302.Fn dlerror , 303where the second call follows the first immediately, the second call 304will always return a null pointer. 305.Pp 306The 307.Fn dlclose 308function 309deletes a reference to the shared object referenced by 310.Fa handle . 311If the reference count drops to 0, the object is removed from the 312address space, and 313.Fa handle 314is rendered invalid. 315Just before removing a shared object in this way, the dynamic linker 316calls the object's 317.Fn _fini 318function, if such a function is defined by the object. 319If 320.Fn dlclose 321is successful, it returns a value of 0. 322Otherwise it returns -1, and sets an error condition that can be 323interrogated with 324.Fn dlerror . 325.Pp 326The object-intrinsic functions 327.Fn _init 328and 329.Fn _fini 330are called with no arguments, and are not expected to return values. 331.Sh NOTES 332ELF executables need to be linked 333using the 334.Fl export-dynamic 335option to 336.Xr ld 1 337for symbols defined in the executable to become visible to 338.Fn dlsym . 339.Pp 340In previous implementations, it was necessary to prepend an underscore 341to all external symbols in order to gain symbol 342compatibility with object code compiled from the C language. 343This is 344still the case when using the (obsolete) 345.Fl aout 346option to the C language compiler. 347.Sh ERRORS 348The 349.Fn dlopen , 350.Fn dlsym , 351and 352.Fn dlfunc 353functions 354return a null pointer in the event of errors. 355The 356.Fn dlclose 357function 358returns 0 on success, or -1 if an error occurred. 359Whenever an error has been detected, a message detailing it can be 360retrieved via a call to 361.Fn dlerror . 362.Sh SEE ALSO 363.Xr ld 1 , 364.Xr rtld 1 , 365.Xr dladdr 3 , 366.Xr dlinfo 3 , 367.Xr link 5 368