I am trying to instrument a program to insert a function call after SP-Update instructions. SP-Update instructions are those modify the esp register such as mov esp, eax ; xchg eax, esp ; add esp, [eax+0x20] and so on. It seems that I should dig into the back end. But which representation of instructions should I focus on? MachineInstr or MCInst or other class? Where is the definitions of all opcodes and registers? In which part of the back end should I inspect the instructions? And How to insert a function call? May I use MachineInstrBuilder::BuildMI ? I am so sorry to ask so many questions because I am a newbie to LLVM. I would appreciate it if any one coulld help me.
You should write a MachineFunctionPass and operate and the MachineInstr level. Your pass will need to be integrated into the LLVM code generator for your target. For MachineInstr’s, registers are just numbers, and there are constants (e.g., Reg::RAX, IIRC) that represent the various registers. You can look at the control-flow integrity pass for a dated (but I think still useful) example of how to write a MachineFunctionPass. It is at . BuildMI is what you want. If you have not done so already, you should read the document on “How to Write an LLVM Pass” and the document on the code generator (the one that briefly explains MachineInstr’s). You should also make use of the doxygen documentation. Regards, John Criswell