[llvm-dev] How to get the address of a global variable in LLVM?
Jonathan Roelofs via llvm-dev
llvm-dev at lists.llvm.org
Fri Apr 28 14:32:14 PDT 2017
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
More information about the llvm-dev
mailing list