Add debug section (nw)

This commit is contained in:
Matt Nadareski
2022-03-14 15:27:42 -07:00
parent 0a486c2195
commit 0fa6673d21
4 changed files with 230 additions and 0 deletions

View File

@@ -2,6 +2,83 @@ using System;
namespace BurnOutSharp.ExecutableType.Microsoft
{
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-type
public enum DebugType : uint
{
/// <summary>
/// An unknown value that is ignored by all tools.
/// </summary>
IMAGE_DEBUG_TYPE_UNKNOWN = 0,
/// <summary>
/// The COFF debug information (line numbers, symbol table, and string table).
/// This type of debug information is also pointed to by fields in the file headers.
/// </summary>
IMAGE_DEBUG_TYPE_COFF = 1,
/// <summary>
/// The Visual C++ debug information.
/// </summary>
IMAGE_DEBUG_TYPE_CODEVIEW = 2,
/// <summary>
/// The frame pointer omission (FPO) information.
/// This information tells the debugger how to interpret nonstandard stack frames,
/// which use the EBP register for a purpose other than as a frame pointer.
/// </summary>
IMAGE_DEBUG_TYPE_FPO = 3,
/// <summary>
/// The location of DBG file.
/// </summary>
IMAGE_DEBUG_TYPE_MISC = 4,
/// <summary>
/// A copy of .pdata section.
/// </summary>
IMAGE_DEBUG_TYPE_EXCEPTION = 5,
/// <summary>
/// Reserved.
/// </summary>
IMAGE_DEBUG_TYPE_FIXUP = 6,
/// <summary>
/// The mapping from an RVA in image to an RVA in source image.
/// </summary>
IMAGE_DEBUG_TYPE_OMAP_TO_SRC = 7,
/// <summary>
/// The mapping from an RVA in source image to an RVA in image.
/// </summary>
IMAGE_DEBUG_TYPE_OMAP_FROM_SRC = 8,
/// <summary>
/// Reserved for Borland.
/// </summary>
IMAGE_DEBUG_TYPE_BORLAND = 9,
/// <summary>
/// Reserved.
/// </summary>
IMAGE_DEBUG_TYPE_RESERVED10 = 10,
/// <summary>
/// Reserved.
/// </summary>
IMAGE_DEBUG_TYPE_CLSID = 11,
/// <summary>
/// PE determinism or reproducibility.
/// </summary>
IMAGE_DEBUG_TYPE_REPRO = 16,
/// <summary>
/// Extended DLL characteristics bits.
/// </summary>
IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS = 20,
}
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#dll-characteristics
[Flags]
public enum DllCharacteristics : ushort

View File

@@ -71,6 +71,13 @@ namespace BurnOutSharp.ExecutableType.Microsoft.PE
#region Tables
/// <summary>
/// The .debug section is used in object files to contain compiler-generated debug information and in image files to contain
/// all of the debug information that is generated.
/// This section describes the packaging of debug information in object and image files.
/// </summary>
public DebugSection DebugDirectory;
/// <summary>
/// The export data section, named .edata, contains information about symbols that other images can access through dynamic linking.
/// Exported symbols are generally found in DLLs, but DLLs can also import symbols.
@@ -235,6 +242,14 @@ namespace BurnOutSharp.ExecutableType.Microsoft.PE
#region Structured Tables
// // Debug Section
// var table = this.GetLastSection(".debug", true);
// if (table != null && table.VirtualSize > 0)
// {
// stream.Seek((int)table.PointerToRawData, SeekOrigin.Begin);
// this.DebugSection = DebugSection.Deserialize(stream, this.SectionTable);
// }
// // Export Table
// var table = this.GetLastSection(".edata", true);
// if (table != null && table.VirtualSize > 0)
@@ -326,6 +341,14 @@ namespace BurnOutSharp.ExecutableType.Microsoft.PE
#region Structured Tables
// // Debug Section
// var table = this.GetLastSection(".debug", true);
// if (table != null && table.VirtualSize > 0)
// {
// int tableAddress = (int)table.PointerToRawData;
// this.DebugSection = DebugSection.Deserialize(content, ref tableAddress, this.SectionTable);
// }
// // Export Table
// var table = this.GetLastSection(".edata", true);
// if (table != null && table.VirtualSize > 0)

View File

@@ -0,0 +1,45 @@
using System.IO;
using BurnOutSharp.ExecutableType.Microsoft.PE.Headers;
using BurnOutSharp.ExecutableType.Microsoft.PE.Tables;
namespace BurnOutSharp.ExecutableType.Microsoft.PE.Sections
{
/// <summary>
/// The .debug section is used in object files to contain compiler-generated debug information and in image files to contain
/// all of the debug information that is generated.
/// This section describes the packaging of debug information in object and image files.
/// </summary>
/// <remarks>https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#the-debug-section</remarks>
public class DebugSection
{
/// <summary>
/// Image files contain an optional debug directory that indicates what form of debug information is present and where it is.
/// This directory consists of an array of debug directory entries whose location and size are indicated in the image optional header.
/// </summary>
public DebugDirectory DebugDirectory;
public static DebugSection Deserialize(Stream stream, SectionHeader[] sections)
{
long originalPosition = stream.Position;
var ds = new DebugSection();
ds.DebugDirectory = DebugDirectory.Deserialize(stream);
// TODO: Read in raw debug data
return ds;
}
public static DebugSection Deserialize(byte[] content, ref int offset, SectionHeader[] sections)
{
int originalPosition = offset;
var ds = new DebugSection();
ds.DebugDirectory = DebugDirectory.Deserialize(content, ref offset);
// TODO: Read in raw debug data
return ds;
}
}
}

View File

@@ -0,0 +1,85 @@
using System.IO;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ExecutableType.Microsoft.PE.Tables
{
/// <summary>
/// Image files contain an optional debug directory that indicates what form of debug information is present and where it is.
/// This directory consists of an array of debug directory entries whose location and size are indicated in the image optional header.
/// </summary>
/// <remarks>https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#debug-directory-image-only</remarks>
public class DebugDirectory
{
/// <summary>
/// Reserved, must be 0.
/// </summary>
public uint Characteristics;
/// <summary>
/// The time and date that the debug data was created.
/// </summary>
public uint TimeDateStamp;
/// <summary>
/// The major version number of the debug data format.
/// </summary>
public ushort MajorVersion;
/// <summary>
/// The minor version number of the debug data format.
/// </summary>
public ushort MinorVersion;
/// <summary>
/// The format of debugging information. This field enables support of multiple debuggers.
/// </summary>
public DebugType DebugType;
/// <summary>
/// The size of the debug data (not including the debug directory itself).
/// </summary>
public uint SizeOfData;
/// <summary>
/// The address of the debug data when loaded, relative to the image base.
/// </summary>
public uint AddressOfRawData;
/// <summary>
/// The file pointer to the debug data.
/// </summary>
public uint PointerToRawData;
public static DebugDirectory Deserialize(Stream stream)
{
var dd = new DebugDirectory();
dd.Characteristics = stream.ReadUInt32();
dd.TimeDateStamp = stream.ReadUInt32();
dd.MajorVersion = stream.ReadUInt16();
dd.MinorVersion = stream.ReadUInt16();
dd.DebugType = (DebugType)stream.ReadUInt32();
dd.SizeOfData = stream.ReadUInt32();
dd.AddressOfRawData = stream.ReadUInt32();
dd.PointerToRawData = stream.ReadUInt32();
return dd;
}
public static DebugDirectory Deserialize(byte[] content, ref int offset)
{
var dd = new DebugDirectory();
dd.Characteristics = content.ReadUInt32(ref offset);
dd.TimeDateStamp = content.ReadUInt32(ref offset);
dd.MajorVersion = content.ReadUInt16(ref offset);
dd.MinorVersion = content.ReadUInt16(ref offset);
dd.DebugType = (DebugType)content.ReadUInt32(ref offset);
dd.SizeOfData = content.ReadUInt32(ref offset);
dd.AddressOfRawData = content.ReadUInt32(ref offset);
dd.PointerToRawData = content.ReadUInt32(ref offset);
return dd;
}
}
}