Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve original PDB path in Debug header #947

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Mono.Cecil.Cil/PortablePdb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,23 @@ public void Write ()
}
}

string GetPdbPath ()
{
if (!module.HasDebugHeader)
return string.Empty;

var debugHeader = module.GetDebugHeader ();
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 +358,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
30 changes: 30 additions & 0 deletions Test/Mono.Cecil.Tests/PortablePdbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,5 +1148,35 @@ 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);

// Sanity check that the CodeView debug directory entry has the expected signature:
// https://github.com/dotnet/runtime/blob/main/docs/design/specs/PE-COFF.md#codeview-debug-directory-entry-type-2
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.