Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MethodIL.cs « metadata « Mono.PEToolkit « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63c3be325cff71c1947106c72fbff3fc3b78cb20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (c) 2002 Sergey Chaban <serge@wildwestsoftware.com>
 */

using System;
using System.IO;

namespace Mono.PEToolkit.Metadata {

	/// <remarks>
	/// See Partition II
	/// 24.4 Common Intermediate Language Physical Layout
	/// </remarks>
	public class MethodIL {

		public enum Format {
			// CorILMethod_TinyFormat, 24.4.2
			Tiny = 2,
			// CorILMethod_FatFormat, 24.4.3
			Fat = 3,

			// encoded in 3 bits
			Shift = 3,
			Mask = 0x7
		}

		public enum Flags {
			TinyFormat = MethodIL.Format.Tiny,
			FatFormat = MethodIL.Format.Fat,
			MoreSections = 0x8,
			InitLocals = 0x10
		}

		internal int fatFlags;
		internal int maxStack;

		internal byte[] bytecode;

		public MethodIL()
		{
			fatFlags = 0;
			maxStack = 0;
		}

		public byte [] ByteCode {
			get {
				return bytecode;
			}
		}

		public int CodeSize {
			get {
				return (bytecode != null)
				        ? bytecode.Length : 0;
			}
		}

		public int MaxStack {
			get {
				return maxStack;
			}
			set {
				maxStack = value;
			}
		}

		public bool InitLocals {
			get {
				return (fatFlags & (int)Flags.InitLocals) != 0;
			}
		}

		public bool HasMoreSections {
			get {
				return (fatFlags & (int)Flags.MoreSections) != 0;
			}
		}

		internal static bool IsMethodTiny(int flags)
		{
			return ((Format)(flags & ((int)Format.Mask >> 1)) == Format.Tiny);
		}

		public void Read(BinaryReader reader)
		{
			fatFlags = 0;
			int codeSize;
			bytecode = null;
			int data = reader.ReadByte();
			if (IsMethodTiny(data)) {
				codeSize = data >> ((int)Format.Shift - 1);
				maxStack = 0; // no locals
				bytecode = reader.ReadBytes(codeSize);
			} else {
				long headPos = reader.BaseStream.Position - 1;
				fatFlags = data | (reader.ReadByte() << 8);
				// first 12 bits are flags
				// next 4 bits is the
				// "size of this header expressed as the count
				// of 4-byte integers occupied"
				int headSize = ((fatFlags >> 12) & 0xF) << 2;
				fatFlags &= 0xFFF;
				maxStack = reader.ReadInt16();
				codeSize = reader.ReadInt32();
				int localTok = reader.ReadInt32();
				reader.BaseStream.Position = headPos + headSize;
				bytecode = reader.ReadBytes(codeSize);
			}
		}

		public virtual void Dump(TextWriter writer)
		{
			string dump = String.Format(
				"Code size    : {0:x4}" + Environment.NewLine + 
				"MaxStack     : {1:x4}" + Environment.NewLine + 
				"InitLocals   : {2}" + Environment.NewLine + 
				"MoreSections : {3}" + Environment.NewLine,
				CodeSize, MaxStack, InitLocals, HasMoreSections
			);
			writer.Write(dump);
		}

		public void DumpHexBytecode(TextWriter w)
		{
			int n = CodeSize >> 3;
			int i = 0;
			for (int x = n; --x >= 0; i += 8) {
				w.WriteLine(
					String.Format("{0:x2} {1:x2} {2:x2} {3:x2} {4:x2} {5:x2} {6:x2} {7:x2}",
						ByteCode[i], ByteCode[i + 1], ByteCode[i + 2], ByteCode[i + 3],
						ByteCode[i + 4], ByteCode[i + 5], ByteCode[i + 6], ByteCode[i + 7]
					)
				);
			}
			for (;i < CodeSize; i++) {
				w.Write("{0:x2} ", ByteCode[i]);
			}
			w.WriteLine();
		}

		public override string ToString()
		{
			StringWriter sw = new StringWriter();
			Dump(sw);
			return sw.ToString();
		}
	}
}