Files

133 lines
3.9 KiB
Python

'''
Created on 1 okt. 2012
@author: vincentb
'''
from __future__ import print_function
import sys
ALLOC_NONE, ALLOC_ROM, ALLOC_RAM, ALLOC_ROMRAM = range(4)
class Section():
def __init__(self, name, lma, vma, size, alloc, load):
self.name = name
self.lma = lma
self.vma = vma
self.size = size
self.alloc = alloc
self.load = load
if not alloc and not load:
self.symantics = ALLOC_NONE
elif alloc and not load:
self.symantics = ALLOC_RAM
else:
if lma == vma:
self.symantics = ALLOC_ROM
else:
self.symantics = ALLOC_ROMRAM
self.sym = {}
def __str__(self):
return "(SECTION \"%s\" address: logic 0x%x, virtual 0x%x, size: %d, symbols: %d, alloc: %s, load: %s)" % \
(self.name, self.lma, self.vma, self.size, len(self.sym), self.alloc, self.load)
class Symbol():
def __init__(self, name, section, addr, size):
self.name = name
self.addr = addr
self.size = size
def __str__(self):
return "SYM \"%s\" addr: 0x%x, size: %d" % (self.name, self.addr, self.size)
def readobjdump(fn):
sections = {
'*ABS*' : Section('*ABS*', 0, 0, 0, False, False),
'*UND*' : Section('*UND*', 0, 0, 0, False, False)
}
f = open(fn)
l = f.readline()
while l != '':
info = l.strip().split()
if len(info) < 4:
l = f.readline()
continue
if info[1].startswith("."):
name = info[1]
size = int(info[2], 16)
vma = int(info[3], 16)
lma = int(info[4], 16)
flags = f.readline().strip().split(", ")
section = Section(name, lma, vma, size, "ALLOC" in flags, "LOAD" in flags)
sections[section.name] = section
if info[-3] in sections:
name = info[-1]
size = int(info[-2], 16)
section = info[-3]
addr = int(info[0], 16)
sym = Symbol(name, section, addr, size)
sections[section].sym[sym.name] = sym
l = f.readline()
f.close()
return sections
if __name__ == "__main__":
sections = readobjdump(sys.argv[1])
rom = 0
ram = 0
params = {}
if len(sys.argv) > 2:
for itm in sys.argv[2:]:
p = itm.find('=')
if p < 0: continue
v = itm[p+1:]
if v.startswith("0x"):
params[itm[0:p]] = int(v[2:], 16)
else:
params[itm[0:p]] = int(v)
print(params)
for section in sections.values():
if section.alloc or section.load:
print("Section ", section.name, ", Size: ", section.size)
if section.symantics == ALLOC_RAM or section.symantics == ALLOC_ROMRAM:
ram += section.size
if section.symantics == ALLOC_ROM or section.symantics == ALLOC_ROMRAM:
rom += section.size
syms = sorted(section.sym.values(), key=lambda s : s.size, reverse = True)
if syms[0].size > 0:
stopAt = syms[0].size / 10
for sym in syms[0:20]:
if sym.size < stopAt: break
print(" - %8d %s" % (sym.size, sym.name))
print("")
if "ROM" in params:
rom_full = " (%0.1f%%, %d bytes free)" % (rom * 100 / params['ROM'], params['ROM'] - rom)
else:
rom_full = ""
if "RAM" in params:
ram_full = " (%0.1f%%, %d bytes free)" % (ram * 100 / params['RAM'], params['RAM'] - ram)
else:
ram_full = ""
print("Total memory usage: %d ROM%s, %d RAM%s" % ( rom, rom_full, ram, ram_full))