public static CodeViewData Read(IMAGE_DEBUG_DIRECTORY ddir,BinaryReader r) { var cvReader = new CodeViewReader { ddir=ddir, r=r }; cvReader.DoRead(); return cvReader.cvData; }
public void ImageDebugDirectoryConstructorWorks_Test() { var idd = new IMAGE_DEBUG_DIRECTORY(RawStructures.RawDebugDirectory, 2); Assert.AreEqual((uint) 0x44332211, idd.Characteristics); Assert.AreEqual(0x88776655, idd.TimeDateStamp); Assert.AreEqual((ushort) 0xaa99, idd.MajorVersion); Assert.AreEqual((ushort) 0xccbb, idd.MinorVersion); Assert.AreEqual((uint) 0x11ffeedd, idd.Type); Assert.AreEqual((uint) 0x55443322, idd.SizeOfData); Assert.AreEqual(0x99887766, idd.AddressOfRawData); Assert.AreEqual(0xddccbbaa, idd.PointerToRawData); }
public byte[] GetDebugInfo(out IMAGE_DEBUG_DIRECTORY pIDD) { uint size; writer.GetDebugInfo(out pIDD, 0, out size, null); var buffer = new byte[size]; writer.GetDebugInfo(out pIDD, size, out size, buffer); return buffer; }
public void NotifyDebugDir(int fExecutable, uint cbData, ref IMAGE_DEBUG_DIRECTORY pbData) { Debug.Assert(cbData == Marshal.SizeOf(typeof(IMAGE_DEBUG_DIRECTORY)), "Got unexpected size for IMAGE_DEBUG_DIRECTORY"); // There may be mutliple calls, or calls with no timestamp, but only one entry should be // for the code-view record describing the PDB file. if (pbData.Type == ImageDebugType.IMAGE_DEBUG_TYPE_CODEVIEW) { Debug.Assert(fExecutable == 1, "Got debug directory that wasn't read from an executable"); Debug.Assert(m_debugTimeDateStamp == 0, "Got unexpected duplicate NotifyDebugDir callback"); Debug.Assert(pbData.TimeDateStamp != 0, "Got unexpected 0 debug timestamp in DLL"); m_debugTimeDateStamp = pbData.TimeDateStamp; } }
public abstract bool GetDebugInfo(ChecksumAlgorithm pdbChecksumAlgorithm, ref uint pdbAge, out Guid guid, out uint stamp, out IMAGE_DEBUG_DIRECTORY pIDD, out byte[] codeViewData);
public override unsafe bool GetDebugInfo(ChecksumAlgorithm pdbChecksumAlgorithm, ref uint pdbAge, out Guid guid, out uint stamp, out IMAGE_DEBUG_DIRECTORY pIDD, out byte[] codeViewData) { pIDD = default; codeViewData = null; if (isDeterministic) { ((ISymUnmanagedWriter3)writer).Commit(); var oldPos = pdbStream.Position; pdbStream.Position = 0; var checksumBytes = Hasher.Hash(pdbChecksumAlgorithm, pdbStream, pdbStream.Length); pdbStream.Position = oldPos; if (writer is ISymUnmanagedWriter8 writer8) { RoslynContentIdProvider.GetContentId(checksumBytes, out guid, out stamp); writer8.UpdateSignature(guid, stamp, pdbAge); return(true); } else if (writer is ISymUnmanagedWriter7 writer7) { fixed(byte *p = checksumBytes) writer7.UpdateSignatureByHashingContent(new IntPtr(p), (uint)checksumBytes.Length); } } writer.GetDebugInfo(out pIDD, 0, out uint size, null); codeViewData = new byte[size]; writer.GetDebugInfo(out pIDD, size, out size, codeViewData); if (writer is IPdbWriter comPdbWriter) { var guidBytes = new byte[16]; Array.Copy(codeViewData, 4, guidBytes, 0, 16); guid = new Guid(guidBytes); comPdbWriter.GetSignatureAge(out stamp, out uint age); Debug.Assert(age == pdbAge); pdbAge = age; return(true); } Debug.Fail($"COM PDB writer doesn't impl {nameof(IPdbWriter)}"); guid = default; stamp = 0; return(false); }
public PeHeaderReader(string filePath) { using (FileStream stream = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { BinaryReader reader = new BinaryReader(stream); dosHeader = FromBinaryReader <IMAGE_DOS_HEADER>(reader); // Add 4 bytes to the offset stream.Seek(dosHeader.e_lfanew, SeekOrigin.Begin); uint ntHeadersSignature = reader.ReadUInt32(); fileHeader = FromBinaryReader <IMAGE_FILE_HEADER>(reader); if (this.Is32BitHeader) { optionalHeader32 = FromBinaryReader <IMAGE_OPTIONAL_HEADER32>(reader); } else { optionalHeader64 = FromBinaryReader <IMAGE_OPTIONAL_HEADER64>(reader); } uint offDebug = 0; uint cbDebug = 0; long cbFromHeader = 0; int loopexit = 0; if (this.Is32BitHeader) { cbDebug = optionalHeader32.Debug.Size; } else { cbDebug = optionalHeader64.Debug.Size; } imageSectionHeaders = new IMAGE_SECTION_HEADER[fileHeader.NumberOfSections]; for (int headerNo = 0; headerNo < imageSectionHeaders.Length; ++headerNo) { imageSectionHeaders[headerNo] = FromBinaryReader <IMAGE_SECTION_HEADER>(reader); if ((imageSectionHeaders[headerNo].PointerToRawData != 0) && (imageSectionHeaders[headerNo].SizeOfRawData != 0) && (cbFromHeader < (long) (imageSectionHeaders[headerNo].PointerToRawData + imageSectionHeaders[headerNo].SizeOfRawData))) { cbFromHeader = (long) (imageSectionHeaders[headerNo].PointerToRawData + imageSectionHeaders[headerNo].SizeOfRawData); } if (cbDebug != 0) { if (this.Is32BitHeader) { if (imageSectionHeaders[headerNo].VirtualAddress <= optionalHeader32.Debug.VirtualAddress && ((imageSectionHeaders[headerNo].VirtualAddress + imageSectionHeaders[headerNo].SizeOfRawData) > optionalHeader32.Debug.VirtualAddress)) { offDebug = optionalHeader32.Debug.VirtualAddress - imageSectionHeaders[headerNo].VirtualAddress + imageSectionHeaders[headerNo].PointerToRawData; } } else { if (imageSectionHeaders[headerNo].VirtualAddress <= optionalHeader64.Debug.VirtualAddress && ((imageSectionHeaders[headerNo].VirtualAddress + imageSectionHeaders[headerNo].SizeOfRawData) > optionalHeader64.Debug.VirtualAddress)) { offDebug = optionalHeader64.Debug.VirtualAddress - imageSectionHeaders[headerNo].VirtualAddress + imageSectionHeaders[headerNo].PointerToRawData; } } } } stream.Seek(offDebug, SeekOrigin.Begin); while (cbDebug >= Marshal.SizeOf(typeof(IMAGE_DEBUG_DIRECTORY))) { if (loopexit == 0) { imageDebugDirectory = FromBinaryReader <IMAGE_DEBUG_DIRECTORY>(reader); long seekPosition = stream.Position; if (imageDebugDirectory.Type == 0x2) { stream.Seek(imageDebugDirectory.PointerToRawData, SeekOrigin.Begin); DebugInfo = FromBinaryReader <IMAGE_DEBUG_DIRECTORY_RAW>(reader); loopexit = 1; //Downloading logic for .NET native images if (new string(DebugInfo.name).Contains(".ni.")) { stream.Seek(seekPosition, SeekOrigin.Begin); loopexit = 0; } } if ((imageDebugDirectory.PointerToRawData != 0) && (imageDebugDirectory.SizeOfData != 0) && (cbFromHeader < (long) (imageDebugDirectory.PointerToRawData + imageDebugDirectory.SizeOfData))) { cbFromHeader = (long) (imageDebugDirectory.PointerToRawData + imageDebugDirectory.SizeOfData); } } cbDebug -= (uint)Marshal.SizeOf(typeof(IMAGE_DEBUG_DIRECTORY)); } if (loopexit != 0) { _pdbName = new string(DebugInfo.name); _pdbName = _pdbName.Remove(_pdbName.IndexOf("\0")); _pdbage = DebugInfo.age.ToString("X"); _debugGUID = DebugInfo.guid; } } }