llvm获取注释

llvm获取注释,llvm,Llvm,我用新的表格更新了我以前的问题。 各位好, 我有以下LLVM IR: @.str = private unnamed_addr constant [3 x i8] c"DS\00", section "llvm.metadata" @llvm.global.annotations = appending global [1 x { i8*, i8*, i8*, i32 }] [{ i8*, i8*, i8*, i32 } { i8* bitcast (i32* @f to i8*), i8*

我用新的表格更新了我以前的问题。 各位好,

我有以下LLVM IR:

@.str = private unnamed_addr constant [3 x i8] c"DS\00", section "llvm.metadata"

@llvm.global.annotations = appending global [1 x { i8*, i8*, i8*, i32 }] [{ i8*, i8*, i8*, i32 } { i8* bitcast (i32* @f to i8*), i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str1, i32 0, i32 0), i32 18 }], section "llvm.metadata"
我需要获得
@f
(或者我可以以某种方式获得
@f=global i32 0,align 4
)的定义,还需要从
@str
获得“DS”。在我的目标代码中,我有:

__attribute__((annotate("DS"))) int f=0;
我在解析@llvm.global.annotations时遇到问题,我想我在@llvm.str时会遇到问题。我尝试的是:

一,

二,

欢迎任何帮助!谢谢大家!

如果要使用runOnModule()而不是runOnFunction()。或者,你也可以学习这个模块。llvm.global.annotations是在函数外部定义的。在内部执行以下操作:

for (Module::global_iterator I = F.global_begin(), E = F.global_end(); I != E; ++I) {

if (I->getName() == "llvm.global.annotations")
{
    errs()<<"\nllvm.global.annotations\n";
    //1. find out what global variable is by "parsing" the IR
    //2. get through the module till you find a load @f 
    //3. you can add metadata to the load function and you can easily get the metadata from the normal pass
}

} 
for(Module::global_迭代器I=F.global_begin(),E=F.global_end();I!=E;++I){
如果(I->getName()=“llvm.global.annotations”)
{
我解决了它。

我将整个带注释的表达式强制转换为Value*。然后,为了避免像getAsString()这样丑陋的东西,我检查
V->getValueID()==Value::ConstantArrayVal
以将其强制转换为
ConstantArray
。因为它只包含数组[0],所以我强制转换了array0>getOperand(0)到
ConstantStruct
。因此,从
ConstantStruct
可以获取所有四个操作数。现在要做的只是从每个字段获取@f、@str的名称。这是通过
ConstantStruct->GetOperator(0)->GetOperator(0)完成的

回答得很晚,但谷歌把我带到了这里,我认为提供一个完整的LLVM通行证来发现自由文本注释会很有帮助。 此LLVM过程将只插入标有_属性((annotate(“someFreeTextAnnotation”))的函数。 守则如下:

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"
#include <set>

using namespace llvm;
const char *AnnotationString = "someFreeTextAnnotation";

namespace {
struct Hello : public FunctionPass {
    static char ID;
    Hello() : FunctionPass(ID) {}
    std::set<Function*> annotFuncs;

    virtual bool doInitialization(Module &M)override{
        getAnnotatedFunctions(&M);
        return false;
    }
    bool shouldInstrumentFunc(Function &F){
        return annotFuncs.find(&F)!=annotFuncs.end();
    }
    void getAnnotatedFunctions(Module *M){
        for (Module::global_iterator I = M->global_begin(),
                E = M->global_end();
                I != E;
                ++I) {

            if (I->getName() == "llvm.global.annotations") {
                ConstantArray *CA = dyn_cast<ConstantArray>(I->getOperand(0));
                for(auto OI = CA->op_begin(); OI != CA->op_end(); ++OI){
                    ConstantStruct *CS = dyn_cast<ConstantStruct>(OI->get());
                    Function *FUNC = dyn_cast<Function>(CS->getOperand(0)->getOperand(0));
                    GlobalVariable *AnnotationGL = dyn_cast<GlobalVariable>(CS->getOperand(1)->getOperand(0));
                    StringRef annotation = dyn_cast<ConstantDataArray>(AnnotationGL->getInitializer())->getAsCString();
                    if(annotation.compare(AnnotationString)==0){
                        annotFuncs.insert(FUNC);
                        //errs() << "Found annotated function " << FUNC->getName()<<"\n";
                    }
                }
            }
        }
    }

    bool runOnFunction(Function &F) override {
        if(shouldInstrumentFunc(F)==false)
            return false;
        errs() << "Instrumenting " << F.getName() << "\n";

        return false;
    }
}; // end of struct Hello
}  // end of anonymous namespace

char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Discover annotation attribute",
        false /* Only looks at CFG */,
        false /* Analysis Pass */);
#包括“llvm/Pass.h”
#包括“llvm/IR/Function.h”
#包括“llvm/Support/raw_ostream.h”
#包括“llvm/IR/Module.h”
#包括“llvm/IR/Constants.h”
#包括
使用名称空间llvm;
const char*AnnotationString=“someFreeTextAnnotation”;
名称空间{
struct Hello:public FunctionPass{
静态字符ID;
Hello():FunctionPass(ID){}
std::set annotFuncs;
虚拟布尔初始化(模块&M)覆盖{
getAnnotatedFunctions(&M);
返回false;
}
布尔shouldInstrumentFunc(函数和函数){
返回annotFuncs.find(&F)!=annotFuncs.end();
}
void getAnnotatedFunctions(模块*M){
对于(Module::global\u iterator I=M->global\u begin(),
E=M->global_end();
I!=E;
++(一){
如果(I->getName()=“llvm.global.annotations”){
ConstantArray*CA=dyn_cast(I->getOperand(0));
对于(自动OI=CA->op_开始();OI!=CA->op_结束();++OI){
ConstantStruct*CS=dyn_cast(OI->get());
函数*FUNC=dyn_cast(CS->getOperand(0)->getOperand(0));
GlobalVariable*AnnotationGL=dyn_cast(CS->getOperand(1)->getOperand(0));
StringRef annotation=dyn_cast(AnnotationGL->getInitializer())->getAsCString();
if(annotation.compare(AnnotationString)==0){
注释函数插入(函数);

//errs()感谢您的回复。我正在尝试按照您的方式更新我的问题。我将我的进度更新到了我的答案中。在剩余的小问题解决后,我将对其进行编辑。
errs()<<"\n "<<(V->getValueID())<<"\n";
if(V->getValueID() == Value::ConstantArrayVal) {
            ConstantArray *ca = (ConstantArray *)V;
            errs()<<"\n "<<(ca[0])<<"\n";  }
[1 x { i8*, i8*, i8*, i32 }] [{ i8*, i8*, i8*, i32 } { i8* bitcast (i32* @f to i8*), i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([9 x i8]* @.str1, i32 0, i32 0), i32 18 }]
for (Module::global_iterator I = F.global_begin(), E = F.global_end(); I != E; ++I) {

if (I->getName() == "llvm.global.annotations")
{
    errs()<<"\nllvm.global.annotations\n";
    //1. find out what global variable is by "parsing" the IR
    //2. get through the module till you find a load @f 
    //3. you can add metadata to the load function and you can easily get the metadata from the normal pass
}

} 
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Constants.h"
#include <set>

using namespace llvm;
const char *AnnotationString = "someFreeTextAnnotation";

namespace {
struct Hello : public FunctionPass {
    static char ID;
    Hello() : FunctionPass(ID) {}
    std::set<Function*> annotFuncs;

    virtual bool doInitialization(Module &M)override{
        getAnnotatedFunctions(&M);
        return false;
    }
    bool shouldInstrumentFunc(Function &F){
        return annotFuncs.find(&F)!=annotFuncs.end();
    }
    void getAnnotatedFunctions(Module *M){
        for (Module::global_iterator I = M->global_begin(),
                E = M->global_end();
                I != E;
                ++I) {

            if (I->getName() == "llvm.global.annotations") {
                ConstantArray *CA = dyn_cast<ConstantArray>(I->getOperand(0));
                for(auto OI = CA->op_begin(); OI != CA->op_end(); ++OI){
                    ConstantStruct *CS = dyn_cast<ConstantStruct>(OI->get());
                    Function *FUNC = dyn_cast<Function>(CS->getOperand(0)->getOperand(0));
                    GlobalVariable *AnnotationGL = dyn_cast<GlobalVariable>(CS->getOperand(1)->getOperand(0));
                    StringRef annotation = dyn_cast<ConstantDataArray>(AnnotationGL->getInitializer())->getAsCString();
                    if(annotation.compare(AnnotationString)==0){
                        annotFuncs.insert(FUNC);
                        //errs() << "Found annotated function " << FUNC->getName()<<"\n";
                    }
                }
            }
        }
    }

    bool runOnFunction(Function &F) override {
        if(shouldInstrumentFunc(F)==false)
            return false;
        errs() << "Instrumenting " << F.getName() << "\n";

        return false;
    }
}; // end of struct Hello
}  // end of anonymous namespace

char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Discover annotation attribute",
        false /* Only looks at CFG */,
        false /* Analysis Pass */);