【问题标题】:How to get the address of a global variable in LLVM?如何获取 LLVM 中全局变量的地址?
【发布时间】:2017-04-28 21:18:33
【问题描述】:

x 是运行时程序中全局变量g 的地址。 LLVM IR 生成如下所示的存储指令:

store i32 30, i32* @g, align 4

我正在编写一个 LLVM pass,它将检测程序,以便在运行时将 x 传递给检测函数 func(int addr)。我可以使用IRBuilder 成功插入对func 的呼叫。我无法做的是插入仪器来收集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);
                    }
                }

运行opt的结果是:

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!

【问题讨论】:

  • @gx。在 C 或 C++ 程序中,g 的值是 load @g

标签: llvm llvm-clang llvm-ir


【解决方案1】:

您传递不同类型或不同大小的参数有两种可能性。

我不太确定,但试试这个:

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());

                        std::vector<Value *> args;
                        args.push_back(po);
                        builder.CreateCall(instrument_func, args);
                    }
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2022-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    相关资源
    最近更新 更多