Disassembling .Net Notes: Method Signatures Part 1

by Jason Haley 13. October 2008 17:03

Method Signatures

When looking at the Ecma spec, Partition II, you will see there is a MethodDefSig and a MethodRefSig.

MethodDefSig: is a signature pointed to by the Method table's Signature column.  The signature will be for either a method or a global function.

MethodRefSig: is a signature pointed to by the MemberRef table's Signature column.  The signatures pointed to from the MemberRef table can be either a Field[Ref] or a MethodRef signature - you need to check the calling convention to know which it is.

The diagram below shows the MethodDef's definition:

image

From Ecma-335, Partition II page 155:

HASTHIS = 0x20, used to encode the keyword instance in the calling convention

EXPLICITTHIS = 0x40, used to encode the keyword explicit in the calling convention

DEFAULT = 0x0, used to encode the keyword default in the calling convention

VARARG = 0x5, used to encode the keyword vararg in the calling convention

GENERIC = 0x10, used to indicate that the method has one or more generic parameters.

The first byte of the Signature holds bits for HASTHIS, EXPLICITTHIS and calling convention (DEFAULT, VARARG,
or GENERIC). These are ORed together.

GenParamCount is the number of generic parameters for the method. This is a compressed int32. [Note: For
generic methods, both MethodDef and MemberRef shall include the GENERIC calling convention, together with
GenParamCount; these are significant for binding—they enable the CLI to overload on generic methods by the
number of generic parameters they include. end note]

ParamCount is an integer that holds the number of parameters (0 or more). It can be any number between 0
and 0x1FFFFFFF. The compiler compresses it too (see Partition II Metadata Validation) – before storing into
the 'blob' (ParamCount counts just the method parameters – it does not include the method’s return type)

The RetType item describes the type of the method’s return value

The Param item describes the type of each of the method’s parameters. There shall be ParamCount instances
of the Param item

 

Example 28: Decoding a method signature that is an instance method with no parameters and void return type

This is about the simplest method signature to decode.

C# code:

private void method0ParamVoidRet()
{

}

Method information from ILDasm:

image

0x20 = IMAGE_CEE_CS_CALLCONV_EXPLICITTHIS (HasThis) and IMAGE_CEE_CS_CALLCONV_DEFAULT (Default)

0x00 = Parameter Count

0x01 = ELEMENT_TYPE_VOID

This tells us the method is an instance method that takes no parameters and returns void (would be a Sub in Visual Basic).

Next notes will provide:

  1. Additional examples of Method signatures with varying parameters, return types, generic and varargs.

 

Comments (2) | Post RSSRSS comment feed |

Categories:
Tags:

Comments

Comments are closed