public MethodBodyDlg(MethodBody body) { InitializeComponent(); textBox1.Text = body.IsFat.ToString(); textBox2.Text = body.HeaderSize.ToString("X8"); textBox3.Text = body.CodeSize.ToString("X8"); textBox4.Text = body.MaxStack.ToString("X4"); textBox5.Text = body.InitLocals.ToString(); textBox6.Text = body.LocalVarSig.ToString("X8"); disassembler = new MSILDisassembler(body); assembler = new MSILAssembler(body); MSILInstruction[] instructions = disassembler.Disassemble(); foreach (MSILInstruction instruction in instructions) { listView1.Items.Add(CreateItem(instruction)); } if (body.Variables != null) { foreach (VariableDefinition variable in body.Variables) listView2.Items.Add(new ListViewItem(new string[] { variable.Index.ToString(), variable.VariableType.ToString() })); } if (body.HasExtraSections) { foreach (MethodBodySection section in body.ExtraSections) { foreach (ExceptionHandler handler in section.ExceptionHandlers) listView3.Items.Add(new ListViewItem(new string[] { handler.Type.ToString(), handler.TryStart.ToString("X4"), handler.TryEnd.ToString("X4"), handler.HandlerStart.ToString("X4"), handler.HandlerEnd.ToString("X4") , handler.CatchType != null ? handler.CatchType.FullName : ""})); } } }
public override void LoadCache() { base.LoadCache(); _paramRange = MemberRange.CreateRange <ParameterDefinition>(this, 5, NETHeader.TablesHeap.GetTable(MetaDataTableType.Param, false)); _semantics = Semantics; _body = Body; _genericParameters = GenericParameters; }
public MSILAssembler(MethodBody methodBody) { this.MethodBody = methodBody; _disassembler = new MSILDisassembler(methodBody); _image = methodBody.Method._netheader._assembly.Image; _offsetConverter = new OffsetConverter(Section.GetSectionByRva(methodBody.Method._netheader._assembly, methodBody.Method.RVA)); _bodyOffset = _offsetConverter.RvaToFileOffset(methodBody.Method.RVA) + methodBody.HeaderSize; _tokenResolver = methodBody.Method._netheader.TokenResolver; }
public override void ClearCache() { base.ClearCache(); _paramRange = null; _semantics = null; _body = null; _genericParameters = new GenericParameter[0]; _hasLoadedGenericParameters = false; }
internal NETMethodReader(PeImage peImage, MethodBody methodbody) { tokenResolver = new MetaDataTokenResolver(peImage.assembly.netHeader); this.peImage = peImage; this.rva = methodbody.Method.RVA; this.methodbody = methodbody; peImage.SetOffset(Offset.FromRva(rva, peImage.assembly).FileOffset); rawoffset = (uint)peImage.stream.Position; signature = peImage.ReadByte(); }
public MethodDlg(MethodBody body) { InitializeComponent(); this.body = body; this.propertyGrid1.SelectedObject = body; this.disassembler = new MSILDisassembler(body); this.Text = "Method Body of " + body.Method.MetaDataToken.ToString("X8"); try { this.Text += " (" + body.Method.ToString() + ")"; } catch { this.Text += " (method.ToString() failed)"; } MSILInstruction[] instructions = disassembler.Disassemble(); foreach (MSILInstruction instruction in instructions) { ListViewItem item = new ListViewItem(new string[] { "IL_" + instruction.Offset.ToString("X4"), BytesToString(instruction.OpCode.Bytes) + " " + BytesToString(instruction.OperandBytes), instruction.OpCode.Name, instruction.GetOperandString(),}); listView1.Items.Add(item); } if (body.Variables != null && body.Variables.Length > 0) { foreach (VariableDefinition variable in body.Variables) listView2.Items.Add(new ListViewItem(new string[] { variable.Index.ToString(), variable.VariableType.FullName, })); } if (body.HasExtraSections) { foreach (MethodBodySection section in body.ExtraSections) if (section.IsExceptionHandler && section.ExceptionHandlers.Length > 0) foreach (ExceptionHandler handler in section.ExceptionHandlers) listView3.Items.Add(new ListViewItem(new string[] { handler.Type == ExceptionHandlerType.Catch ? "Catch -> " + handler.CatchType.FullName : handler.Type.ToString(), handler.TryStart.ToString("X4"), handler.TryEnd.ToString("X4"), handler.HandlerStart.ToString("X4"), handler.HandlerEnd.ToString("X4"), handler.FilterStart.ToString("X4") })); } }
private void AppendCode(Workspace workspace, MethodBody methodBody, BinaryWriter writer) { foreach (MSILInstruction instruction in methodBody.Instructions) { writer.Write(instruction.OpCode.Bytes); // TODO: support all instructions if (instruction.Operand is string) { uint offset = workspace.GetStream<UserStringsHeap>().GetStringOffset(instruction.Operand as string); offset += 0x70000000; instruction.OperandBytes = BitConverter.GetBytes(offset); } if (instruction.Operand is MetaDataMember) { instruction.OperandBytes = BitConverter.GetBytes((instruction.Operand as MetaDataMember).MetaDataToken); } if (instruction.OperandBytes != null) writer.Write(instruction.OperandBytes); } }
public static MethodBody FromMethod(MethodDefinition methodDef) { MethodBody body = new MethodBody(); body.Method = methodDef; body.reader = new NETMethodReader(methodDef.netheader.assembly.peImage, body); if (body.IsFat) body.reader.ReadFatMethod(); return body; }
public override void LoadCache() { base.LoadCache(); _paramRange = MemberRange.CreateRange<ParameterDefinition>(this, 5, NETHeader.TablesHeap.GetTable(MetaDataTableType.Param, false)); _semantics = Semantics; _body = Body; _genericParameters = GenericParameters; }
private void AppendHeader(MethodBody methodBody, BinaryWriter writer) { MSILInstruction lastInstruction = methodBody.Instructions[methodBody.Instructions.Length - 1]; int codeSize = lastInstruction.Offset + lastInstruction.Size; bool isFat = codeSize > 64 || methodBody.HasExtraSections || methodBody.HasVariables || methodBody.MaxStack > 8; if (isFat) { ushort signature = 0x0003; signature |= (byte)(methodBody.HasExtraSections ? 0x08 : 0x00); signature |= (byte)(methodBody.InitLocals ? 0x10 : 0x00); signature |= (0x03 << 12); writer.Write(signature); writer.Write((ushort)methodBody.MaxStack); // must update writer.Write(codeSize); writer.Write(methodBody.LocalVarSig); // must update } else { byte signature = (byte)(0x02 | (codeSize << 2)); writer.Write(signature); } }
private byte[] SerializeMethodBody(Workspace workspace, MethodBody methodBody) { byte[] bytes = null; using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream)) { AppendHeader(methodBody, writer); AppendCode(workspace, methodBody, writer); ASMGlobals.Align(writer, 4); if (methodBody.HasExtraSections) foreach (MethodBodySection section in methodBody.ExtraSections) AppendSection(section, writer); } bytes = stream.ToArray(); } return bytes; }