[Operating System] Loading and linking of programs

Program loading and linking

insert image description here

To run a user program in the system, it must be loaded into the memory first, and then converted into an executable program. The processing steps of the user program can be divided into three steps (compile, link, load):

insert image description here

program compilation

insert image description here

program link

After the source program is compiled, a set of target modules can be obtained. The function of the linker is to assemble this set of object modules and the library functions they need into a complete load module . When linking the target module, the linking methods can be divided into three types according to the linking time.

static link

insert image description here

Static linking: Before the program runs, the target modules and their required library functions are linked into a complete executable file (loaded module), and then they are not disassembled .

Dynamic linking at load time

insert image description here

Dynamic linking when loading: When each target module is loaded into the memory , the linking method is linked while loading .

runtime dynamic linking

insert image description here

Runtime dynamic linking: When the target module is needed during program execution , it is linked . Its advantage is that it is easy to modify and update, and it is easy to realize the sharing of target modules.

program loading

absolute load mode

Absolute loading: At compile time, if you know where the program will be placed in memory, the compiler will generate the object code at the absolute address . The loader loads the program and data into the memory according to the address in the loaded module, that is, the command to load the module obtained after compiling and linking directly uses the absolute address. Absolute loading is only applicable to a single program environment .

It should be noted that the absolute address used in the program can be given when compiling or assembling , or directly given by the programmer . Usually, it is converted to an absolute address when compiling or assembling.

relocatable load mode

Static relocation : Also known as relocatable loading. The address of the loaded module after compiling and linking starts from 0, and the address used in the instruction and the address where the data is stored are logical addresses relative to the starting address . The load module can be loaded into the appropriate location of the memory according to the current situation of the memory. The address is " relocated " when loading , and the logical address is transformed into a physical address (the address transformation is completed once when loading).

insert image description here

As shown in the figure above, the starting physical address of loading is 10000, and all address-related parameters are added by 10000, so the variable changes from logical address 2500 to physical address 12500.

The characteristic of static relocation is that when a job is loaded into memory, all the memory space it requires must be allocated . If there is not enough memory, the job cannot be loaded. Once a job enters the memory, it cannot be moved during operation, nor can it apply for memory space, because the address used in the instruction has been determined when it is loaded.

Dynamic runtime loading method

Dynamic relocation : Also known as dynamic runtime loading. The address of the loaded module after compiling and linking starts from 0. After the loader loads the load module into the memory, it does not immediately convert the logical address into a physical address, but postpones the address conversion until the program is actually executed. Therefore, all addresses after loading into memory are still logical addresses . This method requires the support of a relocation register . The relocation register is used to store the starting position of the loaded module . When the instruction is executed, the target logical address in the instruction will be added to the starting position in the relocation register . In order to obtain the corresponding physical address .

When dynamic relocation is used, the program is allowed to move in the memory, that is, when the movement occurs, only the value of the corresponding starting position in the relocation register needs to be modified.

The characteristics of dynamic relocation: the program can be allocated to discontinuous storage areas ; before the program runs, it only needs to load part of its code to start running, and then during the running of the program, dynamically apply for memory allocation according to the needs; it is convenient for the program The sharing of segments can provide users with an address space much larger than the storage space.

Guess you like

Origin blog.csdn.net/zzy_NIC/article/details/121123483