The motivation behind this post is to investigate that which sections variables belong to, i.e. a global variable, static local variable, a constant variable or a static const variable.The answer may vary from different situations due to different strategies adopted by compilers. Thus, this post is some guide about how to find the answers.
Short answer: the easiest way is probably to compile C/C++ code into assemble code using ‘gcc -S’. There are other tools that can be used to inspect object files to find more information, such as nm, objdump, and readelf.
- nm – a handy utility to list symbols from object files
- objdump – display information about object files
- readelf – read information of ELF file (the binary format in Unix/Linux system)