2

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.

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.