[LLVMdev] How to get the return address on the stack on LLVM

Xueying ZHANG s1031770 at sms.ed.ac.uk
Tue Jul 26 07:49:10 PDT 2011

Hi all,

I want to implement the Xor random canary, so I have to get the return  
address in the prologue and epilogue of the function.

In the prologue of the function, before I insert into the canary on  
the stack, I can get the return address by:

ConstantInt* ci =  
llvm::ConstantInt::get(Type::getInt32Ty(RI->getContext()), 0);
       Value* Args1[] = {ci};
CallInst* callInst = CallInst::Create(Intrinsic::getDeclaration(M,  
                &Args1[0], array_endof(Args1), "Call Return Address", InsPt);

CallInst will get the return address and it works.

While, in the epilogue of the function, due to the canary has been  
inserted. I write the similar code:

ConstantInt* ci2 =  
llvm::ConstantInt::get(Type::getInt32Ty(RI->getContext()), 1);
     Value* Args3[] = {ci2};
     CallInst* callInst1 =  
              &Args3[0], array_endof(Args3), "Caaall Return Address", BB);

But it does not work this time. I cannot get the return address.

What is problem? How can I get the return address? Thank you!


The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the llvm-dev mailing list