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

Re: Shared libs.



Wolfgang Lux wrote:

[ proposal to implement BSD-like shared libraries ]

> What still remainsw is the access to variables in the shared libraries
> from outside of the library. If these variables were referenced from
> the main program, the dynamic loader could just patch all references
> to those variables in the executable (however, only if the executable
> is not shared itself).  [...]

There had to be some straightforward way to avoid this (as any Unix
faces the same problem), and I just found it out the hard way (by
`nm'ing object files and guessing the pieces the BSD manuals lack).  I
wonder why nobody was able to explain this puzzle to us; probably all
the compiler gurus already left us (bammi? Howard?)... :(

This is how BSDs do it:

A shared library defining global variables accesses them like an
external variable indirectly through its global offset table (which
the runtime linker initializes at run time).  The library's symbol
table contains an entry for the variable indicating (besides its name
and its storage size) that the symbol is a "common" symbol.

When creating a program referencing shared libraries, the linker
allocates all storage marked as "common" in the program's BSS segment,
or (respectively for initialized data) copies the "common" data into
the programs DATA segment.  That way, all of the program's references
to variables defined in shared libraries can be resolved at link time;
no need to patch the text segment at run time!

Easy, isn't it?

Michael
-- 
Email: hohmuth@inf.tu-dresden.de
WWW:   http://www.inf.tu-dresden.de/~mh1/