Formatting changes, and minor rewordings.

This commit is contained in:
Michael Kerrisk 2007-06-15 21:40:11 +00:00
parent 129609356a
commit 861003622f
1 changed files with 27 additions and 22 deletions

View File

@ -52,7 +52,7 @@ The four functions
.BR dlclose (),
.BR dlerror ()
implement the interface to the dynamic linking loader.
.SS "dlerror"
.SS "dlerror()"
The function
.BR dlerror ()
returns a human readable string describing the most recent error
@ -65,7 +65,7 @@ since the last call to
.BR dlerror () .
It returns NULL if no errors have occurred since initialization or since
it was last called.
.SS "dlopen"
.SS "dlopen()"
The function
.BR dlopen ()
loads the dynamic library file named by the null-terminated
@ -83,7 +83,7 @@ Otherwise, the dynamic linker searches for the library as follows
(see
.BR ld.so (8)
for further details):
.IP o
.IP o 4
(ELF only) If the executable file for the calling program
contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
then the directories listed in the DT_RPATH tag are searched.
@ -220,7 +220,7 @@ has been called on it as many times as
.BR dlopen ()
has succeeded on it.
The
.B _init
.BR _init ()
routine, if present, is only called once.
But a subsequent call with
.B RTLD_NOW
@ -230,7 +230,7 @@ may force symbol resolution for a library earlier loaded with
If
.BR dlopen ()
fails for any reason, it returns NULL.
.SS "dlsym"
.SS "dlsym()"
The function
.BR dlsym ()
takes a "handle" of a dynamic library returned by
@ -271,7 +271,7 @@ will find the next occurrence of a function in the search order
after the current library.
This allows one to provide a wrapper
around a function in another shared library.
.SS "dlclose"
.SS "dlclose()"
The function
.BR dlclose ()
decrements the reference count on the dynamic library handle
@ -282,22 +282,24 @@ symbols in it, then the dynamic library is unloaded.
The function
.BR dlclose ()
returns 0 on success, and non-zero on error.
.SS "The obsolete symbols _init and _fini"
.SS "The obsolete symbols _init() and _fini()"
The linker recognizes special symbols
.B _init
and
.BR _fini .
If a dynamic library exports a routine named
.BR _init ,
.BR _init (),
then that code is executed after the loading, before
.BR dlopen ()
returns.
If the dynamic library exports a routine named
.BR _fini ,
.BR _fini (),
then that routine is called just before the library is unloaded.
In case you need to avoid linking against the system startup files,
this can be done by giving gcc the "\-nostartfiles" parameter on
the command line.
this can be done by using the
.BR gcc (1)
.I \-nostartfiles"
command-line option.
.LP
Using these routines, or the gcc
.B \-nostartfiles
@ -338,16 +340,19 @@ The function
takes a function pointer and tries to resolve name
and file where it is located.
Information is stored in the
Dl_info structure:
.I Dl_info
structure:
.sp
.in +0.25i
.nf
typedef struct {
const char *dli_fname;/* Filename of defining object */
void *dli_fbase; /* Load address of that object */
const char *dli_sname;/* Name of nearest lower symbol */
void *dli_saddr; /* Exact value of nearest symbol */
const char *dli_fname; /* Filename of defining object */
void *dli_fbase; /* Load address of that object */
const char *dli_sname; /* Name of nearest lower symbol */
void *dli_saddr; /* Exact value of nearest symbol */
} Dl_info;
.fi
.in
.sp
.BR dladdr ()
returns 0 on error, and non-zero on success.
@ -390,9 +395,7 @@ but not
.BR dlvsym ().
.SH EXAMPLE
Load the math library, and print the cosine of 2.0:
.RS
.nf
.if t .ft CW
#include <stdio.h>
#include <stdlib.h>
@ -424,9 +427,7 @@ main(int argc, char **argv)
dlclose(handle);
exit(EXIT_SUCCESS);
}
.if t .ft P
.fi
.RE
.PP
If this program were in a file named "foo.c", you would build the program
with the following command:
@ -435,8 +436,12 @@ with the following command:
gcc \-rdynamic \-o foo foo.c \-ldl
.RE
.PP
Libraries exporting _init() and _fini() will want to be compiled as
follows, using bar.c as the example name:
Libraries exporting
.BR _init ()
and
.BR _fini ()
will want to be compiled as
follows, using \fIbar.c\fP as the example name:
.RS
.LP
gcc \-shared \-nostartfiles \-o bar bar.c