llvm传递分段错误:(核心转储)

时间:2015-04-10 10:13:42

标签: c++ llvm llvm-clang

我编写了一个简单的llvm Pass,用于计算c ++源文件中的操作码。我对源文件没有任何问题,我已成功获取它的.bc文件。现在当我通过我的Pass然后它崩溃了。传递的代码如下(SourceCode不是问题):

#define DEBUG_TYPE "opCounter"
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include <map>

using namespace llvm;
namespace
{
  struct CountOperands : public FunctionPass
  {
    std::map<std::string,int> opCounter;
    static char ID;

    /*Constructor*/ 
    CountOperands() : FunctionPass(ID) {}

    /*RunOnFuntion Method*/
    virtual bool runOnFunction( Function &F)
    {
      errs() << "Function Name: " << F.getName() << "\n";

      /*Reading the OpCode in the function*/
      for (Function::iterator bb = F.begin(), e = F.end(); bb != e; ++bb)
      {    
        BasicBlock &b = *bb;
        errs() << "##########Works fine till here!"<<"\n";

        for (BasicBlock::iterator i = b.begin(), e2 = b.end(); i != e2; ++i)
        {

          if ( opCounter.find(i->getOpcodeName()) == opCounter.end() )
          {
            opCounter[i->getOpcodeName()] = 1; //New OpCode in the list
          }
          else
          {
            opCounter[i->getOpcodeName()] += 1; //Incrementing the old one
          }    
        }
      }
      std::map <std::string, int>::iterator i = opCounter.begin();
      std::map <std::string, int>::iterator e3 = opCounter.end();

      while(i != e3)
      {
        errs() << i->first << ": " << i->second << "\n";
        i++;
      }
      errs() << "\n";
      opCounter.clear();
      return false;
    }
  }; 
}
/*Registering the Pass to PassManager*/
char CountOperands::ID = 0;
static RegisterPass<CountOperands> X("opCounter", "Counts the OpCodes in a single Function");

我正在运行这些命令来通过pass:

运行我的test.cpp程序

clang ++ -emit-llvm testOp.cpp -c -o test.bc 然后制作,最后

opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter&lt; test.bc&gt; / dev / null

输出就像:

homer@ubuntu:~/sbx/walle_code_execution/codeexe/aspire/bin2vm/LLVM-3.6.0/llvm.src/lib/Transforms/OperandCounter$ opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter < test.bc >/dev/null

Function Name: main

 ##########Works fine till here!

0  libLLVM-3.4.so.1 0x00007f9bfecea5d2 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1  libLLVM-3.4.so.1 0x00007f9bfecea3c4
2  libc.so.6        0x00007f9bfd769ff0
3  LLVMopCounter.so 0x00007f9bfc7730a4
4  libLLVM-3.4.so.1 0x00007f9bfe6baf77 llvm::FPPassManager::runOnFunction(llvm::Function&) + 471
5  libLLVM-3.4.so.1 0x00007f9bfe6baffb llvm::FPPassManager::runOnModule(llvm::Module&) + 43
6  libLLVM-3.4.so.1 0x00007f9bfe6bd4b5 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 693
7  opt              0x0000000000412c8d main + 2461
8  libc.so.6        0x00007f9bfd754ec5 __libc_start_main + 245
9  opt              0x0000000000413b40
Stack dump:
0.      Program arguments: opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter 
1.      Running pass 'Function Pass Manager' on module '<stdin>'.
2.      Running pass 'Counts the OpCodes in a single Function' on function '@main'

Segmentation fault (core dumped)

1 个答案:

答案 0 :(得分:0)

答案是:我使用了与clang不兼容的LLVM库。因为我正在使用LLVMlib 3.4处理Clang 3.5。在将两者提升到相同水平后,现在所有通行证都正常工作。 删除了旧的llvm库,并从llvm.org添加了新版本的3.5