Update code so it can rename duplicate member references

This commit is contained in:
de4dot 2011-11-02 04:24:22 +01:00
parent 8b780a4696
commit ccff408a00
4 changed files with 150 additions and 75 deletions

View File

@ -189,11 +189,12 @@ namespace de4dot.renamer {
void findAllMemberReferences() { void findAllMemberReferences() {
Log.v("Finding all MemberReferences"); Log.v("Finding all MemberReferences");
int index = 0;
foreach (var module in modules) { foreach (var module in modules) {
if (modules.Count > 1) if (modules.Count > 1)
Log.v("Finding all MemberReferences ({0})", module.Filename); Log.v("Finding all MemberReferences ({0})", module.Filename);
Log.indent(); Log.indent();
module.findAllMemberReferences(); module.findAllMemberReferences(ref index);
Log.deIndent(); Log.deIndent();
} }
} }

View File

@ -289,11 +289,11 @@ namespace de4dot.renamer {
public Module module; public Module module;
string newNamespace = null; string newNamespace = null;
DefDict<EventDef> events = new DefDict<EventDef>(); EventDefDict events = new EventDefDict();
DefDict<FieldDef> fields = new DefDict<FieldDef>(); FieldDefDict fields = new FieldDefDict();
DefDict<MethodDef> methods = new DefDict<MethodDef>(); MethodDefDict methods = new MethodDefDict();
DefDict<PropertyDef> properties = new DefDict<PropertyDef>(); PropertyDefDict properties = new PropertyDefDict();
DefDict<TypeDef> types = new DefDict<TypeDef>(); TypeDefDict types = new TypeDefDict();
IList<GenericParamDef> genericParams; IList<GenericParamDef> genericParams;
public TypeDefinition TypeDefinition { public TypeDefinition TypeDefinition {
get { return (TypeDefinition)MemberReference; } get { return (TypeDefinition)MemberReference; }
@ -316,11 +316,6 @@ namespace de4dot.renamer {
: base(typeDefinition, null, index) { : base(typeDefinition, null, index) {
this.module = module; this.module = module;
genericParams = createGenericParamDefList(TypeDefinition.GenericParameters); genericParams = createGenericParamDefList(TypeDefinition.GenericParameters);
fields.HandleDupe = (newOne, oldOne) => {
if (oldOne.FieldDefinition.IsLiteral && !newOne.FieldDefinition.IsLiteral)
fields.replaceOldWithNew(oldOne, newOne);
};
} }
public override bool isSame(MemberReference mr) { public override bool isSame(MemberReference mr) {

View File

@ -20,6 +20,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Mono.Cecil; using Mono.Cecil;
using de4dot.blocks;
namespace de4dot.renamer { namespace de4dot.renamer {
interface IResolver { interface IResolver {
@ -34,39 +35,23 @@ namespace de4dot.renamer {
EventDef findEvent(MethodReference methodReference); EventDef findEvent(MethodReference methodReference);
} }
class MyDict<T> { interface RefDict<TRef, TMRef> where TRef : Ref where TMRef : MemberReference {
Dictionary<string, T> dict = new Dictionary<string, T>(StringComparer.Ordinal); IEnumerable<TRef> getAll();
public T this[string key] { IEnumerable<TRef> getSorted();
get { TRef find(TMRef tmref);
T t; void add(TRef tref);
if (dict.TryGetValue(key, out t))
return t;
return default(T);
}
set {
dict[key] = value;
}
}
public IEnumerable<T> getAll() {
foreach (var elem in dict.Values)
yield return elem;
}
} }
class DefDict<T> where T : Ref { class TypeDefDict : RefDict<TypeDef, TypeReference> {
MyDict<IList<T>> dict = new MyDict<IList<T>>(); Dictionary<ScopeAndTokenKey, TypeDef> tokenToTypeDef = new Dictionary<ScopeAndTokenKey, TypeDef>();
public Action<T, T> HandleDupe { get; set; } Dictionary<TypeReferenceKey, TypeDef> typeRefToDef = new Dictionary<TypeReferenceKey, TypeDef>();
public IEnumerable<T> getAll() { public IEnumerable<TypeDef> getAll() {
foreach (var list in dict.getAll()) { return tokenToTypeDef.Values;
foreach (var t in list)
yield return t;
}
} }
public IEnumerable<T> getSorted() { public IEnumerable<TypeDef> getSorted() {
var list = new List<T>(getAll()); var list = new List<TypeDef>(getAll());
list.Sort((a, b) => { list.Sort((a, b) => {
if (a.Index < b.Index) return -1; if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1; if (a.Index > b.Index) return 1;
@ -75,46 +60,140 @@ namespace de4dot.renamer {
return list; return list;
} }
public T find(MemberReference mr) { public TypeDef find(TypeReference typeReference) {
var list = dict[mr.Name]; TypeDef typeDef;
if (list == null) if (tokenToTypeDef.TryGetValue(new ScopeAndTokenKey(typeReference), out typeDef))
return null; return typeDef;
foreach (T t in list) { typeRefToDef.TryGetValue(new TypeReferenceKey(typeReference), out typeDef);
if (t.isSame(mr)) return typeDef;
return t;
}
return null;
} }
public void add(T t) { public void add(TypeDef typeDef) {
var other = find(t.MemberReference); tokenToTypeDef[new ScopeAndTokenKey(typeDef.TypeDefinition)] = typeDef;
if (other != null) { typeRefToDef[new TypeReferenceKey(typeDef.TypeDefinition)] = typeDef;
handleDupe(t, other); }
return; }
}
var list = dict[t.MemberReference.Name]; class FieldDefDict : RefDict<FieldDef, FieldReference> {
if (list == null) Dictionary<ScopeAndTokenKey, FieldDef> tokenToFieldDef = new Dictionary<ScopeAndTokenKey, FieldDef>();
dict[t.MemberReference.Name] = list = new List<T>(); Dictionary<FieldReferenceKey, FieldDef> fieldRefToDef = new Dictionary<FieldReferenceKey, FieldDef>();
list.Add(t);
return; public IEnumerable<FieldDef> getAll() {
return tokenToFieldDef.Values;
} }
public void replaceOldWithNew(T oldOne, T newOne) { public IEnumerable<FieldDef> getSorted() {
if (find(newOne.MemberReference) != oldOne) var list = new List<FieldDef>(getAll());
throw new ApplicationException("Not same member reference"); list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
var list = dict[oldOne.MemberReference.Name]; if (a.Index > b.Index) return 1;
if (!list.Remove(oldOne)) return 0;
throw new ApplicationException("Could not remove old one"); });
list.Add(newOne); return list;
} }
void handleDupe(T newOne, T oldOne) { public FieldDef find(FieldReference fieldReference) {
Log.v("Duplicate MemberReference found: {0} ({1:X8} vs {2:X8})", newOne.MemberReference, newOne.MemberReference.MetadataToken.ToUInt32(), oldOne.MemberReference.MetadataToken.ToUInt32()); FieldDef fieldDef;
if (HandleDupe != null) if (tokenToFieldDef.TryGetValue(new ScopeAndTokenKey(fieldReference), out fieldDef))
HandleDupe(newOne, oldOne); return fieldDef;
fieldRefToDef.TryGetValue(new FieldReferenceKey(fieldReference), out fieldDef);
return fieldDef;
}
public void add(FieldDef fieldDef) {
tokenToFieldDef[new ScopeAndTokenKey(fieldDef.FieldDefinition)] = fieldDef;
fieldRefToDef[new FieldReferenceKey(fieldDef.FieldDefinition)] = fieldDef;
}
}
class MethodDefDict : RefDict<MethodDef, MethodReference> {
Dictionary<ScopeAndTokenKey, MethodDef> tokenToMethodDef = new Dictionary<ScopeAndTokenKey, MethodDef>();
Dictionary<MethodReferenceKey, MethodDef> methodRefToDef = new Dictionary<MethodReferenceKey, MethodDef>();
public IEnumerable<MethodDef> getAll() {
return tokenToMethodDef.Values;
}
public IEnumerable<MethodDef> getSorted() {
var list = new List<MethodDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public MethodDef find(MethodReference methodReference) {
MethodDef methodDef;
if (tokenToMethodDef.TryGetValue(new ScopeAndTokenKey(methodReference), out methodDef))
return methodDef;
methodRefToDef.TryGetValue(new MethodReferenceKey(methodReference), out methodDef);
return methodDef;
}
public void add(MethodDef methodDef) {
tokenToMethodDef[new ScopeAndTokenKey(methodDef.MethodDefinition)] = methodDef;
methodRefToDef[new MethodReferenceKey(methodDef.MethodDefinition)] = methodDef;
}
}
class PropertyDefDict : RefDict<PropertyDef, PropertyReference> {
Dictionary<ScopeAndTokenKey, PropertyDef> tokenToPropDef = new Dictionary<ScopeAndTokenKey, PropertyDef>();
public IEnumerable<PropertyDef> getAll() {
return tokenToPropDef.Values;
}
public IEnumerable<PropertyDef> getSorted() {
var list = new List<PropertyDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public PropertyDef find(PropertyReference propertyReference) {
PropertyDef propDef;
tokenToPropDef.TryGetValue(new ScopeAndTokenKey(propertyReference), out propDef);
return propDef;
}
public void add(PropertyDef propDef) {
tokenToPropDef[new ScopeAndTokenKey(propDef.PropertyDefinition)] = propDef;
}
}
class EventDefDict : RefDict<EventDef, EventReference> {
Dictionary<ScopeAndTokenKey, EventDef> tokenToEventDef = new Dictionary<ScopeAndTokenKey, EventDef>();
public IEnumerable<EventDef> getAll() {
return tokenToEventDef.Values;
}
public IEnumerable<EventDef> getSorted() {
var list = new List<EventDef>(getAll());
list.Sort((a, b) => {
if (a.Index < b.Index) return -1;
if (a.Index > b.Index) return 1;
return 0;
});
return list;
}
public EventDef find(EventReference eventReference) {
EventDef propDef;
tokenToEventDef.TryGetValue(new ScopeAndTokenKey(eventReference), out propDef);
return propDef;
}
public void add(EventDef eventDef) {
tokenToEventDef[new ScopeAndTokenKey(eventDef.EventDefinition)] = eventDef;
} }
} }

View File

@ -27,7 +27,7 @@ namespace de4dot.renamer {
class Module : IResolver { class Module : IResolver {
IObfuscatedFile obfuscatedFile; IObfuscatedFile obfuscatedFile;
MemberRefFinder memberRefFinder; MemberRefFinder memberRefFinder;
DefDict<TypeDef> allTypes = new DefDict<TypeDef>(); TypeDefDict allTypes = new TypeDefDict();
IList<RefToDef<TypeReference, TypeDefinition>> typeRefsToRename = new List<RefToDef<TypeReference, TypeDefinition>>(); IList<RefToDef<TypeReference, TypeDefinition>> typeRefsToRename = new List<RefToDef<TypeReference, TypeDefinition>>();
IList<RefToDef<MethodReference, MethodDefinition>> methodRefsToRename = new List<RefToDef<MethodReference, MethodDefinition>>(); IList<RefToDef<MethodReference, MethodDefinition>> methodRefsToRename = new List<RefToDef<MethodReference, MethodDefinition>>();
IList<RefToDef<FieldReference, FieldDefinition>> fieldRefsToRename = new List<RefToDef<FieldReference, FieldDefinition>>(); IList<RefToDef<FieldReference, FieldDefinition>> fieldRefsToRename = new List<RefToDef<FieldReference, FieldDefinition>>();
@ -187,7 +187,7 @@ namespace de4dot.renamer {
} }
} }
public void findAllMemberReferences() { public void findAllMemberReferences(ref int typeIndex) {
memberRefFinder = new MemberRefFinder(); memberRefFinder = new MemberRefFinder();
memberRefFinder.findAll(ModuleDefinition, ModuleDefinition.Types); memberRefFinder.findAll(ModuleDefinition, ModuleDefinition.Types);
allMethods = new List<MethodDefinition>(memberRefFinder.methodDefinitions.Keys); allMethods = new List<MethodDefinition>(memberRefFinder.methodDefinitions.Keys);
@ -195,7 +195,7 @@ namespace de4dot.renamer {
var allTypesList = new List<TypeDef>(); var allTypesList = new List<TypeDef>();
foreach (var type in new List<TypeDefinition>(memberRefFinder.typeDefinitions.Keys)) { foreach (var type in new List<TypeDefinition>(memberRefFinder.typeDefinitions.Keys)) {
memberRefFinder.removeTypeDefinition(type); memberRefFinder.removeTypeDefinition(type);
var typeDef = new TypeDef(type, this); var typeDef = new TypeDef(type, this, typeIndex++);
allTypes.add(typeDef); allTypes.add(typeDef);
allTypesList.Add(typeDef); allTypesList.Add(typeDef);
@ -302,7 +302,7 @@ namespace de4dot.renamer {
} }
void rebuildAllTypesDict() { void rebuildAllTypesDict() {
var newAllTypes = new DefDict<TypeDef>(); var newAllTypes = new TypeDefDict();
foreach (var typeDef in allTypes.getAll()) foreach (var typeDef in allTypes.getAll())
newAllTypes.add(typeDef); newAllTypes.add(typeDef);
allTypes = newAllTypes; allTypes = newAllTypes;