From 2d3522beb80b1ff80f7b3b688937b320d2050b37 Mon Sep 17 00:00:00 2001 From: cruwaller Date: Tue, 8 Jul 2025 15:00:49 +0300 Subject: [PATCH] Fix SDCC map regex + some minor formatting --- script/sdcc_map.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/script/sdcc_map.py b/script/sdcc_map.py index 9917547..6559c45 100755 --- a/script/sdcc_map.py +++ b/script/sdcc_map.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 usage = """python sdcc_map.py - * Parses an sdcc linker .map file and outputs symbol, section, module + * Parses an sdcc linker .map file and outputs symbol, section, module information in .csv format * Requires python 3.8+ """ @@ -10,6 +10,7 @@ import re from types import SimpleNamespace + class Symbol: def __init__(self, name, address, module, section): self.name = name @@ -18,6 +19,7 @@ def __init__(self, name, address, module, section): self.section = section self.size = -1 + class Module: def __init__(self, name): self.name = name @@ -32,6 +34,7 @@ def process(self): for (n,s) in self.symbols.items(): self.size += s.size + class Section: def __init__(self, name, address, size, attributes): self.name =name @@ -58,9 +61,6 @@ def process(self): sectionEnd = sectionEnd - symbol.size - - - class MapFile: def __init__(self): self.sections = [] @@ -73,22 +73,22 @@ def addSection(self, section): def getModule(self, name): if name in self.modules: return self.modules[name] - + module = self.modules[name] = Module(name) return module def fromFile(self, filename): currentSection = None - sectionHeaderRegex = re.compile("(?P.*?)\s+(?P
.*?)\s+(?P.*?)\s=\s+(?P\d+)\.\sbytes\s\((\w+),(\w+),(?P\w+)\)") - symbolReg = re.compile("(?PC|D):\s+(?P
.*?)\s+(?P\w+)\s+(?P\w+|.*)") - + sectionHeaderRegex = re.compile(r"(?P.*?)\s+(?P
.*?)\s+(?P.*?)\s=\s+(?P\d+)\.\sbytes\s\((\w+),(\w+),(?P\w+)\)") + symbolReg = re.compile(r"(?PC|D):\s+(?P
.*?)\s+(?P\w+)\s+(?P\w+|.*)") + mapfile = open(filename, "r") skip = 0 while line := mapfile.readline(): # SECTION DATA if len(line) > 0 and line[0] == '\x0c': # skip header - for skip in range(4): + for skip in range(4): mapfile.readline() line = mapfile.readline() @@ -97,12 +97,12 @@ def fromFile(self, filename): continue secHead = SimpleNamespace(**match.groupdict()) - + # print("Section Header: {secHead.name}") existing = [x for x in self.sections if x.name == secHead.name] if len(existing) >= 1: - + # print(F"found existing section: {secHead.name}@{existing[0]}") found = existing[0] @@ -123,7 +123,7 @@ def fromFile(self, filename): # print(F"Found symbol: {symbol.name}") currentSection.symbols.append(symbol) - + def process(self): for section in self.sections: @@ -138,17 +138,17 @@ def process(self): def symbolsCsv(self) -> str: out = "" out += "Bytes, Symbol, Module, Address, Section, SectionType\n" - - + + for symbol in self.symbols: out += F'{symbol.size}, {symbol.name}, {symbol.module.name}, {hex(symbol.address)}, {symbol.section.name}, {symbol.section.attributes}\n' return out - + def sectionsCsv(self) -> str: out = "" out += "Bytes, Section, Address, SectionType\n" - + for s in self.sections: out += F'{s.size}, {s.name}, {hex(s.address)}, {s.attributes}\n' @@ -157,20 +157,22 @@ def sectionsCsv(self) -> str: def modulesCsv(self) -> str: out = "" out += "Bytes, Module, Symbols\n" - + for m in sorted(self.modules.values(), key=lambda x: x.size, reverse=True): out += F'{m.size}, {m.name}, {len(m.symbols.keys())}\n' return out + def writeFile(filename, name, ext, data): file = open(f"{os.path.abspath(filename)}.{name}.{ext}", "w") file.write(data) file.close() + def main(infile): - - if not os.path.isfile(infile): + + if not os.path.isfile(infile): print("Unreadable file '%s'." % infile) return 1 @@ -184,6 +186,7 @@ def main(infile): return 0 + if __name__=="__main__": if len(sys.argv)>=2: sys.exit(main(sys.argv[1]))