From: http://bbs.pediy.com/showthread.php? T = 76876
Before that, I have been not very familiar with the details of address relocation. The following is an excerpt from the operating system principles DoS (version 2). This is the best article I have read about relocation. Share with you
.
The following describes how a program loads memory and becomes executable on a computer.
In a program written in assembly or advanced language, the subprogram and data are accessed by the symbolic name. We call the set of symbolic names in the program a "namespace ". The source program of the assembly language is compiled, or
After the source program of the advanced language is compiled, the target program is a module with "0" as the reference address. Then, multiple target modules are connected by the Connection Program into an assembly module with a unified address, so that the memory can be finally loaded
. The addresses in the target module are called relative addresses (or logical addresses), while the set of relative addresses is called "relative address space" or simply "address space ".
Although the Assembly module has a uniform address space, it still uses "0" as the reference address, that is, floating. To load it into the memory for execution, determine the actual physical address of the loaded memory, and modify
Address-related code. This process is called address relocation.
After address space programs and data are relocated, they become absolute address programs that can be directly executed by the CPU. This address set is called "absolute address space" or "storage space ".
The relative address converted from address relocation to absolute address in memory is also known as address ing (map). Based on the time of relocation, it can be divided into static location and dynamic location.
I. static relocation
Static relocation is performed before the program is executed. It directly modifies the instruction on address usage in the assembly module based on the starting position of the memory to be loaded in the assembly module.
For example, an assembly module with "0" as the reference address needs to mount a bucket with 100 as the starting address. Obviously, the program can be correctly executed only after some modifications are made before loading. For example, mov
EAX, [500] the meaning of this command is to load the storage unit content 500 with the relative address of 1234 into the EAX tiring machine. The actual address of the storage unit whose content is 1234 is 1500,
That is, add the Mount address (500) to the relative address (1000). Therefore, the direct address code in the mov eax, [500] command must also add the starting address accordingly and become MOV.
EAX, [1500].
Each instruction in the program involving a direct address must be modified in this way. The location to be modified is called the relocation item. The block start address in the actual loading module's start address modification is called the relocation factor.
To support static relocation, a relocation item table should be generated when the connection program generates a Unified Address Space and assembly module. At this time, The Connection Program does not know the actual location where the assembly module will be loaded, therefore, the relocation table
The given location to be modified is the location indicated by the relative address.
The operating system loader should load the assembly module and the relocation item table together with the memory. The relocation factor is obtained from the actual loading start address of the assembly module, and the following two steps are implemented:
(1) Get the relocation item and add the relocation factor to get the actual address of the location to be modified;
(2) modify the positioning factor of the actual address to modify the instruction code.
After the preceding two steps are performed on all relocation items, static relocation is completed, and then the program can be started for execution. Memory copies of the used relocation item table are discarded immediately.
Static relocation does not require hardware support, but has the following Disadvantages: First, after the program is relocated, it cannot be moved in the memory. Second, the storage space of the program is required to be continuous, do not place programs in
Within several discontinuous regions.
Ii. Dynamic Relocation
Dynamic Relocation refers to address relocation not before the program is executed, but during the program execution. More specifically, address translation is performed before each access to the memory unit. Dynamic Relocation allows Assembly Mode
The block is loaded into the memory without any modification, but it requires hardware-Location Register support.
When the target module of the program is loaded into the memory, the items related to the address remain unchanged from the original relative address. For example, MOV 1, [500], this command is still relative address 500. When this module is
When the Operating System Schedules execution on the processing machine, the operating system will load the actual starting address of this module minus the relative base address of the target module, and then load the difference value into the positioning register. When the CPU obtains an access request
When asked about memory commands, the address translation hardware logic automatically adds the relative address in the command to the value in the Location Register, and then uses this value as the absolute memory address to access the data in the unit.
It can be seen that the time for dynamic relocation is to dynamically perform each time before the memory is accessed during command execution. Dynamic Relocation brings two benefits:
(1) The target module does not need to be modified when it is loaded into the memory, so the correct execution will not be affected after it is loaded. This is extremely beneficial for memory compression and fragment solving;
(2) When a program is composed of several relatively independent target modules, each target module is loaded into a storage area, which may not be sequential adjacent to Kyrgyzstan, as long as each module has its own
.
The price of dynamic relocation technology is hardware support.