[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How does a shared library work?



On Wed, Oct 26, 2005 at 10:18:38AM +0200, Tshepang Lekhonkhobe wrote:
>Hello,
>I wanted to avoid some searching headache by asking this question here:
>I hear that the advantage of shared libraries is that there's only one
>copy of that library in memory and I assume that the library is loaded
>in memory only the first moment that a dependant package is run. But
>who's reponsible for making sure whose turn it is to use that library
>in case of several dependant packages requiring its use? Is it the
>kernel?

Now, I'm no expert on this, so if anyone can elaborate more please do
so.

There are two levels of sharing of libraries. They can be shared in the
sense that two binary executables load the same file from disk, and they
can be shared in the sense that two processes share the memory pages
where a library is loaded. I assume the latter is more interesting for
you. (IIRC you get the former type by not using -fPIC when compiling the
object files.)

When the kernel loads the executable binary from disk it first performs
some stuff related to the ELF data in the binary, then it hands things
over to the runtime linker, /lib/ld-linux.so. The runtime linker maps
share libraries to the process memory, performs some relocations and
then returns execution to the program's entry point.

Each ELF binary consists of sections, roughly they can be separated into
executable sections (containing code) and data sections. When two
processes share a loaded library they share the code sections, while the
data sections are per-process. The resource they share is read-only,
hence no risk of them stepping on each other's toes.

/M

-- 
Magnus Therning                    (OpenPGP: 0xAB4DFBA4)
magnus@therning.org
http://therning.org/magnus

Software is not manufactured, it is something you write and publish.
Keep Europe free from software patents, we do not want censorship
by patent law on written works.

early every electrical engineer believes deep in his heart that he is
a better at writing computer programs than any computer programmer,
and can show as proof the fact that he has written a number of small
applications, each of which was done quickly, easily, and exactly met
his needs.

Attachment: pgpCk45D2F1X6.pgp
Description: PGP signature


Reply to: