user2776945
user2776945

Reputation: 89

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.

Upvotes: 2

Views: 3771

Answers (2)

WU ZHENWEI
WU ZHENWEI

Reputation: 56

Compiling LLVM from source is mandatory if you are developing an in-source pass (within LLVM source tree). It can also be convenient in the case of developing out-of-source passes as it gives you full control over the compilation options. For example, a debug build of LLVM is much more pleasant to work with compared to an optimized one. llvm-pass-tutorial

I just came across similar problems. Seems that the optimized clang build (the one acquired from apt install or pre-built binaries) does not support in-source pass. The only choice I know currently is to build llvm and clang from the source.

LLVM Download Page.

Upvotes: 0

Kuba Ber&#225;nek
Kuba Ber&#225;nek

Reputation: 557

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.

Upvotes: 1

Related Questions