Skip to content

Instantly share code, notes, and snippets.

@RoadrunnerWMC
Created May 6, 2021 02:04
Show Gist options
  • Save RoadrunnerWMC/0cc08178c18923d7a2dd1b572cb53a7e to your computer and use it in GitHub Desktop.
Save RoadrunnerWMC/0cc08178c18923d7a2dd1b572cb53a7e to your computer and use it in GitHub Desktop.
# Exports a file with lines in the form "symbolName 0xADDRESS function_or_label" where "f" indicates a function and "l" a label
# @author RoadrunnerWMC
# @category Data
#
from ghidra.program.database.function import FunctionDB
f = askFile('Choose a file to write to', 'Go!')
COLUMN_2 = 32
COLUMN_3 = 48
symTable = currentProgram.getSymbolTable()
num_functions = 0
num_labels = 0
with file(f.absolutePath, 'w') as fd: # note, cannot use open(), since that is in GhidraScript
for sym in symTable.getSymbolIterator():
name = sym.getName()
addr = sym.getAddress().getOffset()
if isinstance(sym.getObject(), FunctionDB):
function_or_label = u'f'
num_functions += 1
else:
function_or_label = u'l'
num_labels += 1
line = name
line += u' ' * max(COLUMN_2 - len(line), 1)
line += u'0x{:08X}'.format(addr)
line += u' ' * max(COLUMN_3 - len(line), 1)
line += function_or_label
fd.write((line + u'\n').encode('utf-8'))
num_total = num_functions + num_labels
print(u'Exported {} symbols ({} functions, {} labels)'.format(num_total, num_functions, num_labels))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment