[llvm-dev] How to get the address of a global variable in LLVM?

Dipanjan Das via llvm-dev llvm-dev at lists.llvm.org
Fri Apr 28 14:20:40 PDT 2017


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>


More information about the llvm-dev mailing list