如何使用工具代码将汇编代码添加到所有功能的序言和结尾?

问题描述 投票:0回答:1

每个人。我正在研究C项目,我需要一种方法在源代码中每个函数的序言和结尾处添加汇编代码。

让我举个例子。

这是源代码:

int main(int argc, char *argv[]){

    char buff[20];
    int a = 20;
    printf("Variable a: %d\n", a);
}

相对组装代码是这样:

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    

现在,我想做的是找到一种在编译时自动在序言和结语中添加汇编代码的方法。然后得到这样的情况:

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

我对此进行了大量研究。而且我找不到任何可以解释如何做的东西。我知道gcc编译器提供了-finstrument-functions选项来检测功能,但这不是我所需要的。这是因为该选项涉及添加以下两个功能:

void __cyg_profile_func_enter(void *func, void *callsite); 
void __cyg_profile_func_exit(void *func, void *callsite);

在相关程序集中,原来是对这些函数的调用,而这不是我想要的。

我已经读过通过gcc插件也许可以得到这个结果,但是文档确实很差。我也阅读了有关DynamoRio的内容,但我认为我需要的是静态而非动态的仪表系统。

有人能指出我正确的方向吗?预先谢谢大家!

c gcc assembly instrumentation gcc-plugins
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.