12
Core Linux for Red Hat and Fedora learning under GNU Free Documentation License - Copyleft (c) Acácio Oliveira 2012 Everyone is permitted to copy and distribute verbatim copies of this license document, changing is allowed Linux Essenciais and System Administration

101 2.3 manage shared libraries

Embed Size (px)

Citation preview

Page 1: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Linux Essenciais and System Administration

Page 2: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Key Knowledge Areas

Identify shared libraries. Identify the typical locations of system libraries. Load shared libraries.

Linux Installation and Package Management

Manage shared libraries

Terms and Utilities

ldd ldconfig /etc/ld.so.conf LD_LIBRARY_PATH

2

Page 3: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

When source code is written, programmers do not rewrite code for common operations.

These operations are written once and used again by many other programmers. These common operations are stored in what are called libraries.

For an application to successfully compile and run, it needs access to the libraries it waswritten to use.

Dependencies of executable programs on shared libraries

3

Statically compiled applications.Some source code compiles libraries into final executable - Dont need the libraries to run.

dynamically compiled applications.Applications linked to the libraries during compile and must access to the library files later.

statically compiling makes the application larger than a dynamically compiled version.

Page 4: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Ex. on many Linux systems is ln cmd (/bin/ln), which creates links between files, either hard links or soft (or symbolic) links.

This cmd uses shared libraries. Shared libraries involve symbolic links between a generic name for the library and a specific level of the library, if not present or broken, the ln command itself might be inoperative.

Progs with dynamically shared libraries and statically linked versions

4

To protect against this possibility, some Linux systems include a statically linked version of the ln program as the sln program (/sbin/sln).

Example: Fedora12 64-bit - difference in size between dynamically linked ln and statically linked sln.

yourname@yourcomp~$ ls -l /sbin/sln /bin/ln

-rwxr-xr-x. 1 root root 47384 2010-01-12 09:35 /bin/ln-rwxr-xr-x. 1 root root 603680 2010-01-04 09:07 /sbin/sln

Ex:

Statically linked programs are likely to be larger

Page 5: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Linux’s shared libraries are stored in several paths:

/lib Main shared libraries/usr/lib Supplemental libraries/usr/X11R6/lib Shared libraries for X Window

Path and Naming convention

5

Shared libraries naming convention:libraryname-major-minor-patch.so (so extension stands for shared object)

Example: libcrypt-2-1-3.soname of library: libcrypt. major version: 2 minor version: 1 patch level: 3

In many cases there’s symbolic links to the shared library. These are named as:

libraryname.so libraryname.so.major

Example: libcrypt.so libcrypt.so.2

These links allow software to point to them instead of the actual file. This way the minor version and patch level can change without breaking the association.

Page 6: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Linux systems run on hardware that supports 32-bit and 64-bit executables. Many libraries are thus compiled in 32-bit and 64-bit versions.

ldd Output: Viewing required shared libraries

6

64-bit versions are stored under /lib64; 32-bit versions live in the traditional /lib tree.Libraries /lib64/libc-2.11.1.so and /lib/libc-2.11.1.so allow C Progs of 64-bit and 32-bit to run on Linux 64-bit System.

To see which libraries an application uses, use command: ldd file_name

~/usr/bin# ldd wget

libc.so.6 => /lib/libc.so.6 (0x40020000)/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Ex:

wget uses libraries libc.so version 6 and ld-linux.so version 2.

Page 7: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

ldd Output: Dependencies between libraries

7

Left hand column indicates what the program would like to be linked to (no path name given), Right hand column shows what the program will actually be linked to (full path and often a symbolic link)

~/usr/bin# ldd /bin/bash

libtermcap.so.2 => /lib/libtermcap.so.2 (0x40024000)libdl.so.2 => /lib/libdl.so.2 (0x40028000)libc.so.6 => /lib/libc.so.6 (0x4002b000)/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Ex:

When programs are loaded into memory, part of loading process is to make sure that relevant shared libraries are loaded when the program loads.

A library can require other libraries. Dependencies are normal for large applications. A program may use a graphics library, which can use the X11 library, which in turn uses the C library.

~$ ls -lg /lib/libc.so.6lrwxrwxrwx 1 root 14 Oct 14 10:47 /lib/libc.so.6 -> libc-2.2.93.so

~$ ls -lg /lib/libc-2.2.93.so-rwxr-xr-x 1 root 1327065 Sep 6 2002 /lib/libc-2.2.93.so

bash asked to link to libc.so.6, and it ended up linking to /lib/libc2.2.93.so.

Page 8: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

ldd Output: types of information

8

Ex: In Fedora 12 64-bit system (echidna). ln cmd needs 3 shared libraries:

~$ ldd /bin/lnlinux-vdso.so.1 => (0x00007fff644af000)libc.so.6 => /lib64/libc.so.6 (0x00000037eb800000)/lib64/ld-linux-x86-64.so.2 (0x00000037eb400000)

1.(linux-vdso.so.1) is the Linux Virtual Dynamic Shared Object.Necessary logic to allow user programs to access system functions through fastest means available on the processor.Either by interrupt, or for most newer CPUs by fast system call.

Types of ldd info: (1) Linux Virtual Dynamic Shared Objects; (2) Pointers; (3) Absolute paths.

2.(libc.so.6) has a pointer to /lib64/libc.so.6.

3.(/lib64/ld-linux-x86-64.so.2) is the absolute path to another library.

~$ ls -l /lib64/libc.so.6 /lib64/ld-linux-x86-64.so.2lrwxrwxrwx. 1 root root 12 2010-01-14 14:24 /lib64/ld-linux-x86-64.so.2 -> ld-2.11.1.solrwxrwxrwx. 1 root root 14 2010-01-14 14:24 /lib64/libc.so.6 -> libc-2.11.1.so

ls -l cmd shows that last 2 libraries are, in turn, symbolic links to specific versions of the libraries.

Page 9: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Configuring shared libraries

9

If a shared library is installed manually by editing the LIBRARY CONFIGURATION FILE , you must manually inform the system to use the new libraries. (with ldconfig)

The LIBRARY CONFIGURATION FILE is in:/etc/ld.so.conf and /etc/ld.so.cache - contains the directories to be searched for shared libraries.

/etc/ld.so.conf specifies that all the .conf files from subdirectory ld.so.conf.d should be included. Older systems may have all entries in /etc/ld/so/conf and not include entries from /etc/ld.so.conf.d

Ex: ~$ cat /etc/ld.so.conf

include ld.so.conf.d/*.conf

~$ ls /etc/ld.so.conf.d/*.conf

/etc/ld.so.conf.d/kernel-2.6.31.12-174.2.19.fc12.x86_64.conf/etc/ld.so.conf.d/kernel-2.6.31.12-174.2.22.fc12.x86_64.conf/etc/ld.so.conf.d/kernel-2.6.31.12-174.2.3.fc12.x86_64.conf/etc/ld.so.conf.d/mysql-x86_64.conf/etc/ld.so.conf.d/qt-x86_64.conf/etc/ld.so.conf.d/tix-x86_64.conf/etc/ld.so.conf.d/xulrunner-64.conf

Page 10: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Configuring shared libraries

10

Command ldconfig Processes ld.so.conf – including the file libraries from the file, and from the trusted directories: /lib and /usr/lib, and creates the ld.so.cache.

Ex: ~$

In summary - The ld.so.cache file is created from the ld.so.conf file by using ldconfig.

Any time that ld.so.conf file is changed, the cache file needs to be updated - Using ldconfig

Page 11: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Manage shared libraries

Loading and setting specific libraries paths

11

Applications can be made to load libraries from directories not listed in /etc/ld.so.conf older apps that need specific library, developing versions of shared libraries, running scripts with product-specific shared libraries installed in /opt.

If the library is not in the standard path, you can search and add the nonstandard path with:LD_LIBRARY_PATH environment variable.

export LD_LIBRARY_PATH=/usr/lib/oldstuff:/opt/IBM/AgentController/lib

If there’s more than 1 list of directories to search, separate them by colon.

Export LD_LIBRARY_PATH=/usr/newpath

LD_PRELOAD: Specifies libraries containing symbols that override symbols in the real libraries. LD_PRELOAD is seldom used except for debugging and compatibility applications (SOCKS proxy support).

Add the /usr/newpath to the list of searched directories for libraries.

Ex:

Ex:

Page 12: 101 2.3 manage shared libraries

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Fim de sessão

12