Академический Документы
Профессиональный Документы
Культура Документы
David A. Wheeler
1.20, 11 2003
HOWTO ,
Linux.
(static), (shared) (
dynamically loaded ) .
_________________________________________________________
1.
2. (static)
3. (shared)
3.1.
3.1.1.
3.1.2.
3.2.
3.3.
3.3.1. LD_LIBRARY_PATH
3.3.2. LD_DEBUG
3.3.3.
3.4.
3.5.
3.6.
4. (Dynamically Loaded - DL)
4.1.
4.2.
4.3.
4.4.
4.5.
dlopen()
dlerror()
dlsym()
dlclose()
5.
5.1. nm
5.2. (constructor)
(destructor)
5.2.1. _init _fini
(/)
5.3.
(scripts )
5.4. (Symbol versioning)
(Version scripts)
5.5. GNU libtool
5.6.
5.7.
5.8. C++ C
5.9. C++
5.10. Linux Standard Base (LSB - Linux)
5.11.
(shared)
6.
6.1.
6.2.
6.3.
6.4.
6.5.
6.6.
6.7.
libhello.c
libhello.h
demo_use.c
script_static
script_shared
demo_dynamic.c
script_dynamic
7.
8.
1.
HOWTO
Linux,
GNU. " ''
(compiled) (
),
.
,
(recompilation),
(update).
:
(static), (shared),
(dynamically loaded - DL).
,
(executable)
.
,
. , ,
.
(format)
(
) '
,
.
HOWTO
.
(developers) ,
,
(update)
. ,
. ' , ,
(upgrading)
,
. , ,
. C++
,
"C++ mini- howto".
(dynamically linked
libraries - DLLs)
(shared) , DLL
(DL),
DLL
.
, HOWTO DLLs Linux.
HOWTO
/ GNU/Linux : ELF (Executable and
Linking Format). GCC GNU (GNU GCC
toolset)
ELF - ,
Linux
a.out (
2.6 ). ,
.
,
GNU libtool
,
Linux \ . GNU Libtool
script ( ) ,
(.., )
, , . Linux,
GNU Libtool
HOWTO.
(portable interface) ,
portability wrappers (
). GNU Libtool ,
"libltdl". ,
glib (
glibc) dynamic module loading
( ).
glib
http://developer.gnome.org/doc/API/glib/glib-dynamic-loading-o
f-modules.html. , Linux
HOWTO.
Linux,
HOWTO
.
( "" ) HOWTO
http://www.dwheeler.com/program-library, and it has been
Linux
(http://www.tldp.org). (C) 2000
David A. Wheeler
GPL (Gerneral Public License) -
.
_________________________________________________________
2. (static)
object files ( ) - ,
".a".
ar
(archiver).
,
( ).
, ,
, .
(link to programs),
recompilation
,
.
,
,
.
developers
,
(
,
). ,
ELF
( 1-5%)
,
, .
,
object files ,
:
ar rcs my_library.a file1.o file2.o
object files:
file1.o file2. my_library.a,
my_library.a, .
, ar(1).
, ,
, .
(compilation and linking process)
.
gcc(1) ,
() -l
.
info (info:gcc).
GCC - -l
linker (
..
), '
.
.
-l
, linkning,
.
linker ld(1) ,
-l -L, ,
GCC (1) LD (1)
.
_________________________________________________________
3. (shared)
.
,
,
.
,
Linux :
* (update) ,
,
--
*
,
.
*
_________________________________________________________
3.1.
,
.
,
"soname" ,
(real name), .
.
_________________________________________________________
3.1.1.
,
"soname". soname "lib",
, ".so"
( - ".")
interface
( ,
C "lib").
( (fully-qualified) soname
-
, - soname
(symbolic link)
" " .
"
",
.
soname , (minor
number), , (release
number).
. minor release
(configuration control ),
.
, .
,
, ( "linker
name"), soname
.
.
,
,
soname. ,
,
(
).
,
ldconfig(8). ldconfig
sonames
, cache
/etc/ld.so.cache ( ).
ldconfig linker names (
) -
, linker name
symbolic link " " soname
. linker name
soname,
(linking).
H. J. Lu ldconfig linker
names.
, '
(
) . , ldconfig
,
linker
.
, /usr/lib/libreadline.so.3
soname, ldconfig
(realname)
/usr/lib/libreadline.so.3.0.
linker, /usr/lib/libreadline.so
o
/usr/lib/libreadline.so.3.
_________________________________________________________
3.1.2.
(filesystem).
(open source software) GNU
-
info:standards#Directory_Variables. GNU
, ,
/usr/local/lib
(
/usr/local/bin).
.
(Filesystem Ierarchy
Standards - FHS)
(. http://www.pathname.com/fhs). FHS,
/usr/lib,
/lib
/usr/local/lib.
- GNU
, FHS (
,
). :
( - bugs)
"", "local"
() (/usr/local), ""
,
(package managers)
.
,
/usr/local/libexec (
/usr/libexec). Red-Hat-""
/usr/local/lib "'
" -
/etc/ld.so.conf .
(standard)
/usr/X11R6/lib -Windows, ....
/lib/security PAM modules,
(
).
_________________________________________________________
3.2.
GNU
glibc,
Linux,
ELF program loader
( ) .
Linux,
/lib/ld-linux.so.X ( ).
, ,
.
/etc/ld.so.conf.
"" RedHat , ,
/usr/local/lib
/etc/ld.so.conf. bug (),
/usr/local/lib /etc/ld.so.conf
""
RedHat-derived .
,
,
/etc/ld.so.conf -
(standard
set), (preloading).
, , ""
(patches) -
.
, ,
(.o
) /etc/ld.so.preload;
"-"
.
patches -
.
,
caching.
ldconfig (8) ,
/etc/ld.conf, (sets up)
(dynamic link directoriew),
(standard conventions),
"" /etc/ld.so.cache,
.
.
"" :
ldconfig
DLL , ,
DLL - ldconfig
. ,
, dynamic loader
/etc/ld.so.cache
.
, FreeBSD
3.3.2. LD_DEBUG
GNU C loader
LD_DEBUG. "" dl
.
:
export LD_DEBUG=files
command_to_run
,
(dependencies) , SOs
. LD_DEBUG "bindings",
(symbol binding), "libs"
(library search paths),
"versions",
(version dependencies).
LD_DEBUG "help"
,
. , LD_DEBUG
,
debugging testing.
_________________________________________________________
3.3.3.
,
(loading process) - LD
_ RTLD _.
debugging (loader
process) .
' -
' ,
loader ( GCC).
(DLLs)
setuid/setgid (
permissions set), .
, GNU loader (
),
setuid setgid, (
)
. loader
setuid
setgid
credentials
("") - uid euid
, gid egid ,
setuid/setgid (
)
(linking).
GNU glibc ,
/elf/rtld.c
sysdeps/generic/dl-sysdep.c.
uid gid euid egid,
,
. Unix-
:
setuid/setgid
.
_________________________________________________________
3.4.
. ' , object files
GCC
(flags) -fPIC -fpic.
-fPIC -fpic "
" (position independent code),
-
. soname
-Wl gcc. -Wl
linker (
-soname ) -
-Wl , ,
(white spaces) .
:
gcc -shared -Wl,-soname,your_soname \
-o library_name file_list library_list
, object files
(a.o b.o)
.
debugging (-g - -ggdb
debugging
gdb) (warnings - Wall), , ,
.
(compilation) object files (
-c),
fPIC:
gcc -fPIC -g -c -Wall a.c
gcc -fPIC -g -c -Wall b.c
gcc -shared -Wl,-soname,libmystuff.so.1 \
-o libmystuff.so.1.0.1 a.o b.o -lc
:
* "" (strip) ,
-fomit-frame-pointer
compiler, , .
,
debuggers (
) .
* -fPIC -fpic
. -fPIC - fpic
.
-fPIC ,
-fpic (
, PIC -fPIC
, '
-fpic).
-fpic ,
(platform-dependent),
(globally
visible symbols), . linker
" " (
)
. , -fPIC .
* , gcc
(object file)
"- Wl,
-export-dynamic". ,
(dynamic symbol table)
.
( ELF)
((. ld(1)
).
" ", , DL
-
(...
"" -
symbol, ,
.. 4.5, cos symbol
math ). "
",
.
"- rdynamic" "- Wl,
-export-dynamic", Linux,
ELF (ELF documentation),
"- rdynamic" gcc
-linux .
,
-
,
.
(linking option)
"rpath" ld,
runtime ( )
. gcc,
rpath :
-Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH)
client ,
LD_LIBRARY_PATH (
)
,
.
_________________________________________________________
3.5.
,
.
-l -L .
standard (..
/usr/lib),
. ,
,
... .
(flag) -L gcc
.
" rpath" ( ),
standard.
. ,
LD_LIBRARY_PATH,
.
bash, my_program
:
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH my_program
/,
"" object file
LD_PRELOAD -
( ).
(update)
-
(API),
soname.
,
. ,
soname,
, (.,
".orig"),
wrapper script
() .
, "" ,
,
,
. wrapper script
:
#!/bin/sh
export LD_LIBRARY_PATH=/usr/local/my_lib:$LD_LIBRARY_PATH
exec /usr/bin/my_program.orig $*
""
--- (backwards-compatible)
soname
.
" "
" ".
ldd
(1). , ,
ls :
ldd /bin/ls
,
(
():
sonames
ls ),
.
dependencies
* /lib/ld-linux.so. ( N
1, 2).
.
* libc.so. ( N 6).
C.
C (
),
.
: ldd
. ldd (1),
ldd ( )
( ELF,
LD_TRACE_LOADED_OBJECTS) .
ldd
(
ldd). , , ldd
.
_________________________________________________________
3.6.
binary incompatible
("- ") ,
soname . C,
binary-incompatible :
1. ()
,
2. "" (exported data items)
(:
, '
).
3. (exported function) .
4. interface .
,
binary-compatible .
,
o Application Binary Interface (ABI, "
") , .
,
/ .
,
"" ,
,
( )
,
( ),
. :
(arrays).
C++ (
compiled-in templates / compiled dispatched ),
.
, .
""
,
C++.
, " ",
C++
. (
) C++
binary-compatibilty
("
"), Troll Tech's Technical
FAQ:
1.
( binaries
), ""
(evaluates) SuperClass::virtualFunction()
(compile-time, link-time).
2. virtual member functions
( ),
layout vtbl .
3. data members (
) ,
inline member functions.
4. class ierarchy ( ),
.
5. ' private data members (
),
layout .
6. public protected member functions
inline.
7. public protected member function
inline.
8. inline ,
.
9. ( public,
protected private) member function
(portable) ,
.
,
C++ ,
updates
.
,
Unix-
( Linux)
,
() ,
"" ,
.
_________________________________________________________
4. (Dynamically Loaded - DL)
(, " ") (DL)
()
.
plugins modules,
plugin .
, Pluggable Authentication Modules (PAM),
DL
"" (configure)
.
(interpreters)
,
.
,
just-in-time compiler multi-user dungeon (MUD).
Linux, DL ""
(format) -
object files ,
.
link-time
- ' , API
(Application Programming Interface -
) ,
"" , ,
. C
<dlfcn.h> .
Linux
Solaris
"dlopen()" API. ,
- HP- UX
shl_load (),
DLLs,
Windows
interface.
,
wrapping library
.
glib
modules (DLM, Dynamic Loading of Modules) -
. glib
http://developer.gnome.org/doc/API/glib/glib-dynamic-loading-o
f-modules.html. glib
(documentation), ' .
libltdl,
GNU libtool.
, CORBA
Object Request Broker (ORB).
API Linux Solaris,
.
developers C++
(DL), "C++ dlopen
mini-HOWTO".
_________________________________________________________
4.1. dlopen()
dlopen(3)
. C
:
void * dlopen(const char *filename, int flag);
"/" (
), dlopen()
( ). ,
:
1. (
)
LD_LIBRARY_PATH .
2.
/etc/ld.so.cache (
ld.so.conf).
3. /usr/lib.
:
a.out loader.
loader, ,
/usr/lib, /lib (. man page
ld.so(8)). ' ,
( ),
.
dlopen(), flag
RTLD_LAZY, "
(unresolved symbols)
",
RTLD_NOW, "
dlopen() ,
, ".
RTLD_GLOBAL ,
OR flag,
(external symbols)
. debugging,
RTLD_NOW - RTLD_LAZY
(unresolved references).
RTLD_NOW
( ) -
, RTLD_LAZY
.
(..
),
( ,
, X).
dlopen() "handle"
DL
" " (opaque value). dlopen()
NULL ,
.
dlopen(), file handle.
,
("" - exports) _init,
dlopen().
. ,
"" _init _fini.
,
. ' ,
__attribute__((constructor))
__attribute__((destructor)) (
gcc). 5.2
.
_________________________________________________________
4.2. dlerror()
dlerror
(),
dlopen(), dlsym
(), dlclose ().
( ) dlerror()
NULL, .
_________________________________________________________
4.3. dlsym()
.
DL
dlsym (3),
() .
:
(function
attributes)
__attribute__((constructor)) __attribute__((destructor)).
5.2 .
: dlclose() 0 ,
,
man pages Linux.
_________________________________________________________
4.5.
man page
dlopen(3). math,
2.0,
( ):
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/lib/libm.so.6", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%f\n", (*cosine)(2.0));
dlclose(handle);
}
"foo.c",
build :
gcc -o foo foo.c -ldl
_________________________________________________________
5.
5.1. nm
nm(1)
.
.
nm(1)
, ,
.
(
),
(. -l).
nm (symbol
type)
.
:
(local),
(
global,external).
T (
),
D( ), (
),
U( -
), W(" -weak- ":
,
).
,
,
"-" nm (
) grep
. Bourne shell,
/lib,
/usr/lib, (direct)
/usr/lib, /usr/local/lib "cos" :
nm -o /lib/* /usr/lib/* /usr/lib/*/* \
/usr/local/lib/* 2> /dev/null | grep 'cos$'
nm
info:binutils#nm.
_________________________________________________________
5.2. (constructor)
(destructor)
(initialization) "" (cleanup)
gcc__attribute__((constructor))
gcc__attribute__((destructor)). info pages gcc
. constructors
dlopen() (
main(), load time).
destructors dlclose
( exit() main(),
load time). C
(C prototypes) :
void __attribute__ ((constructor)) my_init(void);
void __attribute__ ((destructor)) my_fini(void);
gcc "- nostartfiles" "-nostdlib".
/ (
).
_________________________________________________________
5.2.1. _init _fini (/)
, _init
_fini,
. ,
,
.
-
(function attributes
constructor/destructor) ' .
_init _fini,
.
module: _init _fini. ``_init ''
(is exported) ,
( dlopen()
). C,
(defined)
_init.
_fini,
( dlclose()
-. dlclose(),
). C
:
void _init(void);
void _fini(void);
,
".o" gcc,
gcc "- nostartfiles".
C (linking)
. ,
"multiple-definition" ("-").
modules
.
Jim Mischel Tim Gentry
_init
_fini,
.
_________________________________________________________
5.3.
(scripts )
GNU loader
(script files)
scripting ,
.
. ,
/usr/lib/libc.so :
/* GNU ld script
Use the shared library, but some functions are only in
the static library, so try that secondarily. */
GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a )
,
texinfo scripts ld linker (ld ).
info:ld#Options
info:ld#Commands,
info:ld#Option Commands.
_________________________________________________________
5.4. (Symbol versioning)
(Version scripts)
, (external
functions) "" (bound)
, .
(out of date),
interface-
,
.
symbol versioning
( ) version scripts (
). symbol versioning,
.
ld (man) vesrion scripts
http://www.gnu.org/manual/ld-2.9.1/html_node/ld_25.html.
_________________________________________________________
5.5. GNU libtool
,
GNU libtool
. GNU libtool
(generic library support
script).
libtool
, . interfaces
object files,
(link libraries),
(link
executables),
(debug
executables),
. libltdl,
(portability wrapper)
(dynamic loading programs).
,
http://www.gnu.org/software/libtool/manual.html
_________________________________________________________
5.6.
debugging, .
, .
object files
, debugging (
). ,
, strip(1)
. strip(1)
-
.
"-s" "-s" GNU ld - "-S"
debugger (
) , "-s"
.
gcc
"-Wl,-S" "-Wl,-s".
,
, ""
.
_________________________________________________________
5.7.
paper Whirlwind Tutorial on Creating Really
Teensy ELF Executables for Linux .
. ,
,
ELF.
_________________________________________________________
5.8. C++ C
C++,
C, C++
C "extern C".
, linker
C. , compilers C++ ""
("mangle") C++ (..,
),
C (
).
C C ++,
"extern C" ,
.
# ifndef (
- ),
, foobar.h,
C C++,
:
/* Explain here what foobar does */
#ifndef FOOBAR_H
#define FOOBAR_H
#ifdef __cplusplus
extern "C" {
#endif
... header code for foobar goes here ...
#ifdef __cplusplus
}
#endif
#endif
_________________________________________________________
5.9. C++
developers KDE
GUI C++
,
(relocations).
. Making C++ ready for the desktop
(by Waldo Bastian) .
_________________________________________________________
5.10. Linux Standard Base (LSB - Linux)
LSB project
(standards)
Linux
Linux.
http://www.linuxbase.org.
LSB- 2002,
Developing LSB-certified applications: Five steps to
binary-compatible Linux applications ' George Kraft IV
(Senior software engineer, IBM's Linux Technology Center).
,
(standardized compatibility
layer) . ,
LSB
C C++
LSB- compliant -
linker
. ,
-
LSB . ,
"lsbcc" C/C++
( lsbcc
- linking environment-
LSB ):
$ CC=lsbcc make myapplication
(or)
$ CC=lsbcc ./configure; make myapplication
lsbappchk
LSB:
$ lsbappchk myapplication
LSB (.. RPM v3.
LSB, add-on software /opt by
default). website LSB
.
_________________________________________________________
5.11.
(shared)
;
, ld
"--whole-archive"
,
.a files
.so .
--whole-archive:
gcc -shared -Wl,-soname,libmylib.$(VER) -o libmylib.so $(OBJECTS) \
-Wl,--whole-archive $(LIBS_TO_LINK) -Wl,--no-whole-archive \
$(REGULAR_LIBS)
ld,
--no-whole-archive,
, gcc
standard .
Kendall Bennett
"".
_________________________________________________________
6.
(static, shared, dynamically
loaded). libhello.c ,
libhello.h . demo_use.c
caller .
scripts (script_static
script_dynamic),
. ,
demo_dynamic.c script_dynamic,
.
_________________________________________________________
6.1. libhello.c
/* libhello.c - demonstrate library use. */
#include <stdio.h>
void hello(void) {
printf("Hello, library world.\n");
}
_________________________________________________________
6.2. libhello.h
/* libhello.h - demonstrate library use. */
void hello(void);
_________________________________________________________
6.3. demo_use.c
/* demo_use.c -- demonstrate direct use of the "hello" routine */
#include "libhello.h"
int main(void) {
hello();
return 0;
}
_________________________________________________________
6.4. script_static
#!/bin/sh
# Static library demo
#
#
#
#
#
#
#
#
#
#
(*demo_function)();
/* All done, close things cleanly */
dlclose(module);
return 0;
}
_________________________________________________________
6.7. script_dynamic
#!/bin/sh
# Dynamically loaded library demo
# Presume that libhello.so and friends have
# been created (see dynamic example).
# Compile demo_dynamic program file into an object file.
gcc -Wall -g -c demo_dynamic.c
#
#
#
#
#
#
queries .
, . HOWTO
Linux
http://www.tldp.org.
* "Executable and Linkable Format", TIS
(Tool Interface Standards) (
"Portable Formats Specification"
). ELF (
Linux GNU gcc),
.
ftp://tsx-11.mit.edu/pub/linux/packages/GCC/ELF.doc.tar.gz
MIT,
-
"hps" -
"ps",
Postscript
.
* "ELF: ", Hongjui
Lu. ELF
Linux GNU gcc,
ftp://tsx-11.mit.edu/pub/linux/packages/GCC/elf.ps.gz.
* ld "Using LD, the GNU Linker",
ld .
http://www.gnu.org/manual/ld-2.9.1.
*
"info", ld
gcc.
_________________________________________________________
8.
Copyright (C) 2000 David .
Wheeler. GNU General Public License (GPL).
.
"",
, :
/
General Public License
Free Software Foundation (
.
-
GNU
2 ,
)
, -
. GNU GPL
.
GNU GPL
- , Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
mirroring sites,
:
* mirrors
o master site,
*
o
url
master
site,
http://www.dwheeler.com/program-library, hypertext
link ,
* (David A. Wheeler) .
bugs .
bugs ,
mirroring. o link master site,
mirror
.
sites
links -
,
updates ,
.
, ,
,
()
.
-
" " (public domain work)
.
,
: "You must cause the modified files to carry prominent
notices stating that you changed the files and the date of any
change". ,
.
,
( David A. Wheeler),
, ,
.