[llvm-dev] Global Variable Initialization via Internal Function

Dean Michael Berris via llvm-dev llvm-dev at lists.llvm.org
Sun May 6 21:57:44 PDT 2018


On Mon, May 7, 2018 at 2:43 PM Aaron via llvm-dev <llvm-dev at lists.llvm.org>
wrote:

> 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.

> Example Simple C++ code:

> int func()
> {
>      return 5*2;
> }

> init globalVar = func();

> int main(...)
> ...

> Thanks for helping!

> Aaron

> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



-- 
Dean


More information about the llvm-dev mailing list