[LLVMdev] modifiy the address of GlobalVariable emitted by JIT

ChiaLun h3355626 at msn.com
Thu Jan 10 19:00:53 PST 2013

Hi everyone, 

I am building a binary translator, and try to do block chaining.

LLVM version : 3.1
my machine : x86-32 bit, Linux

Before each *LLVM IR returnInst constantValue*, I insert a call instruction
& a returnInst which looks like

%x = call @G  ;
ret %x;

then remove the *LLVM IR returnInst constantValue*

The initializer of @G is a function which has prototype int f(struct MyType*
and the content in terms of LLVM IR is 

define internal i32 @chaining1057({ i32, [32 x i32] }* %ThreadCtx1) {
  %0 = getelementptr inbounds { i32, [32 x i32] }* %ThreadCtx1, i32 0, i32 1 
  %1 = getelementptr inbounds [32 x i32]* %0, i32 0, i32 23
  store i32 0, i32* %1 ; // the first three instruction just stores
  ret i32 296696 ; // return the same constantValue of the above said

I use JIT to compile the above function and also use
JIT->getOrEmitGlobalVariable to get

the address of @G , and store the address in a pointer variable.  So later,
I can modify the content

pointed by the pointer then 

 %x = call @G

the above instruction would call another function.

My environment has multi-thread, which means maybe some threads would try to
modify the address of @G

at the same time, but I think there is no big deal because what all threads
are trying to do is just

modify the address from A to B ( A , B is fixed ).

I modify the address of the globalvariable by

long* addr = (long*)(cur->getGVAddressPtrVector()[ix]); // no vector
insertion or deletion when this is called.

*addr = (long)next->getMachBlock();

% ix is just a vector index
std::vector<long>& TransBlock::getGVAddressPtrVector()  
	return gvAddress; // this vector stores the address of the globalvariable
emitted by JIT

inline void * getMachBlock() const {
        return machBlock;   // this is the address of LLVM function emitted
by JIT 

When I do block chaining, I got segmentation fault, but when I turn it off,
nothing happened, so I guess

it is the source of the problem. I have been stuck here for quite a time.

Any idea on this is welcomed.

Have A Nice Day

Chia Lun Liu


View this message in context: http://llvm.1065342.n5.nabble.com/modifiy-the-address-of-GlobalVariable-emitted-by-JIT-tp53489.html
Sent from the LLVM - Dev mailing list archive at Nabble.com.

More information about the llvm-dev mailing list