Skip to content

Commit

Permalink
Merge pull request #35 from chaoticgd/inlinetype
Browse files Browse the repository at this point in the history
Give anonymous types defined as part of a global variable, function or local variable declaration better names
  • Loading branch information
chaoticgd authored Sep 3, 2023
2 parents 905464a + bd307c3 commit 2b64e20
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v2.1.7

- stabs: Fixed a number of severe issues relating to inheritance, such as base classes being embedded in sub classes at the wrong offset, and types being misnamed.
- stabs: Anonymous types defined as part of a global variable, function, or local variable declaration are now named appropriately.

## v2.1.6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ public boolean shouldBailOut(Program program, StdumpAST.ParsedJsonFile ast) {


public void importDataTypes(StdumpAST.ImporterState importer) {
importer.stage = StdumpAST.ImportStage.TYPES;

int type_count = importer.ast.deduplicatedTypes.size();

monitor.setMessage("STABS - Importing data types...");
Expand Down Expand Up @@ -285,11 +287,13 @@ public void importFunctions(StdumpAST.ImporterState importer, Program program) {
function.setComment(sourceFile.path);
if(type.returnType != null) {
try {
importer.stage = StdumpAST.ImportStage.RETURN_TYPE;
function.setReturnType(type.returnType.createType(importer), SourceType.ANALYSIS);
} catch (InvalidInputException e) {
log.appendException(e);
}
}
importer.stage = StdumpAST.ImportStage.PARAMETERS;
HashSet<String> parameterNames = fillInParameters(function, importer, def, type);
if(importer.outputLineNumbers) {
for(StdumpAST.LineNumberPair pair : def.lineNumbers) {
Expand All @@ -299,6 +303,7 @@ public void importFunctions(StdumpAST.ImporterState importer, Program program) {
if(importer.markInlinedCode) {
markInlinedCode(def, sourceFile);
}
importer.stage = StdumpAST.ImportStage.LOCAL_VARIABLES;
fillInLocalVariables(function, importer, def, parameterNames);
}
}
Expand Down Expand Up @@ -422,6 +427,8 @@ private void fillInLocalVariables(Function function, StdumpAST.ImporterState imp
}

public void importGlobalVariables(StdumpAST.ImporterState importer) {
importer.stage = StdumpAST.ImportStage.GLOBAL_VARIABLES;

monitor.setMessage("STABS - Importing global variables...");
monitor.setMaximum(importer.ast.files.size());
monitor.setProgress(0);
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/ghidra/emotionengine/symboltable/StdumpAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public static class ParsedJsonFile {
ArrayList<Node> deduplicatedTypes = new ArrayList<Node>();
}

public static enum ImportStage {
TYPES,
RETURN_TYPE,
PARAMETERS,
LOCAL_VARIABLES,
GLOBAL_VARIABLES
}

public static class ImporterState {
// Options.
boolean embedBaseClasses = true;
Expand All @@ -47,6 +55,7 @@ public static class ImporterState {
ParsedJsonFile ast;

// Internal state.
ImportStage stage; // Used to name types defined inline in global/local variable declarations.
ArrayList<DataType> types = new ArrayList<>(); // (data type, size in bytes)
ArrayList<HashMap<Integer, Integer>> stabsTypeNumberToDeduplicatedTypeIndex = new ArrayList<>();
HashMap<String, Integer> typeNameToDeduplicatedTypeIndex = new HashMap<>();
Expand Down Expand Up @@ -128,7 +137,15 @@ String generateName(ImporterState importer, boolean addPrefix) {
}
}
if(name == null || name.isEmpty()) {
return prefixString + "unnamed_" + Integer.toString(absoluteOffsetBytes) + importer.conflictResolutionPostfix;
String dummyName = "unnamed_";
switch(importer.stage) {
case TYPES: dummyName = "unnamed_"; break;
case RETURN_TYPE: dummyName = "anonymousreturntype_"; break;
case PARAMETERS: dummyName = "anonymousparametertype_"; break;
case LOCAL_VARIABLES: dummyName = "anonymouslocaltype_"; break;
case GLOBAL_VARIABLES: dummyName = "anonymousglobaltype_"; break;
}
return prefixString + dummyName + Integer.toString(absoluteOffsetBytes) + importer.conflictResolutionPostfix;
}
return prefixString + name + importer.conflictResolutionPostfix;
}
Expand Down

0 comments on commit 2b64e20

Please sign in to comment.