Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get actual value of global variables in llvm

Tags:

For example:

int x=0;
int y=0;

where x and y are global variables, and in main() function we do the following:

x++;
y++;

How to get the newest value of global variables x and y in llvm.

when I try to do errs()<<g; they give the initial value as @BB0 = global i32 but I need to get the actual value like x=1, by using llvm.

like image 643
R.Omar Avatar asked Jun 13 '17 20:06

R.Omar


1 Answers

Assuming you're using LLVM's API:

If the global is constant you can access its initialization value directly, for example:

Constant* myGlobal = new GlobalVariable( myLlvmModule, myLlvmType, true, GlobalValue::InternalLinkage, initializationValue );
...
Constant* constValue = myGlobal->getInitializer();

And if that value is of e.g. integer type, you can retrieve it like so:

ConstantInt* constInt = cast<ConstantInt>( constValue );
int64_t constIntValue = constInt->getSExtValue();

If the global isn't constant, you must load the data it points to (all globals are actually pointers):

Value* loadedValue = new LoadInst( myGlobal );
like image 77
Christer Swahn Avatar answered Oct 04 '22 14:10

Christer Swahn