From b631a8b2f5087d8e869888fc3a345ab7bd1da4a8 Mon Sep 17 00:00:00 2001 From: Shane Huntley Date: Fri, 17 May 2019 12:59:35 -0700 Subject: [PATCH 1/2] Avoid applying local labels as function name. Take offsets to function addresses into account. --- ApplySig.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ApplySig.py b/ApplySig.py index 30927d4..3b2fe3c 100644 --- a/ApplySig.py +++ b/ApplySig.py @@ -511,8 +511,8 @@ def parse_public_function(f, version, offset): is_local = True if b & FlirtFunctionFlag.FUNCTION_UNRESOLVED_COLLISION: is_collision = True - if b & 0x01 or b & 0x04: - print('Investigate public name flag: 0x{:02X} @ 0x{:04X}'.format(b, offset)) +# if b & 0x01 or b & 0x04: +# print('Investigate public name flag: 0x{:02X} @ 0x{:04X}'.format(b, offset)) b = read_u8(f) name = list() @@ -696,9 +696,14 @@ def funk_rename(addr, funk): global rename_cnt name = funk.name if name != '?': - funk = getFunctionAt(parseAddress(hex(addr))) - funk.setName(name, SourceType.USER_DEFINED) - rename_cnt += 1 + if not funk.is_local: + ghidra_funk = getFunctionAt(parseAddress(hex(addr + funk.offset))) + if ghidra_funk: + ghidra_funk.setName(name, SourceType.USER_DEFINED) + rename_cnt += 1 + else: + # No current defined function at address + pass return def apply_sig(flirt): From 7069f498da794a693ee523cb7514090e7077a91f Mon Sep 17 00:00:00 2001 From: astrelsky Date: Tue, 21 May 2019 11:34:11 -0400 Subject: [PATCH 2/2] Demangled names and create Function/Symbol. Demangled names are applied only if they were mangled. This automatically results in the correct label type. If no function/symbol exists at the address, one is created. --- ApplySig.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ApplySig.py b/ApplySig.py index 3b2fe3c..c9d945f 100644 --- a/ApplySig.py +++ b/ApplySig.py @@ -9,6 +9,7 @@ from __future__ import print_function from ghidra.framework.model import DomainFile from ghidra.program.model.symbol import SourceType +from ghidra.app.util.demangler import DemanglerUtil, DemanglerOptions from ghidra.util import Msg from java.lang import IllegalArgumentException @@ -692,18 +693,31 @@ def get_function_end(funk): return max rename_cnt = 0 +options = DemanglerOptions() def funk_rename(addr, funk): global rename_cnt + global options name = funk.name if name != '?': - if not funk.is_local: - ghidra_funk = getFunctionAt(parseAddress(hex(addr + funk.offset))) + stripped = DemanglerUtil.stripSuperfluousSignatureSpaces(name) + demangled = DemanglerUtil.demangle(currentProgram, stripped) + address = parseAddress(hex(addr + funk.offset)) + if demangled: + demangled.applyTo(currentProgram, address, options, monitor) + elif not funk.is_local: + ghidra_funk = getFunctionAt(address) if ghidra_funk: ghidra_funk.setName(name, SourceType.USER_DEFINED) rename_cnt += 1 else: - # No current defined function at address - pass + createFunction(address, name) + else: + ghidra_symbol = getSymbolAt(address) + if ghidra_symbol: + ghidra_symbol.setName(name, SourceType.USER_DEFINED) + rename_cnt += 1 + else: + createLabel(address, name, True, SourceType.USER_DEFINED) return def apply_sig(flirt):