Ghidra is a software reverse engineering (SRE) framework
PDB - Added OMAP to DebugData and starting to investigate its use.
ghizard92ebc3638251
diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/AbstractSymbolInformation.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/AbstractSymbolInformation.javaindex ec1a44b566..a2a4808330 100644--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/AbstractSymbolInformation.java+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/AbstractSymbolInformation.java@@ -139,7 +139,7 @@ public abstract class AbstractSymbolInformation {builder.append(numExtraBytes);builder.append("\nnumHashRecords: ");builder.append(numHashRecords);- builder.append("\nEnd HashBasics---------------------------------------------\n");+ builder.append("\nEnd HashBasics----------------------------------------------\n");}/**@@ -156,7 +156,7 @@ public abstract class AbstractSymbolInformation {builder.append(hashRecordsLength);builder.append("\nlengthBuckets: ");builder.append(bucketsLength);- builder.append("\nEnd HashHeader---------------------------------------------\n");+ builder.append("\nEnd HashHeader----------------------------------------------\n");}/**@@ -192,7 +192,7 @@ public abstract class AbstractSymbolInformation {builder.append(String.format("0X%08X 0X%04X\n", record.getOffset(), record.getReferenceCount()));}- builder.append("\nEnd HashRecords--------------------------------------------\n");+ builder.append("\nEnd HashRecords---------------------------------------------\n");}/**diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.javaindex 774cf7bcb4..09be0806b8 100644--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/DebugData.java@@ -66,10 +66,12 @@ public class DebugData {private AbstractPdb pdb;private List<Integer> debugStreams = new ArrayList<>();- private List<FramePointerOmissionRecord> framePointerOmissionData = new ArrayList<>();- private List<ImageSectionHeader> imageSectionHeaders = new ArrayList<>();+ private List<FramePointerOmissionRecord> framePointerOmissionData;+ private Map<Long, Long> omapToSource;+ private Map<Long, Long> omapFromSource;+ private List<ImageSectionHeader> imageSectionHeaders;- private List<ImageFunctionEntry> pData = new ArrayList<>();+ private List<ImageFunctionEntry> pData;private RvaVaDebugHeader xDataHeader;private PdbByteReader xDataReader;@@ -86,9 +88,33 @@ public class DebugData {this.pdb = pdb;}+ /**+ * Returns the Frame Pointer Omission data+ * @return the framePointerOmissionData or null if does not exist.+ */+ public List<FramePointerOmissionRecord> getFramePointerOmissionData() {+ return framePointerOmissionData;+ }++ /**+ * Returns the OMAP_TO_SOURCE mapping of RVA to RVA+ * @return the omapToSource or null if does not exist.+ */+ public Map<Long, Long> getOmapToSource() {+ return omapToSource;+ }++ /**+ * Returns the OMAP_FROM_SOURCE mapping of RVA to RVA+ * @return the omapFromSource or null if does not exist.+ */+ public Map<Long, Long> getOmapFromSource() {+ return omapFromSource;+ }+/*** Returns the {@link List}<{@link ImageSectionHeader}>.- * @return the imageSectionHeaders.+ * @return the imageSectionHeaders or null if does not exist.*/public List<ImageSectionHeader> getImageSectionHeaders() {return imageSectionHeaders;@@ -152,10 +178,10 @@ public class DebugData {// TODO: implement.break;case OMAP_TO_SOURCE:- // TODO: implement.+ deserializeOMapToSource(streamNum, monitor);break;case OMAP_FROM_SOURCE:- // TODO: implement.+ deserializeOMapFromSource(streamNum, monitor);break;case SECTION_HEADER:deserializeSectionHeader(streamNum, monitor);@@ -183,6 +209,7 @@ public class DebugData {throws PdbException, CancelledException, IOException {// TODO: check implementation for completeness.PdbByteReader reader = pdb.getReaderForStreamNumber(streamNum, monitor);+ framePointerOmissionData = new ArrayList<>();while (reader.hasMore()) {monitor.checkCanceled();FramePointerOmissionRecord framePointerOmissionRecord =@@ -192,9 +219,52 @@ public class DebugData {}}+ private void deserializeOMapToSource(int streamNum, TaskMonitor monitor)+ throws CancelledException, IOException {+ PdbByteReader reader = pdb.getReaderForStreamNumber(streamNum, monitor);+// PdbLog.message("OMAP_TO_SOURCE DUMP");+// PdbLog.message(reader::dump);+ omapToSource = new HashMap<>();+ while (reader.hasMore()) {+ monitor.checkCanceled();+ try {+ long v1 = reader.parseUnsignedIntVal();+ long v2 = reader.parseUnsignedIntVal();+ omapToSource.put(v1, v2);+ }+ catch (PdbException e) {+ // catching if we do not have a matching pair and then breaking from loop.+ PdbLog.message("OmapToSource unmatched pair");+ break;+ }+ }+ }++ private void deserializeOMapFromSource(int streamNum, TaskMonitor monitor)+ throws CancelledException, IOException {+ PdbByteReader reader = pdb.getReaderForStreamNumber(streamNum, monitor);+// PdbLog.message("OMAP_FROM_SOURCE DUMP");+// PdbLog.message(reader::dump);+ omapFromSource = new HashMap<>();+ while (reader.hasMore()) {+ monitor.checkCanceled();+ try {+ long v1 = reader.parseUnsignedIntVal();+ long v2 = reader.parseUnsignedIntVal();+ omapFromSource.put(v1, v2);+ }+ catch (PdbException e) {+ // catching if we do not have a matching pair and then breaking from loop.+ PdbLog.message("OmapFromSource unmatched pair");+ break;+ }+ }+ }+private void deserializeSectionHeader(int streamNum, TaskMonitor monitor)throws PdbException, CancelledException, IOException {PdbByteReader reader = pdb.getReaderForStreamNumber(streamNum, monitor);+ imageSectionHeaders = new ArrayList<>();while (reader.hasMore()) {monitor.checkCanceled();ImageSectionHeader imageSectionHeader = new ImageSectionHeader(pdb);@@ -239,6 +309,7 @@ public class DebugData {private void deserializePData(int streamNum, TaskMonitor monitor)throws PdbException, CancelledException, IOException {PdbByteReader reader = pdb.getReaderForStreamNumber(streamNum, monitor);+ pData = new ArrayList<>();int streamLength = reader.getLimit();RvaVaDebugHeader header = new RvaVaDebugHeader();header.deserialize(reader);@@ -294,23 +365,49 @@ public class DebugData {dumpDebugStreamList(writer);writer.write("FramePointerOmissionData------------------------------------\n");- for (FramePointerOmissionRecord framePointerOmissionRecord : framePointerOmissionData) {- framePointerOmissionRecord.dump(writer);+ if (framePointerOmissionData != null) {+ for (FramePointerOmissionRecord framePointerOmissionRecord : framePointerOmissionData) {+ framePointerOmissionRecord.dump(writer);+ }}writer.write("End FramePointerOmissionData--------------------------------\n");- int sectionNum = 0;+ writer.write("OmapToSource------------------------------------------------\n");+ if (omapToSource != null) {+ int num = 0;+ for (Map.Entry<Long, Long> entry : omapToSource.entrySet()) {+ writer.write(String.format("0X%08X: 0X%012X, 0X%012X\n", num++, entry.getKey(),+ entry.getValue()));+ }+ }+ writer.write("End OmapToSource--------------------------------------------\n");++ writer.write("OmapFromSource----------------------------------------------\n");+ if (omapFromSource != null) {+ int num = 0;+ for (Map.Entry<Long, Long> entry : omapFromSource.entrySet()) {+ writer.write(String.format("0X%08X: 0X%012X, 0X%012X\n", num++, entry.getKey(),+ entry.getValue()));+ }+ }+ writer.write("End OmapFromSource------------------------------------------\n");+writer.write("ImageSectionHeaders-----------------------------------------\n");- for (ImageSectionHeader imageSectionHeader : imageSectionHeaders) {- imageSectionHeader.dump(writer, sectionNum++);+ if (imageSectionHeaders != null) {+ int sectionNum = 0;+ for (ImageSectionHeader imageSectionHeader : imageSectionHeaders) {+ imageSectionHeader.dump(writer, sectionNum++);+ }}writer.write("End ImageSectionHeaders-------------------------------------\n");writer.write("PData-------------------------------------------------------\n");- for (ImageFunctionEntry entry : pData) {- // TODO: need to output more if/when more PData is available (e.g., interpretation- // of XData.- writer.append(entry.toString());+ if (pData != null) {+ for (ImageFunctionEntry entry : pData) {+ // TODO: need to output more if/when more PData is available (e.g., interpretation+ // of XData.+ writer.append(entry.toString());+ }}writer.write("End PData---------------------------------------------------\n");diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/ImageSectionHeader.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/ImageSectionHeader.javaindex 016afa4e7a..20d45312fd 100644--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/ImageSectionHeader.java+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/bin/format/pdb2/pdbreader/ImageSectionHeader.java@@ -30,6 +30,22 @@ public class ImageSectionHeader {private AbstractPdb pdb;+ private String name;+ // TODO:+ // unionPAVS: DWORD (unsigned 32-bit). Either Physical Address of Virtual Size--not sure+ // what to key off of to interpret one over the other. Guess that it has to do with+ // VirtualAddress--perhaps a value of 0x00000000 or 0xffffffff.+ // See the to-do below (in dump()) regarding unionPAVS.+ private long unionPAVS;+ private long virtualAddress; // DWORD (unsigned 32-bit)+ private long rawDataSize; // DWORD (unsigned 32-bit)+ private long rawDataPointer; // DWORD (unsigned 32-bit)+ private long relocationsPointer; // DWORD (unsigned 32-bit)+ private long lineNumbersPointer; // DWORD (unsigned 32-bit)+ private int numRelocations; // WORD (unsigned 16-bit)+ private int numLineNumbers; // WORD (unsigned 16-bit)+ private long characteristics; // DWORD (unsigned 32-bit)+/*** Constructor.* @param pdb {@link AbstractPdb} to which this type belongs.@@ -39,6 +55,28 @@ public class ImageSectionHeader {this.pdb = pdb;}+ /**+ * Parse the values of this class.+ * @param reader the {@link PdbByteReader} from which to parse the values.+ * @throws PdbException upon no enough data to parse.+ */+ public void parse(PdbByteReader reader) throws PdbException {+ if (reader.numRemaining() < 40) {+ throw new PdbException("Not enough data for ImageSectionHeader");+ }+ PdbByteReader nameReader = reader.getSubPdbByteReader(8);+ name = nameReader.parseNullTerminatedString(pdb.getPdbReaderOptions().getOneByteCharset());+ unionPAVS = reader.parseUnsignedIntVal();+ virtualAddress = reader.parseUnsignedIntVal();+ rawDataSize = reader.parseUnsignedIntVal();+ rawDataPointer = reader.parseUnsignedIntVal();+ relocationsPointer = reader.parseUnsignedIntVal();+ lineNumbersPointer = reader.parseUnsignedIntVal();+ numRelocations = reader.parseUnsignedShortVal();+ numLineNumbers = reader.parseUnsignedShortVal();+ characteristics = reader.parseUnsignedIntVal();+ }+/*** Returns the {@link ImageSectionHeader} name.* @return the name.@@ -120,39 +158,6 @@ public class ImageSectionHeader {return characteristics;}- private String name;- // TODO:- // unionPAVS: DWORD (unsigned 32-bit). Either Physical Address of Virtual Size--not sure- // what to key off of to interpret one over the other. Guess that it has to do with- // VirtualAddress--perhaps a value of 0x00000000 or 0xffffffff.- // See the to-do below (in dump()) regarding unionPAVS.- private long unionPAVS;- private long virtualAddress; // DWORD (unsigned 32-bit)- private long rawDataSize; // DWORD (unsigned 32-bit)- private long rawDataPointer; // DWORD (unsigned 32-bit)- private long relocationsPointer; // DWORD (unsigned 32-bit)- private long lineNumbersPointer; // DWORD (unsigned 32-bit)- private int numRelocations; // WORD (unsigned 16-bit)- private int numLineNumbers; // WORD (unsigned 16-bit)- private long characteristics; // DWORD (unsigned 32-bit)-- public void parse(PdbByteReader reader) throws PdbException {- if (reader.numRemaining() < 40) {- throw new PdbException("Not enough data for ImageSectionHeader");- }- PdbByteReader nameReader = reader.getSubPdbByteReader(8);- name = nameReader.parseNullTerminatedString(pdb.getPdbReaderOptions().getOneByteCharset());- unionPAVS = reader.parseUnsignedIntVal();- virtualAddress = reader.parseUnsignedIntVal();- rawDataSize = reader.parseUnsignedIntVal();- rawDataPointer = reader.parseUnsignedIntVal();- relocationsPointer = reader.parseUnsignedIntVal();- lineNumbersPointer = reader.parseUnsignedIntVal();- numRelocations = reader.parseUnsignedShortVal();- numLineNumbers = reader.parseUnsignedShortVal();- characteristics = reader.parseUnsignedIntVal();- }-/*** Dumps the {@link ImageSectionHeader}. This package-protected method is for* debugging only.diff --git a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/PdbAddressManager.java b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/PdbAddressManager.javaindex 5d95eb9b86..f0149c0b83 100644--- a/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/PdbAddressManager.java+++ b/Ghidra/Features/PDB/src/main/java/ghidra/app/util/pdb/pdbapplicator/PdbAddressManager.java@@ -42,6 +42,9 @@ public class PdbAddressManager {//==============================================================================================private Map<Integer, Long> realAddressesBySection;+ private List<SegmentMapDescription> segmentMapList;+ private List<ImageSectionHeader> imageSectionHeaders;+ private Map<Long, Long> omapFromSource;private List<PeCoffGroupMsSymbol> memoryGroupRefinement;private List<PeCoffSectionMsSymbol> memorySectionRefinement;private List<SegmentInfo> allSegmentsInfo;@@ -81,11 +84,12 @@ public class PdbAddressManager {realAddressesBySection = new HashMap<>();memoryGroupRefinement = new ArrayList<>();memorySectionRefinement = new ArrayList<>();+ // TODO allSegmentInfo might go away if we use ImageSectionHeader. Under investigation.allSegmentsInfo = new ArrayList<>();addressByPreExistingSymbolName = new HashMap<>();primarySymbolByAddress = new HashMap<>();-determineMemoryBlocks();+ determineMemoryBlocks_orig();mapPreExistingSymbols();createAddressRemap();}@@ -140,6 +144,48 @@ public class PdbAddressManager {* {@code Address.EXTERNAL_ADDRESS} if the address is external to the program.*/Address getRawAddress(int segment, long offset) {+ // Investigating+ return getRawAddress_new(segment, offset);+ //return getRawAddress_orig(segment, offset);+ }++ private Address getRawAddress_new(int segment, long offset) {+ if (segment < 0) {+ return BAD_ADDRESS;+ }+ Long relativeVirtualAddress;+ if (imageSectionHeaders != null) {+ if (segment > imageSectionHeaders.size() + 1) {+ return BAD_ADDRESS;+ }+ else if (segment == 0 || segment == imageSectionHeaders.size() + 1) {+ // External address.+ return EXTERNAL_ADDRESS;+ }+ relativeVirtualAddress =+ imageSectionHeaders.get(segment - 1).getVirtualAddress() + offset;+ }+ else {+ if (segment > segmentMapList.size() + 1) {+ return BAD_ADDRESS;+ }+ else if (segment == 0 || segment == segmentMapList.size() + 1) {+ // External address.+ return EXTERNAL_ADDRESS;+ }+ // TODO: Need to verify. Guessing at the moment+ relativeVirtualAddress = segmentMapList.get(segment - 1).getSegmentOffset();+ }+ if (omapFromSource != null) {+ relativeVirtualAddress = omapFromSource.get(relativeVirtualAddress);+ if (relativeVirtualAddress == null) {+ return BAD_ADDRESS;+ }+ }+ return imageBase.add(relativeVirtualAddress);+ }++ private Address getRawAddress_orig(int segment, long offset) {if (segment < 0 || segment > allSegmentsInfo.size()) {return BAD_ADDRESS;}@@ -232,6 +278,7 @@ public class PdbAddressManager {//==============================================================================================//==============================================================================================+ // TODO: this class might go away if we use ImageSectionHeaders directly.private class SegmentInfo {private Address start;private long length;@@ -250,7 +297,7 @@ public class PdbAddressManager {}}- private void determineMemoryBlocks() {+ private void determineMemoryBlocks_orig() {// Set section/segment 0 to image base. (should be what is header), but what is its size?// TODO... made up size for now... is there something else? We could put null instead.// For now, the method that reads this information might report EXTERNAL instead of@@ -282,6 +329,16 @@ public class PdbAddressManager {}}+ private void determineMemoryBlocks() {+ PdbDebugInfo dbi = applicator.getPdb().getDebugInfo();+ segmentMapList = dbi.getSegmentMapList();+ if (dbi instanceof PdbNewDebugInfo) {+ DebugData debugData = ((PdbNewDebugInfo) dbi).getDebugData();+ omapFromSource = debugData.getOmapFromSource();+ imageSectionHeaders = debugData.getImageSectionHeaders();+ }+ }+//==============================================================================================//==============================================================================================/**