What are relocatable programs and what makes a program relocatable? From the OS memory management context, why programs (processes) need to be relocatable?
2 Answers
There is position independent code and position dependent code. Position independent code does not depend upon where it is located in memory. Position independent code is generally desirable. There are a lot of techniques processor/compiler-assembler/linker/loader combinations use to generate position independent code.
If you do something like:
extern int b ;
int a = &b;
the code is inherently not position independent because the assignment depends upon where b has been loaded (however, this situation occurs so commonly, that linkers and loaders have ways of dealing with this).
If a program or shared library only contains position independent code it can be loaded anywhere in memory and is relocatable.
Let's suppose you have a program P that is linked to shared libraries L1 and L2. If L1 and L2 require using the same location in memory, then they cannot be loaded together and P cannot run.

- 20,574
- 3
- 26
- 62
Most programs can be relocated. If the program contains relative addresses of its data then it can be placed anywhere in memory. It it contains absolute addresses then the loader will adjust these addresses in the code when loading the program into memory. http://linker.iecc.com/

- 4,360
- 1
- 30
- 32
-
1Your explanation is good :). However, you explanation is a way in which all programs can be relocated? What do you mean – Mikeez Apr 13 '17 at 20:50
-
1Yes. All normal programs can be relocated. In many cases, however, the program is not relocated but the OS is using virtual address translation to place the program at the virtual address it wants even if the physical address is somewhere else. Relocation is necessary if the same process has multiple programs, DLLs, or Shared objects because they must have different virtual addresses as well. – A Fog Apr 15 '17 at 05:20
-
Thank you A Fog :) – Mikeez May 07 '17 at 05:19