Hind Forsum Hind Forsum - 2 months ago 11
Linux Question

When do we need to use gcc/g++ to generate Map files? "-g" is enough for debug?

If we want to use gdb to debug a release version executable, we make sure we compile using -g and then use objcopy to trim the debug information. This debug information can be used to locate source file lines when going through source files.

But when/why do we also need to generate Map files, any requirement or ability of gdb relies on Map files?

Thanks.

Answer

But when/why do we also need to generate Map files, any requirement or ability of gdb relies on Map files?

Assuming you are talking about the linker -M (synonym to --print-map) and -Map options, these are purely debugging aids intended to tell you what code/data is being pulled into your binary (and why).

GDB does not need or use this output (and the output is not easily parseable, i.e. intended for human consumption only).

Update:

when and how do we use this map file?

As I said before, the file answers questions like:

  • which object or library did the given function come from?
  • how much space did in consume in .text or .data section?
  • why did this particular object file get selected into the link from an archive library (i.e. which unresolved symbol caused this object file to be linked in)?

You use the map file when you have one of the above questions. If you don't have such questions, then you don't have any need for the map file.

As for "how" -- just read it with your favorite editor or pager. If you have a specific question on how to interpret particular lines there, do ask a separate specific question.