position independet code

J

junky_fellow

What is a position independent code ? What are the basic guidelines to
keep in mind while writing a position independent code ?

thanx for any help ....
 
J

jacob navia

junky_fellow said:
What is a position independent code ? What are the basic guidelines to
keep in mind while writing a position independent code ?

thanx for any help ....
Position independent code is assembly that can be loaded at any address
for execution.
There is no way you can do that in C, since the objective of writing in
C is to avoid writing in assembly language. Some compilers support this,
and there are compile time flags to direct the compiler to generate that
kind of code.
 
M

Malcolm

junky_fellow said:
What is a position independent code ? What are the basic guidelines to
keep in mind while writing a position independent code ?
Imagine this (pseudo assembly)

// increments accumulator if not equal to X register

8000 CMP A, X // compare accumulator to x register
8001 JMPZ 8003 // jump if equal to 8003
8002 INC A // increment A
8003 RET // returns

Ad this

8000 CMP A, X
8001 BRNZ +2 // BRNZ branches forwards or backwards by a set number
8002 INC A
8003 RET

The second will work if moved to a new position, but the first has the jump
destination hardcoded and will not.
 
X

xarax

jacob navia said:
Position independent code is assembly that can be loaded at any address
for execution.

It's more than that. Position independent code can be
loaded at any address and later it can be moved/copied
(in its entirety) to another address, and it will run
correctly from that new address.

Most compile/link procedures will generate internal
constants in the object code that are called "relocatable
address constants" (RAC). When a program is loaded into
memory for execution, the loader will "fix up" all of
the RAC fields by adding the base address of the program
to each RAC field, thus yielding a "position dependent"
address constant field that the program can use.

If such a program were copied to another memory location,
the RAC fields would have the old address values and the
program would misbehave in strange and usually disastrous
ways.

A position independent program does not have RAC fields.
Thus, it can be loaded at one memory address and later
copied to another memory address, and it will still run
at the new address. All memory references in the program
are calculated at run-time according to its current
location in memory, rather than loading RAC fields.
There is no way you can do that in C, since the objective of writing in
C is to avoid writing in assembly language. Some compilers support this,
and there are compile time flags to direct the compiler to generate that
kind of code.

There is nothing in the C language that pertains to
position dependent/independent programs, or to
programs that can loaded anywhere (also called
"relocatable") or must be loaded at a specific memory
address (usually device drivers must live at a specific
memory address and refer to specific memory locations
which outside the C language).


--
----------------------------
Jeffrey D. Smith
Farsight Systems Corporation
24 BURLINGTON DRIVE
LONGMONT, CO 80501-6906
http://www.farsight-systems.com
z/Debug debugs your Systems/C programs running on IBM z/OS for FREE!
 
J

jacob navia

xarax said:
It's more than that. Position independent code can be
loaded at any address and later it can be moved/copied
(in its entirety) to another address, and it will run
correctly from that new address.

Yes, you are right, my wording is misleading.
Position independent code can be relocated an undefinite number of
times.

Thanks for this clarification.
 
C

Chris Torek

I was not going to say anything, because (as xarax notes) Standard
C insulates one entirely from the position-independence issues:
you write your code in Standard C and it "just works". If you need
the stuff that PIC may provide, you will not be writing Standard
C, and any non-standard compiler flags (such as -fpic) and/or
C-callable functions (such as mmap()) that you may need are, well,
non-standard -- you will have to investigate the details elsewhere.

It's more than that. Position independent code can be
loaded at any address and later it can be moved/copied
(in its entirety) to another address, and it will run
correctly from that new address.

PIC *used* to mean that, back when I wrote such things by hand.

These days the term has been taken over, at least on Unix-like
systems, to refer to code that is relocatable but not actually
"position independent", even though it will be run inside a
virtual-memory process, so that a dynamic loader can load new code
at run-time. GCC's "-fpic" and/or "-fPIC" flags (which may or may
not have subtly different meanings, depending on processor architecture
-- typically the uppercase version gives you larger displacements,
if "small+fast" and "large+slow" are both offered on the architecture)
tell it to build dynamic-relocation tables for this sort of
not-really-position-independent-code.

In other words, the meaning of "PIC" is now at best muddy: not only
is it off-topic in comp.lang.c, but different people will have
different ideas of what it *should* mean, on different systems. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top