Passを作った時のコマンドメモ(2)
つぎに任意のフォルダでつぎのhello.cppを作成する.
#include "llvm/Pass.h" #include "llvm/IR/Function.h" #include "llvm/Support/raw_ostream.h" using namespace llvm; namespace { struct Hello : public FunctionPass { static char ID; Hello() : FunctionPass(ID) {} bool runOnFunction(Function &F) override { errs() << "Hello: "; errs().write_escaped(F.getName()) << '\n'; return false; } }; } char Hello::ID = 0; static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);
つぎのコマンドを使う.
g++ -g ./hello.cpp -I/usr/local/llvm/include -std=c++11 -D__STDC_CONSTANT_MACTOS -D__STDC_LIMIT_MACTOS `llm-config --cflags --ldflags -libs` -fno-rtti -c -o hello.o
つぎに共有ライブラリの作成
g++ -shared -fpic -o ./hello.so ./hello.o
ここで, test.cというファイルを作成しておく.
#include <stdio.h> int main(void){ printf("Hello\n"); return 0; }
test.cをコマンドでtest.bcをつくる
clang -O3 -emit-llvm test.c -c -o test.bc
これを食わせる,
opt -load ./hello.so -hello < test.bc > /dev/null
これでつぎの出力が得られるはず
Hello: main
これでひとまず, 終.
一応注意としては, -fno-rttiや-std=c++11とかのオプションをちゃんと付ける.
そしてちゃんとPATHを通したり, -Iオプションでincludeのヘッダー場所をしているするのがポイントというか自分がハマったポイントでした.