Ghidra Python script to print codeunits with symbols

240 views Asked by At

I'm using Ghidra to disassemble and study a 68000 binary. I want to write a Python script to get a pretty print version of the disassembly (Save as menu won't be sufficient here).

I thought about simply iterating through codeunits, printing labels if any, then the codeunit. But I get things like :

move.w #-0x5d56,(0xffffa602).w
bsr.b 0x000002c2

while in Ghidra Listing window, it was :

move.w #0xA2AA ,(ptr_to_last_updatable_bg_area).w
bsr.b  set_reg_values_2e2

How can I, at least, recover symbols from addresses (ptr_to_last_updatable_bg_area and set_reg_values_2e2), and, at best, formatted values (unsigned 0xA2AA rather than signed -0x5d56) ?

1

There are 1 answers

0
Naandalist On

print disassembly of a 68000 binary with symbols:

import ghidra.program.model.listing as listing
import ghidra.program.model.symbol as symbol

def print_disassembly(program):
  for codeunit in program.getListing().getCodeUnits():
    label = codeunit.getSymbol()
    if label:
      print(label, end=" ")
    print(codeunit.toString())

def main():
  program = ghidra.program.model.listing.Program.getProgram("program.bin")
  print_disassembly(program)

if __name__ == "__main__":
  main()