From 301a2fab97e1a38c8c7048463d1edd1b82b8c035 Mon Sep 17 00:00:00 2001 From: de4dot Date: Wed, 31 Oct 2012 17:43:51 +0100 Subject: [PATCH] Port more code --- blocks/Block.cs | 4 ++-- blocks/Blocks.cs | 30 +++++++++++++++--------------- blocks/CodeGenerator.cs | 18 +++++++++--------- blocks/ForwardScanOrder.cs | 2 +- blocks/Instr.cs | 26 +++++++++++++------------- blocks/InstructionListParser.cs | 2 +- blocks/ScopeBlock.cs | 2 +- blocks/TryHandlerBlock.cs | 12 ++++++------ dot10 | 2 +- 9 files changed, 49 insertions(+), 49 deletions(-) diff --git a/blocks/Block.cs b/blocks/Block.cs index 1142e016..73e1d748 100644 --- a/blocks/Block.cs +++ b/blocks/Block.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil.Cil; +using dot10.DotNet.Emit; namespace de4dot.blocks { public class Block : BaseBlock { @@ -269,7 +269,7 @@ namespace de4dot.blocks { for (int i = 0; i < instrs.Count; i++) { var instr = instrs[i]; if (instr.OpCode != OpCodes.Nop) - dest.Add(clone ? new Instr(DotNetUtils.clone(instr.Instruction)) : instr); + dest.Add(clone ? new Instr(instr.Instruction.Clone()) : instr); } } diff --git a/blocks/Blocks.cs b/blocks/Blocks.cs index 8b7afb0d..25800f79 100644 --- a/blocks/Blocks.cs +++ b/blocks/Blocks.cs @@ -19,35 +19,35 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; namespace de4dot.blocks { public class Blocks { - MethodDefinition method; - IList locals; + MethodDef method; + IList locals; MethodBlocks methodBlocks; public MethodBlocks MethodBlocks { get { return methodBlocks; } } - public IList Locals { + public IList Locals { get { return locals; } } - public MethodDefinition Method { + public MethodDef Method { get { return method; } } - public Blocks(MethodDefinition method) { + public Blocks(MethodDef method) { this.method = method; updateBlocks(); } public void updateBlocks() { - var body = method.Body; - locals = body.Variables; + var body = method.CilBody; + locals = body.LocalList; methodBlocks = new InstructionListParser(body.Instructions, body.ExceptionHandlers).parse(); } @@ -79,11 +79,11 @@ namespace de4dot.blocks { if (locals.Count == 0) return 0; - var usedLocals = new Dictionary>(); + var usedLocals = new Dictionary>(); foreach (var block in methodBlocks.getAllBlocks()) { for (int i = 0; i < block.Instructions.Count; i++) { var instr = block.Instructions[i]; - VariableDefinition local; + Local local; switch (instr.OpCode.Code) { case Code.Ldloc: case Code.Ldloc_S: @@ -102,7 +102,7 @@ namespace de4dot.blocks { case Code.Ldloca_S: case Code.Ldloca: - local = (VariableDefinition)instr.Operand; + local = (Local)instr.Operand; break; default: @@ -122,7 +122,7 @@ namespace de4dot.blocks { } int newIndex = -1; - var newLocals = new List(usedLocals.Count); + var newLocals = new List(usedLocals.Count); foreach (var local in usedLocals.Keys) { newIndex++; newLocals.Add(local); @@ -137,7 +137,7 @@ namespace de4dot.blocks { return numRemoved; } - static Instruction optimizeLocalInstr(Instr instr, VariableDefinition local, uint newIndex) { + static Instruction optimizeLocalInstr(Instr instr, Local local, uint newIndex) { switch (instr.OpCode.Code) { case Code.Ldloc: case Code.Ldloc_S: @@ -194,7 +194,7 @@ namespace de4dot.blocks { } catch (NullReferenceException) { //TODO: Send this message to the log - Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MetadataToken.ToUInt32(), method.FullName); + Console.WriteLine("Null ref exception! Invalid metadata token in code? Method: {0:X8}: {1}", method.MDToken.Raw, method.FullName); return; } } diff --git a/blocks/CodeGenerator.cs b/blocks/CodeGenerator.cs index 1db3d945..04bf88cc 100644 --- a/blocks/CodeGenerator.cs +++ b/blocks/CodeGenerator.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; namespace de4dot.blocks { class CodeGenerator { @@ -45,11 +45,11 @@ namespace de4dot.blocks { public int filterStart; public int handlerStart; public int handlerEnd; - public TypeReference catchType; - public ExceptionHandlerType handlerType; + public ITypeDefOrRef catchType; + public ExceptionClause handlerType; public ExceptionInfo(int tryStart, int tryEnd, int filterStart, - int handlerStart, int handlerEnd, TypeReference catchType, - ExceptionHandlerType handlerType) { + int handlerStart, int handlerEnd, ITypeDefOrRef catchType, + ExceptionClause handlerType) { if (tryStart > tryEnd || filterStart > handlerStart || tryStart < 0 || tryEnd < 0 || filterStart < 0 || handlerStart < 0 || handlerEnd < 0) throw new ApplicationException("Invalid start/end/filter/handler indexes"); @@ -99,10 +99,10 @@ namespace de4dot.blocks { } void recalculateInstructionOffsets(IList allInstructions) { - int offset = 0; + uint offset = 0; foreach (var instr in allInstructions) { instr.Offset = offset; - offset += instr.GetSize(); + offset += (uint)instr.GetSize(); } } @@ -137,7 +137,7 @@ namespace de4dot.blocks { if (getShortBranch(instruction, out opcode)) { const int instrSize = 5; // It's a long branch instruction var target = (Instruction)instruction.Operand; - int distance = target == null ? int.MaxValue : target.Offset - (instruction.Offset + instrSize); + int distance = target == null ? int.MaxValue : (int)(target.Offset - (instruction.Offset + instrSize)); if (-0x80 <= distance && distance <= 0x7F) { instruction.OpCode = opcode; changed = true; diff --git a/blocks/ForwardScanOrder.cs b/blocks/ForwardScanOrder.cs index 3ba0635d..5f4da69d 100644 --- a/blocks/ForwardScanOrder.cs +++ b/blocks/ForwardScanOrder.cs @@ -52,7 +52,7 @@ namespace de4dot.blocks { int stack = stackStart; foreach (var instr in block.Instructions) - DotNetUtils.updateStack(instr.Instruction, ref stack, false); + instr.Instruction.UpdateStack(ref stack, false); stackEnd = stack; } } diff --git a/blocks/Instr.cs b/blocks/Instr.cs index 536a9c9c..9afb9c27 100644 --- a/blocks/Instr.cs +++ b/blocks/Instr.cs @@ -18,8 +18,8 @@ */ using System; -using Mono.Cecil.Cil; using System.Collections.Generic; +using dot10.DotNet.Emit; namespace de4dot.blocks { public class Instr { @@ -44,8 +44,8 @@ namespace de4dot.blocks { // Returns the variable or null if it's not a ldloc/stloc instruction. It does not return // a local variable if it's a ldloca/ldloca.s instruction. - public static VariableDefinition getLocalVar(IList locals, Instr instr) { - return DotNetUtils.getLocalVar(locals, instr.Instruction); + public static Local getLocalVar(IList locals, Instr instr) { + return instr.Instruction.GetLocal(locals); } static public bool isFallThrough(OpCode opCode) { @@ -104,23 +104,23 @@ namespace de4dot.blocks { } public bool isLdcI4() { - return DotNetUtils.isLdcI4(OpCode.Code); + return instruction.IsLdcI4(); } public int getLdcI4Value() { - return DotNetUtils.getLdcI4Value(instruction); + return instruction.GetLdcI4Value(); } public bool isLdarg() { - return DotNetUtils.isLdarg(instruction); + return instruction.IsLdarg(); } public bool isStloc() { - return DotNetUtils.isStloc(instruction); + return instruction.IsStloc(); } public bool isLdloc() { - return DotNetUtils.isLdloc(instruction); + return instruction.IsLdloc(); } public bool isNop() { @@ -132,23 +132,23 @@ namespace de4dot.blocks { } public bool isLeave() { - return DotNetUtils.isLeave(instruction); + return instruction.IsLeave(); } public bool isBr() { - return DotNetUtils.isBr(instruction); + return instruction.IsBr(); } public bool isBrfalse() { - return DotNetUtils.isBrfalse(instruction); + return instruction.IsBrfalse(); } public bool isBrtrue() { - return DotNetUtils.isBrtrue(instruction); + return instruction.IsBrtrue(); } public bool isConditionalBranch() { - return DotNetUtils.isConditionalBranch(OpCode.Code); + return instruction.IsConditionalBranch(); } public bool getFlippedBranchOpCode(out OpCode opcode) { diff --git a/blocks/InstructionListParser.cs b/blocks/InstructionListParser.cs index 33288f03..82de56e3 100644 --- a/blocks/InstructionListParser.cs +++ b/blocks/InstructionListParser.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil.Cil; +using dot10.DotNet.Emit; namespace de4dot.blocks { class InstructionListParser { diff --git a/blocks/ScopeBlock.cs b/blocks/ScopeBlock.cs index 2ca81c95..677bd4b8 100644 --- a/blocks/ScopeBlock.cs +++ b/blocks/ScopeBlock.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil.Cil; +using dot10.DotNet.Emit; namespace de4dot.blocks { // A normal branch may not transfer out of a protected block (try block), filter handler, diff --git a/blocks/TryHandlerBlock.cs b/blocks/TryHandlerBlock.cs index 1fc7b5b8..a2e8bf9e 100644 --- a/blocks/TryHandlerBlock.cs +++ b/blocks/TryHandlerBlock.cs @@ -17,8 +17,8 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; namespace de4dot.blocks { // Contains the filter handler block and the catch handler block. @@ -27,14 +27,14 @@ namespace de4dot.blocks { HandlerBlock handlerBlock = new HandlerBlock(); // State for an ExceptionHandler instance - TypeReference catchType; - ExceptionHandlerType handlerType; + ITypeDefOrRef catchType; + ExceptionClause handlerType; - public TypeReference CatchType { + public ITypeDefOrRef CatchType { get { return catchType; } } - public ExceptionHandlerType HandlerType { + public ExceptionClause HandlerType { get { return handlerType; } } diff --git a/dot10 b/dot10 index ec483110..526604c8 160000 --- a/dot10 +++ b/dot10 @@ -1 +1 @@ -Subproject commit ec48311025fd4e9a56da7881857d90eaa0e3b5ca +Subproject commit 526604c81df777accde4e45327cffcba954c356c