Re: When do I need PIC?

russell kym horsell <>
3 Aug 2006 10:52:23 -0400

          From comp.compilers

Related articles
When do I need PIC? (joshc) (2006-07-28)
Re: When do I need PIC? (Vincent Zweije) (2006-07-29)
Re: When do I need PIC? (joshc) (2006-07-31)
Re: When do I need PIC? (russell kym horsell) (2006-08-03)
Re: When do I need PIC? (joshc) (2006-08-04)
Re: When do I need PIC? (glen herrmannsfeldt) (2006-08-09)
| List of all articles for this month |

From: russell kym horsell <>
Newsgroups: comp.compilers
Date: 3 Aug 2006 10:52:23 -0400
Organization: Central Iowa (Model) Railroad, Plano, TX, USA
References: 06-07-092 06-07-116
Keywords: code, architecture
Posted-Date: 03 Aug 2006 10:52:23 EDT

joshc <> wrote:
> In my situation it is a bit of a pain if I have to compile my one
> particular function as position independent so I was just looking for
> things that would force me to have to compile the function as PIC to
> be able to relocate it. I know I could just try not to compile it as
> PIC and look at the assembly emitted by the compiler but was looking
> for input from compiler gurus.

Your original question has pretty much been answered. But maybe you
really are asking the quetion
"how can I tell whether my code is already position independent?".

There is no guarantee that a general compiler will generate code that
is actually PIC unless asked to do so.

E.g. there is no real "style for writing C code" that will "always"
result in the output asm/binary being PIC. In any case,
position-indep code generally has special settings in the header parts
of the object files, so a linking loader "knows" they are supposed to
be PIC (although no loader I know of actually tests that the code
ACTUALLY is position indep -- the test would be too hard, in general,
anyway). Unless you ask for -pic (or whatever) these bits will not be

There may be a style of writing C code (say) that will *tend* to
result in position-independ code, given the output .o file will not
claim to be position indep.

E.g. your code must use only local variables or registers for
everything. It's OK to have params to functions, but no function may
refer to any other function -- kind of a big restriction :). No
testing for "errno" or suchlike, either -- they are either global
variables or test functions, depending on platform.

You might ASSUME that loops will tend to get compiled into relative
branch instructions, but usually on some architectures the range of
relative branches is small. You'd need to know "if my while loop is
more than X lines it's unlikely to use a relative branch at the
top/botom". And explicit "gotos" are bound to use absolute addrs, so
they would be out.

The best idea would probably be to create an edit script (e.g.) to look
at the asm output and extract "features" (a la AI probs) that indicate
the code is NOT position indep. E.g. sometimes "@$" or somesuch indicates
a ref to an absolute address. Most (all?) refs to abs addrs indicate non-PIC
code. But there are bound to be a load of tricks.

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.