Order of object initialization across libraries

At run time, once all modules in an application have been loaded, the modules are initialized in their order of priority (the executable program containing the main function is always assigned a priority of 0). When objects are initialized within a library, the order of initialization follows the rules outlined in Assigning priorities to objects. If objects do not have priorities assigned, or have the same priorities, object files are initialized in random order, and the objects within the files are initialized according to their declaration order. Objects are terminated in reverse order of their construction.

Each static library and shared library is loaded and initialized at run time in reverse link order, once all of its dependencies have been loaded and initialized. Link order is the order in which each library was listed on the command line during linking into the main application. For example, if library A calls library B, library B is loaded before library A.

As each module is loaded, objects are initialized in order of priority, according to the rules outlined in Assigning priorities to objects. If objects do not have priorities assigned, or have the same priorities, object files are initialized in reverse link order -- where link order is the order in which the files were given on the command line during linking into the library -- and the objects within the files are initialized according to their declaration order. Objects are terminated in reverse order of their construction.

Example of object initialization across libraries

In this example, the following modules are used:

The dependent libraries are created with the following command strings:

xlC -qmkshrobj -o libS3 fileE.o fileF.o 
xlC -qmkshrobj -o libS4 fileG.o fileH.o
xlC -qmkshrobj -o libS5 fileI.o fileJ.o
xlC -qmkshrobj -o libS6 fileK.o fileL.o

The dependent libraries are linked with their parent libraries using the following command strings:

xlC -qmkshrobj libS1 fileA.o fileB.o -L. -lS3 -lS4
xlC -qmkshrobj libS2 fileC.o fileD.o -L. -lS5 -lS6

The parent libraries are linked with the main program with the following command string:

xlC main.c -o main.out -L. -lS1 -lS2

The following diagram shows the initialization order of the shared libraries.

Figure 1. Object initialization order on Linux

Object initialization order on Linux

Objects are initialized as follows:

Sequence Object Comment
1 libS6 libS2 was entered last on the command line when linked with main, and so is initialized before libS1. However, libS5 and libS6 are dependencies of libS2, so they are initialized first. Since it was entered last on the command line when linked with libS2, libS6 is initialized first. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileL are initialized before those in fileK, because fileL was listed last on the command line when the object files were linked into libS6.)
2 libS5 libS5 was entered before libS6 on the command line when linked with libS2, so it is initialized next. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileJ are initialized before those in fileI, because fileJ was listed last on the command line when the object files were linked into libS5.)
3 libS4 libS4 is a dependency of libS1 and was entered last on the command line during the linking with libS1, so it is initialized next. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileH are initialized before those in fileG, because fileH was listed last on the command line when the object files were linked into libS4.)
4 libS3 libS3 is a dependency of libS1 and was entered first on the command line during the linking with libS1, so it is initialized next. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileF are initialized before those in fileE, because fileF was listed last on the command line when the object files were linked into libS3.)
5 libS2 libS2 is initialized next. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileD are initialized before those in fileC, because fileD was listed last on the command line when the object files were linked into libS2.)
6 libS1 libS1 is initialized next. The objects in this library are initialized according to their priority. (If no priorities are assigned, the objects in fileB are initialized before those in fileA, because fileB was listed last on the command line when the object files were linked into libS1.)
7 main.out Initialized last. The objects in main.out are initialized according to their priority.

IBM Copyright 2003