Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5293: Using pascal strings in OMF

Ryan Kurtz0f3f6095d3e9
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfString.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfString.java
index 236b09eccf..04eb443054 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfString.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfString.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,7 +18,8 @@ package ghidra.app.util.bin.format.omf;
import java.io.IOException;
import ghidra.app.util.bin.StructConverter;
-import ghidra.program.model.data.*;
+import ghidra.program.model.data.DataType;
+import ghidra.program.model.data.PascalString255DataType;
import ghidra.util.exception.DuplicateNameException;
/**
@@ -54,6 +55,13 @@ public class OmfString implements StructConverter {
return str;
}
+ /**
+ * {@return the length (in bytes) of this data type}
+ */
+ public int getDataTypeSize() {
+ return BYTE.getLength() + length;
+ }
+
@Override
public String toString() {
return str;
@@ -65,10 +73,6 @@ public class OmfString implements StructConverter {
return BYTE;
}
- StructureDataType struct = new StructureDataType("OmfString", 0);
- struct.add(BYTE, "length", "");
- struct.add(new StringDataType(), length, "str", null);
- struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
- return struct;
+ return new PascalString255DataType();
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfComdefRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfComdefRecord.java
index b117b6d78f..6b94549a0f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfComdefRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfComdefRecord.java
@@ -64,7 +64,7 @@ public class OmfComdefRecord extends OmfExternalSymbol {
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
for (Reference ref : refs) {
- struct.add(ref.name.toDataType(), "name", null);
+ struct.add(ref.name.toDataType(), ref.name.getDataTypeSize(), "name", null);
struct.add(ref.typeIndex.toDataType(), "type_index", null);
struct.add(BYTE, "data_type", null);
struct.add(ref.communalLength1.toDataType(), "communal_length", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfExternalSymbol.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfExternalSymbol.java
index 76c6ec711b..3a2ee814f3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfExternalSymbol.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfExternalSymbol.java
@@ -61,7 +61,7 @@ public class OmfExternalSymbol extends OmfRecord {
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
for (Reference ref : refs) {
- struct.add(ref.name.toDataType(), "external_name", null);
+ struct.add(ref.name.toDataType(), ref.name.getDataTypeSize(), "external_name", null);
struct.add(ref.typeIndex.toDataType(), "type_index", null);
}
struct.add(BYTE, "checksum", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfFileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfFileHeader.java
index aca6600ad0..53a1c4bae5 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfFileHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfFileHeader.java
@@ -519,7 +519,7 @@ public class OmfFileHeader extends OmfRecord {
StructureDataType struct = new StructureDataType(OmfRecordTypes.getName(recordType), 0);
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
- struct.add(objectName.toDataType(), "name", null);
+ struct.add(objectName.toDataType(), objectName.getDataTypeSize(), "name", null);
struct.add(BYTE, "checksum", null);
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfNamesRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfNamesRecord.java
index febd0c91f6..a01149691f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfNamesRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfNamesRecord.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -48,7 +48,7 @@ public class OmfNamesRecord extends OmfRecord {
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
for (OmfString name : names) {
- struct.add(name.toDataType(), -1, "name", null);
+ struct.add(name.toDataType(), name.getDataTypeSize(), "name", null);
}
struct.add(BYTE, "checksum", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfSymbolRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfSymbolRecord.java
index 671e605459..b23c06d647 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfSymbolRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf/OmfSymbolRecord.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -100,7 +100,7 @@ public class OmfSymbolRecord extends OmfRecord {
struct.add(WORD, "base_frame", null);
}
for (Reference ref : refs) {
- struct.add(ref.name.toDataType(), "name", null);
+ struct.add(ref.name.toDataType(), ref.name.getDataTypeSize(), "name", null);
struct.add(ref.offset.toDataType(), "offset", null);
struct.add(ref.type.toDataType(), "type", null);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf166/Omf166DepList.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf166/Omf166DepList.java
index 7c42c60fad..31293f0aae 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf166/Omf166DepList.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf166/Omf166DepList.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -72,7 +72,7 @@ public class Omf166DepList extends OmfRecord {
if (info.time != null) {
struct.add(DWORD, "time32", null);
}
- struct.add(info.name.toDataType(), "name", null);
+ struct.add(info.name.toDataType(), info.name.getDataTypeSize(), "name", null);
}
struct.add(BYTE, "checksum", null);
struct.setCategoryPath(new CategoryPath(OmfUtils.CATEGORY_PATH));
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleEnd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleEnd.java
index 34c0f73fad..dea327ec36 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleEnd.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleEnd.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -58,7 +58,7 @@ public class Omf51ModuleEnd extends OmfRecord {
StructureDataType struct = new StructureDataType(Omf51RecordTypes.getName(recordType), 0);
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
- struct.add(moduleName.toDataType(), "name", null);
+ struct.add(moduleName.toDataType(), moduleName.getDataTypeSize(), "name", null);
struct.add(WORD, "padding", null);
struct.add(BYTE, "REG MSK", null);
struct.add(BYTE, "padding", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleHeader.java
index 35781bd0c6..dc884d7424 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ModuleHeader.java
@@ -56,7 +56,7 @@ public class Omf51ModuleHeader extends OmfRecord {
StructureDataType struct = new StructureDataType(Omf51RecordTypes.getName(recordType), 0);
struct.add(BYTE, "type", null);
struct.add(WORD, "length", null);
- struct.add(moduleName.toDataType(), "name", null);
+ struct.add(moduleName.toDataType(), moduleName.getDataTypeSize(), "name", null);
struct.add(BYTE, "TRN ID", null);
struct.add(BYTE, "padding", null);
struct.add(BYTE, "checksum", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Omf51Loader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Omf51Loader.java
index a2e47a04be..75c235862a 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Omf51Loader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Omf51Loader.java
@@ -97,13 +97,20 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
Address start = headerBlock.getStart();
for (OmfRecord record : records) {
- Data d = DataUtilities.createData(program, start.add(record.getRecordOffset()),
- record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
- StructConverter.setEndian(d, false);
+ try {
+ Data d = DataUtilities.createData(program, start.add(record.getRecordOffset()),
+ record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
+ StructConverter.setEndian(d, false);
+ }
+ catch (Exception e) {
+ log.appendMsg("Failed to markup record type 0x%x at offset 0x%x. %s."
+ .formatted(record.getRecordType(), record.getRecordOffset(),
+ e.getMessage()));
+ }
}
}
catch (Exception e) {
- log.appendMsg("Failed to markup records");
+ log.appendMsg("Failed to markup records: " + e.getMessage());
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/OmfLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/OmfLoader.java
index e1d1f6049f..d8a4c254a6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/OmfLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/OmfLoader.java
@@ -160,10 +160,19 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
for (OmfRecord record : fileHeader.getRecords()) {
DataUtilities.createData(program, start.add(record.getRecordOffset()),
record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
+ try {
+ DataUtilities.createData(program, start.add(record.getRecordOffset()),
+ record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);
+ }
+ catch (Exception e) {
+ log.appendMsg("Failed to markup record type 0x%x at offset 0x%x. %s."
+ .formatted(record.getRecordType(), record.getRecordOffset(),
+ e.getMessage()));
+ }
}
}
catch (Exception e) {
- log.appendMsg("Failed to markup records");
+ log.appendMsg("Failed to markup records: " + e.getMessage());
}
}