From d3f2fe29bc4f7cd88dd3dd50fe7ce9b17b54a205 Mon Sep 17 00:00:00 2001 From: lateralusX Date: Thu, 30 Jul 2026 18:28:16 +0200 Subject: [PATCH] Fix IsPortableCodeView when rewriting assembly. Current rewrite of the debug directory didn't copy over the Minor/MajorVersion, making portable PDBs not work as intended (IsPortableCodeView returned false). It also reset the timestamp, incorrect in the portable PDB scenario as well, since that field is not a real timestamp but the low 4 bytes of the PDB content id (part of the PDB identity). Fix makes sure to copy Minor/MajorVersion from old, as well as the timestamp when dealing with portable PDBs. For full PDBs all works as before: Minor/MajorVersion will be 0 and the timestamp is regenerated. Towards dotnet/runtime#118700 --- .../src/Mono.Cecil.Binary/CopyImageVisitor.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.CilStrip.Sources/src/Mono.Cecil.Binary/CopyImageVisitor.cs b/src/Microsoft.DotNet.CilStrip.Sources/src/Mono.Cecil.Binary/CopyImageVisitor.cs index 2fd25a44cd..2f1d4eb3f5 100644 --- a/src/Microsoft.DotNet.CilStrip.Sources/src/Mono.Cecil.Binary/CopyImageVisitor.cs +++ b/src/Microsoft.DotNet.CilStrip.Sources/src/Mono.Cecil.Binary/CopyImageVisitor.cs @@ -53,8 +53,16 @@ public override void VisitDebugHeader (DebugHeader dbgHeader) dbgHeader.Age = old.Age; dbgHeader.Characteristics = old.Characteristics; dbgHeader.FileName = old.FileName; + dbgHeader.MajorVersion = old.MajorVersion; + dbgHeader.MinorVersion = old.MinorVersion; dbgHeader.Signature = old.Signature; - dbgHeader.TimeDateStamp = ImageInitializer.TimeDateStampFromEpoch(); + // Portable PDBs (CodeView MinorVersion magic 0x504D) encode a content-derived stamp that + // is part of the PDB identity. Preserve it so the rewritten image keeps matching its + // unmodified PDB (IsPortableCodeView stays true and symbolication still works). Other + // debug entry kinds retain the historical behavior of a freshly generated stamp. + dbgHeader.TimeDateStamp = old.MinorVersion == 0x504D + ? old.TimeDateStamp + : ImageInitializer.TimeDateStampFromEpoch(); dbgHeader.Type = old.Type; }