Skip to content

Commit

Permalink
Clean code: remove useless statements
Browse files Browse the repository at this point in the history
- Remove class Options, only used to check if we need to add the decompiled code inside a function

- Add some comments and use `in` instead of d.get to check if a key exist

- Change category from _NEW_ to pwn
  • Loading branch information
meowmeowxw committed Nov 16, 2020
1 parent efbee4c commit c71511d
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions src/ghidra2dwarf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ghidra2Dwarf
# @author sen, meowmeowxw
# @category _NEW_
# @category PWN
# @keybinding
# @menupath
# @toolbar
Expand Down Expand Up @@ -118,33 +118,19 @@ def wrapper(*args):
g[name] = getattr(l, name)


class Options:
def __init__(self, use_dec=False, only_dec_nam_fun=False, att_deb_inf=False, verbose=False):
self.use_decompiler = use_dec
self.only_decompile_named_funcs = only_dec_nam_fun
self.attach_debug_info = att_deb_inf
self.verbose = verbose
self.filepath = ""
self.filename = ""
self.dwarf_source_path = ""
self.export_options = 0


def add_debug_info():
dwarf_pro_set_default_string_form(dbg, DW_FORM_string)
cu = dwarf_new_die(dbg, DW_TAG_compile_unit, None, None, None, None)
if options.use_decompiler:
c_file_name = os.path.split(decompiled_c_path)[1]
dwarf_add_AT_name(cu, c_file_name)
dir_index = dwarf_add_directory_decl(dbg, ".")
file_index = dwarf_add_file_decl(dbg, c_file_name, dir_index, 0, 0)
dwarf_add_AT_comp_dir(cu, ".")

c_file_name = os.path.split(decompiled_c_path)[1]
dwarf_add_AT_name(cu, c_file_name)
dir_index = dwarf_add_directory_decl(dbg, ".")
file_index = dwarf_add_file_decl(dbg, c_file_name, dir_index, 0, 0)
dwarf_add_AT_comp_dir(cu, ".")

for f in get_functions():
add_function(cu, f, file_index)
pass
# results = ifc.decompileFunction(f, 0, ConsoleTaskMonitor())
# print (results.getDecompiledFunction().getC())

dwarf_add_die_to_debug_a(dbg, cu)
add_global_variables(cu)
add_structures(cu)
Expand Down Expand Up @@ -332,21 +318,17 @@ def add_function(cu, func, file_index):
dwarf_add_AT_targ_address(dbg, die, DW_AT_low_pc, f_start, 0)
dwarf_add_AT_targ_address(dbg, die, DW_AT_high_pc, f_end - 1, 0)

if options.use_decompiler:
func_line = len(decomp_lines) + 1
func_line = len(decomp_lines) + 1

res = get_decompiled_function(func)
d = res.decompiledFunction.c
decomp_lines.extend(d.split("\n"))
res = get_decompiled_function(func)
d = res.decompiledFunction.c
decomp_lines.extend(d.split("\n"))

dwarf_add_AT_unsigned_const(dbg, die, DW_AT_decl_file, file_index)
dwarf_add_AT_unsigned_const(dbg, die, DW_AT_decl_line, func_line)
dwarf_add_line_entry(dbg, file_index, f_start, func_line, 0, True, False)
add_decompiler_func_info(cu, die, func, file_index, func_line)

dwarf_add_AT_unsigned_const(dbg, die, DW_AT_decl_file, file_index)
dwarf_add_AT_unsigned_const(dbg, die, DW_AT_decl_line, func_line)
dwarf_add_line_entry(dbg, file_index, f_start, func_line, 0, True, False)
add_decompiler_func_info(cu, die, func, file_index, func_line)
else:
# TODO: NEVER?
# add_disassembler_func_info(cu, die, func)
pass
return die


Expand All @@ -356,7 +338,7 @@ def write_source():


def add_type(cu, t):
if record.get(t.name, 0) != 0:
if t.name in record:
return record[t.name]

if isinstance(t, Pointer):
Expand Down Expand Up @@ -406,7 +388,7 @@ def add_ptr_type(cu, t):
die = dwarf_new_die(dbg, DW_TAG_pointer_type, cu, None, None, None)
record[t.name] = die

# pointer doesn't have child
# Some pointer don't have childs
if t.dataType:
child_die = add_type(cu, t.dataType)
dwarf_add_AT_reference(dbg, die, DW_AT_type, child_die)
Expand All @@ -427,6 +409,7 @@ def add_enum_type(cu, t):
child_type_die = add_type(cu, int_type)
dwarf_add_AT_reference(dbg, die, DW_AT_type, child_type_die)

# In this way we iterate the values in order
for value in t.values:
name = t.getName(value)
child_die = dwarf_new_die(dbg, DW_TAG_enumerator, die, None, None, None)
Expand Down Expand Up @@ -517,7 +500,6 @@ def generate_dwarf_sections():
dbg,
)
dbg = Dwarf_P_Debug(dbg.value)
options = Options(use_dec=True)
add_debug_info()
write_source()
sections = generate_dwarf_sections()
Expand Down

0 comments on commit c71511d

Please sign in to comment.