ld.so.texi 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename ld.so.info
  4. @settitle ld.so : Dynamic-Link Library support
  5. @c %**end of header
  6. @ifinfo
  7. This file documents the dynamic-link support libraries and utilities for the
  8. Linux OS, version 1.8.1.
  9. Copyright 1996 Michael Deutschmann
  10. This document is subject to the GNU General Public License as published by
  11. the Free Software foundation, version 2 or later (your choice).
  12. Note: The software described in this document is under a different copyright
  13. and license.
  14. @end ifinfo
  15. @titlepage
  16. @title ld.so
  17. @subtitle Dynamic Link library support for the Linux OS.
  18. @author David Engel
  19. @author Eric Youngdale
  20. @author Peter Macdonald
  21. @author Hongjiu Lu
  22. @author Mitch D'Souza
  23. @author Michael Deutschmann (this documentation)
  24. @page
  25. Copyright @copyright{} 1996 Michael Deutschmann
  26. This document is subject to the GNU General Public License as published by
  27. the Free Software foundation, version 2 or later (your choice).
  28. Note: The software described in this document is under a different copyright
  29. and license.
  30. @end titlepage
  31. @ifinfo
  32. @node Top
  33. @top
  34. The @code{ld.so} module provides dynamic linked library support in Linux.
  35. This file documents @code{ld.so} and its companion software.
  36. @menu
  37. * intro:: Introduction
  38. * ld.so:: The dynamic linker core program
  39. * ldd:: A utility to print out dependencies
  40. * ldconfig:: A utility to maintain the cache and symlinks
  41. * libdl:: Manual dynamic linking library
  42. @end menu
  43. @end ifinfo
  44. @node intro
  45. @unnumbered Introduction
  46. The @code{ld.so} suite contains special files and utilities needed for linux
  47. to handle @dfn{dynamic libraries}.
  48. Ordinary static libraries (@file{lib*.a} files) are included into executables
  49. that use their functions. A file that only uses static libraries needs less
  50. intelligence to load, but takes up more space. If many executables use the
  51. same library, there can be much wastage of storage space, since multiple
  52. copies of the library functions are scattered across the executables.
  53. However, static libraries are easier to make.
  54. Dynamic libraries (@file{lib*.so*} files) are not copied into executables ---
  55. the executable is written in such a way that it will automatically load the
  56. libraries. In linux, the executable will first load the special library
  57. @code{ld.so} or @code{ld-linux.so}, which contains the intelligence
  58. to load further dynamic libraries. Since multiple files end up getting
  59. executable data from the same file, dynamic libraries are also known as
  60. shared libraries.
  61. Linux executables come in two flavors, @sc{elf} and a.out.
  62. a.out is the original executable format used by Linux. It has somewhat less
  63. overhead than @sc{elf}. However creating shared libraries for a.out is
  64. @emph{very} involved, and each a.out shared library must be explicitly
  65. registered.
  66. @sc{elf} is a more recent format, which supports a much simpler method of
  67. creating libraries. @sc{elf} libraries may also be linked manually
  68. (@pxref{libdl}).
  69. Since many library authors prefer @sc{elf} and no longer release shared a.out
  70. libraries, a.out is moribund on Linux. This version of the @code{ld.so} can
  71. be compiled to support only @sc{elf}, or to support both formats. (The last
  72. release of ld.so to support a.out alone was 1.8.0.)
  73. @node ld.so
  74. @chapter @code{ld.so}: Dynamic linker core
  75. @code{ld.so} works behind the scenes to handle dynamic libraries in Linux.
  76. Users will almost never have to deal with it directly, but in special cases
  77. one can send instructions to it through environment variables. Also, if
  78. something is wrong with your libraries (usually an incorrect version) ld.so
  79. will give error messages.
  80. Actually @code{ld.so} is the a.out linker. The new @sc{elf} executables are
  81. handled by a related program @code{ld-linux.so}.
  82. @menu
  83. * files:: Configuration files used by the suite
  84. * environment:: Environment settings that tweak @code{ld.so}
  85. * errors:: Complaints @code{ld.so} might make
  86. @end menu
  87. @node files
  88. @section Configuration Files
  89. @table @file
  90. @item /etc/ld.so.cache
  91. A file created by @code{ldconfig} and used to speed linking. It's structure
  92. is private to the suite.
  93. @item /etc/ld.so.conf
  94. A simple list of directories to scan for libraries, in addition to
  95. @file{/usr/lib} and @file{/lib}, which are hardwired. It may contain
  96. comments started with a @samp{#}.
  97. @item /etc/ld.so.preload
  98. A list of libraries to preload. This allows preloading libraries for
  99. setuid/setgid executables securely. It may contain comments.
  100. @end table
  101. @node environment
  102. @section Environment Variables
  103. @table @code
  104. @item LD_AOUT_LIBRARY_PATH
  105. @itemx LD_LIBRARY_PATH
  106. These variables supply a library path for finding dynamic libraries, in the
  107. standard colon seperated format. These variables are ignored when executing
  108. setuid/setgid programs, because otherwise they would be a security hazard.
  109. @code{ld.so} will use @code{LD_AOUT_LIBRARY_PATH} and @code{ld-linux.so} will
  110. use @code{LD_LIBRARY_PATH}.
  111. @item LD_AOUT_PRELOAD
  112. @itemx LD_PRELOAD
  113. These variables allow an extra library not specified in the executable to be
  114. loaded. Generally this is only useful if you want to override a function.
  115. These are also ignored when running setuid/setgid executables. @code{ld.so}
  116. will use @code{LD_AOUT_PRELOAD} and @code{ld-linux.so} will use
  117. @code{LD_PRELOAD}.
  118. @item LD_NOWARN
  119. If non-empty, errors about incompatible minor revisions are suppressed.
  120. @item LD_KEEPDIR
  121. If non-empty, allow executables to specify absolute library names. This
  122. option is deprecated.
  123. @c FIXME:
  124. @c The following are things I noticed in the ld-linux.so source.
  125. @c I don't really understand 'em. Could someone help me?
  126. @c
  127. @c @item LD_BIND_NOW
  128. @c This option is used by the @code{ld-linux.so} only. I don't know
  129. @c what it does. (I suspect, looking at the code, that it specifies
  130. @c "RTLD_NOW" rather than "RTLD_LAZY" mode for the shared libraries.)
  131. @c
  132. @c @item LD_TRACE_LOADED_OBJECTS
  133. @c @itemx LD_WARN
  134. @c These seem to have something to do with the communication between the
  135. @c @code{ld-linux.so} and @code{ldd}. I don't know more.
  136. @end table
  137. @node errors
  138. @section Errors
  139. @table @samp
  140. @item Can't find library @var{library}
  141. The executable required a dynamically linked library that ld.so cannot find.
  142. Your symbolic links may be not set right, or you may have not installed a
  143. library needed by the program.
  144. @item Can't load library @var{library}
  145. The library is corrupt.
  146. @item Incompatible library @var{library}
  147. @itemx Require major version @var{x} and found @var{y}
  148. Your version of the library is incompatible with the executable. Recompiling
  149. the executable, or upgrading the library will fix the problem.
  150. @item using incompatible library @var{library}
  151. @itemx Desire minor version >= @var{x} and found @var{y}.
  152. Your version of the library is older than that expected by the executable,
  153. but not so old that the library interface has radically changed, so the
  154. linker will attempt to run anyway. There is a chance that it will work, but
  155. you should upgrade the library or recompile the software. The environment
  156. variable @code{LD_NOWARN} can be used to supress this message.
  157. @item too many directories in library path
  158. The linker only supports up to 32 library directories. You have too many.
  159. @item dynamic linker error in @var{blah}
  160. The linker is having trouble handling a binary - it is probably corrupt.
  161. @item can't map cache file @var{cache-file}
  162. @itemx cache file @var{cache-file} @var{blah}
  163. The linker cache file (generally @file{/etc/ld.so.cache}) is corrupt or
  164. non-existent. These errors can be ignored, and can be prevented by
  165. regenerating the cache file with @code{ldconfig}.
  166. @end table
  167. @node ldd
  168. @chapter @code{ldd}: Dependency scanner
  169. @code{ldd} is a utility that prints out the dynamic libraries that an
  170. executable is linked to.
  171. Actually @code{ldd} works by signalling ld.so to print the dependencies.
  172. For a.out executables this is done by starting the executable with
  173. @code{argc} equal to 0. The linker detects this and prints the dependencies.
  174. (This can cause problems with @emph{very} old binaries, which would run as
  175. normal only with an inappropriate @code{argc}.)
  176. For @sc{elf} executables, special environment variables are used to tell the
  177. linker to print the dependencies.
  178. @code{ldd} has a few options:
  179. @table @samp
  180. @item -v
  181. Print the version number of @code{ldd} itself
  182. @item -V
  183. Print the version number of the dynamic linker
  184. @item -d
  185. Report missing functions. This is only supported for @sc{elf} executables.
  186. @item -r
  187. Report missing objects. This is also only available for @sc{elf}
  188. executables.
  189. @end table
  190. @node ldconfig
  191. @chapter @code{ldconfig}: Setup program
  192. This utility is used by the system administrator to automatically set up
  193. symbolic links needed by the libraries, and also to set up the cache file.
  194. @code{ldconfig} is run after new dynamic libraries are installed, and if the
  195. cache file or links are damaged. It is also run when upgrading the
  196. @code{ld.so} suite itself.
  197. The @file{/lib} and @file{/usr/lib} directories, and any listed in the file
  198. @file{/etc/ld.so.conf} are scanned by default unless @samp{-n} is used.
  199. Additional directories may be specified on the command line.
  200. It has the following options:
  201. @table @samp
  202. @item -D
  203. Enter debug mode. Implies @samp{-N} and @samp{-X}.
  204. @item -v
  205. Verbose. Print out links created and directories scanned.
  206. @item -n
  207. Check directories specified on the commandline @emph{only}.
  208. @item -N
  209. Do not regenerate the cache.
  210. @item -X
  211. Do not rebuild symbolic links.
  212. @item -l
  213. Set up symbolic links for only libraries presented on the command line.
  214. @item -p
  215. Print out the library pathnames in the cache file (@file{/etc/ld.so.cache})
  216. @end table
  217. @node libdl
  218. @chapter User dynamic linking library
  219. The @code{ld.so} package includes a small library of functions
  220. (@code{libdl}) to allow manual dynamic linking. Normally programs are linked
  221. so that dynamic functions and objects are automagically available. These
  222. functions allow one to manually load and access a symbol from a library.
  223. They are only available for @sc{elf} executables.
  224. @menu
  225. * using libdl:: General points
  226. * functions:: How to use the functions
  227. * example:: A sample program
  228. @end menu
  229. @node using libdl
  230. @section Overview
  231. To access this library, add the flag @samp{-ldl} to your compile command when
  232. linking the executable. You also must include the header file
  233. @code{dlfcn.h}. You may also need the flag @samp{-rdynamic}, which enables
  234. resolving references in the loaded libraries against your executable.
  235. Generally, you will first use @code{dlopen} to open a library. Then you use
  236. @code{dlsym} one or more times to access symbols. Finally you use
  237. @code{dlclose} to close the library.
  238. These facilities are most useful for language interpreters that provide
  239. access to external libraries. Without @code{libdl}, it would be neccessary
  240. to link the interpreter executable with any and all external libraries
  241. needed by the programs it runs. With @code{libdl}, the interpreter only
  242. needs to be linked with the libraries it uses itself, and can dynamically
  243. load in additional ones if programs need it.
  244. @node functions
  245. @section Functions
  246. @deftypefun void *dlopen ( const char @var{filename}, int @var{flags} )
  247. This function opens the dynamic library specified by @var{filename}
  248. and returns an abstract handle, which can be used in subsequent calls to
  249. @code{dlsym}. The function will respect the @code{LD_ELF_LIBRARY_PATH} and
  250. @code{LD_LIBRARY_PATH} environment variables.
  251. @end deftypefun
  252. The following flags can be used with @code{dlopen}:
  253. @deftypevr Macro int RTLD_LAZY
  254. Resolve symbols in the library as they are needed.
  255. @end deftypevr
  256. @deftypevr Macro int RTLD_NOW
  257. Resolve all symbols in the library before returning, and fail if not all can
  258. be resolved. This is mutually exclusive with @code{RTLD_LAZY}.
  259. @end deftypevr
  260. @deftypevr Macro int RTLD_GLOBAL
  261. Make symbols in this library available for resolving symbols in other
  262. libraries loaded with @code{dlopen}.
  263. @end deftypevr
  264. @deftypefun int dlclose ( void *@var{handle} )
  265. This function releases a library handle.
  266. Note that if a library opened twice, the handle will be the same. However,
  267. a reference count is used, so you should still close the library as many
  268. times as you open it.
  269. @end deftypefun
  270. @deftypefun void *dlsym (void *@var{handle},char *@var{symbol-name})
  271. This function looks up the name @var{symbol-name} in the library and returns
  272. it in the void pointer.
  273. If there is an error, a null pointer will be returned. However, it is
  274. possible for a valid name in the library to have a null value, so
  275. @code{dlerror} should be used to check if there was an error.
  276. @end deftypefun
  277. @deftypefun {libdl function} {const char} *dlerror( void )
  278. This function is used to read the error state. It returns a human-readable
  279. string describing the last error, or null, meaning no error.
  280. The function resets the error value each time it is called, so the result
  281. should be copied into a variable. If the function is called more than once
  282. after an error, the second and subsequent calls will return null.
  283. @end deftypefun
  284. @node example
  285. @section Example program
  286. Here is an example program that prints the cosine of two by manually linking
  287. to the math library:
  288. @example
  289. @c The following was snarfed verbatim from the dlopen.3 man file.
  290. #include <stdio.h>
  291. #include <dlfcn.h>
  292. int main(int argc, char **argv) @{
  293. void *handle;
  294. double (*cosine)(double);
  295. char *error;
  296. handle = dlopen ("/lib/libm.so", RTLD_LAZY);
  297. if (!handle) @{
  298. fputs (dlerror(), stderr);
  299. exit(1);
  300. @}
  301. cosine = dlsym(handle, "cos");
  302. if ((error = dlerror()) != NULL) @{
  303. fputs(error, stderr);
  304. exit(1);
  305. @}
  306. printf ("%f\\n", (*cosine)(2.0));
  307. dlclose(handle);
  308. @}
  309. @end example
  310. @contents
  311. @bye