Shared libraries

Saving space

To better understand how a shared library saves space, compare the space used by a shared library and a non-shared library.

A host shared library resembles a non-shared library in three ways. First, both are archive files. Second, the object code in the library typically defines commonly used text symbols and data symbols. The symbols defined inside, and made visible outside, the library are external symbols. Note that the library may also have imported symbols: symbols that it uses but does not define. Third, the link editor searches the library for these symbols when linking a program to resolve its external references. By resolving the references, the link editor produces an executable version of the program, the a.out file.

NOTE: The link editor on the UNIX system is a static linking tool; static linking requires all symbolic references in a program be resolved before the program may be executed. The link editor uses static linking with both a non-shared library and a shared library.

Although these similarities exist, a shared library differs significantly from a non-shared library. The major differences are related to how the libraries are handled to resolve symbolic references.

To produce an a.out file using a non-shared library, the link editor copies the library code that defines a program's unresolved external reference from the library into appropriate .text and .data sections in the program's object file. In contrast, to produce an a.out file using a shared library, the link editor copies from the shared library into the program's object file only a small amount of code for initialization of imported symbols. For more details on imported symbols, see ``Importing symbols''.) For the bulk of the library code, it creates a special section called .lib in the file that identifies the library code needed at run time and resolves the external references to shared library symbols with their correct values. When the UNIX system executes the resulting a.out file, it uses the information in the .lib section to bring the required shared library code into the address space of the process.

``a.out files created using a non-shared library and a shared library'' depicts the a.out files produced using a non-shared version and a shared version of the standard C library to compile the following program:

      printf( "How do you like this manual?\n" );
      result = strcmp( "I do.", answer );
Notice that the shared version is smaller. ``Processes using an archive and a shared library'' depicts the process images in memory of these two files when they are executed.

a.out files created using a non-shared library and a shared library

Now consider what happens when several a.out files need the same code from a library. When using a non-shared library, each file gets its own copy of the code. This results in duplication of the same code on the disk and in memory when the a.out files are run as processes. In contrast, when a shared library is used, the library code remains separate from the code in the a.out files, as indicated in ``Processes using an archive and a shared library''. This separation enables all processes using the same shared library to reference a single copy of the code.

Processes using an archive and a shared library

Next topic: Increase space usage in memory
Previous topic: Space considerations

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003