The Linux Programming Interface

(nextflipdebug5) #1

842 Chapter 41


Figure 41-1 shows the compilation and linking steps involved in producing a shared
library with an embedded soname, linking a program against that shared library,
and creating the soname symbolic link needed to run the program.

Figure 41-1: Creating a shared library and linking a program against it

Figure 41-2 shows the steps that occur when the program created in Figure 41-1 is
loaded into memory in preparation for execution.

To find out which shared libraries a process is currently using, we can list the
contents of the corresponding Linux-specific /proc/PID/maps file (Section 48.5).

ELF header
(other info)
soname=libbar.so
(other info)

$ gcc -g -c \
-fPIC -Wall \
mod1.c mod2.c mod3.c

$ gcc -shared -o libfoo.so \
-Wl,-soname,libbar.so \
mod1.o mod2.o mod3.o

$ gcc -o prog \
prog.c libfoo.so

mod1.o code

mod1.o

libfoo.so

mod2.o code

mod2.o

mod3.o code

mod3.o

prog.o code

prog libbar.so

mod2.o code

$ ln -s libfoo.so \
libbar.so

mod1.o code

mod3.o code

Program header “libfoo.so”
Shared object dependencies:
/lib/ld-linux.so.2
libbar.so

1

(^34)
2

Free download pdf