Re: When do I need PIC?



joshc wrote:
Some things that I think would dictate when I need PIC:

1) Whether or not the function references any global data because the
variables might be accessed relative to the program counter.
2) Whether or not the function calls any other functions because again
the branches could be relative vs absolute.

Are the above two points correct, and what else do I need to worry
about? As you can probably tell this is in the context of an embedded
system.

Thanks.
[The whole point of PIC is so you can move code from one place to
another without having to modify it. If you want to be able to
relocate the code, use PIC, if you don't, don't. -John]

The point of my question was that there are some cases where you can
get away with not compiling code as position independent because due
to the function's nature the compiler generates no code that is
position-relative for that function. In fact, I have done this with
some code that I am currently using and am able to relocate it without
specifically compiling it as position independent.

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.

Thanks.
[This is entirely dependent on your target machine's architecture.
If it uses base register for data references and jumps are either
relative or also off base registers, a lot of code is probably PIC
without making special efforts. If the instruction set has a lot
of direct addresses, it's not. -John]

.



Relevant Pages

  • Re: Possible to determing if .o was compiled as PIC?
    ... following is the compiler gobbledygook on this issue. ... > The best way to determine whether any object is position independent ... if you ever see any absolute relocations in .rela.text like: ... So does the linker know for sure whether or not a particular object file ...
    (comp.unix.solaris)
  • Re: Possible to determing if .o was compiled as PIC?
    ... following is the compiler gobbledygook on this issue. ... The best way to determine whether any object is position independent ... if you ever see any absolute relocations in .rela.text like: ... there are relocations that can be resolved at link time ...
    (comp.unix.solaris)
  • Position Independent ARM Assembly coding
    ... We are observing some discrepancies in the way assembler and compiler ... for Position independent coding. ... Why Assembler is not making the Code position independent like the ...
    (comp.sys.arm)
  • Re: I apologise perhaps for a simple question
    ... It won't do you any harm to set these changes as part of a template, ... you have to use the 'volatile' keyword so that the compiler ... knows that global data values really do change. ... lead to problems at high optimization levels, though i have to say VC2003 ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Volatile for shared data protected by critical section
    ... I don't think that it optimizes register variable use across function calls. ... seeing 'volatile' used much with globals in the CE code base. ... Does the current compiler implementation not attempt to optimize variables ... You are right that the global data will be shared by multiple threads ...
    (microsoft.public.windowsce.embedded)