Ghidra is a software reverse engineering (SRE) framework
GP-955 Add comments, fix arch check
jmlagor784f830b5409
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.javaindex 92ea1f0e69..37998726ce 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/FileHeader.java@@ -141,6 +141,7 @@ public class FileHeader implements StructConverter {/*** Values for the Machine field indicating the intended processor architecture*/+ public final static int IMAGE_FILE_MACHINE_MASK = 0xFFFF;public final static int IMAGE_FILE_MACHINE_UNKNOWN = 0x0; // The content of this field is assumed to be applicable to any machine typepublic final static int IMAGE_FILE_MACHINE_AM33 = 0x1d3; // Matsushita AM33public final static int IMAGE_FILE_MACHINE_AMD64 = 0x8664; // x64@@ -177,6 +178,8 @@ public class FileHeader implements StructConverter {private SectionHeader[] sectionHeaders;private List<DebugCOFFSymbol> symbols = new ArrayList<>();++ // TODO: This is x86-64 architecture-specific and needs to be generalized.private List<_IMAGE_RUNTIME_FUNCTION_ENTRY> irfes = new ArrayList<>();private FactoryBundledWithBinaryReader reader;@@ -252,6 +255,7 @@ public class FileHeader implements StructConverter {/*** Returns the array of RUNTIME_INFO entries, if any are present.* @return An array of _IMAGE_RUNTIME_FUNCTION_ENTRY. The array can be empty.+ * TODO: This is x86-64 architecture-specific and needs to be generalized.*/public List<_IMAGE_RUNTIME_FUNCTION_ENTRY> getImageRuntimeFunctionEntries() {return irfes;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ImageRuntimeFunctionEntries.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ImageRuntimeFunctionEntries.javaindex 56d65b881b..94c27f5749 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ImageRuntimeFunctionEntries.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ImageRuntimeFunctionEntries.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,8 +21,7 @@ import java.util.List;import ghidra.app.util.bin.format.FactoryBundledWithBinaryReader;import ghidra.program.model.address.Address;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.DataUtilities;+import ghidra.program.model.data.*;import ghidra.program.model.listing.Program;import ghidra.program.model.util.CodeUnitInsertionException;import ghidra.util.exception.DuplicateNameException;@@ -142,6 +141,30 @@ public class ImageRuntimeFunctionEntries {return functionEntries;}+ public static void createData(Program program, Address headerStart,+ List<_IMAGE_RUNTIME_FUNCTION_ENTRY> irfes) {+ // TODO: This is x86-64 architecture-specific and needs to be generalized.+ StructureDataType dt = new StructureDataType(".PDATA", 0);+ dt.setCategoryPath(new CategoryPath("/PE"));++ // Lay an array of RUNTIME_INFO structure out over the data+ StructureDataType irfeStruct = new StructureDataType("_IMAGE_RUNTIME_FUNCTION_ENTRY", 0);+ irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "BeginAddress", null);+ irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "EndAddress", null);+ irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "UnwindInfoAddressOrData", null);++ ArrayDataType irfeArray =+ new ArrayDataType(irfeStruct, irfes.size(), irfeStruct.getLength());++ try {+ DataUtilities.createData(program, headerStart, irfeArray, irfeArray.getLength(), true,+ DataUtilities.ClearDataMode.CHECK_FOR_SPACE);+ }+ catch (CodeUnitInsertionException e) {+ return;+ }+ }+// FIXME: change name to conform to Java naming standards// FIXME: If public visibility is required improved member protection is neededpublic static class _IMAGE_RUNTIME_FUNCTION_ENTRY {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/NTHeader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/NTHeader.javaindex 46b671a835..02d130e01c 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/NTHeader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/NTHeader.java@@ -270,7 +270,8 @@ public class NTHeader implements StructConverter, OffsetValidator {fileHeader.processSections(optionalHeader);fileHeader.processSymbols();- if (fileHeader.getMachine() == FileHeader.IMAGE_FILE_MACHINE_AMD64) {+ if ((fileHeader.getMachine() &+ FileHeader.IMAGE_FILE_MACHINE_MASK) == FileHeader.IMAGE_FILE_MACHINE_AMD64) {fileHeader.processImageRuntimeFunctionEntries();}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.javaindex 6ea103389a..df25c84b4d 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/PeLoader.java@@ -280,25 +280,8 @@ public class PeLoader extends AbstractPeDebugLoader {return;}- StructureDataType dt = new StructureDataType(".PDATA", 0);- dt.setCategoryPath(new CategoryPath("/PE"));-- // Lay an array of RUNTIME_INFO structure out over the data- StructureDataType irfeStruct = new StructureDataType("_IMAGE_RUNTIME_FUNCTION_ENTRY", 0);- irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "BeginAddress", null);- irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "EndAddress", null);- irfeStruct.add(ghidra.app.util.bin.StructConverter.IBO32, "UnwindInfoAddressOrData", null);-- ArrayDataType irfeArray =- new ArrayDataType(irfeStruct, irfes.size(), irfeStruct.getLength());-- try {- DataUtilities.createData(program, start, irfeArray, irfeArray.getLength(), true,- DataUtilities.ClearDataMode.CHECK_FOR_SPACE);- }- catch (CodeUnitInsertionException e) {- return;- }+ // TODO: This is x86-64 architecture-specific and needs to be generalized.+ ImageRuntimeFunctionEntries.createData(program, start, irfes);// Each RUNTIME_INFO contains an address to an UNWIND_INFO structure// which also needs to be laid out. When they contain chaining data