Skip to content

Commit

Permalink
Preserve original PDB path in dll
Browse files Browse the repository at this point in the history
  • Loading branch information
sbomer committed May 31, 2024
1 parent 1da2145 commit 0f75387
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
19 changes: 18 additions & 1 deletion Mono.Cecil.Cil/PortablePdb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,20 @@ public void Write ()
}
}

string GetPdbPath ()
{
var debugHeader = module.Image.DebugHeader;
foreach (var entry in debugHeader.Entries) {
var data = entry.Data;
// Pdb path is NUL-terminated path at offset 24.
// https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md#codeview-debug-directory-entry-type-2
if (entry.Directory.Type == ImageDebugType.CodeView && data.Length >= 25)
return System.Text.Encoding.UTF8.GetString (data, 24, data.Length - 25);
}

return string.Empty;
}

public ImageDebugHeader GetDebugHeader ()
{
if (IsEmbedded)
Expand All @@ -341,7 +355,10 @@ public ImageDebugHeader GetDebugHeader ()
// PDB Age
buffer.WriteUInt32 (1);
// PDB Path
var fileName = writer.BaseStream.GetFileName ();
var fileName = GetPdbPath ();
if (string.IsNullOrEmpty (fileName)) {
fileName = writer.BaseStream.GetFileName ();
}
if (string.IsNullOrEmpty (fileName)) {
fileName = module.Assembly.Name.Name + ".pdb";
}
Expand Down
28 changes: 28 additions & 0 deletions Test/Mono.Cecil.Tests/PortablePdbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,5 +1148,33 @@ static void GetCodeViewPdbId (ModuleDefinition module, out byte[] pdbId)
buffer.WriteInt32 (cv.Directory.TimeDateStamp);
pdbId = buffer.buffer;
}

[Test]
public void WritePortablePdbPath ()
{
const string resource = "PdbPathLib.dll";
string destination = Path.GetTempFileName ();

using (var module = GetResourceModule (resource, new ReaderParameters { ReadSymbols = true })) {
module.Write (destination, new WriterParameters { WriteSymbols = true });
}

using (var module = ModuleDefinition.ReadModule (destination, new ReaderParameters { ReadSymbols = true })) {
GetCodeViewPdbPath (module, out string pdbPath);

Assert.AreEqual ("/_/artifacts/obj/PdbPathLib/release/PdbPathLib.pdb", pdbPath);
}
}

static void GetCodeViewPdbPath (ModuleDefinition module, out string pdbPath)
{
var header = module.GetDebugHeader ();
var cv = Mixin.GetCodeViewEntry (header);
Assert.IsNotNull (cv);

CollectionAssert.AreEqual (new byte [] { 0x52, 0x53, 0x44, 0x53 }, cv.Data.Take (4));

pdbPath = Encoding.UTF8.GetString (cv.Data, 24, cv.Data.Length - 25);
}
}
}
Binary file added Test/Resources/assemblies/PdbPathLib.dll
Binary file not shown.
Binary file added Test/Resources/assemblies/PdbPathLib.pdb
Binary file not shown.

0 comments on commit 0f75387

Please sign in to comment.