2
Copyright (C) 2009 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
25
using System.Collections.Generic;
26
using IKVM.Reflection.Reader;
29
namespace IKVM.Reflection
31
public sealed class MethodBody
33
private readonly IList<ExceptionHandlingClause> exceptionClauses;
34
private readonly IList<LocalVariableInfo> locals;
35
private readonly bool initLocals;
36
private readonly int maxStack;
37
private readonly int localVarSigTok;
40
internal MethodBody(ModuleReader module, int rva, IGenericContext context)
42
const byte CorILMethod_TinyFormat = 0x02;
43
const byte CorILMethod_FatFormat = 0x03;
44
const byte CorILMethod_MoreSects = 0x08;
45
const byte CorILMethod_InitLocals = 0x10;
46
const byte CorILMethod_Sect_EHTable = 0x01;
47
const byte CorILMethod_Sect_FatFormat = 0x40;
48
const byte CorILMethod_Sect_MoreSects = 0x80;
50
List<ExceptionHandlingClause> exceptionClauses = new List<ExceptionHandlingClause>();
51
List<LocalVariableInfo> locals = new List<LocalVariableInfo>();
53
BinaryReader br = new BinaryReader(module.stream);
54
byte b = br.ReadByte();
55
if ((b & 3) == CorILMethod_TinyFormat)
58
body = br.ReadBytes(b >> 2);
61
else if ((b & 3) == CorILMethod_FatFormat)
63
initLocals = (b & CorILMethod_InitLocals) != 0;
64
short flagsAndSize = (short)(b | (br.ReadByte() << 8));
65
if ((flagsAndSize >> 12) != 3)
67
throw new BadImageFormatException("Fat format method header size should be 3");
69
maxStack = br.ReadUInt16();
70
int codeLength = br.ReadInt32();
71
localVarSigTok = br.ReadInt32();
72
body = br.ReadBytes(codeLength);
73
if ((b & CorILMethod_MoreSects) != 0)
75
module.stream.Position = (module.stream.Position + 3) & ~3;
76
int hdr = br.ReadInt32();
77
if ((hdr & CorILMethod_Sect_MoreSects) != 0 || (hdr & CorILMethod_Sect_EHTable) == 0)
79
throw new NotImplementedException();
81
else if ((hdr & CorILMethod_Sect_FatFormat) != 0)
83
int count = ComputeExceptionCount((hdr >> 8) & 0xFFFFFF, 24);
84
for (int i = 0; i < count; i++)
86
int flags = br.ReadInt32();
87
int tryOffset = br.ReadInt32();
88
int tryLength = br.ReadInt32();
89
int handlerOffset = br.ReadInt32();
90
int handlerLength = br.ReadInt32();
91
int classTokenOrFilterOffset = br.ReadInt32();
92
exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
97
int count = ComputeExceptionCount((hdr >> 8) & 0xFF, 12);
98
for (int i = 0; i < count; i++)
100
int flags = br.ReadUInt16();
101
int tryOffset = br.ReadUInt16();
102
int tryLength = br.ReadByte();
103
int handlerOffset = br.ReadUInt16();
104
int handlerLength = br.ReadByte();
105
int classTokenOrFilterOffset = br.ReadInt32();
106
exceptionClauses.Add(new ExceptionHandlingClause(module, flags, tryOffset, tryLength, handlerOffset, handlerLength, classTokenOrFilterOffset, context));
110
if (localVarSigTok != 0)
112
ByteReader sig = module.GetStandAloneSig((localVarSigTok & 0xFFFFFF) - 1);
113
Signature.ReadLocalVarSig(module, sig, context, locals);
118
throw new BadImageFormatException();
120
this.exceptionClauses = exceptionClauses.AsReadOnly();
121
this.locals = locals.AsReadOnly();
124
private static int ComputeExceptionCount(int size, int itemLength)
126
// LAMESPEC according to the spec, the count should be calculated as "(size - 4) / itemLength",
127
// FXBUG but to workaround a VB compiler bug that specifies the size incorrectly,
128
// we do a truncating division instead.
129
return size / itemLength;
132
public IList<ExceptionHandlingClause> ExceptionHandlingClauses
134
get { return exceptionClauses; }
137
public bool InitLocals
139
get { return initLocals; }
142
public IList<LocalVariableInfo> LocalVariables
144
get { return locals; }
147
public byte[] GetILAsByteArray()
152
public int LocalSignatureMetadataToken
154
get { return localVarSigTok; }
157
public int MaxStackSize
159
get { return maxStack; }