[llvm] [BOLT] Support instrumentation hook via DT_FINI_ARRAY (PR #67348)
Rafael Auler via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 31 14:25:46 PDT 2023
rafaelauler wrote:
Overall looks good to me.
> BOLT currently hooks its its instrumentation finalization function via DT_FINI. However, this method of calling finalization routines is not supported anymore on newer ABIs like RISC-V. DT_FINI_ARRAY is preferred there.
What about DT_INIT, what does the ABI say? Usually there is symmetry between INIT and FINI, INITARRAY and FINIARRAY.
BTW ideally we would like to add a new entry to dynamic table and insert ourselves into DT_PREINIT_ARRAY, but this is outside the scope of this patch. We don't currently rewrite the dynamic table so we are not doing that for this reason. We need to run our code before every constructor, that's why PREINIT is more adequate.
> While scanning the binary: find the section where DT_FINI_ARRAY points to, read its first dynamic relocation and use its addend to find the address of the fini function we will use to hook;
If we're being pedantic, I think we need to patch the last entry of FINI_ARRAY, not the first. If you patch the first entry, you will still run destructors after your runtime lib. But I don't think we're going to crash because of this (at least for instrumentation), since instrumentation finalization code will just dump profile. No need to implement this, but we should at least have a comment documenting this behavior, so nobody is surprised in the future if they try to implement some runtime lib that expects to run its finalization code after application code.
https://github.com/llvm/llvm-project/pull/67348
More information about the llvm-commits
mailing list