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

In-depth understanding of the structure of the program

2025-02-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In-depth understanding of the structure of the program 1 segments in a static executable program

The program consists of different segments (code segments, data segments). The static characteristics of the program are instructions and data, and the dynamic characteristics are the execution of instructions to deal with data.

The correspondence between the source code and the executable program

The executable statement of the code snippet source code is compiled into the code snippet. After compilation, the fixed size code snippet has a read-only attribute in the system of the memory management unit (a protection against being rewritten). The code segment can include constant data (such as string constants) data segments (.data .bss .rodata)

.bss paragraph (Block Started by Symbol segment)

Stores uninitialized global data without taking up the size of the executable file.

.data section: stores variables with non-zero initial values

.rodata section: stores const decorated and other read-only data

Question: .bss and .data segments also store global data, so why are initialized and uninitialized stored in different segments?

The variables in the .bss section do not need to save the initial values in the program file, so as to reduce the size of the executable program and improve the efficiency of program loading. (for variables in the .bss section, only the variable name and type need to be saved in the executable file and initialized to 0 when loading, while for the data stored in the .data section, the variable name, type, and value need to be saved, and the variable value needs to be copied to the corresponding space when loading)

Programming experiment: you can write a simple test program, use the objdump-h command to view the information of each segment, use the nm command to view the symbols and addresses in the executable file, use objdump-s-j. Data. / a.ou to view the specific information in a segment, and correspond to the above information.

6.2 Segment stack generated after dynamic loading (stack)

Stack time is generated after the program is loaded into memory, its essential time slice continuous storage space

The SP register acts as the "pointer" at the top of the stack to realize the stack operation and unstack operation.

Stacks are usually used for the following purposes

When an interrupt occurs, the stack is used to save the value of the register (field protection)

When a function is called, the stack is used to hold the activity record of the function (stack frame information).

In concurrent programming, each thread has its own independent stack

Heap (Heap)

The stack is just like the stack when the program is loaded into memory before it is generated. Is a piece of "free space" to provide dynamic memory allocation.

Function support is required (malloc, free)

Memory mapped segment (memory mapping segment)

The kernel maps the contents of files in the hard disk directly to memory-mapped segments (mmap)

Dynamic link libraries are mapped to memory-mapped segments when the executable is loaded

When the program is executed, you can create an anonymous mapping area to store program data.

The principle of memory-mapped files:

The file data on the hard disk is logically mapped to memory (zero time-consuming), and the actual loading of the file data is carried out through a page fault interrupt (a data copy). After mapping, the reading and writing of the memory is the reading and writing of the file (which greatly improves the reading and writing efficiency of the file).

Using the traditional way to read the file through the read function, first the kernel program receives the request of the application program, then the kernel program reads the file kernel space in the hard disk, and then copies the data in the kernel space to the application space (using two data copies).

The final layout of each segment in memory:

Here we see that the stack and the starting address of the heap are random, and the purpose of doing this is for security. When the address is random, the darkness of the malicious code modification program becomes larger (it is difficult to obtain the return address directly through the fixed address).

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

Servers

Wechat

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

12
Report