Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net clr method table structure

Tags:

c#

.net

clr

I'm currently reading book titled Pro .NET Performance. One of its chapters contains detailed information about reference types internal structure. Method table is one of the internal fields of reference type layout structure. It is said in this book that method table consists of information about ALL methods of a class. I'm trying to check this theory with a little program

class MyClass
{
    public void M()
    {
    }
}

static void Main(string[] args)
{
     MyClass m = new MyClass();
     m.M();
     Console.ReadLine();
}  

I start this program with WinDbg My WinDbg session looks like the following

!clrstack -a
ConsoleApp.Program.Main(System.String[]) [c:\visual studio 2012\Projects\Algorithms\ConsoleApp\Program.cs @ 36]
    PARAMETERS:
        args (0x00bff274) = 0x02ba2fbc
    LOCALS:
        0x00bff270 = 0x02ba2fd8

0x02ba2fd8 - is the address of MyClass instance What I do next is try to dump MyClass instance

!do 0x02ba2fd8

Name:        ConsoleApp.MyClass
MethodTable: 00f84d74
EEClass:     00f81840
Size:        12(0xc) bytes
File:        C:\visual studio 2012\Projects\Algorithms\ConsoleApp\bin\Debug\ConsoleApp.exe
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
601a4544  4000001        4         System.Int32  1 instance       10 Z

Next step is to dump method table (its address is 00f84d74)

!dumpmt -md 00f84d74

EEClass:         00f81840
Module:          00f83fbc
Name:            ConsoleApp.MyClass
mdToken:         02000002
File:            C:\visual studio 2012\Projects\Algorithms\ConsoleApp\bin\Debug\ConsoleApp.exe
BaseSize:        0xc
ComponentSize:   0x0
Slots in VTable: 6
Number of IFaces in IFaceMap: 0
--------------------------------------
MethodDesc Table
   Entry MethodDe    JIT Name
6005a2c8 5fcf8354 PreJIT System.Object.ToString()
60065600 5fcf835c PreJIT System.Object.Equals(System.Object)
600319b0 5fcf837c PreJIT System.Object.GetHashCode()
600316e8 5fcf8390 PreJIT System.Object.Finalize()
012604c0 00f84d6c    JIT ConsoleApp.MyClass..ctor()
012604f8 00f84d60    JIT ConsoleApp.MyClass.M()

The output from !dumpmt command shows that method table contains entry for M() method. But when I try to dump memory at address 00f84d74

dd  00f84d74

00f84d74  00000200 0000000c 00024188 00000004
00f84d84  601a299c 00f83fbc 00f84db0 00f81840
00f84d94  012604c0 00000000 00f84da0 6005a2c8
00f84da4  60065600 600319b0 600316e8 00000080
00f84db4  00000000 03ba3500 00000000 03ba3504
00f84dc4  00000000 00000000 00000000 00000000
00f84dd4  00000000 00000000 00000000 00000000
00f84de4  00000000 00000000 00000000 00000000

I can't find any references to address of M() method (012604f8)

So the question is whether method table contains references to non-virtual methods? Where are they stored?

like image 251
dmitryo Avatar asked Oct 18 '15 18:10

dmitryo


People also ask

Which of the following 3 components are the internal content of CLR?

The key components of CLR includes the following: Class Loader - Used to load all classes at run time. MSIL to Native code - The Just In Time (JTI) compiler will convert MSIL code into native code. Code Manager - It manages the code at run time.

What is the difference between .NET Framework and CLR?

NET Framework is a software framework that contains huge library of coded solutions to general programming problems, and a Virtual Machine (CLR) that manages the execution of programs. The CLR (Common Language Runtime) is the most vital component of the . NET Framework.

How does .NET Core CLR work?

CLR provides the services and runtime environment to the MSIL code. Internally CLR includes the JIT(Just-In-Time) compiler which converts the MSIL code to machine code which further executed by CPU. CLR also uses the . NET Framework class libraries.


1 Answers

enter image description here

Thanks to one of my colleagues who shed some light to my question. It turns out that method pointers are located at negative offsets relative to method table pointer

like image 130
dmitryo Avatar answered Oct 02 '22 17:10

dmitryo