Compiler link to start code crtexe. c

Source: Internet
Author: User

Have you ever wondered why sometimes the main () function is the entry address and the winmain () function is the entry address in Win32, in fact, it is set when the compiler dynamically links to CRT [C Runtime Library (part of the C standard library )].

The Program sets the breakpoint. Let's look at the crtexe. C code.

The first static int _ cdecl pre_c_init (void)

* The code in maincrtstartup that was executed before executing C initializers was shifted in this function. Also this funciton is the first thing that is executed in C init section.

Not to mention in detail, the comments from foreigners are awesome.

Static int _ cdecl pre_c_init (void) {/** determine if this is a Managed Application */managedapp = check_managed_app (); /** SET _ app_type properly */# ifdef _ winmain _ // use the winmain macro to set whether the application is a GUI program or a console program _ set_app_type (_ gui_app ); # else/* _ winmain _ */_ set_app_type (_ console_app ); # endif/* _ winmain _ * // ** mark this module as an EXE file so that atexit/_ onexit * will do the right thing when called, Including for C ++ * D-tors. */_ onexitbegin = _ onexitend = (_ pvfv *) _ encode_pointer (_ pvfv *) (-1 )); /** propogate the _ fmode and _ commode variables to the DLL */* _ imp1__fmode = _ fmode; * _ imp1__commode = _ commode; # ifdef _ m_ix86/** set the local copy of the Pentium fdiv adjustment flag */_ adjust_fdiv = * _ imp ___ adjust_fdiv; # endif/* _ m_ix86 * // ** run the RTC initialization code for this DLL */# Ifdef _ RTC _ rtc_initialize (); # endif/* _ RTC * // ** call _ setargv (), which will trigger a call to _ setargv () if * setargv. OBJ is linked with the exe. if setargv. OBJ is not * linked with the EXE, a dummy _ setargv () will be called. */# ifdef wprflag _ wsetargv (); # else/* wprflag */_ setargv (); # endif/* wprflag * // ** if the user has supplied a _ matherr routine then set * _ pusermatherr to Point To it. */If (! _ Defamatmatherr) _ setusermatherr (_ matherr); # ifdef _ m_ix86 _ setdefaultprecision (); # endif/* _ m_ix86 * // * enable per-thread locale if user asked for it */If (_ globallocalestatus =-1) {_ configthreadlocale (-1);} return 0 ;}

This function is called in crt0dat. C. For details, skip

Second Function

static void __cdecl pre_cpp_init(void)
Static void _ cdecl pre_cpp_init (void) {# ifdef _ RTC atexit (_ rtc_terminate ); // The cleaner when the registration program exits # endif/* _ RTC * // ** get the arguments for the call to main. note This Must Be * done explicitly, rather than as part of the DLL's * initialization, to implement optional expansion of wild * card chars in Filename ARGs */startinfo. newmode = _ newmode; # ifdef wprflag argret = _ wgetmainargs (& argc, & argv, & envp, _ dowildcard, & startinfo ); # else/* wprflag */argret = _ getmainargs (& argc, & argv, & envp, _ dowildcard, & startinfo ); # endif/* wprflag */# ifndef _ syscrt if (argret <0) _ amsg_exit (_ rt_spacearg); # endif/* _ syscrt */}

Below is the first entry to the program

staticint__tmainCRTStartup(         void         );#ifdef _WINMAIN_#ifdef WPRFLAGint wWinMainCRTStartup(#else  /* WPRFLAG */int WinMainCRTStartup(#endif  /* WPRFLAG */#else  /* _WINMAIN_ */#ifdef WPRFLAGint wmainCRTStartup(#else  /* WPRFLAG */int mainCRTStartup(#endif  /* WPRFLAG */#endif  /* _WINMAIN_ */        void        ){        /*         * The /GS security cookie must be initialized before any exception         * handling targetting the current image is registered.  No function         * using exception handling can be called in the current image until         * after __security_init_cookie has been called.         */        __security_init_cookie();        return __tmainCRTStartup();}


/***
* Maincrtstartup (void)
* Wmaincrtstartup (void)
* Winmaincrtstartup (void)
* Wwinmaincrtstartup (void)
*
* Purpose:
* These routines do the C Runtime initialization, call the appropriate
* User entry function, and handle termination cleanup. For a managed
* App, they then return the exit code back to the calling routine, which
* Is the managed startup Code. For an unmanaged app, they call exit and
* Never return.
*
* Function: user entry called:
* Maincrtstartup main
* Wmaincrtstartup wmain
* Winmaincrtstartup winmain
* Wwinmaincrtstartup wwinmain
Find the entry address for the program. The specific judgment is in function _ tmaincrtstartup ().

Key code:

#ifdef WPRFLAG            mainret = wWinMain(#else  /* WPRFLAG */            mainret = WinMain(#endif  /* WPRFLAG */                       (HINSTANCE)&__ImageBase,                       NULL,                       lpszCommandLine,                       StartupInfo.dwFlags & STARTF_USESHOWWINDOW                        ? StartupInfo.wShowWindow                        : SW_SHOWDEFAULT                      );#else  /* _WINMAIN_ */#ifdef WPRFLAG            __winitenv = envp;            mainret = wmain(argc, argv, envp);#else  /* WPRFLAG */            __initenv = envp;            mainret = main(argc, argv, envp);#endif  /* WPRFLAG */#endif  /* _WINMAIN_ */

We can see that we can judge whether it is wwinmain () or winmain () based on the macro wprflag, and judge whether the entry function is main (), wmain () or winmain () based on _ winmain (), wwinmain ()

Wmain () and wwinmain () are Unicode, while main () and winmain () are ANSI. However, the macro wprflag is not defined in the program, study in detail when you are free

Then find the corresponding entry program for execution.

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.