CRT initialization (refer to self-msdn)

Source: Internet
Author: User

CRT Initialization

This topic describes how the CRT initializes global states in native code.

By default, the linker provided des the CRT library, which provides its own startup code. this startup code initializes the CRT library, callglobal initializers, and then callthe 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 callit?

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 encoded 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. 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. 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 into T _ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.