From 49248ddb98473808f7f32e0e98b2633b7e3d15fa Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 10 Aug 2012 16:12:51 +0200 Subject: [PATCH] Update to Mono.Cecil 0.9.5-95-g9f3c0f8. This fixes a crash when opening a portable library project targeting .NET 4.5. (jbevain/cecil#101) --- .../Mono.Cecil/Mono.Cecil.PE/Image.cs | 1 + .../Mono.Cecil/Mono.Cecil.PE/ImageReader.cs | 10 +++--- .../Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs | 25 ++++++++------- .../Mono.Cecil/AssemblyNameReference.cs | 11 ++++--- .../Mono.Cecil/Mono.Cecil/AssemblyReader.cs | 8 +++-- .../Mono.Cecil/FunctionPointerType.cs | 1 + .../Mono.Cecil/Mono.Cecil/GenericParameter.cs | 1 + .../Mono.Cecil/Mono.Cecil/MetadataSystem.cs | 5 +-- .../Mono.Cecil/Mono.Cecil/ModuleDefinition.cs | 8 +++++ .../Mono.Cecil/Mono.Cecil/ModuleKind.cs | 10 ++++++ .../Mono.Cecil/Mono.Cecil/TypeParser.cs | 2 +- .../Mono.Cecil/Mono.Cecil/TypeReference.cs | 9 ++++++ .../Mono.Cecil/TypeSpecification.cs | 5 +-- .../Mono.Cecil/Mono.Cecil/TypeSystem.cs | 31 +++++++++++++++++-- 14 files changed, 94 insertions(+), 33 deletions(-) diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/Image.cs b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/Image.cs index e5d8075251..a11cf1c404 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/Image.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/Image.cs @@ -41,6 +41,7 @@ namespace Mono.Cecil.PE { public ModuleKind Kind; public TargetRuntime Runtime; public TargetArchitecture Architecture; + public ModuleCharacteristics Characteristics; public string FileName; public Section [] Sections; diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs index b320169192..c96c1db318 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageReader.cs @@ -99,13 +99,14 @@ namespace Mono.Cecil.PE { // Characteristics 2 ushort characteristics = ReadUInt16 (); - ushort subsystem; - ReadOptionalHeaders (out subsystem); + ushort subsystem, dll_characteristics; + ReadOptionalHeaders (out subsystem, out dll_characteristics); ReadSections (sections); ReadCLIHeader (); ReadMetadata (); image.Kind = GetModuleKind (characteristics, subsystem); + image.Characteristics = (ModuleCharacteristics) dll_characteristics; } TargetArchitecture ReadArchitecture () @@ -136,7 +137,7 @@ namespace Mono.Cecil.PE { return ModuleKind.Console; } - void ReadOptionalHeaders (out ushort subsystem) + void ReadOptionalHeaders (out ushort subsystem, out ushort dll_characteristics) { // - PEOptionalHeader // - StandardFieldsHeader @@ -176,6 +177,7 @@ namespace Mono.Cecil.PE { subsystem = ReadUInt16 (); // DLLFlags 2 + dll_characteristics = ReadUInt16 (); // StackReserveSize 4 || 8 // StackCommitSize 4 || 8 // HeapReserveSize 4 || 8 @@ -192,7 +194,7 @@ namespace Mono.Cecil.PE { // CertificateTable 8 // BaseRelocationTable 8 - Advance (pe64 ? 90 : 74); + Advance (pe64 ? 88 : 72); // Debug 8 image.Debug = ReadDataDirectory (); diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs index bea308c919..c62427c6a4 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil.PE/ImageWriter.cs @@ -235,7 +235,7 @@ namespace Mono.Cecil.PE { WriteByte (8); // LMajor WriteByte (0); // LMinor WriteUInt32 (text.SizeOfRawData); // CodeSize - WriteUInt32 (reloc != null ? reloc.SizeOfRawData : 0 + WriteUInt32 ((reloc != null ? reloc.SizeOfRawData : 0) + (rsrc != null ? rsrc.SizeOfRawData : 0)); // InitializedDataSize WriteUInt32 (0); // UninitializedDataSize @@ -267,7 +267,7 @@ namespace Mono.Cecil.PE { WriteUInt32 (0); // Checksum WriteUInt16 (GetSubSystem ()); // SubSystem - WriteUInt16 (0x8540); // DLLFlags + WriteUInt16 ((ushort) module.Characteristics); // DLLFlags const ulong stack_reserve = 0x100000; const ulong stack_commit = 0x1000; @@ -754,17 +754,18 @@ namespace Mono.Cecil.PE { return 0; var public_key = module.Assembly.Name.PublicKey; + if (public_key.IsNullOrEmpty ()) + return 0; - if (public_key != null) { - // in fx 2.0 the key may be from 384 to 16384 bits - // so we must calculate the signature size based on - // the size of the public key (minus the 32 byte header) - int size = public_key.Length; - if (size > 32) - return size - 32; - // note: size == 16 for the ECMA "key" which is replaced - // by the runtime with a 1024 bits key (128 bytes) - } + // in fx 2.0 the key may be from 384 to 16384 bits + // so we must calculate the signature size based on + // the size of the public key (minus the 32 byte header) + int size = public_key.Length; + if (size > 32) + return size - 32; + + // note: size == 16 for the ECMA "key" which is replaced + // by the runtime with a 1024 bits key (128 bytes) return 128; // default strongname signature size } diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs index 3da978057b..4f58fe2d15 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyNameReference.cs @@ -98,7 +98,7 @@ namespace Mono.Cecil { } public byte [] PublicKey { - get { return public_key; } + get { return public_key ?? Empty.Array; } set { public_key = value; HasPublicKey = !public_key.IsNullOrEmpty (); @@ -116,7 +116,7 @@ namespace Mono.Cecil { Array.Copy (hash, (hash.Length - 8), public_key_token, 0, 8); Array.Reverse (public_key_token, 0, 8); } - return public_key_token; + return public_key_token ?? Empty.Array; } set { public_key_token = value; @@ -175,9 +175,10 @@ namespace Mono.Cecil { builder.Append (sep); builder.Append ("PublicKeyToken="); - if (this.PublicKeyToken != null && public_key_token.Length > 0) { - for (int i = 0 ; i < public_key_token.Length ; i++) { - builder.Append (public_key_token [i].ToString ("x2")); + var pk_token = PublicKeyToken; + if (!pk_token.IsNullOrEmpty () && pk_token.Length > 0) { + for (int i = 0 ; i < pk_token.Length ; i++) { + builder.Append (pk_token [i].ToString ("x2")); } } else builder.Append ("null"); diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyReader.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyReader.cs index 9aa5b13658..89d300f771 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyReader.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/AssemblyReader.cs @@ -778,8 +778,12 @@ namespace Mono.Cecil { var nested_types = new MemberDefinitionCollection (type, mapping.Length); - for (int i = 0; i < mapping.Length; i++) - nested_types.Add (GetTypeDefinition (mapping [i])); + for (int i = 0; i < mapping.Length; i++) { + var nested_type = GetTypeDefinition (mapping [i]); + + if (nested_type != null) + nested_types.Add (nested_type); + } metadata.RemoveNestedTypeMapping (type); diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs index db80f76215..756d31f6ff 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/FunctionPointerType.cs @@ -85,6 +85,7 @@ namespace Mono.Cecil { public override IMetadataScope Scope { get { return function.ReturnType.Scope; } + set { throw new InvalidOperationException (); } } public override bool IsFunctionPointer { diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/GenericParameter.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/GenericParameter.cs index d694e296d7..f2b33d88da 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/GenericParameter.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/GenericParameter.cs @@ -107,6 +107,7 @@ namespace Mono.Cecil { ? ((MethodReference) owner).DeclaringType.Scope : ((TypeReference) owner).Scope; } + set { throw new InvalidOperationException (); } } public override ModuleDefinition Module { diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/MetadataSystem.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/MetadataSystem.cs index 693202b6ba..72b04f2858 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/MetadataSystem.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/MetadataSystem.cs @@ -106,7 +106,7 @@ namespace Mono.Cecil { return; var scope = type.scope; - if (scope == null || scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference || scope.Name != "mscorlib") + if (scope == null || scope.MetadataScopeType != MetadataScopeType.AssemblyNameReference) return; Row primitive_data; @@ -124,9 +124,6 @@ namespace Mono.Cecil { if (type.Namespace != "System") return false; - if (!type.HasImage || !type.Module.IsCorlib ()) - return false; - Row primitive_data; if (TryGetPrimitiveData (type, out primitive_data) && primitive_data.Col1.IsPrimitive ()) { etype = primitive_data.Col1; diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs index 66ac0270e0..8236dad909 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleDefinition.cs @@ -208,6 +208,7 @@ namespace Mono.Cecil { TargetRuntime runtime; TargetArchitecture architecture; ModuleAttributes attributes; + ModuleCharacteristics characteristics; Guid mvid; internal AssemblyDefinition assembly; @@ -247,6 +248,11 @@ namespace Mono.Cecil { set { attributes = value; } } + public ModuleCharacteristics Characteristics { + get { return characteristics; } + set { characteristics = value; } + } + public string FullyQualifiedName { get { return fq_name; } } @@ -442,6 +448,7 @@ namespace Mono.Cecil { this.runtime = image.Runtime; this.architecture = image.Architecture; this.attributes = image.Attributes; + this.characteristics = image.Characteristics; this.fq_name = image.FileName; this.reader = new MetadataReader (this); @@ -866,6 +873,7 @@ namespace Mono.Cecil { architecture = parameters.Architecture, mvid = Guid.NewGuid (), Attributes = ModuleAttributes.ILOnly, + Characteristics = (ModuleCharacteristics) 0x8540, }; if (parameters.AssemblyResolver != null) diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleKind.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleKind.cs index bbbad72801..c29da887d8 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleKind.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/ModuleKind.cs @@ -51,4 +51,14 @@ namespace Mono.Cecil { StrongNameSigned = 8, Preferred32Bit = 0x00020000, } + + [Flags] + public enum ModuleCharacteristics { + HighEntropyVA = 0x0020, + DynamicBase = 0x0040, + NoSEH = 0x0400, + NXCompat = 0x0100, + AppContainer = 0x1000, + TerminalServerAware = 0x8000, + } } diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeParser.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeParser.cs index 06dc935a58..3e633ea8d9 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeParser.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeParser.cs @@ -272,7 +272,7 @@ namespace Mono.Cecil { public static TypeReference ParseType (ModuleDefinition module, string fullname) { - if (fullname == null) + if (string.IsNullOrEmpty (fullname)) return null; var parser = new TypeParser (fullname); diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeReference.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeReference.cs index ff28c636fe..f811b4f018 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeReference.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeReference.cs @@ -147,6 +147,15 @@ namespace Mono.Cecil { return scope; } + set { + var declaring_type = this.DeclaringType; + if (declaring_type != null) { + declaring_type.Scope = value; + return; + } + + scope = value; + } } public bool IsNested { diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSpecification.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSpecification.cs index aa9b653f07..75651be59c 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSpecification.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSpecification.cs @@ -42,16 +42,17 @@ namespace Mono.Cecil { public override string Name { get { return element_type.Name; } - set { throw new NotSupportedException (); } + set { throw new InvalidOperationException (); } } public override string Namespace { get { return element_type.Namespace; } - set { throw new NotSupportedException (); } + set { throw new InvalidOperationException (); } } public override IMetadataScope Scope { get { return element_type.Scope; } + set { throw new InvalidOperationException (); } } public override ModuleDefinition Module { diff --git a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSystem.cs b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSystem.cs index 60ccae4e5d..63f6aea287 100644 --- a/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSystem.cs +++ b/src/Libraries/Mono.Cecil/Mono.Cecil/TypeSystem.cs @@ -34,14 +34,23 @@ namespace Mono.Cecil { public abstract class TypeSystem { - sealed class CorlibTypeSystem : TypeSystem { + sealed class CoreTypeSystem : TypeSystem { - public CorlibTypeSystem (ModuleDefinition module) + public CoreTypeSystem (ModuleDefinition module) : base (module) { } internal override TypeReference LookupType (string @namespace, string name) + { + var type = LookupTypeDefinition (@namespace, name) ?? LookupTypeForwarded (@namespace, name); + if (type != null) + return type; + + throw new NotSupportedException (); + } + + TypeReference LookupTypeDefinition (string @namespace, string name) { var metadata = module.MetadataSystem; if (metadata.Types == null) @@ -64,6 +73,22 @@ namespace Mono.Cecil { }); } + TypeReference LookupTypeForwarded (string @namespace, string name) + { + if (!module.HasExportedTypes) + return null; + + var exported_types = module.ExportedTypes; + for (int i = 0; i < exported_types.Count; i++) { + var exported_type = exported_types [i]; + + if (exported_type.Name == name && exported_type.Namespace == @namespace) + return exported_type.CreateReference (); + } + + return null; + } + static void Initialize (object obj) { } @@ -159,7 +184,7 @@ namespace Mono.Cecil { internal static TypeSystem CreateTypeSystem (ModuleDefinition module) { if (module.IsCorlib ()) - return new CorlibTypeSystem (module); + return new CoreTypeSystem (module); return new CommonTypeSystem (module); }