how is a shared library file called by two different processes in Linux?

user188276 picture user188276 · Dec 11, 2010 · Viewed 10.5k times · Source

In Linux, I have a shared library file called foo.so When I execute 2 different process p1, p2 that both use foo.so. Does this foo.so get overlapped by those 2 process?

Answer

jweyrich picture jweyrich · Dec 11, 2010

On Unix-based systems (includes Linux), the code segment (.text) may be shared among multiple processes because it's immutable. Is this overlapping you mention?

Basically, each shared library that contains static data (such as global variables) has a Global Offset Table (GOT). On shared libraries, all references to static data (think of global vars) occur via GOT (they're indirect). So even if the code segment is shared among multiple processes, each process has its exclusive mapping of other segments of the shared library, including the respective GOT, whose entries are relocated accordingly.

In short, only code is shared among processes, not data. However, I think constants may be an exception depending on compilation flags.

I also recommend chapter 10, Dynamic Linking and Loading, from the following book: Linkers and Loaders.