From 18e8c94430aa1cfab7e3fdf9926fd0b18ecd0941 Mon Sep 17 00:00:00 2001 From: Frederik Carlier Date: Sat, 26 Nov 2022 20:57:46 +0100 Subject: [PATCH] MachLinkEditData: Implement .ToString(), keep track of parents Make debugging a bit easier by: - Keeping track of the parent of `MachLinkEditData` objects - Implement a `.ToString()` which returns the start and end of the range, as well as the length --- Melanzana.MachO/LoadCommands/MachDyldInfo.cs | 5 +++++ Melanzana.MachO/LoadCommands/MachSection.cs | 3 +++ Melanzana.MachO/LoadCommands/MachSymbolTable.cs | 2 ++ Melanzana.MachO/LoadCommands/MachTwoLevelHints.cs | 1 + Melanzana.MachO/MachLinkEditData.cs | 4 ++++ 5 files changed, 15 insertions(+) diff --git a/Melanzana.MachO/LoadCommands/MachDyldInfo.cs b/Melanzana.MachO/LoadCommands/MachDyldInfo.cs index 12fb8ff..24f3d42 100644 --- a/Melanzana.MachO/LoadCommands/MachDyldInfo.cs +++ b/Melanzana.MachO/LoadCommands/MachDyldInfo.cs @@ -29,10 +29,15 @@ public MachDyldInfo( ArgumentNullException.ThrowIfNull(exportData); RebaseData = rebaseData; + RebaseData.Parent = this; BindData = bindData; + BindData.Parent = this; WeakBindData = weakBindData; + WeakBindData.Parent = this; LazyBindData = lazyBindData; + LazyBindData.Parent = this; ExportData = exportData; + ExportData.Parent = this; } public MachLinkEditData RebaseData { get; private init; } diff --git a/Melanzana.MachO/LoadCommands/MachSection.cs b/Melanzana.MachO/LoadCommands/MachSection.cs index ff3b55e..4dabc94 100644 --- a/Melanzana.MachO/LoadCommands/MachSection.cs +++ b/Melanzana.MachO/LoadCommands/MachSection.cs @@ -37,6 +37,9 @@ internal MachSection( this.SectionName = sectionName; this.dataStream = stream; this.relocationData = relocationData; + + if(this.relocationData != null) + this.relocationData.Parent = this; } /// diff --git a/Melanzana.MachO/LoadCommands/MachSymbolTable.cs b/Melanzana.MachO/LoadCommands/MachSymbolTable.cs index eaf4301..43f0007 100644 --- a/Melanzana.MachO/LoadCommands/MachSymbolTable.cs +++ b/Melanzana.MachO/LoadCommands/MachSymbolTable.cs @@ -31,7 +31,9 @@ internal MachSymbolTable( this.objectFile = objectFile; this.symbolTableData = symbolTableData; + this.symbolTableData.Parent = this; this.stringTableData = stringTableData; + this.stringTableData.Parent = this; // Create a section map now since the section indexes may change later sectionMap = new Dictionary(); diff --git a/Melanzana.MachO/LoadCommands/MachTwoLevelHints.cs b/Melanzana.MachO/LoadCommands/MachTwoLevelHints.cs index 2575f5f..7f15f94 100644 --- a/Melanzana.MachO/LoadCommands/MachTwoLevelHints.cs +++ b/Melanzana.MachO/LoadCommands/MachTwoLevelHints.cs @@ -7,6 +7,7 @@ public class MachTwoLevelHints : MachLoadCommand public MachTwoLevelHints(MachObjectFile objectFile, MachLinkEditData data) { Data = data; + Data.Parent = this; } public uint FileOffset => Data.FileOffset; diff --git a/Melanzana.MachO/MachLinkEditData.cs b/Melanzana.MachO/MachLinkEditData.cs index fc3e8d1..e319afb 100644 --- a/Melanzana.MachO/MachLinkEditData.cs +++ b/Melanzana.MachO/MachLinkEditData.cs @@ -26,6 +26,8 @@ internal MachLinkEditData(Stream objectStream, uint offset, uint size) this.FileOffset = offset; } + public object? Parent { get; set; } + public uint FileOffset { get; set; } public ulong Size @@ -63,5 +65,7 @@ public Stream GetWriteStream() dataStream = new UnclosableMemoryStream(); return dataStream; } + + public override string ToString() => $"Link Edit Data: 0x{FileOffset:X}-0x{FileOffset + Size:X} (0x{Size:X})"; } }