473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

position independet code

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 ....
Nov 14 '05 #1
5 2189
junky_fellow wrote:
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.
Nov 14 '05 #2

"junky_fellow" <ju**********@yahoo.co.in> wrote in message
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.
Nov 14 '05 #3
"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:41**********************@news.wanadoo.fr...
junky_fellow wrote:
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.


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!

Nov 14 '05 #4
xarax wrote:

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.
Nov 14 '05 #5
>>junky_fellow wrote:
What is a position independent code ? What are the basic guidelines to
keep in mind while writing a position independent code ?

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.
"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:41**********************@news.wanadoo.fr.. .
Position independent code is assembly that can be loaded at any address
for execution.

In article <kO******************@newsread1.news.pas.earthlink .net>,
xarax <xa***@email.com> wrote: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. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: DaWoE | last post by:
Hi, I want to place a div centered of another div in IE. I get this to work in Mozilla, but not in IE. The code i use for mozilla is the following : <html> <head> <style type="text/css">
8
by: Jaime Rios | last post by:
Hi, I created a COM AddIn for Word that performs the functions that it needs to, but I needed to add the ability for the toolbar created by the COM AddIn to remember it's last position and...
19
by: derelicten | last post by:
hello , I have an issue positionating some pics I want to anchor to an existing table cell but I cant just place regular position on the cell because the background is fixed height and the set of...
5
by: Roderik | last post by:
Hi, My logo images are positioned some more to the right in IE6 than in FF1.5. I like them to be as shown in FF1.5. Does anyone have a clue, why they are positioned differently in IE? Problem...
2
by: petermichaux | last post by:
Hi, It seems like determining element position in a web page is a difficult task. In the position reporting source code I've looked at there are special fixes for at least some versions of...
4
by: Petra Meier | last post by:
Hi, I use the common code to determine the position of an element getTop = function(o){ var nTop = null; if(o){ do{ nTop += o.offsetTop;
2
roula
by: roula | last post by:
Hi, I have this code of some divs, and I am trying to put a transparent image in div #transp to cover a certain area of the parent div #main (in order to make an image map of the background image...
2
by: agbee1 | last post by:
Hello: I've finally made the effort to ween myself from overly using tables and use CSS for my positioning. However, I am having a problem with my navigational menu properly aligning in Firefox,...
1
by: spynx | last post by:
import javax.swing.JOptionPane; import java.io.*; import java.util.Arrays; public class election5 { public static void main( String args ) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.