From 7912156e9f5d9f936ba1e3bf74b991d379bdd6d5 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Fri, 23 Apr 2021 15:56:44 -0700 Subject: [PATCH] Add ability to provide custom stream file names --- Mono.Cecil.Cil/PortablePdb.cs | 32 ++++++++++++----------- Mono.Cecil.Cil/Symbols.cs | 14 +++++----- Mono.Cecil.PE/ImageReader.cs | 4 +-- Mono.Cecil/AssemblyReader.cs | 2 +- Mono.Cecil/AssemblyWriter.cs | 6 ++--- Mono.Cecil/ModuleDefinition.cs | 31 ++++++++++++++++++++-- Test/Mono.Cecil.Tests/PortablePdbTests.cs | 4 +-- symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs | 2 +- symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs | 2 +- symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs | 16 ++++++------ 10 files changed, 71 insertions(+), 42 deletions(-) diff --git a/Mono.Cecil.Cil/PortablePdb.cs b/Mono.Cecil.Cil/PortablePdb.cs index 744c274bb..0c4bcdf46 100644 --- a/Mono.Cecil.Cil/PortablePdb.cs +++ b/Mono.Cecil.Cil/PortablePdb.cs @@ -29,12 +29,12 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) return GetSymbolReader (module, Disposable.Owned (file as Stream), file.Name); } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); - return GetSymbolReader (module, Disposable.NotOwned (symbolStream), symbolStream.GetFileName ()); + return GetSymbolReader (module, Disposable.NotOwned (symbolStream), symbolStream.GetFileName (symbolFileName)); } ISymbolReader GetSymbolReader (ModuleDefinition module, Disposable symbolStream, string fileName) @@ -163,7 +163,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) throw new InvalidOperationException (); return new EmbeddedPortablePdbReader ( - (PortablePdbReader) new PortablePdbReaderProvider ().GetSymbolReader (module, GetPortablePdbStream (entry))); + (PortablePdbReader) new PortablePdbReaderProvider ().GetSymbolReader (module, GetPortablePdbStream (entry), fileName)); } static Stream GetPortablePdbStream (ImageDebugHeaderEntry entry) @@ -180,7 +180,7 @@ static Stream GetPortablePdbStream (ImageDebugHeaderEntry entry) return decompressed_stream; } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { throw new NotSupportedException (); } @@ -227,23 +227,23 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) Mixin.CheckFileName (fileName); var file = File.OpenWrite (Mixin.GetPdbFileName (fileName)); - return GetSymbolWriter (module, Disposable.Owned (file as Stream)); + return GetSymbolWriter (module, Disposable.Owned (file as Stream), file.Name); } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); - return GetSymbolWriter (module, Disposable.NotOwned (symbolStream)); + return GetSymbolWriter (module, Disposable.NotOwned (symbolStream), symbolFileName); } - ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable stream) + ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable stream, string fileName) { var metadata = new MetadataBuilder (module, this); var writer = ImageWriter.CreateDebugWriter (module, metadata, stream); - return new PortablePdbWriter (metadata, module, writer); + return new PortablePdbWriter (metadata, module, writer, fileName); } } @@ -252,15 +252,17 @@ public sealed class PortablePdbWriter : ISymbolWriter { readonly MetadataBuilder pdb_metadata; readonly ModuleDefinition module; readonly ImageWriter writer; + readonly string file_name; MetadataBuilder module_metadata; bool IsEmbedded { get { return writer == null; } } - internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module) + internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, string fileName) { this.pdb_metadata = pdb_metadata; this.module = module; + this.file_name = fileName; this.module_metadata = module.metadata_builder; @@ -270,8 +272,8 @@ internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition modul pdb_metadata.AddCustomDebugInformations (module); } - internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer) - : this (pdb_metadata, module) + internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer, string fileName) + : this (pdb_metadata, module, fileName) { this.writer = writer; } @@ -301,7 +303,7 @@ public ImageDebugHeader GetDebugHeader () // PDB Age buffer.WriteUInt32 (1); // PDB Path - var fileName = writer.BaseStream.GetFileName (); + var fileName = writer.BaseStream.GetFileName (file_name); if (string.IsNullOrEmpty (fileName)) { fileName = module.Assembly.Name.Name + ".pdb"; } @@ -401,11 +403,11 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) Mixin.CheckFileName (fileName); var stream = new MemoryStream (); - var pdb_writer = (PortablePdbWriter) new PortablePdbWriterProvider ().GetSymbolWriter (module, stream); + var pdb_writer = (PortablePdbWriter) new PortablePdbWriterProvider ().GetSymbolWriter (module, stream, fileName); return new EmbeddedPortablePdbWriter (stream, pdb_writer); } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { throw new NotSupportedException (); } diff --git a/Mono.Cecil.Cil/Symbols.cs b/Mono.Cecil.Cil/Symbols.cs index 4cf435dc2..603a4e0a0 100644 --- a/Mono.Cecil.Cil/Symbols.cs +++ b/Mono.Cecil.Cil/Symbols.cs @@ -808,7 +808,7 @@ public interface ISymbolReader : IDisposable { public interface ISymbolReaderProvider { ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName); - ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream); + ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName); } #if !NET_CORE @@ -903,7 +903,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) return null; } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { if (module.Image.HasDebugTables ()) return null; @@ -927,7 +927,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre symbolStream.Position = position; if (intHeader == portablePdbHeader) { - return new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream); + return new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream, symbolFileName); } const string nativePdbHeader = "Microsoft C/C++ MSF 7.00"; @@ -945,7 +945,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre if (isNativePdb) { try { - return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, symbolStream); + return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, symbolStream, symbolFileName); } catch (Exception) { // We might not include support for native pdbs. } @@ -958,7 +958,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre if (longHeader == mdbHeader) { try { - return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, symbolStream); + return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, symbolStream, symbolFileName); } catch (Exception) { // We might not include support for mdbs. } @@ -1070,7 +1070,7 @@ public interface ISymbolWriter : IDisposable { public interface ISymbolWriterProvider { ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName); - ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream); + ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName); } public class DefaultSymbolWriterProvider : ISymbolWriterProvider { @@ -1087,7 +1087,7 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) return reader.GetWriterProvider ().GetSymbolWriter (module, fileName); } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { throw new NotSupportedException (); } diff --git a/Mono.Cecil.PE/ImageReader.cs b/Mono.Cecil.PE/ImageReader.cs index 84fdeb660..4cc96800d 100644 --- a/Mono.Cecil.PE/ImageReader.cs +++ b/Mono.Cecil.PE/ImageReader.cs @@ -763,7 +763,7 @@ public static Image ReadImage (Disposable stream, string file_name) reader.ReadImage (); return reader.image; } catch (EndOfStreamException e) { - throw new BadImageFormatException (stream.value.GetFileName (), e); + throw new BadImageFormatException (file_name, e); } } @@ -786,7 +786,7 @@ public static Image ReadPortablePdb (Disposable stream, string file_name reader.ReadMetadata (); return reader.image; } catch (EndOfStreamException e) { - throw new BadImageFormatException (stream.value.GetFileName (), e); + throw new BadImageFormatException (file_name, e); } } } diff --git a/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/AssemblyReader.cs index d5b34b7fe..c655cd8b5 100644 --- a/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/AssemblyReader.cs @@ -100,7 +100,7 @@ static void ReadSymbols (ModuleDefinition module, ReaderParameters parameters) module.SymbolReaderProvider = symbol_reader_provider; var reader = parameters.SymbolStream != null - ? symbol_reader_provider.GetSymbolReader (module, parameters.SymbolStream) + ? symbol_reader_provider.GetSymbolReader (module, parameters.SymbolStream, parameters.SymbolFileName) : symbol_reader_provider.GetSymbolReader (module, module.FileName); if (reader != null) { diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index 200410ee3..332dd50ea 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -95,7 +95,7 @@ static void Write (ModuleDefinition module, Disposable stream, WriterPar module.symbol_reader.Dispose (); var name = module.assembly != null && module.kind != ModuleKind.NetModule ? module.assembly.Name : null; - var fq_name = stream.value.GetFileName (); + var fq_name = stream.value.GetFileName (parameters.FileName); var timestamp = parameters.Timestamp ?? module.timestamp; var symbol_writer_provider = parameters.SymbolWriterProvider; @@ -152,7 +152,7 @@ static ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fq_name, I return null; if (parameters.SymbolStream != null) - return symbol_writer_provider.GetSymbolWriter (module, parameters.SymbolStream); + return symbol_writer_provider.GetSymbolWriter (module, parameters.SymbolStream, parameters.SymbolFileName); return symbol_writer_provider.GetSymbolWriter (module, fq_name); } @@ -956,7 +956,7 @@ public void SetSymbolWriter (ISymbolWriter writer) symbol_writer = writer; if (symbol_writer == null && module.HasImage && module.Image.HasDebugTables ()) - symbol_writer = new PortablePdbWriter (this, module); + symbol_writer = new PortablePdbWriter (this, module, ""); } TextMap CreateTextMap () diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 8fb35a540..1cc46565d 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -28,12 +28,14 @@ public enum ReadingMode { public sealed class ReaderParameters { + string file_name; ReadingMode reading_mode; internal IAssemblyResolver assembly_resolver; internal IMetadataResolver metadata_resolver; internal IMetadataImporterProvider metadata_importer_provider; internal IReflectionImporterProvider reflection_importer_provider; Stream symbol_stream; + string symbol_file_name; ISymbolReaderProvider symbol_reader_provider; bool read_symbols; bool throw_symbols_mismatch; @@ -41,6 +43,11 @@ public sealed class ReaderParameters { bool in_memory; bool read_write; + public string FileName { + get { return file_name; } + set { file_name = value; } + } + public ReadingMode ReadingMode { get { return reading_mode; } set { reading_mode = value; } @@ -76,6 +83,11 @@ public Stream SymbolStream { set { symbol_stream = value; } } + public string SymbolFileName { + get { return symbol_file_name; } + set { symbol_file_name = value; } + } + public ISymbolReaderProvider SymbolReaderProvider { get { return symbol_reader_provider; } set { symbol_reader_provider = value; } @@ -179,14 +191,21 @@ static TargetRuntime GetCurrentRuntime () public sealed class WriterParameters { + string file_name; uint? timestamp; Stream symbol_stream; + string symbol_file_name; ISymbolWriterProvider symbol_writer_provider; bool write_symbols; byte [] key_blob; string key_container; SR.StrongNameKeyPair key_pair; + public string FileName { + get { return file_name; } + set { file_name = value; } + } + public uint? Timestamp { get { return timestamp; } set { timestamp = value; } @@ -197,6 +216,11 @@ public Stream SymbolStream { set { symbol_stream = value; } } + public string SymbolFileName { + get { return symbol_file_name; } + set { symbol_file_name = value; } + } + public ISymbolWriterProvider SymbolWriterProvider { get { return symbol_writer_provider; } set { symbol_writer_provider = value; } @@ -1131,7 +1155,7 @@ public static ModuleDefinition ReadModule (Stream stream, ReaderParameters param Mixin.CheckStream (stream); Mixin.CheckReadSeek (stream); - return ReadModule (Disposable.NotOwned (stream), stream.GetFileName (), parameters); + return ReadModule (Disposable.NotOwned (stream), stream.GetFileName (parameters.FileName), parameters); } static ModuleDefinition ReadModule (Disposable stream, string fileName, ReaderParameters parameters) @@ -1287,8 +1311,11 @@ public static bool HasImage (this ModuleDefinition self) return self != null && self.HasImage; } - public static string GetFileName (this Stream self) + public static string GetFileName (this Stream self, string file_name) { + if (!string.IsNullOrEmpty (file_name)) + return file_name; + var file_stream = self as FileStream; if (file_stream == null) return string.Empty; diff --git a/Test/Mono.Cecil.Tests/PortablePdbTests.cs b/Test/Mono.Cecil.Tests/PortablePdbTests.cs index b0c1f3238..4031740be 100644 --- a/Test/Mono.Cecil.Tests/PortablePdbTests.cs +++ b/Test/Mono.Cecil.Tests/PortablePdbTests.cs @@ -608,9 +608,9 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) return new SymbolWriter (writer_provider.GetSymbolWriter (module, fileName)); } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { - return new SymbolWriter (writer_provider.GetSymbolWriter (module, symbolStream)); + return new SymbolWriter (writer_provider.GetSymbolWriter (module, symbolStream, symbolFileName)); } } diff --git a/symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs b/symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs index 3e2dd18e4..8b9bb376d 100644 --- a/symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs +++ b/symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs @@ -28,7 +28,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) return new MdbReader (module, MonoSymbolFile.ReadSymbolFile (Mixin.GetMdbFileName (fileName))); } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); diff --git a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs index 9b565187d..d33ec6d4b 100644 --- a/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs +++ b/symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs @@ -28,7 +28,7 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName) return new MdbWriter (module, fileName); } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { throw new NotImplementedException (); } diff --git a/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs b/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs index 2b513cd11..b1bf6b722 100644 --- a/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs +++ b/symbols/pdb/Mono.Cecil.Pdb/PdbHelper.cs @@ -25,7 +25,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) return new NativePdbReader (Disposable.Owned (File.OpenRead (Mixin.GetPdbFileName (fileName)) as Stream)); } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); @@ -54,15 +54,15 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) : new NativePdbReaderProvider ().GetSymbolReader (module, fileName); } - public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) + public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); Mixin.CheckReadSeek (symbolStream); return Mixin.IsPortablePdb (symbolStream) - ? new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream) - : new NativePdbReaderProvider ().GetSymbolReader (module, symbolStream); + ? new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream, symbolFileName) + : new NativePdbReaderProvider ().GetSymbolReader (module, symbolStream, symbolFileName); } } @@ -88,7 +88,7 @@ static SymWriter CreateWriter (ModuleDefinition module, string pdb) return writer; } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { throw new NotImplementedException (); } @@ -112,16 +112,16 @@ static bool HasPortablePdbSymbols (ModuleDefinition module) return module.symbol_reader != null && module.symbol_reader is PortablePdbReader; } - public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream) + public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName) { Mixin.CheckModule (module); Mixin.CheckStream (symbolStream); Mixin.CheckReadSeek (symbolStream); if (HasPortablePdbSymbols (module)) - return new PortablePdbWriterProvider ().GetSymbolWriter (module, symbolStream); + return new PortablePdbWriterProvider ().GetSymbolWriter (module, symbolStream, symbolFileName); - return new NativePdbWriterProvider ().GetSymbolWriter (module, symbolStream); + return new NativePdbWriterProvider ().GetSymbolWriter (module, symbolStream, symbolFileName); } } }