Why do applications compiled by GCC always contain the _mcount symbol?
gcc, profiling, shared-libraries, symbols
Solution
Hm. strange, on my machine (ubuntu 9.10) this does not happen.
For a test I just compiled a small hello-word:
#include <stdio.h>
int main (int argc, char **args)
{
printf ("hello world\n");
}
compiled with
gcc test.c
It didn't has the _mcount symbol. I checked with:
nm a.out | grep -i count
Trying some compiler switches (-g, -pg ect.) it turns out that the symbol only appears if you compile your application with -pg, In this case you compile with profiling enabled, so the _mcount symbol has a reason to exist.
Problem
Libraries don't always contain the _mcount symbol, but applications do (you can verify this with gobjdump or the nm utility). I've read that _mcount is used to implement profiling, but the symbol is present even when profiling is disabled and optimization is enabled (-O2). Does it serve some other additional purpose? Update: I am on Solaris, so this is the Solaris linker combined with GCC, I'm not sure if that makes a difference or not. The GCC version is 4.2.2. This happens even if I compile a file that only contains the code `int main() { return 0; }` with no libraries linked. Update2: I type: ``` $ g++ -O2 mytest.cpp $ nm a.out | grep _mcount [65] | 134547444| 1|FUNC |GLOB |0 |11 |_mcount ``` And g++ is not aliased to anything. Also, I tried compiling with the sun CC compiler, and it doesn't have this issue. I also tried updating GCC, symbol still exists in 4.4.1.