Skip to content

Commit

Permalink
ghidra: small refactor and inline serializing of parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarak committed Oct 24, 2024
1 parent 25f0cd4 commit 3fef34f
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions scripts/ghidra/PatchestryDecompileFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import ghidra.program.model.pcode.FunctionPrototype;
import ghidra.program.model.pcode.HighFunction;
import ghidra.program.model.pcode.HighGlobal;
import ghidra.program.model.pcode.HighLocal;
import ghidra.program.model.pcode.HighParam;
import ghidra.program.model.pcode.HighVariable;
import ghidra.program.model.pcode.PcodeBlock;
Expand Down Expand Up @@ -203,12 +205,8 @@ private HighVariable lValueOf(Varnode node) throws Exception {
return var;
}

private String getDisplayName(DataType data_type) throws Exception {
return data_type.getDisplayName().replaceAll(" ", "");
}

private void serializePointerType(Pointer ptr) throws Exception {
name("name").value(getDisplayName(ptr));
name("name").value(ptr.getDisplayName());
name("kind").value("pointer");
name("size").value(ptr.getLength());
DataType elem_type = ptr.getDataType();
Expand All @@ -222,7 +220,7 @@ private void serializePointerType(Pointer ptr) throws Exception {
}

private void serializeTypedefType(TypeDef typedef) throws Exception {
name("name").value(getDisplayName(typedef));
name("name").value(typedef.getDisplayName());
name("kind").value("typedef");
name("size").value(typedef.getLength());

Expand All @@ -238,7 +236,7 @@ private void serializeTypedefType(TypeDef typedef) throws Exception {
}

private void serializeArrayType(Array arr) throws Exception {
name("name").value(getDisplayName(arr));
name("name").value(arr.getDisplayName());
name("kind").value("array");
name("size").value(arr.getLength());
name("num_elements").value(arr.getNumElements());
Expand All @@ -249,16 +247,15 @@ private void serializeArrayType(Array arr) throws Exception {
}

private void serializeBuiltinType(DataType data_type, String kind) throws Exception {
name("name").value(getDisplayName(data_type));
name("name").value(data_type.getDisplayName());
name("size").value(data_type.getLength());
name("kind").value(kind);
}

private void serializeCompositeType(Composite data_type, String kind) throws Exception {
name("name").value(getDisplayName(data_type));
name("name").value(data_type.getDisplayName());
name("kind").value(kind);
name("size").value(data_type.getLength());
name("num_fields").value(data_type.getNumComponents());
name("fields").beginArray();

for (int i = 0; i < data_type.getNumComponents(); i++) {
Expand All @@ -280,12 +277,15 @@ private void serializeFunctionDefinition(FunctionDefinition fd) throws Exception
name("name").value(fd.getDisplayName());
name("kind").value("function");
name("return_type").value(label(fd.getReturnType()));
name("has_varargs").value(fd.hasVarArgs());
name("is_variadic").value(fd.hasVarArgs());
ParameterDefinition[] arguments = fd.getArguments();
name("parameters").beginArray();
for (int i = 0; i < arguments.length; i++) {
beginObject();
name("name").value(arguments[i].getName());
String name = arguments[i].getName();
if (name != null && !name.isEmpty()) {
name("name").value(arguments[i].getName());
}
name("size").value(arguments[i].getLength());
name("type").value(label(arguments[i].getDataType()));
endObject();
Expand Down Expand Up @@ -347,7 +347,18 @@ private void serialize(FunctionPrototype proto) throws Exception {

name("parameters").beginArray();
for (int i = 0; i < proto.getNumParams(); i++) {
serialize(proto.getParam(i).getHighVariable());
HighVariable hv = proto.getParam(i).getHighVariable();
// Assert if hv is not an instance of HighLocal
assert hv instanceof HighLocal;
if (hv != null) {
beginObject();
String hv_name = hv.getName();
if (hv_name != null && !hv_name.isEmpty()) {
name("name").value(hv_name);
}
name("type").value(label(hv.getDataType()));
endObject();
}
}
endArray();
}
Expand Down

0 comments on commit 3fef34f

Please sign in to comment.