$ ld
$ ld
$ ld
$ cc
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.
•
•
•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
•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
my_64bit.a +I first_10x_init +I \ second_10x_init
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