Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0: OMF-51 PR tweaks

Ryan Kurtz0105f683295b
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDef.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDef.java
index 8730254eb4..02174cb6b1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDef.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDef.java
@@ -36,8 +36,8 @@ public class Omf51ExternalDef {
/**
* Creates a new {@link Omf51ExternalDef}
*
- * @param reader A {@link BinaryReader} positioned at the start of the segment definition
- * @param largeSegmentId True if the segment ID is 2 bytes; false if 1 byte
+ * @param reader A {@link BinaryReader} positioned at the start of the external definition
+ * @param largeSegmentId True if the external ID is 2 bytes; false if 1 byte
* @throws IOException if an IO-related error occurred
*/
public Omf51ExternalDef(BinaryReader reader, boolean largeSegmentId) throws IOException {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDefsRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDefsRecord.java
index 9d946c6d07..cf5d6d3ad3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDefsRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51ExternalDefsRecord.java
@@ -26,25 +26,25 @@ import ghidra.util.exception.DuplicateNameException;
public class Omf51ExternalDefsRecord extends OmfRecord {
- private boolean largeSegmentId;
+ private boolean largeExtId;
private List<Omf51ExternalDef> defs = new ArrayList<>();
/**
* Creates a new {@link Omf51ExternalDefsRecord} record
*
* @param reader A {@link BinaryReader} positioned at the start of the record
- * @param largeSegmentId True if the segment ID is 2 bytes; false if 1 byte
+ * @param largeExtId True if the external ID is 2 bytes; false if 1 byte
* @throws IOException if an IO-related error occurred
*/
- public Omf51ExternalDefsRecord(BinaryReader reader, boolean largeSegmentId) throws IOException {
+ public Omf51ExternalDefsRecord(BinaryReader reader, boolean largeExtId) throws IOException {
super(reader);
- this.largeSegmentId = largeSegmentId;
+ this.largeExtId = largeExtId;
}
@Override
public void parseData() throws IOException, OmfException {
while (dataReader.getPointerIndex() < dataEnd) {
- defs.add(new Omf51ExternalDef(dataReader, largeSegmentId));
+ defs.add(new Omf51ExternalDef(dataReader, largeExtId));
}
}
@@ -56,7 +56,7 @@ public class Omf51ExternalDefsRecord extends OmfRecord {
for (Omf51ExternalDef def : defs) {
struct.add(BYTE, "blockType", null);
- struct.add(largeSegmentId ? WORD : BYTE, "extId", null);
+ struct.add(largeExtId ? WORD : BYTE, "extId", null);
struct.add(BYTE, "info", null);
struct.add(BYTE, "unused", null);
struct.add(def.getName().toDataType(), def.getName().getDataTypeSize(), "name", null);
@@ -69,9 +69,9 @@ public class Omf51ExternalDefsRecord extends OmfRecord {
}
/**
- * {@return the list of segments}
+ * {@return the list of external definitions}
*/
- public List<Omf51ExternalDef> getDefs() {
+ public List<Omf51ExternalDef> getDefinitions() {
return defs;
}
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51Fixup.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51Fixup.java
index 6cd80bbbd6..16d5fc09c1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51Fixup.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51Fixup.java
@@ -43,17 +43,17 @@ public class Omf51Fixup {
private int offset;
/**
- * Creates a new {@link Omf51PublicDef}
+ * Creates a new {@link Omf51Fixup}
*
- * @param reader A {@link BinaryReader} positioned at the start of the segment definition
- * @param largeSegmentId True if the segment ID is 2 bytes; false if 1 byte
+ * @param reader A {@link BinaryReader} positioned at the start of the fixup
+ * @param largeBlockId True if the block ID is 2 bytes; false if 1 byte
* @throws IOException if an IO-related error occurred
*/
- public Omf51Fixup(BinaryReader reader, boolean largeSegmentId) throws IOException {
+ public Omf51Fixup(BinaryReader reader, boolean largeBlockId) throws IOException {
refLoc = reader.readNextUnsignedShort();
refType = reader.readNextByte();
blockType = reader.readNextByte();
- blockId = largeSegmentId ? reader.readNextUnsignedShort() : reader.readNextUnsignedByte();
+ blockId = largeBlockId ? reader.readNextUnsignedShort() : reader.readNextUnsignedByte();
offset = reader.readNextUnsignedShort();
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51FixupRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51FixupRecord.java
index defaad3a1a..79ebce1bb5 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51FixupRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51FixupRecord.java
@@ -25,24 +25,25 @@ import ghidra.program.model.data.*;
import ghidra.util.exception.DuplicateNameException;
public class Omf51FixupRecord extends OmfRecord {
- private boolean largeSegmentId;
+ private boolean largeBlockId;
private List<Omf51Fixup> fixups = new ArrayList<>();
/**
- * Creates a new {@link Omf51FixupRecord} record
+ * Creates a new {@link Omf51FixupRecord}
*
* @param reader A {@link BinaryReader} positioned at the start of the record
+ * @param largeBlockId True if the block ID is 2 bytes; false if 1 byte
* @throws IOException if an IO-related error occurred
*/
- public Omf51FixupRecord(BinaryReader reader, boolean largeSegmentId) throws IOException {
+ public Omf51FixupRecord(BinaryReader reader, boolean largeBlockId) throws IOException {
super(reader);
- this.largeSegmentId = largeSegmentId;
+ this.largeBlockId = largeBlockId;
}
@Override
public void parseData() throws IOException, OmfException {
while (dataReader.getPointerIndex() < dataEnd) {
- fixups.add(new Omf51Fixup(dataReader, largeSegmentId));
+ fixups.add(new Omf51Fixup(dataReader, largeBlockId));
}
}
@@ -57,7 +58,7 @@ public class Omf51FixupRecord extends OmfRecord {
fixupStruct.add(WORD, "refLoc", null);
fixupStruct.add(BYTE, "refType", null);
fixupStruct.add(BYTE, "blockType", null);
- fixupStruct.add(largeSegmentId ? WORD : BYTE, "blockId", null);
+ fixupStruct.add(largeBlockId ? WORD : BYTE, "blockId", null);
fixupStruct.add(WORD, "offset", null);
struct.add(new ArrayDataType(fixupStruct, fixups.size()), "fixups", null);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDef.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDef.java
index 6cb94af848..26669eff52 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDef.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDef.java
@@ -45,7 +45,7 @@ public class Omf51PublicDef {
/**
* Creates a new {@link Omf51PublicDef}
*
- * @param reader A {@link BinaryReader} positioned at the start of the segment definition
+ * @param reader A {@link BinaryReader} positioned at the start of the public definition
* @param largeSegmentId True if the segment ID is 2 bytes; false if 1 byte
* @throws IOException if an IO-related error occurred
*/
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDefsRecord.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDefsRecord.java
index f4f179c327..89f30ad559 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDefsRecord.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/omf/omf51/Omf51PublicDefsRecord.java
@@ -67,9 +67,9 @@ public class Omf51PublicDefsRecord extends OmfRecord {
}
/**
- * {@return the list of segments}
+ * {@return the list of public definitions}
*/
- public List<Omf51PublicDef> getDefs() {
+ public List<Omf51PublicDef> getDefinitions() {
return defs;
}
}
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 c5ca9a220f..e33732b2ff 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
@@ -29,11 +29,7 @@ import ghidra.app.util.importer.MessageLog;
import ghidra.program.database.mem.FileBytes;
import ghidra.program.model.address.*;
import ghidra.program.model.data.DataUtilities;
-import ghidra.program.model.listing.Data;
-import ghidra.program.model.listing.Function;
-import ghidra.program.model.listing.FunctionManager;
-import ghidra.program.model.listing.Library;
-import ghidra.program.model.listing.Program;
+import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.reloc.Relocation.Status;
@@ -114,7 +110,7 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
Map<Integer, List<Omf51Content>> contentMap =
OmfUtils.filterRecords(records, Omf51Content.class)
.collect(Collectors.groupingBy(Omf51Content::getSegId));
-
+
// Create some data structures that will aid in segment relocation:
// - A set of addresses currently in use, so we can find holes for new segments in the
// address space
@@ -171,62 +167,70 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
}
return segmentToAddr;
}
-
+
private Map<Integer, Address> processExternalDefs(Program program, List<OmfRecord> records,
MessageLog log, TaskMonitor monitor)
throws Exception {
-
+
Map<Integer, Address> map = new HashMap<>();
List<Omf51ExternalDef> defs = OmfUtils.filterRecords(records, Omf51ExternalDefsRecord.class)
- .map(Omf51ExternalDefsRecord::getDefs)
+ .map(Omf51ExternalDefsRecord::getDefinitions)
.flatMap(List::stream)
.filter(def -> !def.isVariable())
.sorted((a, b) -> Integer.compare(a.getExtId(), b.getExtId()))
.toList();
-
+
int externalSize = defs.size();
-
- if (externalSize == 0) return map;
-
+
+ if (externalSize == 0) {
+ return map;
+ }
+
Address codeEndAddr = Arrays.stream(program.getMemory().getBlocks())
- .filter(block -> block.getSourceName().equals("CODE"))
- .map(block -> block.getEnd())
- .sorted((a, b) -> b.compareTo(a))
- .findFirst()
- .get();
-
- int availableSize = (int)(program.getAddressFactory()
- .getAddressSpace("CODE").getMaxAddress().getOffset() - codeEndAddr.getOffset());
-
+ .filter(block -> block.getSourceName().equals("CODE"))
+ .map(block -> block.getEnd())
+ .sorted((a, b) -> b.compareTo(a))
+ .findFirst()
+ .get();
+
+ int availableSize = (int) (program.getAddressFactory()
+ .getAddressSpace("CODE")
+ .getMaxAddress()
+ .getOffset() -
+ codeEndAddr.getOffset());
+
if (availableSize < externalSize) {
throw new Exception("Not enough CODE space for externals");
}
-
+
// Create an artificial 'EXTERNAL' block in CODE space.
MemoryBlock block = MemoryBlockUtils.createUninitializedBlock(program, false, "EXTERNAL",
- codeEndAddr.add(1), externalSize, "", "CODE", false, false, true, log);
-
+ codeEndAddr.add(1), externalSize, "", "CODE", false, false, true, log);
+
if (block == null) {
throw new Exception("Couldn't create EXTERNAL block");
}
-
+
block.setArtificial(true);
- block.setComment("NOTE: This block is artificial and allows external fixups to work correctly");
-
+ block.setComment(
+ "NOTE: This block is artificial and allows external fixups to work correctly");
+
// Create thunks for each external procedure def.
Address addr = codeEndAddr;
for (Omf51ExternalDef def : defs) {
addr = addr.add(1);
-
- Function f = program.getFunctionManager().createFunction(def.getName().str(), addr,
- new AddressSet(addr), SourceType.IMPORTED);
+
+ Function f = program.getFunctionManager()
+ .createFunction(def.getName().str(), addr, new AddressSet(addr),
+ SourceType.IMPORTED);
ExternalLocation extLoc = program.getExternalManager()
- .addExtFunction(Library.UNKNOWN, def.getName().str(), null, SourceType.IMPORTED);
+ .addExtFunction(Library.UNKNOWN, def.getName().str(), null,
+ SourceType.IMPORTED);
f.setThunkedFunction(extLoc.getFunction());
-
+
map.put(def.getExtId(), addr);
}
-
+
return map;
}
@@ -241,38 +245,39 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
throw new Exception("Record prior to fixup is not content!");
}
Address segmentAddr = segmentToAddr.get(content.getSegId());
-
+
if (segmentAddr == null) {
throw new Exception("Failed to lookup segment ID 0x%x for content fixup!"
.formatted(content.getSegId()));
}
-
+
Address contentAddr = segmentAddr.add(content.getOffset());
-
+
for (Omf51Fixup fixup : fixupRec.getFixups()) {
Address refLocAddr = contentAddr.add(fixup.getRefLoc());
Address baseAddr = null;
Address addr = null;
Status status = Status.UNSUPPORTED;
-
+
switch (fixup.getBlockType()) {
case Omf51Fixup.ID_BLOCK_SEGMENT:
case Omf51Fixup.ID_BLOCK_RELOCATABLE:
baseAddr = segmentToAddr.get(fixup.getBlockId());
addr = fixup.getRefType() == Omf51Fixup.REF_TYPE_CONV
- ? baseAddr.getNewAddress(((baseAddr.getOffset() - 0x20) * 8) + fixup.getOffset())
+ ? baseAddr.getNewAddress(
+ ((baseAddr.getOffset() - 0x20) * 8) + fixup.getOffset())
: baseAddr.add(fixup.getOffset());
break;
case Omf51Fixup.ID_BLOCK_EXTERNAL:
addr = extIdToAddr.get(fixup.getBlockId());
break;
}
-
+
if (addr != null) {
applyFixup(program, refLocAddr, addr, fixup.getRefType());
status = Status.APPLIED;
}
-
+
program.getRelocationTable()
.add(refLocAddr, status, fixup.getRefType(),
new long[] {
@@ -284,11 +289,11 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
previous = record;
}
}
-
+
private void applyFixup(Program program, Address refLocAddr, Address addr, int refType)
throws MemoryAccessException, Exception {
- int normAddr = (int)addr.getOffset() & 0xFFFF;
-
+ int normAddr = (int) addr.getOffset() & 0xFFFF;
+
// Validation
switch (refType) {
case Omf51Fixup.REF_TYPE_BIT:
@@ -305,36 +310,33 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
}
break;
}
-
+
// Patching
switch (refType) {
case Omf51Fixup.REF_TYPE_LOW:
case Omf51Fixup.REF_TYPE_BYTE:
case Omf51Fixup.REF_TYPE_BIT:
case Omf51Fixup.REF_TYPE_CONV:
- program.getMemory()
- .setByte(refLocAddr, (byte)(normAddr & 0xFF));
+ program.getMemory().setByte(refLocAddr, (byte) (normAddr & 0xFF));
break;
case Omf51Fixup.REF_TYPE_HIGH:
- program.getMemory()
- .setByte(refLocAddr, (byte)((normAddr >> 7) & 0xFF));
+ program.getMemory().setByte(refLocAddr, (byte) ((normAddr >> 7) & 0xFF));
break;
case Omf51Fixup.REF_TYPE_WORD:
- program.getMemory()
- .setShort(refLocAddr, (short)(normAddr & 0xFFFF));
+ program.getMemory().setShort(refLocAddr, (short) (normAddr & 0xFFFF));
break;
default:
throw new Exception("Unhandled ref type");
}
}
-
+
private void markupPublicDefs(Program program, List<OmfRecord> records,
Map<Integer, Address> segmentToAddr, MessageLog log, TaskMonitor monitor)
throws Exception {
monitor.setMessage("Marking up public defs...");
for (OmfRecord record : records) {
if (record instanceof Omf51PublicDefsRecord publicDefRec) {
- for (Omf51PublicDef def : publicDefRec.getDefs()) {
+ for (Omf51PublicDef def : publicDefRec.getDefinitions()) {
if (def.getUsageType() == Omf51PublicDef.NUMBER) {
log.appendMsg("Skipping NUMBER public def");
continue;
@@ -344,21 +346,23 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
throw new Exception("Failed to get lookup segment ID 0x%x for public def!"
.formatted(def.getSegId()));
}
-
+
Address defAddress = segmentAddr.add(def.getOffset());
-
+
if (!def.isVariable()) {
FunctionManager functionMgr = program.getFunctionManager();
Function function = functionMgr.getFunctionAt(defAddress);
if (function == null) {
- function = functionMgr.createFunction(def.getName().str(), defAddress, new AddressSet(defAddress),
+ function = functionMgr.createFunction(def.getName().str(), defAddress,
+ new AddressSet(defAddress),
SourceType.IMPORTED);
}
}
-
+
program.getSymbolTable()
- .createLabel(defAddress, def.getName().str(), null, SourceType.IMPORTED);
-
+ .createLabel(defAddress, def.getName().str(), null,
+ SourceType.IMPORTED);
+
program.getSymbolTable()
.addExternalEntryPoint(defAddress);
}
@@ -436,7 +440,7 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {
segmentEnds.put(key(segment), end);
yield start;
}
-
+
default:
throw new Exception(
"Skipping segment '%s'. Relocation type 0x%x is not yet supported"