Skip to content

Instantly share code, notes, and snippets.

@prozacchiwawa
Created May 5, 2024 07:36
Show Gist options
  • Save prozacchiwawa/6fa8fb1239e3ad71263ae36254e7ce6b to your computer and use it in GitHub Desktop.
Save prozacchiwawa/6fa8fb1239e3ad71263ae36254e7ce6b to your computer and use it in GitHub Desktop.
Simple way of marking up a ppc disassembly with addresses and info
#!/usr/bin/env python
import sys
import argparse
import json
branch_instrs = [
'b',
'bl',
'beq',
'beq-',
'beq+',
'bne',
'bne-',
'bne+',
'ble',
'ble-',
'ble+',
'bgt',
'bgt-',
'bgt+',
'bge',
'bge-',
'bge+'
]
ADJ = 0x400
def offset_addr(addr, target, source, adj, annotations):
final_addr = addr + target + adj - source
hex_of_final = f'{final_addr:08x}'
if hex_of_final in annotations:
subd = annotations[hex_of_final]
info = None if 'info' not in subd else subd['info']
return (hex_of_final, subd['name'], info)
else:
return (hex_of_final, hex_of_final, None)
def decorate_disassembly(lines, target, source, adj, annotations):
for l in lines:
line = l.replace('\n','')
if len(line) > 9 and line[8] == ':':
# Disassembly line
instr = line[23:31].strip()
(orig, addr, info) = offset_addr(
int(line[:8].strip(),16),
source,
target,
adj,
annotations
)
def replace_with_offset_addr(arg):
a = arg.strip()
if a.startswith('0x'):
(_, addr, _) = offset_addr(
int(a[2:],16),
source,
target,
adj,
annotations
)
return f'{addr}'
else:
return arg
if instr in branch_instrs:
args = list(map(replace_with_offset_addr, line[29:].split(',')))
output_line = f'{line[8:29]}{",".join(args)}'
else:
output_line = f'{line[8:]}'
if orig != addr:
print('')
print(f'{addr}:')
if info:
for i in info:
print(f';; {i}')
print(f'{orig}{output_line}')
else:
print(line)
def hex_int(use_hex):
if use_hex.startswith('0x'):
return int(use_hex[2:], 16)
else:
return int(use_hex, 16)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
prog='readdress',
description='rewrite objdump disassembly with annotations and offset'
)
parser.add_argument('filename')
parser.add_argument('-s', '--source', type=hex_int)
parser.add_argument('-t', '--target', type=hex_int)
parser.add_argument('-a', '--adj', default=ADJ, type=hex_int)
parser.add_argument('-i', '--info', type=lambda f: json.load(open(f)), default={})
args = parser.parse_args()
lines = open(args.filename).readlines()
decorate_disassembly(
lines,
args.source,
args.target,
args.adj,
args.info
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment