everyone.
I'm working on a C project and I need a way to add assembly code in the prologue and epilogue of every function in the source code.
Let me give you an example.
This is the source code:
int main(int argc, char *argv[]){
char buff[20];
int a = 20;
printf("Variable a: %d\n", a);
}
The relative assemly code is this:
0x0000000000001169 <+0>: endbr64
0x000000000000116d <+4>: push rbp
0x000000000000116e <+5>: mov rbp,rsp
0x0000000000001171 <+8>: sub rsp,0x40
0x0000000000001175 <+12>: mov DWORD PTR [rbp-0x34],edi
0x0000000000001178 <+15>: mov QWORD PTR [rbp-0x40],rsi
0x000000000000117c <+19>: mov rax,QWORD PTR fs:0x28
0x0000000000001185 <+28>: mov QWORD PTR [rbp-0x8],rax
0x0000000000001189 <+32>: xor eax,eax
0x000000000000118b <+34>: mov DWORD PTR [rbp-0x24],0x14
0x0000000000001192 <+41>: mov eax,DWORD PTR [rbp-0x24]
0x0000000000001195 <+44>: mov esi,eax
0x0000000000001197 <+46>: lea rdi,[rip+0xe66] # 0x2004
0x000000000000119e <+53>: mov eax,0x0
0x00000000000011a3 <+58>: call 0x1070 <printf@plt>
0x00000000000011a8 <+63>: mov eax,0x0
0x00000000000011ad <+68>: mov rdx,QWORD PTR [rbp-0x8]
0x00000000000011b1 <+72>: xor rdx,QWORD PTR fs:0x28
0x00000000000011ba <+81>: je 0x11c1 <main+88>
0x00000000000011bc <+83>: call 0x1060 <__stack_chk_fail@plt>
0x00000000000011c1 <+88>: leave
0x00000000000011c2 <+89>: ret
Now what I want to do is find a way to automatically add my assembly code in the prologue and epilogue when I compile it. And then get a situation like this:
0x0000000000001169 <+0>: endbr64
0x000000000000116d <+4>: push rbp
0x000000000000116e <+5>: mov rbp,rsp
0x0000000000001171 <+8>: sub rsp,0x40
0x0000000000001175 <+12>: mov DWORD PTR [rbp-0x34],edi
0x0000000000001178 <+15>: mov QWORD PTR [rbp-0x40],rsi
0x000000000000117c <+19>: mov rax,QWORD PTR fs:0x28
0x0000000000001185 <+28>: mov QWORD PTR [rbp-0x8],rax
// instrument code here...
0x0000000000001189 <+32>: xor eax,eax
0x000000000000118b <+34>: mov DWORD PTR [rbp-0x24],0x14
0x0000000000001192 <+41>: mov eax,DWORD PTR [rbp-0x24]
0x0000000000001195 <+44>: mov esi,eax
0x0000000000001197 <+46>: lea rdi,[rip+0xe66] # 0x2004
0x000000000000119e <+53>: mov eax,0x0
0x00000000000011a3 <+58>: call 0x1070 <printf@plt>
0x00000000000011a8 <+63>: mov eax,0x0
// instrument code here...
0x00000000000011ad <+68>: mov rdx,QWORD PTR [rbp-0x8]
0x00000000000011b1 <+72>: xor rdx,QWORD PTR fs:0x28
0x00000000000011ba <+81>: je 0x11c1 <main+88>
0x00000000000011bc <+83>: call 0x1060 <__stack_chk_fail@plt>
0x00000000000011c1 <+88>: leave
0x00000000000011c2 <+89>: ret
I've done a lot of research on this. And I couldn't find anything that would explain how to do it.
I know that the gcc compiler offers the -finstrument-functions option to instrument the functions, but is not what I need. That's because that option involves adding these two functions:
void __cyg_profile_func_enter(void *func, void *callsite);
void __cyg_profile_func_exit(void *func, void *callsite);
that in the relevant assembly turn out to be calls to those functions, and that's not what I want.
I've read that through a gcc plugin maybe you can get this result, but the documentation is really poor.
I also read something about DynamoRio, but I think what I need is a static and not dynamic instrumentation system.
Can someone point me in the right direction? Thank you all in advance!