Ghidra is a software reverse engineering (SRE) framework
GP-0: Forcing OMF-51 headers to be little endian
Ryan Kurtz6a18427e4c68
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/StructConverter.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/StructConverter.javaindex cbc09903fa..050e14cb59 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/StructConverter.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/StructConverter.java@@ -17,7 +17,9 @@ package ghidra.app.util.bin;import java.io.IOException;+import ghidra.docking.settings.SettingsDefinition;import ghidra.program.model.data.*;+import ghidra.program.model.listing.Data;import ghidra.util.exception.DuplicateNameException;/**@@ -112,4 +114,26 @@ public interface StructConverter {* @see ghidra.program.model.data.StructureDataType*/public DataType toDataType() throws DuplicateNameException, IOException;++ /**+ * Recursively sets the given {@link Data} and its components to big/little endian+ *+ * @param data The {@link Data}+ * @param bigEndian True to set to big endian; false to set to little endian+ * @throws Exception if there was a problem setting the endianness+ */+ public static void setEndian(Data data, boolean bigEndian) throws Exception {+ for (int i = 0; i < data.getNumComponents(); i++) {+ Data component = data.getComponent(i);+ SettingsDefinition[] settings = component.getDataType().getSettingsDefinitions();+ for (int j = 0; j < settings.length; j++) {+ if (settings[j] instanceof EndianSettingsDefinition endianSetting) {+ endianSetting.setBigEndian(component, bigEndian);+ }+ }+ for (int j = 0; j < component.getNumComponents(); j++) {+ setEndian(component.getComponent(j), bigEndian);+ }+ }+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/CodeSignatureCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/CodeSignatureCommand.javaindex 7481c6c29c..c202598174 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/CodeSignatureCommand.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/CodeSignatureCommand.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,6 +18,7 @@ package ghidra.app.util.bin.format.macho.commands;import java.io.IOException;import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.macho.MachHeader;import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureBlobParser;import ghidra.app.util.bin.format.macho.commands.codesignature.CodeSignatureGenericBlob;@@ -64,7 +65,7 @@ public class CodeSignatureCommand extends LinkEditDataCommand {try {Data d = DataUtilities.createData(program, addr, blob.toDataType(), -1,DataUtilities.ClearDataMode.CHECK_FOR_SPACE);- LoadCommand.setEndian(d, true);+ StructConverter.setEndian(d, true);blob.markup(program, addr, header, monitor, log);}catch (Exception e) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/LoadCommand.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/LoadCommand.javaindex e4973474d9..46fd65296e 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/LoadCommand.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/LoadCommand.java@@ -22,11 +22,9 @@ import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.macho.MachHeader;import ghidra.app.util.importer.MessageLog;-import ghidra.docking.settings.SettingsDefinition;import ghidra.program.flatapi.FlatProgramAPI;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressSpace;-import ghidra.program.model.data.EndianSettingsDefinition;import ghidra.program.model.listing.*;import ghidra.util.exception.CancelledException;import ghidra.util.task.TaskMonitor;@@ -203,28 +201,6 @@ public abstract class LoadCommand implements StructConverter {return null;}- /**- * Recursively sets the given {@link Data} and its components to big/little endian- *- * @param data The {@link Data}- * @param bigEndian True to set to big endian; false to set to little endian- * @throws Exception if there was a problem setting the endianness- */- public static void setEndian(Data data, boolean bigEndian) throws Exception {- for (int i = 0; i < data.getNumComponents(); i++) {- Data component = data.getComponent(i);- SettingsDefinition[] settings = component.getDataType().getSettingsDefinitions();- for (int j = 0; j < settings.length; j++) {- if (settings[j] instanceof EndianSettingsDefinition endianSetting) {- endianSetting.setBigEndian(component, true);- }- }- for (int j = 0; j < component.getNumComponents(); j++) {- setEndian(component.getComponent(j), bigEndian);- }- }- }-//-------------------Legacy code to support Raw Binary markup----------------------------------/*** Marks-up this {@link LoadCommand} with data structures and comments. Assumes the programdiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/codesignature/CodeSignatureSuperBlob.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/codesignature/CodeSignatureSuperBlob.javaindex d6fbb5854b..2cd6311632 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/codesignature/CodeSignatureSuperBlob.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/macho/commands/codesignature/CodeSignatureSuperBlob.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.@@ -20,9 +20,9 @@ import java.util.ArrayList;import java.util.List;import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.macho.MachConstants;import ghidra.app.util.bin.format.macho.MachHeader;-import ghidra.app.util.bin.format.macho.commands.LoadCommand;import ghidra.app.util.importer.MessageLog;import ghidra.program.model.address.Address;import ghidra.program.model.data.*;@@ -97,7 +97,7 @@ public class CodeSignatureSuperBlob extends CodeSignatureGenericBlob {CodeSignatureGenericBlob blob = indexBlobs.get(i);Data d = DataUtilities.createData(program, addr.add(blobIndex.getOffset()),blob.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);- LoadCommand.setEndian(d, true);+ StructConverter.setEndian(d, true);blob.markup(program, addr.add(blobIndex.getOffset()), header, monitor, log);}}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.javaindex a3af77b00f..a2e47a04be 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@@ -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.@@ -21,6 +21,7 @@ import java.util.*;import ghidra.app.util.MemoryBlockUtils;import ghidra.app.util.Option;import ghidra.app.util.bin.ByteProvider;+import ghidra.app.util.bin.StructConverter;import ghidra.app.util.bin.format.omf.*;import ghidra.app.util.bin.format.omf.omf51.Omf51RecordFactory;import ghidra.app.util.importer.MessageLog;@@ -28,6 +29,7 @@ import ghidra.program.database.mem.FileBytes;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressSpace;import ghidra.program.model.data.DataUtilities;+import ghidra.program.model.listing.Data;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.MemoryBlock;import ghidra.util.exception.CancelledException;@@ -95,8 +97,9 @@ public class Omf51Loader extends AbstractProgramWrapperLoader {Address start = headerBlock.getStart();for (OmfRecord record : records) {- DataUtilities.createData(program, start.add(record.getRecordOffset()),+ Data d = DataUtilities.createData(program, start.add(record.getRecordOffset()),record.toDataType(), -1, DataUtilities.ClearDataMode.CHECK_FOR_SPACE);+ StructConverter.setEndian(d, false);}}catch (Exception e) {