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

The structural system of the program (10)

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

Share

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

Let's look at the structure of the program in this section. In the program we usually write, in fact, its structure is worth studying, which helps us to write more efficient code, and also helps us to understand the architecture of the entire executable file. All right, let's cut the crap. So what exactly is our program made of? It is composed of different segments, including code segments, data segments, and so on.

The program has two characteristics, static and dynamic. Static features refer to instructions and data, while dynamic features refer to actions that execute instructions to process data. Let's take a look at the correspondence between the program source code and the executable program file, as shown in the following figure

We have talked about this before in the C language, and today we review it again. From the figure above, we can see that initialized global variables and initialized static-decorated local variables are stored in .data section, while uninitialized variables are stored in .bss section, and functions are stored in .text section.

Let's introduce the above sections one by one. The first is the code snippet (.text), which has the following characteristics.

1. The executable statements in the source code are compiled into the code snippet

2. Code snippets have read-only attributes in systems with memory management units

3. The size of the code snippet is fixed after compilation (cannot be changed dynamically)

4. Code snippets can contain constant data (such as constant strings).

The second is the data segment, which includes (.data, .bss, .rodata). Data segments are used for variables with global lifetimes in source code everywhere, where .bss segments store variables that are not initialized (or initialized to 0),. Data segments store variables with initial values other than 0, and .rodata stores variables modified by the const keyword. So let's think about it: we said earlier that initialized and uninitialized global variables and static local variables are stored separately, why is it so complicated? Generally, after the program is loaded, all memory units in the. bss section are initialized to 0, and the initial values related to the .data section in the program file are written to the corresponding memory unit. Because the variables in the .bss section do not need to save the initial values in the program file, the size of the executable file is reduced and the loading efficiency of the program is improved.

Next, let's take the code as an example to analyze and illustrate.

Int g_var_v = 1int g_main () {static no_var_v1; static var_v2 = 2; return 0;}

Let's compile to see the results.

We see 8 bytes each in the .data and .bss segments. Next, let's change the type of the global variable g_no_var_v to char, and then look at the result.

We see that the result is still 8 because it is four-byte aligned, so the result is still 8, if we change to two char types, then the result is 4. The entry address of g_main () is the starting address of the .text segment, which proves once again that our program specifies the entry function itself.

Let's take a look at the Stack. The essence of the stack in the program is a piece of continuous memory space, and the SP register acts as the "pointer" at the top of the stack to realize the stack operation and the stack operation. As shown in the following figure

The functions of stacks are roughly as follows:

1. When an interrupt occurs, the stack is used to save the value of the register.

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

3. In concurrent programming, each thread has its own independent stack.

Let's take a look at the Heap, which is a piece of "idle" memory space, which is designed to provide dynamic memory allocation; of course, the allocation of heap space needs the support of the malloc function, and it also needs to manually free the space with the help of the free function after use. Let's take a look at the memory-mapped segment (Memory Mapping Segment). In the kernel, the contents of the hard disk file are directly mapped to the memory-mapped segment (mmap), and the dynamic link library is mapped to the memory-mapped segment when the executable program is loaded, so that the program can create an anonymous mapping area to store program data during execution.

Let's briefly introduce the principle of memory-mapped files:

1. Logically map the file data on the hard disk to memory (zero time consuming)

2. Actual loading of file data through a page fault interrupt (a data copy)

3. The read and write of the mapped memory is the read and write of the file data.

The mapping relationship is as follows:

In the structural system of the program, its overall distribution is shown in the following figure

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