Review the compilation process II from helloworld

Source: Internet
Author: User

For the sake of simplicity, the examples in this article will not use standard library functions such as printf. In this article, we only use functions defined by ourselves, in the following example, verification and debugging are mainly performed in the Linux environment. It doesn't matter if there is no Linux development environment. This article has copied all the debugging results in the Linux environment, reading should not be affected.
The following are the three files used in this article:
// File 1: numbench. h

 
Int add (INT in1, int in2 );

// File 2: numbench. c

 
# Include "numbench. H" int add (INT in1, int in2) {return in1 + in2 ;}

// File 3: Hello. c

 
// Hello. C # include "numparts. H "# define num_1 10 const int num_2 = 15; int main () {// will call add 3 + Add (num_1, num_2); Return 0 ;}

I. Preprocessing:
 
1. Run the following command to pre-compile the above files. Note that the compilation process, including pre-compilation, is based on each source file (that is. C ,. CPP files ):
Run the following two commands to pre-compile the source files hello. C and numbench. C,
# Gcc-e hello. C-O hello. I
# Gcc-e numbench. C-o numbench. I

After executing the preceding two commands, you can see that the numbench. I and hello. I pre-compiled files have been generated under the directory. Open these two files and you can see the following content:
The content of the hello. I file is:
#1 "Hello. c"
#1 "<built-in>"
#1 "<command-line>"
#1 "Hello. c"

#1 "numbench. H" 1

Int add (INT in1, int in2 );
#3 "Hello. c" 2

Const int num_2 = 15;

Int main ()
{

3 + Add (10, num_2 );
Return 0;
}

The content of the numbench. I file is:
#1 "numbench. c"
#1 "<built-in>"
#1 "<command-line>"
#1 "numbench. c"

#1 "numbench. H" 1

Int add (INT in1, int in2 );
#3 "numbench. c" 2
Int add (INT in1, int in2)
{
Return in1 + in2;
}

You can see these changes in the pre-compiled files after pre-Compilation:
1) Each compilation unit (namely, the source file hello. C and numbench. c) copies the content in the header file;
2) from the hello. I file, we can see that the macro defined in the source file hello. C # define num_1 10 is replaced with the number 10 it represents.
3) the comments in the source file hello. C are deleted from the hello. I file.
4) from the hello. I file, we can see that the constants defined in the source file hello. C still exist like normal variables, and they will always exist,
In the subsequent compilation phase, we will also check their types. We have always advocated the use of const instead of # define macro, which is based on this consideration,
All macros are processed at this step, while const constants exist. The Compiler checks const constants. If they are used incorrectly, a warning is given.


Ii. Compilation

Next, compile the numparts. I and hello. I files generated by preprocessing. You can use the following command:
# Gcc-s hello. I-O hello. s
# Gcc-s numbench. I-o numbench. s
You can also use the following name to combine preprocessing and compilation, but you can directly replace the file generated by preprocessing with the source file:
# Gcc-s hello. C-O hello. s
# Gcc-s numbench. C-o numbench. s
The Hello. s and numw.s assembly files will be generated. The contents of these two files are:
The content of the hello. s file is:
. File "Hello. c"
. Globl num_2
. Section. rodata
. Align 4
. Type num_2, @ object
. Size num_2, 4
Num_2:
. Long 15
. Text
. Globl main
. Type main, @ Function
Main:
Pushl % EBP
Movl % ESP, % EBP
Andl $-16, % ESP
Subl $16, % ESP
Movl num_2, % eax
Movl % eax, 4 (% ESP)
Movl $10, (% ESP)
Call add
Movl $0, % eax
Leave
RET
. Size main,.-Main
. Ident "GCC: (Ubuntu 4.4.3-4ubuntu5. 1) 4.4.3"
. Section. Note. GNU-stack, "", @ progbits

The content of the numbench. s file is:
. File "numbench. c"
. Text
. Globl add
. Type add, @ Function
Add:
Pushl % EBP
Movl % ESP, % EBP
Movl 12 (% EBP), % eax
Movl 8 (% EBP), % edX
Leal (% edX, % eax), % eax
Popl % EBP
RET
. Size add,.-Add
. Ident "GCC: (Ubuntu 4.4.3-4ubuntu5. 1) 4.4.3"
. Section. Note. GNU-stack, "", @ progbits
Numbench. s Compilation Code It is relatively simple. We can see that the two arguments passed in by the function are first put into the registers eax and EDX, then the two parameters are added, and finally the result is returned. This is indeed the source file numbench. content in C

Iii. Assembly

During the compilation process, the compiler reorganizes the code. After the code is compiled, it is put into the code segment, initialized constant static variables, and so on.
3.1: Compile the assembly code generated in step 2 into a machine code:
# Gcc-C hello. S-O hello. o
# Gcc-C numbench. S-O numbench. o
Here, the hello. O and numbench. O files are both target files in ELF format in Linux,
The target file mainly includes executable files, dynamic library links, and static library links. After the above steps, we can see that all the commands in the target file have been processed into languages that can be recognized by machines, some of the relocation work is not processed yet.
The target file in Linux is in the ELF format.

3.2: view the content of the target file Here, the content in hello. O is used as an example:
You can use the following command to View Details:
# Objdump-s-d hello. o
The following is an analysis of some content in the hello. o file:

3.2.1: The content of the code snippet. Text is (This section is the code snippet). The first column [] in the section is enclosed as the column offset, followed by the actual content in the Section ::
[1, 0000] 5589e583 e4f083ec 10a10000 00008944
[0010] 2404c704 240a0000 00e8fcff ffffb800
[0020] 1000000c9 C3
The content produced by disassembly of the above machine code in the. text section is as follows:
00000000 <main>:
0: 55 push % EBP
1: 89 E5 mov % ESP, % EBP
3: 83 E4 F0 and $0xfffffff0, % ESP
6: 83 EC 10 sub $0x10, % ESP
9: A1 00 00 00 00 mov 0x0, % eax
E: 89 44 24 04 mov % eax, 0x4 (% ESP)
12: C7 04 24 0a 00 00 00 movl $ 0xa, (% ESP)
19: E8 fc ff call 1A <main + 0x1a>
1e: B8 00 00 00 mov $0x0, % eax
23: C9 leave
24: C3 RET

The following comparison shows that the 55 and 89 E5 columns in the second column are. the content in text, which can be seen from the disassembly here, can also be seen in the Assembly file hello. the content in S is similar.

3.2.2: The content of the constant section. rodata is (This section usually stores some constants ):
[0000] 0f000000
Here, 0f000000 is the hexadecimal representation of 0x0f, 0x00, 0x00, 0x00, that is, the decimal 15, that is, in the source file hello. the constant defined in C: const int num_2 = 15;

3.2.3: Section. Comment content ):
0000 00474343 3a202855 62756e74 7520342e. GCC: (Ubuntu 4.
0010 342e332d 34756275 6e747535 2e312920 4.3-4ubuntu5. 1)
0020 342e342e 3300 4.4.3.


3.2.4: Use the following command for numbench. O:
# Objdump-s-d numbench. o
You can also obtain the content in the numbench. o file. The following is the original content that is not analyzed and processed. Here, repeated analysis is not performed:

Numbench. O: File Format elf32-i386

Contents of section. Text:
0000 5589e58b 109c8b55 088d0402 5dc3 U... e... u...].
Contents of section. Comment:
0000 00474343 3a202855 62756e74 7520342e. GCC: (Ubuntu 4.
0010 342e332d 34756275 6e747535 2e312920 4.3-4ubuntu5. 1)
0020 342e342e 3300 4.4.3.

Disassembly of section. Text:

00000000 <add>:
0: 55 push % EBP
1: 89 E5 mov % ESP, % EBP
3: 8B 45 0C mov 0xc (% EBP), % eax
6: 8B 55 08 mov 0x8 (% EBP), % edX
9: 8d 04 02 LEA (% edX, % eax, 1), % eax
C: 5D pop % EBP
D: C3 RET

Iv. Links
The link content is complex and will be summarized later.

 

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.