c++符号分析:如何确定执行了哪些静态初始化
C++ symbol analysis: how to determine which static initialization is performed?
我想分析是什么原因导致我的共享c++库在Linux上由GCC (v.6.1.1)编译。
readelf -sW libfoo.so
告诉我有特别大的函数叫做__static_initialization_and_destruction_0
,例如:
000000000026c420 10272 FUNC LOCAL DEFAULT 12 __static_initialization_and_destruction_0(int, int) [clone .constprop.1774]
我将-Wl,-Map,foo.map
添加到CXX
标志以生成链接器映射文件。在该映射文件中查找0x000000000026c420
产生:
.text.startup 0x000000000026c420 0x2825 CMakeFiles/foo.dir/bar.cpp.o
所以现在我知道bar.cpp
是导致静态初始化的转换单元,但上述文件不包含任何static
变量。但是,它包含了很多头文件。
我如何找出哪些变量在这些函数中被静态初始化?
编译你的程序 g3佤邦,-adhln
您将获得带有源代码的汇编代码。在汇编代码中,您将发现如下内容:
_Z41__static_initialization_and_destruction_0ii:
所有定义静态变量的代码都将在这里提及,直到下一次返回(ret
)。
源:
struct Foo {
Foo() {}
};
static Foo a;
static Foo b;
编译:g++ text.cpp -c -O0 -g3 -Wa,-ahln > out.txt
组装:35 _Z41__static_initialization_and_destruction_0ii:
36 .LFB3:
3:text.cpp **** };
4:text.cpp ****
5:text.cpp **** static Foo a;
6:text.cpp **** static Foo b;
37 .loc 1 6 0
38 .cfi_startproc
39 0000 55 pushq %rbp
40 .cfi_def_cfa_offset 16
41 .cfi_offset 6, -16
42 0001 4889E5 movq %rsp, %rbp
43 .cfi_def_cfa_register 6
44 0004 4883EC10 subq $16, %rsp
45 0008 897DFC movl %edi, -4(%rbp)
46 000b 8975F8 movl %esi, -8(%rbp)
47 .loc 1 6 0
48 000e 837DFC01 cmpl $1, -4(%rbp)
49 0012 751D jne .L4
50 .loc 1 6 0 is_stmt 0 discriminator 1
51 0014 817DF8FF cmpl $65535, -8(%rbp)
51 FF0000
52 001b 7514 jne .L4
5:text.cpp **** struct Foo {
Foo() {}
};
static Foo a;
static Foo b;
53 .loc 1 5 0 is_stmt 1 discriminator 3
54 001d BF000000 movl $_ZL1a, %edi
54 00
55 0022 E8000000 call _ZN3FooC1Ev
55 00
56 .loc 1 6 0 discriminator 3
57 0027 BF000000 movl $_ZL1b, %edi
57 00
58 002c E8000000 call _ZN3FooC1Ev
58 00
59 .L4:
60 .loc 1 6 0 is_stmt 0
61 0031 90 nop
62 0032 C9 leave
63 .cfi_def_cfa 7, 8
64 0033 C3 ret
65 .cfi_endproc
这两个54 001d BF000000 movl $_ZL1a, %edi
57 0027 BF000000 movl $_ZL1b, %edi
是静态变量。使用c++filt可以得到正确的变量名。
构造函数:
call _ZN3FooC1Ev
相关文章:
- 使用g++静态初始化带有命名标签的嵌套C++结构
- 静态初始化的反义词是什么?
- 在静态初始化期间运行代码
- 结构数组的嵌入式C++静态初始化
- 初始化与类类型相同的静态成员(静态初始化顺序问题)
- 使用 constexpr 和 std::array 进行静态初始化来替换动态初始化的 std::vector 的闰年
- MSVC 2017 在单个翻译单元中违反静态初始化顺序
- VC++6 线程安全静态初始化
- 具有必要副作用的静态初始化被优化掉了
- 未使用全球的静态初始化器
- w/ w/结构带char缓冲液的静态初始化[]
- 静态初始化的对象不能放在块中尝试
- 功能指针的静态初始化
- lambda(或函数)C++中的静态初始化
- C++静态初始化顺序:添加到映射中
- 复杂类型数组的静态初始化
- C++ - 在派生类中静态初始化受基类保护的成员变量
- 静态初始化插件工厂
- 静态初始化包含功能指针的对象的constexpr std ::数组
- 找出静态初始化是否结束