如何在LLVM中获取全局变量的地址?

如何在LLVM中获取全局变量的地址?,llvm,llvm-clang,llvm-ir,Llvm,Llvm Clang,Llvm Ir,设x为程序运行时全局变量g的地址。LLVM IR生成存储指令,如下所示: store i32 30, i32* @g, align 4 我正在编写一个LLVM过程,它将对程序进行指令插入,以便在运行时将x传递给指令插入函数func(int addr)。我可以使用IRBuilder成功插入对func的调用。我不能做的是插入仪器来收集x if (StoreInst *store_inst = dyn_cast<StoreInst>(&I)) {

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

store i32 30, i32* @g, align 4
我正在编写一个LLVM过程,它将对程序进行指令插入,以便在运行时将
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);
                    }
                }

有两种可能,一种是传递不同类型的参数,另一种是传递不同大小的参数

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

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);
                    }
                }
if(StoreInst*store_inst=dyn_cast&I)){
Value*po=store_inst->getPointerOperand();
如果(isa(po)){

错误代码(<代码> @ g < /COD> >代码> x/COD>。如C或C++程序中所想到的 G/<代码>的值是<代码>加载@ g< /COD>。
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);
                    }
                }