0

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 ?

cjMec
  • 133
  • 1
  • 9
  • Sounds like an exam question to me. – Michael Jul 22 '15 at 14:04
  • No . I just started learning System Programming for the new semester and was hence wondering...when I got to know about Program relocation – cjMec Jul 22 '15 at 14:07
  • It's hard to tell what your instructor means by "program relocation". The definition you gave doesn't fit how I would interpret what "program relocation" means. I think you'll need to ask your instructor if you want an answer that will be marked correct on the exam. – Ross Ridge Jul 22 '15 at 14:53
  • It's not about an answer for the exam.It's for my understanding of the topic. – cjMec Jul 22 '15 at 14:58
  • Program relocation is the one involved in the assembly process..creating Modification records for absolute programs. – cjMec Jul 22 '15 at 15:00
  • Maybe you are talking about ASLR ? If that's the case, it isn't really related to virtual memory, and their purposes are different. – ElderBug Jul 22 '15 at 15:11

2 Answers2

2

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

Pierre Lebeaupin
  • 1,103
  • 8
  • 20
1

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.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Well I actually thought of the combined work of logical memory and virtual memory together. Thank you for pointing it out ! – cjMec Jul 24 '15 at 13:45
  • What about the connection between program relocation and logical memory then ? – cjMec Jul 24 '15 at 13:48
  • They really are not connected. Relocation was the norm in the days before virtual/logical memory. If you want to run two programs at the same time on a multiuser system without logical/virtual memory, you have to be able to relocate to fit the available memory. – user3344003 Jul 24 '15 at 18:39
  • So now that we have virtual/logical memory, we don't need program relocation yes? – cjMec Jul 25 '15 at 15:50
  • Program relocation is starting to be used as a security measure. Library relocation is still critical. – user3344003 Jul 25 '15 at 15:59