这可以使用模块传递来完成。下面是我的代码,如果你需要帮助运行它,你可以看这里。
酒吧.c
int your_fun(int arg2) {
int x = arg2;
return x+2;
}
骨架.cpp
#include "llvm/Pass.h"
#include "llvm/IR/Module.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 ModulePass {
static char ID;
SkeletonPass() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
for (auto& F : M) {
errs() << "\tFunction: " << F.getName() << "\n";
for (auto& BB : F) {
errs() << "\t\tBasic Block: " << BB.getName() << "\n";
for (auto& I : BB) {
errs() << "\t\t\tInstruction: " << I.getOpcodeName() << "\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_ModuleOptimizerEarly,
registerSkeletonPass);
static RegisterStandardPasses RegisterMyPass1(PassManagerBuilder::EP_EnabledOnOptLevel0,
registerSkeletonPass);
输出:
| => clang -Xclang -load -Xclang build/skeleton/libSkeletonPass.so foo.c bar.c
Module: foo.c!
Function: my_fun!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: load
Instruction: store
Instruction: load
Instruction: add
Instruction: ret
Function: main!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: store
Instruction: store
Instruction: store
Instruction: store
Instruction: load
Instruction: icmp
Instruction: br
Basicblock: if.then!
Instruction: load
Instruction: store
Instruction: br
Basicblock: if.else!
Instruction: load
Instruction: call
Instruction: store
Instruction: br
Basicblock: if.end!
Instruction: load
Instruction: ret
Module: bar.c!
Function: your_fun!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: load
Instruction: store
Instruction: load
Instruction: add
Instruction: ret
输出:如果包含链接到 bar.c 的头文件
Module: foo.c!
Function: your_fun!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: load
Instruction: store
Instruction: load
Instruction: add
Instruction: ret
Function: my_fun!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: load
Instruction: store
Instruction: load
Instruction: add
Instruction: ret
Function: main!
Basicblock: entry!
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: alloca
Instruction: store
Instruction: store
Instruction: store
Instruction: store
Instruction: store
Instruction: load
Instruction: icmp
Instruction: br
Basicblock: if.then!
Instruction: load
Instruction: store
Instruction: br
Basicblock: if.else!
Instruction: load
Instruction: call
Instruction: store
Instruction: load
Instruction: call
Instruction: store
Instruction: br
Basicblock: if.end!
Instruction: load
Instruction: ret