Global Variable Initialization via Internal Function

Hello,

I’m trying to figure out how to initialize global variables via LLVM IR and I found that clang creates a internal function to initialize global variables if initialization values are not constants.

Do we really need to create an internal function for complex global variable initializations during variable declaration? Can’t we create instructions in global scope?

Example Simple C++ code:

int func()
{
return 5*2;
}

init globalVar = func();

int main(…)

Thanks for helping!

Aaron

Hello,

I'm trying to figure out how to initialize global variables via LLVM IR

and I found that clang creates a internal function to initialize global
variables if initialization values are not constants.

Do we really need to create an internal function for complex global

variable initializations during variable declaration? Can't we create
instructions in global scope?

There might be other ways, but the C++ standard says something about the
order of initialisation of objects, and operating systems usually do this
by a convention. The convention seems to be that it will call functions
that are referred to from specially-named sections in a binary. So far, the
"cleanest" way of doing that is to define functions that get referred to
(by address) from these special sections.