1645c4be3SJohn Polstra.\" This source code is a product of Sun Microsystems, Inc. and is provided 2645c4be3SJohn Polstra.\" for unrestricted use provided that this legend is included on all tape 3645c4be3SJohn Polstra.\" media and as a part of the software program in whole or part. Users 4645c4be3SJohn Polstra.\" may copy or modify this source code without charge, but are not authorized 5645c4be3SJohn Polstra.\" to license or distribute it to anyone else except as part of a product or 6645c4be3SJohn Polstra.\" program developed by the user. 7645c4be3SJohn Polstra.\" 8645c4be3SJohn Polstra.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC. 9645c4be3SJohn Polstra.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY 10645c4be3SJohn Polstra.\" OF SUCH SOURCE CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT 11645c4be3SJohn Polstra.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND. SUN MICROSYSTEMS, INC. DISCLAIMS 12645c4be3SJohn Polstra.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED 13645c4be3SJohn Polstra.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN 14645c4be3SJohn Polstra.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT, 15645c4be3SJohn Polstra.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 16645c4be3SJohn Polstra.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY. 17645c4be3SJohn Polstra.\" 18645c4be3SJohn Polstra.\" This source code is provided with no support and without any obligation on 19645c4be3SJohn Polstra.\" the part of Sun Microsystems, Inc. to assist in its use, correction, 20645c4be3SJohn Polstra.\" modification or enhancement. 21645c4be3SJohn Polstra.\" 22645c4be3SJohn Polstra.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 23645c4be3SJohn Polstra.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS 24645c4be3SJohn Polstra.\" SOURCE CODE OR ANY PART THEREOF. 25645c4be3SJohn Polstra.\" 26645c4be3SJohn Polstra.\" Sun Microsystems, Inc. 27645c4be3SJohn Polstra.\" 2550 Garcia Avenue 28645c4be3SJohn Polstra.\" Mountain View, California 94043 29645c4be3SJohn Polstra.\" 30645c4be3SJohn Polstra.\" Copyright (c) 1991 Sun Microsystems, Inc. 31645c4be3SJohn Polstra.\" 32645c4be3SJohn Polstra.\" @(#) dlopen.3 1.6 90/01/31 SMI 337f3dea24SPeter Wemm.\" $FreeBSD$ 34fbc400a6SNik Clayton.\" 3549e8c06bSKonstantin Belousov.Dd July 7, 2009 36a307d598SRuslan Ermilov.Os 37645c4be3SJohn Polstra.Dt DLOPEN 3 38645c4be3SJohn Polstra.Sh NAME 397ec37597SAlexey Zelkin.Nm dlopen , 407ec37597SAlexey Zelkin.Nm dlsym , 417ec37597SAlexey Zelkin.Nm dlfunc , 427ec37597SAlexey Zelkin.Nm dlerror , 437ec37597SAlexey Zelkin.Nm dlclose 44645c4be3SJohn Polstra.Nd programmatic interface to the dynamic linker 4525bb73e0SAlexey Zelkin.Sh LIBRARY 4625bb73e0SAlexey Zelkin.Lb libc 47645c4be3SJohn Polstra.Sh SYNOPSIS 4832eef9aeSRuslan Ermilov.In dlfcn.h 49645c4be3SJohn Polstra.Ft void * 50645c4be3SJohn Polstra.Fn dlopen "const char *path" "int mode" 51645c4be3SJohn Polstra.Ft void * 52a4a96c81SMike Barcroft.Fn dlsym "void * restrict handle" "const char * restrict symbol" 53fda23019SGarrett Wollman.Ft dlfunc_t 54a4a96c81SMike Barcroft.Fn dlfunc "void * restrict handle" "const char * restrict symbol" 55645c4be3SJohn Polstra.Ft const char * 56645c4be3SJohn Polstra.Fn dlerror "void" 57645c4be3SJohn Polstra.Ft int 58645c4be3SJohn Polstra.Fn dlclose "void *handle" 59645c4be3SJohn Polstra.Sh DESCRIPTION 60645c4be3SJohn PolstraThese functions provide a simple programmatic interface to the services of the 61645c4be3SJohn Polstradynamic linker. 62645c4be3SJohn PolstraOperations are provided to add new shared objects to a 63645c4be3SJohn Polstraprogram's address space, to obtain the address bindings of symbols 64645c4be3SJohn Polstradefined by such 65645c4be3SJohn Polstraobjects, and to remove such objects when their use is no longer required. 66645c4be3SJohn Polstra.Pp 6795f4226bSRuslan ErmilovThe 68645c4be3SJohn Polstra.Fn dlopen 6995f4226bSRuslan Ermilovfunction 70645c4be3SJohn Polstraprovides access to the shared object in 71645c4be3SJohn Polstra.Fa path , 72645c4be3SJohn Polstrareturning a descriptor that can be used for later 73645c4be3SJohn Polstrareferences to the object in calls to 74645c4be3SJohn Polstra.Fn dlsym 75645c4be3SJohn Polstraand 76645c4be3SJohn Polstra.Fn dlclose . 77645c4be3SJohn PolstraIf 78645c4be3SJohn Polstra.Fa path 79645c4be3SJohn Polstrawas not in the address space prior to the call to 80645c4be3SJohn Polstra.Fn dlopen , 81645c4be3SJohn Polstrait is placed in the address space. 82645c4be3SJohn PolstraWhen an object is first loaded into the address space in this way, its 83645c4be3SJohn Polstrafunction 84645c4be3SJohn Polstra.Fn _init , 85645c4be3SJohn Polstraif any, is called by the dynamic linker. 86645c4be3SJohn PolstraIf 87645c4be3SJohn Polstra.Fa path 88645c4be3SJohn Polstrahas already been placed in the address space in a previous call to 89645c4be3SJohn Polstra.Fn dlopen , 90645c4be3SJohn Polstrait is not added a second time, although a reference count of 91645c4be3SJohn Polstra.Fn dlopen 92645c4be3SJohn Polstraoperations on 93645c4be3SJohn Polstra.Fa path 94645c4be3SJohn Polstrais maintained. 95645c4be3SJohn PolstraA null pointer supplied for 96645c4be3SJohn Polstra.Fa path 97645c4be3SJohn Polstrais interpreted as a reference to the main 98645c4be3SJohn Polstraexecutable of the process. 992efeeba5SRuslan ErmilovThe 100645c4be3SJohn Polstra.Fa mode 1012efeeba5SRuslan Ermilovargument 102645c4be3SJohn Polstracontrols the way in which external function references from the 103645c4be3SJohn Polstraloaded object are bound to their referents. 10407f643a5SJohn PolstraIt must contain one of the following values, possibly ORed with 10507f643a5SJohn Polstraadditional flags which will be described subsequently: 106645c4be3SJohn Polstra.Bl -tag -width RTLD_LAZYX 107645c4be3SJohn Polstra.It Dv RTLD_LAZY 108645c4be3SJohn PolstraEach external function reference is resolved when the function is first 109645c4be3SJohn Polstracalled. 110645c4be3SJohn Polstra.It Dv RTLD_NOW 111645c4be3SJohn PolstraAll external function references are bound immediately by 112645c4be3SJohn Polstra.Fn dlopen . 113645c4be3SJohn Polstra.El 114645c4be3SJohn Polstra.Pp 115645c4be3SJohn Polstra.Dv RTLD_LAZY 116645c4be3SJohn Polstrais normally preferred, for reasons of efficiency. 117645c4be3SJohn PolstraHowever, 118645c4be3SJohn Polstra.Dv RTLD_NOW 119645c4be3SJohn Polstrais useful to ensure that any undefined symbols are discovered during the 120645c4be3SJohn Polstracall to 121645c4be3SJohn Polstra.Fn dlopen . 12207f643a5SJohn Polstra.Pp 12307f643a5SJohn PolstraOne of the following flags may be ORed into the 12407f643a5SJohn Polstra.Fa mode 12507f643a5SJohn Polstraargument: 126b2e4ce45SKonstantin Belousov.Bl -tag -width RTLD_NODELETE 12707f643a5SJohn Polstra.It Dv RTLD_GLOBAL 12807f643a5SJohn PolstraSymbols from this shared object and its directed acyclic graph (DAG) 12907f643a5SJohn Polstraof needed objects will be available for resolving undefined references 13007f643a5SJohn Polstrafrom all other shared objects. 13107f643a5SJohn Polstra.It Dv RTLD_LOCAL 13207f643a5SJohn PolstraSymbols in this shared object and its DAG of needed objects will be 13307f643a5SJohn Polstraavailable for resolving undefined references only from other objects 134760d9686SRuslan Ermilovin the same DAG. 135760d9686SRuslan ErmilovThis is the default, but it may be specified 13607f643a5SJohn Polstraexplicitly with this flag. 137c6de4ce7SMaxim Sobolev.It Dv RTLD_TRACE 138c6de4ce7SMaxim SobolevWhen set, causes dynamic linker to exit after loading all objects 139c6de4ce7SMaxim Sobolevneeded by this shared object and printing a summary which includes 140c6de4ce7SMaxim Sobolevthe absolute pathnames of all objects, to standard output. 141c6de4ce7SMaxim SobolevWith this flag 142c6de4ce7SMaxim Sobolev.Fn dlopen 143c6de4ce7SMaxim Sobolevwill return to the caller only in the case of error. 144b2e4ce45SKonstantin Belousov.It Dv RTLD_NODELETE 145b2e4ce45SKonstantin BelousovPrevents unload of the loaded object on 146b2e4ce45SKonstantin Belousov.Fn dlclose . 147b2e4ce45SKonstantin BelousovThe same behaviour may be requested by 148b2e4ce45SKonstantin Belousov.Fl "z nodelete" 149b2e4ce45SKonstantin Belousovoption of the static linker 150b2e4ce45SKonstantin Belousov.Xr ld 1 . 15149e8c06bSKonstantin Belousov.It Dv RTLD_NOLOAD 15249e8c06bSKonstantin BelousovOny return valid handle for the object if it is already loaded in 15349e8c06bSKonstantin Belousovthe process address space, otherwise 15449e8c06bSKonstantin Belousov.Dv NULL 15549e8c06bSKonstantin Belousovis returned. 15649e8c06bSKonstantin BelousovOther mode flags may be specified, which will be applied for promotion 15749e8c06bSKonstantin Belousovfor the found object. 15807f643a5SJohn Polstra.El 15907f643a5SJohn Polstra.Pp 160645c4be3SJohn PolstraIf 161645c4be3SJohn Polstra.Fn dlopen 162645c4be3SJohn Polstrafails, it returns a null pointer, and sets an error condition which may 163645c4be3SJohn Polstrabe interrogated with 164645c4be3SJohn Polstra.Fn dlerror . 165645c4be3SJohn Polstra.Pp 16695f4226bSRuslan ErmilovThe 167645c4be3SJohn Polstra.Fn dlsym 16895f4226bSRuslan Ermilovfunction 169645c4be3SJohn Polstrareturns the address binding of the symbol described in the null-terminated 170645c4be3SJohn Polstracharacter string 171645c4be3SJohn Polstra.Fa symbol , 172645c4be3SJohn Polstraas it occurs in the shared object identified by 173645c4be3SJohn Polstra.Fa handle . 174645c4be3SJohn PolstraThe symbols exported by objects added to the address space by 175645c4be3SJohn Polstra.Fn dlopen 176645c4be3SJohn Polstracan be accessed only through calls to 177645c4be3SJohn Polstra.Fn dlsym . 178645c4be3SJohn PolstraSuch symbols do not supersede any definition of those symbols already present 179645c4be3SJohn Polstrain the address space when the object is loaded, nor are they available to 180645c4be3SJohn Polstrasatisfy normal dynamic linking references. 1817f244df8SJohn Polstra.Pp 1827f244df8SJohn PolstraIf 1837f244df8SJohn Polstra.Fn dlsym 1847f244df8SJohn Polstrais called with the special 185645c4be3SJohn Polstra.Fa handle 1867f244df8SJohn Polstra.Dv NULL , 1877f244df8SJohn Polstrait is interpreted as a reference to the executable or shared object 1887f244df8SJohn Polstrafrom which the call 189760d9686SRuslan Ermilovis being made. 190760d9686SRuslan ErmilovThus a shared object can reference its own symbols. 1917f244df8SJohn Polstra.Pp 1927f244df8SJohn PolstraIf 193645c4be3SJohn Polstra.Fn dlsym 1947f244df8SJohn Polstrais called with the special 1957f244df8SJohn Polstra.Fa handle 1967f244df8SJohn Polstra.Dv RTLD_DEFAULT , 1977f244df8SJohn Polstrathe search for the symbol follows the algorithm used for resolving 198760d9686SRuslan Ermilovundefined symbols when objects are loaded. 199760d9686SRuslan ErmilovThe objects searched are 2007f244df8SJohn Polstraas follows, in the given order: 2017f244df8SJohn Polstra.Bl -enum 2027f244df8SJohn Polstra.It 2037f244df8SJohn PolstraThe referencing object itself (or the object from which the call to 2047f244df8SJohn Polstra.Fn dlsym 2057f244df8SJohn Polstrais made), if that object was linked using the 2067f244df8SJohn Polstra.Fl Wsymbolic 2077f244df8SJohn Polstraoption to 2087f244df8SJohn Polstra.Xr ld 1 . 2097f244df8SJohn Polstra.It 2107f244df8SJohn PolstraAll objects loaded at program start-up. 2117f244df8SJohn Polstra.It 2127f244df8SJohn PolstraAll objects loaded via 2137f244df8SJohn Polstra.Fn dlopen 2147f244df8SJohn Polstrawith the 2157f244df8SJohn Polstra.Dv RTLD_GLOBAL 2167f244df8SJohn Polstraflag set in the 2177f244df8SJohn Polstra.Fa mode 2187f244df8SJohn Polstraargument. 219c56864a7SPeter Pentchev.It 220c56864a7SPeter PentchevAll objects loaded via 221c56864a7SPeter Pentchev.Fn dlopen 222c56864a7SPeter Pentchevwhich are in needed-object DAGs that also contain the referencing object. 2237f244df8SJohn Polstra.El 224645c4be3SJohn Polstra.Pp 225645c4be3SJohn PolstraIf 226645c4be3SJohn Polstra.Fn dlsym 227645c4be3SJohn Polstrais called with the special 228645c4be3SJohn Polstra.Fa handle 229645c4be3SJohn Polstra.Dv RTLD_NEXT , 230645c4be3SJohn Polstrathen the search for the symbol is limited to the shared objects 231645c4be3SJohn Polstrawhich were loaded after the one issuing the call to 232645c4be3SJohn Polstra.Fn dlsym . 233645c4be3SJohn PolstraThus, if the function is called from the main program, all 234645c4be3SJohn Polstrathe shared libraries are searched. 235645c4be3SJohn PolstraIf it is called from a shared library, all subsequent shared 236645c4be3SJohn Polstralibraries are searched. 237645c4be3SJohn Polstra.Dv RTLD_NEXT 238645c4be3SJohn Polstrais useful for implementing wrappers around library functions. 239645c4be3SJohn PolstraFor example, a wrapper function 240645c4be3SJohn Polstra.Fn getpid 241645c4be3SJohn Polstracould access the 242645c4be3SJohn Polstra.Dq real 243645c4be3SJohn Polstra.Fn getpid 244645c4be3SJohn Polstrawith 245c786c636SJoerg Wunsch.Li dlsym(RTLD_NEXT, \&"getpid\&") . 246dc12134aSGarrett Wollman(Actually, the 247dc12134aSGarrett Wollman.Fn dlfunc 248dc12134aSGarrett Wollmaninterface, below, should be used, since 249dc12134aSGarrett Wollman.Fn getpid 250dc12134aSGarrett Wollmanis a function and not a data object.) 251645c4be3SJohn Polstra.Pp 2527ec37597SAlexey ZelkinIf 2537ec37597SAlexey Zelkin.Fn dlsym 2547ec37597SAlexey Zelkinis called with the special 2557ec37597SAlexey Zelkin.Fa handle 2567ec37597SAlexey Zelkin.Dv RTLD_SELF , 2577ec37597SAlexey Zelkinthen the search for the symbol is limited to the shared object 2587ec37597SAlexey Zelkinissuing the call to 2597ec37597SAlexey Zelkin.Fn dlsym 2607ec37597SAlexey Zelkinand those shared objects which were loaded after it. 2617ec37597SAlexey Zelkin.Pp 26295f4226bSRuslan ErmilovThe 2637f244df8SJohn Polstra.Fn dlsym 26495f4226bSRuslan Ermilovfunction 2657f244df8SJohn Polstrareturns a null pointer if the symbol cannot be found, and sets an error 2667f244df8SJohn Polstracondition which may be queried with 2677f244df8SJohn Polstra.Fn dlerror . 2687f244df8SJohn Polstra.Pp 26995f4226bSRuslan ErmilovThe 270dc12134aSGarrett Wollman.Fn dlfunc 27195f4226bSRuslan Ermilovfunction 272dc12134aSGarrett Wollmanimplements all of the behavior of 273dc12134aSGarrett Wollman.Fn dlsym , 274dc12134aSGarrett Wollmanbut has a return type which can be cast to a function pointer without 275dc12134aSGarrett Wollmantriggering compiler diagnostics. 27695f4226bSRuslan Ermilov(The 27795f4226bSRuslan Ermilov.Fn dlsym 27895f4226bSRuslan Ermilovfunction 279dc12134aSGarrett Wollmanreturns a data pointer; in the C standard, conversions between 280760d9686SRuslan Ermilovdata and function pointer types are undefined. 281760d9686SRuslan ErmilovSome compilers and 282dc12134aSGarrett Wollman.Xr lint 1 283760d9686SRuslan Ermilovutilities warn about such casts.) 284dc12134aSGarrett WollmanThe precise return type of 285dc12134aSGarrett Wollman.Fn dlfunc 286dc12134aSGarrett Wollmanis unspecified; applications must cast it to an appropriate function pointer 287dc12134aSGarrett Wollmantype. 288dc12134aSGarrett Wollman.Pp 28995f4226bSRuslan ErmilovThe 290645c4be3SJohn Polstra.Fn dlerror 29195f4226bSRuslan Ermilovfunction 292645c4be3SJohn Polstrareturns a null-terminated character string describing the last error that 293645c4be3SJohn Polstraoccurred during a call to 294645c4be3SJohn Polstra.Fn dlopen , 295e8598335SAlexey Zelkin.Fn dladdr , 296e8598335SAlexey Zelkin.Fn dlinfo , 297645c4be3SJohn Polstra.Fn dlsym , 298dc12134aSGarrett Wollman.Fn dlfunc , 299645c4be3SJohn Polstraor 300645c4be3SJohn Polstra.Fn dlclose . 301645c4be3SJohn PolstraIf no such error has occurred, 302645c4be3SJohn Polstra.Fn dlerror 303645c4be3SJohn Polstrareturns a null pointer. 304645c4be3SJohn PolstraAt each call to 305645c4be3SJohn Polstra.Fn dlerror , 306760d9686SRuslan Ermilovthe error indication is reset. 307760d9686SRuslan ErmilovThus in the case of two calls 308645c4be3SJohn Polstrato 309645c4be3SJohn Polstra.Fn dlerror , 310645c4be3SJohn Polstrawhere the second call follows the first immediately, the second call 311645c4be3SJohn Polstrawill always return a null pointer. 312645c4be3SJohn Polstra.Pp 31395f4226bSRuslan ErmilovThe 314645c4be3SJohn Polstra.Fn dlclose 31595f4226bSRuslan Ermilovfunction 316645c4be3SJohn Polstradeletes a reference to the shared object referenced by 317645c4be3SJohn Polstra.Fa handle . 318645c4be3SJohn PolstraIf the reference count drops to 0, the object is removed from the 319645c4be3SJohn Polstraaddress space, and 320645c4be3SJohn Polstra.Fa handle 321645c4be3SJohn Polstrais rendered invalid. 322645c4be3SJohn PolstraJust before removing a shared object in this way, the dynamic linker 323645c4be3SJohn Polstracalls the object's 324645c4be3SJohn Polstra.Fn _fini 325645c4be3SJohn Polstrafunction, if such a function is defined by the object. 326645c4be3SJohn PolstraIf 327645c4be3SJohn Polstra.Fn dlclose 328645c4be3SJohn Polstrais successful, it returns a value of 0. 329645c4be3SJohn PolstraOtherwise it returns -1, and sets an error condition that can be 330645c4be3SJohn Polstrainterrogated with 331645c4be3SJohn Polstra.Fn dlerror . 332645c4be3SJohn Polstra.Pp 333645c4be3SJohn PolstraThe object-intrinsic functions 334645c4be3SJohn Polstra.Fn _init 335645c4be3SJohn Polstraand 336645c4be3SJohn Polstra.Fn _fini 337645c4be3SJohn Polstraare called with no arguments, and are not expected to return values. 338f28529d7SJoseph Koshy.Sh NOTES 339f28529d7SJoseph KoshyELF executables need to be linked 340f28529d7SJoseph Koshyusing the 341f28529d7SJoseph Koshy.Fl export-dynamic 342f28529d7SJoseph Koshyoption to 343f28529d7SJoseph Koshy.Xr ld 1 344f28529d7SJoseph Koshyfor symbols defined in the executable to become visible to 345f28529d7SJoseph Koshy.Fn dlsym . 346c786c636SJoerg Wunsch.Pp 347c786c636SJoerg WunschIn previous implementations, it was necessary to prepend an underscore 348c786c636SJoerg Wunschto all external symbols in order to gain symbol 349760d9686SRuslan Ermilovcompatibility with object code compiled from the C language. 350760d9686SRuslan ErmilovThis is 351c786c636SJoerg Wunschstill the case when using the (obsolete) 352c786c636SJoerg Wunsch.Fl aout 353c786c636SJoerg Wunschoption to the C language compiler. 354645c4be3SJohn Polstra.Sh ERRORS 35595f4226bSRuslan ErmilovThe 356dc12134aSGarrett Wollman.Fn dlopen , 357dc12134aSGarrett Wollman.Fn dlsym , 358645c4be3SJohn Polstraand 359dc12134aSGarrett Wollman.Fn dlfunc 36095f4226bSRuslan Ermilovfunctions 361dc12134aSGarrett Wollmanreturn a null pointer in the event of errors. 36295f4226bSRuslan ErmilovThe 363645c4be3SJohn Polstra.Fn dlclose 36495f4226bSRuslan Ermilovfunction 365645c4be3SJohn Polstrareturns 0 on success, or -1 if an error occurred. 366645c4be3SJohn PolstraWhenever an error has been detected, a message detailing it can be 367645c4be3SJohn Polstraretrieved via a call to 368645c4be3SJohn Polstra.Fn dlerror . 369645c4be3SJohn Polstra.Sh SEE ALSO 370645c4be3SJohn Polstra.Xr ld 1 , 371645c4be3SJohn Polstra.Xr rtld 1 , 3724088069cSDima Dorfman.Xr dladdr 3 , 3737ec37597SAlexey Zelkin.Xr dlinfo 3 , 374645c4be3SJohn Polstra.Xr link 5 375