Microsoft .NET Native Developer Preview 内部初探
准备研究:
安装完毕并启动VS2013,根据MSDN 的.NET Native 文档(http://msdn.microsoft.com/en-us/library/dn600165(v=vs.110).aspx)
我新建项目:
Visual C# -> 应用商店应用 -> Windows 应用程序 -> 网格应用程序(Windows)
新建项目后开启Native 编译;

然后在菜单 [生成] 里面 [活动解决方案平台] 选择 x64,(因为目前的版本仅支持X64和ARM平台);

在 [项目] 菜单的 [生成] 选项卡里 [Compile with .NET Native tool chain] 上打对号;

在 [项目] 菜单的 [调试] 选项卡里选中如下:

ok,编译。。。你会发现编译速度很是慢长....。
然后看看我们的输出目录:

发现比正常的程序多了个[ilc.out]的目录,这个目录里面的程序就是本文的主角。

我们可以直接看到的差别就是 App1.exe 变的很小,5KB,而多了个App1.dll 7MB多;用CFF Explorer打开App1.exe:
查看其引入表:
发现
App1.exe只对App1.dll有引用,且只引用了App1.dll的导出函数RHBinder__ShimExeMain。
用IDA打开 App1.exe 找到入口函数可看到:
这句可以说明App1.exe是个stub,里面什么代码也没有,直接调用App1.dll的导出函数RHBinder__ShimExeMain。
ok,我们直接用IDA打开App1.dll等待分析完毕(dll较大,分析并加载符号较慢):

我们可以看到App1.dll导出了下列函数:(其中就有)RHBinder__ShimExeMain
01.Name Address Ordinal02.---- ------- -------03.$thread_static_index 0000000000AD2DB0 1 04.AppendExceptionStackFrame 000000000076E004 2 05.CheckStaticClassConstruction 0000000000765598 3 06.CreateCommandLine 000000000076ABB8 4 07.CtorCharArray 00000000007734A8 5 08.CtorCharArrayStartLength 0000000000773190 6 09.CtorCharCount 0000000000772A28 7 10.CtorCharPtr 0000000000772EC0 8 11.CtorCharPtrStartLength 0000000000772C1C 9 12.FailFast 0000000000768E1C 10 13.GetRuntimeException 0000000000769084 11 14.RHBinder__ShimExeMain 0000000000426CF0 12 15.RHBinder__DllMain 0000000000426D24自然,RHBinder__ShimExeMain是此程序的入口点。
下面我们用 CFF Explorer 打开App1.dll:

看其引入表:

把对系统dll(api-ms*)的引用排除,剩下:
mrt100_app.dll
App1.dll
也就是其引用了自身导出的:
1.CtorCharArrayStartLength2.CreateCommandLine3.CtorCharPtrStartLength4.CtorCharPtr5.CtorCharCount6.CtorCharArray引用别的dll的函数并不奇怪,但为什么引用自身导出的函数而不是直接调用这个就比较奇怪了,他是怎么实现的呢?因为连接时这个dll并不存在,也就没有这个dll的导出,不知道怎么链接的,以后分析吧。
明显这个是一些支持函数,并不是主要的,主角看来在另一个dll(mrt100_app.dll)身上:
CFF Explorer 打开 mrt100_app.dll:

看见没 Microsoft .NET Native Runtime......
用vc工具 dumpbin /exports mrt100_app.dll 看其导出:

001.1 Microsoft (R) COFF/PE Dumper Version 11.00.61030.0002.2 Copyright (C) Microsoft Corporation. All rights reserved.003.3004.4005.5 Dump of file mrt100_app.dll006.6007.7 File Type: DLL008.8009.9 Section contains the following exports for mrt100.dll010.10011.11 00000000 characteristics012.12 533689BD time date stamp Sat Mar 29 16:52:13 2014013.13 0.00 version014.14 1 ordinal base015.15 277 number of functions016.16 277 number of names017.17018.18 ordinal hint RVA name019.19020.20 1 0 00035DD8 GetRuntimeException021.21 2 1 00036314 ProcessFinalizers022.22 3 2 00001EF8 RegisterCodeManager023.23 4 3 00034DC0 RhBox024.24 5 4 00036760 RhCanUnloadModule025.25 6 5 000367C0 RhCollect026.26 7 6 00002E00 RhEnableShutdownFinalization027.27 8 7 00035E5C RhExceptionHandling_FailedAllocation028.28 9 8 000361B8 RhExceptionHandling_ThrowClasslibArithmeticException029.29 10 9 0003622C RhExceptionHandling_ThrowClasslibDivideByZeroException030.30 11 A 00036144 RhExceptionHandling_ThrowClasslibIndexOutOfRangeException031.31 12 B 000362A0 RhExceptionHandling_ThrowClasslibOverflowException032.32 13 C 00035EDC RhExceptionHandling_ThrowInter033.33 14 D 000360D8 RhExceptionHandling_ThrowIntra034.34 15 E 00003244 RhFindBlob035.35 16 F 00036498 RhGcStress_Initialize036.36 17 10 00034C74 RhGetArrayElementType037.37 18 11 00034C3C RhGetComponentSize038.38 19 12 00034BA8 RhGetCorElementType039.39 20 13 00003808 RhGetCurrentObjSize040.40 21 14 00034B5C RhGetEETypeClassification041.41 22 15 00034B0C RhGetEETypeHash042.42 23 16 00003BAC RhGetExceptionsForCurrentThread043.43 24 17 00003830 RhGetGCNow044.44 25 18 00003618 RhGetGcCollectionCount045.45 26 19 00003668 RhGetGcLatencyMode046.46 27 1A 000368D0 RhGetGcTotalMemory047.47 28 1B 00002CBC RhGetGenericInstantiation048.48 29 1C 00034C10 RhGetInterface049.49 30 1D 00003854 RhGetLastGCDuration050.50 31 1E 0000383C RhGetLastGCStartTime051.51 32 1F 00003114 RhGetLoadedModules052.52 33 20 000037F8 RhGetLohCompactionMode053.53 34 21 00003610 RhGetMaxGcGeneration054.54 35 22 00003EF0 RhGetModuleFileNameAndBaseFromIP055.55 36 23 000031A8 RhGetModuleFromEEType056.56 37 24 00034C40 RhGetNonArrayBaseType057.57 38 25 00034BBC RhGetNullableType058.58 39 26 00034C34 RhGetNumInterfaces059.59 40 27 000033E4 RhGetStaticFieldAddress060.60 41 28 000034C0 RhGetThreadStaticFieldAddress061.61 42 29 000030B0 RhGetValueTypeSize062.62 43 2A 00035144 RhHandleAlloc063.63 44 2B 000350C8 RhHandleAllocDependent064.64 45 2C 0003504C RhHandleAllocVariable065.65 46 2D 000045E8 RhHandleCompareExchangeVariableType066.66 47 2E 00004388 RhHandleFree067.67 48 2F 000043BC RhHandleGet068.68 49 30 000043C0 RhHandleGetDependent069.69 50 31 0000458C RhHandleGetVariableType070.70 51 32 000043FC RhHandleSet071.71 52 33 000043F4 RhHandleSetDependentSecondary072.72 53 34 000045A8 RhHandleSetVariableType073.73 54 35 00034BB0 RhHasReferenceFields074.74 55 36 00002E10 RhHasShutdownStarted075.75 56 37 00034BF4 RhIsArray076.76 57 38 00034BCC RhIsNullable077.77 58 39 000037E8 RhIsPromoted078.78 59 3A 00003680 RhIsServerGc079.79 60 3B 00034BDC RhIsString080.80 61 3C 00034C04 RhIsValueType081.81 62 3D 00034C88 RhMemberwiseClone082.82 63 3E 00034EB4 RhNewArray083.83 64 3F 00034FA8 RhNewObject084.84 65 40 00036870 RhReRegisterForFinalize085.85 66 41 00003694 RhRegisterGcCallout086.86 67 42 00004404 RhRegisterRefCountedHandleCallback087.87 68 43 00035210 RhRethrow088.88 69 44 000017E0 RhSetErrorInfoBuffer089.89 70 45 00003670 RhSetGcLatencyMode090.90 71 46 00003800 RhSetLohCompactionMode091.91 72 47 00002EFC RhSpinWait092.92 73 48 000035E8 RhSuppressFinalize093.93 74 49 00035CC4 RhThrowEx094.94 75 4A 00035D1C RhThrowHwEx095.95 76 4B 00034908 RhTypeCast_AreTypesAssignable096.96 77 4C 00034728 RhTypeCast_AreTypesEquivalent097.97 78 4D 000347D4 RhTypeCast_CheckArrayStore098.98 79 4E 00033914 RhTypeCast_CheckCast099.99 80 4F 000349F4 RhTypeCast_CheckCastArray100.100 81 50 00033ADC RhTypeCast_CheckCastClass101.101 82 51 000344E0 RhTypeCast_CheckCastInterface102.102 83 52 00034A84 RhTypeCast_CheckUnbox103.103 84 53 00034738 RhTypeCast_CheckVectorElemAddr104.104 85 54 00034688 RhTypeCast_IsInstanceOf105.105 86 55 00034610 RhTypeCast_IsInstanceOfArray106.106 87 56 00033B6C RhTypeCast_IsInstanceOfClass107.107 88 57 000346C4 RhTypeCast_IsInstanceOfInterface108.108 89 58 00004D94 RhUnbox109.109 90 59 00003750 RhUnregisterGcCallout110.110 91 5A 000044B8 RhUnregisterRefCountedHandleCallback111.111 92 5B 00003600 RhWaitForPendingFinalizers112.112 93 5C 00002F0C RhYield113.113 94 5D 00036930 RhpAssignRefEDX114.114 95 5E 00036A30 RhpBulkWriteBarrier115.115 96 5F 00036AD0 RhpCheckCctor116.116 97 60 00036960 RhpCheckedAssignRefEDX117.117 98 61 000369A0 RhpCheckedLockCmpXchg118.118 99 62 000369F0 RhpCheckedXchg119.119 100 63 00003E28 RhpClearThreadDoNotTriggerGC120.120 101 64 00004FBC RhpDbl2IntOvf121.121 102 65 00004FE4 RhpDbl2LngOvf122.122 103 66 00005088 RhpDbl2ULng123.123 104 67 0000500C RhpDbl2ULngOvf124.124 105 68 00036B70 RhpDblRemRev125.125 106 69 00036D10 RhpEHJumpByref126.126 107 6A 00036CF0 RhpEHJumpObject127.127 108 6B 00036CD0 RhpEHJumpScalar128.128 109 6C 00005660 RhpETWLogLiveCom129.129 110 6D 00005828 RhpETWShouldWalkCom130.130 111 6E 000351C0 RhpFailFastForPInvokeException131.131 112 6F 0000502C RhpFlt2IntOvf132.132 113 70 00005058 RhpFlt2LngOvf133.133 114 71 00036B30 RhpFltRemRev134.134 115 72 00036CB0 RhpGcPoll135.135 116 73 00036CC0 RhpGcPollStress136.136 117 74 00036F70 RhpGetThread137.137 118 75 00037740 RhpInitialInterfaceDispatch138.138 119 76 00036F90 RhpInterfaceDispatch3139.139 120 77 00037080 RhpInterfaceDispatch36140.140 121 78 00036FB0 RhpInterfaceDispatch4141.141 122 79 00037170 RhpInterfaceDispatch32142.142 123 7A 00036FD0 RhpInterfaceDispatch4143.143 124 7B 00037360 RhpInterfaceDispatch64144.144 125 7C 00037010 RhpInterfaceDispatch8145.145 126 7D 00036DD0 RhpLoopHijack146.146 127 7E 000379F0 RhpNewArray147.147 128 7F 00037900 RhpNewFast148.148 129 80 00037940 RhpNewFinalizable149.149 130 81 00003FCC RhpPInvokeExceptionGuard150.150 131 82 00005C2C RhpRegisterModule151.151 132 83 00037870 RhpResolveInterfaceMethod152.152 133 84 00037CE0 RhpRethrow153.153 134 85 000382B0 RhpReversePInvoke154.154 135 86 00034D30 RhpReversePInvokeBadTransition155.155 136 87 00038420 RhpReversePInvokeReturn156.156 137 88 00003E6C RhpSetThreadDoNotTriggerGC157.157 138 89 00036A70 RhpShutdown158.158 139 8A 000063C4 RhpSuppressGcStress159.159 140 8B 00037BD0 RhpThrowEx160.160 141 8C 00043968 RhpTrapThreads161.161 142 8D 000063C4 RhpUnsuppressGcStress162.162 143 8E 00038260 RhpWaitForGC163.163 144 8F 000381D0 RhpWaitForSuspend164.164 145 90 00001FBC UnregisterCodeManager165.165 146 91 00038454 _copysign166.166 147 92 0003845A _ecvt_s167.167 148 93 00038460 acos168.168 149 94 00038466 acosf169.169 150 95 0003846C asin170.170 151 96 00038472 asinf171.171 152 97 00038478 atan172.172 153 98 0003847E atan2173.173 154 99 00038484 atan2f174.174 155 9A 0003848A atanf175.175 156 9B 00038490 ceil176.176 157 9C 00038496 ceilf177.177 158 9D 0003849C copysign178.178 159 9E 000384A2 copysignf179.179 160 9F 000384A8 cos180.180 161 A0 000384AE cosf181.181 162 A1 000384B4 cosh182.182 163 A2 000384BA coshf183.183 164 A3 000384C0 exp184.184 165 A4 000384C6 expf185.185 166 A5 000384CC floor186.186 167 A6 000384D2 floorf187.187 168 A7 000384D8 fmod188.188 169 A8 000384DE fmodf189.189 170 A9 000384E4 log190.190 171 AA 000384EA log10191.191 172 AB 000384F0 log10f192.192 173 AC 000384F6 logf193.193 174 AD 000384FC memcmp194.194 175 AE 00038502 memcpy195.195 176 AF 00038508 memmove196.196 177 B0 0003850E memset197.197 178 B1 00038514 modf198.198 179 B2 0003851A pow199.199 180 B3 00038520 powf200.200 181 B4 00038526 sin201.201 182 B5 0003852C sinf202.202 183 B6 00038532 sinh203.203 184 B7 00038538 sinhf204.204 185 B8 0003853E sqrt205.205 186 B9 00038544 sqrtf206.206 187 BA 0003BA70 t119207.207 188 BB 0003ACD0 t13208.208 189 BC 0003ACE8 t2209.209 190 BD 0003649C t2.m0210.210 191 BE 00036494 t2.m1211.211 192 BF 0003B2A0 t27212.212 193 C0 0003B2C0 t28213.213 194 C1 0003AF78 t3214.214 195 C2 00036498 t3.m0215.215 196 C3 000364A4 t3.m1216.216 197 C4 0003AD00 t32217.217 198 C5 0003AD78 t35218.218 199 C6 0003ADB8 t37219.219 200 C7 0003ADD8 t38220.220 201 C8 0003ADF8 t39221.221 202 C9 0003AE18 t40222.222 203 CA 0003AE30 t41223.223 204 CB 0003AE80 t44224.224 205 CC 00043010 t44static_data225.225 206 CD 00043018 t45static_data226.226 207 CE 000364A8 t5.m1227.227 208 CF 0003B3A0 t50228.228 209 D0 0003B450 t56229.229 210 D1 00036314 t56.m0230.230 211 D2 0003ACA0 t6231.231 212 D3 0003B530 t67232.232 213 D4 0003B548 t68233.233 214 D5 00043020 t70static_data234.234 215 D6 0003B5A0 t71235.235 216 D7 00036144 t71.m10236.236 217 D8 000360D8 t71.m11237.237 218 D9 00035EDC t71.m12238.238 219 DA 00035E5C t71.m13239.239 220 DB 00035DD8 t71.m14240.240 221 DC 00035D1C t71.m15241.241 222 DD 00035CC4 t71.m16242.242 223 DE 00035210 t71.m17243.243 224 DF 000351C0 t71.m25244.244 225 E0 000362A0 t71.m7245.245 226 E1 0003622C t71.m8246.246 227 E2 000361B8 t71.m9247.247 228 E3 00043028 t71static_data248.248 229 E4 00043000 t71static_gcdata249.249 230 E5 0003AF10 t78250.250 231 E6 0003B6A0 t81251.251 232 E7 0003ACA8 t82252.252 233 E8 0003ACB0 t83253.253 234 E9 0003B6B8 t84254.254 235 EA 0003AF28 t87255.255 236 EB 0003B750 t90256.256 237 EC 00035144 t90.m0257.257 238 ED 000350C8 t90.m1258.258 239 EE 00034C3C t90.m10259.259 240 EF 00034C34 t90.m11260.260 241 F0 00034C10 t90.m12261.261 242 F1 00034C04 t90.m13262.262 243 F2 00034BF4 t90.m14263.263 244 F3 00034BDC t90.m15264.264 245 F4 00034BCC t90.m16265.265 246 F5 00034BBC t90.m17266.266 247 F6 00034BB0 t90.m18267.267 248 F7 00034BA8 t90.m19268.268 249 F8 0003504C t90.m2269.269 250 F9 00034B5C t90.m20270.270 251 FA 00034B0C t90.m21271.271 252 FB 00034FA8 t90.m3272.272 253 FC 00034EB4 t90.m4273.273 254 FD 00034DC0 t90.m5274.274 255 FE 00034D30 t90.m6275.275 256 FF 00034C88 t90.m7276.276 257 100 00034C74 t90.m8277.277 258 101 00034C40 t90.m9278.278 259 102 0003B768 t91279.279 260 103 0003B7E8 t95280.280 261 104 00033B6C t95.m0281.281 262 105 00033ADC t95.m1282.282 263 106 000344E0 t95.m11283.283 264 107 000347D4 t95.m12284.284 265 108 00034738 t95.m13285.285 266 109 00034728 t95.m15286.286 267 10A 00034688 t95.m17287.287 268 10B 00033914 t95.m18288.288 269 10C 00034A84 t95.m2289.289 270 10D 00034610 t95.m3290.290 271 10E 000349F4 t95.m4291.291 272 10F 000346C4 t95.m5292.292 273 110 00034908 t95.m9293.293 274 111 0003854A tan294.294 275 112 00038550 tanf295.295 276 113 00038556 tanh296.296 277 114 0003855C tanhf297.297298.298 Summary299.299300.300 6000 .data301.301 3000 .pdata302.302 9000 .rdata303.303 1000 .reloc304.304 1000 .rsrc305.305 39000 .text306.306 1000 .tls
发现有些函数名称如 t71.m10 貌似是被混淆过呦~~
看其引入:
按照惯例排除(api-ms*)后仅剩下了个msvcr120_app.dll,这个是VC2012 Runtime里面的一个dll,相当于系统dll,就不多分析了。
回头看 App1.dll ,因为是个dll,所以他被加载后首先执行的是其入口点函数 RHBinder__DllMain,而 RHBinder__DllMain 里面没别的代码,仅仅直接调用了 RHBinder__DllProcessAttach:

我们看到其调用了几个类的静态构造函数 cctor.
当dll入口执行完毕后执行的是RHBinder__ShimExeMain


终于看见我们想要看的C#的入口函数$0_App1__Program_Main,很明显在AOT后AOT编译器给入口函数Main重命名了。
相应的托管代码在:C:UsersBinSysDesktopApp1App1objdDebugApp.g.i.cs
01.#if !DISABLE_XAML_GENERATED_MAIN02.public static class Program03.{04.[global::System.CodeDom.Compiler.GeneratedCodeAttribute('Microsoft.Windows.UI.Xaml.Build.Tasks',' 4.0.0.0')]05.[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]06.static void Main(string[] args)07.{08.global::Windows.UI.Xaml.Application.Start((p) => new App());09.}10.}11.#endif是新new 了个App1的对象:



每个托管代码看来都有对象的Native实现哦。
浙公网安备 33010602011771号