What are gcc linker map files used for?

Monku picture Monku · Mar 5, 2014 · Viewed 15.7k times · Source

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

Answer

Garr Godfrey picture Garr Godfrey · Apr 20, 2016

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.