I have written a simple llvm Pass that is counting the opcodes in c++ source file. I have no issues with source file and I have successfully taken .bc file of it. Now when I run it through my Pass then it crashes. The code for pass is below (SourceCode is not the issue):
#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");
I am running these commands to run my test.cpp program through pass:
clang++ -emit-llvm testOp.cpp -c -o test.bc then make and finally
opt -load ../../../Release+Asserts/lib/LLVMopCounter.so -opCounter < test.bc >/dev/null
Output is like:
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)
The answer is: I was using incompatible LLVM libraries with clang. As i was working on Clang 3.5 with LLVMlib 3.4. After raising both to same levels, Now all Passes are working fine. Deleted old llvm libraries and added new one of version 3.5 from llvm.org