Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run LLVM pass with opt

Tags:

llvm

I have just started to work with LLVM. I have wrote my own Hello pass, which worked fine.

Now I want to run opt with the stack protector pass, from StackProtector.cpp, but I am having trouble with that. When I look at the source code, it looks like I should use the flag -stack-protector:

INITIALIZE_PASS(StackProtector, "stack-protector", "Insert stack protectors", false, false)

But this flag is not recognized by opt.

I am not sure which file to "load", as it is not as simple as loading my own LLVMHello.so file and I could not find a StackProtector.so file; I believe this might be the problem.

Edit:

I finally got an answer from LLVMDev. Actually, the pass I wanted to run is performed by llc, not opt. I could not find the option -stack-protector, though, with

llc --help

because this option is hidden. If instead I do

llc --help-hidden

it is shown that the pass is there, and I just need to run

llc -print-before=stack-protector <input>
like image 716
Izabela Avatar asked Dec 15 '25 19:12

Izabela


1 Answers

First you add in your pass :

static RegisterPass<StackProtector> X("StackProtector", "Insert stack protectors", false, false);

Second, in the terminal when you run the pass on a target file, after you run make, you have something like:

//home/YOURNAME/llvm/Release+Asserts/bin/opt -load //home/YOURNAME/llvm/Release+Asserts/lib/StackProtector.so -StackProtector //home/YOURNAME/llvm/tools/clang/woRKSPACE/Test.bc

where Test.bc is your target code. Also, be aware: in your Makefile, don't forget to add LIBRARYNAME = StackProtector.

Also, be aware if the pass in not already registered (if so, you will get a segfault error)

like image 122
Alex Avatar answered Dec 18 '25 04:12

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!