Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Develop and debug the first LLVM Pass

Shulou Source: shulou.com Published: 2022-06-01 06:24:57 09月16日 Update

1. Download and compile LLVM

LLVM download address http://releases.llvm.org/download.html

The latest version is 6.0.0. After the download is complete, execute tar to extract the llvm package:

Tar-xzvf llvm-6.0.0.src.tar.xz

Download the Clang and decompress:

Tar-xzvf cfe-6.0.0.src.tar.xz

Copy the cfe-6.0.0.src directory to llvm-6.0.0.src/tools and rename it clang. Install cmake through brew:

Brew install cmake

Then change to the llvm-6.0.0.src directory under the command line, create a new build directory, and change to the build directory to execute the cmake generation configuration file. The command is as follows:

Mkdir buildcd buildcmake-G Xcode CMAKE_BUILD_TYPE= "Debug"..

After the execution is completed, you can see the project file LLVM.xcodeproj of Xcode. When you open the project file, you will be prompted whether to create Schemes automatically, as shown in the figure:

Click Manually Manage Schemes to create manually and add ALL_BUILD, as shown in the figure:

Then compile on Xcode, the compilation process, such as slow, may take about 20 minutes to half an hour, all the compiled files will be about 11G.

two。 Write Pass code

The function of writing the first Pass is very simple, just output each method name in the program, the steps are as follows:

(1) create a new folder under the lib/Transforms directory with the folder name of TestPass, and create the file TestPass.cpp under the TestPass directory as follows:

/ / Import the header file # include "llvm/ADT/Statistic.h" # include "llvm/IR/Function.h" # include "llvm/Pass.h" # include "llvm/Support/raw_ostream.h" using namespace llvm; / / specify the LLVM namespace namespace {/ / define the First class inherited from FunctionPass struct First: public FunctionPass {static char ID / / Pass ID First (): FunctionPass (ID) {} / / define runOnFunction to overload the abstract virtual function bool runOnFunction (Function & F) override {/ / print the name of each function that inherits the parent class

Tags: Compile file directory parameter such as figure code function command project success name address effect folder program switch generate output run develop Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Linux Huawei vpn