Skip to content

Commit

Permalink
Fixed SYM fake objects applying.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Kononovich committed Jan 27, 2020
1 parent 6d8e43b commit d8c76ac
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/main/java/psx/PsxLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> optio
}

int transId = program.startTransaction("Load and apply SYM file...");
SymFile symFile = SymFile.fromBinary(symPath);
SymFile symFile = SymFile.fromBinary(symPath, log);

symFile.createOverlays(program, log, monitor);
symFile.applySymbols(program, log, monitor);
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/psyq/sym/SymDef.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
import ghidra.program.model.data.UnsignedLongDataType;
import ghidra.program.model.data.UnsignedShortDataType;

public class SymDef extends SymObject {
public class SymDef extends SymName {
private final SymDefClass defClass;
private final SymDefType defType;
private final long size;
private String name;

private Integer[] dims;
private final boolean hasTag;
private String tag;

public SymDef(SymDefClass defClass, SymDefType defType, boolean hasTag, long size, String name, long offset, long overlayId) {
super(offset, overlayId);
super(name, offset, overlayId);

this.defClass = defClass;
this.defType = defType;
Expand Down Expand Up @@ -118,14 +117,6 @@ public long getSize() {
return size;
}

public void setName(String newName) {
this.name = newName;
}

public String getName() {
return name;
}

public void setDims(Integer[] dims) {
this.dims = dims.clone();
}
Expand Down
58 changes: 45 additions & 13 deletions src/main/java/psyq/sym/SymFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,22 @@ public class SymFile {
private List<SymObject> objects = new ArrayList<>();
private List<SymOverlay> overlays = new ArrayList<>();

public static SymFile fromBinary(String path) {
public static SymFile fromBinary(String path, MessageLog log) {
try {
FileInputStream fis = new FileInputStream(path);
byte[] fileData = fis.readAllBytes();
fis.close();

ByteArrayProvider provider = new ByteArrayProvider(fileData);
BinaryReader reader = new BinaryReader(provider, true);
return new SymFile(reader);
return new SymFile(reader, log);
} catch (IOException e) {
log.appendException(e);
return null;
}
}

private SymFile(BinaryReader reader) throws IOException {
private SymFile(BinaryReader reader, MessageLog log) throws IOException {
String sig = reader.readNextAsciiString(3);

if (!sig.equals("MND")) {
Expand All @@ -77,6 +78,7 @@ private SymFile(BinaryReader reader) throws IOException {
SymStructUnionEnum currStructUnion = null;
SymFunc currFunc = null;
Map<String, SymFunc> defFuncs = new HashMap<>();
Map<String, SymStructUnionEnum> fakeObjs = new HashMap<>();
long currOverlay = 0L;

while (reader.getPointerIndex() < reader.length()) {
Expand Down Expand Up @@ -211,11 +213,25 @@ private SymFile(BinaryReader reader) throws IOException {
SymFunc func = new SymFunc(def2, defName, offset, currOverlay);
defFuncs.put(defName, func);
} else if (currFunc == null) { // exclude function blocks
if (fakeObjs.containsKey(defTag)) {
def2 = fakeObjs.get(defTag).toSymDef();
fakeObjs.remove(defTag);
}

objects.add(new SymExtStat(def2, offset, currOverlay));
}
} break;
case TPDEF: {
objects.add(new SymTypedef(def2));
SymObject obj;

if (fakeObjs.containsKey(defTag)) {
obj = fakeObjs.get(defTag);
((SymName)obj).setName(defName);
} else {
obj = new SymTypedef(def2);
}

objects.add(obj);
} break;
// STRUCT, UNION, ENUM begin
case STRTAG:
Expand All @@ -240,6 +256,12 @@ private SymFile(BinaryReader reader) throws IOException {
throw new IOException("Non-defined struct|union|enum field definition");
}

if (fakeObjs.containsKey(defTag)) {
def2 = fakeObjs.get(defTag).toSymDef();
def2.setName(defName);
fakeObjs.remove(defTag);
}

currStructUnion.addField(def2);
} break;
// STRUCT, UNION, ENUM end
Expand All @@ -254,7 +276,12 @@ private SymFile(BinaryReader reader) throws IOException {
throw new IOException("Wrong EOS type");
}

objects.add(currStructUnion);
if (defTag.matches("\\.\\d+fake")) {
fakeObjs.put(defTag, currStructUnion);
} else {
objects.add(currStructUnion);
}

currStructUnion = null;
} break;
default: break;
Expand All @@ -275,6 +302,11 @@ private SymFile(BinaryReader reader) throws IOException {
}
}

if (fakeObjs.size() > 0) {
log.appendMsg(String.format("Not all fake objects were used: %d items", fakeObjs.size()));
objects.addAll(fakeObjs.values());
}

objects.addAll(defFuncs.values());
}

Expand Down Expand Up @@ -379,17 +411,10 @@ private static boolean applySymbol(Program program, SymObject obj, Address addr,

if (obj instanceof SymFunc) {
SymFunc sf = (SymFunc)obj;
PsxLoader.setFunction(program, addr, sf.getFuncName(), true, false, log);
PsxLoader.setFunction(program, addr, sf.getName(), true, false, log);
setFunctionArguments(program, addr, sf, log);
SetCommentCmd cmd = new SetCommentCmd(addr, CodeUnit.PLATE_COMMENT, String.format("File: %s", sf.getFileName()));
cmd.applyTo(program);
} else if (obj instanceof SymName) {
SymName sn = (SymName)obj;
try {
st.createLabel(addr, sn.getObjectName(), SourceType.ANALYSIS);
} catch (InvalidInputException e) {
log.appendException(e);
}
} else if (obj instanceof SymTypedef) {
SymTypedef tpdef = (SymTypedef)obj;

Expand Down Expand Up @@ -474,6 +499,13 @@ private static boolean applySymbol(Program program, SymObject obj, Address addr,
mgr.addDataType(edt, DataTypeConflictHandler.REPLACE_HANDLER);
} break;
}
} else if (obj instanceof SymName) {
SymName sn = (SymName)obj;
try {
st.createLabel(addr, sn.getName(), SourceType.ANALYSIS);
} catch (InvalidInputException e) {
log.appendException(e);
}
} else {
System.out.println(String.format("unkn type offset: 0x%08X", addr.getOffset()));
}
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/psyq/sym/SymFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import java.util.ArrayList;
import java.util.List;

public class SymFunc extends SymObject {
public class SymFunc extends SymName {
private String fileName = null;
private final String funcName;

private long endOffset = 0L;

Expand All @@ -14,10 +13,9 @@ public class SymFunc extends SymObject {


public SymFunc(SymDef retnType, String funcName, long offset, long overlayId) {
super(offset, overlayId);
super(funcName, offset, overlayId);

this.retnType = retnType;
this.funcName = funcName;
}

public void setEndOffset(long endOffset) {
Expand All @@ -35,10 +33,6 @@ public void setFileName(String fileName) {
public String getFileName() {
return fileName;
}

public String getFuncName() {
return funcName;
}

public void addArgument(SymDef arg) {
args.add(arg);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/psyq/sym/SymName.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ public SymName(String name, long offset, long overlayId) {
this.name = name;
}

public String getObjectName() {
public String getName() {
return name;
}

public void setName(String newName) {
name = newName;
}
}
30 changes: 16 additions & 14 deletions src/main/java/psyq/sym/SymStructUnionEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import java.util.ArrayList;
import java.util.List;

public class SymStructUnionEnum extends SymObject {
private final String name;
public class SymStructUnionEnum extends SymName {
private final long size;
private final boolean isFake;
private final SymDefTypePrim type;
Expand All @@ -14,7 +13,7 @@ public class SymStructUnionEnum extends SymObject {
private final static String FAKE_R = "\\.(\\d+)fake";

public SymStructUnionEnum(String name, long size, SymDefTypePrim type) {
super(0L, 0L);
super(name, 0L, 0L);

this.name = name;
this.isFake = name.matches(FAKE_R);
Expand All @@ -25,23 +24,26 @@ public SymStructUnionEnum(String name, long size, SymDefTypePrim type) {
public void addField(SymDef field) {
this.fields.add(field);
}

public SymDef toSymDef() {
SymDefClass cl;

switch (type) {
case STRUCT: cl = SymDefClass.STRTAG; break;
case UNION: cl = SymDefClass.UNTAG; break;
case ENUM: cl = SymDefClass.ENTAG; break;
default: return null;
}

return new SymDef(cl, new SymDefType(new SymDefTypePrim[] {type}), false, size, name, getOffset(), getOverlayId());
}

@Override
public String getName() {
return isFake ? getFakeStructUnionEnumName(name, type) : name;
}

private static String getFakeStructUnionEnumName(String name, SymDefTypePrim type) {
// Pattern pat = Pattern.compile(FAKE_R);
// Matcher mat = pat.matcher(name);
//
// if (mat.find()) {
// switch (type) {
// case UNION: return String.format("FakeUnion%s", mat.group(1));
// case ENUM: return String.format("FakeEnum%s", mat.group(1));
// default: return String.format("FakeStruct%s", mat.group(1));
// }
// }

return name;
}

Expand Down

0 comments on commit d8c76ac

Please sign in to comment.