Wednesday, 21 August 2013

ld initfirst does not force fini at last

ld initfirst does not force fini at last

I am trying to force the fini call of a library to be done after every
other fini calls. So my first guess was to link this library with
initfirst option:
g++ -Wl,-z,initfirst -shared lib1.o lib1.so
And living the other library just like before:
g++ -shared lib2.o lib2.so
Finally I link my executable against these two libraries:
g++ main.cpp -l1 -l2
And apparently, the init call is well working (is the first) but the fini
order is still the same, here is a sample of the output of LD_DEBUG:
2224: calling init: ../lib1/lib1.so
2224: ...
2224: calling init: ../lib2/lib2.so
2224: calling fini: ./a.out [0]
2224: calling fini: ../lib1/lib1.so [0]
2224: calling fini: ../lib2/lib2.so [0]
2224: ...
Is this expected? I noticed that if I do:
g++ main.cpp -l2 -l1
I have the right fini order (which seems logical in this case).
My issue is that I don't have necessary the control over the build of the
executable...
So it seems that initfirst is just a way to bypass the init order but also
break the guarantee to have a fini order reverse of the init order. Is
there a way to have the same for fini? If not, does the link order or
something else at build/link time guarantee the init and fini order in any
case? According to C++ static initialization order, apparently not...
Environment: g++ 4.6.3, ld 2.22

No comments:

Post a Comment