public CorDebugAssembly(CorDebugProcess process, string name, Pdbx.PdbxFile pdbxFile, uint idx) { m_process = process; m_appDomain = null; m_name = name; m_pdbxFile = pdbxFile; m_pdbxAssembly = (pdbxFile != null) ? pdbxFile.Assembly : null; m_htTokenCLRToPdbx = new Hashtable(); m_htTokenTinyCLRToPdbx = new Hashtable(); m_idx = idx; m_primaryAssembly = null; m_isFrameworkAssembly = false; if (m_pdbxAssembly != null) { if (!string.IsNullOrEmpty(pdbxFile.PdbxPath)) { string pth = pdbxFile.PdbxPath.ToLower(); if (pth.Contains(Path.DirectorySeparatorChar + "buildoutput" + Path.DirectorySeparatorChar)) { #region V4_1_FRAMEWORK_ASSEMBLIES List <string> frameworkAssemblies = new List <string> { "mfdpwsclient", "mfdpwsdevice", "mfdpwsextensions", "mfwsstack", "microsoft.spot.graphics", "microsoft.spot.hardware", "microsoft.spot.hardware.serialport", "microsoft.spot.hardware.usb", "microsoft.spot.ink", "microsoft.spot.io", "microsoft.spot.native", "microsoft.spot.net", "microsoft.spot.net.security", "microsoft.spot.time", "microsoft.spot.tinycore", "microsoft.spot.touch", "mscorlib", "system.http", "system.io", "system.net.security", "system", "system.xml.legacy", "system.xml", }; #endregion // V4_1_FRAMEWORK_ASSEMBLIES m_isFrameworkAssembly = (frameworkAssemblies.Contains(name.ToLower())); } else { m_isFrameworkAssembly = pdbxFile.PdbxPath.ToLower().Contains(Path.PathSeparator + ".net micro framework" + Path.PathSeparator); } } m_pdbxAssembly.CorDebugAssembly = this; foreach (Pdbx.Class c in m_pdbxAssembly.Classes) { AddTokenToHashtables(c.Token, c); foreach (Pdbx.Field field in c.Fields) { AddTokenToHashtables(field.Token, field); } foreach (Pdbx.Method method in c.Methods) { AddTokenToHashtables(method.Token, method); } } if (File.Exists(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".dll"))) { MetaData = ModuleDefinition.ReadModule(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".dll")); } else if (File.Exists(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".exe"))) { MetaData = ModuleDefinition.ReadModule(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".exe")); } if (MetaData != null && File.Exists(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".exe.mdb"))) { DebugData = new Mono.Cecil.Mdb.MdbReaderProvider().GetSymbolReader(MetaData, Path.ChangeExtension(m_pdbxFile.PdbxPath, ".exe")); MetaData.ReadSymbols(DebugData); } else if (MetaData != null && File.Exists(Path.ChangeExtension(m_pdbxFile.PdbxPath, ".dll.mdb"))) { DebugData = new Mono.Cecil.Mdb.MdbReaderProvider().GetSymbolReader(MetaData, Path.ChangeExtension(m_pdbxFile.PdbxPath, ".dll")); MetaData.ReadSymbols(DebugData); } } }
void Rewrite(string file, string keyfile, IEnumerable<string> options) { // Specify assembly read and write parameters // We want to keep a valid symbols file (pdb or mdb) var read_params = new ReaderParameters(); var write_params = new WriterParameters(); var pdb = Path.ChangeExtension(file, "pdb"); var mdb = Path.ChangeExtension(file, "mdb"); ISymbolReaderProvider provider = null; if (File.Exists(pdb)) { provider = new Mono.Cecil.Pdb.PdbReaderProvider(); } else if (File.Exists(mdb)) { provider = new Mono.Cecil.Mdb.MdbReaderProvider(); } read_params.SymbolReaderProvider = provider; read_params.ReadSymbols = true; write_params.WriteSymbols = true; if (!String.IsNullOrEmpty(keyfile) && File.Exists(keyfile)) { keyfile = Path.GetFullPath(keyfile); var fs = new FileStream(keyfile, FileMode.Open); var keypair = new System.Reflection.StrongNameKeyPair(fs); fs.Close(); write_params.StrongNameKeyPair = keypair; } else { Console.Error.WriteLine("No keyfile specified or keyfile missing."); } // Load assembly and process all modules var assembly = AssemblyDefinition.ReadAssembly(file, read_params); var rewritten = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "RewrittenAttribute"); if (rewritten == null) { foreach (var module in assembly.Modules) { foreach (var reference in module.AssemblyReferences) { var resolved = module.AssemblyResolver.Resolve(reference); if (reference.Name == "mscorlib") { mscorlib = resolved; } } } if (mscorlib == null) { Console.Error.WriteLine("Failed to locate mscorlib"); return; } TypeMarshal = mscorlib.MainModule.GetType("System.Runtime.InteropServices.Marshal"); TypeStringBuilder = mscorlib.MainModule.GetType("System.Text.StringBuilder"); TypeVoid = mscorlib.MainModule.GetType("System.Void"); TypeIntPtr = mscorlib.MainModule.GetType("System.IntPtr"); TypeInt32 = mscorlib.MainModule.GetType("System.Int32"); TypeBindingsBase = assembly.Modules.Select(m => m.GetType("OpenTK.BindingsBase")).First(); foreach (var module in assembly.Modules) { foreach (var type in module.Types) { Rewrite(type, options); } } } else { Console.Error.WriteLine("Error: assembly has already been rewritten"); } // Save rewritten assembly assembly.Write(file, write_params); }
static void Main(string[] args) { string input = null; string output = null; string keyPairContainer = null; var options = new Mono.Options.OptionSet() { { "i|input=", i => input = i }, { "o|output:", o => output = o }, { "k|key:", k => keyPairContainer = k }, }; try { var extra = options.Parse(args); var pdb = Path.ChangeExtension(input, "pdb"); var mdb = Path.ChangeExtension(input, "mdb"); ISymbolReaderProvider provider = null; if (File.Exists(pdb)) { provider = new Mono.Cecil.Pdb.PdbReaderProvider(); } else if (File.Exists(mdb)) { provider = new Mono.Cecil.Mdb.MdbReaderProvider(); } var assemblyResolver = new DefaultAssemblyResolver(); assemblyResolver.AddSearchDirectory(System.IO.Path.GetDirectoryName(input)); var readParameters = new ReaderParameters() { AssemblyResolver = assemblyResolver, ReadingMode = ReadingMode.Immediate, ReadSymbols = true, SymbolReaderProvider = provider, }; var writeParameters = new WriterParameters() { WriteSymbols = true, }; if (keyPairContainer != null) { writeParameters.StrongNameKeyPair = new System.Reflection.StrongNameKeyPair(keyPairContainer); } if (output == null) { output = input; } Console.WriteLine("CilTK Rewriter"); Console.WriteLine("Reading assembly from {0}", input); Console.WriteLine("Writing assembly to {0}", output); InputAssembly = input; var assembly = Mono.Cecil.AssemblyDefinition.ReadAssembly(input, readParameters); var labelReplace = new LabelReplacer(); labelReplace.Visit(assembly); var infoReplace = new InfoReplacer(); infoReplace.Visit(assembly); var cilReplacer = new CilReplacer(labelReplace); cilReplacer.Visit(assembly); //remove references to Silk foreach (var module in assembly.Modules) { int index = -1; for (int i = 0; i < module.AssemblyReferences.Count; ++i) { if (module.AssemblyReferences[i].Name == "Silk") { index = i; break; } } if (index != -1) { module.AssemblyReferences.RemoveAt(index); } } //write to a temp file then copy to output to assist with debugging var temp = System.IO.Path.GetTempFileName(); assembly.Write(temp, writeParameters); System.IO.File.Copy(temp, output, true); } catch (Mono.Options.OptionException) { options.WriteOptionDescriptions(Console.Out); System.Environment.Exit(1); } }
void Rewrite(string file, string keyfile, IEnumerable <string> options) { // Specify assembly read and write parameters // We want to keep a valid symbols file (pdb or mdb) var read_params = new ReaderParameters(); var write_params = new WriterParameters(); var pdb = Path.ChangeExtension(file, "pdb"); var mdb = Path.ChangeExtension(file, "mdb"); ISymbolReaderProvider provider = null; if (File.Exists(pdb)) { provider = new Mono.Cecil.Pdb.PdbReaderProvider(); } else if (File.Exists(mdb)) { provider = new Mono.Cecil.Mdb.MdbReaderProvider(); } read_params.SymbolReaderProvider = provider; read_params.ReadSymbols = true; write_params.WriteSymbols = true; if (!String.IsNullOrEmpty(keyfile) && File.Exists(keyfile)) { keyfile = Path.GetFullPath(keyfile); var fs = new FileStream(keyfile, FileMode.Open); var keypair = new System.Reflection.StrongNameKeyPair(fs); fs.Close(); write_params.StrongNameKeyPair = keypair; } else { Console.Error.WriteLine("No keyfile specified or keyfile missing."); } // Load assembly and process all modules var assembly = AssemblyDefinition.ReadAssembly(file, read_params); var rewritten = assembly.CustomAttributes.FirstOrDefault(a => a.AttributeType.Name == "RewrittenAttribute"); if (rewritten == null) { foreach (var module in assembly.Modules) { foreach (var reference in module.AssemblyReferences) { var resolved = module.AssemblyResolver.Resolve(reference); if (reference.Name == "mscorlib") { mscorlib = resolved; } } } if (mscorlib == null) { Console.Error.WriteLine("Failed to locate mscorlib"); return; } TypeMarshal = mscorlib.MainModule.GetType("System.Runtime.InteropServices.Marshal"); TypeStringArray = mscorlib.MainModule.GetType("System.String").MakeArrayType().Resolve(); TypeStringBuilder = mscorlib.MainModule.GetType("System.Text.StringBuilder"); TypeVoid = mscorlib.MainModule.GetType("System.Void"); TypeIntPtr = mscorlib.MainModule.GetType("System.IntPtr"); TypeBindingsBase = assembly.Modules.Select(m => m.GetType("OpenTK.BindingsBase")).First(); foreach (var module in assembly.Modules) { foreach (var type in module.Types) { Rewrite(type, options); } } } else { Console.Error.WriteLine("Error: assembly has already been rewritten"); } // Save rewritten assembly assembly.Write(file, write_params); }
public CorDebugAssembly (CorDebugProcess process, string name, Pdbx.PdbxFile pdbxFile, uint idx) { m_process = process; m_appDomain = null; m_name = name; m_pdbxFile = pdbxFile; m_pdbxAssembly = (pdbxFile != null) ? pdbxFile.Assembly : null; m_htTokenCLRToPdbx = new Hashtable (); m_htTokenTinyCLRToPdbx = new Hashtable (); m_idx = idx; m_primaryAssembly = null; m_isFrameworkAssembly = false; if (m_pdbxAssembly != null) { if (!string.IsNullOrEmpty (pdbxFile.PdbxPath)) { string pth = pdbxFile.PdbxPath.ToLower (); if (pth.Contains (@"\buildoutput\")) { #region V4_1_FRAMEWORK_ASSEMBLIES List<string> frameworkAssemblies = new List<string> { "mfdpwsclient", "mfdpwsdevice", "mfdpwsextensions", "mfwsstack", "microsoft.spot.graphics", "microsoft.spot.hardware", "microsoft.spot.hardware.serialport", "microsoft.spot.hardware.usb", "microsoft.spot.ink", "microsoft.spot.io", "microsoft.spot.native", "microsoft.spot.net", "microsoft.spot.net.security", "microsoft.spot.time", "microsoft.spot.tinycore", "microsoft.spot.touch", "mscorlib", "system.http", "system.io", "system.net.security", "system", "system.xml.legacy", "system.xml", }; #endregion // V4_1_FRAMEWORK_ASSEMBLIES m_isFrameworkAssembly = (frameworkAssemblies.Contains (name.ToLower ())); } else { m_isFrameworkAssembly = pdbxFile.PdbxPath.ToLower().Contains(Path.PathSeparator + ".net micro framework" + Path.PathSeparator); } } m_pdbxAssembly.CorDebugAssembly = this; foreach (Pdbx.Class c in m_pdbxAssembly.Classes) { AddTokenToHashtables (c.Token, c); foreach (Pdbx.Field field in c.Fields) { AddTokenToHashtables (field.Token, field); } foreach (Pdbx.Method method in c.Methods) { AddTokenToHashtables (method.Token, method); } } if (File.Exists (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".dll"))) MetaData = ModuleDefinition.ReadModule (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".dll")); else if (File.Exists (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".exe"))) MetaData = ModuleDefinition.ReadModule (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".exe")); if (MetaData != null && File.Exists (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".pdb"))) { DebugData = new Mono.Cecil.Pdb.PdbReaderProvider ().GetSymbolReader (MetaData, Path.ChangeExtension (m_pdbxFile.PdbxPath, ".pdb")); MetaData.ReadSymbols (DebugData); } else if (MetaData != null && File.Exists (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".exe.mdb"))) { DebugData = new Mono.Cecil.Mdb.MdbReaderProvider ().GetSymbolReader (MetaData, Path.ChangeExtension (m_pdbxFile.PdbxPath, ".exe")); MetaData.ReadSymbols (DebugData); } else if (MetaData != null && File.Exists (Path.ChangeExtension (m_pdbxFile.PdbxPath, ".dll.mdb"))) { DebugData = new Mono.Cecil.Mdb.MdbReaderProvider ().GetSymbolReader (MetaData, Path.ChangeExtension (m_pdbxFile.PdbxPath, ".dll")); MetaData.ReadSymbols (DebugData); } } }