Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are gcc linker map files used for?

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

like image 486
Monku Avatar asked Mar 05 '14 13:03

Monku


People also ask

What is linker map file?

A linker map is a file produced by the linker. The file shows the symbols and sections contained in a binary. The linker also provides the memory address and size for each symbol.

What is the purpose of a .map file?

More generically, the . MAP extension can be used to denote any file type that indicates relative offsets from a starting point. MAP file types are used in this way, for example, when variables in software are expected to be "mapped" to a specific spot in memory.

Which linker does GCC use?

But gcc does not link object files. Instead it uses collect2 which is just wrapper for the GNU ld linker: ~$ /usr/lib/gcc/x86_64-linux-gnu/4.9/collect2 --version collect2 version 4.9.

What is the role of linker script file?

A linker script is a file that tells the linker which sections to include in the output file, as well as which order to put them in, what type of file is to be produced, and what is to be the address of the first instruction.


2 Answers

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.

like image 172
Garr Godfrey Avatar answered Sep 18 '22 15:09

Garr Godfrey


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.

like image 31
Employed Russian Avatar answered Sep 16 '22 15:09

Employed Russian