CS 241: Systems Programming Lecture 30. Dynamic Libraries
Fall 2019
- Prof. Stephen Checkoway
1
CS 241: Systems Programming Lecture 30. Dynamic Libraries Fall 2019 - - PowerPoint PPT Presentation
CS 241: Systems Programming Lecture 30. Dynamic Libraries Fall 2019 Prof. Stephen Checkoway 1 Announcements No class on Friday No o ffi ce hours Thursday or Friday of this week 2 Last time Static libraries (or archives) are a way of bundling
Fall 2019
1
No class on Friday No office hours Thursday or Friday of this week
2
Static libraries (or archives) are a way of bundling a collection of object files together
line $ clang -o prog main.o libfoo.a
3
Like static libraries, dynamic libraries start as a collection of object files (.o)
copies the relevant library code/data into the output Unlike static libraries, dynamic libraries are produced by the linker
linker inserts references to the library into the output, but does not copy the library code/data into the output At run time the dynamic linker (the loader) loads the executable and all of its required libraries into memory
4
5
library .o files program .o files
prog libex.a
ar ld
6
library .o files program .o files
prog
ld ld
libex.so
Reference
Programs linked to static libraries
Programs linked to dynamic libraries
data; they need their own copy of the writable data
7
When a library is used by many applications (e.g., libc), which of the following is not a benefit of using a dynamic library as compared to using a static library?
8
When a library is used by only one application, which of the following is not a benefit of using a static library as compared to a dynamic library?
speed
9
Steps
$ clang -fPIC -o a.o a.c
$ clang -fPIC -shared -Wl,-soname=libfoo.so.1 \
Option details
10
Each dynamic library has a soname
soname
parameters) have a different soname
11
Example sonames
On the file system the soname is a symbolic link to the actual library
12
For a given library foo, there are typically two symbolic links
The first symbol link is used at link time, the second at run time The two need not be in the same directory
/lib/x86_64-linux-gnu/libz.so.1.2.11
13
We can also specify a library using a command line option: -l
Using -lblah tells the linker to look for the file
— a static library
— a dynamic library on ELF-based systems
libblah.so is a symlink to libblah.so.1.0.0 which has a soname of libblah.so.1
14
When the compiler searches for files, it looks in a variety of paths
We can add a directory to a specific search path
15
We have a library, foo, we want to link against with
We add
16
When the program starts, the dynamic linker looks at the sonames recorded for all of the binaries and looks for a file with a matching name (which is usually a symlink) and loads that library An additional runtime path can be added to the program at link time by using -Wl,-rpath=⟨path⟩ to add path to the list of directories searched By using the special symbol $ORIGIN we can add a path relative to the directory of the program
17
https://checkoway.net/teaching/cs241/2019-fall/exercises/Lecture-30.html Grab a laptop and a partner and try to get as much of that done as you can!
18