Update detection of <Module> type

This commit is contained in:
de4dot 2012-01-08 18:46:23 +01:00
parent d295fa24a2
commit 0398666c93
5 changed files with 26 additions and 11 deletions

View File

@ -44,11 +44,9 @@ namespace AssemblyData.methodsrewriter {
var tmpTokenToTypeDefinition = new Dictionary<int, TypeDefinition>();
foreach (var t in module.GetTypes())
tmpTokenToType[t.MetadataToken] = t;
foreach (var t in moduleDefinition.GetTypes()) {
foreach (var t in moduleDefinition.GetTypes())
tmpTokenToTypeDefinition[t.MetadataToken.ToInt32()] = t;
if (moduleType == null && t.FullName == "<Module>")
moduleType = t;
}
moduleType = DotNetUtils.getModuleType(moduleDefinition);
foreach (var token in tmpTokenToType.Keys) {
var mtype = new MType(tmpTokenToType[token], tmpTokenToTypeDefinition[token]);
tokenToType[token] = mtype;

View File

@ -281,11 +281,17 @@ namespace de4dot.blocks {
}
public static TypeDefinition getModuleType(ModuleDefinition module) {
foreach (var type in module.Types) {
if (type.FullName == "<Module>")
return type;
if (module.Types.Count == 0)
return null;
if (module.Runtime == TargetRuntime.Net_1_0 || module.Runtime == TargetRuntime.Net_1_1) {
if (module.Types[0].FullName == "<Module>")
return module.Types[0];
return null;
}
return null;
// It's always the first one, no matter what it is named.
return module.Types[0];
}
public static bool isEmpty(MethodDefinition method) {

View File

@ -169,8 +169,9 @@ namespace de4dot.code.deobfuscators {
}
void restoreBaseType() {
var moduleType = DotNetUtils.getModuleType(module);
foreach (var type in module.GetTypes()) {
if (type.BaseType != null || type.IsInterface || type.FullName == "<Module>")
if (type.BaseType != null || type.IsInterface || type == moduleType)
continue;
Log.v("Adding System.Object as base type: {0} ({1:X8})",
Utils.removeNewlines(type),

View File

@ -271,8 +271,9 @@ namespace de4dot.code.deobfuscators.SmartAssembly {
bool hasEmptyClassesInEveryNamespace() {
var namespaces = new Dictionary<string, int>(StringComparer.Ordinal);
var moduleType = DotNetUtils.getModuleType(module);
foreach (var type in module.Types) {
if (type.FullName == "<Module>")
if (type == moduleType)
continue;
var ns = type.Namespace;
if (!namespaces.ContainsKey(ns))

View File

@ -81,6 +81,10 @@ namespace de4dot.code.renamer {
return baseInfo;
}
bool isModuleType() {
return type.TypeDefinition == DotNetUtils.getModuleType(type.TypeDefinition.Module);
}
public void prepareRenameTypes(TypeRenamerState state) {
var checker = NameChecker;
@ -94,7 +98,12 @@ namespace de4dot.code.renamer {
string origClassName = null;
if (isWinFormsClass())
origClassName = findWindowsFormsClassName(type);
if (oldFullName != "<Module>" && !checker.isValidTypeName(oldName)) {
if (isModuleType()) {
if (oldNamespace != "")
newNamespace = "";
rename("<Module>");
}
else if (!checker.isValidTypeName(oldName)) {
if (origClassName != null && checker.isValidTypeName(origClassName))
rename(state.getTypeName(oldName, origClassName));
else {