11

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

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

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
pinky
  • 129
  • 1
  • 4
  • I'm sorry, but you could very easily use a search engine to find information on this. Please do your own research. If you have a specific question that you can't find the answer to on your own, then come back and post it here. – Jonathon Reinhart Feb 04 '13 at 05:35

3 Answers3

11

In early computers, code was position-dependent: each program was built to be loaded into, and run from, a particular address. In order to run multiple jobs using separate programs at the same time, an operator had to carefully schedule the jobs so that no two simultaneous jobs would run programs that required the same load addresses.

For example, if both the payroll program and the accounts receivable program were built to run at address 32K, the operator could not run both at the same time. Sometimes, an operator would keep multiple versions of a program around, each built for a different load address, to expand his options.

To make things more flexible, position-independent code was invented. Position-independent code could run from any address at which the operator chose to load it. Position-independent code has been used not only to coordinate the work of user-level applications, but within operating systems as well.

11

Position independent code can run correctly wherever the code is loaded in the 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 might look like: "jump 585 bytes from the current position" or "jump 5745 bytes from the base address of this module" instead of "jump to address 0x46fae55". Likewise for any other instructions that references memory address has to be written relative to the current code position or relative to a base address that is determined at runtime.

The use of Memory Management Unit (MMU) and virtual memory address makes position independent code almost obsolete for executables. Shared libraries though, have to be written as position independent code because they can be mapped to any position in the address space of the executables.

Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • Also data has to be accessed relative to the location of the code (e.g. relative to the instruction pointer/program counter). – Alexey Frunze Feb 04 '13 at 05:50
  • @Lie Ryan, But even with MMU , the shared libraries still need position independent code , right? – Lunar Mushrooms Feb 04 '13 at 06:24
  • @FUZxxl: I've clarified the last paragraph – Lie Ryan Feb 04 '13 at 13:11
  • 1
    @Lie «The use of Memory Management Unit (MMU) and virtual memory address makes position independent code almost obsolete for executables» I disagree. PIE (Position independent executables) are a way to enhance the security of code as injected shell code may make even less assumptions about the whereabouts of the code. – fuz Feb 04 '13 at 13:16
3

To add to the answer of Lie Ryan -- This is not a matter of c programming language, but of the system architecture.

E.g. intel x86 segmented architecture supported semi-automatically position independent loading of small executables in .com format, where the OS could load cs=ds=es=ss to 2^16 different values.

The .exe format OTOH introduced 'relocation', which meant that in the executable there is an array of offsets (relative to the loading address of the binary), that has to added with the loading 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
Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57