CRT初始化 (参考自MSDN)
来源:互联网 发布:淘宝图片轮播图片制作 编辑:程序博客网 时间:2024/05/16 17:32
CRT Initialization
This topic describes how the CRT initializes global states in native code.
By default, the linker includes the CRT library, which provides its own startup code. This startup code initializes the CRT library, calls global initializers, and then calls the user-provided main function for console applications.
Initializing a Global Object
Consider the following code:
int func(void){ return 3;}int gi = func();int main(){ return gi;}
According to the C/C++ standard, func() must be called before main() is executed.But who calls it?
One way to determine this is to set a breakpoint in func(),debug the application,
and examine the stack.This is possible because the CRT source code is included with Visual Studio.
When you browse the functions on the stack, you will find that the CRT is looping through a list of function pointers and calling each one as it encounters them.These functions are either similar to func() or constructors for class instances.
The CRT obtains the list of function pointers from the Visual C++ compiler.When the compiler sees a global initializer, it generates a dynamic initializer in the .CRT$XCU section (where CRT is the section name and XCU is the group name).To obtain a list of those dynamic initializers run the command dumpbin /all main.obj, and then search the .CRT$XCU section (when main.cpp is compiled as a C++ file, not a C file).It will be similar to the following:
SECTION HEADER #6.CRT$XCU name 0 physical address 0 virtual address 4 size of raw data 1F2 file pointer to raw data (000001F2 to 000001F5) 1F6 file pointer to relocation table 0 file pointer to line numbers 1 number of relocations 0 number of line numbers40300040 flags Initialized Data 4 byte align Read OnlyRAW DATA #6 00000000: 00 00 00 00 ....RELOCATIONS #6 Symbol Symbol Offset Type Applied To Index Name -------- ---------------- ----------------- -------- ------ 00000000 DIR32 00000000 C ??__Egi@@YAXXZ (void __cdecl `dynamic initializer for 'gi''(void))
The CRT defines two pointers:
__xc_a in .CRT$XCA
__xc_z in .CRT$XCZ
Both groups do not have any other symbols defined except __xc_a and __xc_z.Now, when the linker reads various .CRT groups, it combines them in one section and orders them alphabetically.This means that the userdefined global initializers (which the Visual C++ compiler puts in .CRT$XCU) will always come after .CRT$XCA and before .CRT$XCZ.
The section will resemble the following:
.CRT$XCA __xc_a.CRT$XCU Pointer to Global Initializer 1 Pointer to Global Initializer 2.CRT$XCZ __xc_z
So, the CRT library uses both __xc_a and __xc_z to determine the start and end of the global initializers list because of the way in which they are laid out in memory after the image is loaded.
- CRT初始化 (参考自MSDN)
- uint(C# 参考)---msdn参考
- msdn之CRT调试
- AOP(转自MSDN)
- new 修饰符(msdn C# 参考)
- 参考MSDN的checked用法(C# 参考)
- 调用dll vc++(转自msdn)
- 线程池(翻译自MSDN)
- TFS如何搁置和取消挂起的搁置(参考自msdn)
- 部署桌面应用程序(VC++)MSDN参考网址
- __cdecl (抄自msdn)
- MSDN 技术资源库参考文档:
- MDX 函数参考 (MDX)--MSDN
- MSDN非常好的参考链接
- 预防SQL注射攻击(译自MSDN)
- .NET 数据访问体系结构指南(转自MSDN,收藏)
- 线程池(Thread Pooling),译自MSDN
- CCeSocket的bug(zz自msdn新闻组)
- Review_2012_11_13_11_14_11_15_11_16
- codeforces 167div2-4
- 二分图最大匹配(匈牙利算法)
- 安裝Drupal 7简体中文版
- 关于MySQL数据库报错的解决办法:
- CRT初始化 (参考自MSDN)
- 从 XML 文件到 MySQL 的转换,调试笔记
- Solve an LP problem in C++ using Gurobi
- 类初始化
- 关于在网页中使用文本编辑器提交不了长篇幅的文章的解决办法:
- 黑马程序员-c#总结
- 如何在网站网页中添加音乐代码
- 心形线方程-Geek献给女友的爱意情人节
- 18个最常用的Win8快捷键