diff options
author | Vitek Karas <vitek.karas@microsoft.com> | 2021-11-11 20:40:42 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-11 20:40:42 +0300 |
commit | ff616bf90e3aff2292a0b1536f7af13cd8810276 (patch) | |
tree | 1c3734dea16e9796a8aeb198ea659ae9435e31d9 /Mono.Cecil | |
parent | 3bef7fc7b7910f826790ebfeccb381b76a650571 (diff) |
Fix deterministic MVID and add PdbChecksum (#31)
Diffstat (limited to 'Mono.Cecil')
-rw-r--r-- | Mono.Cecil/AssemblyWriter.cs | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index c83f997..e60ab1d 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -118,13 +118,17 @@ namespace Mono.Cecil { metadata.SetSymbolWriter (symbol_writer); BuildMetadata (module, metadata); - if (parameters.DeterministicMvid) - metadata.ComputeDeterministicMvid (); + ImageDebugHeader debugHeader = null; + if (symbol_writer != null) + debugHeader = symbol_writer.GetDebugHeader (); - var writer = ImageWriter.CreateWriter (module, metadata, stream); + var writer = ImageWriter.CreateWriter (module, metadata, stream, debugHeader); stream.value.SetLength (0); writer.WriteImage (); + if (parameters.DeterministicMvid) + ComputeDeterministicMvid (writer, module); + if (parameters.HasStrongNameKey) CryptoService.StrongName (stream.value, writer, parameters); } @@ -156,6 +160,26 @@ namespace Mono.Cecil { return symbol_writer_provider.GetSymbolWriter (module, fq_name); } + + static void ComputeDeterministicMvid (ImageWriter writer, ModuleDefinition module) + { + long previousPosition = writer.BaseStream.Position; + writer.BaseStream.Seek(0, SeekOrigin.Begin); + + // The hash should be computed with the MVID set to all zeroes + // which it is - we explicitly write all zeroes GUID into the heap + // as the MVID. + // Same goes for strong name signature, which also already in the image but all zeroes right now. + Guid guid = CryptoService.ComputeGuid (CryptoService.ComputeHash (writer.BaseStream)); + + // The MVID GUID is always the first GUID in the GUID heap + writer.MoveToRVA (TextSegment.GuidHeap); + writer.WriteBytes (guid.ToByteArray ()); + writer.Flush (); + module.Mvid = guid; + + writer.BaseStream.Seek(previousPosition, SeekOrigin.Begin); + } } abstract class MetadataTable { @@ -2642,25 +2666,6 @@ namespace Mono.Cecil { method_debug_information_table.rows [rid - 1].Col2 = GetBlobIndex (signature); } - - public void ComputeDeterministicMvid () - { - var guid = CryptoService.ComputeGuid (CryptoService.ComputeHash ( - data, - resources, - string_heap, - user_string_heap, - blob_heap, - table_heap, - code)); - - var position = guid_heap.position; - guid_heap.position = 0; - guid_heap.WriteBytes (guid.ToByteArray ()); - guid_heap.position = position; - - module.Mvid = guid; - } } sealed class SignatureWriter : ByteBuffer { |