xiaogw
xiaogw

Reputation: 745

Replace a call instruction with LLVM

I’m a beginner in LLVM. And I want to replace all the call instructions in a program with “push next instruction address on stack, jump to callee function”. So does anyone know where can I implement this replacement? Write a pass and insert into the clang compiling chain? And how can I implement this replacement?

Thanks!

Upvotes: 1

Views: 2069

Answers (1)

Mike C. Delorme
Mike C. Delorme

Reputation: 86

I would suggest creating a new LLVM instrinsic. You will have to provide a target-specific definition for your intrinsic so that the compiler knows what machine code to generate. In your case you would provide a target-specific definition that implements push+jump.

Once you have your intrinsic you can create an LLVM pass that searches the LLVM IR for all call instructions that aren't to your intrinsic and replaces them with call instructions that are to your intrinsic.

Documentation on adding a new intrinsic function: http://llvm.org/docs/ExtendingLLVM.html

Tutorial on how to implement your own LLVM pass: http://llvm.org/docs/WritingAnLLVMPass.html

Upvotes: 1

Related Questions