From b1220c2acd0b39a9b5f21d55f120146cdc2c6de4 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Fri, 30 Aug 2019 15:47:02 -0700 Subject: [PATCH] Write symbol files before the assembly --- Mono.Cecil.Cil/PortablePdb.cs | 27 ++++++++++++++----- Mono.Cecil.Cil/Symbols.cs | 2 ++ Mono.Cecil/AssemblyWriter.cs | 3 +++ Test/Mono.Cecil.Tests/PortablePdbTests.cs | 5 ++++ symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs | 6 ++++- symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs | 17 ++++++++---- 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Mono.Cecil.Cil/PortablePdb.cs b/Mono.Cecil.Cil/PortablePdb.cs index b5515ba91..bfd31b55e 100644 --- a/Mono.Cecil.Cil/PortablePdb.cs +++ b/Mono.Cecil.Cil/PortablePdb.cs @@ -330,6 +330,14 @@ void CheckMethodDebugInformationTable () } public void Dispose () + { + if (IsEmbedded) + return; + + writer.stream.Dispose (); + } + + public void Write () { if (IsEmbedded) return; @@ -348,7 +356,6 @@ void WritePdbFile () writer.WriteMetadata (); writer.Flush (); - writer.stream.Dispose (); } void WritePdbHeap () @@ -425,8 +432,6 @@ public ISymbolReaderProvider GetReaderProvider () public ImageDebugHeader GetDebugHeader () { - writer.Dispose (); - var directory = new ImageDebugDirectory { Type = ImageDebugType.EmbeddedPortablePdb, MajorVersion = 0x0100, @@ -450,10 +455,12 @@ public ImageDebugHeader GetDebugHeader () directory.SizeOfData = (int) data.Length; - return new ImageDebugHeader (new [] { - writer.GetDebugHeader ().Entries [0], - new ImageDebugHeaderEntry (directory, data.ToArray ()) - }); + var header = writer.GetDebugHeader (); + var entries = new ImageDebugHeaderEntry [header.Entries.Length + 1]; + Array.Copy (header.Entries, 0, entries, 0, header.Entries.Length); + entries [entries.Length - 1] = new ImageDebugHeaderEntry (directory, data.ToArray ()); + + return new ImageDebugHeader (entries); } public void Write (MethodDebugInformation info) @@ -461,8 +468,14 @@ public void Write (MethodDebugInformation info) writer.Write (info); } + public void Write () + { + writer.Write (); + } + public void Dispose () { + writer.Dispose (); } } diff --git a/Mono.Cecil.Cil/Symbols.cs b/Mono.Cecil.Cil/Symbols.cs index f1f85a0a6..535bc6022 100644 --- a/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Cecil.Cil/Symbols.cs @@ -38,6 +38,7 @@ public enum ImageDebugType { CodeView = 2, Deterministic = 16, EmbeddedPortablePdb = 17, + PdbChecksum = 19, } public sealed class ImageDebugHeader { @@ -1015,6 +1016,7 @@ public interface ISymbolWriter : IDisposable { ISymbolReaderProvider GetReaderProvider (); ImageDebugHeader GetDebugHeader (); void Write (MethodDebugInformation info); + void Write (); } public interface ISymbolWriterProvider { diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 3ea1088b3..4ab94a8d5 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -121,6 +121,9 @@ static void Write (ModuleDefinition module, Disposable stream, WriterPar if (parameters.DeterministicMvid) metadata.ComputeDeterministicMvid (); + if (symbol_writer != null) + symbol_writer.Write (); + var writer = ImageWriter.CreateWriter (module, metadata, stream); stream.value.SetLength (0); writer.WriteImage (); diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index 6e1efbc90..3b538ec74 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -632,6 +632,11 @@ public void Write (MethodDebugInformation info) symbol_writer.Write (info); } + public void Write () + { + symbol_writer.Write (); + } + public void Dispose () { symbol_writer.Dispose (); diff --git a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs index becbb4f88..8f34d95eb 100644 --- a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs +++ b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs @@ -167,11 +167,15 @@ public ImageDebugHeader GetDebugHeader () return new ImageDebugHeader (); } - public void Dispose () + public void Write () { writer.WriteSymbolFile (mvid); } + public void Dispose () + { + } + class SourceFile : ISourceFile { readonly CompileUnitEntry compilation_unit; diff --git a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs index 01b6c7a51..2e560a9f2 100644 --- a/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs +++ b/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.cs @@ -28,6 +28,9 @@ public class NativePdbWriter : ISymbolWriter { readonly Dictionary documents; readonly Dictionary import_info_to_parent; + ImageDebugDirectory debug_directory; + byte [] debug_info; + internal NativePdbWriter (ModuleDefinition module, SymWriter writer) { this.module = module; @@ -44,10 +47,7 @@ public ISymbolReaderProvider GetReaderProvider () public ImageDebugHeader GetDebugHeader () { - ImageDebugDirectory directory; - var data = writer.GetDebugInfo (out directory); - directory.TimeDateStamp = (int) module.timestamp; - return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data)); + return new ImageDebugHeader (new ImageDebugHeaderEntry (debug_directory, debug_info)); } public void Write (MethodDebugInformation info) @@ -253,14 +253,21 @@ SymDocumentWriter GetDocument (Document document) return doc_writer; } - public void Dispose () + public void Write () { var entry_point = module.EntryPoint; if (entry_point != null) writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ()); + debug_info = writer.GetDebugInfo (out debug_directory); + debug_directory.TimeDateStamp = (int) module.timestamp; + writer.Close (); } + + public void Dispose () + { + } } enum CustomMetadataType : byte {