一:看下面一些概念

1MethodTable

MethodTable可以说在CLR里面无处不在,这个东西主要是作为对象的数据类型存在,主要包含了EEClass

模块地址,类型名称,模块路径等。

2.EEClass 

EEclass描述了实例对象和类型里面的函数描述符起始块地址,起始块以MethodDescChunk描述。EEclass

成员变量m_pChunks存放了起始块的地址。

3.MethodDesc

看这个名称就知道,它是函数描述符。主要包含了函数名称,函数所述模块,函数是否被jit编译等属性。

4.FixUpPreCode

从字面理解修理预代码,其实它也是这个功能。就是它描述了托管函数被编译前和编译后的变化。编译前

会调用非托管PrecodeFixupThunk函数,编译后直接跳到编译的结果。

二:它们是如何组织的

MethodTable会通过CLR生成实例化对象。在这个生成的过程中,会初始化EEClass。然后会给MethodDescChunks

以及MethodDesc分配相应的地址,MethodDesc跟MethodDescChunks地址是连接在一起的,类似于MethodDescChunks

->MethodDes(1)->MehtodDesc(2)->MethodDesc(3)...... 这种形式。下面就是把MethodDescChunks当MethodDesc分配

完成的时候,会遍历MethodDesc,没遍历一个,就会出初始化一个FixUpPreCode结构体。用以描述函数没被编译之前

的状态。他们的关联方式如下图所示:

三:CLR是如何被运行出来的

作为一个.Net 程式核心部分,这一块是一个重点也是一个难点

主要的步骤有以下几步:

1.CLR会加载当前项目bin/debug/文件夹下面的【目名称.ConsoleApp15.runtimeconfig.json】的这个文件,以读取runtime 的配置

2.CLR 会获取当前runtime运行时的指针

3.通过runtime运行时指针函数,传递进去需要加载的模块,类名称,以及函数名称,获取到需要调用的函数指针

4.获取到函数指针之后,就可以释放掉上面运行步骤所占用的内存

5.调用获取到的函数指针调用函数,比如Main函数,这个是程式入口。

到了这一步不多说了,因为所有的.net程序都是从Main函数开始的。这样流程就会被C#代码所接管。

四:代码是如何构造的

由于CLR代码长达几十万行甚至几百万行,所以无法一一展示。

这里取一部分看看是如何执行的

1.MethodTable的构建过程(MethodTablebuilder.cpp 12163行)

    MethodTableBuilder builder(
NULL,
pClass,
&GetThread()->m_MarshalAlloc,
pamTracker); pMT = builder.BuildMethodTableThrowing(
pAllocator,
pLoaderModule,
pModule,
cl,
pInterfaceBuildInfo,
pLayoutRawFieldInfos,
pParentMethodTable,
&genericsInfo,
parentInst,
(WORD)cInterfaces); END_SO_INTOLERANT_CODE;
RETURN(TypeHandle(pMT));

2.EEclass构建过程(methodtablebuilder.cpp 11957行)

    EEClass * pClass = MethodTableBuilder::CreateClass(
pModule,
cl,
fHasLayout,
fIsDelegate,
fIsEnum,
&genericsInfo,
pAllocator,
pamTracker);

3.MethodDescchunks和MethodDesc的构建过程是在buildmethodtablethrowing函数里面调用    AllocAndInitMethodDescs();,先看看    AllocAndInitMethodDescs();函数(methodtablebuilder.cpp 1666行)

VOID MethodTableBuilder::AllocAndInitMethodDescs()
{
STANDARD_VM_CONTRACT;
if (sizeOfMethodDescs != 0)
{
AllocAndInitMethodDescChunk(startIndex, NumDeclaredMethods() - startIndex, sizeOfMethodDescs);
}
}

4.继续看 AllocAndInitMethodDescChunk函数(methodtablebuilder.cpp 6836行),很明显看到了里面构建了methoddescchunks和methoddesc。

VOID MethodTableBuilder::AllocAndInitMethodDescChunk(COUNT_T startIndex, COUNT_T count, SIZE_T sizeOfMethodDescs)
{
CONTRACTL {
STANDARD_VM_CHECK;
PRECONDITION(sizeOfMethodDescs <= MethodDescChunk::MaxSizeOfMethodDescs);
} CONTRACTL_END; void * pMem = GetMemTracker()->Track(
GetLoaderAllocator()->GetHighFrequencyHeap()->AllocMem(S_SIZE_T(sizeof(TADDR) + sizeof(MethodDescChunk) + sizeOfMethodDescs))); // Skip pointer to temporary entrypoints
MethodDescChunk * pChunk = (MethodDescChunk *)((BYTE*)pMem + sizeof(TADDR)); COUNT_T methodDescCount = 0; SIZE_T offset = sizeof(MethodDescChunk); #ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:22019) // Suppress PREFast warning about integer underflow
#endif // _PREFAST_
for (COUNT_T i = 0; i < count; i++)
#ifdef _PREFAST_
#pragma warning(pop)
#endif // _PREFAST_ {
bmtMDMethod * pMDMethod = (*bmtMethod)[static_cast<SLOT_INDEX>(startIndex + i)]; MethodDesc * pMD = (MethodDesc *)((BYTE *)pChunk + offset); pMD->SetChunkIndex(pChunk); InitNewMethodDesc(pMDMethod, pMD); #ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:22018) // Suppress PREFast warning about integer underflow
#endif // _PREFAST_
offset += pMD->SizeOf();
#ifdef _PREFAST_
#pragma warning(pop)
#endif // _PREFAST_ methodDescCount++; // If we're a value class, we want to create duplicate slots
// and MethodDescs for all methods in the vtable
// section (i.e. not non-virtual instance methods or statics).
// In the name of uniformity it would be much nicer
// if we created _all_ value class BoxedEntryPointStubs at this point.
// However, non-virtual instance methods only require unboxing
// stubs in the rare case that we create a delegate to such a
// method, and thus it would be inefficient to create them on
// loading: after all typical structs will have many non-virtual
// instance methods.
//
// Unboxing stubs for non-virtual instance methods are created
// in code:MethodDesc::FindOrCreateAssociatedMethodDesc. if (NeedsTightlyBoundUnboxingStub(pMDMethod))
{
MethodDesc * pUnboxedMD = (MethodDesc *)((BYTE *)pChunk + offset); //////////////////////////////////
// Initialize the new MethodDesc // <NICE> memcpy operations on data structures like MethodDescs are extremely fragile
// and should not be used. We should go to the effort of having proper constructors
// in the MethodDesc class. </NICE> memcpy(pUnboxedMD, pMD, pMD->SizeOf()); // Reset the chunk index
pUnboxedMD->SetChunkIndex(pChunk); if (bmtGenerics->GetNumGenericArgs() == 0) {
pUnboxedMD->SetHasNonVtableSlot();
} //////////////////////////////////////////////////////////
// Modify the original MethodDesc to be an unboxing stub pMD->SetIsUnboxingStub(); ////////////////////////////////////////////////////////////////////
// Add the new MethodDesc to the non-virtual portion of the vtable if (!bmtVT->AddUnboxedMethod(pMDMethod))
BuildMethodTableThrowException(IDS_CLASSLOAD_TOO_MANY_METHODS); pUnboxedMD->SetSlot(pMDMethod->GetUnboxedSlotIndex());
pMDMethod->SetUnboxedMethodDesc(pUnboxedMD); offset += pUnboxedMD->SizeOf();
methodDescCount++;
}
}
_ASSERTE(offset == sizeof(MethodDescChunk) + sizeOfMethodDescs); pChunk->SetSizeAndCount((ULONG)sizeOfMethodDescs, methodDescCount); GetHalfBakedClass()->AddChunk(pChunk);
}

5.关于构建fixupprecode (methodtablebuilder.cpp 10497行)

   {
for (MethodDescChunk *pChunk = GetHalfBakedClass()->GetChunks(); pChunk != NULL; pChunk = pChunk->GetNextChunk())
{
// Make sure that temporary entrypoints are create for methods. NGEN uses temporary
// entrypoints as surrogate keys for precodes.
pChunk->EnsureTemporaryEntryPointsCreated(GetLoaderAllocator(), GetMemTracker());
}
}

CLR里的MethodTable,MethodDescChunk,MethodDesc,FixUpPreCode都是什么意思的更多相关文章

  1. Excel2010表格里设置每页打印时都有表头

    在打印Excel表格时常常会出现如果存在多页打印时,往往从第二页开始就会出现没有表头的情况,导致到后面都不清楚对应的是哪个数据,查看时也很麻烦,下面就将为大家介绍如何在Excel表格里设置每页打印时都 ...

  2. Java 里 如何使用Base64,网上都是废物的说法

    百度搜索Java里如何使用Base64,结果很多文章都是让引用第三方Jar包,我靠我想了一下 他妈的Java里连这个都不提供,就直接忽略里那些废物的文章.继续搜索,算是找到答案: Java8以后 官方 ...

  3. Poseidon 系统是一个日志搜索平台——认证看链接ppt,本质是索引的倒排列表和原始日志数据都存在HDFS,而文档和倒排的元数据都在NOSQL里,同时针对单个filed都使用了独立索引,使用MR来索引和搜索

    Poseidon 系统是一个日志搜索平台,可以在百万亿条.100PB 大小的日志数据中快速分析和检索.360 公司是一个安全公司,在追踪 APT(高级持续威胁)事件,经常需要在海量的历史日志数据中检索 ...

  4. 为什么mvc里面的ModelState.IsValid一只都是true

    http://zhidao.baidu.com/link?url=H69JQBpF8vbJEOUUc1RCjRZZ05gSGn6PiPL740aGgR3qIfFTT__pt4KgEg7O47lReYR ...

  5. 关于微信里wx.getUserInfo获取用户信息都是拼音的转成中文方法

    加一个参数:lang:"zh_CN" 就可以了  1.  加在js里面 wx.getUserInfo({ lang:"zh_CN", success: func ...

  6. 用Notepad++在文本文件里快速在每行头尾都加上指定的内容(转载)

  7. 揭示同步块索引(上):从lock开始

    转自:http://www.cnblogs.com/yuyijq/archive/2009/03/13/1410071.html 大家都知道引用类型对象除实例字段的开销外,还有两个字段的开销:类型指针 ...

  8. VS工程里的文件都是啥?如何打包? 2015-03-04

    打完补充:以下内容全部是我一家之言,只是愿意分享,内容如有不妥还请见谅. ====================================================== 刚才接收了一份代 ...

  9. m个苹果放在n个筐里,每个筐至少一个,所有的筐都一样,有多少种放法

    package com.study; import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...

随机推荐

  1. JMM 最最最核心的概念:Happens-before 原则

    关于 Happens-before,<Java 并发编程的艺术>书中是这样介绍的: Happens-before 是 JMM 最核心的概念.对应 Java 程序员来说,理解 Happens ...

  2. [LeetCode每日一题]81. 搜索旋转排序数组 II

    [LeetCode每日一题]81. 搜索旋转排序数组 II 问题 已知存在一个按非降序排列的整数数组 nums ,数组中的值不必互不相同. 在传递给函数之前,nums 在预先未知的某个下标 k(0 & ...

  3. 在IDEA配置tomcat

    springboot的项目写多了,导致都快忘记怎么在idea中配置tomcat 点击加号,而不是Templates 选择Tomcat 服务器的Local 服务器配置 部署配置,选择Artifact 到 ...

  4. Android LayoutInflater.inflate源码解析

    一年多以前看过源码,感觉了解比较透彻了,长时间不经大脑思考,靠曾经总结的经验使用inflate方法,突然发现不知道什么时候忘记其中的原理了,上网查了一些资料,还各有不同,反而把我搞糊涂了,还是自己看源 ...

  5. Java常用类详解

    目录 1. String类 1.1 String的特性 1.2 String字面量赋值的内存理解 1.3 String new方式赋值的内存理解 1.4 String 拼接字面量和变量的方式赋值 1. ...

  6. sscanf的应用

    1.提取字符串 2.提取指定长度的字符串 3.提取指定字符为止的字符串 4.取仅包含指定字符集的字符串 5.取到指定字符集为止的字符串 #include <stdio.h> int mai ...

  7. 基于虹软人脸识别,实现RTMP直播推流追踪视频中所有人脸信息(C#)

    前言 大家应该都知道几个很常见的例子,比如在张学友的演唱会,在安检通道检票时,通过人像识别系统成功识别捉了好多在逃人员,被称为逃犯克星:人行横道不遵守交通规则闯红灯的路人被人脸识别系统抓拍放在大屏上以 ...

  8. CentOS7启动SSH服务报:Job for ssh.service failed because the control process exited with error code

    CentOS7启动SSH服务报:Job for ssh.service failed because the control process exited with error code....... ...

  9. 更新索引库: $locate string 寻找包含有string的路径: $updatedb

    更新索引库: $locate string 寻找包含有string的路径: $updatedb 与find不同,locate并不是实时查找.你需要更新数据库,以获得最新的文件索引信息.

  10. linux系统开机自动挂载光驱 和 fstab文件详解

    Linux 通过 UUID 在 fstab 中自动挂载分区 summerm6关注 2019.10.17 16:29:00字数 1,542阅读 607 https://xiexianbin.cn/lin ...