/ ghidra / SuperExportSymbolsScript.py
SuperExportSymbolsScript.py
 1  #Export all functions in the database to a Binana symbol file
 2  # @runtime Jython
 3  # @category Binana
 4  # @author Thunderbrew 
 5  # @keybinding Shift-E
 6  # @menupath 
 7  # @toolbar logo.png
 8  
 9  from ghidra.program.model.symbol import SymbolType
10  from java.awt import Toolkit
11  from java.awt.datatransfer import StringSelection
12  from ghidra.app.decompiler import DecompInterface
13  from ghidra.util.task import ConsoleTaskMonitor
14  from ghidra.program.model.symbol.SourceType import *
15  
16  functionManager = currentProgram.getFunctionManager()
17  
18  file_location = askFile("Choose a file to save your Binana symbols ot", "Go")
19  
20  listing = currentProgram.getListing()
21  
22  decomp_interface = DecompInterface()
23  decomp_interface.openProgram(currentProgram)
24  
25  def get_function_type(func):
26      """Uses the Decompiler to get the refined signature."""
27      if func is None:
28          return ""
29          
30      results = decomp_interface.decompileFunction(func, 30, ConsoleTaskMonitor())
31      high_func = results.getHighFunction()
32      
33      if high_func is None:
34          return "" 
35  
36      ret_type = high_func.getFunctionPrototype().getReturnType().getName().replace(" *", "*")
37  
38      call_conv = high_func.getFunctionPrototype().getModelName()
39  
40      params = []
41      num_params = high_func.getFunctionPrototype().getNumParams()
42      for i in range(num_params):
43          p = high_func.getFunctionPrototype().getParam(i)
44          params.append("{} {}".format(p.getDataType().getName().replace(" *", "*"), p.getName()))
45      
46      param_str = "(" + (", ".join(params)) + ")"
47      return ret_type + " " + call_conv + " func" + param_str
48  
49  def get_symbol_entry_for_function(func):
50      name = func.getName()
51      
52      entry_addr = func.getEntryPoint().toString().upper()[-8:]
53      body = func.getBody()
54      end_addr = (body.getMaxAddress().add(1)).toString().upper()[-8:]
55      
56      func_type = get_function_type(func)
57  
58      if func_type == "":
59          output = "{} {} f end={}".format(name, entry_addr, end_addr)
60      else:
61          output = "{} {} f end={} type=\"{}\"".format(
62              name, 
63              entry_addr, 
64              end_addr, 
65              func_type 
66          ) 
67      return output
68  
69  def export_function_symbols(file):
70    monitor.setMessage("Exporting function symbols...")
71  
72    for f in functionManager.getFunctionsNoStubs(1):
73      monitor.checkCanceled() # throws exception if canceled
74  
75      if f.isExternal() or f.isThunk():
76        continue
77  
78      func_line = get_symbol_entry_for_function(f)
79      monitor.setMessage(func_line)
80  
81      file.write(func_line + "\n")
82  
83    return
84    
85  with open(file_location.absolutePath, "w") as file:
86    export_function_symbols(file)
87    file.close()