0

I want to instrument some code that gets executed before any other code in my module.

I thought about calling the code in the start of the main function. But there is not always a main function or it is not always named "main". Or it is a library and it doesn't even have a main function.

Are there some other, smarter ways?

Fee
  • 719
  • 9
  • 24

1 Answers1

3

You can put the code you want to run early into a function and add that function to llvm.global_ctors. This is the equivalent of using __attribute__((constructor)) in C or C++.

To do this from a pass, you can use the llvm::appendToGlobalCtors function, which is declared in llvm/Transforms/Utils/ModuleUtils.h.

Community
  • 1
  • 1
Ismail Badawi
  • 36,054
  • 7
  • 85
  • 97