Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright (C) 2008-2011 Jeroen Frijters
Copyright (C) 2008-2015 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@@ -81,7 +81,8 @@ namespace IKVM.Reflection.Writer
moduleBuilder.ApplyUnmanagedExports(imageFileMachine);
moduleBuilder.FixupMethodBodyTokens();
moduleBuilder.ModuleTable.Add(0, moduleBuilder.Strings.Add(moduleBuilder.moduleName), moduleBuilder.Guids.Add(moduleBuilder.ModuleVersionId), 0, 0);
int moduleVersionIdIndex = moduleBuilder.Guids.Add(moduleBuilder.GetModuleVersionIdOrEmpty());
moduleBuilder.ModuleTable.Add(0, moduleBuilder.Strings.Add(moduleBuilder.moduleName), moduleVersionIdIndex, 0, 0);
if (moduleBuilder.UserStrings.IsEmpty)
{
@@ -212,6 +213,9 @@ namespace IKVM.Reflection.Writer
writer.Headers.OptionalHeader.DataDirectory[6].Size = code.DebugDirectoryLength;
}
// Set the PE File timestamp
writer.Headers.FileHeader.TimeDateStamp = moduleBuilder.GetTimeDateStamp();
// we need to start by computing the number of sections, because code.PointerToRawData depends on that
writer.Headers.FileHeader.NumberOfSections = 2;
@@ -309,7 +313,8 @@ namespace IKVM.Reflection.Writer
}
stream.Seek(text.PointerToRawData, SeekOrigin.Begin);
code.Write(mw, sdata.VirtualAddress);
int guidHeapOffset;
code.Write(mw, sdata.VirtualAddress, out guidHeapOffset);
if (sdata.SizeOfRawData != 0)
{
@@ -332,17 +337,28 @@ namespace IKVM.Reflection.Writer
// file alignment
stream.SetLength(reloc.PointerToRawData + reloc.SizeOfRawData);
// if we don't have a guid, generate one based on the contents of the assembly
if (moduleBuilder.universe.Deterministic && moduleBuilder.GetModuleVersionIdOrEmpty() == Guid.Empty)
{
Guid guid = GenerateModuleVersionId(stream);
stream.Position = guidHeapOffset + (moduleVersionIdIndex - 1) * 16;
stream.Write(guid.ToByteArray(), 0, 16);
moduleBuilder.__SetModuleVersionId(guid);
}
// do the strong naming
if (keyPair != null)
{
StrongName(stream, keyPair, writer.HeaderSize, text.PointerToRawData, code.StrongNameSignatureRVA - text.VirtualAddress + text.PointerToRawData, code.StrongNameSignatureLength);
}
#if !NO_SYMBOL_WRITER
if (moduleBuilder.symbolWriter != null)
{
moduleBuilder.WriteSymbolTokenMap();
moduleBuilder.symbolWriter.Close();
}
#endif
}
private static int ComputeStrongNameSignatureLength(byte[] publicKey)
@@ -424,5 +440,24 @@ namespace IKVM.Reflection.Writer
length -= read;
}
}
private static Guid GenerateModuleVersionId(Stream stream)
{
SHA1Managed hash = new SHA1Managed();
using (CryptoStream cs = new CryptoStream(Stream.Null, hash, CryptoStreamMode.Write))
{
stream.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[8192];
HashChunk(stream, cs, buf, (int)stream.Length);
}
byte[] bytes = new byte[16];
Buffer.BlockCopy(hash.Hash, 0, bytes, 0, bytes.Length);
// set GUID type to "version 4" (random)
bytes[7] &= 0x0F;
bytes[7] |= 0x40;
bytes[8] &= 0x3F;
bytes[8] |= 0x80;
return new Guid(bytes);
}
}
}