modifiy the address of GlobalVariable emitted by JIT

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

modifiy the address of GlobalVariable emitted by JIT

ChiaLun
This post was updated on .
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
@G declaration in C++:

G = new llvm::GlobalVariable( *(tb->getModule()),
                                          pty,
                                          false,GlobalValue::InternalLinkage,0,
                                          "chainGV",0,0,0);

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) {
"block":
  %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 information
  ret i32 296696 ; // return the same constantValue of the above said returnInst
}  

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
    }

All the calling convention of my function is CallingConv::C

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