2011-12-21 04:47:45 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-12-21 04:47:45 +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.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators {
|
|
|
|
|
class UnusedMethodsFinder {
|
2012-11-01 18:28:09 +08:00
|
|
|
|
ModuleDef module;
|
2011-12-28 20:30:44 +08:00
|
|
|
|
MethodCollection removedMethods;
|
2012-11-01 18:28:09 +08:00
|
|
|
|
Dictionary<MethodDef, bool> possiblyUnusedMethods = new Dictionary<MethodDef, bool>();
|
|
|
|
|
Stack<MethodDef> notUnusedStack = new Stack<MethodDef>();
|
2011-12-21 04:47:45 +08:00
|
|
|
|
|
2012-11-01 18:28:09 +08:00
|
|
|
|
public UnusedMethodsFinder(ModuleDef module, IEnumerable<MethodDef> possiblyUnusedMethods, MethodCollection removedMethods) {
|
2011-12-21 04:47:45 +08:00
|
|
|
|
this.module = module;
|
2011-12-28 20:30:44 +08:00
|
|
|
|
this.removedMethods = removedMethods;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
foreach (var method in possiblyUnusedMethods) {
|
2012-11-07 09:02:38 +08:00
|
|
|
|
if (method != module.ManagedEntryPoint && !removedMethods.exists(method))
|
2011-12-21 04:47:45 +08:00
|
|
|
|
this.possiblyUnusedMethods[method] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 18:28:09 +08:00
|
|
|
|
public IEnumerable<MethodDef> find() {
|
2011-12-21 04:47:45 +08:00
|
|
|
|
if (possiblyUnusedMethods.Count == 0)
|
|
|
|
|
return possiblyUnusedMethods.Keys;
|
|
|
|
|
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
foreach (var method in type.Methods)
|
|
|
|
|
check(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (notUnusedStack.Count > 0) {
|
|
|
|
|
var method = notUnusedStack.Pop();
|
|
|
|
|
if (!possiblyUnusedMethods.Remove(method))
|
|
|
|
|
continue;
|
|
|
|
|
check(method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return possiblyUnusedMethods.Keys;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 18:28:09 +08:00
|
|
|
|
void check(MethodDef method) {
|
2012-11-06 22:58:55 +08:00
|
|
|
|
if (method.Body == null)
|
2011-12-21 04:47:45 +08:00
|
|
|
|
return;
|
|
|
|
|
if (possiblyUnusedMethods.ContainsKey(method))
|
|
|
|
|
return;
|
2011-12-28 20:30:44 +08:00
|
|
|
|
if (removedMethods.exists(method))
|
|
|
|
|
return;
|
2011-12-21 04:47:45 +08:00
|
|
|
|
|
2012-11-06 22:58:55 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
2011-12-21 04:47:45 +08:00
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Call:
|
|
|
|
|
case Code.Calli:
|
|
|
|
|
case Code.Callvirt:
|
|
|
|
|
case Code.Newobj:
|
|
|
|
|
case Code.Ldtoken:
|
|
|
|
|
case Code.Ldftn:
|
2011-12-28 20:30:44 +08:00
|
|
|
|
case Code.Ldvirtftn:
|
2011-12-21 04:47:45 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 18:28:09 +08:00
|
|
|
|
var calledMethod = DotNetUtils.getMethod2(module, instr.Operand as IMethod);
|
2011-12-21 04:47:45 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (possiblyUnusedMethods.ContainsKey(calledMethod))
|
|
|
|
|
notUnusedStack.Push(calledMethod);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|