dlopen.3 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. .\" -*- nroff -*-
  2. .\" Copyright 1995 Yggdrasil Computing, Incorporated.
  3. .\" written by Adam J. Richter (adam@yggdrasil.com),
  4. .\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
  5. .\"
  6. .\" This is free documentation; you can redistribute it and/or
  7. .\" modify it under the terms of the GNU General Public License as
  8. .\" published by the Free Software Foundation; either version 2 of
  9. .\" the License, or (at your option) any later version.
  10. .\"
  11. .\" The GNU General Public License's references to "object code"
  12. .\" and "executables" are to be interpreted as the output of any
  13. .\" document formatting or typesetting system, including
  14. .\" intermediate and printed output.
  15. .\"
  16. .\" This manual is distributed in the hope that it will be useful,
  17. .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. .\" GNU General Public License for more details.
  20. .\"
  21. .\" You should have received a copy of the GNU General Public
  22. .\" License along with this manual; if not, write to the Free
  23. .\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
  24. .\" USA.
  25. .\"
  26. .TH DLOPEN 3 "16 May 1995" "Linux" "Linux Programmer's Manual"
  27. .SH NAME
  28. dlclose, dlerror, dlopen, dlsym \- Programming interface to dynamic linking loader.
  29. .SH SYNOPSIS
  30. .B #include <dlfcn.h>
  31. .sp
  32. .BI "void *dlopen (const char *" "filename" ", int " flag ");
  33. .br
  34. .BI "const char *dlerror(void);"
  35. .br
  36. .BI "void *dlsym(void *"handle ", char *"symbol ");"
  37. .br
  38. .BI "int dladdr(void *"address ", Dl_info *"dlip ");"
  39. .br
  40. .BI "int dlclose (void *"handle ");
  41. .sp
  42. Special symbols:
  43. .BR "_init" ", " "_fini" ". "
  44. .SH DESCRIPTION
  45. .B dlopen
  46. loads a dynamic library from the file named by the null terminated
  47. string
  48. .I filename
  49. and returns an opaque "handle" for the dynamic library.
  50. If
  51. .I filename
  52. is not an absolute path (i.e., it does not begin with a "/"), then the
  53. file is searched for in the following locations:
  54. .RS
  55. .PP
  56. A colon-separated list of directories in the user's
  57. \fBLD_LIBRARY\fP path environment variable.
  58. .PP
  59. The list of libraries specified in \fI/etc/ld.so.cache\fP.
  60. .PP
  61. \fI/usr/lib\fP, followed by \fI/lib\fP.
  62. .RE
  63. .PP
  64. If
  65. .I filename
  66. is a NULL pointer, then the returned handle is for the main program.
  67. .PP
  68. External references in the library are resolved using the libraries
  69. in that library's dependency list and any other libraries previously
  70. opened with the
  71. .B RTLD_GLOBAL
  72. flag.
  73. If the executable was linked
  74. with the flag "-rdynamic", then the global symbols in the executable
  75. will also be used to resolve references in a dynamically loaded
  76. library.
  77. .PP
  78. .I flag
  79. must be either
  80. .BR RTLD_LAZY ,
  81. meaning resolve undefined symbols as code from the dynamic library is
  82. executed, or
  83. .BR RTLD_NOW ,
  84. meaning resolve all undefined symbols before
  85. .B dlopen
  86. returns, and fail if this cannot be done.
  87. Optionally,
  88. .B RTLD_GLOBAL
  89. may be or'ed with
  90. .IR flag,
  91. in which case the external symbols defined in the library will be
  92. made available to subsequently loaded libraries.
  93. .PP
  94. If the library exports a routine named
  95. .BR _init ,
  96. then that code is executed before dlopen returns.
  97. If the same library is loaded twice with
  98. .BR dlopen() ,
  99. the same file handle is returned. The dl library maintains link
  100. counts for dynamic file handles, so a dynamic library is not
  101. deallocated until
  102. .B dlclose
  103. has been called on it as many times as
  104. .B dlopen
  105. has succeeded on it.
  106. .PP
  107. If
  108. .B dlopen
  109. fails for any reason, it returns NULL.
  110. A human readable string describing the most recent error that occurred
  111. from any of the dl routines (dlopen, dlsym or dlclose) can be
  112. extracted with
  113. .BR dlerror() .
  114. .B dlerror
  115. returns NULL if no errors have occurred since initialization or since
  116. it was last called. (Calling
  117. .B dlerror()
  118. twice consecutively, will always result in the second call returning
  119. NULL.)
  120. .B dlsym
  121. takes a "handle" of a dynamic library returned by dlopen and the null
  122. terminated symbol name, returning the address where that symbol is
  123. loaded. If the symbol is not found,
  124. .B dlsym
  125. returns NULL; however, the correct way to test for an error from
  126. .B dlsym
  127. is to save the result of
  128. .B dlerror
  129. into a variable, and then check if saved value is not NULL.
  130. This is because the value of the symbol could actually be NULL.
  131. It is also necessary to save the results of
  132. .B dlerror
  133. into a variable because if
  134. .B dlerror
  135. is called again, it will return NULL.
  136. .PP
  137. .B dladdr
  138. returns information about the shared library containing the memory
  139. location specified by
  140. .IR address .
  141. .B dladdr
  142. returns zero on success and non-zero on error.
  143. .PP
  144. .B dlclose
  145. decrements the reference count on the dynamic library handle
  146. .IR handle .
  147. If the reference count drops to zero and no other loaded libraries use
  148. symbols in it, then the dynamic library is unloaded. If the dynamic
  149. library exports a routine named
  150. .BR _fini ,
  151. then that routine is called just before the library is unloaded.
  152. .SH EXAMPLES
  153. .B Load the math library, and print the cosine of 2.0:
  154. .RS
  155. .nf
  156. .if t .ft CW
  157. #include <dlfcn.h>
  158. int main(int argc, char **argv) {
  159. void *handle = dlopen ("/lib/libm.so", RTLD_LAZY);
  160. double (*cosine)(double) = dlsym(handle, "cos");
  161. printf ("%f\\n", (*cosine)(2.0));
  162. dlclose(handle);
  163. }
  164. .if t .ft P
  165. .fi
  166. .PP
  167. If this program were in a file named "foo.c", you would build the program
  168. with the following command:
  169. .RS
  170. .LP
  171. gcc -rdynamic -o foo foo.c -ldl
  172. .RE
  173. .RE
  174. .LP
  175. .B Do the same thing, but check for errors at every step:
  176. .RS
  177. .nf
  178. .if t .ft CW
  179. #include <stdio.h>
  180. #include <dlfcn.h>
  181. int main(int argc, char **argv) {
  182. void *handle;
  183. double (*cosine)(double);
  184. char *error;
  185. handle = dlopen ("/lib/libm.so", RTLD_LAZY);
  186. if (!handle) {
  187. fputs (dlerror(), stderr);
  188. exit(1);
  189. }
  190. cosine = dlsym(handle, "cos");
  191. if ((error = dlerror()) != NULL) {
  192. fputs(error, stderr);
  193. exit(1);
  194. }
  195. printf ("%f\\n", (*cosine)(2.0));
  196. dlclose(handle);
  197. }
  198. .if t .ft P
  199. .fi
  200. .RE
  201. .SH ACKNOWLEDGEMENTS
  202. The dlopen interface standard comes from Solaris.
  203. The Linux dlopen implementation was primarily written by
  204. Eric Youngdale with help from Mitch D'Souza, David Engel,
  205. Hongjiu Lu, Andreas Schwab and others.
  206. The manual page was written by Adam Richter.
  207. .SH SEE ALSO
  208. .BR ld(1) ,
  209. .BR ld.so(8) ,
  210. .BR ldconfig(8) ,
  211. .BR ldd(1) ,
  212. .BR ld.so.info .