2011-09-27 07:48:47 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-09-27 07:48:47 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot 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.
|
|
|
|
|
|
|
|
|
|
de4dot 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 de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
using dot10.DotNet;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace AssemblyData.methodsrewriter {
|
|
|
|
|
class MModule {
|
|
|
|
|
public Module module;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public ModuleDefMD moduleDef;
|
2011-12-17 01:46:30 +08:00
|
|
|
|
TypeDefinitionDict<MType> typeReferenceToType = new TypeDefinitionDict<MType>();
|
2011-09-27 07:48:47 +08:00
|
|
|
|
Dictionary<int, MType> tokenToType = new Dictionary<int, MType>();
|
|
|
|
|
Dictionary<int, MMethod> tokenToGlobalMethod;
|
|
|
|
|
Dictionary<int, MField> tokenToGlobalField;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
TypeDef moduleType;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MModule(Module module, ModuleDefMD moduleDefinition) {
|
2011-09-27 07:48:47 +08:00
|
|
|
|
this.module = module;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
this.moduleDef = moduleDefinition;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
initTokenToType();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initTokenToType() {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
moduleType = moduleDef.Types[0];
|
|
|
|
|
foreach (var typeDef in moduleDef.GetTypes()) {
|
|
|
|
|
int token = (int)typeDef.MDToken.Raw;
|
2012-01-19 01:39:59 +08:00
|
|
|
|
Type type;
|
|
|
|
|
try {
|
|
|
|
|
type = module.ResolveType(token);
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
tokenToType[token] = null;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
typeReferenceToType.add(typeDef, null);
|
2012-01-19 01:39:59 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-11-01 05:44:54 +08:00
|
|
|
|
var mtype = new MType(type, typeDef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
tokenToType[token] = mtype;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
typeReferenceToType.add(typeDef, mtype);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MType getType(ITypeDefOrRef typeRef) {
|
|
|
|
|
return typeReferenceToType.find(typeRef);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MMethod getMethod(IMethod methodRef) {
|
|
|
|
|
var type = getType(methodRef.DeclaringType);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
if (type != null)
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return type.getMethod(methodRef);
|
|
|
|
|
if (!new SigComparer().Equals(moduleType, methodRef.DeclaringType))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
initGlobalMethods();
|
|
|
|
|
foreach (var method in tokenToGlobalMethod.Values) {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
if (new SigComparer().Equals(methodRef, method.methodDef))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 05:44:54 +08:00
|
|
|
|
public MField getField(IField fieldRef) {
|
|
|
|
|
var type = getType(fieldRef.DeclaringType);
|
2011-09-27 07:48:47 +08:00
|
|
|
|
if (type != null)
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return type.getField(fieldRef);
|
|
|
|
|
if (!new SigComparer().Equals(moduleType, fieldRef.DeclaringType))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
initGlobalFields();
|
|
|
|
|
foreach (var field in tokenToGlobalField.Values) {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
if (new SigComparer().Equals(fieldRef, field.fieldDef))
|
2011-09-27 07:48:47 +08:00
|
|
|
|
return field;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MMethod getMethod(MethodBase method) {
|
|
|
|
|
if (method.Module != module)
|
|
|
|
|
throw new ApplicationException("Not our module");
|
|
|
|
|
if (method.DeclaringType == null)
|
|
|
|
|
return getGlobalMethod(method);
|
|
|
|
|
var type = tokenToType[method.DeclaringType.MetadataToken];
|
|
|
|
|
return type.getMethod(method.MetadataToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MMethod getGlobalMethod(MethodBase method) {
|
|
|
|
|
initGlobalMethods();
|
|
|
|
|
return tokenToGlobalMethod[method.MetadataToken];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initGlobalMethods() {
|
|
|
|
|
if (tokenToGlobalMethod != null)
|
|
|
|
|
return;
|
|
|
|
|
tokenToGlobalMethod = new Dictionary<int, MMethod>();
|
|
|
|
|
|
|
|
|
|
var tmpTokenToGlobalMethod = new Dictionary<int, MethodInfo>();
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
|
foreach (var m in module.GetMethods(flags))
|
|
|
|
|
tmpTokenToGlobalMethod[m.MetadataToken] = m;
|
|
|
|
|
foreach (var m in moduleType.Methods) {
|
|
|
|
|
if (m.Name == ".cctor") //TODO: Use module.GetMethod(token) to get .cctor method
|
|
|
|
|
continue;
|
2012-11-01 05:44:54 +08:00
|
|
|
|
var token = (int)m.MDToken.Raw;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
tokenToGlobalMethod[token] = new MMethod(tmpTokenToGlobalMethod[token], m);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initGlobalFields() {
|
|
|
|
|
if (tokenToGlobalField != null)
|
|
|
|
|
return;
|
|
|
|
|
tokenToGlobalField = new Dictionary<int, MField>();
|
|
|
|
|
|
|
|
|
|
var tmpTokenToGlobalField = new Dictionary<int, FieldInfo>();
|
|
|
|
|
var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
|
|
|
|
|
foreach (var f in module.GetFields(flags))
|
|
|
|
|
tmpTokenToGlobalField[f.MetadataToken] = f;
|
|
|
|
|
foreach (var f in moduleType.Fields) {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
var token = (int)f.MDToken.Raw;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
tokenToGlobalField[token] = new MField(tmpTokenToGlobalField[token], f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
2012-11-01 05:44:54 +08:00
|
|
|
|
return moduleDef.Location;
|
2011-09-27 07:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|