我正在尝试理解readelf实用程序是如何计算函数大小的。我写了一个简单的程序
#include <stdio.h>
int main() {
printf("Test!\n");
}现在,为了检查函数大小,我使用了这个(这样可以吗?):
readelf -sw a.out|sort -n -k 3,3|grep FUNC
这产生了:
1: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@GLIBC_2.2.5 (2)
2: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@GLIBC_2.2.5 (2)
29: 0000000000400470 0 FUNC LOCAL DEFAULT 13 deregister_tm_clones
30: 00000000004004a0 0 FUNC LOCAL DEFAULT 13 register_tm_clones
31: 00000000004004e0 0 FUNC LOCAL DEFAULT 13 __do_global_dtors_aux
34: 0000000000400500 0 FUNC LOCAL DEFAULT 13 frame_dummy
48: 0000000000000000 0 FUNC GLOBAL DEFAULT UND puts@@GLIBC_2.2.5
50: 00000000004005b4 0 FUNC GLOBAL DEFAULT 14 _fini
51: 0000000000000000 0 FUNC GLOBAL DEFAULT UND __libc_start_main@@GLIBC_
58: 0000000000400440 0 FUNC GLOBAL DEFAULT 13 _start
64: 00000000004003e0 0 FUNC GLOBAL DEFAULT 11 _init
45: 00000000004005b0 2 FUNC GLOBAL DEFAULT 13 __libc_csu_fini
60: 000000000040052d 16 FUNC GLOBAL DEFAULT 13 main
56: 0000000000400540 101 FUNC GLOBAL DEFAULT 13 __libc_csu_init现在如果我检查main函数的大小,它显示为16,它是如何得出这个结果的?这是堆栈大小吗?
编译使用的是gcc 4.8.5版(Ubuntu 4.8.5-2ubuntu1~14.04.1)
GNU readelf (用于Ubuntu的GNU Binutils ) 2.24
发布于 2018-04-10 19:16:58
ELF符号具有指定其大小的属性st_size (参见<elf.h>):
typedef struct
{
...
Elf32_Word st_size; /* Symbol size */
...
} Elf32_Sym;此属性由生成二进制文件的工具链生成;例如,当查看由C编译器生成的汇编代码时:
gcc -c -S test.c
cat test.s您将看到类似这样的内容
.globl main
.type main, @function
main:
...
.LFE0:
.size main, .-main其中.size是一个特殊的as伪操作。
更新:
.size是代码的大小。
在这里,.size得到. - main的结果,其中".“是实际地址,main是main()开始的地址。
https://stackoverflow.com/questions/49751786
复制相似问题