14

What are the ".map" files generated by gcc/g++ linker option "-Map" used for ? And how to read them ?

Monku
  • 2,440
  • 4
  • 33
  • 57

3 Answers3

14

I recommend generating a map file and keeping a copy for any software you put into production.

It can be useful for deciphering crash reports. Depending on the system, you likely can get a stack dump from the crash. The stack dump will include memory addresses and one of the registers will include the Instruction Pointer. That tells you the memory address code was executing at. On some systems, code addresses can be moved around (when loading dynamic libraries, hence, dynamic), but the lower order bytes should remain the same.

The map file is a MAP from memory location -> code location. It gives you the name of the function at a given memory address. Due to optimizations, it may not be extremely accurate, but it gives you a place to start in terms of looking for bugs that cause the crash.

Now, in 30 years of writing commercial software, this is the only thing I've used the map files for. Twice successfully.

Garr Godfrey
  • 8,257
  • 2
  • 25
  • 23
  • One might take it one step further and do `objdump --disassemble --source` to get a very useful dump which can be used for crash analysis. Great to have when the target environment is not capable of providing a proper source level debugger. More useful than a map file because it shows source code details interleaved with the instructions, which helps when a lot of inlining is done. – doug65536 Dec 01 '16 at 11:34
  • 2
    While the proposed technique of keeping a copy of the map is useful, in 99.9% of the cases it is significantly inferior to keeping a copy of unstripped binary with full debug info. – Employed Russian Mar 05 '17 at 16:09
  • Why would linker's map be inaccurate? You mention a cause for this as optimizations, but optimizations occur during compilation before the linker and such a map are generated. – sherrellbc Jul 13 '17 at 16:45
  • an optimized object file won't have all the object code in tidy chunks that correspond to line numbers or even functions. So you might have a map file that gives you all the entry points of functions, not all of that function exists between that address and the next mapped address. – Garr Godfrey Jul 14 '17 at 20:13
  • Now, the reason I've used these so rarely is that these crashes are generally unreproducible. A debug build won't help. All you get is a couple addresses for the "once in a blue moon" crashes – Garr Godfrey Oct 28 '20 at 20:48
4

What are the ".map" files generated by gcc/g++ linker option "-Map" used for?

There is no such thing as 'gcc linker' -- GCC and linker are independent and separate projects.

Usually the map is used for understanding decisions that ld made while linking the binary. From man ld:

-M
   --print-map
       Print a link map to the standard output.
       A link map provides information about the link, including the following:
       ·   Where object files are mapped into memory.
       ·   How common symbols are allocated.
       ·   All archive members included in the link, with a mention of the symbol which caused the archive member to be brought in.
       ·   The values assigned to symbols.
       ...

If you don't understand what that means, you likely don't (yet) have the questions that this output answers, and hence have no need to read it.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • 30
    _If you don't understand what that means, you likely don't (yet) have the questions that this output answers, and hence have no need to read it._ - Well, the OP has to start somewhere, shouldn't s/he? More often than not, scraping through dust one finds gems :) – legends2k Mar 06 '14 at 06:56
0

The compiler gcc is one program that generates object code files, the linker ld is a second program to combine the object code files into an executable. The two can be combined into a single command line.

If you are generating a program to run on an ARM processor you need to use arm-none-eabi-gcc and arm-none-eabi-ld so that the code will be correct for the ARM architecture. Gcc and ld will generate code for your host computer.