llvm获取注释

Ale*_*lex 3 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* 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"
Run Code Online (Sandbox Code Playgroud)

我需要获取@f(或者也许我可以以某种方式获取 的定义@f = global i32 0, align 4),并且我还需要从 获取“DS” @.str。在我的目标代码中我有:

__attribute__((annotate("DS"))) int f=0;
Run Code Online (Sandbox Code Playgroud)

我在解析 @llvm.global.annotations 时遇到问题,我想我会遇到 @.str 的问题。我尝试过的:

1.

for (Module::global_iterator I = F.global_begin(), E = F.global_end(); I != E; ++I) {
    if (I->getName() == "llvm.global.annotations") {
       Value *V = cast<Value>(I->getOperand(0));
        errs()<<"\n "<<*(V)<<"\n";
        errs()<<"\n "<<*(V->getType())<<"\n";
Run Code Online (Sandbox Code Playgroud)

结果 :

  [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 }]

 [1 x { i8*, i8*, i8*, i32 }]
Run Code Online (Sandbox Code Playgroud)

2.

errs()<<"\n "<<(V->getValueID())<<"\n";
if(V->getValueID() == Value::ConstantArrayVal) {
            ConstantArray *ca = (ConstantArray *)V;
            errs()<<"\n "<<(ca[0])<<"\n";  }
Run Code Online (Sandbox Code Playgroud)

结果 :

[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 }]
Run Code Online (Sandbox Code Playgroud)

欢迎任何帮助!谢谢 !

小智 5

答案很晚了,但 Google 引导我来到这里,我认为提供发现自由文本注释的完整 LLVM 通道会很有帮助。此 LLVM 传递仅检测标有 __attribute((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 */);
Run Code Online (Sandbox Code Playgroud)