cjMec
cjMec

Reputation: 133

Virtual/Logical Memory and Program relocation

Virtual memory along with logical memory helps to make sure programs do not corrupt each others data.

Program relocation does an almost similar thing of making sure that multiple programs does not corrupt each other.Relocation modifies object program so that it can be loaded at a new, alternate address.

How are virtual memory, logical memory and program relocation related ? Are they similar ? If they are same/similar, then why do we need program relocation ?

Upvotes: 0

Views: 550

Answers (2)

user3344003
user3344003

Reputation: 21710

Virtual memory does not prevent programs from interfering with out other. It is logical memory that does so. Unfortunately, it is common for the two concepts to be conflated to "virtual memory."

There are two types of relocation and it is not clear which you are referring to. However, they are connected. On the other hand, the concept is not really related to virtual memory.

The first concept of relocatable code. This is critical for shared libraries that usually have to be mapped to different addresses.

Relocatable code uses offsets rather than absolute addresses. When a program results in an instruction sequence something like:

JMP SOMELABEL
. . . 
SOMELABEL:

The computer or assembler encodes this as

JUMP the-number-of-bytes-to-SOMELABEL

rather than

JUMP to-the-address-of-somelabel.

By using offsets the code works the same way no matter where the JMP instruction is located.

The second type of relocation uses the first. In the past relocation was mostly used for libraries. Now, some OS's will load program segments at different places in memory. That is intended for security. It is designed to keep malicious cracks that depend upon the application being loaded at a specific address.

Both of these concepts work with or without virtual memory.

Note that generally the program is not modified to relocated it. I generally, because an executable file will usually have some addresses that need to be fixed up at run time.

Upvotes: 1

Pierre Lebeaupin
Pierre Lebeaupin

Reputation: 1113

Relocatable programs, or said another way position-independent code, is traditionally used in two circumstances:

  • systems without virtual memory (or too basic virtual memory, e.g. classic MacOS), for any code
  • for dynamic libraries, even on systems with virtual memory, given that a dynamic library could find itself lodaded on an address that is not its preferred one if other code is already at that space in the address space of the host program.

However, today even main executable programs on systems with virtual memory tend to be position-independent (e.g. the PIE* build flag on Mac OS X) so that they can be loaded at a randomized address to protect against exploits, e.g. those using ROP**.

* Position Independent Executable
** Return-Oriented Programming

Upvotes: 2

Related Questions