Question about Global Variable

Hi,

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[10] =“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[20]; 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);

idxVec.push_back(Zero1);
idxVec.push_back(Zero2);

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[0]
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[0], which should be like

char a[10]=“test str”;

main(){

char *AddrOfGstr[20];
AddrOfGstr[0] = 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.

Qiuyu

Hi,

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<ConstantArray>(gI->getInitializer());

// the above instruction enable me to get the content of initial string of global variable, like char a[10] ="test global";

Yup.

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,

Ok.

Type *PointerAryType = ArrayType::get(PointerType::get(Type::SByteTy) , 20);
AllocaInst *PointerAry = new AllocaInst(PointerAryType , 0 , "AddrOfGstr", BB);

Okay, this should be fine.

// Here, I tried to insert IR like alloca [20 x sbyte *], for C code, it is char *AddrOfGstr[20]; 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?

Okay, you do not want to use the initializer here. Instead, you want the *address* of the global. The address of the global is represented by the global variable object itself. In your code above, the 'gI' variable should be a pointer to a GlobalVariable. This Value* will have a pointer-to-array-of-byte type.

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);

idxVec.push_back(Zero1);
idxVec.push_back(Zero2);

Constant *pStr = ConstantExpr::getGetElementPtr(Cstr, idxVec); // trying to get pointer of initial value

Very close. Try using gI instead of Cstr.

Is the question clear? How could I fix it? Thanks so much.

Try the above, and please let me know if it doesn't help!

-Chris