Finding static initializers and destructors in C++
Asked Answered
M

2

8

I have a program with way too many static initializers and destructors. I want to get rid of all of them. So i need a way to find them.

Running nm on the executable gives something like this: 0004bfc0 t _Z41_static_initialization_and_destruction_0ii

Is there a good way to get a list of files from where static_initializers are being included?

Mastectomy answered 5/2, 2011 at 2:3 Comment(2)
possible duplicate of Determine static initialization order after compilation?Bookkeeper
Not a duplicate. This question is about finding all static initializers, the linked question is about predicting the order in which they are executed.Vincenz
T
3

you could run nm on an object file which is later linked into the final executable. or create a script to parse nm's output for you if you've a lot to go through.

depending on the definitions of the data, you may also find you have duplicates which could be reduced to one object.

Tour answered 5/2, 2011 at 7:25 Comment(0)
R
0

If you're using Microsoft Visual C++'s PDB file format on Windows, you can use this script:

llvm-pdbutil dump --globals file.pdb | awk '/addr = / { if (sym) { off = $0; sub(/.*addr = [0-9]*:/, "", off); syms[sym] = +off; inv[+off] = sym; } } { sym = ""; } /`.*`/ { sym = $0; sub(/`[ \t]*$/, "", sym); sub(/.*`/, "", sym); } END { for (k in inv) { if (syms["__xc_a"] < +k && +k < syms["__xc_z"]) { print(inv[k]); } } }'

It will print all C++ initializers.

Alternatively, if you're inside Visual Studio, you can Watch this expression:

__xc_a+1,[__xc_z - __xc_a - 1]

It will list all C++ initializers when you expand the array.

Rapid answered 2/9 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.