The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order.
Then, w
You might try using linker scripts for ld
. You can read more about it here, but I guess what you are looking for is
.ctors : { *(SORT(.ctors)) MyLastInitChance.o(SORT(.ctors)) }
.dtors : { *(SORT(.dtors)) MyLastInitChance.o(SORT(.dtors)) }
in SECTIONS{...}
block. That should rearrange .ctors
sections so file provided will call it's constructors as the last one. Obviously more advanced solutions are also available if you find yorself in need for one;)
HINT: writing your own link-script is tedious. Use ld
's --verbose
option that prints out used link script and modify it. Then add your linking script using -T
switch.