What is the difference between position-dependent code and position-independent code?

What is the difference between position-dependent code and position-independent code?

And also how can we implement / call our own static and dynamic libraries with an example?

+3


source to share


3 answers


Position-independent code can work correctly wherever the code is loaded into memory. This is usually achieved by using relative jumps for function calls with relative jumps, the jump address is calculated from the current position in the code stream, so the code may look like this: "jump 585 bytes from the current position" or "jump 5745 bytes" from the base address of this module "instead of" go to address 0x46fae55 "Similarly, for any other instructions that refer to a memory address, you must write relative to the current position of the code or relative to the base address, which is determined at runtime.



The use of a memory management unit (MMU) and virtual memory addresses makes position-independent code almost obsolete for executables. However, shared libraries should be written as position-independent code because they can be mapped to any position in the address space of executables.

+7


source


On early computers, code was location dependent: each program was built to load and run from a specific address. To run multiple jobs at the same time using separate programs, the operator had to carefully schedule the jobs so that no two simultaneous jobs would launch programs that required the same load addresses.

For example, if both the payroll program and the receivables program were built to operate at 32K, the operator could not work at the same time. Sometimes the operator has to support several versions of the program, each of which is built for a different download address in order to expand its parameters.



To make things more flexible, position-independent code was invented. The position-independent code can work from any address to which the operator decides to upload it. Location-independent code has been used not only to coordinate user-level applications, but also in operating systems.

+4


source


To add to Lee Ryan's answer - this is not a question of c programming language, but system architecture.

eg. Intel x86 segmented architecture supports semi-automatic self-loading of small executables in .com format, where the OS can load cs = ds = es = ss up to 2 ^ 16 different values.

In the OTOH .exe format, "move" is introduced, which means there is an array of offsets (relative to the download address of the binary) in the executable file that must be appended with the download address: eg.

  relocation_table:  // list of values to be modified
          0022, 0100, ...
  .text 
  0020:   xx yy 12 00      mov ax,[0x0012]   <-- the "absolute address" 0012 is
  // located at address 0022 in the binary -- that has to be added with the real 
  // location of the the "position-independent" code

      

+2


source







All Articles