相关疑难解决方法(0)

如何从C程序内部或内联汇编中获取C函数的大小?

假设我有如下函数:

# cat 003.c

int foo(int a, int b)
{
    return a+b;
}
Run Code Online (Sandbox Code Playgroud)

并像这样编译它:

gcc -S 003.c
Run Code Online (Sandbox Code Playgroud)

得到以下汇编结果:

     .file   "003.c"
     .text
 .globl foo
     .type   foo, @function
 foo:
 .LFB2:
     pushq   %rbp
 .LCFI0:
     movq    %rsp, %rbp
 .LCFI1:
     movl    %edi, -4(%rbp)
     movl    %esi, -8(%rbp)
     movl    -8(%rbp), %edx
     movl    -4(%rbp), %eax
     addl    %edx, %eax
     leave
     ret
 .LFE2:
     .size   foo, .-foo /* size of the function foo, how to get it?*/
Run Code Online (Sandbox Code Playgroud)

上面的最后一行确实得到了函数的大小.编译器在哪里存储大小?我可以使用C或内联asm在我的原始C程序中以某种方式获取函数的大小吗?

c assembly gcc gnu-assembler elf

7
推荐指数
1
解决办法
7726
查看次数

标签 统计

assembly ×1

c ×1

elf ×1

gcc ×1

gnu-assembler ×1