Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to find all static objects

Tags:

c++

I'd like to identify where all the objects of static storage duration are , in a large codebase; so that I can review to see if there are any potential problems with static initialization order.

Is there a good way to do this?

Merely searching the keyword static is not good enough, as it will miss any objects declared at namespace scope.

The linker's map file does indicate how big bss and data areas are , however it has all the names stripped out for symbols that are not extern.

Currently I am sifting through a dump of each object file looking for DATA and BSS, however that is painful and there is a lot of junk such as class vtables and compiler-generated static data.

like image 320
M.M Avatar asked Apr 11 '26 19:04

M.M


1 Answers

Disclaimer: this is a fairly localized and incomplete answer. I leave it here in the hope that someone may benefit from it still (and maybe build on it).

Using the gcc toolchain, at startup __main calls __do_global_ctors which does a backward traversal of __CTOR_LIST__. Using nm on a .so library, for example, I get:

00000000004e2040 d __CTOR_END__
00000000004e2000 d __CTOR_LIST__
00000000004e2050 d __DTOR_END__
00000000004e2048 d __DTOR_LIST__

From then on, I suppose you could get from those addresses to the effective functions being executed; however, as you noticed, mapping back to the source name might be awkward (especially in anonymous namespaces). You may be able to recover them from the debugging information though (source location), however I have not progressed so far.

like image 182
Matthieu M. Avatar answered Apr 13 '26 09:04

Matthieu M.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!