Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Program Compiler system in C language

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the example analysis of the program compilation system in C language, which is very detailed and has a certain reference value. Interested friends must read it!

Translation environment and execution environment of the program

In any implementation of ANSI C, there are two different environments:

The first is the translation environment, where the source code is converted into executable machine instructions.

The second is the execution environment, which is used to actually execute code.

How does a .c file become an .exe executable file? The following picture is a rough process:

Compile and link translation environment

Each source file that makes up a program is converted into object code (object code) through the compilation process.

Each object file is bundled together by a linker to form a single-but complete executable program.

The linker also introduces any functions used by the program in the standard C function library, and it can search the programmer's personal library and link the functions it needs to the program.

Several stages of compilation

Next, I'll use the Linux platform to show you the three processes of compilation:

Let's write a simple C program first:

Then execute an instruction like this:

Gcc test.c

This instruction is for gcc, the compiler, to compile our code. After executing this instruction, we will find that an executable file like a.out will be generated.

Let's execute the following instruction:

. / a.out

So we can execute the executable file.

To give you a better sense of the compilation process, let's take a step-by-step look at it:

Pretreatment

Let's execute the following instruction to stop the code after preprocessing:

Gcc-E test.c-o test.i

This instruction means to output the preprocessed information to a test.i file.

What you can find is that there is an extra test,i file, which we can open and have a look at:

What can be found is that three points have changed:

The header file is expanded

The macro was replaced by text

The comment was deleted.

We do a processing on the original code, do not include the header file of stdio.h, we write a header file ourselves:

Let's take a look at what the preprocessed file looks like:

The effect is the same as above.

So a few actions of preprocessing

The inclusion of header files

Completion of preprocessing instructions (eg:#define, # pragma... )

Deletion of comments

Compile

Execute the following instruction to compile the file to form the assembly code:

Gcc-S test.c

After execution, you can produce a test.s file, which we can open and have a look at:

This is actually the assembly code.

So a few actions of compilation

Grammar analysis

Lexical analysis

Semantic analysis

Symbol summary

Symbol summary: the symbol summary is the global symbol. For example, our wharf file above summarizes an Add,.c file, an Add and main.

Compilation

Next, let's execute this instruction:

Gcc-c test.c

Assemble the source file, resulting in an object file for test.o:

When we open this file, we will find that it is a binary file that we do not understand:

So assembly is actually converting assembly code into binary code (machine instructions).

This process also does one thing-- to form a symbol table.

Link

Two things that links do

Merge segment table

Merging and relocation of symbol tables

Under the Linux system, test.o binaries are organized in a format like elf.

Elf organizes the file into a segment. Both test.o and Add.o have a paragraph, so how can we read files in elf format?

We have a tool called readelf, which can read such a file, so we enter a command like this:

Readelf test.o-a

We can really see the existence of such a paragraph.

Then there is a summary of the symbol table below:

In fact, the a.out file is also in elf format, so in fact, the link is to merge the segment tables of these elf files, and then the Add function in test has an address.

Operation environment

The process of program execution:

The program must be loaded into memory. In an environment with an operating system: this is usually done by the operating system. In a separate environment, the loading of the program must be arranged manually, or it may be done by placing the executable code into read-only memory.

The execution of the program begins. The main function is then called.

Start executing the program code. At this point the program will use a runtime stack (stack) to store the function's local variables and return address. Programs can also use static memory, and variables stored in static memory retain their values throughout the program's execution.

Terminate the program. The main function is terminated normally; it may also be terminated unexpectedly.

The above is all the contents of the article "sample Analysis of Program Compiler system in C language". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report