Get the value of a GlobalVariable

Hi,

I am executing a C program using JIT. When the program exits, the control
comes back to my program which initiates the JIT. Now, I want to read
the value(not the LLVM nomenclature) of a global variable (not the LLVM
nomenclature). I am able to get the global variable, using the following
command:
GlobalVariable *my_global = Mod->getNamedGlobal(“MY_GLOBAL”);

I want to know the value which was stored in this variable, I use the
following command, but it gives error:
APInt value = my_global->getValue();

Error is:
llvm/lib/IR/Constants.cpp:1257: llvm::Constant *llvm::Constant::getSplatValue() const: Assertion `this->getType()->isVectorTy() && “Only valid for vectors!”’ failed.

I am new to LLVM, so a lot of nomenclature is still not very familiar to me
and I am trying to understand it as I go forward.

Thanks,
Varun Agrawal

Varun Agrawal <varun729@gmail.com> writes:

GlobalVariable *my_global = Mod->getNamedGlobal("MY_GLOBAL");

I want to know the value which was stored in this variable, I use the
following command, but it gives error:
APInt value = my_global->getValue();

Error is:
llvm/lib/IR/Constants.cpp:1257: llvm::Constant
*llvm::Constant::getSplatValue() const: Assertion
`this->getType()->isVectorTy() && "Only valid for vectors!"' failed.

ExecutionEngine::getPointerToGlobal returns a void* to the memory
address used by the GlobalValue (a GlobalVariable in your case.) Then
you only need to cast the void* to the correct pointer type (int* in
your case) and read it:

int value = *static_cast<int*>(MyExecutionEngine->getPointerToGlobal(my_global));