Dipanjan Das via llvm-dev
2017-Apr-28 21:20 UTC
[llvm-dev] How to get the address of a global variable in LLVM?
Let `x` be the address of a global variable `g` in a program at run-time.
LLVM IR produces a store instruction as shown below:
store i32 30, i32* @g, align 4
I am writing an LLVM pass which will instrument the program such that `x`
is passed to an instrumentation function `func(int addr)` at run-time. I
can insert a call to `func` using `IRBuilder` successfully. What I am not
being able to do is to insert instrumentation to collect `x`.
if (StoreInst *store_inst = dyn_cast<StoreInst>(&I)) {
Value* po = store_inst->getPointerOperand();
if(isa<GlobalVariable>(po)) {
errs() << "store [pointer]: "
<< *po << '\n';
Constant *instrument_func
F.getParent()->getOrInsertFunction("func", Type::getVoidTy(Ctx),
Type::getInt32Ty(Ctx), NULL);
IRBuilder<> builder(&I);
builder.SetInsertPoint(&B,
++builder.GetInsertPoint());
Value* args[] = {po};
builder.CreateCall(instrument_func, args);
}
}
The result of running `opt` is :
Call parameter type does not match function signature!
i32* @a
i32 call void bitcast (void (i64)* @func to void (i32)*)(i32* @a)
LLVM ERROR: Broken function found, compilation aborted!
--
Thanks & Regards,
Dipanjan
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170428/459d855e/attachment.html>
Jonathan Roelofs via llvm-dev
2017-Apr-28 21:32 UTC
[llvm-dev] How to get the address of a global variable in LLVM?
On 4/28/17 3:20 PM, Dipanjan Das via llvm-dev wrote:> > Let `x` be the address of a global variable `g` in a program at > run-time. LLVM IR produces a store instruction as shown below: > > store i32 30, i32* @g, align 4 > > I am writing an LLVM pass which will instrument the program such that > `x` is passed to an instrumentation function `func(int addr)` at > run-time. I can insert a call to `func` using `IRBuilder` successfully. > What I am not being able to do is to insert instrumentation to collect `x`. > > if (StoreInst *store_inst = dyn_cast<StoreInst>(&I)) { > > Value* po = store_inst->getPointerOperand(); > if(isa<GlobalVariable>(po)) { > errs() << "store [pointer]: " << *po << '\n'; > > Constant *instrument_func > F.getParent()->getOrInsertFunction("func", Type::getVoidTy(Ctx), > Type::getInt32Ty(Ctx), NULL); > > IRBuilder<> builder(&I); > builder.SetInsertPoint(&B, > ++builder.GetInsertPoint()); > > Value* args[] = {po}; > builder.CreateCall(instrument_func, args); > } > } > > The result of running `opt` is : > > Call parameter type does not match function signature! > i32* @a > i32 call void bitcast (void (i64)* @func to void (i32)*)(i32* @a) > LLVM ERROR: Broken function found, compilation aborted! >You need a load instruction since your function takes an i32, not an i32*.... A global's value in llvm is its address, not the numeric data it contains. Jon> > -- > > Thanks & Regards, > > Dipanjan > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-- Jon Roelofs jonathan at codesourcery.com CodeSourcery / Mentor Embedded / Siemens
Tim Northover via llvm-dev
2017-Apr-28 21:34 UTC
[llvm-dev] How to get the address of a global variable in LLVM?
On 28 April 2017 at 14:32, Jonathan Roelofs via llvm-dev <llvm-dev at lists.llvm.org> wrote:> You need a load instruction since your function takes an i32, not an > i32*.... A global's value in llvm is its address, not the numeric data it > contains.I suspect he actually wants to make his function take an "i8*" and bitcast his pointer to that type before calling it. [*] Tim. [*] Keeping it as an i32/i64 and using ptrtoint instead of bitcast would also work, but be less portable and elegant.