$ ld -b lib3.o -o lib3.so

$ ld -b +init lib2_init +fini lib2_fini lib2.o -o lib2.so

$ ld -b +init lib1_init +fini lib1_fini lib1.o ./lib3.so -o \ lib1.so

$ cc -L. main.o -l1 -l2 -lc

Output from running a.out: lib2_init lib3_init lib1_init

lib1

lib2

lib3 lib1_fini lib3_fini lib2_fini

Ordering Within an Executable or Shared Library

Multiple initializers/terminators within the same load module (an executable or shared library) are called in an order following these rules:

Inits in .o (object) files or .a (archive) files are called in the reverse order of the link line.

Finis in .o or .a files are called in forward order of the link line.

HP-UX 10.X style initializers are called in forward order of the +I options specified on the link line when loading a shared library. They are then called in reverse order when unloading the library.

HP-UX 10.X style initializers are called after inits and before finis.

Any inits or finis in archive (.a) files are called only if the .o which contains it is used during the link. Use the linker -voption to determine which .o files within an archive file were used.

Shared libraries on the link line (dependent libraries) follow the ordering described in Ordering Among Executables and Shared Libraries.

For example, the linker command:

$ ld -b first_64bit.o -l:libfoo.so second_64bit.o

my_64bit.a +I first_10x_init +I \ second_10x_init -o libbar.so

results in the following order when library is loaded:

1.inits from any .o files used in my_64bit.a

2.inits in second_64bit.o

3.inits in first_64bit.o

4.first_10x_init

5.second_10x_init

and the following order when library is unloaded:

1.second_10x_init

2.first_10x_init

3.finis in first_64bit.o

4.finis in second_64bit.o

5.finis from any .o files used in my_64bit.a

NOTE: The libfoo.so object file is ignored in this example. It follows the rules described in Ordering Among Executables and Shared Libraries

Link Time Support for Ordering Initializers Based on Compile Time Defined Priority The linker also provides support for ordering of initializers within the same load module. The ordering of the

Initializers for Shared Libraries 141