3

I'm studying about compilers, and I suddenly got curious that if I compile a program (let's say test.c) with -g option, does an actual file with a symbol table gets created and stored somewhere or it just gets combined with the executable?

I am totally new, so I am really learning the basics and may sound stupid question to some of you.. sorry, but please let me know!

Thank you!

1 Answers1

5

The ELF object file format (which is what gcc generates on many common platforms) is a container format which can contain lots of different types of information. It always contains a symbol table of externally linked symbols (either defined or needed from a library). In addition, if -g was specified, the ELF file will contain lots of information only used by the debugger, including all the local symbols used.

You can use objdump --debugging or readelf --debug-dump to look at the debugging information in a somewhat human-readable format. Good luck!

One thing generally not contained in the debugging information is the actual source code. What you'll find is the name of the source file and line numbers corresponding to different blocks of compiled code. The debugger reads the source code file to report source lines; that will not work as expected if the source code file has been edited since the executable was compiled.

rici
  • 234,347
  • 28
  • 237
  • 341
  • This is very helpful! Thank you so much! – user10378354 Sep 20 '18 at 09:27
  • This answer could be improved: symbol table != debug info, and there could be 0, 1 or 2 separate symbol tables in an executable. – Employed Russian Sep 20 '18 at 14:40
  • @employed: yes, i could have written a book about the subject, but I chose to go for a brief overview wirh some tools which OP can use to improve their learning process. Because they will learn a lot more that way. But I don't think my answer is misleading; it implies most of your points. – rici Sep 20 '18 at 15:21