Skip to content
This repository
Browse code

Add support for reading and writing custom dll characteristics

  • Loading branch information...
commit 4e0cec75888fb06b81b68e9cd59fe3a47944cca5 1 parent b876fcf
Jb Evain authored
1  Mono.Cecil.PE/Image.cs
@@ -41,6 +41,7 @@ sealed class Image {
41 41
 		public ModuleKind Kind;
42 42
 		public TargetRuntime Runtime;
43 43
 		public TargetArchitecture Architecture;
  44
+		public ModuleCharacteristics Characteristics;
44 45
 		public string FileName;
45 46
 
46 47
 		public Section [] Sections;
10  Mono.Cecil.PE/ImageReader.cs
@@ -99,13 +99,14 @@ void ReadImage ()
99 99
 			// Characteristics		2
100 100
 			ushort characteristics = ReadUInt16 ();
101 101
 
102  
-			ushort subsystem;
103  
-			ReadOptionalHeaders (out subsystem);
  102
+			ushort subsystem, dll_characteristics;
  103
+			ReadOptionalHeaders (out subsystem, out dll_characteristics);
104 104
 			ReadSections (sections);
105 105
 			ReadCLIHeader ();
106 106
 			ReadMetadata ();
107 107
 
108 108
 			image.Kind = GetModuleKind (characteristics, subsystem);
  109
+			image.Characteristics = (ModuleCharacteristics) dll_characteristics;
109 110
 		}
110 111
 
111 112
 		TargetArchitecture ReadArchitecture ()
@@ -136,7 +137,7 @@ static ModuleKind GetModuleKind (ushort characteristics, ushort subsystem)
136 137
 			return ModuleKind.Console;
137 138
 		}
138 139
 
139  
-		void ReadOptionalHeaders (out ushort subsystem)
  140
+		void ReadOptionalHeaders (out ushort subsystem, out ushort dll_characteristics)
140 141
 		{
141 142
 			// - PEOptionalHeader
142 143
 			//   - StandardFieldsHeader
@@ -176,6 +177,7 @@ void ReadOptionalHeaders (out ushort subsystem)
176 177
 			subsystem = ReadUInt16 ();
177 178
 
178 179
 			// DLLFlags				2
  180
+			dll_characteristics = ReadUInt16 ();
179 181
 			// StackReserveSize		4 || 8
180 182
 			// StackCommitSize		4 || 8
181 183
 			// HeapReserveSize		4 || 8
@@ -192,7 +194,7 @@ void ReadOptionalHeaders (out ushort subsystem)
192 194
 			// CertificateTable		8
193 195
 			// BaseRelocationTable	8
194 196
 
195  
-			Advance (pe64 ? 90 : 74);
  197
+			Advance (pe64 ? 88 : 72);
196 198
 
197 199
 			// Debug				8
198 200
 			image.Debug = ReadDataDirectory ();
2  Mono.Cecil.PE/ImageWriter.cs
@@ -267,7 +267,7 @@ void WriteOptionalHeaders ()
267 267
 
268 268
 			WriteUInt32 (0);	// Checksum
269 269
 			WriteUInt16 (GetSubSystem ());	// SubSystem
270  
-			WriteUInt16 (0x8540);	// DLLFlags
  270
+			WriteUInt16 ((ushort) module.Characteristics);	// DLLFlags
271 271
 
272 272
 			const ulong stack_reserve = 0x100000;
273 273
 			const ulong stack_commit = 0x1000;
8  Mono.Cecil/ModuleDefinition.cs
@@ -208,6 +208,7 @@ public sealed class ModuleDefinition : ModuleReference, ICustomAttributeProvider
208 208
 		TargetRuntime runtime;
209 209
 		TargetArchitecture architecture;
210 210
 		ModuleAttributes attributes;
  211
+		ModuleCharacteristics characteristics;
211 212
 		Guid mvid;
212 213
 
213 214
 		internal AssemblyDefinition assembly;
@@ -247,6 +248,11 @@ public sealed class ModuleDefinition : ModuleReference, ICustomAttributeProvider
247 248
 			set { attributes = value; }
248 249
 		}
249 250
 
  251
+		public ModuleCharacteristics Characteristics {
  252
+			get { return characteristics; }
  253
+			set { characteristics = value; }
  254
+		}
  255
+
250 256
 		public string FullyQualifiedName {
251 257
 			get { return fq_name; }
252 258
 		}
@@ -442,6 +448,7 @@ internal ModuleDefinition (Image image)
442 448
 			this.runtime = image.Runtime;
443 449
 			this.architecture = image.Architecture;
444 450
 			this.attributes = image.Attributes;
  451
+			this.characteristics = image.Characteristics;
445 452
 			this.fq_name = image.FileName;
446 453
 
447 454
 			this.reader = new MetadataReader (this);
@@ -866,6 +873,7 @@ public static ModuleDefinition CreateModule (string name, ModuleParameters param
866 873
 				architecture = parameters.Architecture,
867 874
 				mvid = Guid.NewGuid (),
868 875
 				Attributes = ModuleAttributes.ILOnly,
  876
+				Characteristics = (ModuleCharacteristics) 0x8540,
869 877
 			};
870 878
 
871 879
 			if (parameters.AssemblyResolver != null)
10  Mono.Cecil/ModuleKind.cs
@@ -51,4 +51,14 @@ public enum ModuleAttributes {
51 51
 		StrongNameSigned = 8,
52 52
 		Preferred32Bit = 0x00020000,
53 53
 	}
  54
+
  55
+	[Flags]
  56
+	public enum ModuleCharacteristics {
  57
+		HighEntropyVA = 0x0020,
  58
+		DynamicBase = 0x0040,
  59
+		NoSEH = 0x0400,
  60
+		NXCompat = 0x0100,
  61
+		AppContainer = 0x1000,
  62
+		TerminalServerAware = 0x8000,
  63
+	}
54 64
 }
6  Test/Mono.Cecil.Tests/ImageReadTests.cs
@@ -134,5 +134,11 @@ public void WindowsPhoneNonSignedAssembly (ModuleDefinition module)
134 134
 			Assert.AreEqual (0, module.Image.StrongName.VirtualAddress);
135 135
 			Assert.AreEqual (0, module.Image.StrongName.Size);
136 136
 		}
  137
+
  138
+		[TestModule ("metro.exe", Verify = false)]
  139
+		public void MetroAssembly(ModuleDefinition module)
  140
+		{
  141
+			Assert.AreEqual(ModuleCharacteristics.AppContainer, module.Characteristics & ModuleCharacteristics.AppContainer);
  142
+		}
137 143
 	}
138 144
 }
BIN  Test/Resources/assemblies/metro.exe
Binary file not shown

0 notes on commit 4e0cec7

Please sign in to comment.
Something went wrong with that request. Please try again.