From 0ec08233c246cb404f535007d6dde928f0c399e6 Mon Sep 17 00:00:00 2001 From: oh2erk <32195719+haarlaj@users.noreply.github.com> Date: Sun, 14 Apr 2024 21:51:05 +0300 Subject: [PATCH 1/4] all mzloader header shorts are now treated as uint --- .../ghidra/app/util/opinion/MzLoader.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java index a26d9fd0201..06831d73f38 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java @@ -67,7 +67,7 @@ public Collection findSupportedLoadSpecs(ByteProvider provider) throws OldDOSHeader header = mz.getHeader(); if (header.isDosSignature() && !header.hasNewExeHeader() && !header.hasPeHeader()) { List results = - QueryOpinionService.query(getName(), "" + header.e_magic(), null); + QueryOpinionService.query(getName(), "" + Short.toUnsignedInt(header.e_magic()), null); for (QueryResult result : results) { loadSpecs.add(new LoadSpec(this, 0, result)); } @@ -139,7 +139,7 @@ private void markupHeaders(Program program, FileBytes fileBytes, MzExecutable mz MessageLog log, TaskMonitor monitor) { monitor.setMessage("Marking up headers..."); OldDOSHeader header = mz.getHeader(); - int blockSize = paragraphsToBytes(header.e_cparhdr()); + int blockSize = paragraphsToBytes(Short.toUnsignedInt(header.e_cparhdr())); try { Address headerSpaceAddr = AddressSpace.OTHER_SPACE.getAddress(0); MemoryBlock headerBlock = MemoryBlockUtils.createInitializedBlock(program, true, @@ -156,7 +156,7 @@ private void markupHeaders(Program program, FileBytes fileBytes, MzExecutable mz if (!relocations.isEmpty()) { DataType relocationType = relocations.get(0).toDataType(); int len = relocationType.getLength(); - addr = addr.add(header.e_lfarlc()); + addr = addr.add(Short.toUnsignedInt(header.e_lfarlc())); for (int i = 0; i < relocations.size(); i++) { monitor.checkCancelled(); DataUtilities.createData(program, addr.add(i * len), relocationType, -1, @@ -183,11 +183,11 @@ private void processMemoryBlocks(Program program, FileBytes fileBytes, Set knownSegments = new TreeSet<>(); relocationFixups.forEach(rf -> knownSegments.add(space.getAddress(rf.segment, 0))); knownSegments.add(space.getAddress(INITIAL_SEGMENT_VAL, 0)); - if (header.e_cs() > 0) { - knownSegments.add(space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, 0)); + if (Short.toUnsignedInt(header.e_cs()) > 0) { + knownSegments.add(space.getAddress((INITIAL_SEGMENT_VAL + Short.toUnsignedInt(header.e_cs())) & 0xffff, 0)); } // Allocate an initialized memory block for each segment we know about - int endOffset = pagesToBytes(header.e_cp() - 1) + header.e_cblp(); + int endOffset = pagesToBytes(Short.toUnsignedInt(header.e_cp()) - 1) + Short.toUnsignedInt(header.e_cblp()); if (endOffset > reader.length()) { log.appendMsg( "File is 0x%x bytes but header reports 0x%x".formatted(reader.length(), endOffset)); @@ -253,7 +253,7 @@ private void processMemoryBlocks(Program program, FileBytes fileBytes, // Allocate an uninitialized memory block for extra minimum required data space if (lastBlock != null) { - int extraAllocSize = paragraphsToBytes(header.e_minalloc()); + int extraAllocSize = paragraphsToBytes(Short.toUnsignedInt(header.e_minalloc())); if (extraAllocSize > 0) { MemoryBlockUtils.createUninitializedBlock(program, false, "DATA", lastBlock.getEnd().add(1), extraAllocSize, "", "mz", true, true, false, log); @@ -340,7 +340,7 @@ private void processEntryPoint(Program program, SegmentedAddressSpace space, MzE int ipValue = Short.toUnsignedInt(header.e_ip()); Address addr = - space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, ipValue); + space.getAddress((INITIAL_SEGMENT_VAL + Short.toUnsignedInt(header.e_cs())) & 0xffff, ipValue); SymbolTable symbolTable = program.getSymbolTable(); try { @@ -397,7 +397,7 @@ private void processRegisters(Program program, MzExecutable mz, MessageLog log, BigInteger.valueOf(Short.toUnsignedLong(header.e_sp()))); context.setValue(ss, entry.getAddress(), entry.getAddress(), BigInteger.valueOf( - Integer.toUnsignedLong((header.e_ss() + INITIAL_SEGMENT_VAL) & 0xffff))); + Integer.toUnsignedLong((Short.toUnsignedInt(header.e_ss()) + INITIAL_SEGMENT_VAL) & 0xffff))); for (MemoryBlock block : program.getMemory().getBlocks()) { Address start = block.getStart(); @@ -473,7 +473,7 @@ private Set getRelocationFixups(SegmentedAddressSpace space, * @return The segmented addresses converted to a file offset */ private int addressToFileOffset(int segment, int offset, OldDOSHeader header) { - return (short) segment * 16 + offset + paragraphsToBytes(header.e_cparhdr()); + return (short) segment * 16 + offset + paragraphsToBytes(Short.toUnsignedInt(header.e_cparhdr())); } /** From 7aabcc649689ac5b6514e9e45e7032e41c91bea2 Mon Sep 17 00:00:00 2001 From: oh2erk <32195719+haarlaj@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:19:13 +0300 Subject: [PATCH 2/4] reverting bad code --- .../src/main/java/ghidra/app/util/opinion/MzLoader.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java index 06831d73f38..9ec3150da29 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java @@ -67,7 +67,7 @@ public Collection findSupportedLoadSpecs(ByteProvider provider) throws OldDOSHeader header = mz.getHeader(); if (header.isDosSignature() && !header.hasNewExeHeader() && !header.hasPeHeader()) { List results = - QueryOpinionService.query(getName(), "" + Short.toUnsignedInt(header.e_magic()), null); + QueryOpinionService.query(getName(), "" + header.e_magic(), null); for (QueryResult result : results) { loadSpecs.add(new LoadSpec(this, 0, result)); } @@ -183,7 +183,7 @@ private void processMemoryBlocks(Program program, FileBytes fileBytes, Set knownSegments = new TreeSet<>(); relocationFixups.forEach(rf -> knownSegments.add(space.getAddress(rf.segment, 0))); knownSegments.add(space.getAddress(INITIAL_SEGMENT_VAL, 0)); - if (Short.toUnsignedInt(header.e_cs()) > 0) { + if (header.e_cs() > 0) { knownSegments.add(space.getAddress((INITIAL_SEGMENT_VAL + Short.toUnsignedInt(header.e_cs())) & 0xffff, 0)); } // Allocate an initialized memory block for each segment we know about @@ -340,7 +340,7 @@ private void processEntryPoint(Program program, SegmentedAddressSpace space, MzE int ipValue = Short.toUnsignedInt(header.e_ip()); Address addr = - space.getAddress((INITIAL_SEGMENT_VAL + Short.toUnsignedInt(header.e_cs())) & 0xffff, ipValue); + space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, ipValue); SymbolTable symbolTable = program.getSymbolTable(); try { @@ -397,7 +397,7 @@ private void processRegisters(Program program, MzExecutable mz, MessageLog log, BigInteger.valueOf(Short.toUnsignedLong(header.e_sp()))); context.setValue(ss, entry.getAddress(), entry.getAddress(), BigInteger.valueOf( - Integer.toUnsignedLong((Short.toUnsignedInt(header.e_ss()) + INITIAL_SEGMENT_VAL) & 0xffff))); + Integer.toUnsignedLong((header.e_ss() + INITIAL_SEGMENT_VAL) & 0xffff))); for (MemoryBlock block : program.getMemory().getBlocks()) { Address start = block.getStart(); From b7ab3714f54594cb0b92e05275c9ab9455b25de0 Mon Sep 17 00:00:00 2001 From: oh2erk <32195719+haarlaj@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:27:44 +0300 Subject: [PATCH 3/4] reverting bad code v2 --- .../src/main/java/ghidra/app/util/opinion/MzLoader.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java index 9ec3150da29..0d07753d9b4 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java @@ -67,7 +67,7 @@ public Collection findSupportedLoadSpecs(ByteProvider provider) throws OldDOSHeader header = mz.getHeader(); if (header.isDosSignature() && !header.hasNewExeHeader() && !header.hasPeHeader()) { List results = - QueryOpinionService.query(getName(), "" + header.e_magic(), null); + QueryOpinionService.query(getName(), "" + header.e_magic(), null); for (QueryResult result : results) { loadSpecs.add(new LoadSpec(this, 0, result)); } @@ -340,7 +340,7 @@ private void processEntryPoint(Program program, SegmentedAddressSpace space, MzE int ipValue = Short.toUnsignedInt(header.e_ip()); Address addr = - space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, ipValue); + space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, ipValue); SymbolTable symbolTable = program.getSymbolTable(); try { @@ -397,7 +397,7 @@ private void processRegisters(Program program, MzExecutable mz, MessageLog log, BigInteger.valueOf(Short.toUnsignedLong(header.e_sp()))); context.setValue(ss, entry.getAddress(), entry.getAddress(), BigInteger.valueOf( - Integer.toUnsignedLong((header.e_ss() + INITIAL_SEGMENT_VAL) & 0xffff))); + Integer.toUnsignedLong((header.e_ss() + INITIAL_SEGMENT_VAL) & 0xffff))); for (MemoryBlock block : program.getMemory().getBlocks()) { Address start = block.getStart(); From 704f4d51cf61f89b3eacb3fef96ddaffcf13b545 Mon Sep 17 00:00:00 2001 From: oh2erk <32195719+haarlaj@users.noreply.github.com> Date: Mon, 29 Apr 2024 23:31:49 +0300 Subject: [PATCH 4/4] reverting bad code v3 --- .../Base/src/main/java/ghidra/app/util/opinion/MzLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java index 0d07753d9b4..e6131dc48d5 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/MzLoader.java @@ -184,7 +184,7 @@ private void processMemoryBlocks(Program program, FileBytes fileBytes, relocationFixups.forEach(rf -> knownSegments.add(space.getAddress(rf.segment, 0))); knownSegments.add(space.getAddress(INITIAL_SEGMENT_VAL, 0)); if (header.e_cs() > 0) { - knownSegments.add(space.getAddress((INITIAL_SEGMENT_VAL + Short.toUnsignedInt(header.e_cs())) & 0xffff, 0)); + knownSegments.add(space.getAddress((INITIAL_SEGMENT_VAL + header.e_cs()) & 0xffff, 0)); } // Allocate an initialized memory block for each segment we know about int endOffset = pagesToBytes(Short.toUnsignedInt(header.e_cp()) - 1) + Short.toUnsignedInt(header.e_cblp());