GP-2557 Change demangler to use standardized anonymous function

definition names
This commit is contained in:
ghidra1 2022-09-15 15:28:54 -04:00
parent 6a2cd80550
commit 0b1b8d5a36
5 changed files with 32 additions and 27 deletions

View file

@ -20,29 +20,27 @@ import ghidra.util.InvalidNameException;
public class DataTypeNamingUtil {
private static final String ANONYMOUS_FUNCTION_DEF_PREFIX = "_func";
private DataTypeNamingUtil() {
// no construct
}
/**
* Generate a simple mangled function definition name and apply it to the specified functionDefinition.
* Generated name will start with {@code _function_}.
* @param functionDefinition function definition whose name should be set
* @param namePrefix prefix to be applied to generated name. An underscore will separate this prefix from the
* remainder of the mangled name. If null specified a prefix of "_function" will be used.
* @return name applied to functionDefinition
* @throws IllegalArgumentException if generated name contains unsupported characters
*/
public static String setMangledAnonymousFunctionName(
FunctionDefinitionDataType functionDefinition, String namePrefix)
FunctionDefinitionDataType functionDefinition)
throws IllegalArgumentException {
DataType returnType = functionDefinition.getReturnType();
ParameterDefinition[] parameters = functionDefinition.getArguments();
if (namePrefix == null) {
namePrefix = "_function";
}
StringBuilder sb = new StringBuilder(namePrefix);
StringBuilder sb = new StringBuilder(ANONYMOUS_FUNCTION_DEF_PREFIX);
GenericCallingConvention convention = functionDefinition.getGenericCallingConvention();
if (convention != null && convention != GenericCallingConvention.unknown) {

View file

@ -15,14 +15,12 @@
*/
package ghidra.app.util.bin.format.dwarf4.next;
import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.DW_AT_count;
import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.DW_AT_upper_bound;
import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.DW_TAG_subrange_type;
import java.util.*;
import java.util.stream.Collectors;
import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFAttribute.*;
import static ghidra.app.util.bin.format.dwarf4.encoding.DWARFTag.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
@ -311,7 +309,7 @@ public class DWARFDataTypeImporter {
if (dni.isAnon() && mangleAnonFuncNames) {
String mangledName =
DataTypeNamingUtil.setMangledAnonymousFunctionName(funcDef, dni.getName());
DataTypeNamingUtil.setMangledAnonymousFunctionName(funcDef);
dni = dni.replaceName(mangledName, dni.getOriginalName());
}

View file

@ -20,6 +20,7 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import ghidra.app.util.DataTypeNamingUtil;
import ghidra.program.model.data.*;
import ghidra.program.model.symbol.Namespace;
@ -314,7 +315,8 @@ public abstract class AbstractDemangledFunctionDefinitionDataType extends Demang
@Override
public DataType getDataType(DataTypeManager dataTypeManager) {
FunctionDefinitionDataType fddt = new FunctionDefinitionDataType(getName());
FunctionDefinitionDataType fddt =
new FunctionDefinitionDataType(DEMANGLER_ANONYMOUS_FUNCTION_CATEGORY_PATH, getName());
if (returnType != null) {
fddt.setReturnType(returnType.getDataType(dataTypeManager));
@ -322,6 +324,8 @@ public abstract class AbstractDemangledFunctionDefinitionDataType extends Demang
setParameters(fddt, dataTypeManager);
DataTypeNamingUtil.setMangledAnonymousFunctionName(fddt);
DataType dt = DemangledDataType.findDataType(dataTypeManager, namespace, getName());
if (dt == null || !(dt instanceof FunctionDefinitionDataType)) {
dt = fddt;

View file

@ -32,6 +32,11 @@ import ghidra.program.model.symbol.Namespace;
*/
public class DemangledDataType extends DemangledType {
protected static final String DEMANGLER_ROOT_CATEGORY_PATH = "/Demangler";
protected static final CategoryPath DEMANGLER_ANONYMOUS_FUNCTION_CATEGORY_PATH =
new CategoryPath(DEMANGLER_ROOT_CATEGORY_PATH + "/!_anon_funcs_");
private static final Pattern ARRAY_SUBSCRIPT_PATTERN = Pattern.compile("\\[\\d*\\]");
public static final char SPACE = ' ';
@ -145,7 +150,7 @@ public class DemangledDataType extends DemangledType {
}
else if (isUnion()) {
if (baseType == null || !(baseType instanceof Union)) {
dt = new UnionDataType(getDemanglerCategoryPath(name, getNamespace()), name);
dt = new UnionDataType(getDemanglerCategoryPath(getNamespace()), name);
}
}
else if (isEnum()) {
@ -153,25 +158,25 @@ public class DemangledDataType extends DemangledType {
if (enumType == null || INT.equals(enumType) || UNSIGNED_INT.equals(enumType)) {
// Can't tell how big an enum is, just use the size of a pointer
dt = new EnumDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new EnumDataType(getDemanglerCategoryPath(getNamespace()), name,
dataTypeManager.getDataOrganization().getIntegerSize());
}
else if (CHAR.equals(enumType) || UNSIGNED_CHAR.equals(enumType)) {
dt = new EnumDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new EnumDataType(getDemanglerCategoryPath(getNamespace()), name,
dataTypeManager.getDataOrganization().getCharSize());
}
else if (SHORT.equals(enumType) || UNSIGNED_SHORT.equals(enumType)) {
dt = new EnumDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new EnumDataType(getDemanglerCategoryPath(getNamespace()), name,
dataTypeManager.getDataOrganization().getShortSize());
}
else if (LONG.equals(enumType) || UNSIGNED_LONG.equals(enumType)) {
dt = new EnumDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new EnumDataType(getDemanglerCategoryPath(getNamespace()), name,
dataTypeManager.getDataOrganization().getLongSize());
}
else {
dt = new EnumDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new EnumDataType(getDemanglerCategoryPath(getNamespace()), name,
dataTypeManager.getDataOrganization().getIntegerSize());
}
}
@ -187,7 +192,7 @@ public class DemangledDataType extends DemangledType {
// I don't know what this is
// If it isn't pointed to, or isn't a referent, then assume typedef.
if (!(isReference() || isPointer())) { // Unknown type
dt = new TypedefDataType(getDemanglerCategoryPath(name, getNamespace()), name,
dt = new TypedefDataType(getDemanglerCategoryPath(getNamespace()), name,
new DWordDataType());
}
else {
@ -394,7 +399,7 @@ public class DemangledDataType extends DemangledType {
return true;
}
private static String getNamespacePath(String dtName, Demangled namespace) {
private static String getNamespacePath(Demangled namespace) {
Demangled ns = namespace;
String namespacePath = "";
while (ns != null) {
@ -404,8 +409,8 @@ public class DemangledDataType extends DemangledType {
return namespacePath;
}
private static CategoryPath getDemanglerCategoryPath(String dtName, Demangled namespace) {
return new CategoryPath("/Demangler" + getNamespacePath(dtName, namespace));
protected static CategoryPath getDemanglerCategoryPath(Demangled namespace) {
return new CategoryPath(DEMANGLER_ROOT_CATEGORY_PATH + getNamespacePath(namespace));
}
static Structure createPlaceHolderStructure(String dtName, Demangled namespace) {
@ -414,7 +419,7 @@ public class DemangledDataType extends DemangledType {
}
StructureDataType structDT = new StructureDataType(dtName, 0);
structDT.setDescription("PlaceHolder Structure");
structDT.setCategoryPath(getDemanglerCategoryPath(dtName, namespace));
structDT.setCategoryPath(getDemanglerCategoryPath(namespace));
return structDT;
}

View file

@ -200,7 +200,7 @@ public abstract class AbstractFunctionTypeApplier extends MsTypeApplier {
argsListApplier.applyTo(this);
}
setCallingConvention(applicator, callingConvention, hasThisPointer);
DataTypeNamingUtil.setMangledAnonymousFunctionName(functionDefinition, "_func");
DataTypeNamingUtil.setMangledAnonymousFunctionName(functionDefinition);
setApplied();
// resolvedDataType = applicator.resolveHighUse(dataType);