Skip to content

Commit

Permalink
utils: make util to print nearest symbol to address
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoomTen committed Oct 5, 2021
1 parent 98930a0 commit fd3b39f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions utils/get_nearest_symbol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
import re
import sys
from lib.gbtool import *

def get_relative_symbol(sym_table, address):
relative = 0
while not (is_symbol_defined(sym_table, address)):
address -= 1
relative += 1
if relative == 0:
return get_symbol(sym_table, address)
return '%s + %d' % (get_symbol(sym_table, address), relative)

if __name__ == '__main__':
if len(sys.argv) < 2:
print(f'{sys.argv[0]} address')
print('\taddress can be in BB:AAAA form or as a hex number')
exit(0)

addr_str = lambda x: '%03x:%04x' % x

# extract symbols
with open('shi_kong_xing_shou.sym', 'r') as sym:
string = sym.read()
symbol = read_symbols(string)
rom_sym = symbol['rom']
ram_sym = symbol['ram']

# get address
s = sys.argv[1]
s_= re.match('([0-9a-fA-F]+):([0-9a-fA-F]{4})$', s)
if s_:
start = addr2offset(*str2addr(s))
else:
start = int(s, 16)

print(get_relative_symbol(rom_sym, start))
3 changes: 3 additions & 0 deletions utils/lib/gbtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def get_symbol(symbol_table, address):
else:
return 'unk_%03x_%04x' % offset2addr(address)

def is_symbol_defined(symbol_table, address):
return address in symbol_table

# syntactic sugar
get_number = lambda src, bytes: int.from_bytes(src.read(bytes), byteorder='little')

Expand Down

0 comments on commit fd3b39f

Please sign in to comment.