Sorry for bothering you guys again.
I got problem when I am trying to recover the Global Variable Initial value. What I did is like the following
ConstantArray *Cstr = dyn_cast(gI->getInitializer());
// the above instruction enable me to get the content of initial string of global variable, like char a =“test global”;
And then I make some change for the Cstr and write it back to the global variable by gI->setInitializer(Constant *); Meanwhile I am trying to put a routine (constructing IR routine) into entry of main function, which is for recover initial vaule of global variable to the original string. In this routine,
Type *PointerAryType = ArrayType::get(PointerType::get(Type::SByteTy) , 20);
AllocaInst *PointerAry = new AllocaInst(PointerAryType , 0 , “AddrOfGstr”, BB);
// Here, I tried to insert IR like alloca [20 x sbyte *], for C code, it is char *AddrOfGstr; which pointer array is for storing the pointer of initial value of each
// global variable.
So next step in my Pass, I should get the pointer of initial value of each global variable and assign them the AddrOfGstr[i]. Here, I got problem, I can get initial value by gI->getInitializer(), but this pointer is not the memory address of the initial value, it is a pointer of constant, like constant *. I cannot assign it to the AddrOfGstr[i]. How could I get memory address the initial vaule, not the pointer of constant?
I tried to use the following way,
std::vector <Value *> idxVec;
Value *Zero1 = ConstantInt::get(Type::IntTy , 0);
Value *Zero2 = ConstantInt::get(Type::IntTy , 0);
Constant *pStr = ConstantExpr::getGetElementPtr(Cstr, idxVec); // trying to get pointer of initial value
GetElementPtrInst *GEP = new GetElementPtrInst( PointerAry , idxVec, “GetAryElement” , BB); // insert getelementptr and get the pionter of AddrOfGstr
StoreInst *storeAddr = new StoreInst(pStr , GEP , BB); // trying to assign the pStr to GEP
The above code is for getting the pionter of initial value of Cstr and assign it to AddrOfGstr, which should be like
char a=“test str”;
AddrOfGstr = a;
the code is compiled successfully, but when I run the pass, it failed since the instruction
Constant *pStr = ConstantExpr::getGetElementPtr(Cstr, idxVec);
I got the error as the following,
pt: Constants.cpp:1398: static llvm::Constant* llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, const std::vector<llvm::Value*, std::allocatorllvm::Value* >&): Assertion `Ty && “GEP indices invalid!”’ failed.
Is the question clear? How could I fix it? Thanks so much.