Upload
m3onh0x84
View
235
Download
0
Embed Size (px)
Citation preview
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
1/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Chapter 8
Managing Shared Libraries
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
2/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Objectives
Manage shared libraries
Understand and identify shared libraries and their
locations
Define and implement commands associated with
system libraries
Install and support Multiple Versions of a Shared
Library
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
3/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
MANAGING SHARED LIBRARIES
Programs are built using libraries of functions. There are
two ways to link any library into a program: dynamically
(using sharedlibraries), and statically
Static link: the contents of that library is copied into the
binary executable program file. Thus a statically linked
binary should be able to run on any Linux system without
any supporting files
Dynamic link : executable program contains references tothe libraries and functions. When the program is executed
these linkages are resolved: partially through code that is
linked into program. Program binaries are much smaller
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
4/29
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
5/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
The libc and ld.so/ld-linux.so Libraries
The most important library on a Linux system is libc. Almost
all programs are linked against libc
Also all Linux dynamically linked programs depend on the
ld.so and ld-linux.so libraries If any of these files are damaged or missing then most
commands on the system will NOT work.This includes the
user shells, most shell commands like ls, mv, cp,
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
6/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Updating libc
Recommended way to replace a shared library:
1. Create the new files:
On the same file system as the older versions which you intend to
replace, in a subdirectory or under different names
# cp libc-2.2.4.so /lib
2. Issue the ln -f command :
# cd /lib
# ln f libc-2.2.4.so libc.so.6 You must NOT remove ("rm") the files in one command, intending
to copy or move (cp or mv) the new versions in another step
because the cp and mv commands depend on these libraries
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
7/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Inode Numbers
Inode numbers are identifiers of a file or directory on a filesystem
Inode number is unique for each file or directory in a filesystem
Inode numbers are also called index numbers
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
8/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Inode Numbers
Inode numbers can be displayed using ls command withioption
$ ls i ~
12110 dante 68349 dir3 12118 file3
12115 dante_1 68451 dir4 12119 file4
67773 dir1 12169 file1 68552 practice
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
9/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Linking Files and Directories
Links are used to create alternate names or aliases for files anddirectories on a system
There are two kinds of links:
hard link
symbolic link (orsoftlink)
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
10/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Hard link
Used to link files on the same file system
Files that are hard linked share the same inode number (referto the same data on disk)
Hard links are not used to link directories and cannot cross filesystems.
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
11/29
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
12/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Hard link
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
13/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Symbolic Link
Used to link files ordirectories.
Symbolically linked files do not share a single inode, these links
can cross file system .
A file with symbolically link:
$ls -l Test
-rw-r--r-- 1 torey staff 35 May 8 linktest
lrwxrwxrwx 1 torey staff 8 May 12 symlink--> linktest
$ls -F
linktest
symlink@
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
14/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Symbolic Link
The structure of a symbolic link is as follows:
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
15/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Creating Link
Command Format :ln [-s]
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
16/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Examle Creating Hard Link
$ln /export/home/user2/dante essay
$ls -i /export/home/user2/dante
89532 dante
$ls -i essay
89532 essay
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
17/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Examle Creating Symbolic Link
$ln -s tutor.vi symlink
$ls -l symlink
lrwxrwxrwx 1 torey staff 8 May 9 symlink--->tutor.vi
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
18/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Files Related to Shared Libraries
In the /etc/ directory there are critical files which are neededby the dynamic linking libraries: /etc/ld.so.cache and
/etc/ld.so.conf
ld.so.cache file is an indexof all the shared libraries. This fileis built and updated using the ldconfig command
ld.so.conf file is a list of all the directories on the system where
the default shared libraries are to be found
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
19/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Commands Related to System Libraries
ldconfig maintain the /etc/ld.so.cache
file
ldd view the list of libraries againstwhich a binary is linked
ltrace trace each library function that
is called from a program
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
20/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
ldconfig Command
Syntax : ldconfig [options] lib_dirs Description: Update the ld.so cache file with shared libraries
specified on the command line in lib_dirs
Options :
-p Display the contents of the current cache insteadof recreating it.
-v Verbose mode. Display progress duringexecution
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
21/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
ldd Command
Syntax : ldd programs Example :
Thebash shell requires four shared libraries:
# ldd /bin/bash
libtermcap.so.2 => /lib/libtermcap.so.2 (0x4002c000)
libdl.so.2 => /lib/libdl.so.2 (0x40030000)
libc.so.6 => /lib/i686/libc.so.6 (0x40034000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
22/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Installing and Supporting Multiple Versions of a Shared Library
Generally programs will use the appropriate library againstwhich they were linked
It is also possible for programs to be linked to more specific
versions of a given library , in cases where they must depend
on some specific behavior that is missingfrom future versions
Finally it's possible to override the normal shared library
loading mechanism using LD_* environment variables
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
23/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Installing and Supporting Multiple Versions of a Shared
Library
LD_LIBRARY_PATH
A colon-separated list of directories in which to search for
ELF libraries at execution time. This is similar to the PATH
environment variable.
LD_PRELOAD
A whitespace-separated list of additional, user-specified, ELF
shared libraries to be loaded before all others.
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
24/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Errors Related to Missing or Damaged Shared Libraries
Errors :
- File not found
- error in loading shared libraries: libprintit.so: cannot
open shared object file: No such file or directory
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
25/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Errors Related to Missing or Damaged Shared Libraries
Check to see if the command is a dangling symlink ( use ls -iand follow symlinks until you find a regular file )
Check the file type ( using the file command )
If the file is a script, check the
"hash bang
"
line ( the firstline should be of the form: # !... and should contain a full
path to the appropriate script interpreter's executable)
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
26/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Errors Related to Missing or Damaged Shared Libraries
If the file is dynamically linked, check for missing sharedlibraries, or permissions problems on those libraries or their
parent directories (using the ldd command)
Check your environment for any variables with names like
LD_* (using the printenv or set commands)
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
27/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Errors Related to Missing or Damaged Shared Libraries
These error messages can result from missing shared library
(. so) files. Shared libraries might be linked against other
shared libraries
So, when troubleshooting problems that might involve a
missing shared library: run the ldd command on the
troublesome binary, AND ON EACH OF THE LIBRARIES
TO WHICH THAT BINARY ISLINKED
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
28/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Errors Related to Missing or Damaged Shared Libraries
Errors may be due topermissions problems on any of thelibraries
Errors can also be the result of a corrupted or out-of-date
ld.so.cache file, which can make the entire system unusable.
Remove it and build a new one with ldconfig
In addition, there may be a missing line in the /etc/ld.so.conf
file: check and repair it with a text editor and run the ldconfig
command if necessary
8/9/2019 LPI 101 Ch08 Managing Shared Libraries
29/29
SAIGONLAB 83 Nguyn Th Nh, P9, Q.Tn Bnh, Tp. HCM LPI 102
Summary
Manage shared libraries
Understand and identify shared libraries and their locations
Define and implement commands associated with system
libraries Install and support Multiple Versions of a Shared Library