On the principle of c/c++ compilation (3)

-------------Foreword

I just finished reading "Advanced C/C++ Compilation Technology" in a hurry. I know that reading it once is not enough, and just reading it is not enough. I should write this blog post first to record some of my conjectures. Verified, verified and not conjecture. In the end, if there is anything wrong in the following, please correct me, keep learning and making progress!

-------------text

Let's talk about this book first. This book was written by Milan Stevanovic and translated by Lu Yusheng. The content of the structure is the hardware foundation, the life cycle of the program, the introduction of each stage in the life cycle, and the solution of various problems.

In the last article, we mentioned that the execution starting point of our program is not the entry point of the main function. Before it, there are some necessary things that need to be executed, and it is quite complicated. So what happens when we execute the file?

We said at the beginning that the execution of the program is related to the physical hardware, and we must really find the real address in order to execute it, because that address actually lives our data and operation instructions! Then the problem is, the address of our executable file is not the real physical address, so they can't find those data and instructions. Therefore, there is a loading process in the early stage of execution.

The role of the loader

Its most important function is to copy the section created by the linker into the process memory map. It does not need to know the internal structure of each section, it only needs to care whether it is readable or readable, and when the executable asks the second construction to start Whether it needs to be patched before.

loading process

The kernel loader module comes in handy when the binary format is recognized. First the loader locates the PT_INTERP section in the executable binary for the dynamic loading process (if any).

The loader then reads the header of the program's binary file segments and determines the address and byte length of each segment. It is important to point out that the loader still does not write any data to the memory map at this stage. At this stage, the loader will create and maintain a set of executable file segments, that is, the page width of each segment, and the structure associated with the program memory map.

After the program is started, the operation of copying the segment of the executable file is performed, and the virtual memory relationship between the physical memory page allocated to the process and the program memory mapping table has been established. Only when the kernel needs a certain program segment at runtime will it start to load its corresponding page. This strategy makes each part of the program only load when it is really needed at runtime.

The above are all copied from the book, sorry, I think it means literally.

Specifically how to do the above operations, we will not discuss.

Oh, by the way, I have to mention a more interesting thing, that is, the parameter passing mechanism. We have already covered calling functions, but how to pass parameters? Of course, the big guys have already designed the stack-based implementation mechanism, for example, cdecl, stdcall, fastcall, thiscall. Each convention is specially designed for specific situations from different design perspectives, so I won't talk about it here. .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325741871&siteId=291194637