Disassembly and Analysis of the Startup Process of C program

Source: Internet
Author: User
0x01 tool preparation

1. The simplest C code is one,

Int main (){

Return 0 ;}

2. ollydbg

3. VC ++ 6.0

4. GCC (mingw)

0x02 code analysis

Int main ()

{

Return 0;

}

Add the-nostdlib compilation option under GCC, that is, the linker does not link to the standard library, and the following error message is displayed:

 

D: \ backup \ My document \ SRC> GCC main. C-nostdlib-O main.exe

C: \ release E ~ 1 \ admini ~ 1 \ locals ~ 1 \ temp \ ccmsu3wr. O: Main. c :(. Text + 0x9): Undefined re

Ference to '_ main'

Collect2.exe: Error: LD returned 1 exit status

 

For the-nostdlib compilation option, only the items specified by the command line are passed to the linker. Neither the standard Startup File nor library is passed to the linker. This option is implicitly enabled with options-nostartfiles and-nodefaultlibs. This option can also be used to write -- no-standard-libraries.

After GCC executes the Assembly, when only option-nostartfiles is enabled in the link section, the result is normal and no error message is displayed. Many error messages are prompted in the-nodefaultlibs option.

The main function depends on some system standard library files. Some functions are required during the link, for example, pre_cpp_init, check_managed_app, pre_c_init, _ tmaincrtstartup, _ interlockedcompareexchangepointer, duplicate_ppstrings, winmaincrtstartup, maincrtstartup, _ timeout ....

The _ main in the Assembly is the main in the C language, because the assembler and the C compiler name the symbol differently by an underscore.

The linker searches for the _ start symbol in the system standard library file, similar to the/lib/crt2.o file, and then creates the heap object and stack in _ start, open the device provided in advance, pass the argv and argc parameters into the main function, and then call the main function.

0x03 disassembly Analysis of VC Main Function

1: int main ()

2 :{

00401010 push EBP // save EBP on the stack

00401011 mov EBP, esp // The reference point for giving the current position of the stack to EBP to store values in the stack structure

00401013 sub ESP, 40 h // allocate space

00401016 push EBX // Save the data segment Value

00401017 push ESI // source address pointer

00401018 push EDI // target address pointer

00401019 Lea EDI, [ebp-40h] // load a valid address to get pointers to local variables and function parameters. Here the [ebp-40h] is the base address and then shifted down to 40 h, that is, the starting address of the space previously mentioned for the local variable; load this value into the EDI register to get the address of the local variable

0040101c mov ECx, 10 h // store 10 h in the ECX register

00401021 mov eax, 0 cccccccch

00401026 rep STOs dword ptr [EDI] // initialize the local variable space, DS: [EDI]

3: Return 0;

00401028 XOR eax, eax

4 :}

0040102a pop EDI // restore all register values

0040102b pop ESI

0040102c pop EBX

0040102d mov ESP, EBP // restore Stack

0040102f pop EBP

00401030 RET // return to the source EIP address

 

Check the call stack by VC. You can see that the maincrtstartup function is also started before the main function. This function is the multi-byte encoding startup function in the Console environment. The maincrtstartup function is called at address 7c816fd7 in kernel32.dll.

Main () Line 2

Maincrtstartup () line 206 + 25 bytes

Kernel32! 7c816fd7 ()

0x04 ollydbg disassembly Analysis


Od loading ,.


Stack window.


Through the stack, we can see that kelnel32 calls the entry function (maincrtstartup). For OD, the main function is not an entry point, but a maincrtstartup function.

Go one step until 00401146. od analysis calls the getversion function to obtain the version number of the current running platform. Because it is a console program, the system obtains the version number of MS-dos.

Continue to the single step, one step to 0040119e, one step into, you can see that heapcreate applied for heap space function, the size is determined by the passed parameters, and the call contains heapdestroy destroy heap function. Therefore, 0040119e is used to initialize the heap space ,.


At 004011c0, the OD analysis is the getcommandlinea function, which obtains the first address of the command line parameter information.

After entering the call below, you can see the getenvironmentstringsw and getenvironmentstrings functions to get the first address of the environment variable ,. Return to the register and stack in Unicode encoding form, and use the widechartomultibyte function to convert the Unicode string to a multi-byte string,

In addition, some functions for Parameter Analysis and environment variable information are analyzed to obtain the parameters required by the main function. Then, at the location of 00402d4b, the parameters are uploaded to the main function, to execute the content in the main function.

0x05 postscript

I recently read c ++ disassembly and reverse analysis technology secrets, and read chapter 3 to understand the startup function. When I find the user entry, the system has to prepare for the main function, in addition, the C language program entry function learned last semester is not a main function, but a _ start function, which leads to some thinking about what happened to the compiler during program compilation and system execution, therefore, we want to perform some analysis on instances. In the process of thinking, some involve the compiler knowledge, including how it works, and how it is linked after compilation. This part of content is not very familiar, in this aspect, you must master the compilation principles, and learn the relevant content of the compiler. Those things have not been learned, so there are some shortcomings. I have a better understanding of some of the content of disassembly, and can have a deeper understanding of some of the relatively underlying things, including data exchange between stacks, stacks, and registers. In addition, IDA is not used as an artifact. It is better to use IDA to analyze some functions statically.


Disassembly and Analysis of the Startup Process of C program

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.