2013-11-26 01:31:10 +01:00
|
|
|
// OpenTK.Rewrite: IL rewriter for OpenTK.dll
|
2013-11-25 00:19:54 +01:00
|
|
|
// Copyright (C) 2013 Stefanos Apostolopoulos
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
|
|
|
namespace OpenTK.Rewrite
|
|
|
|
{
|
|
|
|
// Replaces OpenTK.InteropHelper method instances
|
|
|
|
// with the s IL instructions.
|
|
|
|
class Program
|
|
|
|
{
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
if (args.Length == 0)
|
|
|
|
{
|
2013-11-25 08:53:27 +01:00
|
|
|
Console.WriteLine("Usage: rewrite [file.dll] [file.snk]");
|
2013-11-25 00:19:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var program = new Program();
|
2013-11-25 08:53:27 +01:00
|
|
|
var file = args[0];
|
|
|
|
var key = args.Length >= 2 ? args[1] : null;
|
|
|
|
program.Rewrite(file, key);
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 08:53:27 +01:00
|
|
|
void Rewrite(string file, string keyfile)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-25 08:53:27 +01:00
|
|
|
// 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();
|
2013-11-25 10:06:10 +01:00
|
|
|
#if false // Disabled because symbols file is locked during AfterBuild
|
2013-11-25 08:53:27 +01:00
|
|
|
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;
|
2013-11-25 10:06:10 +01:00
|
|
|
#endif
|
2013-11-25 08:53:27 +01:00
|
|
|
|
2013-11-25 10:06:10 +01:00
|
|
|
if (!String.IsNullOrEmpty(keyfile) && File.Exists(keyfile))
|
2013-11-25 08:53:27 +01:00
|
|
|
{
|
2013-11-25 10:06:10 +01:00
|
|
|
keyfile = Path.GetFullPath(keyfile);
|
2013-11-25 08:53:27 +01:00
|
|
|
var fs = new FileStream(keyfile, FileMode.Open);
|
|
|
|
var keypair = new System.Reflection.StrongNameKeyPair(fs);
|
|
|
|
fs.Close();
|
|
|
|
write_params.StrongNameKeyPair = keypair;
|
|
|
|
}
|
2013-11-25 10:06:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Console.Error.WriteLine("No keyfile specified or keyfile missing.");
|
|
|
|
}
|
2013-11-25 08:53:27 +01:00
|
|
|
|
|
|
|
// Load assembly and process all modules
|
|
|
|
var assembly = AssemblyDefinition.ReadAssembly(file, read_params);
|
2013-11-25 00:19:54 +01:00
|
|
|
foreach (var module in assembly.Modules)
|
|
|
|
{
|
|
|
|
foreach (var reference in module.AssemblyReferences)
|
|
|
|
{
|
|
|
|
module.AssemblyResolver.Resolve(reference);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var type in module.Types)
|
|
|
|
{
|
|
|
|
Rewrite(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 08:53:27 +01:00
|
|
|
// Save rewritten assembly
|
|
|
|
assembly.Write(file, write_params);
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rewrite(TypeDefinition type)
|
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
var entry_points = type.Fields.FirstOrDefault(f => f.Name == "EntryPoints");
|
|
|
|
if (entry_points != null)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
foreach (var method in type.Methods)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
if (method.CustomAttributes.Any(a => a.AttributeType.Name == "SlotAttribute"))
|
|
|
|
{
|
|
|
|
ProcessMethod(method, entry_points);
|
|
|
|
}
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 01:31:10 +01:00
|
|
|
// Create body for method
|
|
|
|
static void ProcessMethod(MethodDefinition method, FieldDefinition entry_points)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
var nint = method.DeclaringType.Module.Import(typeof(IntPtr));
|
|
|
|
var body = method.Body;
|
2013-11-25 00:19:54 +01:00
|
|
|
var instructions = body.Instructions;
|
|
|
|
var il = body.GetILProcessor();
|
2013-11-26 01:31:10 +01:00
|
|
|
var slot_attribute = method.CustomAttributes
|
|
|
|
.First(a => a.AttributeType.Name == "SlotAttribute");
|
|
|
|
var slot = (int)slot_attribute.ConstructorArguments.First().Value;
|
|
|
|
|
|
|
|
instructions.Clear();
|
|
|
|
|
|
|
|
// Declare pinned variables for every reference and array parameter
|
|
|
|
// and push each parameter on the stack
|
|
|
|
EmitParameters(method, nint, body, il);
|
2013-11-25 00:19:54 +01:00
|
|
|
|
2013-11-26 01:31:10 +01:00
|
|
|
// push the entry point address on the stack
|
|
|
|
EmitEntryPoint(entry_points, il, slot);
|
2013-11-25 00:19:54 +01:00
|
|
|
|
2013-11-26 01:31:10 +01:00
|
|
|
// issue calli
|
|
|
|
EmitCall(il, method);
|
|
|
|
|
|
|
|
// return
|
|
|
|
il.Emit(OpCodes.Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitParameters(MethodDefinition method, TypeReference nint, MethodBody body, ILProcessor il)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < method.Parameters.Count; i++)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
var p = method.Parameters[i];
|
|
|
|
switch (i)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
case 0:
|
|
|
|
il.Emit(OpCodes.Ldarg_0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
il.Emit(OpCodes.Ldarg_1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
il.Emit(OpCodes.Ldarg_2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
il.Emit(OpCodes.Ldarg_3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
il.Emit(OpCodes.Ldarg_S, (byte)i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p.ParameterType.IsArray || p.ParameterType.IsByReference)
|
|
|
|
{
|
|
|
|
body.Variables.Add(new VariableDefinition(new PinnedType(nint)));
|
|
|
|
var index = body.Variables.Count - 1;
|
|
|
|
switch (index)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
case 0:
|
|
|
|
il.Emit(OpCodes.Stloc_0);
|
|
|
|
il.Emit(OpCodes.Ldloc_0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
il.Emit(OpCodes.Stloc_1);
|
|
|
|
il.Emit(OpCodes.Ldloc_1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
il.Emit(OpCodes.Stloc_2);
|
|
|
|
il.Emit(OpCodes.Ldloc_2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
il.Emit(OpCodes.Stloc_3);
|
|
|
|
il.Emit(OpCodes.Ldloc_3);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
il.Emit(OpCodes.Stloc_S, (byte)index);
|
|
|
|
il.Emit(OpCodes.Ldloc_S, (byte)index);
|
|
|
|
break;
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
2013-11-26 01:31:10 +01:00
|
|
|
//il.Emit(OpCodes.Conv_I);
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-26 01:31:10 +01:00
|
|
|
static void EmitEntryPoint(FieldDefinition entry_points, ILProcessor il, int slot)
|
|
|
|
{
|
|
|
|
il.Emit(OpCodes.Ldsfld, entry_points);
|
|
|
|
switch (slot)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_1);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_3);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_4);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_5);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_6);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_7);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
il.Emit(OpCodes.Ldc_I4_8);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (slot < 128)
|
|
|
|
il.Emit(OpCodes.Ldc_I4_S, (byte)slot);
|
|
|
|
else
|
|
|
|
il.Emit(OpCodes.Ldc_I4, slot);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
il.Emit(OpCodes.Ldelem_I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EmitCall(ILProcessor il, MethodReference reference)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
|
|
|
var signature = new CallSite(reference.ReturnType)
|
|
|
|
{
|
2013-11-25 00:58:09 +01:00
|
|
|
CallingConvention = MethodCallingConvention.Default,
|
2013-11-25 00:19:54 +01:00
|
|
|
};
|
2013-11-25 08:53:27 +01:00
|
|
|
|
2013-11-26 01:31:10 +01:00
|
|
|
foreach (var p in reference.Parameters)
|
2013-11-25 00:19:54 +01:00
|
|
|
{
|
2013-11-26 01:31:10 +01:00
|
|
|
signature.Parameters.Add(p);
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
2013-11-25 08:53:27 +01:00
|
|
|
|
|
|
|
// Since the last parameter is always the entry point address,
|
|
|
|
// we do not need any special preparation before emiting calli.
|
2013-11-26 01:31:10 +01:00
|
|
|
il.Emit(OpCodes.Calli, signature);
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
2013-11-25 20:00:22 +01:00
|
|
|
|
|
|
|
// IntPtr Pin<T>({ref} T{[];[,];[,,]} arg)
|
|
|
|
// Pin the parameter and return an unmanaged pointer
|
|
|
|
static void RewritePin(ILProcessor il, Instruction inst, MethodReference reference)
|
|
|
|
{
|
|
|
|
var greference = reference as GenericInstanceMethod;
|
|
|
|
if (greference == null)
|
|
|
|
throw new InvalidOperationException("reference must match generic method Pin<T>");
|
|
|
|
|
|
|
|
var ptype = greference.GenericArguments.First();
|
|
|
|
}
|
2013-11-25 00:19:54 +01:00
|
|
|
}
|
|
|
|
}
|