Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6577: More of the PE LoadConfig data directory has been parsed and marked up

Ryan Kurtz7a29be8cf763
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ExceptionDataDirectory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ExceptionDataDirectory.java
index 86d7a693b0..e35f2169ed 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ExceptionDataDirectory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/ExceptionDataDirectory.java
@@ -15,8 +15,6 @@
*/
package ghidra.app.util.bin.format.pe;
-import static ghidra.app.util.bin.format.pe.FileHeader.*;
-
import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
@@ -53,22 +51,19 @@ public class ExceptionDataDirectory extends DataDirectory {
reader.setPointerIndex(ptr);
try {
- functionEntries =
- switch (ntHeader.getFileHeader().getMachine() & IMAGE_FILE_MACHINE_MASK) {
- case IMAGE_FILE_MACHINE_I386:
- case IMAGE_FILE_MACHINE_IA64:
- case IMAGE_FILE_MACHINE_AMD64:
- yield new ImageRuntimeFunctionEntries_X86(reader, size, ntHeader);
- case IMAGE_FILE_MACHINE_ARM:
- case IMAGE_FILE_MACHINE_ARM64:
- case IMAGE_FILE_MACHINE_ARMNT:
- yield new ImageRuntimeFunctionEntries_ARM(reader, size, ntHeader);
- default:
- Msg.error(this,
- String.format("Exception Data unsupported architecture: 0x%02x",
- ntHeader.getFileHeader().getMachine()));
- yield null;
- };
+ FileHeader fileHeader = ntHeader.getFileHeader();
+ boolean isChpe = isChpe(fileHeader);
+ if (fileHeader.isX86() && !isChpe) {
+ functionEntries = new ImageRuntimeFunctionEntries_X86(reader, size, ntHeader);
+ }
+ else if (fileHeader.isArm() || isChpe) {
+ functionEntries = new ImageRuntimeFunctionEntries_ARM(reader, size, ntHeader);
+ }
+ else {
+ Msg.error(this, String.format("Exception Data unsupported architecture: 0x%02x",
+ fileHeader.getMachine()));
+ functionEntries = null;
+ }
return true;
}
catch (IOException e) {
@@ -95,4 +90,14 @@ public class ExceptionDataDirectory extends DataDirectory {
functionEntries.markup(program, addr);
}
}
+
+ private boolean isChpe(FileHeader fileHeader) {
+ DataDirectory[] dirs = ntHeader.getOptionalHeader().getDataDirectories();
+ if (dirs.length > OptionalHeader.IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG) {
+ LoadConfigDataDirectory dataDir =
+ (LoadConfigDataDirectory) dirs[OptionalHeader.IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG];
+ return dataDir.getLoadConfigDirectory().getChpeMetadataPointer() != 0;
+ }
+ return false;
+ }
}
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.java
index 79285db6bb..f9d184eb04 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
@@ -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.
@@ -206,6 +206,33 @@ public class FileHeader implements StructConverter {
return MachineName.getName(machine);
}
+ /**
+ * {@return whether or not the machine is an X86 variant}
+ */
+ public boolean isX86() {
+ return switch (machine & IMAGE_FILE_MACHINE_MASK) {
+ case IMAGE_FILE_MACHINE_I386:
+ case IMAGE_FILE_MACHINE_AMD64:
+ yield true;
+ default:
+ yield false;
+ };
+ }
+
+ /**
+ * {@return whether or not the machine is an ARM variant}
+ */
+ public boolean isArm() {
+ return switch (machine & IMAGE_FILE_MACHINE_MASK) {
+ case IMAGE_FILE_MACHINE_ARM:
+ case IMAGE_FILE_MACHINE_ARM64:
+ case IMAGE_FILE_MACHINE_ARMNT:
+ yield true;
+ default:
+ yield false;
+ };
+ }
+
/**
* Returns the number of sections.
* Sections equate to Ghidra memory blocks.
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDataDirectory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDataDirectory.java
index 8025e5e6ab..ef91cae755 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDataDirectory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDataDirectory.java
@@ -18,6 +18,8 @@ package ghidra.app.util.bin.format.pe;
import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.format.pe.chpe.ImageArm64ecMetadata;
+import ghidra.app.util.bin.format.pe.chpe.ImageChpeMetadataX86;
import ghidra.app.util.bin.format.pe.dvrt.ImageDynamicRelocationTable;
import ghidra.app.util.importer.MessageLog;
import ghidra.program.model.address.Address;
@@ -68,6 +70,16 @@ public class LoadConfigDataDirectory extends DataDirectory {
markupSeHandler(program, isBinary, monitor, log, nt);
ControlFlowGuard.markup(lcd, program, log, nt);
+ ImageChpeMetadataX86 chpeMetadata = lcd.getChpeMetadataX86();
+ if (chpeMetadata != null) {
+ chpeMetadata.markup(program, isBinary, monitor, log, nt);
+ }
+
+ ImageArm64ecMetadata arm64ecMetadata = lcd.getArm64ecMetadata();
+ if (arm64ecMetadata != null) {
+ arm64ecMetadata.markup(program, isBinary, monitor, log, nt);
+ }
+
ImageDynamicRelocationTable dvrt = lcd.getDynamicRelocationTable();
if (dvrt != null) {
dvrt.markup(program, isBinary, monitor, log, nt);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDirectory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDirectory.java
index 0bc5a144cc..3e62830151 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDirectory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/LoadConfigDirectory.java
@@ -19,6 +19,8 @@ import java.io.IOException;
import ghidra.app.util.bin.BinaryReader;
import ghidra.app.util.bin.StructConverter;
+import ghidra.app.util.bin.format.pe.chpe.ImageArm64ecMetadata;
+import ghidra.app.util.bin.format.pe.chpe.ImageChpeMetadataX86;
import ghidra.app.util.bin.format.pe.dvrt.ImageDynamicRelocationTable;
import ghidra.program.model.data.*;
import ghidra.util.Msg;
@@ -69,14 +71,25 @@ public class LoadConfigDirectory implements StructConverter {
private long guardRfFailureRoutineFunctionPointer;
private int dynamicValueRelocTableOffset;
private short dynamicValueRelocTableSection;
- private short reserved1;
+ private short reserved2;
private long guardRfVerifyStackPointerFunctionPointer;
private int hotPatchTableOffset;
- private int reserved2;
- private long reserved3;
+ private int reserved3;
+ private long enclaveConfigurationPointer;
+ private long volatileMetadataPointer;
+ private long guardEhContinuationTable;
+ private long guardEhContinuationCount;
+ private long guardXfgCheckFunctionPointer;
+ private long guardXfgDispatchFunctionPointer;
+ private long guardXfgTableDispatchFunctionPointer;
+ private long castGuardOsDeterminedFailureMode;
+ private long guardMemcpyFunctionPointer;
+ private long umaFunctionPointers;
private boolean is64bit;
private ImageDynamicRelocationTable dvrt;
+ private ImageChpeMetadataX86 chpeMetadataX86;
+ private ImageArm64ecMetadata arm64ecMetadata;
LoadConfigDirectory(BinaryReader reader, int index, NTHeader nt) throws IOException {
FileHeader fh = nt.getFileHeader();
@@ -144,17 +157,59 @@ public class LoadConfigDirectory implements StructConverter {
guardRfFailureRoutineFunctionPointer = readPointer(reader);
dynamicValueRelocTableOffset = reader.readNextInt();
dynamicValueRelocTableSection = reader.readNextShort();
- reserved1 = reader.readNextShort();
+ reserved2 = reader.readNextShort();
}
if (reader.getPointerIndex() - index < size) {
guardRfVerifyStackPointerFunctionPointer = readPointer(reader);
hotPatchTableOffset = reader.readNextInt();
}
if (reader.getPointerIndex() - index < size) {
- reserved2 = reader.readNextInt();
- reserved3 = readPointer(reader);
+ reserved3 = reader.readNextInt();
+ }
+ if (reader.getPointerIndex() - index < size) {
+ enclaveConfigurationPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ volatileMetadataPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardEhContinuationTable = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardEhContinuationCount = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardXfgCheckFunctionPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardXfgDispatchFunctionPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardXfgTableDispatchFunctionPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ castGuardOsDeterminedFailureMode = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ guardMemcpyFunctionPointer = readPointer(reader);
+ }
+ if (reader.getPointerIndex() - index < size) {
+ umaFunctionPointers = readPointer(reader);
}
+ // Parse the CHPE Metadata
+ if (chpeMetadataPointer != 0) {
+ FileHeader fileHeader = nt.getFileHeader();
+ BinaryReader r = reader.clone(nt.vaToPointer(chpeMetadataPointer));
+ if (fileHeader.isArm()) {
+ arm64ecMetadata = new ImageArm64ecMetadata(r, nt, chpeMetadataPointer);
+ }
+ if (fileHeader.isX86()) {
+ chpeMetadataX86 = new ImageChpeMetadataX86(r, nt, chpeMetadataPointer);
+ }
+ }
+
+ // Parse the Dynamic Value Relocation Table (DVRT)
if (dynamicValueRelocTableOffset != 0 && dynamicValueRelocTableSection != 0) {
SectionHeader section = fh.getSectionHeader(dynamicValueRelocTableSection - 1);
if (section != null) {
@@ -255,6 +310,13 @@ public class LoadConfigDirectory implements StructConverter {
return guardAddressTakenIatEntryCount;
}
+ /**
+ * {@return the CHPE metadata pointer (could be 0 if it's not a CHPE)}
+ */
+ public long getChpeMetadataPointer() {
+ return chpeMetadataPointer;
+ }
+
/**
* {@return the ReturnFlowGuard failure routine address (could be 0 if ReturnFlowGuard is not
* being used)}
@@ -287,6 +349,20 @@ public class LoadConfigDirectory implements StructConverter {
return dvrt;
}
+ /**
+ * {@return the ARM64EC metadata (could be {@code null} if there is none)}
+ */
+ public ImageArm64ecMetadata getArm64ecMetadata() {
+ return arm64ecMetadata;
+ }
+
+ /**
+ * {@return the X86 CHPE metadata (could be {@code null} if there is none)}
+ */
+ public ImageChpeMetadataX86 getChpeMetadataX86() {
+ return chpeMetadataX86;
+ }
+
@Override
public DataType toDataType() throws DuplicateNameException {
StructureDataType struct = new StructureDataType(is64bit ? NAME64 : NAME32, 0);
@@ -352,15 +428,42 @@ public class LoadConfigDirectory implements StructConverter {
struct.add(ptr, "GuardRFFailureRoutineFunctionPointer", null);
struct.add(DWORD, "DynamicValueRelocTableOffset", null);
struct.add(WORD, "DynamicValueRelocTableSection", null);
- struct.add(WORD, "Reserved1", null);
+ struct.add(WORD, "Reserved2", null);
}
if (struct.getLength() < size) {
struct.add(ptr, "GuardRFVerifyStackPointerFunctionPointer", null);
struct.add(DWORD, "HotPatchTableOffset", null);
}
if (struct.getLength() < size) {
- struct.add(DWORD, "Reserved2", null);
- struct.add(counter, "Reserved3", null);
+ struct.add(DWORD, "Reserved3", null);
+ struct.add(ptr, "EnclaveConfigurationPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "VolatileMetadataPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "GuardEHContinuationTable", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(counter, "GuardEHContinuationCount", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "GuardXFGCheckFunctionPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "GuardXFGDispatchFunctionPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "GuardXFGTableDispatchFunctionPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "CastGuardOsDeterminedFailureMode", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "GuardMemcpyFunctionPointer", null);
+ }
+ if (struct.getLength() < size) {
+ struct.add(ptr, "UmaFunctionPointers", null);
}
struct.setCategoryPath(new CategoryPath("/PE"));
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ChpeRangeType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ChpeRangeType.java
new file mode 100644
index 0000000000..14fc6f5c3c
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ChpeRangeType.java
@@ -0,0 +1,79 @@
+/* ###
+ * 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.pe.chpe;
+
+/* ###
+ * 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.
+ */
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * CHPE range types
+ */
+public enum ChpeRangeType {
+
+ ARM64(0x0),
+ ARM64EC(0x1),
+ X86_64(0x2),
+ UNKNOWN(0x100); // made up to handle unknown types
+
+ private long value;
+
+ /**
+ * Creates a new {@link ChpeRangeType}
+ *
+ * @param value The defined value of the type
+ */
+ private ChpeRangeType(long value) {
+ this.value = value;
+ }
+
+ /**
+ * {@return the type's defined value}
+ */
+ public long getValue() {
+ return value;
+ }
+
+ /**
+ * Reads a {@link ChpeRangeType}
+ *
+ * @param value The defined value of the type
+ * @return The type of the given value, or {@link #UNKNOWN} if the value does not correspond to
+ * a known type
+ * @throws IOException if there was an IO-related error
+ */
+ public static ChpeRangeType type(int value) throws IOException {
+ return Arrays.stream(ChpeRangeType.values())
+ .filter(e -> value == e.getValue())
+ .findFirst()
+ .orElse(UNKNOWN);
+ }
+}
+
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecCodeRangeEntryPoint.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecCodeRangeEntryPoint.java
new file mode 100644
index 0000000000..86ef1c8c7d
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecCodeRangeEntryPoint.java
@@ -0,0 +1,97 @@
+/* ###
+ * 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.pe.chpe;
+
+import java.io.IOException;
+
+import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverter;
+import ghidra.app.util.bin.format.pe.*;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.data.*;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryAccessException;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Represents a {@code IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT} structure
+ */
+public class ImageArm64ecCodeRangeEntryPoint implements StructConverter, PeMarkupable {
+
+ private int startRva;
+ private int endRva;
+ private int entryPoint;
+
+ private long rva;
+
+ /**
+ * Creates a new {@link ImageArm64ecCodeRangeEntryPoint}
+ *
+ * @param reader A {@link BinaryReader} that points to the start of the structure
+ * @param rva The relative virtual address of the structure
+ * @throws IOException if there was an IO-related error
+ */
+ public ImageArm64ecCodeRangeEntryPoint(BinaryReader reader, long rva) throws IOException {
+ this.rva = rva;
+
+ startRva = reader.readNextInt();
+ endRva = reader.readNextInt();
+ entryPoint = reader.readNextInt();
+ }
+
+ /**
+ * {@return the start RVA}
+ */
+ public int getStartRva() {
+ return startRva;
+ }
+
+ /**
+ * {@return the end RVA}
+ */
+ public int getEndRva() {
+ return endRva;
+ }
+
+ /**
+ * {@return the entry point RVA}
+ */
+ public int getEntryPoint() {
+ return entryPoint;
+ }
+
+ @Override
+ public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
+ NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
+ IOException, MemoryAccessException {
+ Address imageBase = program.getImageBase();
+ DataType dt = toDataType();
+ PeUtils.createData(program, imageBase.add(rva), dt, log);
+ }
+
+ @Override
+ public DataType toDataType() throws DuplicateNameException, IOException {
+ StructureDataType struct = new StructureDataType("IMAGE_ARM64EC_CODE_RANGE_ENTRY_POINT", 0);
+ struct.add(IBO32, "StartRva", null);
+ struct.add(IBO32, "EndRva", null);
+ struct.add(IBO32, "EntryPoint", null);
+ struct.setCategoryPath(new CategoryPath("/PE"));
+ return struct;
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecMetadata.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecMetadata.java
new file mode 100644
index 0000000000..d1279beec3
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecMetadata.java
@@ -0,0 +1,224 @@
+/* ###
+ * 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.pe.chpe;
+
+import java.io.IOException;
+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.pe.*;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.data.*;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryAccessException;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Represents a {@code IMAGE_ARM64EC_METADATA} structure
+ */
+@SuppressWarnings("unused")
+public class ImageArm64ecMetadata implements StructConverter, PeMarkupable {
+
+ private int version;
+ private int codeMap;
+ private int codeMapCount;
+ private int codeRangesToEntryPoints;
+ private int redirectionMetadata;
+ private int osArm64xDispatchCallNoRedirect;
+ private int osArm64xDispatchRet;
+ private int osArm64xDispatchCall;
+ private int osArm64xDispatchIcall;
+ private int osArm64xDispatchIcallCfg;
+ private int alternateEntryPoint;
+ private int auxiliaryIat;
+ private int codeRangesToEntryPointsCount;
+ private int redirectionMetadataCount;
+ private int getX64InformationFunctionPointer;
+ private int setX64InformationFunctionPointer;
+ private int extraRfeTable;
+ private int extraRfeTableSize;
+ private int osArm64xDispatchFptr;
+ private int auxiliaryIatCopy;
+ private int osArm64xHelper0;
+ private int osArm64xHelper1;
+ private int osArm64xHelper2;
+ private int osArm64xHelper3;
+ private int osArm64xHelper4;
+ private int osArm64xHelper5;
+ private int osArm64xHelper6;
+ private int osArm64xHelper7;
+ private int osArm64xHelper8;
+
+ private long va;
+ private List<ImageChpeRangeEntry> codeMapEntries = new ArrayList<>();
+ private List<ImageArm64ecRedirectionEntry> redirectionEntries = new ArrayList<>();
+ private List<ImageArm64ecCodeRangeEntryPoint> codeRangeEntryPoints = new ArrayList<>();
+
+ /**
+ * Creates a new {@link ImageArm64ecMetadata}
+ *
+ * @param reader A {@link BinaryReader} that points to the start of the structure
+ * @param nt The {@link NTHeader}
+ * @param va The virtual address of the structure
+ * @throws IOException if there was an IO-related error
+ */
+ public ImageArm64ecMetadata(BinaryReader reader, NTHeader nt, long va) throws IOException {
+ this.va = va;
+
+ version = reader.readNextInt();
+ codeMap = reader.readNextInt();
+ codeMapCount = reader.readNextInt();
+ codeRangesToEntryPoints = reader.readNextInt();
+ redirectionMetadata = reader.readNextInt();
+ osArm64xDispatchCallNoRedirect = reader.readNextInt();
+ osArm64xDispatchRet = reader.readNextInt();
+ osArm64xDispatchCall = reader.readNextInt();
+ osArm64xDispatchIcall = reader.readNextInt();
+ osArm64xDispatchIcallCfg = reader.readNextInt();
+ alternateEntryPoint = reader.readNextInt();
+ auxiliaryIat = reader.readNextInt();
+ codeRangesToEntryPointsCount = reader.readNextInt();
+ redirectionMetadataCount = reader.readNextInt();
+ getX64InformationFunctionPointer = reader.readNextInt();
+ setX64InformationFunctionPointer = reader.readNextInt();
+ extraRfeTable = reader.readNextInt();
+ extraRfeTableSize = reader.readNextInt();
+ osArm64xDispatchFptr = reader.readNextInt();
+ auxiliaryIatCopy = reader.readNextInt();
+ if (version >= 2) {
+ osArm64xHelper0 = reader.readNextInt();
+ osArm64xHelper1 = reader.readNextInt();
+ osArm64xHelper2 = reader.readNextInt();
+ osArm64xHelper3 = reader.readNextInt();
+ osArm64xHelper4 = reader.readNextInt();
+ osArm64xHelper5 = reader.readNextInt();
+ osArm64xHelper6 = reader.readNextInt();
+ osArm64xHelper7 = reader.readNextInt();
+ osArm64xHelper8 = reader.readNextInt();
+ }
+
+ BinaryReader r = reader.clone(nt.rvaToPointer(codeMap));
+ long startIndex = r.getPointerIndex();
+ for (int i = 0; i < codeMapCount; i++) {
+ codeMapEntries.add(new ImageChpeRangeEntry(r,
+ codeMap + (r.getPointerIndex() - startIndex)));
+ }
+
+ r = reader.clone(nt.rvaToPointer(codeRangesToEntryPoints));
+ startIndex = r.getPointerIndex();
+ for (int i = 0; i < codeRangesToEntryPointsCount; i++) {
+ codeRangeEntryPoints.add(new ImageArm64ecCodeRangeEntryPoint(r,
+ codeRangesToEntryPoints + (r.getPointerIndex() - startIndex)));
+ }
+
+ r = reader.clone(nt.rvaToPointer(redirectionMetadata));
+ startIndex = r.getPointerIndex();
+ for (int i = 0; i < redirectionMetadataCount; i++) {
+ redirectionEntries.add(new ImageArm64ecRedirectionEntry(r,
+ redirectionMetadata + (r.getPointerIndex() - startIndex)));
+ }
+ }
+
+ /**
+ * {@return the metadata version}
+ */
+ public int getVersion() {
+ return version;
+ }
+
+ /**
+ * {@return the {@link List} of {@link ImageChpeRangeEntry code map entries}}
+ */
+ public List<ImageChpeRangeEntry> getCodeMapEntries() {
+ return codeMapEntries;
+ }
+
+ /**
+ * {@return the {@link List} of {@link ImageArm64ecRedirectionEntry redirection entries}}
+ */
+ public List<ImageArm64ecRedirectionEntry> getRedirectionEntries() {
+ return redirectionEntries;
+ }
+
+ /**
+ * {@return the {@link List} of {@link ImageArm64ecCodeRangeEntryPoint code range entry points}}
+ */
+ public List<ImageArm64ecCodeRangeEntryPoint> getCodeRangeEntryPoints() {
+ return codeRangeEntryPoints;
+ }
+
+ @Override
+ public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
+ NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
+ IOException, MemoryAccessException {
+ AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();
+ PeUtils.createData(program, space.getAddress(va), toDataType(), log);
+ for (ImageChpeRangeEntry entry : codeMapEntries) {
+ entry.markup(program, isBinary, monitor, log, ntHeader);
+ }
+ for (ImageArm64ecCodeRangeEntryPoint entry : codeRangeEntryPoints) {
+ entry.markup(program, isBinary, monitor, log, ntHeader);
+ }
+ for (ImageArm64ecRedirectionEntry entry : redirectionEntries) {
+ entry.markup(program, isBinary, monitor, log, ntHeader);
+ }
+ }
+
+ @Override
+ public DataType toDataType() throws DuplicateNameException, IOException {
+ StructureDataType struct = new StructureDataType("IMAGE_ARM64EC_METADATA", 0);
+ struct.add(DWORD, "Version", null);
+ struct.add(IBO32, "CodeMap", null);
+ struct.add(DWORD, "CodeMapCount", null);
+ struct.add(IBO32, "CodeRangesToEntryPoint", null);
+ struct.add(IBO32, "RedirectionMetdata", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_call_no_redirect", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_ret", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_call", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_icall", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_icall_cfg", null);
+ struct.add(IBO32, "AlternateEntryPoint", null);
+ struct.add(IBO32, "AuxiliaryIAT", null);
+ struct.add(DWORD, "CodeRangesToEntryPointsCount", null);
+ struct.add(DWORD, "RedirectionMetadataCount", null);
+ struct.add(IBO32, "GetX64InformationFunctionPointer", null);
+ struct.add(IBO32, "SetX64InformationFunctionPointer", null);
+ struct.add(IBO32, "ExtraRFETable", null);
+ struct.add(DWORD, "ExtraRFETableSize", null);
+ struct.add(IBO32, "__os_arm64x_dispatch_fptr", null);
+ struct.add(IBO32, "AuxiliaryIATCopy", null);
+ if (version >= 2) {
+ struct.add(IBO32, "__os_arm64x_helper0", null);
+ struct.add(IBO32, "__os_arm64x_helper1", null);
+ struct.add(IBO32, "__os_arm64x_helper2", null);
+ struct.add(IBO32, "__os_arm64x_helper3", null);
+ struct.add(IBO32, "__os_arm64x_helper4", null);
+ struct.add(IBO32, "__os_arm64x_helper5", null);
+ struct.add(IBO32, "__os_arm64x_helper6", null);
+ struct.add(IBO32, "__os_arm64x_helper7", null);
+ struct.add(IBO32, "__os_arm64x_helper8", null);
+ }
+
+ struct.setCategoryPath(new CategoryPath("/PE"));
+ return struct;
+ }
+
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecRedirectionEntry.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecRedirectionEntry.java
new file mode 100644
index 0000000000..b950c8b6d5
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageArm64ecRedirectionEntry.java
@@ -0,0 +1,87 @@
+/* ###
+ * 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.pe.chpe;
+
+import java.io.IOException;
+
+import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverter;
+import ghidra.app.util.bin.format.pe.*;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.data.*;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryAccessException;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Represents a {@code IMAGE_ARM64EC_REDIRECTION_ENTRY} structure
+ */
+public class ImageArm64ecRedirectionEntry implements StructConverter, PeMarkupable {
+
+ private int source;
+ private int destination;
+
+ private long rva;
+
+ /**
+ * Creates a new {@link ImageArm64ecRedirectionEntry}
+ *
+ * @param reader A {@link BinaryReader} that points to the start of the structure
+ * @param rva The relative virtual address of the structure
+ * @throws IOException if there was an IO-related error
+ */
+ public ImageArm64ecRedirectionEntry(BinaryReader reader, long rva) throws IOException {
+ this.rva = rva;
+
+ source = reader.readNextInt();
+ destination = reader.readNextInt();
+ }
+
+ /**
+ * {@return the source RVA}
+ */
+ public int getSource() {
+ return source;
+ }
+
+ /**
+ * {@return the destination RVA}
+ */
+ public int getDesintation() {
+ return destination;
+ }
+
+ @Override
+ public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
+ NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
+ IOException, MemoryAccessException {
+ Address imageBase = program.getImageBase();
+ DataType dt = toDataType();
+ PeUtils.createData(program, imageBase.add(rva), dt, log);
+ }
+
+ @Override
+ public DataType toDataType() throws DuplicateNameException, IOException {
+ StructureDataType struct = new StructureDataType("IMAGE_ARM64EC_REDIRECTION_ENTRY", 0);
+ struct.add(IBO32, "Source", null);
+ struct.add(IBO32, "Destination", null);
+ struct.setCategoryPath(new CategoryPath("/PE"));
+ return struct;
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeMetadataX86.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeMetadataX86.java
new file mode 100644
index 0000000000..aeb8a90a05
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeMetadataX86.java
@@ -0,0 +1,155 @@
+/* ###
+ * 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.pe.chpe;
+
+import java.io.IOException;
+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.pe.*;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.AddressSpace;
+import ghidra.program.model.data.*;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryAccessException;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Represents a {@code IMAGE_CHPE_METADATA_X86} structure
+ */
+@SuppressWarnings("unused")
+public class ImageChpeMetadataX86 implements StructConverter, PeMarkupable {
+
+ private int version;
+ private int chpeCodeAddressRangeOffset;
+ private int chpeCodeAddressRangeCount;
+ private int wowA64ExceptionHandlerFunctionPointer;
+ private int wowA64DispatchCallFunctionPointer;
+ private int wowA64DispatchIndirectCallFunctionPointer;
+ private int wowA64DispatchIndirectCallCfgFunctionPointer;
+ private int wowA64DispatchRetFunctionPointer;
+ private int wowA64DispatchRetLeafFunctionPointer;
+ private int wowA64DispatchJumpFunctionPointer;
+ private int compilerIatPointer;
+ private int wowA64RdtscFunctionPointer;
+ private int unknown1;
+ private int unknown2;
+ private int unknown3;
+ private int unknown4;
+
+ private long va;
+ private List<ImageChpeRangeEntry> codeMapEntries = new ArrayList<>();
+
+ /**
+ * Creates a new {@link ImageChpeMetadataX86}
+ *
+ * @param reader A {@link BinaryReader} that points to the start of the structure
+ * @param nt The {@link NTHeader}
+ * @param va The virtual address of the structure
+ * @throws IOException if there was an IO-related error
+ */
+ public ImageChpeMetadataX86(BinaryReader reader, NTHeader nt, long va) throws IOException {
+ this.va = va;
+
+ version = reader.readNextInt();
+ chpeCodeAddressRangeOffset = reader.readNextInt();
+ chpeCodeAddressRangeCount = reader.readNextInt();
+ wowA64ExceptionHandlerFunctionPointer = reader.readNextInt();
+ wowA64DispatchCallFunctionPointer = reader.readNextInt();
+ wowA64DispatchIndirectCallFunctionPointer = reader.readNextInt();
+ wowA64DispatchIndirectCallCfgFunctionPointer = reader.readNextInt();
+ wowA64DispatchRetFunctionPointer = reader.readNextInt();
+ wowA64DispatchRetLeafFunctionPointer = reader.readNextInt();
+ wowA64DispatchJumpFunctionPointer = reader.readNextInt();
+ if (version >= 2) {
+ compilerIatPointer = reader.readNextInt();
+ }
+ if (version >= 3) {
+ wowA64RdtscFunctionPointer = reader.readNextInt();
+ }
+ if (version >= 4) {
+ unknown1 = reader.readNextInt();
+ unknown2 = reader.readNextInt();
+ unknown3 = reader.readNextInt();
+ unknown4 = reader.readNextInt();
+ }
+
+ BinaryReader r = reader.clone(nt.rvaToPointer(chpeCodeAddressRangeOffset));
+ long startIndex = r.getPointerIndex();
+ for (int i = 0; i < chpeCodeAddressRangeCount; i++) {
+ codeMapEntries.add(new ImageChpeRangeEntry(r,
+ chpeCodeAddressRangeOffset + (r.getPointerIndex() - startIndex)));
+ }
+ }
+
+ /**
+ * {@return the metadata version}
+ */
+ public int getVersion() {
+ return version;
+ }
+
+ /**
+ * {@return the {@link List} of {@link ImageChpeRangeEntry code map entries}}
+ */
+ public List<ImageChpeRangeEntry> getCodeMapEntries() {
+ return codeMapEntries;
+ }
+
+ @Override
+ public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
+ NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
+ IOException, MemoryAccessException {
+ AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();
+ PeUtils.createData(program, space.getAddress(va), toDataType(), log);
+ for (ImageChpeRangeEntry entry : codeMapEntries) {
+ entry.markup(program, isBinary, monitor, log, ntHeader);
+ }
+ }
+
+ @Override
+ public DataType toDataType() throws DuplicateNameException, IOException {
+ StructureDataType struct = new StructureDataType("IMAGE_CHPE_METADATA_X86", 0);
+ struct.add(DWORD, "Version", null);
+ struct.add(IBO32, "CHPECodeAddressRangeOffset", null);
+ struct.add(DWORD, "CHPECodeAddressRangeCount", null);
+ struct.add(IBO32, "WowA64ExceptionHandlerFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchCallFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchIndirectCallFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchIndirectCallCfgFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchRetFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchRetLeafFunctionPointer", null);
+ struct.add(IBO32, "WowA64DispatchJumpFunctionPointer", null);
+ if (version >= 2) {
+ struct.add(IBO32, "CompilerIATPointer", null);
+ }
+ if (version >= 3) {
+ struct.add(IBO32, "WowA64RdtscFunctionPointer", null);
+ }
+ if (version >= 4) {
+ struct.add(IBO32, "Unknown1", null);
+ struct.add(IBO32, "Unknown2", null);
+ struct.add(IBO32, "Unknown3", null);
+ struct.add(IBO32, "Unknown4", null);
+ }
+ struct.setCategoryPath(new CategoryPath("/PE"));
+ return struct;
+ }
+}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeRangeEntry.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeRangeEntry.java
new file mode 100644
index 0000000000..388d990c5b
--- /dev/null
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/pe/chpe/ImageChpeRangeEntry.java
@@ -0,0 +1,109 @@
+/* ###
+ * 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.pe.chpe;
+
+import java.io.IOException;
+
+import ghidra.app.util.bin.BinaryReader;
+import ghidra.app.util.bin.StructConverter;
+import ghidra.app.util.bin.format.pe.*;
+import ghidra.app.util.importer.MessageLog;
+import ghidra.program.model.address.Address;
+import ghidra.program.model.data.*;
+import ghidra.program.model.listing.CommentType;
+import ghidra.program.model.listing.Program;
+import ghidra.program.model.mem.MemoryAccessException;
+import ghidra.program.model.util.CodeUnitInsertionException;
+import ghidra.util.exception.DuplicateNameException;
+import ghidra.util.task.TaskMonitor;
+
+/**
+ * Represents a {@code IMAGE_CHPE_RANGE_ENTRY} structure
+ */
+public class ImageChpeRangeEntry implements StructConverter, PeMarkupable {
+
+ private static final int TYPE_MASK = 0x3;
+ private static final int OFFSET_MASK = ~TYPE_MASK;
+
+ private ChpeRangeType type;
+ private int startOffset;
+ private long length;
+
+ private long rva;
+
+ /**
+ * Creates a new {@link ImageChpeRangeEntry}
+ *
+ * @param reader A {@link BinaryReader} that points to the start of the structure
+ * @param rva The relative virtual address of the structure
+ * @throws IOException if there was an IO-related error
+ */
+ public ImageChpeRangeEntry(BinaryReader reader, long rva) throws IOException {
+ this.rva = rva;
+
+ int value = reader.readNextInt();
+ type = ChpeRangeType.type(value & TYPE_MASK);
+ startOffset = value & OFFSET_MASK;
+ length = reader.readNextUnsignedInt();
+ }
+
+ /**
+ * {@return the {@link ChpeRangeType type} of range
+ */
+ public ChpeRangeType getRangeType() {
+ return type;
+ }
+
+ /**
+ * {@return the start offset of the range}
+ */
+ public int getStartOffset() {
+ return startOffset;
+ }
+
+ /**
+ * {@return the length of the range}
+ */
+ public long getLength() {
+ return length;
+ }
+
+ @Override
+ public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
+ NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
+ IOException, MemoryAccessException {
+ Address imageBase = program.getImageBase();
+ DataType dt = toDataType();
+ Address addr = imageBase.add(rva);
+ PeUtils.createData(program, addr, dt, log);
+ program.getListing().setComment(addr, CommentType.PLATE, type.name());
+ }
+
+ @Override
+ public DataType toDataType() throws DuplicateNameException, IOException {
+ TypeDef offsetPtr =
+ new PointerTypedefBuilder(Pointer32DataType.dataType, null)
+ .type(PointerType.IMAGE_BASE_RELATIVE)
+ .bitMask(OFFSET_MASK)
+ .build();
+
+ StructureDataType struct = new StructureDataType("IMAGE_CHPE_RANGE_ENTRY", 0);
+ struct.add(offsetPtr, "StartOffset", null);
+ struct.add(DWORD, "Length", null);
+ struct.setCategoryPath(new CategoryPath("/PE"));
+ return struct;
+ }
+}