Reputation: 141
I'm new to LLVM and trying to use LLVM to do instrumentation. Specifically, a call doing some check work is expected to be inserted after SP-Update instructions. SP-Update instructions are those implicitly or explicitly modify the esp register. For example, mov esp, eax is an explicit SP-Update instruction and pop eax is an implicit one. I have to localization these instructions and add my check code after them. Intuitively, I need to modify the backend part. But I don't know which classes or functiones I should dig into. If anyone could help me? Thanks a lot in advance.
Upvotes: 2
Views: 667
Reputation: 91
Usually llvm prepares the frames for the generated functions and I think that you're interested in the inline assembly more than in the normal updates for the esp register.
Anyway, you can go to the lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp source and modify the function X86ATTInstPrinter::printInst so that for the instructions in question it would print some additional code.
Something like that:
if (MI->getOpcode() == X86::PUSH16r
|| MI->getOpcode() == X86::PUSH32r
|| MI->getOpcode() == X86::PUSH64r) {
OS << "\tcall your_function\n";
}
So suppose you added the code in the place and compiled the llc binary of llvm. There's an example on how you use that:
m.c:
#include <stdio.h>
void your_function() {
printf("your_function called\n");
}
void foo();
int main() {
foo();
}
t.c:
void foo() {
asm("push %rax");
asm("pop %rax");
}
then you do the following commands using your built llvm binaries:
clang t.c -c -emit-llvm
llc t.bc
llvm-mc t.s -o t.o -filetype=obj
clang m.c t.o -o ttt
now you can run the file and get the needed result:
./ttt
your_function called
your_function called
btw, note, that the two prints are from push instruction one of which is in the inline assembly while another one is in the llvm generate frame of foo function, not from the pop instruction
Upvotes: 3