I am trying to write a python script to analyse a .map file. However, i am only getting .text and .bss files in the output. I would like the output to have all .text, .data, .rodata and .bss. with comment and size. I have tried outputting outputting it to a text file at various stages and confirm that the regex works. I am new to this kind of thing in python and would appreciate any help.
i am trying to get the ouput in the form:
".text" "comment" size
So far i have written this code:
import sys, re, os
from itertools import chain, groupby
class Objectfile:
def __init__ (self, section, offset, size, comment):
self.section = section.strip ()
self.offset = offset
self.size = size
self.path = (None, None)
self.basepath = None
if comment:
self.path = re.match (r'^(.+?)(?:\(([^\)]+)\))?$', comment).groups ()
self.basepath = os.path.basename (self.path[0])
self.children = []
def __repr__ (self):
return '<Objectfile {} {:x} {:x} {} {}>'.format (self.section, self.offset, self.size, self.path, repr (self.children))
def parseSections (fd):
"""
Quick&Dirty parsing for GNU ld’s linker map output, needs LANG=C, because
some messages are localized.
"""
sections = []
# skip until memory map is found
found = False
while True:
l = fd.readline ()
if not l:
break
if l.strip () == 'Memory Configuration':
found = True
break
if not found:
return None
# long section names result in a linebreak afterwards
sectionre = re.compile ('(?P<section>.+?|.{14,}\n)[ ]+0x(?P<offset>[0-9a-f]+)[ ]+0x(?P<size>[0-9a-f]+)(?:[ ]+(?P<comment>.+))?\n+', re.I)
subsectionre = re.compile ('[ ]{16}0x(?P<offset>[0-9a-f]+)[ ]+(?P<function>.+)\n+', re.I)
s = fd.read ()
pos = 0
while True:
m = sectionre.match (s, pos)
if not m:
# skip that line
try:
nextpos = s.index ('\n', pos)+1
pos = nextpos
continue
except ValueError:
break
pos = m.end ()
section = m.group ('section')
v = m.group ('offset')
offset = int (v, 16) if v is not None else None
v = m.group ('size')
size = int (v, 16) if v is not None else None
comment = m.group ('comment')
if section != '*default*' and size > 0:
of = Objectfile (section, offset, size, comment)
if section.startswith (' '):
sections[-1].children.append (of)
while True:
m = subsectionre.match (s, pos)
if not m:
break
pos = m.end ()
offset, function = m.groups ()
offset = int (offset, 16)
if sections and sections[-1].children:
sections[-1].children[-1].children.append ((offset, function))
else:
sections.append (of)
return sections
def main ():
f = open(r'C:\Users\rusty12\Desktop\MAP FIle Analysis\AnchorBle_Fbl.map')
sections = parseSections (f)
if sections is None:
print ('start of memory config not found, did you invoke the compiler/linker with LANG=C?')
return
sectionWhitelist = {'.text', '.data', '.bss', '.rodata'}
whitelistedSections = list (filter (lambda x: x.section in sectionWhitelist, sections))
for s in whitelistedSections:
objects = s.children
groupsize = {}
for k, g in groupby (sorted (objects, key=lambda x: x.basepath), lambda x: x.basepath):
groupsize[k] = sum (map (lambda x: x.size, g))
objects.sort (reverse=True, key=lambda x: x.size)
for i in sorted (groupsize) :
print('\"{}\",\"{}\",{}'.format(s.section,i,groupsize[i]))
if __name__ == '__main__':
main ()
The Map file is in the form:
Memory Configuration
Name Origin Length Attributes
ROM 0x00000000 0x00012000 xr
RAM 0x20000200 0x0005fe00 rw
*default* 0x00000000 0xffffffff
Linker script and memory map
LOAD _builds/SVW/_gen/swb/anchorble/fbl/objects/AppHeader.o
LOAD _builds/SVW/_gen/swb/anchorble/fbl/objects/AppHeader_Cfg.o
LOAD _builds/SVW/_gen/swb/anchorble/fbl/objects/CanIf.o
.text.CanIf_ControllerModeIndication
0x00000788 0x78 _builds/SVW/_gen/swb/anchorble/fbl/objects/CanIf.o
0x00000788 CanIf_ControllerModeIndication
.bss.triggered_irq_count
0x20001df0 0x4 rbd/pk/dialog/DA1469x_SDK/sdk/interfaces/ble/binaries_AB/DA1469x-Release/libble_stack_da1469x.a(cmac_mailbox.c.obj)
.rodata.__sf_fake_stdin
0x00010f70 0x20 c:/dv5187/gcc/gcc-arm-none-eabi-7-2018-q2-update-win32/bin/../lib/gcc/arm-none-eabi/7.3.1/../../../../arm-none-eabi/lib/thumb/v8-m.main/fpv5-sp/hard\libg_nano.a(lib_a-findfp.o)
0x00010f70 __sf_fake_stdin
.data.rbUds_TxConfirmCfg_State
0x20002cc1 0x1 _builds/SVW/_gen/swb/anchorble/fbl/objects/rbUds_TxConfirmCfg.o
0x20002cc4 . = ALIGN (0x4)
User contributions licensed under CC BY-SA 3.0