llvm pass segmentation fault:(核心转储)

llvm pass segmentation fault:(Core dumped)

我写了一个简单的 llvm Pass 来计算 C++ 源文件中的操作码。我对源文件没有任何问题,我已经成功地获取了它的 .bc 文件。现在,当我 运行 它通过我的通行证时,它就会崩溃。 pass 的代码如下(源代码不是问题):

#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");

我正在 运行通过传递将这些命令发送到 运行 我的 test.cpp 程序:

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

opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter < test.bc >/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)

答案是:我在 clang 中使用了不兼容的 LLVM 库。当我使用 LLVMlib 3.4 开发 Clang 3.5 时。将两者提升到相同水平后,现在所有通行证都可以正常工作。 删除了旧的 llvm 库并从 llvm.org

添加了新的 3.5 版