简体   繁体   中英

How can I easily generate a list of symbols with static storage?

We have a large C++ project that we're building with both GCC and MSVC, and we've encountered the static initialization order fiasco . Is there a way to generate a list of symbols that take part in static initialization so I can generate a plan to resolve the problem?

I've created a map file from both GCC and MSVC. MSVC's output didn't look very useful. It appears that GCC's map file could be used - I extracted everything related to the bss section. However, many of the symbols come from libraries and just add noise to the information.

Is there a trick or some other convenient way to obtain the information I'm looking for (short of reading every source file manually)?

For Visual C++: Sort the lines of the .map file. This will ensure the symbols are ordered by address.

Search for symbols __xc_a and __xc_z . The symbols that appear between those two symbols are all of the dynamic initializers for objects with static storage duration. The initializers will be executed in the order they appear in the list.

Each entry in the .map file includes both

  1. the name of the global variable (eg, the initializer for global variable fred will be fred$initializer$ , plus the required C++ name decoration), and
  2. the object file that contained the global variable (eg fred.obj ). If the symbol came from a static library, the static library will be listed (eg libfred:fred.obj ).

(I don't know enough about GCC to answer how to do this with their tools.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM