40

If binary file size is not an issue, are there any drawbacks using -g and not strip binaries that are to be run in a performance critical environment? I have a lot of disk space but the binary is cpu intensive and uses a lot of memory. The binary is loaded once and is alive for several hours.

EDIT:

The reason why I want to use binaries with debugging information is to generate useful core dumps in case of segmentation faults.

Johan
  • 5,003
  • 3
  • 36
  • 50
  • I think the question boils down to whether symbols are loaded into memory by default or not. If they are, this would contribute to load time and memory consumption. Interesting question, looking forward to answers. +1 – 0xC0000022L May 09 '11 at 11:30
  • @unapersson: It's hard to make such tests in our environment, as the performance depends deeply on external factors that quickly varies. – Johan May 09 '11 at 11:45

1 Answers1

33

The ELF loader loads segments, not sections; the mapping from sections to segments is determined by the linker script used for building the executable.

The default linker script does not map debug sections to any segment, so this is omitted.

Symbol information comes in two flavours: static symbols are processed out-of-band and never stored as section data; dynamic symbol tables are generated by the linker and added to a special segment that is loaded along with the executable, as it needs to be accessible to the dynamic linker. The strip command only removes the static symbols, which are never referenced in a segment anyway.

So, you can use full debug information through the entire process, and this will not affect the size of the executable image in RAM, as it is not loaded. This also means that the information is not included in core dumps, so this does not give you any benefit here either.

The objcopy utility has a special option to copy only the debug information, so you can generate a second ELF file containing this information and use stripped binaries; when analyzing the core dump, you can then load both files into the debugger:

objcopy --only-keep-debug myprogram myprogram.debug
strip myprogram
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
  • Thank you for this cluefull explanation! To verify I understood correctly... A binary file built with -g will execute as fast as one that is stripped (built from the same source code), and they will both generate the same core dump in case of a segmentation fault? – Johan May 09 '11 at 13:46
  • Exactly. Also, stripping a binary file removes the information that -g generated/preserved, plus a bit more, so the result after stripping should be identical whether or not you compiled with -g. – Simon Richter May 09 '11 at 15:38