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

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

一个简单的 LLVM pass 示例,适用于 LLVM-7.0.0。 尝试 运行:

时出现此错误

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

我看到一个叫做 main 的函数!

...

clang-7:错误:无法执行命令:分段错误(核心已转储) clang-7:错误:clang 前端命令因信号而失败(使用 -v 查看调用) clang 版本 7.0.0 (tags/RELEASE_700/final) 目标:x86_64-unknown-linux-gnu 线程模型:posix clang-7:注意:诊断消息:请向 https://bugs.llvm.org/ 提交错误报告,并包括崩溃回溯、预处理源和相关的 运行 脚本。 clang-7:错误:无法执行命令:分段错误(核心已转储) clang-7:注意:诊断消息:生成预处理源时出错。

LLVM-7.0.0 的简单 LLVM pass

#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程序是一个简单的hello world程序。 LLVM_HOME 设置正确。 使用预构建的 llvm 文件。

您不是唯一遇到此错误的人 (https://bugs.llvm.org/show_bug.cgi?id=34573),从 LLVM 5 开始使用 RegisterStandardPasses 时,LLVM 似乎在程序结束时崩溃。

根据这个答案:https://github.com/sampsyo/llvm-pass-skeleton/issues/7#issuecomment-401834287 一个解决方案是在 link 程序时将 -Wl,-znodelete 添加到编译器标志。它对我有用。

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

我刚遇到类似的问题。似乎优化的 clang 构建(从 apt installpre-built binaries 获得的构建)不支持源内传递。我目前知道的唯一选择是从源代码构建 llvm 和 clang。

LLVM Download Page