|
| 1 | +/* |
| 2 | +InjectModuleInitializer |
| 3 | +
|
| 4 | +Command line program to inject a module initializer into a .NET assembly. |
| 5 | +
|
| 6 | +Copyright (C) 2009-2016 Einar Egilsson |
| 7 | +http://einaregilsson.com/module-initializers-in-csharp/ |
| 8 | +
|
| 9 | +This program is licensed under the MIT license: http://opensource.org/licenses/MIT |
| 10 | + */ |
| 11 | +using System; |
| 12 | +using System.Diagnostics; |
| 13 | +using System.IO; |
| 14 | +using System.Reflection; |
| 15 | +using Mono.Cecil; |
| 16 | +using Mono.Cecil.Cil; |
| 17 | +using Mono.Cecil.Pdb; |
| 18 | +using Mono.Collections.Generic; |
| 19 | +using MethodAttributes = Mono.Cecil.MethodAttributes; |
| 20 | + |
| 21 | +namespace EinarEgilsson.Utilities.InjectModuleInitializer |
| 22 | +{ |
| 23 | + public class InjectionException : Exception |
| 24 | + { |
| 25 | + public InjectionException(string msg) : base(msg) { } |
| 26 | + } |
| 27 | + |
| 28 | + internal class Injector |
| 29 | + { |
| 30 | + public const string DefaultInitializerClassName = "ModuleInitializer"; |
| 31 | + public const string DefaultInitializerMethodName = "Run"; |
| 32 | + |
| 33 | + private AssemblyDefinition Assembly { get; set; } |
| 34 | + |
| 35 | + private string PdbFile(string assemblyFile) |
| 36 | + { |
| 37 | + Debug.Assert(assemblyFile != null); |
| 38 | + string path = Path.ChangeExtension(assemblyFile, ".pdb"); |
| 39 | + if (File.Exists(path)) |
| 40 | + { |
| 41 | + return path; |
| 42 | + } |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public void Inject(string assemblyFile, string moduleInitializer=null, string keyfile=null) |
| 48 | + { |
| 49 | + try |
| 50 | + { |
| 51 | + if (!File.Exists(assemblyFile)) |
| 52 | + { |
| 53 | + throw new InjectionException(Errors.AssemblyDoesNotExist(assemblyFile)); |
| 54 | + } |
| 55 | + if (keyfile != null && !File.Exists(keyfile)) |
| 56 | + { |
| 57 | + throw new InjectionException(Errors.KeyFileDoesNotExist(keyfile)); |
| 58 | + } |
| 59 | + ReadAssembly(assemblyFile); |
| 60 | + MethodReference callee = GetModuleInitializer(moduleInitializer); |
| 61 | + InjectInitializer(callee); |
| 62 | + |
| 63 | + WriteAssembly(assemblyFile, keyfile); |
| 64 | + } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + throw new InjectionException(ex.Message); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void InjectInitializer(MethodReference callee) |
| 72 | + { |
| 73 | + Debug.Assert(Assembly != null); |
| 74 | + TypeReference voidRef = Assembly.MainModule.Import(callee.ReturnType); |
| 75 | + const MethodAttributes attributes = MethodAttributes.Static |
| 76 | + | MethodAttributes.SpecialName |
| 77 | + | MethodAttributes.RTSpecialName; |
| 78 | + var cctor = new MethodDefinition(".cctor", attributes, voidRef); |
| 79 | + ILProcessor il = cctor.Body.GetILProcessor(); |
| 80 | + il.Append(il.Create(OpCodes.Call, callee)); |
| 81 | + il.Append(il.Create(OpCodes.Ret)); |
| 82 | + |
| 83 | + TypeDefinition moduleClass = Find(Assembly.MainModule.Types, t => t.Name == "<Module>"); |
| 84 | + moduleClass.Methods.Add(cctor); |
| 85 | + |
| 86 | + Debug.Assert(moduleClass != null, "Found no module class!"); |
| 87 | + } |
| 88 | + |
| 89 | + public void WriteAssembly(string assemblyFile, string keyfile) |
| 90 | + { |
| 91 | + Debug.Assert(Assembly != null); |
| 92 | + var writeParams = new WriterParameters(); |
| 93 | + if (PdbFile(assemblyFile) != null) |
| 94 | + { |
| 95 | + writeParams.WriteSymbols = true; |
| 96 | + writeParams.SymbolWriterProvider = new PdbWriterProvider(); |
| 97 | + } |
| 98 | + if (keyfile != null) |
| 99 | + { |
| 100 | + writeParams.StrongNameKeyPair = new StrongNameKeyPair(File.ReadAllBytes(keyfile)); |
| 101 | + } |
| 102 | + Assembly.Write(assemblyFile, writeParams); |
| 103 | + } |
| 104 | + |
| 105 | + public void ReadAssembly(string assemblyFile) |
| 106 | + { |
| 107 | + Debug.Assert(Assembly == null); |
| 108 | + |
| 109 | + var resolver = new DefaultAssemblyResolver(); |
| 110 | + resolver.AddSearchDirectory(Path.GetDirectoryName(assemblyFile)); |
| 111 | + |
| 112 | + var readParams = new ReaderParameters(ReadingMode.Immediate) |
| 113 | + { |
| 114 | + AssemblyResolver = resolver |
| 115 | + }; |
| 116 | + |
| 117 | + if (PdbFile(assemblyFile) != null) |
| 118 | + { |
| 119 | + readParams.ReadSymbols = true; |
| 120 | + readParams.SymbolReaderProvider = new PdbReaderProvider(); |
| 121 | + } |
| 122 | + Assembly = AssemblyDefinition.ReadAssembly(assemblyFile, readParams); |
| 123 | + } |
| 124 | + |
| 125 | + public MethodReference GetModuleInitializer(string moduleInitializer = null) |
| 126 | + { |
| 127 | + Debug.Assert(Assembly != null); |
| 128 | + ModuleDefinition module = Assembly.MainModule; |
| 129 | + string methodName; |
| 130 | + TypeDefinition moduleInitializerClass; |
| 131 | + if (string.IsNullOrEmpty(moduleInitializer)) |
| 132 | + { |
| 133 | + methodName = DefaultInitializerMethodName; |
| 134 | + moduleInitializerClass = Find(module.Types, t => t.Name == DefaultInitializerClassName); |
| 135 | + if (moduleInitializerClass == null) |
| 136 | + { |
| 137 | + return null; |
| 138 | + } |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + if (!moduleInitializer.Contains("::")) |
| 143 | + { |
| 144 | + return null; |
| 145 | + } |
| 146 | + string typeName = moduleInitializer.Substring(0, moduleInitializer.IndexOf("::", StringComparison.Ordinal)); |
| 147 | + methodName = moduleInitializer.Substring(typeName.Length + 2); |
| 148 | + moduleInitializerClass = Find(module.Types, t => t.FullName == typeName); |
| 149 | + if (moduleInitializerClass == null) |
| 150 | + { |
| 151 | + return null; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + MethodDefinition callee = Find(moduleInitializerClass.Methods, m => m.Name == methodName); |
| 156 | + if (callee == null) |
| 157 | + { |
| 158 | + throw new InjectionException(Errors.MethodNameDoesNotExist(moduleInitializerClass.FullName, methodName)); |
| 159 | + } |
| 160 | + if (callee.Parameters.Count > 0) |
| 161 | + { |
| 162 | + throw new InjectionException(Errors.ModuleInitializerMayNotHaveParameters()); |
| 163 | + } |
| 164 | + if (callee.IsPrivate || callee.IsFamily) |
| 165 | + { |
| 166 | + throw new InjectionException(Errors.ModuleInitializerMayNotBePrivate()); |
| 167 | + } |
| 168 | + if (!callee.ReturnType.FullName.Equals(typeof(void).FullName)) //Don't compare the types themselves, they might be from different CLR versions. |
| 169 | + { |
| 170 | + throw new InjectionException(Errors.ModuleInitializerMustBeVoid()); |
| 171 | + } |
| 172 | + if (!callee.IsStatic) |
| 173 | + { |
| 174 | + throw new InjectionException(Errors.ModuleInitializerMustBeStatic()); |
| 175 | + } |
| 176 | + return callee; |
| 177 | + } |
| 178 | + |
| 179 | + //No LINQ, since we want to target 2.0 |
| 180 | + private static T Find<T>(Collection<T> objects, Predicate<T> condition) where T:class |
| 181 | + { |
| 182 | + foreach (T obj in objects) |
| 183 | + { |
| 184 | + if (condition(obj)) |
| 185 | + { |
| 186 | + return obj; |
| 187 | + } |
| 188 | + } |
| 189 | + return null; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments