dlopen.3 6.1 KB

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