65

My gcc build toolchain produces a .map file. How do I display the memory map graphically?

Makoto
  • 104,088
  • 27
  • 192
  • 230
JeffV
  • 52,985
  • 32
  • 103
  • 124
  • 1
    I am really interested in this question. Was hoping you would get some nice answers. But maybe everyone like me only upvoting your question and waiting :) Stay tuned.. Cheers - Prakash – prakash Sep 09 '08 at 12:13
  • Given a list of (address, symbol type, symbol name) tuples from a map file, it's not immediately obvious what types of graphs could be constructed. Could you revise the question to explain what you'd like to visualize? – David Joyner Sep 09 '08 at 13:47
  • 1
    try this one: http://www.absint.com/stackanalyzer/ i dont use GCC so I really cant answer but thought the link might be what you are after. – Mauro Sep 10 '08 at 12:28
  • I don't see why this was closed. There are no comments indicating the questions lack of focus and the question is very specific. "How to view a map file graphicaly". the question should be reopened. – Jay M Jun 27 '22 at 07:12
  • I can't add this answer as the question is closed but this tool does the job: https://www.sikorskiy.net/info/prj/amap/ – Jay M Jun 27 '22 at 07:12
  • Also this: https://github.com/PromyLOPh/linkermapviz – Jay M Jun 27 '22 at 07:22

2 Answers2

29

Here's the beginnings of a script in Python. It loads the map file into a list of Sections and Symbols (first half). It then renders the map using HTML (or do whatever you want with the sections and symbols lists).

You can control the script by modifying these lines:

with open('t.map') as f:
colors = ['9C9F84', 'A97D5D', 'F7DCB4', '5C755E']
total_height = 32.0

map2html.py

from __future__ import with_statement
import re

class Section:
    def __init__(self, address, size, segment, section):
        self.address = address
        self.size = size
        self.segment = segment
        self.section = section
    def __str__(self):
        return self.section+""

class Symbol:
    def __init__(self, address, size, file, name):
        self.address = address
        self.size = size
        self.file = file
        self.name = name
    def __str__(self):
        return self.name

#===============================
# Load the Sections and Symbols
#
sections = []
symbols = []

with open('t.map') as f:
    in_sections = True
    for line in f:
        m = re.search('^([0-9A-Fx]+)\s+([0-9A-Fx]+)\s+((\[[ 0-9]+\])|\w+)\s+(.*?)\s*$', line)
        if m:
            if in_sections:
                sections.append(Section(eval(m.group(1)), eval(m.group(2)), m.group(3), m.group(5)))
            else:
                symbols.append(Symbol(eval(m.group(1)), eval(m.group(2)), m.group(3), m.group(5)))
        else:
            if len(sections) > 0:
                in_sections = False


#===============================
# Gererate the HTML File
#

colors = ['9C9F84', 'A97D5D', 'F7DCB4', '5C755E']
total_height = 32.0

segments = set()
for s in sections: segments.add(s.segment)
segment_colors = dict()
i = 0
for s in segments:
    segment_colors[s] = colors[i % len(colors)]
    i += 1

total_size = 0
for s in symbols:
    total_size += s.size

sections.sort(lambda a,b: a.address - b.address)
symbols.sort(lambda a,b: a.address - b.address)

def section_from_address(addr):
    for s in sections:
        if addr >= s.address and addr < (s.address + s.size):
            return s
    return None

print "<html><head>"
print "  <style>a { color: black; text-decoration: none; font-family:monospace }</style>"
print "<body>"
print "<table cellspacing='1px'>"
for sym in symbols:
    section = section_from_address(sym.address)
    height = (total_height/total_size) * sym.size
    font_size = 1.0 if height > 1.0 else height
    print "<tr style='background-color:#%s;height:%gem;line-height:%gem;font-size:%gem'><td style='overflow:hidden'>" % \
        (segment_colors[section.segment], height, height, font_size)
    print "<a href='#%s'>%s</a>" % (sym.name, sym.name)
    print "</td></tr>"
print "</table>"
print "</body></html>"

And here's a bad rendering of the HTML it outputs:

Map

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
  • Thanks. It can be made more accurate by taking into account the 1 pixel gap. Also, you would want to make links in this "map" to some kind of key/dictionary section on the page. But yeah, this could be useful. – Frank Krueger Sep 26 '08 at 22:09
  • Did you write it. IF so, You wrote this script to answer this question? or you wrote it for your regular use? – claws Mar 24 '10 at 08:28
  • @Frank Krueger, I have never used python before, I just installed the latest version v.3.2 or Windows and was unable to run the above script. Could somebody please fix the script, it is probably just minor issues. – theAlse Aug 02 '11 at 07:39
  • Doesn't work at all on my map file unfortunately. It doesn't help that the .map format seems to be not very computer-readable. – Timmmm Oct 21 '16 at 08:51
  • This appears to use pre-2.4 version of python. I tried using it on Python 3.x, had to add parens to the print statements, but then it got hung up on the sort function, using a lambda function where it expected a key argument. I don't know python & lambdas well enough to progress past that right now. – Nerf Herder Jan 04 '17 at 23:13
  • @Frank Krueger: amazing piece of code! Could one also extract all this data from the .elf file? Or is it only in the .map file? – K.Mulier Apr 11 '19 at 18:55
8

I've written a C# program to display the information in a Map file along with information not usually present in the map file (like static symbols provided you can use binutils). The code is available here. In short it parses the map file and also uses BINUTILS (if available) to gather more information. To run it you need to download the code and run the project under visual studio, browse to the map file path and click Analyze.

Note: Only works for GCC/LD map files

Screenshot: [3]

stevieb
  • 9,065
  • 3
  • 26
  • 36
Sredni
  • 410
  • 5
  • 10
  • 1
    Just linking to your own stuff is [not a good answer](//stackoverflow.com/help/promotion). A good answer that involves an off-site resource includes the essential parts of the answer here for future users, and references the following: _What is this thing you're talking about? Where do I install it? How do I install it? How do I use this thing to solve the **exact problem** I have in my question? Are you affiliated with this thing in any way, shape, or form?_ See: [How can I link to an external resource in a community-friendly way?](//meta.stackexchange.com/questions/94022) – Mogsdad Mar 09 '16 at 16:49
  • OK I've made some edits. Beyond this, I don't see anything else I can add. *What is this thing you're talking about?* - Self evident - it's a map file parser/viewer. *Where/How do I install it?* - Windows, run project using visual studio *How do I use this thing to solve the exact problem I have in my question?* - Load the map file, run the software *Are you affiliated with this thing in any way, shape, or form?* - Err yeah I wrote it. – Sredni Mar 10 '16 at 06:36
  • Will this also work with `mono`? – user1273684 Jan 26 '18 at 15:13
  • It might, but I have not tried it. The main external dependency is ObjectListView which according to their [faq](http://objectlistview.sourceforge.net/html/faq.htm) *mostly works* on Mono. – Sredni Jan 28 '18 at 03:29
  • 2
    Irrespective of others comments, I like this tool. I have used it several times now and it downloads, builds and just works in seconds! – Ashley Duncan May 03 '18 at 09:15