Created
July 20, 2016 00:47
-
-
Save lpereira/96501811fd1f684da78f64970b2c61fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
def parse_nm_output(handle): | |
current_object = None | |
symbols = {} | |
symbol_to_object = {} | |
for line in handle.readlines(): | |
line = line.strip() | |
if not current_object: | |
if line.endswith('.o:'): | |
current_object = line[:-1] | |
symbols[current_object] = { | |
'defined': set(), | |
'undefined': set() | |
} | |
else: | |
if line == '': | |
current_object = None | |
elif line[0].isdigit(): | |
addr, type, symbol = line.split() | |
if type == "T" and not symbol.startswith("."): | |
if symbol in symbol_to_object: | |
continue | |
symbols[current_object]['defined'].add(symbol) | |
symbol_to_object[symbol] = current_object | |
elif line.startswith(("U ", "u ")): | |
type, symbol = line.split() | |
symbols[current_object]['undefined'].add(symbol) | |
return symbols, symbol_to_object | |
if __name__ == '__main__': | |
terse = '--terse' in sys.argv | |
objs, syms_to_obj = parse_nm_output(sys.stdin) | |
print("digraph nm {") | |
print(" rankdir = LR") | |
print(" labelalloc = 3") | |
print(" concentrate = false") | |
print(" node [") | |
print(" fontsize=\"14\"") | |
print(" fontname=\"helvetica\"") | |
print(" ]") | |
for obj, lists in objs.items(): | |
print("\t\"%s\" [" % obj) | |
print("\t\tshape = \"none\"") | |
print("\t\tlabel = <<table border=\"0\" cellspacing=\"0\" color=\"#616233\">") | |
print("\t\t\t<tr><td border=\"1\" bgcolor=\"#e8ea7a\"><font color=\"#000000\">%s</font></td></tr>" % obj) | |
lists['referenced'] = [undef for undef in lists['undefined'] if undef in syms_to_obj] | |
if not terse: | |
for symbol in lists['defined']: | |
print("\t\t\t<tr><td port=\"DEF_%s\" border=\"1\" align=\"left\" bgcolor=\"#828344\"><font color=\"#ffffff\">%s</font></td></tr>" % (symbol, symbol)) | |
for symbol in lists['referenced']: | |
print("\t\t\t<tr><td port=\"REF_%s\" border=\"1\" align=\"left\" bgcolor=\"#626324\"><font color=\"#ffffff\">%s</font></td></tr>" % (symbol, symbol)) | |
print("\t\t</table>>") | |
print("\t];") | |
connected = set() | |
for obj, lists in objs.items(): | |
for symbol in lists['referenced']: | |
if terse: | |
key = (obj, syms_to_obj[symbol]) | |
if not key in connected: | |
connected.add(key) | |
print("\t\"%s\" -> \"%s\"" % key) | |
else: | |
print("\t\"%s\":REF_%s:e -> \"%s\":DEF_%s:w;" % (obj, symbol, syms_to_obj[symbol], symbol)) | |
print("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment