Search code examples
compiler-errorsclangllvmclang++llvm-clang

when using the llvm pass on a program, error: unable to execute command: Segmentation fault (core dumped)


A simple LLVM pass example, for LLVM-7.0.0. I get this error when trying to run:

clang -I~/clang_llvm2/include -Xclang -load -Xclang build/skeleton/libSkeletonPass.* test/a.cpp

I saw a function called main!

...

clang-7: error: unable to execute command: Segmentation fault (core dumped) clang-7: error: clang frontend command failed due to signal (use -v to see invocation) clang version 7.0.0 (tags/RELEASE_700/final) Target: x86_64-unknown-linux-gnu Thread model: posix clang-7: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script. clang-7: error: unable to execute command: Segmentation fault (core dumped) clang-7: note: diagnostic msg: Error generating preprocessed source(s).

The simple LLVM pass for LLVM-7.0.0

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
using namespace llvm;

namespace {
  struct SkeletonPass : public FunctionPass {
    static char ID;
    SkeletonPass() : FunctionPass(ID) {}

    bool runOnFunction(Function &F) {
      errs() << "I saw a function called " << F.getName() << "!\n";
      return false;
    }
  };
}

char SkeletonPass::ID = 0;

// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
static void registerSkeletonPass(const PassManagerBuilder &,
                         legacy::PassManagerBase &PM) {
  PM.add(new SkeletonPass());
}
static RegisterStandardPasses
  RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
                 registerSkeletonPass);

a.cpp program is a simple hello world program. LLVM_HOME is properly set. Using prebuilt llvm files.


Solution

  • You're not alone with this error (https://bugs.llvm.org/show_bug.cgi?id=34573), LLVM seems to crash at the end of the program when RegisterStandardPasses is used since LLVM 5.

    According to this answer: https://github.com/sampsyo/llvm-pass-skeleton/issues/7#issuecomment-401834287 a solution is to add -Wl,-znodelete to the compiler flags when you link your program. It worked for me.