Skip to content

Ghidra is a software reverse engineering (SRE) framework

OMF format: Added handling of many record types (Closes #4856)

Gravelbonese61f50f9e88b
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfFileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfFileHeader.java
index fa2de8a9d4..53c4a22e9e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfFileHeader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfFileHeader.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.ByteProvider;
+import ghidra.app.util.importer.MessageLog;
import ghidra.util.task.TaskMonitor;
public class OmfFileHeader extends OmfRecord {
@@ -36,7 +37,7 @@ public class OmfFileHeader extends OmfRecord {
private ArrayList<OmfSymbolRecord> symbols = new ArrayList<OmfSymbolRecord>();
private ArrayList<OmfFixupRecord> fixup = new ArrayList<OmfFixupRecord>();
private ArrayList<OmfSegmentHeader> extraSeg = null; // Holds implied segments that don't have official header record
-// private OmfModuleEnd endModule = null;
+ // private OmfModuleEnd endModule = null;
public OmfFileHeader(BinaryReader reader) throws IOException {
readRecordHeader(reader);
@@ -44,7 +45,7 @@ public class OmfFileHeader extends OmfRecord {
readCheckSumByte(reader);
isLittleEndian = reader.isLittleEndian();
}
-
+
/**
* This is usually the original source filename
* @return the name
@@ -52,7 +53,7 @@ public class OmfFileHeader extends OmfRecord {
public String getName() {
return objectName;
}
-
+
/**
* The name of the object module (within a library)
* @return the name
@@ -60,14 +61,14 @@ public class OmfFileHeader extends OmfRecord {
public String getLibraryModuleName() {
return libModuleName;
}
-
+
/**
* @return the string identifying the architecture this object was compiled for
*/
public String getMachineName() {
return "i386"; // This is the only possibility
}
-
+
/**
* If the OMF file contains a "translator" record, this is usually a string
* indicating the compiler which produced the file.
@@ -76,14 +77,14 @@ public class OmfFileHeader extends OmfRecord {
public String getTranslator() {
return translator;
}
-
+
/**
* @return true if the file describes the load image for a little endian architecture
*/
public boolean isLittleEndian() {
return isLittleEndian;
}
-
+
/**
* @return the list of segments in this file
*/
@@ -97,7 +98,7 @@ public class OmfFileHeader extends OmfRecord {
public ArrayList<OmfSegmentHeader> getExtraSegments() {
return extraSeg;
}
-
+
/**
* @return the list of group records for this file
*/
@@ -111,21 +112,21 @@ public class OmfFileHeader extends OmfRecord {
public ArrayList<OmfExternalSymbol> getExternalSymbols() {
return externsymbols;
}
-
+
/**
* @return the list of symbols exported by this file
*/
public ArrayList<OmfSymbolRecord> getPublicSymbols() {
return symbols;
}
-
+
/**
* @return the list of relocation records for this file
*/
public ArrayList<OmfFixupRecord> getFixups() {
return fixup;
}
-
+
/**
* Sort the explicit data-blocks for each segment into address order.
*/
@@ -167,7 +168,7 @@ public class OmfFileHeader extends OmfRecord {
segment.addEnumeratedData(datablock);
}
}
-
+
/**
* Given an index, retrieve the specific segment it refers to. This
* incorporates the special Borland segments, where the index has
@@ -193,7 +194,7 @@ public class OmfFileHeader extends OmfRecord {
res = segments.get(index-1);
return res;
}
-
+
/**
* Resolve special names associated with each segment: segment, class, overlay names
* and group: group name
@@ -209,7 +210,7 @@ public class OmfFileHeader extends OmfRecord {
groups.get(i).resolveNames(nameList);
}
}
-
+
/**
* Given an index, either find an existing Borland segment, or create a new one.
* Borland compilers can introduce special segments with a separate indexing
@@ -233,7 +234,7 @@ public class OmfFileHeader extends OmfRecord {
}
return segment;
}
-
+
private void evaluateComdef(OmfComdefRecord comdef) {
OmfSymbol[] coms = comdef.getSymbols();
for (OmfSymbol sym : coms) {
@@ -242,7 +243,7 @@ public class OmfFileHeader extends OmfRecord {
int count = (extraSeg==null) ? 1 : extraSeg.size()+1;
createOrFindBorlandSegment(count,dt);
sym.setSegmentRef(count);
-
+
}
}
}
@@ -258,39 +259,30 @@ public class OmfFileHeader extends OmfRecord {
*/
public static OmfFileHeader scan(BinaryReader reader,TaskMonitor monitor,boolean initialCommentsOnly) throws IOException, OmfException {
OmfRecord record = OmfRecord.readRecord(reader);
- if ((record.getRecordType() & (byte)0xfc)!=OmfRecord.THEADR) {
+ if (!(record instanceof OmfFileHeader)) {
throw new OmfException("Object file does not start with proper header");
}
OmfFileHeader header = (OmfFileHeader)record;
- byte type = record.getRecordType();
- type &= 0xfe;
- while(type != MODEND) {
+ while(!(record instanceof OmfModuleEnd)) {
if (monitor.isCancelled()) {
break; // Return what we have
}
record = OmfRecord.readRecord(reader);
- type = record.getRecordType();
- type &= 0xfe; // Mask off the least significant bit
- if (initialCommentsOnly && (type != COMENT)) {
- break;
- }
- switch(type) {
- case COMENT:
- byte commentClass = ((OmfCommentRecord)record).getCommentClass();
+ if(record instanceof OmfCommentRecord coment) {
+ byte commentClass = coment.getCommentClass();
if (commentClass == OmfCommentRecord.COMMENT_CLASS_TRANSLATOR) {
- header.translator = ((OmfCommentRecord)record).getValue();
+ header.translator = coment.getValue();
}
else if (commentClass == OmfCommentRecord.COMMENT_CLASS_LIBMOD) {
- header.libModuleName = ((OmfCommentRecord)record).getValue();
+ header.libModuleName = coment.getValue();
}
+ } else if(initialCommentsOnly) {
break;
- default:
- break; // Skip most records
}
}
return header;
}
-
+
/**
* Parse the entire object file
* @param reader is the byte stream
@@ -299,85 +291,64 @@ public class OmfFileHeader extends OmfRecord {
* @throws IOException for problems reading data
* @throws OmfException for malformed records
*/
- public static OmfFileHeader parse(BinaryReader reader,TaskMonitor monitor) throws IOException, OmfException {
+ public static OmfFileHeader parse(BinaryReader reader,TaskMonitor monitor, MessageLog log) throws IOException, OmfException {
OmfRecord record = OmfRecord.readRecord(reader);
- if ((record.getRecordType() & (byte)0xfc)!=OmfRecord.THEADR) {
+ if (!(record instanceof OmfFileHeader)) {
throw new OmfException("Object file does not start with proper header");
}
OmfFileHeader header = (OmfFileHeader)record;
Object lastDataBlock = null;
-
- while((record.getRecordType() & (byte)0xfe) != OmfRecord.MODEND) {
+
+ while(!(record instanceof OmfModuleEnd)) {
if (monitor.isCancelled()) {
break; // Return what we have
}
record = OmfRecord.readRecord(reader);
- byte type = record.getRecordType();
- type &= 0xfe; // Mask off the least significant bit
- switch(type) {
- case COMENT:
- byte commentClass = ((OmfCommentRecord)record).getCommentClass();
+ if (record instanceof OmfCommentRecord comment) {
+ byte commentClass = comment.getCommentClass();
if (commentClass == OmfCommentRecord.COMMENT_CLASS_TRANSLATOR) {
- header.translator = ((OmfCommentRecord)record).getValue();
+ header.translator = comment.getValue();
}
else if (commentClass == OmfCommentRecord.COMMENT_CLASS_LIBMOD) {
- header.libModuleName = ((OmfCommentRecord)record).getValue();
+ header.libModuleName = comment.getValue();
}
- break;
- case MODEND:
-// header.endModule = (OmfModuleEnd)record;
- // We are not currently examining the end module record
- break;
- case COMDEF:
- case LCOMDEF:
- header.evaluateComdef((OmfComdefRecord)record);
- header.externsymbols.add((OmfExternalSymbol)record);
- break;
- case LEXTDEF:
- case EXTDEF:
+ } else if(record instanceof OmfModuleEnd) {
+ // We are not currently examining the end module record
+ // header.endModule = end;
+ } else if(record instanceof OmfComdefRecord comdef) {
+ header.evaluateComdef(comdef);
header.externsymbols.add((OmfExternalSymbol)record);
- break;
- case PUBDEF:
- case LPUBDEF:
- header.symbols.add((OmfSymbolRecord)record);
- break;
- case LINNUM:
- break; // Not saving this information currently
- case LNAMES:
- ((OmfNamesRecord)record).appendNames(header.nameList); // Keep names, otherwise don't save record
- break;
- case SEGDEF:
- header.segments.add((OmfSegmentHeader)record);
- break;
- case GRPDEF:
- header.groups.add((OmfGroupRecord)record);
- break;
- case FIXUPP:
- OmfFixupRecord fixuprec = (OmfFixupRecord)record;
+ } else if(record instanceof OmfExternalSymbol external) {
+ header.externsymbols.add(external);
+ } else if(record instanceof OmfSymbolRecord symbol) {
+ header.symbols.add(symbol);
+ } else if(record instanceof OmfNamesRecord names) {
+ names.appendNames(header.nameList); // Keep names, otherwise don't save record
+ } else if(record instanceof OmfSegmentHeader seghead) {
+ header.segments.add(seghead);
+ } else if(record instanceof OmfGroupRecord group) {
+ header.groups.add(group);
+ } else if(record instanceof OmfFixupRecord fixuprec) {
fixuprec.setDataBlock(lastDataBlock);
header.fixup.add(fixuprec);
- break;
- case LEDATA:
- OmfEnumeratedData enumheader = (OmfEnumeratedData)record;
+ } else if(record instanceof OmfEnumeratedData enumheader) {
header.addEnumeratedBlock(enumheader);
lastDataBlock = enumheader;
- break;
- case LIDATA:
- OmfIteratedData iterheader = (OmfIteratedData)record;
+ } else if(record instanceof OmfIteratedData iterheader) {
if (iterheader.getSegmentIndex() <= 0 || iterheader.getSegmentIndex() > header.segments.size()) {
throw new OmfException("Bad segment index on LIDATA");
}
OmfSegmentHeader segheader2 = header.segments.get(iterheader.getSegmentIndex()-1);
segheader2.addIteratedData(iterheader);
lastDataBlock = iterheader;
- break;
- default:
- // Should never reach here
+ } else if(record instanceof OmfUnsupportedRecord unsupported) {
+ if(unsupported.doLogMessage())
+ log.appendMsg(unsupported.getMessage());
}
}
return header;
}
-
+
/**
* Assign a load image address to each segment. Follow OMF rules for grouping and ordering
* the segments in memory.
@@ -401,7 +372,7 @@ public class OmfFileHeader extends OmfRecord {
}
}
}
-
+
// Fill in any remaining segments
for(int i=0;i<segments.size();++i) {
OmfSegmentHeader segment = segments.get(i);
@@ -438,7 +409,7 @@ public class OmfFileHeader extends OmfRecord {
}
}
}
-
+
/**
* Check that the file has the specific OMF magic number
* @param reader accesses the bytes of the file
@@ -457,7 +428,7 @@ public class OmfFileHeader extends OmfRecord {
}
return true;
}
-
+
/**
* Create a reader for a specific OMF file
* @param provider is the underlying ByteProvider
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfLineNumberRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfLineNumberRecord.java
deleted file mode 100644
index 61feaf3791..0000000000
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfLineNumberRecord.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * 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.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.app.util.bin.format.omf;
-
-import java.io.IOException;
-import java.util.ArrayList;
-
-import ghidra.app.util.bin.BinaryReader;
-
-public class OmfLineNumberRecord extends OmfRecord {
- private int baseGroup;
- private int baseSegment;
- private LineSubrecord[] linenumber;
-
- public OmfLineNumberRecord(BinaryReader reader) throws IOException {
- readRecordHeader(reader);
- boolean hasBigFields = hasBigFields();
- baseGroup = OmfRecord.readIndex(reader);
- baseSegment = OmfRecord.readIndex(reader);
- long max = reader.getPointerIndex() + getRecordLength() - 1;
- ArrayList<LineSubrecord> linelist = new ArrayList<LineSubrecord>();
- while(reader.getPointerIndex() < max) {
- LineSubrecord subrec = LineSubrecord.read(reader,hasBigFields);
- linelist.add(subrec);
- }
- readCheckSumByte(reader);
- linenumber = new LineSubrecord[linelist.size()];
- linelist.toArray(linenumber);
- }
-
- public static class LineSubrecord {
- private int lineNumber;
- private int lineNumberOffset;
-
- public static LineSubrecord read(BinaryReader reader,boolean hasBigFields) throws IOException {
- LineSubrecord subrec = new LineSubrecord();
- subrec.lineNumber = reader.readNextShort() & 0xffff;
- subrec.lineNumberOffset = OmfRecord.readInt2Or4(reader, hasBigFields);
- return subrec;
- }
- }
-}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfRecord.java
index 853c2ace93..c10dddec51 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfRecord.java
@@ -21,12 +21,25 @@ import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
public abstract class OmfRecord {
+ public final static byte RHEADR = (byte)0x6E; // Obselete
+ public final static byte REGINT = (byte)0x70; // Obselete
+ public final static byte REDATA = (byte)0x72; // Obselete
+ public final static byte RIDATA = (byte)0x74; // Obselete
+ public final static byte OVLDEF = (byte)0x76; // Obselete
+ public final static byte ENDREC = (byte)0x78; // Obselete
+ public final static byte BLKDEF = (byte)0x7A; // Obselete
+ public final static byte BLKEND = (byte)0x7C; // Obselete
+ public final static byte DEBSYM = (byte)0x7E; // Obselete
public final static byte THEADR = (byte)0x80;
public final static byte LHEADR = (byte)0x82;
+ public final static byte PEDATA = (byte)0x84; // Obselete
+ public final static byte PIDATA = (byte)0x86; // Obselete
public final static byte COMENT = (byte)0x88;
public final static byte MODEND = (byte)0x8A;
public final static byte EXTDEF = (byte)0x8C;
+ public final static byte TYPDEF = (byte)0x8E; // Obselete
public final static byte PUBDEF = (byte)0x90;
+ public final static byte LOCSYM = (byte)0x92; // Obselete
public final static byte LINNUM = (byte)0x94;
public final static byte LNAMES = (byte)0x96;
public final static byte SEGDEF = (byte)0x98;
@@ -34,10 +47,26 @@ public abstract class OmfRecord {
public final static byte FIXUPP = (byte)0x9C;
public final static byte LEDATA = (byte)0xA0;
public final static byte LIDATA = (byte)0xA2;
+ public final static byte LIBHED = (byte)0xA4; // Obselete
+ public final static byte LIBNAM = (byte)0xA6; // Obselete
+ public final static byte LIBLOC = (byte)0xA8; // Obselete
+ public final static byte LIBDIC = (byte)0xAA; // Obselete
public final static byte COMDEF = (byte)0xB0;
+ public final static byte BAKPAT = (byte)0xB2;
public final static byte LEXTDEF = (byte)0xB4;
public final static byte LPUBDEF = (byte)0xB6;
public final static byte LCOMDEF = (byte)0xB8;
+ public final static byte CEXTDEF = (byte)0xBC;
+ public final static byte COMDAT = (byte)0xC2;
+ public final static byte LINSYM = (byte)0xC4;
+ public final static byte ALIAS = (byte)0xC6;
+ public final static byte NBKPAT = (byte)0xC8;
+ public final static byte LLNAMES = (byte)0xCA;
+ public final static byte VERNUM = (byte)0xCC;
+ public final static byte VENDEXT = (byte)0xCE;
+ public final static byte START = (byte)0xF0;
+ public final static byte END = (byte)0xF1;
+
protected byte recordType;
protected int recordLength;
protected byte checkSum;
@@ -45,20 +74,20 @@ public abstract class OmfRecord {
public byte getRecordType() {
return recordType;
}
-
+
public int getRecordLength() {
return recordLength;
}
-
+
public void readRecordHeader(BinaryReader reader) throws IOException {
recordType = reader.readNextByte();
recordLength = reader.readNextShort() & 0xffff;
}
-
+
public void readCheckSumByte(BinaryReader reader) throws IOException {
checkSum = reader.readNextByte();
}
-
+
public byte calcCheckSum(BinaryReader reader) throws IOException {
byte res = reader.readNextByte();
res += reader.readNextByte();
@@ -67,28 +96,28 @@ public abstract class OmfRecord {
res += reader.readNextByte();
return res;
}
-
+
public boolean validCheckSum(BinaryReader reader) throws IOException {
if (checkSum == 0) return true; // Sum compilers just set this to zero
return (calcCheckSum(reader) == 0);
}
-
+
public boolean hasBigFields() {
return ((recordType & 1)!=0);
}
-
+
public static int readInt1Or2(BinaryReader reader,boolean isBig) throws IOException {
if (isBig)
return (reader.readNextShort() & 0xffff);
return (reader.readNextByte() & 0xff);
}
-
+
public static int readInt2Or4(BinaryReader reader,boolean isBig) throws IOException {
if (isBig)
return reader.readNextInt();
return (reader.readNextShort() & 0xffff);
}
-
+
public static int readIndex(BinaryReader reader) throws IOException {
int indexWord;
byte firstByte = reader.readNextByte();
@@ -98,11 +127,11 @@ public abstract class OmfRecord {
indexWord = firstByte;
return indexWord;
}
-
+
public static OmfRecord readRecord(BinaryReader reader) throws IOException, OmfException {
OmfRecord res = null;
byte type = reader.peekNextByte();
- type &= 0xfe; // Mask off the least significant bit
+ type &= 0xfe; // Mask off the least significant bit => 16/32 bit flag
switch(type) {
case THEADR:
case LHEADR:
@@ -120,9 +149,6 @@ public abstract class OmfRecord {
case PUBDEF:
res = new OmfSymbolRecord(reader,false);
break;
- case LINNUM:
- res = new OmfLineNumberRecord(reader);
- break;
case LNAMES:
res = new OmfNamesRecord(reader);
break;
@@ -153,12 +179,43 @@ public abstract class OmfRecord {
case LCOMDEF:
res = new OmfComdefRecord(reader,true);
break;
+ case RHEADR:
+ case REGINT:
+ case REDATA:
+ case RIDATA:
+ case OVLDEF:
+ case ENDREC:
+ case BLKDEF:
+ case BLKEND:
+ case DEBSYM:
+ case LINNUM:
+ case PEDATA:
+ case PIDATA:
+ case LIBHED:
+ case LIBNAM:
+ case LIBLOC:
+ case LIBDIC:
+ res = new OmfUnsupportedRecord(reader, false);
+ break;
+ case LOCSYM:
+ case TYPDEF:
+ case CEXTDEF:
+ case COMDAT:
+ case LINSYM:
+ case ALIAS:
+ case BAKPAT:
+ case NBKPAT:
+ case LLNAMES:
+ case VERNUM:
+ case VENDEXT:
+ res = new OmfUnsupportedRecord(reader, true);
+ break;
default:
throw new OmfException("Unrecognized record type");
}
return res;
}
-
+
/**
* Read the OMF string format, 1-byte length, followed by that many ascii characters
* @param reader
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfUnsupportedRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfUnsupportedRecord.java
new file mode 100644
index 0000000000..74c0c52d7a
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/OmfUnsupportedRecord.java
@@ -0,0 +1,52 @@
+/* ###
+ * IP: GHIDRA
+ * REVIEWED: NO
+ *
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.app.util.bin.format.omf;
+
+import java.io.IOException;
+import ghidra.app.util.bin.BinaryReader;
+
+public class OmfUnsupportedRecord extends OmfRecord {
+ private long offset;
+ private boolean logMessage;
+
+ /***
+ * Skip an unsupported OMF record.
+ *
+ * @param reader The byte stream with the unsupported record to skip
+ * @throws IOException
+ */
+ public OmfUnsupportedRecord(BinaryReader reader, boolean log) throws IOException {
+ readRecordHeader(reader);
+ offset = reader.getPointerIndex();
+ logMessage = log;
+
+ reader.setPointerIndex(reader.getPointerIndex() + getRecordLength());
+ }
+
+ public boolean doLogMessage() {
+ return logMessage;
+ }
+
+ /***
+ * Get a message suitable for logging about this record
+ * @return String Message text about record
+ */
+ public String getMessage() {
+ return "Unsupported OMF record of type " + Long.toHexString((getRecordType() & 0xff)) + " of length " + getRecordLength() + " at " + offset;
+ }
+
+}
\ No newline at end of file
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 4a1ced92b0..32341f1c86 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
@@ -115,7 +115,7 @@ public class OmfLoader extends AbstractProgramWrapperLoader {
OmfFileHeader header = null;
BinaryReader reader = OmfFileHeader.createReader(provider);
try {
- header = OmfFileHeader.parse(reader, monitor);
+ header = OmfFileHeader.parse(reader, monitor, log);
header.resolveNames();
header.sortSegmentDataBlocks();
OmfFileHeader.doLinking(IMAGE_BASE, header.getSegments(), header.getGroups());