Where does the address in the process come from

Source: Internet
Author: User

Write for so many years code, address this thing will use every day, so today summed up the origin of the address this thing.
This article refers to the reference to the "Programmer's Self-cultivation," a book.
First look at the following code:

#include <iostream>
#include <cstdint>

void Fun ()
{
    std::cout<< "this a fun ()" < <std::endl;
}

uint32_t a = 1;

int main ()
{
    uint32_t b = 2;
    std::cout<<a<<std::endl;
    std::cout<<b<<std::endl;
    std::cout<< (void*) fun<<std::endl;
    std::cout<<&b<<std::endl;

    return 0;
}

The results of the implementation are as follows:

1
2
0x4008ed
0x7fff391dd54c
1. Executable file generation process

Learn the principles of compiling (not learned) are aware that the executable file generation process includes: precompiled, compile, assemble, link, the final generation of the operating system can directly load the execution of the file.
[IMG1]

Now let's analyze two important links: compilation and linking. 1.1. Target file Generation

The final generation of the target file generation is done through assembler, here we do not too much delve into the workings of the assembler, we just need to know the source code through precompilation, compilation and assembly generated machine code, that is, the target file.
The target file is a common process of compiling intermediate (for large projects) in our work, and first we look at the contents of the ' TEST.O ' file generated by the sample code above.
Before that we need to know the structure of the target files under Linux.
The executable file format, called the Elf, includes the target file ('. O ', also known as a relocatable file), the executable file, the shared target file ('. so '), and the core dump file (P57).
[Img2]

The elf file itself consists of a number of segments , including:elf file headers, Cong (section Header table), symbol tables, relocation tables, string tables , and so on. Read the "C expert programming" people should know the various sections of the elf file, I should also be in school through this book to understand the ELF file structure, and later through the "Programmer's Self-cultivation" has been in-depth understanding.
The meaning of each section in the elf file (P67) is as follows:
[IMG3]

Cong is a very important structure in the elf, each segment of the elf is determined by a segment table, and the compiler, linker, and loader all locate and access the properties of each segment through the section Header table.
That's all. Let's take a look at the Cong of the compiled target file ' TEST.O ' to see the ELF file structure and content information through readelf and Objdump.
[IMG4]

Objdump listed the target file's segment table information, you can see the program code snippet size is: 0xFF, file offset is: ox0040, after the text section is the data section, size is ox04 (global variable a), file offset to ox0140.
Note Here is the VMA and LMA two fields, respectively, representing: virtual Memory address and load Memory address, and all 0 of the values of the dummy and Mount addresses (which can be considered consistent in server development). We know: * * The program is running in the virtual address space.
So much to say, is to say a little: in the target file compiled for the build, the virtual space address is not assigned .
In fact, Objdump does not list all the section information for the target file, you can list the Cong information for the target file through the Readelf tool.
[IMG5] 1.2. executable file Generation

After compiling the build target file, you need to call the system's linker to link the target file to the executable file. In simple consideration, this is explained by static links .
Now that the linker is linking to multiple target files, it is done using a similar segment merge, as shown in the following figure:
[Img6]

Now the linker generally uses the two-step link method to link:
- space and address allocation ;
Here the space and address assignment has two meanings:
-The output of the executable file space, that is, to generate executables;
-Allocates the virtual address space after the program is loaded, that is, the virtual address that is used when the runtime is allocated;
The emphasis here is that the virtual address assignment Simply initializes the starting virtual address for each segment of the segment, and does not modify the address used by the instruction in the code snippet (this change will take place in the second step of the two-step link).
You can see that the VMA for each segment of the executable file that was generated after the link has been assigned a virtual address:
[Img7]

Here needs to know: 64-bit operating System program allocation (load) of the virtual start address is 0x400000, 32-bit System program allocation (load) of the virtual start address is 0x8048000.
[Img8] Symbolic analysis and relocation;
After the linker assigns the starting virtual address to each segment, the next thing to do is to fix the code directives so that all the addresses used in the executable file are virtual addresses, not the relative offset addresses in the destination file. Instructions to modify the process of using the target file in the Segment table, symbol table, relocation table, etc., there is no in-depth understanding, these are also static linked things, this article is intended to explain the origin of the virtual address, so not too much depth.
[IMG9]
[IMG10]

Figure 1 is the disassembly of the target file code, you can see that the instructions are all relative offset, instruction parameters are false addresses. Figure 2 The chain delivered into the complete executable, the instruction offsets are all virtual addresses, and the variables and function addresses in the instruction parameters are replaced with the real virtual addresses.
You can run the elf files generated by the debug source code as follows:

Breakpoint 1, Main () at test.cpp:37          0;
(GDB) p a
$ = 1
(gdb) p &a
$ = (uint32_t *) 0x601078 <a>
(gdb) p &b
$ = (uint32_t *) 0x7fffffffe40c
(GDB) p Fun
$ = {void (void)} 0x4008ed <fun () >

The address of output A is consistent with the address of a in the executable file generated by the link, and the address of the fun function is also consistent. 1.3. Virtual address space is independent

After the executable is generated, we need to know that the virtual address space of the process is independent and that the process can use all the address space except the OS kernel region . The independence here is that the virtual addresses used within the process are not related to other processes, and a process can use 4GB (32-bit OS) to remove all other virtual address spaces used by the OS kernel, as in every process. So how does the process of separation, this can be in the next section, the process of implementation, a simple introduction.

The following is a simple structure diagram of the process address space: When each executable is generated, the virtual address space is allocated from the 0x400000 (64-bit) and the 32-bit is 0x8048000.
[Img11] 2. Execution process of executable file

From the above elaboration we know that the compilation chain delivered into the executable file has generated a virtual address , is the process of execution of the load and use of the address. Then the execution of the process eventually needs to be loaded into physical memory, so the most important logic in the run is the mapping of the virtual address to the physical address .
The following is a brief description of the implementation of the elf file:
-Create virtual address space for the process
This process simply creates a data structure for the most important page table , which maps the virtual address space to the physical address space.
Page table, each process has a page table entry , and the Process page table is used for mapping the physical page corresponding to the logical page. This ignores the process of how the operating system is paginated and how files have been loaded. When the process loads, it chooses to load some pages into memory according to the criteria, and when the page is accessed through the page table, the page is not in memory, then a page fault terminal is loaded.
[Img12]

To read the Elf file header, to establish the mapping relationship between virtual space and executable file
[IMG13]

Set the Register to the entry address of the program startup, start execution

"1" http://mqzhuang.iteye.com/blog/901602
"2" http://www.cnblogs.com/zy691357966/p/5525684.html

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.