__attribute__((constructor)) call order confusion

前端 未结 4 1025
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 02:55

The answer here demonstrates that __attribute__((constructor)) is not called after static initialization, it is called in the declaration order.

Then, w

4条回答
  •  生来不讨喜
    2021-02-06 03:29

    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.

提交回复
热议问题