char Hello::ID = 0; static RegisterPass<Hello> X("hello", "Hello World Pass");
当然,因为源码中已经提供了初始样本,可以不对代码进行改动,这与官方教程中的内容有些许不同:
//===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements two versions of the LLVM "Hello World" pass described // in docs/WritingAnLLVMPass.html // //===----------------------------------------------------------------------===//
//hello_llvm.c #include<stdio.h> intadd(int a, int b) { return a+b; } intsub(int a, int b) { return a-b; } intmain(){ printf("hello LLVM\n"); return0; }
编译为bitcode:
clang -emit-llvm -c hello_llvm.c -o hello_llvm.bc
运行pass,识别函数名:
# root @ zzz in ~/Desktop $ opt -load ~/Desktop/llvm-project/build/lib/LLVMHello.so -hello < hello_llvm.bc > /dev/null -enable-new-pm=0 Hello : add Hello : sub Hello : main
注:若在最后运行时遇到opt: unknown pass name 'hello'报错,则可根据提醒,添加-enable-new-pm=0 参数,使用旧版的pass